And passing all stock(ish) test cases

This commit is contained in:
Reid 'arrdem' McKenzie 2022-12-26 01:19:08 -07:00
parent 34ceb1b392
commit 5eb718d20e
11 changed files with 770 additions and 128 deletions

View file

@ -103,10 +103,7 @@ Raw labels are inserted directly into bytecode.
Absolute labels are double quantities. Absolute labels are double quantities.
Relative labels are single signed byte quantities with a ±127 range. Relative labels are single signed byte quantities with a ±127 range.
The zero page (`#00XX`) is used for system devices, along other things. The zero page (`#00XX`) is used for program globals and convenient scratch space.
It's common to see labels such as `.System/vector`, being a reference to the address `#0000` packed into just `#00`
However as UXN has a special `LDZ` operation for loading from the zero page, this address can be specified as simply `#96` to save a byte.
As the last device is mapped to `#CX`, it is common to see `#DX`, `#EX` and `#FX` used for program-global variables for ease of access.
Literal byte relative references ala `,foo` are used for control flow. Literal byte relative references ala `,foo` are used for control flow.
Using only a single byte, these references have a range of ±127 instructions. Using only a single byte, these references have a range of ±127 instructions.

BIN
resources/test/tests.rom Normal file

Binary file not shown.

Binary file not shown.

596
resources/test/tests.tal Normal file
View file

