From f5579e9acfe55a11a9940e4c0caca1afd3fd301f Mon Sep 17 00:00:00 2001 From: d6 Date: Sat, 25 Dec 2021 23:07:41 -0500 Subject: [PATCH] init --- math32.tal | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++---- tester.py | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 tester.py diff --git a/math32.tal b/math32.tal index d69f81a..23181ac 100644 --- a/math32.tal +++ b/math32.tal @@ -5,12 +5,12 @@ %DEBUG { #ff #0e DEO } %RTN { JMP2r } -%EMIT { #18 DEO } +%EMIT { .Console/write DEO } %DIGIT { #00 SWP ;digits ADD2 LDA EMIT } %SPACE { #20 EMIT } %NEWLINE { #0a EMIT } -%TOR { ROT ROT } +%TOR { ROT ROT } ( a b c -> c a b ) %TOR2 { ROT2 ROT2 } %POP4 { POP2 POP2 } @@ -18,10 +18,17 @@ %Y { #1234 #ffff } %Z { #fedc #ba98 } +( devices ) +|10 @Console [ &vector $2 &read $1 &pad $5 &write $1 ] + +( program ) |0100 - ( run a bunch of test cases ) + +( #0000 ;pos STA2 ) + ;interact .Console/vector DEO2 + BRK - #1000 #03 ;divmod16-by-8 JSR2 ;emit-short-byte JSR2 NEWLINE +( #1000 #03 ;divmod16-by-8 JSR2 ;emit-short-byte JSR2 NEWLINE NEWLINE #1124 #1244 #01 ;left-shift JSR2 ;emit-long JSR2 NEWLINE @@ -77,9 +84,77 @@ X Y #3d ;eq32 ;emit-byte ;test32 JSR2 Y Y #3d ;eq32 ;emit-byte ;test32 JSR2 X Y #7e ;ne32 ;emit-byte ;test32 JSR2 - X X #7e ;ne32 ;emit-byte ;test32 JSR2 + X X #7e ;ne32 ;emit-byte ;test32 JSR2 ) BRK +( e.g. #31 #33 -> #13 ) +@parse-byte ( c0 c1 -> x^ ) + ( lower char ) + DUP #3a LTH ,&lo-digit JCN + #57 ,&lo JMP &lo-digit #30 + &lo SUB SWP + + ( higher char )) + DUP #3a LTH ,&hi-digit JCN + #57 ,&hi JMP &hi-digit #30 + &hi SUB #40 SFT ORA +RTN + +@buf $24 +@pos $2 + +@read-long ( addr* -> x** ) + DUP2 ,&loc STR2 LDA2 ;parse-byte JSR2 + ,&loc LDR2 #0002 ADD2 LDA2 ;parse-byte JSR2 + ,&loc LDR2 #0004 ADD2 LDA2 ;parse-byte JSR2 + ,&loc LDR2 #0006 ADD2 LDA2 ;parse-byte JSR2 +RTN +[ &loc $2 ] + +%POS++ { ;pos LDA2k INC2 SWP2 STA2 } +%RESET-POS { #0000 ;pos STA2 #00 ;buf STA } + +@interact + .Console/read DEI ( char^ ) + DUP #0a EQU ( char^ char=\n? ) + ,&exec JCN ( char^ ) + ;pos LDA2 ;buf ADD2 STA POS++ BRK + &exec + POP ( ) + ;buf LDA LIT '+ EQU ;test-add32 JCN2 + ;buf LDA LIT '* EQU ;test-mul32 JCN2 + ;buf LDA LIT '- EQU ;test-sub32 JCN2 + ;buf LDA LIT 'L EQU ;test-left-shift JCN2 + LIT '? EMIT NEWLINE RESET-POS BRK + +@test-add32 + ( format: "+ xxxxxxxx yyyyyyyy" ) + ;buf #0002 ADD2 ;read-long JSR2 + ;buf #000b ADD2 ;read-long JSR2 + ;add32 JSR2 ;emit-long JSR2 + NEWLINE RESET-POS BRK + +@test-mul32 + ( format: "+ xxxxxxxx yyyyyyyy" ) + ;buf #0002 ADD2 ;read-long JSR2 + ;buf #000b ADD2 ;read-long JSR2 + ;mul32 JSR2 ;emit-long JSR2 + NEWLINE RESET-POS BRK + +@test-sub32 + ( format: "+ xxxxxxxx yyyyyyyy" ) + ;buf #0002 ADD2 ;read-long JSR2 + ;buf #000b ADD2 ;read-long JSR2 + ;sub32 JSR2 ;emit-long JSR2 + NEWLINE RESET-POS BRK + +@test-left-shift + ( format: "+ xxxxxxxx yy" ) + ;buf #0002 ADD2 ;read-long JSR2 + ;buf #000b ADD2 ;read-byte JSR2 + ;left-shift JSR2 ;emit-long JSR2 + NEWLINE RESET-POS BRK + @test16 ( x* y* symbol^ test-addr* emit-addr* -> ) ,&emitaddr STR2 ,&testaddr STR2 diff --git a/tester.py b/tester.py new file mode 100644 index 0000000..9ad6441 --- /dev/null +++ b/tester.py @@ -0,0 +1,50 @@ +#!/usr/bin/python + +from os import environ +from subprocess import Popen, PIPE +from random import randint + +ubyte = {'sz': 1 << 8, 'fmt': b'%02x'} +ushort = {'sz': 1 << 16, 'fmt': b'%04x'} +ulong = {'sz': 1 << 32, 'fmt': b'%08x'} + +def u32(x): + return x % 4294967296 + +def case(p, x, y, sym, f): + p.stdin.write(b'%s %08x %08x\n' % (sym, x, y)) + p.stdin.flush() + got = p.stdout.readline().strip().decode('utf-8') + expected = '%08x' % u32(f(x, y)) + return got == expected + +def test(p, runs, sym, f): + fails = 0 + cases = [] + maximum = (1 << 32) - 1 + for i in range(0, runs): + x = randint(0, maximum) + y = randint(0, maximum) + ok = case(p, x, y, sym, f) + if not ok: + fails += 1 + cases.append((x, y)) + name = sym.decode('utf-8') + if fails == 0: + print('%s passed %d runs' % (name, runs)) + else: + print('%s failed %d/%d runs (%r)' % (name, fails, runs, cases)) + +def main(): + home = environ['HOME'] + p = Popen([home + '/w/uxn/bin/uxncli', 'run.rom'], stdin=PIPE, stdout=PIPE) + runs = 1000 + test(p, runs, b'+', lambda x, y: x + y) + test(p, runs, b'-', lambda x, y: x - y) + test(p, runs, b'*', lambda x, y: x * y) + #test(p, runs, b'L', lambda x, y: x << y) + p.stdin.close() + p.stdout.close() + +if __name__ == "__main__": + main()