Infrastructure for long math using a new page 2, fills up page 0, no page 2 yet
authorNick Downing <nick@ndcode.org>
Sun, 16 Jun 2019 13:40:12 +0000 (23:40 +1000)
committerNick Downing <nick@ndcode.org>
Sun, 16 Jun 2019 13:40:12 +0000 (23:40 +1000)
sm2.asm

diff --git a/sm2.asm b/sm2.asm
index 049c2e3..1839c51 100644 (file)
--- a/sm2.asm
+++ b/sm2.asm
@@ -1,5 +1,6 @@
 page0  =       1
 page1  =       2
+page2  =       3
 
        .area   SM (abs,ovr)
 
@@ -8,24 +9,102 @@ page1        =       2
 
        .org    page0 * 0x100
 
-       jp      start
+; administrative
 
 page0_trace:
-       jp      print_trace
+       jp      start ; will be overwritten with print_trace
 
 page0_esc:
        ld      l,c
        ld      h,b
        jp      (hl)
 
+page0_page2:
+       exx
+       pop     hl
+       exx
+       inc     h ; page 1
 page0_page1:
        pop     de
        ld      a,(bc)
        inc     bc
        ld      l,a
-       inc     h ; page 1
+       inc     h ; page 1 (or 2 if came from page0_page2)
+       jp      (hl)
+
+; long ops
+
+page0_stkld_l:
+       rst     8 ; ld hl,(bc)+
+       add     hl,sp
+       call    ld_l
+       jr      dispatch2
+
+page0_stkst_l:
+       rst     8 ; ld hl,(bc)+
+       add     hl,sp
+       call    st_l
+       jr      dispatch0
+
+page0_imm_l:
+;      ld      a,(bc)
+;      inc     bc
+;      exx
+;      ld      l,a
+;      exx
+;      ld      a,(bc)
+;      inc     bc
+;      exx
+;      ld      h,a
+;      exx
+;      inc     h ; page 1
+;      jr      page0_imm_w
+       call    imm_l
+dispatch2:
+       ld      a,(bc)
+       inc     bc
+       ld      l,a
+       ld      h,page2
        jp      (hl)
 
+page0_ld_l:
+       pop     hl
+       call    ld_l
+       jr      dispatch2
+
+page0_st_l:
+       exx
+       pop     hl
+       exx
+       pop     de
+       pop     hl
+       call    st_l
+       jr      dispatch0
+
+page0_imm_st_l:
+       call    imm_l
+       pop     hl
+       call    st_l
+       jr      dispatch0
+
+; less used long ops
+
+page0_imm_xchg_l:
+       call    imm_l
+       ;call   xchg_l
+       ;jr     dispatch0
+       jr      xchg_entry
+page0_xchg_l:
+       exx
+       pop     hl
+       exx
+       pop     de
+xchg_entry:
+       call    xchg_l
+       jr      dispatch0
+
+; less used word ops
+
 page0_imm_xchg_w:
        ld      a,(bc)
        inc     bc
@@ -38,11 +117,9 @@ page0_xchg_w:
        pop     hl
        ex      (sp),hl
        push    hl
-       ld      a,(bc)
-       inc     bc
-       ld      l,a
-       ld      h,page0
-       jp      (hl)
+       jr      dispatch0
+
+; control transfer ops
 
 page0_call:
        pop     de
@@ -97,7 +174,9 @@ page0_jmp:
        ld      l,a
        jp      (hl)
 
-       .org    page0 * 0x100 + 0x84
+       .org    page0 * 0x100 + 0x86
+
+; stack ops (reachable from start of page 1)
 
 imm_call:
        push    de
@@ -116,9 +195,10 @@ page0_imm_call:
        ld      l,a
        jp      (hl)
 
-stkptr:
-       push    de
-page0_stkptr:
+page0_ret:
+       pop     bc
+page0_stkadj:
+stkadj:
        ld      a,(bc)
        inc     bc
        ld      l,a
@@ -126,17 +206,16 @@ page0_stkptr:
        inc     bc
        ld      h,a
        add     hl,sp
-       ex      de,hl
+       ld      sp,hl
+dispatch0:
        ld      a,(bc)
        inc     bc
        ld      l,a
-       ld      h,page1
+       ld      h,page0
        jp      (hl)
 
-page0_ret:
-       pop     bc
-page0_stkadj:
-stkadj:
+page0_stkptr:
+stkptr:
        ld      a,(bc)
        inc     bc
        ld      l,a
@@ -144,16 +223,17 @@ stkadj:
        inc     bc
        ld      h,a
        add     hl,sp
-       ld      sp,hl
+       ex      de,hl
        ld      a,(bc)
        inc     bc
        ld      l,a
