Fix divide, fix tests
authorNick Downing <nick@ndcode.org>
Fri, 22 Apr 2022 00:00:09 +0000 (10:00 +1000)
committerNick Downing <nick@ndcode.org>
Fri, 22 Apr 2022 02:26:34 +0000 (12:26 +1000)
preForth/seedForth-z80.pre
preForth/seedForthInteractive.seedsource
preForth/z80.c

index ddc41ca..191b946 100644 (file)
@@ -332,7 +332,7 @@ code depth ( -- n )
        rr      h
        rr      l
        push    hl
-       jr      next1
+       jp next1 ;jr    next1
 ;
 
 code sp@ ( -- x )
@@ -392,6 +392,17 @@ code um/mod ( ud u1 -- u2 u3 )
        pop     bc ; pop u1
        pop     hl ; pop ud hi
        pop     de ; pop ud lo
+ push hl
+ call print_hexw
+ ld l,e
+ ld h,d
+ call print_hexw
+ ld a,0x2f
+ call print_char
+ ld l,c
+ ld h,b
+ call print_hexw
+ pop hl
 
        ld      a,16
        or      a
@@ -399,19 +410,41 @@ udiv_loop:
        ex      de,hl
        adc     hl,hl
        ex      de,hl
-       adc     hl,hl ; can't overflow, leaves cf = 0
+       adc     hl,hl
+       jr      nc,udiv_test
+       ; shift left has overflowed, so we can always subtract bc, and
+       ; always cf=1 to indicate subtraction went, record cf in quotient
+       sbc     hl,bc
+       jr      udiv_cont
+udiv_test:
+       ; shift left has not overflowed, see if we can subtract bc, cf=0
+       ; indicates subtraction went, record complement of cf in quotient
        sbc     hl,bc
-       jr      nc,udiv_skip
+       jr      nc,udiv_goes
        add     hl,bc
-udiv_skip:
+udiv_goes:
+       ccf
+udiv_cont:
        dec     a
        jr      nz,udiv_loop
        ex      de,hl
-       adc     hl,hl
+       adc     hl,hl ; record final quotient bit
+ ld a,0x3d
+ call print_char
+ push hl
+ call print_hexw
+ ld a,0x72
+ call print_char
+ ld l,e
+ ld h,d
+ call print_hexw
+ pop hl
+ ld a,0xa
+ call print_char
 
        push    de ; push u2 (remainder)
        push    hl ; push u1 (quotient)
-       
+
        exx
        jr      next1
 ;
@@ -428,6 +461,28 @@ next1:     ld      a,(bc)
        ld      h,a
        inc     bc
        jp      (hl)
+
+print_hexw:
+       ld      a,h
+       call    print_hexb
+       ld      a,l
+print_hexb:
+       push    af
+       rrca
+       rrca
+       rrca
+       rrca
+       call    print_hexn
+       pop     af
+print_hexn:
+       and     0xf
+       add     a,0x30
+       cp      0x3a
+       jr      c,print_char
+       add     a,0x41 - 0x3a
+print_char:
+       out     (STDOUT_PORT),a
+       ret
 ;
 
 : negate ( n1 -- n2 )
index 089137f..3c63819 100644 (file)
@@ -274,9 +274,9 @@ t{ 15 10 xor -> 5 }t
 t{ 21845 dup xor -> 0 }t  \ $5555
 t{ 21845 dup 2* xor -> 65535 }t
 
-t{ -2147483648 2147483647 <  -> -1 }t  \ 32bit $80000000 $7FFFFFFF
-t{ -2147483648 0 <  -> -1 }t \ 32bit $80000000 0
-t{ 0 -2147483648 <  -> 0 }t  \ 32bit 0 $80000000
+t{ -32768 32767 <  -> -1 }t  \ 16bit $8000 $7FFF
+t{ -32768 0 <  -> -1 }t \ 16bit $8000 0
+t{ 0 -32768 <  -> 0 }t  \ 16bit 0 $8000
 
 \ both positive
 t{ 10 10 < -> 0 }t
index a9b8df3..787e241 100644 (file)
@@ -192,6 +192,7 @@ void ioWrite(int param, ushort address, byte data) {
   case STDOUT_PORT:
     stdio_service(1, STDIO_BUFFER_SIZE);
     stdio_buffer_write(&stdout_buffer, data);
+ stdio_service(1, 1);
     break;
   case STDERR_PORT:
     stdio_service(1, STDIO_BUFFER_SIZE);