From: rs <> Date: Fri, 10 Oct 2025 22:29:20 +0000 (-0500) Subject: Add divmod routine and test within emulator X-Git-Url: https://git.the-white-hart.net/?a=commitdiff_plain;h=cf7fa1856eb87570275abf0b7327cbfaa54f0de2;p=vhdl Add divmod routine and test within emulator --- diff --git a/projects/cpu_0/asm/emu.py b/projects/cpu_0/asm/emu.py index 8cc6832..ffff076 100755 --- a/projects/cpu_0/asm/emu.py +++ b/projects/cpu_0/asm/emu.py @@ -236,8 +236,8 @@ def main() -> int: emu.dump() emu.r_push(0xdeadbeef) emu.p_push(0x1234) - emu.p_push(0x567) - emu.pc = 'mul_uu' + emu.p_push(0x41) + emu.pc = 'div_uu' emu.dump() emu.run_until(0xdeadbeef) diff --git a/projects/cpu_0/asm/runtime.asm b/projects/cpu_0/asm/runtime.asm index e5ad06f..bb78d7b 100644 --- a/projects/cpu_0/asm/runtime.asm +++ b/projects/cpu_0/asm/runtime.asm @@ -190,7 +190,7 @@ mem_set_16: ( Multiply two unsigned integers ) -( a b -- b*a ) +( a b -- a*b ) mul_uu: #8 0i8 ~ >r >r >r #8 0i8 ( acc ) ( ctr b a ) .loop: @@ -224,13 +224,49 @@ mul_uu: drop drop drop ; +( Compute division with remainder of two unsigned integers ) +( a b -- a%b a/b ) +divmod_uu: + #8 0i8 ~ >r >r >r #8 0i8 ( rem ) ( ctr b a ) +.loop: + ( rem ) ( ctr b a ) + ( if ctr == 0 then done ) + r> r> r> dup jz .done + + ( rem a b ctr ) ( ) + ( decrement counter ) + lsr >r + + ( rem a b ) ( ctr ) + ( shift accumulator ) + >r >r shl r@ #32 0x80000000i32 & jz .nocarry + #8 1i8 | +.nocarry: + r> shl + + ( rem a ) ( ctr b ) + ( attempt subtraction ) + >r dup r> r@ swap >r + ( rem rem b ) ( ctr b a ) + swap - dup jn .next + swap r> #8 1i8 + >r +.next: + drop + ( rem ) ( ctr b a ) + jmp .loop + +.done: + ( rem a b ctr ) ( ) + drop drop ; + + ( Divide an unsigned integer by another unsigned integer ) -( a b -- b/a ) +( a b -- a/b ) div_uu: - ; + call divmod_uu swap drop ; ( Take one unsigned integer modulo another unsigned integer ) -( a b -- b%a ) +( a b -- a%b ) mod_uu: - ; + call divmod_uu drop ;