]> cloudbase.mooo.com Git - z180-stamp-cpm3.git/blobdiff - cbios/misc.180
b0call - call functions in bank 0 from any bank
[z180-stamp-cpm3.git] / cbios / misc.180
index 0352a591bfc7a11d7f8afb39356c7eeda9b0fad7..b7fee3f161da0a78aeccc5a42c492d455af289c8 100644 (file)
@@ -1,5 +1,5 @@
 
 
-       global add_hla,div32_16
+       global add_hla,div32_16,div32_r
        global ioiniml,ioini1l
        global intinit
        global bufinit
        global ioiniml,ioini1l
        global intinit
        global bufinit
@@ -13,8 +13,8 @@
        include config.inc
        include z180reg.inc
 
        include config.inc
        include z180reg.inc
 
-;--------------------------------------------------------------
-;
+
+;--------------------------------------------------------------------
 ; add a to hl
 ;
 ;    return:
 ; add a to hl
 ;
 ;    return:
@@ -30,14 +30,36 @@ add_hla:
        inc     h
        ret
 
        inc     h
        ret
 
+;--------------------------------------------------------------------
+; rounded div 32 by 16 bit
+;
+;      HLDE:   Dividend (x)
+;      BC:     Divisor  (y)
+;    return:
+;      HLDE:   Rounded Quotient (q)
+;      BC:     Remainder              (r)
+
+       cseg
+
+div32_r:
+       push bc
+       srl     b               ;y/2
+       rr      c
+       add     hl,bc           ;low x + y/2
+       pop     bc
+       jr      nc,div_r1
+       inc     hl
+div_r1:
+       ;fall thru
+
 ;--------------------------------------------------------------------
 ; Divide 32 bit by 16
 ;
 ;--------------------------------------------------------------------
 ; Divide 32 bit by 16
 ;
-;    HLDE:     Divident (x)
+;    DEHL:     Dividend (x)
 ;    BC:       Divisor  (y)
 ;
 ;  return:
 ;    BC:       Divisor  (y)
 ;
 ;  return:
-;    HLDE:     Quotient
+;    DEHL:     Quotient
 ;    BC:       Reminder
 
        cseg    ; common area
 ;    BC:       Reminder
 
        cseg    ; common area
@@ -46,26 +68,25 @@ div32_16:
        push    de              ;save alternate registers (de,bc)
        push    bc
        exx                     ;high
        push    de              ;save alternate registers (de,bc)
        push    bc
        exx                     ;high
-       push    de              ;low x
-       push    bc              ;low y
-       ld      bc,0            ;bc  = high y = 0
-       ld      d,b             ;de  = high r = 0
-       ld      e,c
-       ex      de,hl           ;de = x, hl = r
+       push    hl              ;lx
+       push    bc              ;ly
+       ld      bc,0            ;bc  = hy = 0
+       ld      h,b             ;hl  = hr = 0
+       ld      l,c
+                               ;de = x, hl = r
        exx                     ;low
        exx                     ;low
-       pop     bc              ;bc' = low y
-       ex      (sp),hl         ;hl' = low x, save alternate hl
-       ld      de,0            ;de' = low r = 0
+       pop     bc              ;bc' = ly
+       ex      (sp),hl         ;hl' = lx, save alternate hl
+       ld      de,0            ;de' = lr = 0
        ex      de,hl           ;de = x, hl = r
        exx                     ;high
        ld      a,32            ;count
 ;
        ex      de,hl           ;de = x, hl = r
        exx                     ;high
        ld      a,32            ;count
 ;
-; divide x/y
-;     x: hl
-;     y: de
-;   result:
-;     x/y: hl
-;     rem: de
+; start:
+;     de: x    (de: hx, de': lx)
+;     bc: y    (bc: hy, bc': ly)
+;     hl: 0
+;
 div_lp:                                ;do
        exx                     ;    low
        ex      de,hl           ;    x
 div_lp:                                ;do
        exx                     ;    low
        ex      de,hl           ;    x
@@ -96,15 +117,18 @@ div_no_restore:                    ;
        dec     a               ;
        jr      nz,div_lp       ;while (--count)
 
        dec     a               ;
        jr      nz,div_lp       ;while (--count)
 
-       ex      de,hl           ; q
+; result:
+;     de: q    (de: hq, de': lq)
+;     hl: r    (hl: hr, hl': lr)
+
        exx                     ;low
        exx                     ;low
-       ex      de,hl           ; q
+       ex      de,hl           ;hl = lq,  de = lr
 
 
-       ex      (sp),hl         ;low q
-       push    de              ;low r
+       ex      (sp),hl         ;lq
+       push    de              ;lr
        exx                     ;high
        exx                     ;high
-       pop     bc              ;bc = r
-       pop     de              ;de = low q
+       pop     bc              ;bc = lr
+       pop     hl              ;de = lq
 
        exx                     ;low
        pop     bc              ;restore alternate registers
 
        exx                     ;low
        pop     bc              ;restore alternate registers
@@ -253,8 +277,8 @@ cpu_frq:
        ld      d,h
        ld      e,l
        call    get_timer
        ld      d,h
        ld      e,l
        call    get_timer
-       push    hl
        push    de
        push    de
+       push    hl
 
 ; delay  ~8ms @ 18.432MHz  --> 147456 clock cycles
 ; delay ~10ms @ 18.432MHz  --> 184320 clock cycles
 
 ; delay  ~8ms @ 18.432MHz  --> 147456 clock cycles
 ; delay ~10ms @ 18.432MHz  --> 184320 clock cycles
@@ -268,21 +292,16 @@ dly_lp:
        sbc     hl,de           ; 10
        jr      nz,dly_lp       ; 6/8  -> 18
 
        sbc     hl,de           ; 10
        jr      nz,dly_lp       ; 6/8  -> 18
 
-       pop     de
        pop     hl
        pop     hl
+       pop     de
        call    get_timer
 
        call    get_timer
 
-;      ld      hl,4            ; round
-       ld      hl,5            ; round
-       add     hl,de
-       ex      de,hl
-       ld      hl,0
        ld      bc,10
        ld      bc,10
-       call    div32_16
-       ld      b,d
-       ld      c,e
-       ld      hl,00119h       ;18432000/(2**16)
-       ld      de,04000h       ;18432000%(2**16)
+       call    div32_r
+       ld      b,h
+       ld      c,l
+       ld      de,00119h       ;18432000/(2**16)
+       ld      hl,04000h       ;18432000%(2**16)
 
        ld      a,b
        or      a
 
        ld      a,b
        or      a
@@ -291,15 +310,15 @@ dly_lp:
        cp      2
        jr      c,cpuf_done
 cpuf_div:
        cp      2
        jr      c,cpuf_done
 cpuf_div:
-       call    div32_16
+       call    div32_r
 cpuf_done:
        ret
 
 ;--------------------------------------------------------------------
 
 get_timer:
 cpuf_done:
        ret
 
 ;--------------------------------------------------------------------
 
 get_timer:
-       push    hl
        push    de
        push    de
+       push    hl
        ld      hl,1*256 + 3            ; d = subcommand, e = command
        push    hl
        ld      hl,0
        ld      hl,1*256 + 3            ; d = subcommand, e = command
        push    hl
        ld      hl,0
@@ -315,8 +334,8 @@ get_timer:
 
        pop     bc
        inc     sp
 
        pop     bc
        inc     sp
-       pop     de
        pop     hl
        pop     hl
+       pop     de
        ret
 
 ;--------------------------------------------------------------------
        ret
 
 ;--------------------------------------------------------------------