chiptune: more debugging of lz4 code

This commit is contained in:
Vince Weaver 2018-02-20 01:15:15 -05:00
parent e0946b1bdf
commit 0cb0911151
2 changed files with 62 additions and 42 deletions

View File

@ -51,16 +51,17 @@ lz4_decode:
unpmain: unpmain:
ldy #0 ldy #0 ; used to index
parsetoken: parsetoken:
jsr getsrc jsr getsrc ; get next token
pha pha ; save for later (need bottom 4 bits)
lsr ; number of literals in top 4 bits
lsr ; so shift into place
lsr lsr
lsr lsr
lsr beq copymatches ; if zero, then no literals
lsr
beq copymatches
jsr buildcount jsr buildcount
tax tax
@ -72,15 +73,20 @@ parsetoken:
bcs done bcs done
copymatches: copymatches:
jsr getsrc jsr getsrc ; get 16-bit delta value
sta delta sta delta
jsr getsrc jsr getsrc
sta delta+1 sta delta+1
pla
and #$0f pla ; restore token
and #$0f ; get bottom 4 bits
; match count. 0 means 4
; 15 means 19+, must be calculated
jsr buildcount jsr buildcount
clc clc
adc #4 adc #4 ; adjust count by 4 (minmatch)
tax tax
bcc copy_skip bcc copy_skip
inc count+1 inc count+1

View File