@ -0,0 +1,596 @@
( Opcode Tester )
( Requirements:
EQU/EQU2 should put #00 or #01 on the stack
#18 DEO should output ascii character to console )
%EMIT { #18 DEO }
%FAIL { #00 EQU ;fail JCN2 }
%TEST-SHORT { EQU2 DUP #30 ADD EMIT FAIL }
%TEST-BYTE { EQU DUP #30 ADD EMIT FAIL }
%MODE { #20 EMIT }
%OPCODE { #0a EMIT }
%TYPE { OPCODE OPCODE }
%DEBUG { #010e DEO }
%HALT { #010f DEO BRK }
|0000
@zeropage
&byte $1 &short $2
|0100
( Logic )
( EQU )
@test_equ
&normal
#f8 #f8 EQU [ #01 ] TEST-BYTE
#01 #01 EQU [ #01 ] TEST-BYTE
#f8 #01 EQU [ #00 ] TEST-BYTE
#01 #f8 EQU [ #00 ] TEST-BYTE
MODE
&short
#f801 #f801 EQU2 [ #01 ] TEST-BYTE
#01f8 #01f8 EQU2 [ #01 ] TEST-BYTE
#f801 #01f8 EQU2 [ #00 ] TEST-BYTE
#01f8 #f801 EQU2 [ #00 ] TEST-BYTE
MODE
&keep
#f8 #f8 EQUk ADD ADD [ #f1 ] TEST-BYTE
#01 #01 EQUk ADD ADD [ #03 ] TEST-BYTE
#f8 #01 EQUk ADD ADD [ #f9 ] TEST-BYTE
#01 #f8 EQUk ADD ADD [ #f9 ] TEST-BYTE
MODE
&short_keep
#f801 #f801 EQU2k #00 ADD2 ADD2 [ #f102 ] TEST-SHORT
#01f8 #01f8 EQU2k #00 ADD2 ADD2 [ #04f0 ] TEST-SHORT
#f801 #01f8 EQU2k #00 ADD2 ADD2 [ #f9f9 ] TEST-SHORT
#01f8 #f801 EQU2k #00 ADD2 ADD2 [ #f9f9 ] TEST-SHORT
OPCODE
( NEQ )
@test_neq
&normal
#f8 #f8 NEQ [ #00 ] TEST-BYTE
#01 #01 NEQ [ #00 ] TEST-BYTE
#f8 #01 NEQ [ #01 ] TEST-BYTE
#01 #f8 NEQ [ #01 ] TEST-BYTE
MODE
&short
#f801 #f801 NEQ2 [ #00 ] TEST-BYTE
#01f8 #01f8 NEQ2 [ #00 ] TEST-BYTE
#f801 #01f8 NEQ2 [ #01 ] TEST-BYTE
#01f8 #f801 NEQ2 [ #01 ] TEST-BYTE
MODE
&keep
#f8 #f8 NEQk ADD ADD [ #f0 ] TEST-BYTE
#01 #01 NEQk ADD ADD [ #02 ] TEST-BYTE
#f8 #01 NEQk ADD ADD [ #fa ] TEST-BYTE
#01 #f8 NEQk ADD ADD [ #fa ] TEST-BYTE
MODE
&short_kep
#f801 #f801 NEQ2k #00 ADD2 ADD2 [ #f002 ] TEST-SHORT
#01f8 #01f8 NEQ2k #00 ADD2 ADD2 [ #03f0 ] TEST-SHORT
#f801 #01f8 NEQ2k #00 ADD2 ADD2 [ #faf9 ] TEST-SHORT
#01f8 #f801 NEQ2k #00 ADD2 ADD2 [ #faf9 ] TEST-SHORT
OPCODE
( GTH )
@test_gth
&normal
#f8 #f8 GTH [ #00 ] TEST-BYTE
#01 #01 GTH [ #00 ] TEST-BYTE
#f8 #01 GTH [ #01 ] TEST-BYTE
#01 #f8 GTH [ #00 ] TEST-BYTE
MODE
&short
#f801 #f801 GTH2 [ #00 ] TEST-BYTE
#01f8 #01f8 GTH2 [ #00 ] TEST-BYTE
#f801 #01f8 GTH2 [ #01 ] TEST-BYTE
#01f8 #f801 GTH2 [ #00 ] TEST-BYTE
MODE
&keep
#f8 #f8 GTHk ADD ADD [ #f0 ] TEST-BYTE
#01 #01 GTHk ADD ADD [ #02 ] TEST-BYTE
#f8 #01 GTHk ADD ADD [ #fa ] TEST-BYTE
#01 #f8 GTHk ADD ADD [ #f9 ] TEST-BYTE
MODE
&short_keep
#f801 #f801 GTH2k #00 ADD2 ADD2 [ #f002 ] TEST-SHORT
#01f8 #01f8 GTH2k #00 ADD2 ADD2 [ #03f0 ] TEST-SHORT
#f801 #01f8 GTH2k #00 ADD2 ADD2 [ #faf9 ] TEST-SHORT
#01f8 #f801 GTH2k #00 ADD2 ADD2 [ #f9f9 ] TEST-SHORT
OPCODE
( LTH )
@test_lth
&normal
#f8 #f8 LTH [ #00 ] TEST-BYTE
#01 #01 LTH [ #00 ] TEST-BYTE
#f8 #01 LTH [ #00 ] TEST-BYTE
#01 #f8 LTH [ #01 ] TEST-BYTE
MODE
&short
#f801 #f801 LTH2 [ #00 ] TEST-BYTE
#01f8 #01f8 LTH2 [ #00 ] TEST-BYTE
#f801 #01f8 LTH2 [ #00 ] TEST-BYTE
#01f8 #f801 LTH2 [ #01 ] TEST-BYTE
MODE
&keep
#f8 #f8 LTHk ADD ADD [ #f0 ] TEST-BYTE
#01 #01 LTHk ADD ADD [ #02 ] TEST-BYTE
#f8 #01 LTHk ADD ADD [ #f9 ] TEST-BYTE
#01 #f8 LTHk ADD ADD [ #fa ] TEST-BYTE
MODE
&short_keep
#f801 #f801 LTH2k #00 ADD2 ADD2 [ #f002 ] TEST-SHORT
#01f8 #01f8 LTH2k #00 ADD2 ADD2 [ #03f0 ] TEST-SHORT
#f801 #01f8 LTH2k #00 ADD2 ADD2 [ #f9f9 ] TEST-SHORT
#01f8 #f801 LTH2k #00 ADD2 ADD2 [ #faf9 ] TEST-SHORT
TYPE
( Arithmetic )
( ADD )
@test_add
&normal
#ff #00 ADD [ #ff ] TEST-BYTE
#01 #ff ADD [ #00 ] TEST-BYTE
#ff #ff ADD [ #fe ] TEST-BYTE
#fe #ff ADD [ #fd ] TEST-BYTE
MODE
&short
#ffff #0000 ADD2 [ #ffff ] TEST-SHORT
#0001 #ffff ADD2 [ #0000 ] TEST-SHORT
#ffff #ffff ADD2 [ #fffe ] TEST-SHORT
#fffe #ffff ADD2 [ #fffd ] TEST-SHORT
MODE
&keep
#ff #00 ADDk ADD ADD [ #fe ] TEST-BYTE
#01 #ff ADDk ADD ADD [ #00 ] TEST-BYTE
#ff #ff ADDk ADD ADD [ #fc ] TEST-BYTE
#fe #ff ADDk ADD ADD [ #fa ] TEST-BYTE
MODE
&short_keep
#ffff #0000 ADD2k ADD2 ADD2 [ #fffe ] TEST-SHORT
#0001 #ffff ADD2k ADD2 ADD2 [ #0000 ] TEST-SHORT
#ffff #ffff ADD2k ADD2 ADD2 [ #fffc ] TEST-SHORT
#fffe #ffff ADD2k ADD2 ADD2 [ #fffa ] TEST-SHORT
OPCODE
( SUB )
@test_sub
&normal
#ff #00 SUB [ #ff ] TEST-BYTE
#01 #ff SUB [ #02 ] TEST-BYTE
#ff #ff SUB [ #00 ] TEST-BYTE
#fe #ff SUB [ #ff ] TEST-BYTE
MODE
&short
#ffff #0000 SUB2 [ #ffff ] TEST-SHORT
#0001 #ffff SUB2 [ #0002 ] TEST-SHORT
#ffff #ffff SUB2 [ #0000 ] TEST-SHORT
#fffe #ffff SUB2 [ #ffff ] TEST-SHORT
MODE
&keep
#ff #00 SUBk ADD ADD [ #fe ] TEST-BYTE
#01 #ff SUBk ADD ADD [ #02 ] TEST-BYTE
#ff #ff SUBk ADD ADD [ #fe ] TEST-BYTE
#fe #ff SUBk ADD ADD [ #fc ] TEST-BYTE
MODE
&short_keep
#ffff #0000 SUB2k ADD2 ADD2 [ #fffe ] TEST-SHORT
#0001 #ffff SUB2k ADD2 ADD2 [ #0002 ] TEST-SHORT
#ffff #ffff SUB2k ADD2 ADD2 [ #fffe ] TEST-SHORT
#fffe #ffff SUB2k ADD2 ADD2 [ #fffc ] TEST-SHORT
OPCODE
( MUL )
@test_mul
&normal
#00 #01 MUL [ #00 ] TEST-BYTE
#3f #e7 MUL [ #d9 ] TEST-BYTE
#37 #3f MUL [ #89 ] TEST-BYTE
#10 #02 MUL [ #20 ] TEST-BYTE
MODE
&short
#1000 #0003 MUL2 [ #3000 ] TEST-SHORT
#abcd #1234 MUL2 [ #4fa4 ] TEST-SHORT
#8000 #0200 MUL2 [ #0000 ] TEST-SHORT
#2222 #0003 MUL2 [ #6666 ] TEST-SHORT
MODE
&keep
#00 #01 MULk ADD ADD [ #01 ] TEST-BYTE
#3f #e7 MULk ADD ADD [ #ff ] TEST-BYTE
#37 #3f MULk ADD ADD [ #ff ] TEST-BYTE
#10 #02 MULk ADD ADD [ #32 ] TEST-BYTE
MODE
&short_keep
#1000 #0003 MUL2k ADD2 ADD2 [ #4003 ] TEST-SHORT
#abcd #1234 MUL2k ADD2 ADD2 [ #0da5 ] TEST-SHORT
#8000 #0200 MUL2k ADD2 ADD2 [ #8200 ] TEST-SHORT
#2222 #0003 MUL2k ADD2 ADD2 [ #888b ] TEST-SHORT
OPCODE
( DIV )
@test_div
&normal
#10 #02 DIV [ #08 ] TEST-BYTE
#20 #20 DIV [ #01 ] TEST-BYTE
#34 #01 DIV [ #34 ] TEST-BYTE
#02 #ef DIV [ #00 ] TEST-BYTE
MODE
&short
#1000 #0040 DIV2 [ #0040 ] TEST-SHORT
#abcd #1234 DIV2 [ #0009 ] TEST-SHORT
#8000 #0200 DIV2 [ #0040 ] TEST-SHORT
#2222 #0003 DIV2 [ #0b60 ] TEST-SHORT
MODE
&keep
#10 #02 DIVk ADD ADD [ #1a ] TEST-BYTE
#20 #20 DIVk ADD ADD [ #41 ] TEST-BYTE
#34 #01 DIVk ADD ADD [ #69 ] TEST-BYTE
#02 #ef DIVk ADD ADD [ #f1 ] TEST-BYTE
MODE
&short_keep
#1000 #0040 DIV2k ADD2 ADD2 [ #1080 ] TEST-SHORT
#abcd #1234 DIV2k ADD2 ADD2 [ #be0a ] TEST-SHORT
#8000 #0200 DIV2k ADD2 ADD2 [ #8240 ] TEST-SHORT
#2222 #0003 DIV2k ADD2 ADD2 [ #2d85 ] TEST-SHORT
TYPE
( Stack )
( INC )
@test_inc
&normal
#01 INC [ #02 ] TEST-BYTE
#ff INC [ #00 ] TEST-BYTE
#fe INC [ #ff ] TEST-BYTE
#00 INC [ #01 ] TEST-BYTE
MODE
&short
#0001 INC2 [ #0002 ] TEST-SHORT
#ffff INC2 [ #0000 ] TEST-SHORT
#fffe INC2 [ #ffff ] TEST-SHORT
#0000 INC2 [ #0001 ] TEST-SHORT
MODE
&keep
#01 INCk ADD [ #03 ] TEST-BYTE
#ff INCk ADD [ #ff ] TEST-BYTE
#fe INCk ADD [ #fd ] TEST-BYTE
#00 INCk ADD [ #01 ] TEST-BYTE
MODE
&short_keep
#0001 INC2k ADD2 [ #0003 ] TEST-SHORT
#ffff INC2k ADD2 [ #ffff ] TEST-SHORT
#fffe INC2k ADD2 [ #fffd ] TEST-SHORT
#0000 INC2k ADD2 [ #0001 ] TEST-SHORT
OPCODE
( POP )
@test_pop
&normal
#0a #0b POP [ #0a ] TEST-BYTE
#0a #0b #0c POP POP [ #0a ] TEST-BYTE
#0a #0b #0c ADD POP [ #0a ] TEST-BYTE
#0a #0b #0c POP ADD [ #15 ] TEST-BYTE
MODE
&short
#0a0b #0c0d POP2 [ #0a0b ] TEST-SHORT
#0a0b #0c0d #0e0f POP2 POP2 [ #0a0b ] TEST-SHORT
#0a0b #0c0d #0e0f ADD2 POP2 [ #0a0b ] TEST-SHORT
#0a0b #0c0d #0e0f POP2 ADD2 [ #1618 ] TEST-SHORT
MODE
&keep
#0a #0b POPk [ #0b ] TEST-BYTE POP
#0a #0b #0c POPk POP [ #0b ] TEST-BYTE POP
#0a #0b #0c ADD POPk [ #17 ] TEST-BYTE POP
#0a #0b #0c POPk ADD [ #17 ] TEST-BYTE POP
MODE
&short_keep
#0a0b #0c0d POP2k [ #0c0d ] TEST-SHORT POP2
#0a0b #0c0d #0e0f POP2k POP2 [ #0c0d ] TEST-SHORT POP2
#0a0b #0c0d #0e0f ADD2 POP2k [ #1a1c ] TEST-SHORT POP2
#0a0b #0c0d #0e0f POP2k ADD2 [ #1a1c ] TEST-SHORT POP2
OPCODE
( DUP )
@test_dup
&normal
#0a #0b DUP ADD ADD [ #20 ] TEST-BYTE
MODE
&short
#0a0b DUP2 ADD2 [ #1416 ] TEST-SHORT
MODE
&keep
#0a #0b DUPk ADD ADD ADD [ #2b ] TEST-BYTE
MODE
&short_keep
#0a0b DUP2k ADD2 ADD2 [ #1e21 ] TEST-SHORT
OPCODE
( NIP )
@test_nip
&normal
#12 #34 #56 NIP ADD [ #68 ] TEST-BYTE
MODE
&short
#1234 #5678 #9abc NIP2 ADD2 [ #acf0 ] TEST-SHORT
MODE
&keep
#12 #34 #56 NIPk ADD ADD [ #e0 ] TEST-BYTE POP
MODE
&short_keep
#1234 #5678 #9abc NIP2k ADD2 ADD2 [ #8bf0 ] TEST-SHORT POP2
OPCODE
( SWP )
@test_swp
&normal
#02 #10 SWP DIV [ #08 ] TEST-BYTE
MODE
&short
#0a0b #0c0d SWP2 NIP2 [ #0a0b ] TEST-SHORT
MODE
&keep
#02 #10 SWPk DIV ADD ADD [ #1a ] TEST-BYTE
MODE
&short_keep
#0a0b #0c0d SWP2k POP2 POP2 POP2 [ #0a0b ] TEST-SHORT
OPCODE
( OVR )
@test_ovr
&normal
#02 #10 OVR DIV ADD [ #0a ] TEST-BYTE
MODE
&short
#0a0b #0c0d OVR2 NIP2 ADD2 [ #1416 ] TEST-SHORT
MODE
&keep
#02 #10 OVRk DIV ADD ADD ADD [ #1c ] TEST-BYTE
MODE
&short_keep
#0a0b #0c0d OVR2k NIP2 ADD2 ADD2 ADD2 [ #2a2e ] TEST-SHORT
OPCODE
( ROT )
@test_rot
&normal
#02 #04 #10 ROT DIV ADD [ #0c ] TEST-BYTE
MODE
&short
#0a0b #0c0d #0c0f ROT2 ADD2 NIP2 [ #161a ] TEST-SHORT
MODE
&keep
#02 #04 #10 ROTk DIV ADD ADD ADD ADD [ #22 ] TEST-BYTE
MODE
&short_keep
#0a0b #0c0d #0c0f ROT2k ADD2 NIP2 ADD2 ADD2 ADD2 [ #3841 ] TEST-SHORT
TYPE
( Bitwise )
( AND )
@test_and
&normal
#fc #3f AND [ #3c ] TEST-BYTE
#f0 #0f AND [ #00 ] TEST-BYTE
#ff #3c AND [ #3c ] TEST-BYTE
#02 #03 AND [ #02 ] TEST-BYTE
MODE
&short
#f0f0 #00f0 AND2 [ #00f0 ] TEST-SHORT
#aaaa #5555 AND2 [ #0000 ] TEST-SHORT
#ffff #1234 AND2 [ #1234 ] TEST-SHORT
#abcd #0a0c AND2 [ #0a0c ] TEST-SHORT
MODE
&keep
#fc #3f ANDk ADD ADD [ #77 ] TEST-BYTE
#f0 #0f ANDk ADD ADD [ #ff ] TEST-BYTE
#ff #3c ANDk ADD ADD [ #77 ] TEST-BYTE
#02 #03 ANDk ADD ADD [ #07 ] TEST-BYTE
MODE
&short_keep
#f0f0 #00f0 AND2k ADD2 ADD2 [ #f2d0 ] TEST-SHORT
#aaaa #5555 AND2k ADD2 ADD2 [ #ffff ] TEST-SHORT
#ffff #1234 AND2k ADD2 ADD2 [ #2467 ] TEST-SHORT
#abcd #0a0c AND2k ADD2 ADD2 [ #bfe5 ] TEST-SHORT
OPCODE
( ORA )
@test_ora
&normal
#0f #f0 ORA [ #ff ] TEST-BYTE
#ab #cd ORA [ #ef ] TEST-BYTE
#12 #34 ORA [ #36 ] TEST-BYTE
#88 #10 ORA [ #98 ] TEST-BYTE
MODE
&short
#0f0f #f0f0 ORA2 [ #ffff ] TEST-SHORT
#abab #cdcd ORA2 [ #efef ] TEST-SHORT
#1122 #1234 ORA2 [ #1336 ] TEST-SHORT
#8888 #1000 ORA2 [ #9888 ] TEST-SHORT
MODE
&keep
#0f #f0 ORAk ADD ADD [ #fe ] TEST-BYTE
#ab #cd ORAk ADD ADD [ #67 ] TEST-BYTE
#12 #34 ORAk ADD ADD [ #7c ] TEST-BYTE
#88 #10 ORAk ADD ADD [ #30 ] TEST-BYTE
MODE
&short_keep
#0f0f #f0f0 ORA2k ADD2 ADD2 [ #fffe ] TEST-SHORT
#abab #cdcd ORA2k ADD2 ADD2 [ #6967 ] TEST-SHORT
#1122 #1234 ORA2k ADD2 ADD2 [ #368c ] TEST-SHORT
#8888 #1000 ORA2k ADD2 ADD2 [ #3110 ] TEST-SHORT
OPCODE
( EOR )
@test_eor
&normal
#00 #00 EOR [ #00 ] TEST-BYTE
#ff #00 EOR [ #ff ] TEST-BYTE
#aa #55 EOR [ #ff ] TEST-BYTE
#ff #ff EOR [ #00 ] TEST-BYTE
MODE
&short
#ffff #ff00 EOR2 [ #00ff ] TEST-SHORT
#aaaa #5555 EOR2 [ #ffff ] TEST-SHORT
#1122 #1234 EOR2 [ #0316 ] TEST-SHORT
#8888 #1000 EOR2 [ #9888 ] TEST-SHORT
MODE
&keep
#00 #00 EORk ADD ADD [ #00 ] TEST-BYTE
#ff #00 EORk ADD ADD [ #fe ] TEST-BYTE
#aa #55 EORk ADD ADD [ #fe ] TEST-BYTE
#ff #ff EORk ADD ADD [ #fe ] TEST-BYTE
MODE
&short_keep
#ffff #ff00 EOR2k ADD2 ADD2 [ #fffe ] TEST-SHORT
#aaaa #5555 EOR2k ADD2 ADD2 [ #fffe ] TEST-SHORT
#1122 #1234 EOR2k ADD2 ADD2 [ #266c ] TEST-SHORT
#8888 #1000 EOR2k ADD2 ADD2 [ #3110 ] TEST-SHORT
OPCODE
( SFT )
@test_sft
&normal
#ff #08 SFT [ #00 ] TEST-BYTE
#ff #e0 SFT [ #00 ] TEST-BYTE
#ff #11 SFT [ #fe ] TEST-BYTE
#ff #12 SFT [ #7e ] TEST-BYTE
MODE
&short
#ffff #01 SFT2 [ #7fff ] TEST-SHORT
#ffff #70 SFT2 [ #ff80 ] TEST-SHORT
#ffff #7e SFT2 [ #0180 ] TEST-SHORT
#ffff #e3 SFT2 [ #c000 ] TEST-SHORT
MODE
&keep
#ff #08 SFTk ADD ADD [ #07 ] TEST-BYTE
#ff #e0 SFTk ADD ADD [ #df ] TEST-BYTE
#ff #11 SFTk ADD ADD [ #0e ] TEST-BYTE
#ff #12 SFTk ADD ADD [ #8f ] TEST-BYTE
MODE
&short_keep
#ffff #01 SFT2k ROT POP ADD2 [ #7ffe ] TEST-SHORT
#ffff #70 SFT2k ROT POP ADD2 [ #ff7f ] TEST-SHORT
#ffff #7e SFT2k ROT POP ADD2 [ #017f ] TEST-SHORT
#ffff #e3 SFT2k ROT POP ADD2 [ #bfff ] TEST-SHORT
TYPE
( Memory )
( STA/LDA )
@test_sta_lda
&normal
#34 ;absolute/byte STA ;absolute/byte LDA [ #34 ] TEST-BYTE
#56 ;absolute/byte STA ;absolute/byte LDA [ #56 ] TEST-BYTE
MODE
&short
#1234 ;absolute/short STA2 ;absolute/short LDA2 [ #1234 ] TEST-SHORT
#5678 ;absolute/short STA2 ;absolute/short LDA2 [ #5678 ] TEST-SHORT
OPCODE
( STZ/LDZ )
@test_stz_ldz
&normal
#ab .zeropage/byte STZ .zeropage/byte LDZ [ #ab ] TEST-BYTE
#cd .zeropage/byte STZ .zeropage/byte LDZ [ #cd ] TEST-BYTE
MODE
&short
#1234 .zeropage/short STZ2 .zeropage/short LDZ2 [ #1234 ] TEST-SHORT
#5678 .zeropage/short STZ2 .zeropage/short LDZ2 [ #5678 ] TEST-SHORT
OPCODE
( STR/LDR )
@test_str_ldr
&normal
[ LIT &before1 fe ] POP
;&before1 LDA [ #fe ] TEST-BYTE ( ensure we even have the right ROM loc. )
,&before1 LDR [ #fe ] TEST-BYTE ( test loading the seed value )
#22 ,&before1 STR ,&before1 LDR [ #22 ] TEST-BYTE
#ef ,&after1 STR ,&after1 LDR [ #ef ] TEST-BYTE
MODE
&short
[ LIT2 &before2 fefe ] POP2
;&before2 LDA2 [ #fefe ] TEST-BYTE ( ensure we even have the right ROM loc. )
,&before2 LDR2 [ #fefe ] TEST-SHORT ( test loading the seed value )
#1234 ,&before2 STR2 ,&before2 LDR2 [ #1234 ] TEST-SHORT
#5678 ,&after2 STR2 ,&after2 LDR2 [ #5678 ] TEST-SHORT
[ LIT &after1 $1 ] POP
[ LIT2 &after2 $2 ] POP2
OPCODE
@test_dei_deo
( DEI/DEO )
LIT "1 EMIT
LIT "1 EMIT
TYPE
( Branching )
( JMP )
@test_jmp
&normal
#12 #34 ,&reljmp JMP SWP &reljmp POP [ #12 ] TEST-BYTE
MODE
&short
#56 #78 ;&absjmp JMP2 SWP &absjmp POP [ #56 ] TEST-BYTE
OPCODE
( JCN )
@test_jnc
&normal
#23 #01 ,&reljcn-y JCN INC &reljcn-y [ #23 ] TEST-BYTE
#23 #00 ,&reljcn-n JCN INC &reljcn-n [ #24 ] TEST-BYTE
MODE
&short
#23 #01 ;&absjcn-y JCN2 INC &absjcn-y [ #23 ] TEST-BYTE
#23 #00 ;&absjcn-n JCN2 INC &absjcn-n [ #24 ] TEST-BYTE
OPCODE
( JSR - Requires return mode )
@test_jsr
&short
#12 #34 ;routine JSR2 [ #46 ] TEST-BYTE
OPCODE
( STH )
@test_sth
&normal
LITr ff STHr [ #ff ] TEST-BYTE
LITr 0a LITr 0b ADDr STHr [ #15 ] TEST-BYTE
#0a STH #0b STH ADDr STHr [ #15 ] TEST-BYTE
MODE
&short
#000a STH2 #000b STH2 ADD2r STH2r [ #0015 ] TEST-SHORT
TYPE
( Keep )
@test_keep
&normal
#12 #34 ADDk ADD ADD [ #8c ] TEST-BYTE
OPCODE
@halt
DEBUG
HALT
@fail
#ffff #0000 DEO2
DEBUG
HALT
@routine ( a b -- c )
ADD
JMP2r
@absolute
&byte $1 &short $2

View file

@ -1,9 +1,5 @@
use std::env::args; use std::env::args;
use std::ffi::CStr;
use std::fs::File; use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Read;
use uxn::vm::{Uxn, UxnError}; use uxn::vm::{Uxn, UxnError};
@ -13,44 +9,19 @@ fn main() -> Result<(), std::io::Error> {
let argv: Vec<String> = args().collect(); let argv: Vec<String> = args().collect();
// Load a bytecode file // Load a bytecode file
let f: File = File::open(&argv[1])?; let program: File = File::open(&argv[1])?;
let reader = BufReader::new(f); vm.load_rom(program).unwrap();
let mut i = 0x0100u16;
for b in reader.bytes() {
vm.sta1(i, b?).expect("Write failed");
i += 1;
}
// Load a symbol table // Load a symbol table
if argv.len() > 2 { if argv.len() > 2 {
let f: File = File::open(&argv[2])?; let symbols: File = File::open(&argv[2])?;
let mut reader: BufReader<File> = BufReader::new(f); vm.load_symbols(symbols)?;
loop {
let mut addr_buff = [0u8; 2];
match reader.read_exact(&mut addr_buff) {
Ok(_) => (),
Err(_) => break,
}
let addr = u16::from_le_bytes(addr_buff);
let mut sym_buff: Vec<u8> = Vec::new();
reader.read_until(0u8, &mut sym_buff)?;
let label = CStr::from_bytes_with_nul(sym_buff.as_slice())
.unwrap()
.to_str()
.unwrap()
.to_string();
eprintln!("#{:04X} {}", addr, label);
vm.symbols.insert(addr, label);
}
} }
// Execute the VM // Execute the VM
loop { loop {
if vm.is_halted() { if vm.is_halted() {
eprintln!("Interpreter is halted");
return Ok(()); return Ok(());
} }
@ -58,7 +29,7 @@ fn main() -> Result<(), std::io::Error> {
Ok(()) => return Ok(()), Ok(()) => return Ok(()),
Err(UxnError::Break) => { Err(UxnError::Break) => {
eprintln!("Interpreter broke; continuing..."); eprintln!("Interpreter broke");
continue; continue;
} }
Err(UxnError::ExecutionLimit(_)) => continue, Err(UxnError::ExecutionLimit(_)) => continue,

View file

@ -1,8 +1,5 @@
use std::env::args; use std::env::args;
use std::fs::File; use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Read;
use uxn::vm::Uxn; use uxn::vm::Uxn;
@ -10,28 +7,8 @@ fn main() -> Result<(), std::io::Error> {
let mut vm = Uxn::new(); let mut vm = Uxn::new();
let argv: Vec<String> = args().collect(); let argv: Vec<String> = args().collect();
let f: File = File::open(&argv[1])?; let symbols: File = File::open(&argv[1])?;
let mut reader = BufReader::new(f); vm.load_symbols(symbols)?;
loop {
let mut addr_buff = [0u8; 2];
match reader.read_exact(&mut addr_buff) {
Ok(_) => (),
Err(_) => break,
}
let addr = u16::from_le_bytes(addr_buff);
let mut sym_buff: Vec<u8> = Vec::new();
reader.read_until(0u8, &mut sym_buff)?;
vm.symbols.insert(
addr,
std::str::from_utf8(sym_buff.as_slice())
.unwrap()
.to_string(),
);
}
vm.debug_symbols(); vm.debug_symbols();
Ok(()) Ok(())

View file

@ -1,4 +1,5 @@
use std::io::{stderr, stdout, Write}; #[allow(unused_imports)]
use std::io::{stderr, stdin, stdout, Write};
use crate::device::{Device, DeviceError}; use crate::device::{Device, DeviceError};
use crate::vm::Uxn; use crate::vm::Uxn;
@ -16,18 +17,17 @@ use crate::vm::Uxn;
* *
*/ */
#[derive(Debug)] #[derive(Debug)]
#[allow(dead_code)]
pub struct ConsoleDevice { pub struct ConsoleDevice {
vector: u16, vector: u16,
inbuffer: [u8; 256], inbuffer: Vec<u8>,
inidx: usize,
} }
impl ConsoleDevice { impl ConsoleDevice {
pub fn new() -> ConsoleDevice { pub fn new() -> ConsoleDevice {
ConsoleDevice { ConsoleDevice {
vector: 0, vector: 0,
inidx: 255, inbuffer: Vec::new(),
inbuffer: [0; 256],
} }
} }
} }
@ -42,7 +42,7 @@ impl Device for ConsoleDevice {
* If inidx < inbuffer.size, we want to return the next character out of the * If inidx < inbuffer.size, we want to return the next character out of the
* inbuffer. * inbuffer.
*/ */
fn dei1(&mut self, vm: &mut Uxn, port: u8) -> Result<u8, DeviceError> { fn dei1(&mut self, _vm: &mut Uxn, port: u8) -> Result<u8, DeviceError> {
let slot = port & 0xF; let slot = port & 0xF;
match slot { match slot {
0x2 => Ok(0), 0x2 => Ok(0),
@ -51,7 +51,7 @@ impl Device for ConsoleDevice {
} }
// DEI2 is not supported on the terminal device // DEI2 is not supported on the terminal device
fn dei2(&mut self, vm: &mut Uxn, port: u8) -> Result<u16, DeviceError> { fn dei2(&mut self, _vm: &mut Uxn, port: u8) -> Result<u16, DeviceError> {
Err(DeviceError::PortTypeError( Err(DeviceError::PortTypeError(
port, port,
"double reads from the console are not supported".into(), "double reads from the console are not supported".into(),
@ -88,7 +88,7 @@ impl Device for ConsoleDevice {
} }
} }
fn deo2(&mut self, vm: &mut Uxn, port: u8, val: u16) -> Result<(), DeviceError> { fn deo2(&mut self, _vm: &mut Uxn, port: u8, _val: u16) -> Result<(), DeviceError> {
Err(DeviceError::PortTypeError( Err(DeviceError::PortTypeError(
port, port,
"double writes to the console are not supported".into(), "double writes to the console are not supported".into(),

View file

@ -1,4 +1,3 @@
use std::borrow::Borrow;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
@ -65,7 +64,7 @@ impl Device for SystemDevice {
// The state ports // The state ports
0xe => self.debug, 0xe => self.debug,
0xf => 0, 0xf => self.state,
_ => unreachable!(), _ => unreachable!(),
}) })
} }

View file

@ -3,7 +3,3 @@ pub mod isa;
pub mod memory; pub mod memory;
pub mod stack; pub mod stack;
pub mod vm; pub mod vm;
#[cfg(test)]
#[macro_use]
extern crate quickcheck;

186
src/vm.rs
View file

@ -1,6 +1,11 @@
use std::borrow::Borrow; use std::borrow::Borrow;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::ffi::CStr;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Read;
use std::rc::Rc; use std::rc::Rc;
use std::*; use std::*;
@ -98,28 +103,78 @@ impl Uxn {
vm vm
} }
pub fn load_rom(&mut self, file: File) -> Result<(), UxnError> {
let reader = BufReader::new(file);
let mut i = 0x0100u16;
for b in reader.bytes() {
self.sta1(i, b.unwrap())?;
i += 1;
}
Ok(())
}
pub fn load_symbols(&mut self, file: File) -> Result<(), io::Error> {
let mut reader: BufReader<File> = BufReader::new(file);
loop {
let mut addr_buff = [0u8; 2];
match reader.read_exact(&mut addr_buff) {
Ok(_) => (),
Err(_) => break,
}
let addr = u16::from_le_bytes(addr_buff);
let mut sym_buff: Vec<u8> = Vec::new();
reader.read_until(0u8, &mut sym_buff)?;
let label = CStr::from_bytes_with_nul(sym_buff.as_slice())
.unwrap()
.to_str()
.unwrap()
.to_string();
self.symbols.insert(addr, label);
}
Ok(())
}
pub fn is_halted(&mut self) -> bool { pub fn is_halted(&mut self) -> bool {
self.dei1(0x0f).unwrap() != 0 self.dei1(0x0f).unwrap() != 0
} }
pub fn is_tracing(&self) -> bool {
false
}
pub fn debug(&mut self) { pub fn debug(&mut self) {
let wst = self.wst.clone(); let wst = self.wst.clone();
let rst = self.rst.clone(); let rst = self.rst.clone();
eprint!( print!(
"<clock> #{:04X}\n<pc> #{:04X}\n<data stack>", "<symbols> #{:04X}\n<clock> #{:04X}\n<pc> #{:04X}\n<data stack>",
self.clock, self.pc self.symbols.len(),
self.clock,
self.pc
); );
let wst_idx = wst.borrow_mut().idx(); let wst_idx = wst.borrow_mut().idx();
for i in 0..wst_idx { if wst_idx != 0 {
eprint!(" #{:02X}", self.wst.borrow_mut().get1(i).unwrap()); for i in 0..wst_idx {
print!(" #{:02X}", self.wst.borrow_mut().get1(i).unwrap());
}
} else {
print!(" empty")
} }
eprint!("\n<return stack>"); print!("\n<return stack>");
let rst_idx = rst.borrow_mut().idx(); let rst_idx = rst.borrow_mut().idx();
for i in 0..rst_idx { if rst_idx != 0 {
eprint!(" #{:02X}", self.rst.borrow_mut().get1(i).unwrap()); for i in 0..rst_idx {
print!(" #{:02X}", self.rst.borrow_mut().get1(i).unwrap());
}
} else {
print!(" empty")
} }
eprint!("\n"); print!("\n");
} }
pub fn debug_symbols(&self) { pub fn debug_symbols(&self) {
@ -150,31 +205,65 @@ impl Uxn {
} }
pub fn sta1(&mut self, address: u16, val: u8) -> Result<(), MemoryError> { pub fn sta1(&mut self, address: u16, val: u8) -> Result<(), MemoryError> {
if self.is_tracing() {
eprintln!(
" STA #{:04X} ({}) <- #{:02X}",
address,
self.symbols.get(&address).unwrap_or(&"".to_string()),
val
)
}
self.memory.clone().borrow_mut().set1(address, val)?; self.memory.clone().borrow_mut().set1(address, val)?;
Ok(()) Ok(())
} }
pub fn sta2(&mut self, address: u16, val: u16) -> Result<(), MemoryError> { pub fn sta2(&mut self, address: u16, val: u16) -> Result<(), MemoryError> {
if self.is_tracing() {
eprintln!(
" STA #{:04X} ({}) <- #{:04X}",
address,
self.symbols.get(&address).unwrap_or(&"".to_string()),
val
)
}
self.memory.clone().borrow_mut().set2(address, val)?; self.memory.clone().borrow_mut().set2(address, val)?;
Ok(()) Ok(())
} }
pub fn lda1(&mut self, address: u16) -> Result<u8, MemoryError> { pub fn lda1(&self, address: u16) -> Result<u8, MemoryError> {
if address != self.pc && self.is_tracing() {
eprintln!(
" LDA #{:04X} ({})",
address,
self.symbols.get(&address).unwrap_or(&"".to_string()),
)
}
self.memory.clone().borrow_mut().get1(address) self.memory.clone().borrow_mut().get1(address)
} }
pub fn lda2(&mut self, address: u16) -> Result<u16, MemoryError> { pub fn lda2(&self, address: u16) -> Result<u16, MemoryError> {
if address != self.pc && self.is_tracing() {
eprintln!(
" LDA #{:04X} ({})",
address,
self.symbols.get(&address).unwrap_or(&"".to_string()),
)
}
self.memory.clone().borrow_mut().get2(address) self.memory.clone().borrow_mut().get2(address)
} }
pub fn branch(&mut self, address: u16) { pub fn branch(&mut self, address: u16) {
match self.symbols.get(&address) { match self.symbols.get(&address) {
Some(label) => eprintln!( Some(label) => {
"Branch #{:04X} to {} (#{:04X})", if self.is_tracing() {
self.pc - 1, eprintln!(
label, "Branch #{:04X} to {} (#{:04X})",
address self.pc - 1,
), label,
address
)
}
}
None => (), None => (),
} }
self.pc = address; self.pc = address;
@ -186,7 +275,11 @@ impl Uxn {
Err(e) => Err(UxnError::MemoryError(e)), Err(e) => Err(UxnError::MemoryError(e)),
Ok(icode) => { Ok(icode) => {
match self.symbols.get(&self.pc) { match self.symbols.get(&self.pc) {
Some(sym) => eprintln!("{}:", sym), Some(sym) => {
if self.is_tracing() {
eprintln!("{}:", sym)
}
}
None => (), None => (),
} }
@ -202,17 +295,19 @@ impl Uxn {
// Extract flags // Extract flags
let (kflag, rflag, sflag, icode5) = Icode::parse(icode); let (kflag, rflag, sflag, icode5) = Icode::parse(icode);
eprintln!( if self.is_tracing() {
" cycle #{:04X}; pc #{:04X}, rom pc: #{:04X}: {} ( {:05b} s: {:1x} r: {:1x} k: {:1x} )", eprintln!(
self.clock, " cycle #{:04X}; pc #{:04X}, rom pc: #{:04X}: {} ( {:05b} s: {:1x} r: {:1x} k: {:1x} )",
self.pc - 1, self.clock,
self.pc - 0x0101, self.pc - 1,
Icode::nameof(icode), self.pc - 0x0101,
icode5, Icode::nameof(icode),
sflag, icode5,
rflag, sflag,
kflag rflag,
); kflag
)
}
// Swizzle the stacks as needed // Swizzle the stacks as needed
let [wst, rst]: [Rc<RefCell<dyn Stack>>; 2] = { let [wst, rst]: [Rc<RefCell<dyn Stack>>; 2] = {
@ -254,17 +349,18 @@ impl Uxn {
let load = |addr: u16| -> Result<u16, MemoryError> { let load = |addr: u16| -> Result<u16, MemoryError> {
if sflag == 1 { if sflag == 1 {
self.memory.borrow_mut().get2(addr) self.lda2(addr)
} else { } else {
Ok(self.memory.borrow_mut().get1(addr)? as u16) Ok(self.lda1(addr)? as u16)
} }
}; };
let store = |addr: u16, value: u16| { // FIXME: Can't use self.sta1/sta2 due to *self reference uniqueness
let store = |this: &mut Uxn, addr: u16, val: u16| {
if sflag == 1 { if sflag == 1 {
self.memory.borrow_mut().set2(addr, value) this.sta2(addr, val)
} else { } else {
self.memory.borrow_mut().set1(addr, value as u8) this.sta1(addr, val as u8)
} }
}; };
@ -276,11 +372,6 @@ impl Uxn {
(1, _, _, Icode::BRK) => { (1, _, _, Icode::BRK) => {
// BRKk aka LIT -- a // BRKk aka LIT -- a
let val = load(self.pc)?; let val = load(self.pc)?;
if sflag == 0 {
eprintln!(" LIT #{:02X}", val);
} else {
eprintln!(" LIT #{:04X}", val);
}
push(wst.clone(), val)?; push(wst.clone(), val)?;
self.branch(self.pc + if sflag == 1 { 2 } else { 1 }); self.branch(self.pc + if sflag == 1 { 2 } else { 1 });
} }
@ -401,7 +492,7 @@ impl Uxn {
(_, _, _, Icode::STZ) => { (_, _, _, Icode::STZ) => {
// STZ val addr8 -- // STZ val addr8 --
let addr = wst.borrow_mut().pop1()? as u16; let addr = wst.borrow_mut().pop1()? as u16;
store(addr, pop(wst.clone())?)?; store(self, addr, pop(wst.clone())?)?;
} }
(_, _, _, Icode::LDR) => { (_, _, _, Icode::LDR) => {
// LDR addr8 -- a8 // LDR addr8 -- a8
@ -411,9 +502,9 @@ impl Uxn {
} }
(_, _, _, Icode::STR) => { (_, _, _, Icode::STR) => {
// STR val addr8 -- // STR val addr8 --
let delta = wst.borrow_mut().pop1()?; let delta = wst.borrow_mut().pop1()? as i8;
let addr = self.pc.wrapping_add(delta as u16); let addr = self.pc.wrapping_add(delta as u16);
store(addr, pop(wst)?)?; store(self, addr, pop(wst)?)?;
} }
(_, _, _, Icode::LDA) => { (_, _, _, Icode::LDA) => {
// LDA a16 // LDA a16
@ -423,27 +514,24 @@ impl Uxn {
(_, _, _, Icode::STA) => { (_, _, _, Icode::STA) => {
// STA val a16 -- // STA val a16 --
let addr = wst.borrow_mut().pop2()?; let addr = wst.borrow_mut().pop2()?;
store(addr, pop(wst.clone())?)?; store(self, addr, pop(wst.clone())?)?;
} }
(_, _, 0, Icode::DEI) => { (_, _, 0, Icode::DEI) => {
// DEI port8 -- a8 // DEI port8 -- a8
let mut wst = wst.borrow_mut(); let mut wst = wst.borrow_mut();
let port = wst.pop1()?; let port = wst.pop1()?;
eprintln!(" DEI got port {:02X}", port);
wst.push1(self.dei1(port)?)?; wst.push1(self.dei1(port)?)?;
} }
(_, _, 1, Icode::DEI) => { (_, _, 1, Icode::DEI) => {
// DEI2 port8 -- a16 // DEI2 port8 -- a16
let mut wst = wst.borrow_mut(); let mut wst = wst.borrow_mut();
let port = wst.pop1()?; let port = wst.pop1()?;
eprintln!(" DEI2 got port {:02X}", port);
wst.push2(self.dei2(port)?)?; wst.push2(self.dei2(port)?)?;
} }
(_, _, 0, Icode::DEO) => { (_, _, 0, Icode::DEO) => {
// DEO1 a8 port8 -- // DEO1 a8 port8 --
let mut wst = wst.borrow_mut(); let port = wst.borrow_mut().pop1()?;
let port = wst.pop1()?; let val = wst.borrow_mut().pop1()?;
let val = wst.pop1()?;
self.deo1(port, val).unwrap(); self.deo1(port, val).unwrap();
} }
(_, _, 1, Icode::DEO) => { (_, _, 1, Icode::DEO) => {
@ -473,10 +561,8 @@ impl Uxn {
} }
(_, _, _, Icode::DIV) => { (_, _, _, Icode::DIV) => {
// DIV a b -- c // DIV a b -- c
self.debug();
let b = pop(wst.clone())?; let b = pop(wst.clone())?;
let a = pop(wst.clone())?; let a = pop(wst.clone())?;
eprintln!(" DIV {:04X} {:04X}", a, b);
push(wst.clone(), a / b)?; push(wst.clone(), a / b)?;
} }
(_, _, _, Icode::AND) => { (_, _, _, Icode::AND) => {

20
tests/std_tests.rs Normal file
View file

@ -0,0 +1,20 @@
use std::fs::File;
use uxn::vm::*;
macro_rules! test_case {
($fname:expr) => {
concat!(env!("CARGO_MANIFEST_DIR"), "/resources/test/", $fname) // assumes Linux ('/')!
};
}
#[test]
fn reference_tests() {
let mut vm = Uxn::new();
let progf = File::open(test_case!("tests.rom")).unwrap();
vm.load_rom(progf).unwrap();
let symf = File::open(test_case!("tests.rom.sym")).unwrap();
vm.load_symbols(symf).unwrap();
assert_eq!(vm.run(0xFFFF), Err(UxnError::Break));
assert_ne!(vm.dei2(0x0000).unwrap(), 0xFFFF);
}