From 0850eb63c197cdc0f179277d5533c9ad929fb5ff Mon Sep 17 00:00:00 2001 From: Vince Weaver Date: Tue, 20 Feb 2018 23:18:28 -0500 Subject: [PATCH] chiptune: found bug in lz4 code --- asm_routines/lz4_decode.s | 47 +++++++++++++++++++++++++-------------- gr-sim/lz4d_verbose.c | 28 +++++++++++++++++------ 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/asm_routines/lz4_decode.s b/asm_routines/lz4_decode.s index 864290a5..57b30257 100644 --- a/asm_routines/lz4_decode.s +++ b/asm_routines/lz4_decode.s @@ -131,21 +131,21 @@ done_getsrc: ; buildcount ;============ buildcount: - ldx #1 - stx count+1 - cmp #$0f + ldx #1 ; high count starts at 1 + stx count+1 ; (loops at zero?) + cmp #$0f ; if LITERAL_COUNT < 15, we are done bne done_buildcount -minus_buildcount: - sta count - jsr getsrc - tax +buildcount_loop: + sta count ; save LITERAL_COUNT (15) + jsr getsrc ; get the next byte + tax ; put in X clc - adc count - bcc skip_buildcount + adc count ; add new byte to old value + bcc bc_8bit_oflow ; if overflow, increment high byte inc count+1 -skip_buildcount: - inx - beq minus_buildcount +bc_8bit_oflow: + inx ; check if read value was 255 + beq buildcount_loop ; if it was, keep looping and adding done_buildcount: rts @@ -168,14 +168,27 @@ putdst: putdst_end: rts - ;================ + ;============================= ; docopy - ;================ + ;============================= + ; copies ram[count+1]-1:X bytes + ; from src to dst docopy: + ; working around bug in original code that would loop 256 too + ; many times if incoming X was equal to 0 + cpx #$0 + bne docopy_loop jsr getput dex - bne docopy - dec count+1 - bne docopy + bne docopy_hack + +docopy_loop: + jsr getput ; get/put byte + dex ; decrement count + bne docopy_loop ; if not zero, loop +docopy_hack: + dec count+1 ; if zero, decrement high byte + bne docopy_loop ; if not zero, loop + rts diff --git a/gr-sim/lz4d_verbose.c b/gr-sim/lz4d_verbose.c index 0c51eb4a..248901de 100644 --- a/gr-sim/lz4d_verbose.c +++ b/gr-sim/lz4d_verbose.c @@ -39,22 +39,24 @@ void buildcount(void) { ram[count+1]=x; // ?? // stx count+1 cmp(0xf); // if 15, more complicated // cmp #$0f if (z==0) goto done_buildcount; // otherwise A is count // bne ++ -minus_buildcount: +buildcount_loop: ram[count]=a; //- sta count // printf("MBC "); - printf("\tADDITIONAL BUILDCOUNT "); getsrc(); //jsr getsrc + printf("\tADDITIONAL BUILDCOUNT 0x%x, adding 0x%x\n",a,ram[count]); x=a; //tax c=0; //clc adc(ram[count]); //adc count + printf("\tGOT 0x%x c=%d\n",a,c); if (c==0) goto skip_buildcount; // bcc + ram[count+1]++; //inc count+1 skip_buildcount: - x++; //+ inx - if (x==0) goto minus_buildcount; //beq - + printf("\tUPDATED COUNT %02X%02X\n",ram[count+1],a); + x++; // check if x is 255 //+ inx + if (x==0) goto buildcount_loop; // if so, add in next byte //beq - done_buildcount: ; //++ rts - printf("\tBUILDCOUNT= r[c+1]=%02X r[c]=%02X a=%02X x=%02X\n", - ram[count+1],ram[count],a,x); + printf("\tBUILDCOUNT= %02X%02X r[c+1]=%02X r[c]=%02X a=%02X x=%02X\n", + ram[count+1],a,ram[count+1],ram[count],a,x); } @@ -79,14 +81,26 @@ static void getput(void) { putdst(); // ; fallthrough } +// 202 -> 201 -> 100 -> 1FF +// 201 -> 100 -> 1FF +// 200 -> 2ff -> 2fe +// 1ff -> 1fe + static void docopy(void) { + printf("\tDOCOPY ENTRY: %02X%02X\n",ram[count+1],x); // docopy: + if (x==0) { + getput(); + x--; + goto dc_hack; + } docopy_label: - printf("\tDOCOPY %02X%02X: ",ram[count+1]-1,x); + printf("\tDOCOPY %02X%02X: ",ram[count+1],x); getput(); // jsr getput x--; // dex if (x!=0) goto docopy_label; // bne docopy +dc_hack: ram[count+1]--; // dec count+1 if (ram[count+1]!=0) goto docopy_label; //bne docopy //rts