Make tree loader faster by inlining (at the expense of size)
authorNick Downing <nick@ndcode.org>
Tue, 14 Jun 2022 08:00:05 +0000 (18:00 +1000)
committerNick Downing <nick@ndcode.org>
Tue, 14 Jun 2022 08:01:11 +0000 (18:01 +1000)
loader/tree_decode.py
loader/tree_encode.py
loader/tree_loader.asm

index 39ec7ee..dbe554f 100755 (executable)
@@ -35,8 +35,8 @@ assert tree[0x32] == 0xa9 # lda #NN
 assert tree[0x34] & ~0x20 == 0x18 # clc
 bits = tree[0x33] | ((tree[0x34] & 0x20) << 3)
 
-assert tree[0x5c] == 0x4c # jmp NNNN
-start = tree[0x5d] | (tree[0x5e] << 8)
+assert tree[0x65] == 0x4c # jmp NNNN
+start = tree[0x66] | (tree[0x67] << 8)
 
 assert src == load_addr + len(tree) - 0x300
 dest += 1
@@ -72,16 +72,16 @@ i = len(tree) - 0x300
 j = len(bin)
 while count < 0:
   i -= n
-  assert i >= 0x86
+  assert i >= 0x98
   j = decode(tree[i:i + n], bits, j)
 
   i -= 1
-  assert i >= 0x86 - 1 # last one is garbage
+  assert i >= 0x98 - 1 # last one is garbage
   bits = (tree[i] << 1) | 1
   n = 8
 
   count += 1
-assert i == 0x86 - 1 # last one is garbage
+assert i == 0x98 - 1 # last one is garbage
 assert j == 0
 
 load_addr = start
index d7a5359..3e97ab6 100755 (executable)
@@ -17,7 +17,7 @@ out_bin = sys.argv[4]
 
 with open(tree_loader_bin, 'rb') as fin:
   tree_loader = list(fin.read())
-assert len(tree_loader) == 0x86
+assert len(tree_loader) == 0x98
 
 with open(in_bin, 'rb') as fin:
   data = list(fin.read())
@@ -158,25 +158,25 @@ tree[0x33] = bits & 0xff
 assert tree[0x34] == 0x18 # clc
 tree[0x34] |= (bits >> 3) & 0x20 # clc or sec
 
-assert tree[0x5c] == 0x4c # jmp NNNN
-tree[0x5d] = start & 0xff
-tree[0x5e] = start >> 8
+assert tree[0x65] == 0x4c # jmp NNNN
+tree[0x66] = start & 0xff
+tree[0x67] = start >> 8
 
-assert tree[0x61] == 0xbd # lda NNNN,x
-tree[0x62] = high1 & 0xff
-tree[0x63] = high1 >> 8
+assert tree[0x6a] == 0xbd # lda NNNN,x
+tree[0x6b] = high1 & 0xff
+tree[0x6c] = high1 >> 8
 
-assert tree[0x65] == 0xbd # lda NNNN,x
-tree[0x66] = right1 & 0xff
-tree[0x67] = right1 >> 8
+assert tree[0x6e] == 0xbd # lda NNNN,x
+tree[0x6f] = right1 & 0xff
+tree[0x70] = right1 >> 8
 
-assert tree[0x6d] == 0xbd # lda NNNN,x
-tree[0x6e] = high1 & 0xff
-tree[0x6f] = high1 >> 8
+assert tree[0x7f] == 0xbd # lda NNNN,x
+tree[0x80] = high1 & 0xff
+tree[0x81] = high1 >> 8
 
-assert tree[0x71] == 0xbd # lda NNNN,x
-tree[0x72] = left1 & 0xff
-tree[0x73] = left1 >> 8
+assert tree[0x83] == 0xbd # lda NNNN,x
+tree[0x84] = left1 & 0xff
+tree[0x85] = left1 >> 8
 
 load_size = len(tree)
 hdr = [load_addr & 0xff, load_addr >> 8, load_size & 0xff, load_size >> 8]
index 6e80f82..03e601d 100644 (file)
@@ -1,5 +1,7 @@
        .r65c02
 
+FASTER = 1
+
        .area   zpage
        .setdp
 
@@ -49,13 +51,19 @@ loop1:      lda     src
 0$:    dec     src
 
        lda     [src],y
-.if 1
+.if FASTER
+       bcs     token
+       sta     [dest],y
+
+       lda     dest
+       bne     1$
+       dec     dest + 1
+1$:    dec     dest
+.else ; smaller
        jmp     expand
-expand_ret0:
-.else
-       jsr     expand
 .endif
 
+expand_ret0:
        ;clc
        rol     bits
        bne     loop1
@@ -76,7 +84,8 @@ expand_ret0:
 
        jmp     0
 
-token: pha
+token: ; enter with 9-bit byte in cf:a (and we know cf=1, it is token)
+       pha
        tax
 
        ; asxxxx inconsistency:
@@ -85,13 +94,19 @@ token:      pha
        asl     a
 
        lda     0xaaaa,x                ; right1 (above decoded data)
-.if 1
+.if FASTER
+       bcs     token
+       sta     [dest],y
+
+       lda     dest
+       bne     0$
+       dec     dest + 1
+0$:    dec     dest
+.else ; smaller
        jmp     expand
-expand_ret1:
-.else
-       jsr     expand
 .endif
 
+expand_ret1:
        pla
        tax
 
@@ -99,9 +114,8 @@ expand_ret1:
        lsr     a
 
        lda     0xaaaa,x                ; left1 (above decoded data)
-expand:        ; a = token bits 0..7, cf = token bit 8
+expand:        ; enter with 9-bit byte in cf:a
        bcs     token
-
        sta     [dest],y
 
        lda     dest
@@ -109,11 +123,7 @@ expand:    ; a = token bits 0..7, cf = token bit 8
        dec     dest + 1
 0$:    dec     dest
 
-.if 1
        tsx
        inx
-       beq     expand_ret0
+       beq     expand_ret0 ; we need cf=0 here
        bne     expand_ret1
-.else
-       rts
-.endif