commit 0f4981c7e4fccf5ce8b23c884e3a3511869ad068
parent 1706c59a08ff17f17e82da825f104ece25e7ed96
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Mon, 5 Jun 2023 14:52:19 -0400
rpi: add /mod
Algorithm taken from ARM developer guide describing an unsigned divide macro
expansion.
Diffstat:
2 files changed, 32 insertions(+), 0 deletions(-)
diff --git a/fs/xcomp/arm/rpi/kernel.fs b/fs/xcomp/arm/rpi/kernel.fs
@@ -708,6 +708,32 @@ xcode xor ( n n -- n )
r0 ppop, eor) rTOP rdn) r0 rm) ,)
exit,
+pc ," divide by zero" 0 align4
+xcode /mod ( a b -- r q )
+ ( pc ) r0 pc>reg,
+ mov) r1 rd) 14 i) ,)
+ cmp) rTOP rn) 0 i) ,)
+ lblerrmsg abs>rel b) z) ,)
+ ldr) r0 rd) rPSP rn) ,) \ r0=dividend/remainder rTOP=divisor
+ mov) r1 rd) 0 i) ,) \ r1=quotient
+ mov) r2 rd) rTOP rm) ,) \ r2=tmp
+ \ double tmp until 2 * tmp > dividend
+ cmp) r2 rn) r0 rm) 1 lsr) ,)
+pc
+ mov) ls) r2 rd) r2 rm) 1 lsl) ,)
+ cmp) r2 rn) r0 rm) 1 lsr) ,)
+ ( pc ) abs>rel b) ls) ,)
+pc
+ cmp) r0 rn) r2 rm) ,) \ can we subtract temp from dividend?
+ sub) cs) r0 rdn) r2 rm) ,) \ if yes, do it
+ adc) r1 rdn) r1 rm) ,) \ double and add carry
+ mov) r2 rd) r2 rm) 1 lsr) ,)
+ cmp) r2 rn) rTOP rm) ,)
+ ( pc ) abs>rel b) hs) ,)
+ str) r0 rd) rPSP rn) ,) \ remainder
+ mov) rTOP rd) r1 rm) ,) \ quotient
+ exit,
+
\ Interpret loop
xcode ; ximm pushret,
wcall, popret,
diff --git a/fs/xcomp/rpiboot.fs b/fs/xcomp/rpiboot.fs
@@ -98,4 +98,10 @@ uartinit \ see the (wnf) if comment is mishandled
1+ c@ ')' = if exit then else drop then
again ; immediate
( hello, another comment! )
+
+\ Arithmetic
+: 0>= $80000000 < ;
+: 0< 0>= not ;
+: / /mod nip ;
+: mod /mod drop ;
prompt interactive!