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.
This commit is contained in:
stupidcomputer 2024-06-16 19:05:58 -05:00
parent 6a038fcdde
commit ee93c4db4c
2 changed files with 78 additions and 8 deletions

View File

@ -23,9 +23,9 @@ id testing : B = []
: imul(a, b, t_o) = setlistval(t_o, omul(B[a], B[b])) : imul(a, b, t_o) = setlistval(t_o, omul(B[a], B[b]))
: icmp(a, b, z) = { \ : icmp(a, b, z) = { \
a = b : equals -> 1 , \ B[a] = B[b] : equals -> 1 , \
a > b : greater -> 1 , \ B[a] > B[b] : greater -> 1 , \
a < b : less -> 1 \ B[a] < B[b] : less -> 1 \
} }
: irst(a, b, c) = equals -> 0, greater -> 0, less -> 0 : irst(a, b, c) = equals -> 0, greater -> 0, less -> 0
: ield(addr, b) = setlistval(addr, equals) : ield(addr, b) = setlistval(addr, equals)
@ -40,7 +40,6 @@ id testing : B = []
: isto(v, addr) = setlistval(addr, v) : isto(v, addr) = setlistval(addr, v)
: imov(from, target) = setlistval(target, B[from]) : imov(from, target) = setlistval(target, B[from])
: ipsto(value, ptr) = setlistval(B[ptr], value)
# registers # registers
# instruction pointer # instruction pointer
@ -70,7 +69,6 @@ id testing : B = []
paramthree -> B[addr + 3] paramthree -> B[addr + 3]
: exec = { \ : exec = { \
inst = sto : isto(paramone, paramtwo), \ inst = sto : isto(paramone, paramtwo), \
inst = psto : ipsto(paramone, paramtwo), \
inst = mov : imov(paramone, paramtwo), \ inst = mov : imov(paramone, paramtwo), \
inst = add : iadd(paramone, paramtwo, paramthree), \ inst = add : iadd(paramone, paramtwo, paramthree), \
inst = cmp : icmp(paramone, paramtwo, paramthree), \ inst = cmp : icmp(paramone, paramtwo, paramthree), \
@ -97,7 +95,6 @@ id testing : B = []
: main = loopaction, loop : main = loopaction, loop
: sto = 1 : sto = 1
: psto = 16
: mov = 17 : mov = 17
: add = 2 : add = 2
: cmp = 3 : cmp = 3
@ -114,5 +111,5 @@ id testing : B = []
: div = 14 : div = 14
: rst = 15 : 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]
""" """

View File

@ -31,7 +31,6 @@ def instruction_test_helper(override_text, expected_output):
}) })
server.start() server.start()
time.sleep(1)
return server.outputs[-1]["output"] == "true" return server.outputs[-1]["output"] == "true"
class ISATest(unittest.TestCase): class ISATest(unittest.TestCase):
@ -82,3 +81,77 @@ class ISATest(unittest.TestCase):
[14, 1, 6, 5, 3.5, 4], [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]
)
)