@ -21,24 +21,28 @@ static unsigned char input[MAX_INPUT];
static void getsrc(void) { static void getsrc(void) {
//getsrc: //getsrc:
a=ram[y_indirect(src,y)]; // lda (src), y a=ram[y_indirect(src,y)]; // lda (src), y
printf("LOAD %02X%02X: %02X\n",ram[src+1],ram[src],a);
ram[src]++; //inc src ram[src]++; //inc src
if (ram[src]!=0) goto done_getsrc; //bne + if (ram[src]!=0) goto done_getsrc; //bne +
ram[src+1]++; //inc src+1 ram[src+1]++; //inc src+1
done_getsrc: ; done_getsrc: ;
printf("LOADED %02X%02X-1: %02X\n",ram[src+1],ram[src],a);
//+ rts //+ rts
} }
void buildcount(void) { void buildcount(void) {
printf("\tBUILDCOUNT: A=0x%x\n",a);
//buildcount: //buildcount:
x=1; // ldx #1 x=1; // ?? // ldx #1
ram[count+1]=x; // stx count+1 ram[count+1]=x; // ?? // stx count+1
cmp(0xf); // cmp #$0f cmp(0xf); // if 15, more complicated // cmp #$0f
if (z==0) goto done_buildcount; // bne ++ if (z==0) goto done_buildcount; // otherwise A is count // bne ++
minus_buildcount: minus_buildcount:
ram[count]=a; //- sta count ram[count]=a; //- sta count
printf("MBC "); // printf("MBC ");
printf("\tADDITIONAL BUILDCOUNT ");
getsrc(); //jsr getsrc getsrc(); //jsr getsrc
x=a; //tax x=a; //tax
c=0; //clc c=0; //clc
@ -49,6 +53,8 @@ skip_buildcount:
x++; //+ inx x++; //+ inx
if (x==0) goto minus_buildcount; //beq - if (x==0) goto minus_buildcount; //beq -
done_buildcount: ; //++ rts done_buildcount: ; //++ rts
printf("\tBUILDCOUNT= r[c+1]=%02X r[c]=%02X a=%02X x=%02X\n",
ram[count+1],ram[count],a,x);
} }
@ -56,12 +62,14 @@ static void putdst(void) {
// printf("PUTADDR=%04X\n",y_indirect(dst,y)); // printf("PUTADDR=%04X\n",y_indirect(dst,y));
// putdst: // putdst:
ram[y_indirect(dst,y)]=a; // sta (dst), y ram[y_indirect(dst,y)]=a; // sta (dst), y
if (y!=0) printf("ERROR ERROR ERROR ERROR ERROR\n");
printf("\t\tPUT: %02X%02X = %02X\n",ram[dst+1],ram[dst],a);
ram[dst]++; // inc dst ram[dst]++; // inc dst
if (ram[dst]!=0) goto putdst_end; // bne + if (ram[dst]!=0) goto putdst_end; // bne +
ram[dst+1]++; // inc dst+1 ram[dst+1]++; // inc dst+1
putdst_end:; putdst_end:;
//+ rts //+ rts
printf("\tPUT: %02X%02X-1,%02X = %02X\n",ram[dst+1],ram[dst],y,a);
} }
static void getput(void) { static void getput(void) {
@ -73,7 +81,9 @@ static void getput(void) {
static void docopy(void) { static void docopy(void) {
// docopy: // docopy:
docopy_label: docopy_label:
printf("\tDOCOPY %02X%02X: ",ram[count+1]-1,x);
getput(); // jsr getput getput(); // jsr getput
x--; // dex x--; // dex
if (x!=0) goto docopy_label; // bne docopy if (x!=0) goto docopy_label; // bne docopy
@ -163,27 +173,30 @@ int main(int argc, char **argv) {
//unpack: //;unpacker entrypoint //unpack: //;unpacker entrypoint
goto unpmain; // jmp unpmain // goto unpmain; // jmp unpmain
unpmain: //unpack:
y=0; //ldy #0 y=0; // used for offset //ldy #0
parsetoken: parsetoken:
printf("PT "); printf("LOAD TOKEN: ");
getsrc(); // jsr getsrc getsrc(); // jsr getsrc
pha(); // pha // get token
lsr(); // lsr pha(); // save for later // pha
lsr(); // lsr lsr(); // num literals in top 4 // lsr
lsr(); // lsr lsr(); // lsr
lsr(); // lsr lsr(); // lsr
if (a==0) goto copymatches; // beq copymatches lsr(); // lsr
if (a==0) goto copymatches; // if zero, no literals // beq copymatches
buildcount(); // otherwise, build the count // jsr buildcount
buildcount(); // jsr buildcount
x=a; // tax x=a; // tax
docopy(); // jsr docopy docopy(); // jsr docopy
a=ram[src]; // lda src a=ram[src]; // lda src
cmp(ram[end]); // cmp end cmp(ram[end]); // cmp end
a=ram[src+1]; // lda src+1 a=ram[src+1]; // lda src+1
sbc(ram[end+1]); // sbc end+1 sbc(ram[end+1]); // sbc end+1
if (c) { if (c) {
printf("Done!\n"); printf("Done!\n");
@ -193,16 +206,17 @@ parsetoken:
} }
copymatches: copymatches:
printf("CM1 "); printf("\tDELTAL ");
getsrc(); // jsr getsrc getsrc(); // jsr getsrc
ram[delta]=a; // sta delta ram[delta]=a; // sta delta
printf("CM1 "); printf("\tDELTAH ");
getsrc(); // jsr getsrc getsrc(); // jsr getsrc
ram[delta+1]=a; // sta delta+1 ram[delta+1]=a; // sta delta+1
printf("DELTA is %02X%02X\n",ram[delta+1],ram[delta]); printf("\tDELTA is %02X%02X\n",ram[delta+1],ram[delta]);
pla(); // pla pla(); // restore token // pla
a=a&0xf; // and #$0f a=a&0xf; // get bottom 4 bits // and #$0f
buildcount(); // jsr buildcount buildcount(); // jsr buildcount
c=0; // clc c=0; // clc
adc(4); // adc #4 adc(4); // adc #4
x=a; // tax x=a; // tax
@ -213,7 +227,7 @@ copy_skip:
pha(); // pha pha(); // pha
a=ram[src]; // lda src a=ram[src]; // lda src
pha(); // pha pha(); // pha
// printf("SAVED SRC: %02X%02X\n",ram[src+1],ram[src]); printf("\tSAVED SRC: %02X%02X\n",ram[src+1],ram[src]);
// printf("CALCULATING: DST %02X%02X - DELTA %02X%02X\n",ram[dst+1],ram[dst],ram[delta+1],ram[delta]); // printf("CALCULATING: DST %02X%02X - DELTA %02X%02X\n",ram[dst+1],ram[dst],ram[delta+1],ram[delta]);
c=1; // sec c=1; // sec
a=ram[dst]; // lda dst a=ram[dst]; // lda dst
@ -222,13 +236,13 @@ copy_skip:
a=ram[dst+1]; // lda dst+1 a=ram[dst+1]; // lda dst+1
sbc(ram[delta+1]); // sbc delta+1 sbc(ram[delta+1]); // sbc delta+1
ram[src+1]=a; // sta src+1 ram[src+1]=a; // sta src+1
// printf("NEW SRC: %02X:%02X\n",ram[src+1],ram[src]); printf("\tNEW SRC: %02X%02X\n",ram[src+1],ram[src]);
docopy(); // jsr docopy docopy(); // jsr docopy
pla(); // pla pla(); // pla
ram[src]=a; // sta src ram[src]=a; // sta src
pla(); // pla pla(); // pla
ram[src+1]=a; // sta src+1 ram[src+1]=a; // sta src+1
printf("RESTORED SRC: %02X%02X\n",ram[src+1],ram[src]); printf("\tRESTORED SRC: %02X%02X\n",ram[src+1],ram[src]);
goto parsetoken; // jmp parsetoken goto parsetoken; // jmp parsetoken
done: done: