From 6583a09d5f6bc673f7a664d47120f19827d2769c Mon Sep 17 00:00:00 2001 From: zedarider Date: Mon, 17 Oct 2016 19:17:22 +0100 Subject: fully working add and sub --- src/ymh15/test_mips.cpp | 127 ++++++++++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 54 deletions(-) (limited to 'src/ymh15/test_mips.cpp') diff --git a/src/ymh15/test_mips.cpp b/src/ymh15/test_mips.cpp index 3523844..1b9ed42 100644 --- a/src/ymh15/test_mips.cpp +++ b/src/ymh15/test_mips.cpp @@ -8,32 +8,37 @@ using namespace std; int main() { mips_mem_h ram = mips_mem_create_ram(4096); mips_cpu_h cpu = mips_cpu_create(ram); - - uint32_t inst = gen_instruction(9, 10, 8, 0, 0x21); - uint32_t inst2 = gen_instruction(12, 13, 11, 0, 0x20); - uint32_t ans; - uint32_t ans2; - - mips_mem_write(ram, 0, 4, (uint8_t*)&inst); - mips_mem_write(ram, 4, 4, (uint8_t*)&inst2); - mips_cpu_set_register(cpu, 9, 4); - mips_cpu_set_register(cpu, 10, 5); - mips_cpu_set_register(cpu, 12, 232); - mips_cpu_set_register(cpu, 13, 2356); - - mips_cpu_step(cpu); - mips_cpu_step(cpu); - - mips_cpu_get_register(cpu, 8, &ans); - mips_cpu_get_register(cpu, 11, &ans2); + srand(time(NULL)); + + mips_test_begin_suite(); + + int testId = mips_test_begin_test("ADD"); + mips_test_end_test(testId, test_add(ram, cpu, 0x20, 0x3fffffff, 0, 10), "Testing the adder without overflow"); + + testId = mips_test_begin_test("ADD"); + mips_test_end_test(testId, !test_add(ram, cpu, 0x20, 0x7fffffff, 1, 1), "Testing the adder with overflow"); + + testId = mips_test_begin_test("ADDU"); + mips_test_end_test(testId, test_add(ram, cpu, 0x21, 0x3fffffff, 0, 10), "testing without overflow"); + + testId = mips_test_begin_test("ADDU"); + mips_test_end_test(testId, test_add(ram, cpu, 0x21, 0x7fffffff, 1, 1), "Testing the adder with overflow"); + + testId = mips_test_begin_test("SUB"); + mips_test_end_test(testId, test_add(ram, cpu, 0x22, 0x3fffffff, 0, 10), "Testing the adder without overflow"); + + testId = mips_test_begin_test("SUB"); + mips_test_end_test(testId, !test_add(ram, cpu, 0x22, 0x7fffffff, 1, 1), "Testing the adder with overflow"); + + testId = mips_test_begin_test("SUBU"); + mips_test_end_test(testId, test_add(ram, cpu, 0x23, 0x3fffffff, 0, 10), "Testing the adder without overflow"); + + testId = mips_test_begin_test("SUBU"); + mips_test_end_test(testId, test_add(ram, cpu, 0x23, 0x7fffffff, 1, 1), "Testing the adder with overflow"); + - if(!ans || !ans2) { - printf("failed\n"); - } else { - printf("4 + 5 = %d\n", ans); - printf("232 + 2356 = %d\n", ans2); - } + mips_test_end_suite(); return 0; } @@ -62,40 +67,54 @@ uint32_t gen_instruction(uint32_t opcode, uint32_t memory) { } uint32_t change_endianness(uint32_t inst) { - inst = (inst << 24 | ((inst << 8)&0x00ff0000) | - ((inst >> 8)&0x0000ff00) |inst >> 24); + inst = (inst << 24 | ((inst << 8)&0xff0000) | + ((inst >> 8)&0xff00) |inst >> 24); return inst; } -void test_endian(mips_mem_h ram) { - uint32_t address, length, data, data0, data1, data2, data3, dataf; - address = 0; - length = 4; - data = 0x01234567; - data0 = 0; - data1 = 0; - data2 = 0; - data3 = 0; - dataf = 0; - - printf("Data in: %#010x\n", data); - - //data = (data << 24 | ((data << 8)&0x00ff0000) | - // ((data >> 8)&0x0000ff00) |data >> 24); - - mips_mem_write(ram, address, length, (uint8_t*)&data); - mips_mem_read(ram, address, 1, (uint8_t*)&data0); - mips_mem_read(ram, address+1, 1, (uint8_t*)&data1); - mips_mem_read(ram, address+2, 1, (uint8_t*)&data2); - mips_mem_read(ram, address+3, 1, (uint8_t*)&data3); - - mips_mem_read(ram, address, 4, (uint8_t*)&dataf); +int test_add(mips_mem_h ram, mips_cpu_h cpu, uint32_t type, uint32_t max, uint8_t value, unsigned i_t) { + + uint32_t inst, ans, a, b; + + for(unsigned i = 0; i < i_t; ++i) { + mips_error mips_err; + mips_cpu_reset(cpu); + inst = gen_instruction(9, 10, 8, 0, type); + if(value) { + a = max; + if(type > 0x21) { + b = -max; + } else { + b = max; + } + } else { + a = rand() % max; + b = rand() % max; + } + + mips_mem_write(ram, 0, 4, (uint8_t*)&inst); + + mips_cpu_set_register(cpu, 9, a); + mips_cpu_set_register(cpu, 10, b); + + mips_err = mips_cpu_step(cpu); - cout << "Data at " << address << ": " << data0 << endl; - cout << "Data at " << address+1 << ": " << data1 << endl; - cout << "Data at " << address+2 << ": " << data2 << endl; - cout << "Data at " << address+3 << ": " << data3 << endl; + mips_cpu_get_register(cpu, 8, &ans); + + if(type < 0x22) { + printf("%#10x + %#10x = %#10x\n", a, b, ans); + if(mips_err == mips_ExceptionArithmeticOverflow) { + return 0; + } + } else { + printf("%#10x - %#10x = %#10x\n", a, b, ans); + if(mips_err == mips_ExceptionArithmeticOverflow) { + return 0; + } + } + + } - printf("Dataf: %#010x\n", dataf); + return 1; } -- cgit