gr_viewer: get it like the others

This commit is contained in:
Vince Weaver 2023-05-06 01:44:03 -04:00
parent cc165c3efb
commit 59095129e3
10 changed files with 630 additions and 337 deletions

View File

@ -4,7 +4,19 @@ DOS33 = ../../utils/dos33fs-utils/dos33
TOKENIZE = ../../utils/asoft_basic-utils/tokenize_asoft
PNG2DHGR = ../../utils/hgr-utils/png2dhgr
all: gr_viewer.dsk
all: make_gr_viewer
###
make_gr_viewer: make_gr_viewer.o
$(CC) $(LFLAGS) -o make_gr_viewer make_gr_viewer.o
make_gr_viewer.o: make_gr_viewer.c
$(CC) $(CFLAGS) -c make_gr_viewer.c
###
gr_viewer.dsk: HELLO LOADER
$(DOS33) -y gr_viewer.dsk SAVE A HELLO
@ -29,4 +41,4 @@ loader.o: loader.s
clean:
rm -f *~ *.o LOADER HELLO *.lst
rm -f *~ *.o LOADER HELLO *.lst make_gr_viewer

View File

@ -13,8 +13,8 @@ SET_GR = $C050
SET_TEXT = $C051
FULLGR = $C052
TEXTGR = $C053
PAGE0 = $C054
PAGE1 = $C055
PAGE1 = $C054
PAGE2 = $C055
LORES = $C056 ; Enable LORES graphics
HIRES = $C057 ; Enable HIRES graphics
AN3 = $C05E ; Annunciator 3
@ -43,7 +43,7 @@ PTRIG = $C070
;BELL = $FBDD ;; ring the bell
;BASCALC= $FBC1 ;;
;VTAB = $FC22 ;; VTAB to CV
;HOME = $FC58 ;; Clear the text screen
HOME = $FC58 ;; Clear the text screen
WAIT = $FCA8 ;; delay 1/2(26+27A+5A^2) us
;SETINV = $FE80 ;; INVERSE
;SETNORM= $FE84 ;; NORMAL

View File

@ -1,4 +1,4 @@
5 HOME
10 PRINT "SIMPLE LORES IMAGE VIEWER"
20 PRINT CHR$(4)+"BRUN LOADER"
20 PRINT CHR$(4)+"BRUN LORES"

View File

@ -0,0 +1,100 @@
; VMW Productions GR/ZX02 viewer
; by deater (Vince Weaver) <vince@deater.net>
.include "zp.inc"
.include "hardware.inc"
WHICH = $E0
lores_start:
;===================
; Init RTS disk code
;===================
jsr rts_init
;===================
; set graphics mode
;===================
jsr HOME
bit LORES
bit FULLGR
bit SET_GR
bit PAGE1
lda #0
sta WHICH
;===================
; Load graphics
;===================
load_loop:
;=============================
ldx WHICH
lda filenames_low,X
sta OUTL
lda filenames_high,X
sta OUTH
jsr load_image
wait_until_keypress:
lda KEYPRESS ; 4
bpl wait_until_keypress ; 3
bit KEYRESET ; clear the keyboard buffer
cmp #$88 ; left button
bne inc_which
dec WHICH
bpl which_ok
ldx #(MAX_FILES-1)
bne store_which ; bra
inc_which:
inc WHICH
ldx WHICH
cpx #MAX_FILES
bcc which_ok ; blt
ldx #0
store_which:
stx WHICH
which_ok:
jmp load_loop
;==========================
; Load Image
;===========================
load_image:
bit PAGE1
jsr opendir_filename ; open and read entire file into memory
lda #<$A000
sta zx_src_l+1
lda #>$A000
sta zx_src_h+1
lda #$08
jsr zx02_full_decomp
bit PAGE2
rts
.include "zx02_optim.s"
.include "rts.s"

View File