-       ld      h,page0
+       ld      h,page1
        jp      (hl)
 
-stkld_w:
-       push    de
+; word ops
+
 page0_stkld_w:
+stkld_w:
        ld      a,(bc)
        inc     bc
        ld      l,a
@@ -190,7 +270,7 @@ stkst_w:
        jp      (hl)
 
 page0_imm_w:
-       inc     h ; page1
+       inc     h ; page1 (or page2 if came from page0_imm_l)
 imm_w:
        ld      a,(bc)
        inc     bc
@@ -254,12 +334,14 @@ st_w:
 
 page1_imm_call:
        jr      imm_call
-page1_stkptr:
-       jr      stkptr
 page1_stkadj:
        push    de
        jr      stkadj
+page1_stkptr:
+       push    de
+       jr      stkptr
 page1_stkld_w:
+       push    de
        jr      stkld_w
 page1_stkst_w:
        jr      stkst_w
@@ -342,28 +424,28 @@ page1_or_w:
        ld      h,page1
        jp      (hl)
 
-;page1_imm_xor_w:
-;      ld      a,(bc)
-;      inc     bc
-;      xor     e
-;      ld      e,a
-;      ld      a,(bc)
-;      inc     bc
-;      xor     d
-;      ld      d,a
-;      ld      a,(bc)
-;      inc     bc
-;      ld      l,a
-;      jp      (hl)
-
-page1_imm_xor_w: ; xor is less common than and/or, so save space for immediate
+page1_imm_xor_w:
        ld      a,(bc)
-       ld      l,a
        inc     bc
+       xor     e
+       ld      e,a
        ld      a,(bc)
-       ld      h,a
        inc     bc
-       .db     0x3e ; ld a,
+       xor     d
+       ld      d,a
+       ld      a,(bc)
+       inc     bc
+       ld      l,a
+       jp      (hl)
+
+;page1_imm_xor_w: ; xor is less common than and/or, so save space for immediate
+;      ld      a,(bc)
+;      ld      l,a
+;      inc     bc
+;      ld      a,(bc)
+;      ld      h,a
+;      inc     bc
+;      .db     0x3e ; ld a,
 page1_xor_w:
        pop     hl
        ld      a,e
@@ -545,7 +627,7 @@ page1_lt_sw: ; put this at the end because it's the longest one
        ld      h,page0
        jp      (hl)
 
-; math package
+; word math package
 
 imm_sl_w:
        ex      de,hl
@@ -746,6 +828,74 @@ divnn:     ; negative dividend, negative divisor
        pop     bc
        ret
 
+; long math package
+
+imm_l:
+       ld      a,(bc)
+       inc     bc
+       exx
+       ld      l,a
+       exx
+       ld      a,(bc)
+       inc     bc
+       exx
+       ld      h,a
+       exx
+       ld      a,(bc)
+       inc     bc
+       ld      e,a
+       ld      a,(bc)
+       inc     bc
+       ld      d,a
+       ret
+
+ld_l:
+       ld      a,(hl)
+       inc     hl
+       exx
+       ld      l,a
+       exx
+       ld      a,(hl)
+       inc     hl
+       exx
+       ld      h,a
+       exx
+       ld      e,(hl)
+       inc     hl
+       ld      d,(hl)
+       ret
+
+st_l:
+       exx
+       ld      a,l
+       exx
+       ld      (hl),a
+       inc     hl
+       exx
+       ld      a,h
+       exx
+       ld      (hl),a
+       inc     hl
+       ld      (hl),e
+       inc     hl
+       ld      (hl),d
+       ret
+
+xchg_l:
+       exx
+       pop     de
+       exx
+       pop     hl
+       push    de
+       exx
+       push    hl
+       exx
+       push    hl
+       exx
+       push    de
+       exx
+       ret
+
 ; non-restoring division routine
 
 ; de = divisor, hl:a = dividend with hl = previous remainder, a = next byte
@@ -1095,6 +1245,14 @@ digits:
 ; sm code
 
 start:
+       ld      hl,print_trace
+       ld      (page0_trace + 1),hl
+
+       ld      hl,rst8
+       ld      de,8
+       ld      bc,7
+       ldir
+
        ld      h,page0
        call    page0_jmp
        .db     <page0_imm_call
@@ -1103,6 +1261,15 @@ start:
        .db     <page0_esc
        jp      0
 
+rst8:  ; ld hl,(bc)+
+       ld      a,(bc)
+       inc     bc
+       ld      l,a
+       ld      a,(bc)
+       inc     bc
+       ld      h,a
+       ret
+
 sm_main:
        ; create stack frame
        .db     <page0_stkadj