From 295b4b6e0a1517de261ea6dc3702315dfef1f588 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Mon, 27 Apr 2020 15:04:37 -0400 Subject: [PATCH] z80a: add BIN( parameter Will be useful for assembling binaries for the TRS-80 which can't start at addr 0. --- blk/201 | 17 +++++++++-------- blk/202 | 17 ++++++++++------- blk/203 | 18 ++++++++++-------- blk/204 | 10 ++++++++++ blk/212 | 10 ++-------- blk/213 | 6 ++++-- blk/215 | 2 +- blk/243 | 17 ++++++++--------- blk/249 | 2 ++ blk/284 | 2 +- blk/286 | 3 +-- blk/289 | 2 +- blk/290 | 4 ++-- blk/295 | 4 ++-- blk/298 | 4 ++-- blk/300 | 2 +- blk/301 | 2 +- blk/302 | 2 +- blk/317 | 3 +-- blk/326 | 4 ++-- blk/330 | 2 +- emul/stage0.bin | Bin 7310 -> 7310 bytes 22 files changed, 72 insertions(+), 61 deletions(-) create mode 100644 blk/204 diff --git a/blk/201 b/blk/201 index ad97566..0cbae58 100644 --- a/blk/201 +++ b/blk/201 @@ -3,13 +3,14 @@ Forth words, opcode assembly is a bit different than with a typical assembler. For example, what would traditionally be "ld a, b" would become "A B LDrr,". -H@ offset at which we consider our PC 0. Used to compute PC. To -have a proper PC, call "H@ ORG !" at the beginning of your -assembly process. +BIN( is the addr at which the compiled binary will live. It is +often 0. -Labels are a convenient way of managing relative jump -calculations. Backward labels are easy. It is only a matter or -recording "HERE" and do subtractions. Forward labels record the -place where we should write the offset, and then when we get to -that point later on, the label records the offset there. +ORG is H@ offset at which we begin spitting binary. Used to +compute PC. To have a proper PC, call "H@ ORG !" at the +beginning of your assembly process. PC is H@ - ORG + BIN(. + + + + (cont.) diff --git a/blk/202 b/blk/202 index 99c472e..ca5d8c8 100644 --- a/blk/202 +++ b/blk/202 @@ -1,13 +1,16 @@ +Labels are a convenient way of managing relative jump +calculations. Backward labels are easy. It is only a matter or +recording "HERE" and do subtractions. Forward labels record the +place where we should write the offset, and then when we get to +that point later on, the label records the offset there. + To avoid using dict memory in compilation targets, we pre-declare label variables here, which means we have a limited number of it. For now, 4 ought to be enough. -Flow -There are 2 label types: backward and forward. For each type, -there are two actions: set and write. Setting a label is -declaring where it is. It has to be performed at the label's -destination. Writing a label is writing its offset difference -to the binary result. It has to be done right after a relative -jump operation. Yes, labels are only for relative jumps. + + + + (cont.) diff --git a/blk/203 b/blk/203 index 19fbc5b..1500cbb 100644 --- a/blk/203 +++ b/blk/203 @@ -1,14 +1,16 @@ +Flow + +There are 2 label types: backward and forward. For each type, +there are two actions: set and write. Setting a label is +declaring where it is. It has to be performed at the label's +destination. Writing a label is writing its offset difference +to the binary result. It has to be done right after a relative +jump operation. Yes, labels are only for relative jumps. + For backward labels, set happens before write. For forward labels, write happen before set. The write operation writes a dummy placeholder, and then the set operation writes the offset at that placeholder's address. -Variable actions are expected to be called with labels in -front of them. Example, "L2 FSET" - -About that "1 -": z80 relative jumps record "e-2", that is, -the offset that *counts the 2 bytes of the jump itself*. -Because we set the label *after* the jump OP1 itself, that's 1 -byte that is taken care of. We still need to adjust by another -byte before writing the offset. + (cont.) diff --git a/blk/204 b/blk/204 new file mode 100644 index 0000000..2ec567c --- /dev/null +++ b/blk/204 @@ -0,0 +1,10 @@ +Variable actions are expected to be called with labels in +front of them. Example, "L2 FSET" + +About that "1 -": z80 relative jumps record "e-2", that is, +the offset that *counts the 2 bytes of the jump itself*. +Because we set the label *after* the jump OP1 itself, that's 1 +byte that is taken care of. We still need to adjust by another +byte before writing the offset. + + diff --git a/blk/212 b/blk/212 index d99e524..01ef181 100644 --- a/blk/212 +++ b/blk/212 @@ -1,8 +1,2 @@ -( 59 == z80a's memory ) -H@ 0x59 RAM+ ! -10 ALLOT - -213 LOAD 215 LOAD 216 LOAD 217 LOAD 218 LOAD 219 LOAD -220 LOAD 222 LOAD 223 LOAD 224 LOAD 226 LOAD 228 LOAD -230 LOAD 232 LOAD 234 LOAD 236 LOAD 238 LOAD 240 LOAD -242 LOAD 243 LOAD 246 LOAD 247 LOAD 249 LOAD +213 LOAD Z80A$ +215 249 LOADR diff --git a/blk/213 b/blk/213 index ba45efe..abe21ea 100644 --- a/blk/213 +++ b/blk/213 @@ -1,7 +1,9 @@ : Z80AMEM+ 0x59 RAM+ @ + ; : ORG 0 Z80AMEM+ ; -: L1 2 Z80AMEM+ ; : L2 4 Z80AMEM+ ; -: L3 6 Z80AMEM+ ; : L4 8 Z80AMEM+ ; +: BIN( 2 Z80AMEM+ ; +: L1 4 Z80AMEM+ ; : L2 6 Z80AMEM+ ; +: L3 8 Z80AMEM+ ; : L4 10 Z80AMEM+ ; +: Z80A$ H@ 0x59 RAM+ ! 12 ALLOT 0 BIN( ! ; : A 7 ; : B 0 ; : C 1 ; : D 2 ; : E 3 ; : H 4 ; : L 5 ; : (HL) 6 ; : BC 0 ; : DE 1 ; : HL 2 ; : AF 3 ; : SP AF ; diff --git a/blk/215 b/blk/215 index 751f665..e7d890b 100644 --- a/blk/215 +++ b/blk/215 @@ -2,7 +2,7 @@ : SPLITB 256 /MOD SWAP ; -: PC H@ ORG @ - ; +: PC H@ ORG @ - BIN( @ + ; ( A, spits an assembled byte, A,, spits an assembled word Both increase PC. To debug, change C, to .X ) : A, C, ; : A,, SPLITB A, A, ; diff --git a/blk/243 b/blk/243 index 96de066..ed6ab3f 100644 --- a/blk/243 +++ b/blk/243 @@ -1,16 +1,15 @@ : JPccnn, SWAP <<3 0xc2 OR A, A,, ; +: BCALL, BIN( @ + CALLnn, ; +: BJP, BIN( @ + JPnn, ; +: BJPcc, BIN( @ + JPccnn, ; -( 26 == next ) -: JPNEXT, 26 JPnn, ; -( 29 == chkPS ) -: chkPS, 29 CALLnn, ; +: JPNEXT, 26 BJP, ; ( 26 == next ) -: CODE - ( same as CREATE, but with native word ) +: chkPS, 29 BCALL, ; ( 29 == chkPS ) + +: CODE ( same as CREATE, but with native word ) (entry) - ( 23 == nativeWord ) - 23 C, + 23 C, ( 23 == nativeWord ) ; - : ;CODE JPNEXT, ; diff --git a/blk/249 b/blk/249 index 329b69d..8223a05 100644 --- a/blk/249 +++ b/blk/249 @@ -4,3 +4,5 @@ : PUSH0, BC 0 LDddnn, BC PUSHqq, ; : PUSH1, BC 1 LDddnn, BC PUSHqq, ; : PUSHZ, BC 0 LDddnn, IFZ, BC INCss, THEN, BC PUSHqq, ; +: HLZ, A H LDrr, L ORr, ; +: DEZ, A D LDrr, E ORr, ; diff --git a/blk/284 b/blk/284 index be95eef..1039e61 100644 --- a/blk/284 +++ b/blk/284 @@ -6,7 +6,7 @@ 4 A, H@ XCURRENT ! ( set current tip of dict, 0x42 ) 0x17 A, ( nativeWord ) - 0x14 CALLnn, ( popRS ) + 0x14 BCALL, ( popRS ) HL PUSHqq, IY POPqq, ( --> IP ) JPNEXT, diff --git a/blk/286 b/blk/286 index 39dc7c7..428b893 100644 --- a/blk/286 +++ b/blk/286 @@ -1,8 +1,7 @@ CODE (?br) ( 0x67 ) HL POPqq, chkPS, - A H LDrr, - L ORr, + HLZ, JRZ, L2 BWR ( BR + 2. False, branch ) ( True, skip next 2 bytes and don't branch ) IY INCss, diff --git a/blk/289 b/blk/289 index 10b05ca..dd0fc4d 100644 --- a/blk/289 +++ b/blk/289 @@ -12,5 +12,5 @@ PC ORG @ 1 + ! ( main ) ( LATEST is a label to the latest entry of the dict. It is written at offset 0x08 by the process or person building Forth. ) - 0x08 LDHL(nn), + BIN( @ 0x08 + LDHL(nn), RAMSTART 0x02 + LD(nn)HL, ( RAM+02 == CURRENT cont. ) diff --git a/blk/290 b/blk/290 index 9d29ae6..c1c8f53 100644 --- a/blk/290 +++ b/blk/290 @@ -1,4 +1,4 @@ EXDEHL, HL L1 @ LDddnn, - 0x03 CALLnn, ( 03 == find ) - 0x33 JPnn, ( 33 == execute ) + 0x03 BCALL, ( 03 == find ) + 0x33 BJP, ( 33 == execute ) diff --git a/blk/295 b/blk/295 index d33893a..1e541e4 100644 --- a/blk/295 +++ b/blk/295 @@ -1,8 +1,7 @@ ( DE contains prev offset ) HL POPqq, ( <-- lvl 2 ) ( HL is prev field's addr. Is offset zero? ) - A D LDrr, - E ORr, + DEZ, IFNZ, ( get absolute addr from offset ) ( carry cleared from "or e" ) @@ -13,4 +12,5 @@ JRNZ, AGAIN, ( inner-B292, try to match again ) ( Z set? end of dict, unset Z ) + ( cont. ) diff --git a/blk/298 b/blk/298 index 5714b62..07762c6 100644 --- a/blk/298 +++ b/blk/298 @@ -2,6 +2,6 @@ L2 BSET ( abortUnderflow ) HL PC 7 - LDddnn, DE RAMSTART 0x02 + LDdd(nn), ( RAM+02 == CURRENT ) - 0x03 CALLnn, ( find ) - 0x33 JPnn, ( 33 == execute ) + 0x03 BCALL, ( find ) + 0x33 BJP, ( 33 == execute ) diff --git a/blk/300 b/blk/300 index 4ef0561..31a3c52 100644 --- a/blk/300 +++ b/blk/300 @@ -3,7 +3,7 @@ PC ORG @ 0x1b + ! ( next ) we jump to current IP, but we also take care of increasing it by 2 before jumping. ) ( Before we continue: are stacks within bounds? ) - 0x1d CALLnn, ( chkPS ) + 0x1d BCALL, ( chkPS ) ( check RS ) IX PUSHqq, HL POPqq, DE RS_ADDR LDddnn, diff --git a/blk/301 b/blk/301 index aa6d312..65c92b9 100644 --- a/blk/301 +++ b/blk/301 @@ -3,7 +3,7 @@ PC ORG @ 0x34 + ! ( execute ) ( DE points to wordref ) EXDEHL, E (HL) LDrr, - D 0 LDrn, + D BIN( @ 256 / LDrn, EXDEHL, ( HL points to code pointer ) DE INCss, diff --git a/blk/302 b/blk/302 index 1d4708c..fc7f1fe 100644 --- a/blk/302 +++ b/blk/302 @@ -4,7 +4,7 @@ PC ORG @ 0x0f + ! ( compiledWord ) 2. Set new IP to the second atom of the list 3. Execute the first atom of the list. ) IY PUSHqq, HL POPqq, ( <-- IP ) - 0x11 CALLnn, ( 11 == pushRS ) + 0x11 BCALL, ( 11 == pushRS ) EXDEHL, ( HL points to PFA ) ( While we increase, dereference into DE for execute call later. ) diff --git a/blk/317 b/blk/317 index 6962d0a..975c34b 100644 --- a/blk/317 +++ b/blk/317 @@ -1,8 +1,7 @@ CODE NOT HL POPqq, chkPS, - A L LDrr, - H ORr, + HLZ, PUSHZ, ;CODE diff --git a/blk/326 b/blk/326 index 11caee5..25e6c85 100644 --- a/blk/326 +++ b/blk/326 @@ -2,12 +2,12 @@ CODE >R HL POPqq, chkPS, ( 17 == pushRS ) - 17 CALLnn, + 17 BCALL, ;CODE CODE R> ( 20 == popRS ) - 20 CALLnn, + 20 BCALL, HL PUSHqq, ;CODE diff --git a/blk/330 b/blk/330 index 7629b60..67eaa2f 100644 --- a/blk/330 +++ b/blk/330 @@ -3,7 +3,7 @@ CODE _find ( cur w -- a f ) DE POPqq, ( cur ) chkPS, ( 3 == find ) - 3 CALLnn, + 3 BCALL, IFNZ, ( not found ) HL PUSHqq, diff --git a/emul/stage0.bin b/emul/stage0.bin index 2a0637969ca41cc73a32a1974f074ea87f7b11df..feb5f885a7719fd0b807fe1b4f4e04d36c0c2f07 100644 GIT binary patch delta 15 WcmeCP?6cg^&csx+b#o^Zs}uk)$OTvc delta 15 WcmeCP?6cg^&csx^WpgJJs}uk)$pu&d