@ -0,0 +1,246 @@
/* TODO: bail if will over-write files */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_FILES 1024
static char *png_filenames[MAX_FILES];
static char *zx02_filenames[MAX_FILES];
static char *labels[MAX_FILES];
/* Format:
file_label FILE.ZX02 file.png
*/
int main(int argc, char **argv) {
char *result;
char string[BUFSIZ];
int num_files=0;
int i,j,count,line=0;
char temp_png[BUFSIZ];
char temp_zx02[BUFSIZ];
char temp_label[BUFSIZ];
char s_filename[BUFSIZ];
char filename_base[BUFSIZ];
FILE *sss;
/******************************************/
/* parse command line */
/******************************************/
if (argc>1) {
strcpy(filename_base,argv[1]);
}
else {
strcpy(filename_base,"lores_plain");
}
sprintf(s_filename,"%s.s",filename_base);
/******************************************/
/* scan in data */
/******************************************/
while(1) {
line++;
result=fgets(string,BUFSIZ,stdin);
if (result==NULL) {
break;
}
if (result[0]=='#') continue;
count=sscanf(string,"%s %s %s",temp_label,temp_zx02,temp_png);
if (count!=3) {
fprintf(stderr,"WARNING! weird input line %d (%d)\n",line,count);
fprintf(stderr,"\t%s",string);
}
else {
labels[num_files]=strdup(temp_label);
png_filenames[num_files]=strdup(temp_png);
zx02_filenames[num_files]=strdup(temp_zx02);
num_files++;
if (num_files>=MAX_FILES) {
fprintf(stderr,"ERROR! Too many files!\n");
exit(-1);
}
}
}
if (num_files==0) {
fprintf(stderr,"ERROR! No files found!\n");
exit(-1);
}
/******************************************/
/* generate S file */
/******************************************/
sss=fopen(s_filename,"w");
if (sss==NULL) {
fprintf(stderr,"ERRROR opening %s\n",s_filename);
exit(-1);
}
fprintf(sss,"; Some nice hires images\n\n");
fprintf(sss,".include \"../lores_main.s\"\n\n");
fprintf(sss,"MAX_FILES = %d\n\n",num_files);
fprintf(sss,"filenames_low:\n");
for(i=0;i<num_files;i++) {
fprintf(sss,"\t.byte <%s_filename\n",labels[i]);
}
fprintf(sss,"\n");
fprintf(sss,"filenames_high:\n");
for(i=0;i<num_files;i++) {
fprintf(sss,"\t.byte >%s_filename\n",labels[i]);
}
fprintf(sss,"\n");
fprintf(sss,"; filename to open is 30-character Apple text:\n");
for(i=0;i<num_files;i++) {
fprintf(sss,"%s_filename:\t; %s\n",labels[i],zx02_filenames[i]);
fprintf(sss,"\t.byte ");
for(j=0;j<strlen(zx02_filenames[i]);j++) {
if (j!=0) fprintf(sss,",");
fprintf(sss,"\'%c\'|$80",zx02_filenames[i][j]);
}
fprintf(sss,",$00\n\n");
}
fclose(sss);
/******************************************/
/* generate Makefile */
/******************************************/
/* don't over-write as a precaution */
if (access("Makefile", F_OK) == 0) {
fprintf(stderr,"Makefile already exists! Out of caution giving up.\n");
exit(-1);
}
sss=fopen("Makefile","w");
if (sss==NULL) {
fprintf(stderr,"ERRROR opening Makefile\n");
exit(-1);
}
fprintf(sss,"include ../../../Makefile.inc\n\n");
fprintf(sss,"ZX02 = ~/research/6502_compression/zx02.git/build/zx02\n");
fprintf(sss,"PNG_TO_GR = ../../../utils/gr-utils/png2gr\n");
fprintf(sss,"LINKER_SCRIPTS = ../../../linker_scripts\n");
fprintf(sss,"DOS33 = ../../../utils/dos33fs-utils/dos33\n");
fprintf(sss,"EMPTY_DISK = ../../../empty_disk/empty.dsk\n");
fprintf(sss,"TOKENIZE = ../../../utils/asoft_basic-utils/tokenize_asoft\n");
fprintf(sss,"\n");
fprintf(sss,"all:\t%s.dsk\n\n",filename_base);
fprintf(sss,"####\n\n");
fprintf(sss,"LORES:\t%s.o\n",filename_base);
fprintf(sss,"\tld65 -o LORES %s.o -C $(LINKER_SCRIPTS)/apple2_1000.inc\n\n",filename_base);
fprintf(sss,"%s.o:\t%s.s ../lores_main.s ../zx02_optim.s \\\n",filename_base,filename_base);
fprintf(sss,"\t\t../zp.inc ../hardware.inc\n");
fprintf(sss,"\tca65 -o %s.o %s.s -l %s.lst\n",filename_base,filename_base,filename_base);
fprintf(sss,"####\n\n");
fprintf(sss,"HELLO:\t../hello.bas\n");
fprintf(sss,"\t$(TOKENIZE) < ../hello.bas > HELLO\n\n");
fprintf(sss,"####\n\n");
fprintf(sss,"%s.dsk:\tHELLO LORES\\\n",filename_base);
for(i=0;i<num_files;i++) {
fprintf(sss,"\t\t%s.gr.zx02",png_filenames[i]);
if (i!=(num_files-1)) {
fprintf(sss,"\\");
}
fprintf(sss,"\n");
}
fprintf(sss,"\tcp $(EMPTY_DISK) %s.dsk\n",filename_base);
fprintf(sss,"\t$(DOS33) -y %s.dsk SAVE A HELLO\n",filename_base);
fprintf(sss,"\t$(DOS33) -y %s.dsk BSAVE -a 0x1000 LORES\n",filename_base);
for(i=0;i<num_files;i++) {
fprintf(sss,"\t$(DOS33) -y %s.dsk BSAVE -a 0xa000 %s.gr.zx02 %s\n",
filename_base,
png_filenames[i],zx02_filenames[i]);
}
fprintf(sss,"####\n\n");
fprintf(sss,"clean:\n");
fprintf(sss,"\trm -f *~ *.o *.lst\n\n");
fclose(sss);
/******************************************/
/* generate Makefile.graphics */
/******************************************/
sss=fopen("Makefile.graphics","w");
if (sss==NULL) {
fprintf(stderr,"ERRROR opening Makefile\n");
exit(-1);
}
fprintf(sss,"include ../../../Makefile.inc\n\n");
fprintf(sss,"ZX02 = ~/research/6502_compression/zx02.git/build/zx02\n");
fprintf(sss,"PNG_TO_GR = ../../../utils/gr-utils/png2gr\n");
fprintf(sss,"LINKER_SCRIPTS = ../../../linker_scripts\n");
fprintf(sss,"DOS33 = ../../../utils/dos33fs-utils/dos33\n");
fprintf(sss,"EMPTY_DISK = ../../../empty_disk/empty.dsk\n");
fprintf(sss,"TOKENIZE = ../../../utils/asoft_basic-utils/tokenize_asoft\n");
fprintf(sss,"\n");
fprintf(sss,"all:\\\n");
for(i=0;i<num_files;i++) {
fprintf(sss,"\t\t%s.gr.zx02",png_filenames[i]);
if (i!=(num_files-1)) {
fprintf(sss,"\\");
}
fprintf(sss,"\n");
}
// fprintf(sss,"\tcp $(EMPTY_DISK) %s.dsk\n",filename_base);
// fprintf(sss,"\t$(DOS33) -y %s.dsk SAVE A HELLO\n",filename_base);
// fprintf(sss,"\t$(DOS33) -y %s.dsk BSAVE -a 0x0c00 LORES\n",filename_base);
// for(i=0;i<num_files;i++) {
// fprintf(sss,"\t$(DOS33) -y %s.dsk BSAVE -a 0xa000 %s.gr.zx02 %s\n",
// filename_base,
// png_filenames[i],zx02_filenames[i]);
// }
for(i=0;i<num_files;i++) {
fprintf(sss,"\n####\n\n");
fprintf(sss,"%s.gr.zx02:\t%s.gr\n",png_filenames[i],png_filenames[i]);
fprintf(sss,"\t$(ZX02) %s.gr %s.gr.zx02\n",png_filenames[i],png_filenames[i]);
fprintf(sss,"\n");
fprintf(sss,"%s.gr:\t%s.png\n",png_filenames[i],png_filenames[i]);
fprintf(sss,"\t$(PNG_TO_GR) %s.png %s.gr\n",png_filenames[i],png_filenames[i]);
fprintf(sss,"\n");
}
fprintf(sss,"####\n\n");
fprintf(sss,"clean:\n");
fprintf(sss,"\trm -f *~ *.o *.lst\n\n");
fclose(sss);
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -0,0 +1,2 @@
# label dos3.3 name prefix for png file
lady1 LADY1.ZX02 lady1

View File

@ -1,168 +1,36 @@
; Loader for GR-loader
nibtbl = $300 ; nothing uses the bottom 128 bytes of $300, do they?
bit2tbl = $380 ; bit2tbl: .res 86 ; = nibtbl+128
filbuf = $3D6 ; filbuf: .res 4 ; = bit2tbl+86
; read any file slot 6 version
; based on FASTLD6 and RTS copyright (c) Peter Ferrie 2011-2013,2018
; modified to assembled with ca64 -- vmw
; modified to assembled with ca65 -- vmw
; added code to patch it to run from current disk slot -- vmw
; USAGE:
; file to load in namlo:namhi
; Loads file contents to addr from filesystem
; also stores filesize in ldsizel:ldsizeh
.include "hardware.inc"
LZ4_SRC = $00
LZ4_DST = $02
LZ4_END = $04
COUNT = $06
DELTA = $08
WHICH = $f0
adrlo = $26 ; constant from boot prom
adrhi = $27 ; constant from boot prom
tmpsec = $3c ; constant from boot prom
reqsec = $3d ; constant from boot prom
sizelo = $44
sizehi = $45
secsize = $46
TEMPY = $fa
namlo = $fb
namhi = $fc
step = $fd ; state for stepper motor
tmptrk = $fe ; temporary copy of current track
phase = $ff ; current phase for /seek
OUTL = $fe ; for picking filename
OUTH = $ff
dirbuf = $b000
dirbuf = $900
; note, don't put this immediately below
; the value being read as destaddr-4
; is temporarily overwritten during read
; process
; note also, can't load file bigger than $8000 (32k) in size?
; seems to break things?
start:
jsr init ; unhook DOS, init nibble table
lda #1
sta WHICH
bit SET_GR
bit LORES
bit FULLGR
bit PAGE1
clear_out_ram:
ldx #$14
stx OUTH
ldy #0
sty OUTL
lda #$77
clear_ram_outer:
ldy #0
clear_ram_inner:
dey
sta (OUTL),Y
bne clear_ram_inner
inx
stx OUTH
cpx #$c0
bne clear_ram_outer
;======================
which_load_loop:
ldy #0 ; load first filename
lda filenames_low,Y
sta OUTL
lda filenames_high,Y
sta OUTH
opendir_filename:
; clear out the filename with $A0 (space)
lda #<filename
sta namlo
lda #>filename
sta namhi
ldy #29
wipe_filename_loop:
lda #$A0
sta (namlo),Y
dey
bpl wipe_filename_loop
ldy #0
copy_filename_loop:
lda (OUTL),Y
beq copy_filename_done
ora #$80
sta (namlo),Y
iny
bne copy_filename_loop
copy_filename_done:
jsr opendir ; open and read entire file into memory
;======================
;==============================
;==============================
loop_forever:
; wait until keypressed
bit KEYRESET
wait_until_keypress:
lda KEYPRESS ; check if keypressed
bpl wait_until_keypress ; if not, loop
bit KEYRESET
inc WHICH
lda WHICH
cmp #10
bne no_new
lda #1
sta WHICH
no_new:
clc
adc #'0'
sta gr_filename+2
; hope they updated the WHICH_LOAD value
jmp which_load_loop
; filename to open is 30-character Apple text, must be padded with space ($A0)
; filename to open is 30-character Apple text:
filename:
.byte $A0,$A0,$A0,$A0,$A0,$A0,$A0,$A0,$A0,$A0
.byte $A0,$A0,$A0,$A0,$A0,$A0,$A0,$A0,$A0,$A0
.byte $A0,$A0,$A0,$A0,$A0,$A0,$A0,$A0,$A0,$A0
filenames_low:
.byte <gr_filename
filenames_high:
.byte >gr_filename
gr_filename:
.byte "F01.GR",0
.byte $A0,$A0,$A0,$A0,$A0,$A0,$A0,$A0
.byte $A0,$A0,$A0,$A0,$A0,$A0,$A0,$A0
.byte $A0,$A0,$A0,$A0,$A0,$A0,$A0,$A0
.byte $A0,$A0,$A0,$A0,$A0,$A0
;=======================================================
;=======================================================
;=======================================================
;unhook DOS and build nibble table
init:
rts_init:
; patch to use current drive
; locate input paramater list
@ -230,6 +98,45 @@ L3: inx ; increment x x=4, a=0f
rts
;=======================================================
;=======================================================
;=======================================================
; filename in OUTL:OUTH
opendir_filename:
; clear out the filename with $A0 (space)
lda #<filename
sta namlo
lda #>filename
sta namhi
ldy #29
wipe_filename_loop:
lda #$A0
sta (namlo),Y
dey
bpl wipe_filename_loop
ldy #0
copy_filename_loop:
lda (OUTL),Y
beq copy_filename_done
sta (namlo),Y
iny
bne copy_filename_loop
copy_filename_done:
; fallthrough
;=======================================================
;=======================================================
;=======================================================
;===========================
; opendir
;===========================
@ -335,13 +242,18 @@ L5:
; increase load size by 4, to account for offst and length
lda filbuf+2
sta ldsizel ; store out raw size
adc #3
sta sizelo
sta secsize
lda filbuf+3
sta ldsizeh ; store out raw size
adc #0
sta sizehi
sta ldsizeh
beq readfirst
lda #0 ; was **stz secsize**
sta secsize
@ -420,7 +332,6 @@ L7:
iny
dex
bpl L7
rts
@ -614,184 +525,6 @@ L26:
bne copy_cur
.if 0
;==============================
; old code
; [re-]read sector
re_read_addr:
jsr readadr
repeat_until_right_sector:
cmp reqsec
bne re_read_addr
;==========================
; read sector data
;==========================
;
readdata:
mlsmc07:
ldy $c0ec ; read data until valid
bpl readdata
find_D5:
cpy #$d5 ; if not D5, repeat
bne readdata
find_AA:
mlsmc08:
ldy $c0ec ; read data until valid, should be AA
bpl find_AA
cpy #$aa ; we need Y=#$AA later
bne find_D5
find_AD:
mlsmc09:lda $c0ec ; read data until high bit set (valid)
bpl find_AD
eor #$ad ; should match $ad
bne * ; lock if didn't find $ad (failure)
L12:
mlsmc0A:ldx $c0ec ; read data until high bit set (valid)
bpl L12
eor nibtbl-$80, x
sta bit2tbl-$aa, y
iny
bne L12
L13:
mlsmc0B:ldx $c0ec ; read data until high bit set (valid)
bpl L13
eor nibtbl-$80, x
sta (adrlo), y ; the real address
iny
cpy secsize
bne L13
ldy #0
L14:
ldx #$a9
L15:
inx
beq L14
lda (adrlo), y
lsr bit2tbl-$aa, x
rol
lsr bit2tbl-$aa, x
rol
sta (adrlo), y
iny
cpy secsize
bne L15
rts
; no tricks here, just the regular stuff
;=================
; readadr -- read address field
;=================
; Find address field, put track in cutrk, sector in tmpsec
readadr:
mlsmc0C:lda $c0ec ; read data until we find a $D5
bpl readadr
adr_d5:
cmp #$d5
bne readadr
adr_aa:
mlsmc0D:lda $c0ec ; read data until we find a $AA
bpl adr_aa
cmp #$aa
bne adr_d5
adr_96:
mlsmc0E:lda $c0ec ; read data until we find a $96
bpl adr_96
cmp #$96
bne adr_d5
ldy #3 ; three?
; first read volume/volume
; then track/track
; then sector/sector?
adr_read_two_bytes:
sta curtrk ; store out current track
tax
L20:
mlsmc0F:lda $c0ec ; read until full value
bpl L20
rol
sta tmpsec
L21:
mlsmc10:lda $c0ec ; read until full value
bpl L21 ; sector value is (v1<<1)&v2????
and tmpsec
dey ; loop 3 times
bne adr_read_two_bytes
seekret:
rts ; return
;================
; SEEK
;================
; current track in curtrk
; desired track in phase
seek:
asl curtrk ; multiply by 2
asl phase ; multiply by 2
lda #0
sta step
copy_cur:
lda curtrk ; load current track
sta tmptrk ; store as temptrk
sec ; calc current-desired
sbc phase
beq seekret ; if they match, we are done!
bcs seek_neg ; if negative, skip ahead
eor #$ff ; ones-complement the distance
inc curtrk ; increment current (make it 2s comp?)
bcc L114 ; skip ahead
seek_neg:
adc #$fe
dec curtrk
L114:
cmp step
bcc L115
lda step
L115:
cmp #8
bcs L116
tay
sec
L116:
lda curtrk
ldx step1, y
bne L118
L117:
clc
lda tmptrk
ldx step2, y
L118:
stx tmpsec
and #3
rol
tax
mlsmc11:sta $c0e0, x
L119:
ldx #$13
L120:
dex
bne L120
dec tmpsec
bne L119
lsr
bcs L117
inc step
bne copy_cur
.endif
step1: .byte $01, $30, $28, $24, $20, $1e, $1d, $1c
step2: .byte $70, $2c, $26, $22, $1f, $1e, $1d, $1c
@ -799,7 +532,7 @@ sectbl: .byte $00,$0d,$0b,$09,$07,$05,$03,$01,$0e,$0c,$0a,$08,$06,$04,$02,$0f
; From $BA96 of DOS33
;nibtbl: .res 128 ; = *
nibtbl: .res 128 ; = *
; .byte $00,$01,$98,$99,$02,$03,$9C,$04 ; $BA96 ; 00
; .byte $05,$06,$A0,$A1,$A2,$A4,$A4,$A5 ; $BA9E ; 08
; .byte $07,$08,$A8,$A9,$AA,$09,$0A,$0B ; $BAA6 ; 10
@ -818,8 +551,7 @@ sectbl: .byte $00,$0d,$0b,$09,$07,$05,$03,$01,$0e,$0c,$0a,$08,$06,$04,$02,$0f
; .byte $00,$00,$00,$00,$00,$00,$00,$00
;bit2tbl: .res 86 ; = nibtbl+128
;filbuf: .res 4 ; = bit2tbl+86
bit2tbl: .res 86 ; = nibtbl+128
filbuf: .res 4 ; = bit2tbl+86
;dataend = filbuf+4

42
graphics/gr_viewer/zp.inc Normal file
View File

@ -0,0 +1,42 @@
;; ZX0 addresses
ZX0_src = $00
ZX0_dst = $02
offset = $04
bitr = $06
pntr = $07
WHICH_LOAD = $09
CURRENT_DISK = $0A
adrlo = $26 ; constant from boot prom
adrhi = $27 ; constant from boot prom
tmpsec = $3c ; constant from boot prom
reqsec = $3d ; constant from boot prom
sizelo = $44
sizehi = $45
secsize = $46
INL = $EE
INH = $EF
ldsizel = $f0
ldsizeh = $f1
namlo = $fb
namhi = $fc
step = $fd ; state for stepper motor
tmptrk = $fe ; temporary copy of current track
phase = $ff ; current phase for /seek
TEMPY = $fa
OUTL = $fe ; for picking filename
OUTH = $ff

View File

@ -0,0 +1,159 @@
; De-compressor for ZX02 files
; ----------------------------
;
; Decompress ZX02 data (6502 optimized format), optimized for speed and size
; 138 bytes code, 58.0 cycles/byte in test file.
;
; Compress with:
; zx02 input.bin output.zx0
;
; (c) 2022 DMSC
; Code under MIT license, see LICENSE file.
;ZP=$80
;offset = ZP+0
;ZX0_src = ZP+2
;ZX0_dst = ZP+4
;bitr = ZP+6
;pntr = ZP+7
; Initial values for offset, source, destination and bitr
;zx0_ini_block:
; .byte $00, $00, <comp_data, >comp_data, <out_addr, >out_addr, $80
;--------------------------------------------------
; Decompress ZX0 data (6502 optimized format)
zx02_full_decomp:
; ; Get initialization block
; ldy #7
;
;copy_init: lda zx0_ini_block-1, y
; sta offset-1, y
; dey
; bne copy_init
sta ZX0_dst+1 ; page to output to in A
zx_src_l:
ldy #$dd
sty ZX0_src
zx_src_h:
ldy #$dd
sty ZX0_src+1
ldy #$80
sty bitr
ldy #0
sty offset
sty offset+1
sty ZX0_dst ; always on even page
; Decode literal: Ccopy next N bytes from compressed file
; Elias(length) byte[1] byte[2] ... byte[N]
decode_literal:
jsr get_elias
cop0: lda (ZX0_src), y
inc ZX0_src
bne plus1
inc ZX0_src+1
plus1: sta (ZX0_dst),y
inc ZX0_dst
bne plus2
inc ZX0_dst+1
plus2: dex
bne cop0
asl bitr
bcs dzx0s_new_offset
; Copy from last offset (repeat N bytes from last offset)
; Elias(length)
jsr get_elias
dzx0s_copy:
lda ZX0_dst
sbc offset ; C=0 from get_elias
sta pntr
lda ZX0_dst+1
sbc offset+1
sta pntr+1
cop1:
lda (pntr), y
inc pntr
bne plus3
inc pntr+1
plus3: sta (ZX0_dst),y
inc ZX0_dst
bne plus4
inc ZX0_dst+1
plus4: dex
bne cop1
asl bitr
bcc decode_literal
; Copy from new offset (repeat N bytes from new offset)
; Elias(MSB(offset)) LSB(offset) Elias(length-1)
dzx0s_new_offset:
; Read elias code for high part of offset
jsr get_elias
beq exit ; Read a 0, signals the end
; Decrease and divide by 2
dex
txa
lsr ; @
sta offset+1
; Get low part of offset, a literal 7 bits
lda (ZX0_src), y
inc ZX0_src
bne plus5
inc ZX0_src+1
plus5:
; Divide by 2
ror ; @
sta offset
; And get the copy length.
; Start elias reading with the bit already in carry:
ldx #1
jsr elias_skip1
inx
bcc dzx0s_copy
; Read an elias-gamma interlaced code.
; ------------------------------------
get_elias:
; Initialize return value to #1
ldx #1
bne elias_start
elias_get: ; Read next data bit to result
asl bitr
rol ; @
tax
elias_start:
; Get one bit
asl bitr
bne elias_skip1
; Read new bit from stream
lda (ZX0_src), y
inc ZX0_src
bne plus6
inc ZX0_src+1
plus6: ;sec ; not needed, C=1 guaranteed from last bit
rol ;@
sta bitr
elias_skip1:
txa
bcs elias_get
; Got ending bit, stop reading
exit:
rts