From ee93c4db4c398cbd34d03a01d58e6875480c1fd4 Mon Sep 17 00:00:00 2001 From: stupidcomputer Date: Sun, 16 Jun 2024 19:05:58 -0500 Subject: [PATCH] add more tests for the ISA; ISA bugfix added more tests to the ISA -- covers all the instructions except RST (reset). fixed a bug in the ISA -- when comparing two numbers, we really compared their pointers. in C, it's the difference between ``` int a = 10; int b = 12; int *ap = &a; int *bp = &b; if (ap < bp) { /* do stuff */ } ``` the problem is that you're comparing the pointers, *not* the values they refer to. it instead should be like if (*ap < *bp) { /* do stuff */ } because you need to *dereference* the value before comparing. that's what happens in cli/data/computer.py now. --- cli/data/computer.py | 11 +++---- cli/tests/isa.py | 75 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 8 deletions(-) diff --git a/cli/data/computer.py b/cli/data/computer.py index cf5597d..4292117 100644 --- a/cli/data/computer.py +++ b/cli/data/computer.py @@ -23,9 +23,9 @@ id testing : B = [] : imul(a, b, t_o) = setlistval(t_o, omul(B[a], B[b])) : icmp(a, b, z) = { \ - a = b : equals -> 1 , \ - a > b : greater -> 1 , \ - a < b : less -> 1 \ + B[a] = B[b] : equals -> 1 , \ + B[a] > B[b] : greater -> 1 , \ + B[a] < B[b] : less -> 1 \ } : irst(a, b, c) = equals -> 0, greater -> 0, less -> 0 : ield(addr, b) = setlistval(addr, equals) @@ -40,7 +40,6 @@ id testing : B = [] : isto(v, addr) = setlistval(addr, v) : imov(from, target) = setlistval(target, B[from]) -: ipsto(value, ptr) = setlistval(B[ptr], value) # registers # instruction pointer @@ -70,7 +69,6 @@ id testing : B = [] paramthree -> B[addr + 3] : exec = { \ inst = sto : isto(paramone, paramtwo), \ - inst = psto : ipsto(paramone, paramtwo), \ inst = mov : imov(paramone, paramtwo), \ inst = add : iadd(paramone, paramtwo, paramthree), \ inst = cmp : icmp(paramone, paramtwo, paramthree), \ @@ -97,7 +95,6 @@ id testing : B = [] : main = loopaction, loop : sto = 1 -: psto = 16 : mov = 17 : add = 2 : cmp = 3 @@ -114,5 +111,5 @@ id testing : B = [] : div = 14 : rst = 15 -: instwidth = [2,3,1,1,1,1,1,1,1,1,3,3,3,3,0,2,2] +: instwidth = [2,3,2,1,1,1,1,1,1,1,3,3,3,3,0,2,2] """ diff --git a/cli/tests/isa.py b/cli/tests/isa.py index 728d55f..e4379fa 100644 --- a/cli/tests/isa.py +++ b/cli/tests/isa.py @@ -31,7 +31,6 @@ def instruction_test_helper(override_text, expected_output): }) server.start() - time.sleep(1) return server.outputs[-1]["output"] == "true" class ISATest(unittest.TestCase): @@ -82,3 +81,77 @@ class ISATest(unittest.TestCase): [14, 1, 6, 5, 3.5, 4], ) ) + + def test_jmp(self): + self.assertTrue( + instruction_test_helper( + [7, 10, 0, 0, 0, 0, 0, 0, 0, 1, 3, 3], # jump to addr 10 + [7, 10, 3, 0, 0, 0, 0, 0, 0, 1, 3, 3] + ) + ) + + def test_cmp_eq(self): + self.assertTrue( + instruction_test_helper( + # check if 3 is equal to 3. if so, store 3 into addr 3, otherwise store 1 into address 2 + [3, 1, 1, 8, 10, 1, 1, 2, 0, 1, 3, 3], + [3, 1, 3, 8, 10, 1, 1, 2, 0, 1, 3, 3], + ) + ) + + def test_cmp_neq(self): + self.assertTrue( + instruction_test_helper( + # check if 3 is equal to 1. if not, store 3 into addr 3, otherwise store 1 into address 2 + [3, 1, 2, 9, 10, 1, 1, 2, 0, 1, 3, 3], + [3, 1, 3, 9, 10, 1, 1, 2, 0, 1, 3, 3], + ) + ) + + def test_cmp_gt(self): + self.assertTrue( + instruction_test_helper( + [3, 1, 2, 10, 10, 1, 1, 2, 0, 1, 3, 3], + [3, 1, 3, 10, 10, 1, 1, 2, 0, 1, 3, 3], + ) + ) + + def test_cmp_lt(self): + self.assertTrue( + instruction_test_helper( + [3, 1, 4, 11, 10, 1, 1, 2, 0, 1, 3, 3], + [3, 1, 4, 11, 10, 1, 1, 2, 0, 1, 3, 3], + ) + ) + + def test_eld(self): + self.assertTrue( + instruction_test_helper( + [3, 1, 1, 4, 1], + [1, 1, 1, 4, 1], + ) + ) + + def test_gld(self): + self.assertTrue( + instruction_test_helper( + [3, 4, 1, 5, 1], + [1, 4, 1, 5, 1], + ) + ) + + def test_lld(self): + self.assertTrue( + instruction_test_helper( + [3, 1, 4, 6, 1], + [1, 1, 4, 6, 1], + ) + ) + + def test_mov(self): + self.assertTrue( + instruction_test_helper( + [17, 1, 2], + [17, 17, 2] + ) + )