forth: Forth-ify "/MOD"

This commit is contained in:
Virgil Dupras 2020-03-25 22:51:23 -04:00
parent 61bb22e8c9
commit 52e6eaafc7
4 changed files with 76 additions and 36 deletions

Binary file not shown.

View File

@ -1405,44 +1405,13 @@ MULT:
jp next jp next
; Borrowed from http://wikiti.brandonw.net/
; Divides AC by DE and places the quotient in AC and the remainder in HL
.db "/MOD"
.fill 3
.dw $-MULT
.db 0
DIVMOD:
.dw nativeWord
pop de
pop bc
call chkPS
ld a, b ; AC
ld b, 16
ld hl, 0
.loop:
scf
rl c
rla
adc hl, hl
sbc hl, de
jr nc, .skip
add hl, de
dec c
.skip:
djnz .loop
ld b, a
push hl ; REM
push bc
jp next
; It might look peculiar to have specific words for "0" and "1", but although ; It might look peculiar to have specific words for "0" and "1", but although
; it slightly beefs ups the ASM part of the binary, this one-byte-save-per-use ; it slightly beefs ups the ASM part of the binary, this one-byte-save-per-use
; really adds up when we compare total size. ; really adds up when we compare total size.
.db "0" .db "0"
.fill 6 .fill 6
.dw $-DIVMOD .dw $-MULT
.db 0 .db 0
ZERO: ZERO:
.dw nativeWord .dw nativeWord

View File

@ -29,8 +29,13 @@
( -- ) ( -- )
: OP1 CREATE C, DOES> C@ A, ; : OP1 CREATE C, DOES> C@ A, ;
0xc9 OP1 RET,
0x76 OP1 HALT, 0x76 OP1 HALT,
0xc9 OP1 RET,
0x17 OP1 RLA,
0x07 OP1 RLCA,
0x1f OP1 RRA,
0x0f OP1 RRCA,
0x37 OP1 SCF,
( r -- ) ( r -- )
: OP1r : OP1r
@ -42,6 +47,7 @@
OR A, OR A,
; ;
0x04 OP1r INCr, 0x04 OP1r INCr,
0x05 OP1r DECr,
0x46 OP1r LDr(HL), 0x46 OP1r LDr(HL),
( r -- ) ( r -- )
@ -68,7 +74,7 @@
0xc5 OP1qq PUSHqq, 0xc5 OP1qq PUSHqq,
0xc1 OP1qq POPqq, 0xc1 OP1qq POPqq,
0x03 OP1qq INCss, 0x03 OP1qq INCss,
0x09 OP1qq ADHLss, 0x09 OP1qq ADDHLss,
( rd rr ) ( rd rr )
: OP1rr : OP1rr
@ -115,6 +121,22 @@
0x80 OP2br RESbr, 0x80 OP2br RESbr,
0x40 OP2br BITbr, 0x40 OP2br BITbr,
( bitwise rotation ops have a similar sig )
( r -- )
: OProt
CREATE C,
DOES>
0xcb A,
C@ ( r op )
OR A,
;
0x10 OProt RLr,
0x00 OProt RLCr,
0x18 OProt RRr,
0x08 OProt RRCr,
0x20 OProt SLAr,
0x38 OProt SRLr,
( cell contains both bytes. MSB is spit as-is, LSB is ORed with r ) ( cell contains both bytes. MSB is spit as-is, LSB is ORed with r )
( r -- ) ( r -- )
: OP2r : OP2r
@ -128,6 +150,18 @@
0xed41 OP2r OUT(C)r, 0xed41 OP2r OUT(C)r,
0xed40 OP2r INr(C), 0xed40 OP2r INr(C),
( ss -- )
: OP2ss
CREATE C,
DOES>
0xed A,
C@ SWAP ( op ss )
16 * ( op ss<< 4 )
OR A,
;
0x4a OP2ss ADCHLss,
0x42 OP2ss SBCHLss,
( dd nn -- ) ( dd nn -- )
: OP3ddnn : OP3ddnn
CREATE C, CREATE C,
@ -138,7 +172,7 @@
OR A, OR A,
SPLITB A, A, SPLITB A, A,
; ;
0x01 OP2ddnn LDddnn, 0x01 OP3ddnn LDddnn,
( nn -- ) ( nn -- )
: OP3nn : OP3nn
@ -150,8 +184,19 @@
0xcd OP3nn CALLnn, 0xcd OP3nn CALLnn,
0xc3 OP3nn JPnn, 0xc3 OP3nn JPnn,
: OPJR
CREATE C,
DOES>
C@ A, 2 - A,
;
0x18 OPJR JRe,
0x38 OPJR JRCe,
0x30 OPJR JRNCe,
0x28 OPJR JRZe,
0x20 OPJR JRNZe,
0x10 OPJR DJNZe,
( Specials ) ( Specials )
: JRe, 0x18 A, 2 - A, ;
: JPNEXT, ROUTINE N [LITN] JPnn, ; : JPNEXT, ROUTINE N [LITN] JPnn, ;
: CODE : CODE

View File

@ -110,6 +110,32 @@ CODE XOR
HL PUSHqq, HL PUSHqq,
;CODE ;CODE
( Borrowed from http://wikiti.brandonw.net/ )
( Divides AC by DE and places the quotient in AC and the
remainder in HL )
CODE /MOD
DE POPqq,
BC POPqq,
chkPS,
A B LDrr,
B 16 LDrn,
HL 0 LDddnn,
( loop )
SCF,
C RLr,
RLA,
HL ADCHLss,
DE SBCHLss,
4 JRNCe, ( skip )
DE ADDHLss,
C DECr,
( skip )
0 12 - DJNZe, ( loop )
B A LDrr,
HL PUSHqq,
BC PUSHqq,
;CODE
CODE C! CODE C!
HL POPqq, HL POPqq,
DE POPqq, DE POPqq,