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 38877d36ab
3 changed files with 99 additions and 29 deletions

View File

@ -22,10 +22,10 @@ id testing : B = []
: idiv(a, b, t_o) = setlistval(t_o, odiv(B[a], B[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 \
: icmp(a, b, z) = { \\
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
@ -64,27 +63,26 @@ id testing : B = []
: paramthree = 0
# main execution flows
: load(addr) = jumped -> 0, inst -> B[addr], \
paramone -> B[addr + 1], \
paramtwo -> B[addr + 2], \
: load(addr) = jumped -> 0, inst -> B[addr], \\
paramone -> B[addr + 1], \\
paramtwo -> B[addr + 2], \\
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), \
inst = eld : ield(paramone, paramtwo), \
inst = gld : igld(paramone, paramtwo), \
inst = lld : illd(paramone, paramtwo), \
inst = jmp : ijmp(paramone, paramtwo, paramthree), \
inst = be : ibe(paramone, paramtwo, paramthree), \
inst = bne : ibne(paramone, paramtwo, paramthree), \
inst = bg : ibg(paramone, paramtwo, paramthree), \
inst = bl : ibl(paramone, paramtwo, paramthree), \
inst = sub : isub(paramone, paramtwo, paramthree), \
inst = mul : imul(paramone, paramtwo, paramthree), \
inst = div : idiv(paramone, paramtwo, paramthree) \
: exec = { \\
inst = sto : isto(paramone, paramtwo), \\
inst = mov : imov(paramone, paramtwo), \\
inst = add : iadd(paramone, paramtwo, paramthree), \\
inst = cmp : icmp(paramone, paramtwo, paramthree), \\
inst = eld : ield(paramone, paramtwo), \\
inst = gld : igld(paramone, paramtwo), \\
inst = lld : illd(paramone, paramtwo), \\
inst = jmp : ijmp(paramone, paramtwo, paramthree), \\
inst = be : ibe(paramone, paramtwo, paramthree), \\
inst = bne : ibne(paramone, paramtwo, paramthree), \\
inst = bg : ibg(paramone, paramtwo, paramthree), \\
inst = bl : ibl(paramone, paramtwo, paramthree), \\
inst = sub : isub(paramone, paramtwo, paramthree), \\
inst = mul : imul(paramone, paramtwo, paramthree), \\
inst = div : idiv(paramone, paramtwo, paramthree) \\
}
: incip = {jumped = 0 : ip -> ip + instwidth[inst] + 1}
@ -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]
"""

View File

@ -4,5 +4,5 @@ payload = """
: m = 3
: y = m * x + b
: y = 2m * x + b
: \frac{x^{2+3}}{3}*4*testing
: \\frac{x^{2+3}}{3}*4*testing
"""

View File

@ -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]
)
)