From 8e8aee436fdd03c7700030d8e0f5826aea0572ec Mon Sep 17 00:00:00 2001 From: zedarider Date: Mon, 17 Oct 2016 20:05:54 +0100 Subject: added bitwise operators --- src/ymh15/mips_cpu.cpp | 19 +++++++++-- src/ymh15/mips_cpu.o | Bin 13384 -> 13968 bytes src/ymh15/mips_cpu_ymh15.hpp | 2 ++ src/ymh15/test_mips | Bin 328152 -> 328056 bytes src/ymh15/test_mips.cpp | 73 ++++++++++++++++++++++++++++++++++++------ src/ymh15/test_mips.o | Bin 33176 -> 32600 bytes src/ymh15/test_mips_ymh15.hpp | 9 ++++++ 7 files changed, 90 insertions(+), 13 deletions(-) diff --git a/src/ymh15/mips_cpu.cpp b/src/ymh15/mips_cpu.cpp index d3d4399..ea7cce1 100644 --- a/src/ymh15/mips_cpu.cpp +++ b/src/ymh15/mips_cpu.cpp @@ -151,21 +151,23 @@ mips_error exec_instruction(mips_cpu_h state, uint32_t inst) { mips_error exec_R(mips_cpu_h state, uint32_t var[8]) { if((var[FUNC]&0xf0) == 0x20 && (var[FUNC]&0xf) < 4) { return add_sub(state, var, ((int32_t)-(var[FUNC]&0xf)/2)*2+1); + } else if((var[FUNC]&0xf0) == 0x20 && (var[FUNC]&0xf) < 7) { + return bitwise(state, var); } - return mips_Success; + return mips_ExceptionInvalidInstruction; } mips_error exec_J(mips_cpu_h state, uint32_t var[8]) { //TODO - return mips_Success; + return mips_ExceptionInvalidInstruction; } mips_error exec_I(mips_cpu_h state, uint32_t var[8]) { //TODO - return mips_Success; + return mips_ExceptionInvalidInstruction; } mips_error add_sub(mips_cpu_h state, uint32_t var[8], int32_t add_sub) { @@ -179,3 +181,14 @@ mips_error add_sub(mips_cpu_h state, uint32_t var[8], int32_t add_sub) { } return mips_ExceptionArithmeticOverflow; } + +mips_error bitwise(mips_cpu_h state, uint32_t var[8]) { + if((var[FUNC]&0xf) == 4) { + state->regs[var[REG_D]] = state->regs[var[REG_S]] & state->regs[var[REG_T]]; + } else if((var[FUNC]&0xf) == 5) { + state->regs[var[REG_D]] = state->regs[var[REG_S]] | state->regs[var[REG_T]]; + } else { + state->regs[var[REG_D]] = state->regs[var[REG_S]] ^ state->regs[var[REG_T]]; + } + return mips_Success; +} diff --git a/src/ymh15/mips_cpu.o b/src/ymh15/mips_cpu.o index c3067f3..1781493 100644 Binary files a/src/ymh15/mips_cpu.o and b/src/ymh15/mips_cpu.o differ diff --git a/src/ymh15/mips_cpu_ymh15.hpp b/src/ymh15/mips_cpu_ymh15.hpp index 5c6ea90..1e2900a 100644 --- a/src/ymh15/mips_cpu_ymh15.hpp +++ b/src/ymh15/mips_cpu_ymh15.hpp @@ -17,7 +17,9 @@ mips_error exec_instruction(mips_cpu_h state, uint32_t inst); mips_error exec_R(mips_cpu_h state, uint32_t var[8]); mips_error exec_J(mips_cpu_h state, uint32_t var[8]); mips_error exec_I(mips_cpu_h state, uint32_t var[8]); + mips_error add_sub(mips_cpu_h state, uint32_t var[8], int32_t add_sub); +mips_error bitwise(mips_cpu_h state, uint32_t var[8]); uint8_t get_msb(uint32_t word); diff --git a/src/ymh15/test_mips b/src/ymh15/test_mips index 6d0340c..78350cd 100755 Binary files a/src/ymh15/test_mips and b/src/ymh15/test_mips differ diff --git a/src/ymh15/test_mips.cpp b/src/ymh15/test_mips.cpp index 1b9ed42..4a2de99 100644 --- a/src/ymh15/test_mips.cpp +++ b/src/ymh15/test_mips.cpp @@ -14,29 +14,37 @@ int main() { 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"); + mips_test_end_test(testId, test_add(ram, cpu, ADD, 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"); + mips_test_end_test(testId, !test_add(ram, cpu, ADD, 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"); + mips_test_end_test(testId, test_add(ram, cpu, ADDU, 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"); + mips_test_end_test(testId, test_add(ram, cpu, ADDU, 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"); + mips_test_end_test(testId, test_add(ram, cpu, SUB, 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"); + mips_test_end_test(testId, !test_add(ram, cpu, SUB, 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"); + mips_test_end_test(testId, test_add(ram, cpu, SUBU, 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"); + mips_test_end_test(testId, test_add(ram, cpu, SUBU, 0x7fffffff, 1, 1), "Testing the adder with overflow"); + testId = mips_test_begin_test("AND"); + mips_test_end_test(testId, test_bitwise(ram, cpu, AND), "Testing bitwise and"); + + testId = mips_test_begin_test("OR"); + mips_test_end_test(testId, test_bitwise(ram, cpu, OR), "Testing bitwise or"); + + testId = mips_test_begin_test("XOR"); + mips_test_end_test(testId, test_bitwise(ram, cpu, XOR), "Testing bitwise xor"); mips_test_end_suite(); return 0; @@ -103,12 +111,12 @@ int test_add(mips_mem_h ram, mips_cpu_h cpu, uint32_t type, uint32_t max, uint8_ if(type < 0x22) { printf("%#10x + %#10x = %#10x\n", a, b, ans); - if(mips_err == mips_ExceptionArithmeticOverflow) { + if(mips_err == mips_ExceptionArithmeticOverflow || a+b!=ans) { return 0; } } else { printf("%#10x - %#10x = %#10x\n", a, b, ans); - if(mips_err == mips_ExceptionArithmeticOverflow) { + if(mips_err == mips_ExceptionArithmeticOverflow || a-b!=ans) { return 0; } } @@ -118,3 +126,48 @@ int test_add(mips_mem_h ram, mips_cpu_h cpu, uint32_t type, uint32_t max, uint8_ return 1; } +int test_bitwise(mips_mem_h ram, mips_cpu_h cpu, uint8_t op) { + uint32_t a, b, ans, inst; + int passed; + for(unsigned i = 0; i < 10; ++i) { + passed = 0; + mips_cpu_reset(cpu); + inst = gen_instruction(8, 9, 7, 0, op); + + a = rand() % 0xffffffff; + b = rand() % 0xffffffff; + + mips_mem_write(ram, 0, 4, (uint8_t*)&inst); + + mips_cpu_set_register(cpu, 8, a); + mips_cpu_set_register(cpu, 9, b); + + mips_error mips_err = mips_cpu_step(cpu); + + mips_cpu_get_register(cpu, 7, &ans); + + if(op == AND) { + printf("%#10x & %#10x = %#10x\n", a, b, ans); + if((a & b) == ans) { + passed = 1; + } + } else if(op == OR) { + printf("%#10x | %#10x = %#10x\n", a, b, ans); + if((a | b) == ans) { + passed = 1; + } + } else { + printf("%#10x ^ %#10x = %#10x\n", a, b, ans); + if((a ^ b) == ans) { + passed = 1; + } + } + + + if(mips_err != mips_Success || !passed) { + return 0; + } + } + return 1; +} + diff --git a/src/ymh15/test_mips.o b/src/ymh15/test_mips.o index 6a13e71..8411646 100644 Binary files a/src/ymh15/test_mips.o and b/src/ymh15/test_mips.o differ diff --git a/src/ymh15/test_mips_ymh15.hpp b/src/ymh15/test_mips_ymh15.hpp index 7b85ca4..60004f1 100644 --- a/src/ymh15/test_mips_ymh15.hpp +++ b/src/ymh15/test_mips_ymh15.hpp @@ -5,6 +5,14 @@ #include #include +#define ADD 0x20 +#define ADDU 0x21 +#define SUB 0x22 +#define SUBU 0x23 +#define AND 0x24 +#define OR 0x25 +#define XOR 0x26 + uint32_t gen_instruction(uint32_t src1, uint32_t src2, uint32_t dest, uint32_t shift, uint32_t function); uint32_t gen_instruction(uint32_t opcode, uint32_t src, uint32_t dest, @@ -13,5 +21,6 @@ uint32_t gen_instruction(uint32_t opcode, uint32_t memory); uint32_t change_endianness(uint32_t inst); int test_add(mips_mem_h ram, mips_cpu_h cpu, uint32_t type, uint32_t max, uint8_t value, unsigned i_t); +int test_bitwise(mips_mem_h ram, mips_cpu_h cpu, uint8_t op); #endif // TEST_MIPS_YMH15_H -- cgit