From f3c228e2152daa0872c77bbda72d9db82ea54336 Mon Sep 17 00:00:00 2001 From: m8pple Date: Mon, 27 Oct 2014 11:24:03 +0000 Subject: Added driver programs for the fragments. --- fragments/run_addu.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++++ fragments/run_fibonacci.cpp | 63 ++++++++++++++++++++++++++++++++ makefile | 5 +++ 3 files changed, 155 insertions(+) create mode 100644 fragments/run_addu.cpp create mode 100644 fragments/run_fibonacci.cpp diff --git a/fragments/run_addu.cpp b/fragments/run_addu.cpp new file mode 100644 index 0000000..effa24e --- /dev/null +++ b/fragments/run_addu.cpp @@ -0,0 +1,87 @@ +#include "mips.h" + +#include "f_addu.c" + +int main(int argc, char *argv[]) +{ + const char *srcName="f_addu-mips.bin"; + + if(argc>1){ + srcName=argv[1]; + } + + mips_mem_h m=mips_mem_create_ram(0x20000, 4); + mips_cpu_h c=mips_cpu_create(m); + + FILE *src=fopen(srcName,"rb"); + if(!src){ + fprintf(stderr, "Cannot load source file '%s', try specifying the relative path to f_addu-mips.bin.", srcName); + exit(1); + } + + uint32_t v; + uint32_t offset=0; + while(1==fread(&v, 4, 1, src)){ + if(mips_mem_write(m, offset, 4, (uint8_t*)&v)){ + fprintf(stderr, "Memory error while loading binary."); + exit(1); + } + offset+=4; + } + fprintf(stderr, "Loaded %d bytes of binary at address 0.\n", offset); + + fclose(src); + + // No error checking... oh my! + + /////////////////////////////////////////////////////// + // we can start the program at the addu instruction + + uint32_t a=63, b=45; + + mips_cpu_set_pc(c, 4); // Place the program counter on the addu instruction + + mips_cpu_set_register(c, 4, a); // Set first input + mips_cpu_set_register(c, 5, b); // Set second input + + mips_cpu_step(c); // Execute the addu at address 4 + + uint32_t sum; + mips_cpu_get_register(c, 2, &sum); // Get the result back + + uint32_t sum_ref=a+b; + fprintf(stderr, "%d + %d = %d, expected = %d\n", a, b, sum, sum_ref); + if(sum_ref!=sum){ + fprintf(stderr, "Sum is wrong.\n"); + exit(1); + } + + + /////////////////////////////////////////////////////////////// + // Or we can execute the whole thing as a proper MIPS function call + + a=10000; + b=34239809; + + uint32_t sentinelPC=0x10000000; + + mips_cpu_reset(c); // get back to a zero state, with pc=0 + + mips_cpu_set_register(c, 31, sentinelPC); // set function return address to something invalid just in case + mips_cpu_set_register(c, 4, a); // Set first function input (MIPS calling convention) + mips_cpu_set_register(c, 5, b); // Set second input argument (MIPS calling convention) + + mips_cpu_step(c); // execute the jr at address 0 + mips_cpu_step(c); // execute the addu in the delay slot at address 4 + + mips_cpu_get_register(c, 2, &sum); // Get function result back (MIPS calling convention) + + sum_ref=a+b; + fprintf(stderr, "%d + %d = %d, expected = %d\n", a, b, sum, sum_ref); + if(sum_ref!=sum){ + fprintf(stderr, "Sum is wrong.\n"); + exit(1); + } + + return 0; +} diff --git a/fragments/run_fibonacci.cpp b/fragments/run_fibonacci.cpp new file mode 100644 index 0000000..041d667 --- /dev/null +++ b/fragments/run_fibonacci.cpp @@ -0,0 +1,63 @@ +#include "mips.h" + +#include "f_fibonacci.c" + +int main(int argc, char *argv[]) +{ + const char *srcName="f_fibonacci-mips.bin"; + + if(argc>1){ + srcName=argv[1]; + } + + mips_mem_h m=mips_mem_create_ram(0x20000, 4); + mips_cpu_h c=mips_cpu_create(m); + + FILE *src=fopen(srcName,"rb"); + if(!src){ + fprintf(stderr, "Cannot load source file '%s', try specifying the relative path to f_fibonacci-mips.bin.", srcName); + exit(1); + } + + uint32_t v; + uint32_t offset=0; + while(1==fread(&v, 4, 1, src)){ + if(mips_mem_write(m, offset, 4, (uint8_t*)&v)){ + fprintf(stderr, "Memory error while loading binary."); + exit(1); + } + offset+=4; + } + fprintf(stderr, "Loaded %d bytes of binary at address 0.", offset); + + fclose(src); + + // No error checking... oh my! + + uint32_t n=12; // Value we will calculate fibonacci of + + uint32_t sentinelPC=0x10000000; + + mips_cpu_set_register(c, 31, sentinelPC); // set return address to something invalid + mips_cpu_set_register(c, 4, n); // Set input argument + mips_cpu_set_register(c, 29, 0x1000); // Create a stack pointer + + uint32_t steps=0; + while(!mips_cpu_step(c)){ + fprintf(stderr, "Step %d.\n", steps); + ++steps; + uint32_t pc; + mips_cpu_get_pc(c, &pc); + if(pc==sentinelPC) + break; + } + + uint32_t fib_n; + mips_cpu_get_register(c, 2, &fib_n); // Get the result back + + uint32_t fib_n_ref=f_fibonacci(n); + + fprintf(stderr, "fib(%d) = %d, expected = %d\n", n, fib_n, fib_n_ref); + + return 0; +} diff --git a/makefile b/makefile index 93075cb..d1aec94 100644 --- a/makefile +++ b/makefile @@ -24,3 +24,8 @@ USER_CPU_SRCS = \ USER_CPU_OBJECTS = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(USER_CPU_SRCS))) src/$(LOGIN)/test_mips : $(DEFAULT_OBJECTS) $(USER_CPU_OBJECTS) + +fragments/run_fibonacci : $(DEFAULT_OBJECTS) $(USER_CPU_OBJECTS) + +fragments/run_addu : $(DEFAULT_OBJECTS) $(USER_CPU_OBJECTS) + -- cgit