diff --git a/chiptune_player/chiptune_player.s b/chiptune_player/chiptune_player.s index 13015c2a..ef963684 100644 --- a/chiptune_player/chiptune_player.s +++ b/chiptune_player/chiptune_player.s @@ -390,73 +390,26 @@ new_song: jsr clear_bottoms ; clear bottom of page 0/1 - lda #>LZ4_BUFFER + lda #>LZ4_BUFFER ; point to LZ4 data sta OUTH lda #(LZ4_BUFFER+3) @@ -472,12 +425,7 @@ bloop3: adc #0 sta LZ4_SRC+1 - jsr next_subsong - - ; should tail call - - rts - + ; Fall through to next_subsong ;================= ; next sub-song @@ -502,12 +450,53 @@ next_subsong: jsr lz4_decode ; decode + ; tail-call? + rts + + + ;=================== + ; print header info + ;=================== + ; shortcut to print header info + ; a = VTAB + +print_header_info: + + sta CV + + iny ; adjust pointer + tya + ldy #0 + clc + adc OUTL + sta OUTL + lda OUTH + adc #$0 + sta OUTH + + lda (OUTL),Y ; get HTAB value + sta CH + + inc OUTL ; increment 16-bits + bne bloop22 + inc OUTH +bloop22: + + jmp print_both_pages ; print, tail call + + + + ;========== ; filenames ;========== krw_file: + .asciiz "CHRISTMAS.KRW" + .asciiz "CAMOUFLAGE.KRW" + .asciiz "FIGHTING.KRW" + .asciiz "UNIVERSE.KRW" .asciiz "INTRO2.KRW" .asciiz "TECHNO.KRW" diff --git a/gr-sim/6502_emulate.c b/gr-sim/6502_emulate.c index d7ab117a..565b31d2 100644 --- a/gr-sim/6502_emulate.c +++ b/gr-sim/6502_emulate.c @@ -126,3 +126,12 @@ void lsr(void) { a=(temp_a&0xff); // printf("LSR A=%x\n",a); } + +unsigned char high(int value) { + return (value>>8)&0xff; +} + +unsigned char low(int value) { + return (value&0xff); +} + diff --git a/gr-sim/6502_emulate.h b/gr-sim/6502_emulate.h index 4eeaeb69..fc67e629 100644 --- a/gr-sim/6502_emulate.h +++ b/gr-sim/6502_emulate.h @@ -12,3 +12,7 @@ void cmp(int value); void pha(void); void pla(void); void lsr(void); + +unsigned char high(int value); +unsigned char low(int value); + diff --git a/gr-sim/Makefile b/gr-sim/Makefile index c5108b30..f4bf527a 100644 --- a/gr-sim/Makefile +++ b/gr-sim/Makefile @@ -7,7 +7,7 @@ SDL_INCLUDE= `sdl-config --cflags` all: fade fixed_point rainbow sparkle starfield starfield_fixed kaleido \ tfv mode7_demo text text_demo tfv_multiply rasterbars rasterbars_fixed \ - lz4d_verbose 6502_test + lz4d_verbose 6502_test krw_decode #### Library @@ -38,6 +38,14 @@ lz4d_verbose.o: lz4d_verbose.c demo_title.c #### +krw_decode: krw_decode.o 6502_emulate.o + $(CC) $(LFLAGS) $(SDL_LIBS) -o krw_decode krw_decode.o 6502_emulate.o + +krw_decode.o: krw_decode.c demo_title.c + $(CC) $(CFLAGS) -c krw_decode.c + +#### + fixed_point: fixed_point.o $(CC) $(LFLAGS) -o fixed_point fixed_point.o @@ -231,6 +239,6 @@ clean: rm -f *~ *.o gr-sim rainbow sparkle starfield starfield_fixed kaleido \ tfv text mode7_demo fade fixed_point tfv_multiply \ rasterbars rasterbars_fixed lookup_tables text_demo \ - lz4d_verbose 6502_test + lz4d_verbose 6502_test krw_decode diff --git a/gr-sim/krw_decode.c b/gr-sim/krw_decode.c new file mode 100644 index 00000000..01e0d0e0 --- /dev/null +++ b/gr-sim/krw_decode.c @@ -0,0 +1,368 @@ +#include +#include + +#include "6502_emulate.h" + +#define MAX_INPUT 65536 +static unsigned char input[MAX_INPUT]; + +#define LZ4_BUFFER 0x2000 +#define ORGOFFSET 0x6000 + + +#define src 0x0 +#define dst 0x2 +#define end 0x4 +#define count 0x6 +#define delta 0x8 + +#define CH 0x24 +#define CV 0x25 + +#define OUTL 0xFE +#define OUTH 0xFF + +static void getsrc(void) { +//getsrc: + 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 + if (ram[src]!=0) goto done_getsrc; //bne + + ram[src+1]++; //inc src+1 +done_getsrc: ; + + //+ rts +} + +void buildcount(void) { + printf("\tBUILDCOUNT: A=0x%x\n",a); +//buildcount: + x=1; // ?? // ldx #1 + 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 ++ +buildcount_loop: + ram[count]=a; //- sta count +// printf("MBC "); + 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: + 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= %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); +} + + +static void putdst(void) { +// printf("PUTADDR=%04X\n",y_indirect(dst,y)); + // putdst: + 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 + if (ram[dst]!=0) goto putdst_end; // bne + + ram[dst+1]++; // inc dst+1 +putdst_end:; + //+ rts + +} + +static void getput(void) { + // getput: + printf("GP "); + getsrc(); // jsr getsrc + 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: +docopy_label: + printf("\tDOCOPY %02X%02X: ",ram[count+1],x); + getput(); // jsr getput + x--; // dex + if (x!=0) goto docopy_label; // bne docopy + ram[count+1]--; // dec count+1 + if (ram[count+1]!=0) goto docopy_label; //bne docopy + //rts +} + + + + + +int lz4_decode(void) { + + FILE *fff; + + int size; + short orgoff,paksize,pakoff; + + //LZ4 data decompressor for Apple II + //Peter Ferrie (peter.ferrie@gmail.com) + + //init = 0 ;set to 1 if you know the values + //hiunp = 0 ;unpacker entirely in high memory + //hipak = 0 ;packed data entirely in high memory (requires hiunp) + + //oep = 0; //first unpacked byte to run, you must set this by yourself + orgoff = ORGOFFSET; //offset of first unpacked byte, you must set this by yourself + paksize = size-0xb-8; + // minus 4 for checksum at end + // not sure what other 4 is from? + // block checksum? though had that disabled? + + //size of packed data, you must set this by yourself if hiunp=0 + pakoff = LZ4_BUFFER+11; // 11 byte offset to data? + + +//LCBANK2 = $c083 +//MOVE = $fe2c + + a=(pakoff&0xff); //lda #>8); //lda #>pakoff + ram[src+1]=a; //sta src+1 + a=(pakoff+paksize)>>8; //lda #>(pakoff+paksize) + ram[end+1]=a; // sta end+1 + a=(orgoff>>8); //lda #>orgoff ;original unpacked data offset + ram[dst+1]=a; //sta dst+1 + a=(orgoff&0xff); //lda #127)) printf("."); + else printf("%c",temp); + } + printf("\n"); + } + } + + printf("\n"); + // rts + + fff=fopen("out.out","w"); + if (fff==NULL) { + fprintf(stderr,"Error opening!\n"); + return -1; + } + + printf("Out size=%d\n",out_size); + + fwrite(&ram[ORGOFFSET],1,out_size,fff); + + fclose(fff); + + return 0; +} + +static void print_both_pages(void) { + int i; + + for(i=0;i