From 61bb22e8c924f9ae8f039024d51a3c0fde76da68 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Wed, 25 Mar 2020 21:49:09 -0400 Subject: [PATCH] forth: straighten out "/MOD" The removal of the "divide" routine highlighted the fact that nativeWord wasn't properly stabilized. --- emul/forth/z80c.bin | Bin 288 -> 288 bytes forth/forth.asm | 60 ++++++++++++++++++-------------------------- forth/z80c.fs | 2 +- 3 files changed, 25 insertions(+), 37 deletions(-) diff --git a/emul/forth/z80c.bin b/emul/forth/z80c.bin index ffa729c26ab2f3a30ec277de063492952cb343e8..74adf553b3c011b243779439a72cb84a691871cf 100644 GIT binary patch literal 288 zcmWIY4`BcTb_ND|`t;~wDF!2#&;X#gC`24AeDx_v+&|1U2rho`??a%} z-={~fJ_QmW>ELijpgtL>abRhfoTHx$$SO&=k=0A{tCpmLO!WsE2?P=dsjc}{TfkBg z5IH2NwfR+Rz*5eNXu67lMmalx1%<#y0Uej8#sIP*0HRm~BzzF$=(m|*QLt)ZuqaSa Pjp1zuNOe2}0DTVt6s=p* literal 288 zcmWIY4`BcTb_RxRj1Mm!JS)p^_36>WQVd2ep#eZ~QHVHL`07)TxPO>y5M2D=--ke{ zzfX@|eF`K%(!t@5Kz%Y$ lvl 1 - ld l, h ; divide h by de - ld h, 0 - call .dv1 - ld b, c ; save result in b - ld a, l ; (remainder + l) / de - pop hl ; <-- lvl 1 - ld h, a -.dv1: - ld c, 0xff ; result in c -.dv2: - inc c ; dumb routine - call .subde ; divide using subtract and count - jr nc, .dv2 - add hl, de - ret -.subde: - ld a, l - sub e ; subtract de from hl - ld l, a - ld a, h - sbc a, d - ld h, a - ret - - ; Parse string at (HL) as a decimal value and return value in DE. ; Reads as many digits as it can and stop when: ; 1 - A non-digit character is read @@ -782,10 +754,10 @@ ROUTINE: ld de, compiledWord cp 'L' jr z, .end - ld de, nativeWord + ld de, JUMPTBL cp 'V' jr z, .end - ld de, JUMPTBL + ld de, JUMPTBL+3 cp 'N' jr z, .end ld de, sysvarWord @@ -800,7 +772,7 @@ ROUTINE: ld de, NUMBER cp 'M' jr z, .end - ld de, JUMPTBL+3 + ld de, JUMPTBL+6 cp 'P' jr nz, .notgood ; continue to end on match @@ -1433,6 +1405,8 @@ MULT: 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 @@ -1440,10 +1414,24 @@ MULT: DIVMOD: .dw nativeWord pop de - pop hl + pop bc call chkPS - call divide - push hl + 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 diff --git a/forth/z80c.fs b/forth/z80c.fs index e2f0e19..db5205b 100644 --- a/forth/z80c.fs +++ b/forth/z80c.fs @@ -24,7 +24,7 @@ CODE ROT HL POPqq, ( C ) DE POPqq, ( B ) BC POPqq, ( A ) - ROUTINE P CALLnn, + chkPS, DE PUSHqq, ( B ) HL PUSHqq, ( C ) BC PUSHqq, ( A )