├── apc.exe ├── apc32.exe ├── demo.tap ├── dzx7b.exe ├── manic.scr ├── tap8k.exe ├── zx7b.exe ├── example.tap ├── manic.zx7b ├── saukav.exe ├── GenTape ├── GenTape.exe └── GenTape.c ├── make.bat ├── benchmark_files ├── 128.ROM ├── lena.tif ├── 128_1k.ROM ├── lena1k.tif ├── 128_16k.ROM ├── lena16k.tif ├── alice1k.txt ├── alice16k.txt └── alice.txt ├── shr8k.bat ├── demo.asm ├── example.asm ├── bacteria_dzx7b.asm ├── tap8k.c ├── dzx7b_slow.asm ├── dzx7b_medium.asm ├── readme.txt ├── saukav.txt ├── dzx7b_fast.asm ├── shrinkler_onecall.asm ├── shr8k.asm ├── shrinkler.asm ├── dzx7b.c ├── zx7b.c └── saukav.c /apc.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/apc.exe -------------------------------------------------------------------------------- /apc32.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/apc32.exe -------------------------------------------------------------------------------- /demo.tap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/demo.tap -------------------------------------------------------------------------------- /dzx7b.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/dzx7b.exe -------------------------------------------------------------------------------- /manic.scr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/manic.scr -------------------------------------------------------------------------------- /tap8k.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/tap8k.exe -------------------------------------------------------------------------------- /zx7b.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/zx7b.exe -------------------------------------------------------------------------------- /example.tap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/example.tap -------------------------------------------------------------------------------- /manic.zx7b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/manic.zx7b -------------------------------------------------------------------------------- /saukav.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/saukav.exe -------------------------------------------------------------------------------- /GenTape/GenTape.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/GenTape/GenTape.exe -------------------------------------------------------------------------------- /make.bat: -------------------------------------------------------------------------------- 1 | zx7b manic.scr manic.zx7b 2 | sjasmplus example.asm 3 | tap8k example.bin MANIC 4 | -------------------------------------------------------------------------------- /benchmark_files/128.ROM: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/benchmark_files/128.ROM -------------------------------------------------------------------------------- /benchmark_files/lena.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/benchmark_files/lena.tif -------------------------------------------------------------------------------- /shr8k.bat: -------------------------------------------------------------------------------- 1 | shrinkler -d -p %1 data.shr 2 | sjasmplus shr8k.asm 3 | GenTape %2 basic '%3' 0 shr8k.bin 4 | -------------------------------------------------------------------------------- /benchmark_files/128_1k.ROM: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/benchmark_files/128_1k.ROM -------------------------------------------------------------------------------- /benchmark_files/lena1k.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/benchmark_files/lena1k.tif -------------------------------------------------------------------------------- /benchmark_files/128_16k.ROM: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/benchmark_files/128_16k.ROM -------------------------------------------------------------------------------- /benchmark_files/lena16k.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniovillena/zx7b/HEAD/benchmark_files/lena16k.tif -------------------------------------------------------------------------------- /demo.asm: -------------------------------------------------------------------------------- 1 | output demo.bin 2 | org $8000 3 | ld hl, data 4 | ld de, $4000 5 | ld bc, $1b00 6 | ldir 7 | halt 8 | data incbin manic.scr 9 | -------------------------------------------------------------------------------- /example.asm: -------------------------------------------------------------------------------- 1 | output example.bin 2 | org $8000 3 | ld hl, binario-1 4 | ld de, $5aff 5 | call dzx7 6 | di 7 | halt 8 | include dzx7b_slow.asm 9 | incbin manic.zx7b 10 | binario: -------------------------------------------------------------------------------- /benchmark_files/alice1k.txt: -------------------------------------------------------------------------------- 1 | ALICE'S ADVENTURES IN WONDERLAND Lewis Carroll THE MILLENNIUM FULCRUM EDITION 2.9 CHAPTER I Down the Rabbit-Hole Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, `and what is the use of a book,' thought Alice `without pictures or conversation?' So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her. There was nothing so VERY remarkable in that; nor did Alice think it so VERY much out of the way to hear the Rabbit say to itself, `Oh dear! Oh dear! I shall be late!' (when she thought i -------------------------------------------------------------------------------- /bacteria_dzx7b.asm: -------------------------------------------------------------------------------- 1 | %include "define.asm" 2 | org $100 3 | start push si 4 | mov ch, (final-start)>>8 5 | mov di, start-final-$100 6 | rep movsb 7 | std 8 | mov si, $0feff 9 | mov di, rawsize+$0ff 10 | mov bp, getbit+start-final-$200 11 | mov al, $80 12 | jmp copyby+start-final-$200 13 | belsi mov bl, [si] 14 | dec si 15 | rcl bl, 1 16 | jnc offend 17 | mov bh, $10 18 | nexbit call bp 19 | rcl bh, 1 20 | jnc nexbit 21 | inc bh 22 | shr bh, 1 23 | offend rcr bl, 1 24 | push si 25 | lea si, [di+bx+1] 26 | rep movsb 27 | pop si 28 | db $3c 29 | copyby movsb 30 | mainlo call bp 31 | jnc copyby 32 | mov bh, cl 33 | db $3d 34 | lenval call bp 35 | rcl cx, 1 36 | call bp 37 | jnc lenval 38 | inc cl 39 | jnz belsi 40 | getbit add al, al 41 | jnz toret 42 | lodsb 43 | adc al, al 44 | toret ret 45 | incbin "bacteria.com.zx7b" 46 | final: 47 | -------------------------------------------------------------------------------- /tap8k.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(int argc, char* argv[]){ 4 | unsigned char mem[0x10000], checksum= 0; 5 | FILE *fi, *fo; 6 | char *output_name, *ext; 7 | int i, size; 8 | if( argc==1 ) 9 | printf("\n" 10 | "tap8k v0.99. Generates a ZX Spectrum TAP file from ORG $8000 binary 10 Sep 2017\n\n" 11 | " tap8k \n\n" 12 | " Origin binary file\n" 13 | " Up 10 chars name to show during loading\n" 14 | "An output file with .TAP added extension will be generated\n"), 15 | exit(0); 16 | if( argc!=3 ) 17 | printf("\nInvalid number of parameters\n"), 18 | exit(-1); 19 | if( !(ext= strchr(argv[1], '.')) ) 20 | printf("\nInvalid argument name: %s\n", argv[1]), 21 | exit(-1); 22 | fi= fopen(argv[1], "rb"); 23 | if( !fi ) 24 | printf("\nInput file not found: %s\n", argv[1]), 25 | exit(-1); 26 | output_name= (char *)malloc(strlen(argv[1])+1); 27 | ext[0]= 0; 28 | strcpy(output_name, argv[1]); 29 | strcat(output_name, ".TAP"); 30 | fo= fopen(output_name, "wb+"); 31 | if( !fo ) 32 | printf("\nCannot create output file: %s\n", output_name), 33 | exit(-1); 34 | size= fread(mem+23, 1, 0x10000, fi); 35 | *(int*)(mem)= 19; 36 | for ( i= 0; i<10 && argv[2][i]; i++ ) 37 | mem[i+4]= argv[2][i]; 38 | while( ++i<11 ) 39 | mem[i+3]= ' '; 40 | *(short*)(mem+18)= *(short*)(mem+14)= *(short*)(mem+21)= size+20; 41 | size+= 24; 42 | for ( checksum= 0, i= 2; i<20; ++i ) 43 | checksum^= mem[i]; 44 | mem[20]= checksum; 45 | fwrite(mem, 1, 21, fo); 46 | *(short*)(mem)= size-2; 47 | mem[2]= 255; 48 | *(int*)(mem+3)= 0x8e117d06; 49 | *(int*)(mem+7)= 0x0e37c0fd; 50 | *(int*)(mem+11)= 0x2196398f; 51 | *(int*)(mem+15)= 0xb8edda6d; 52 | *(int*)(mem+19)= 0xe9f9eb13; 53 | for ( checksum= 0, i= 2; i1 27 | ldd ; copy literal byte 28 | add a, a ; read next bit, faster method 29 | jr z, mailab ; call routine only if need to load next byte 30 | jr c, maicoo ; next bit indicates either literal or sequence 31 | ldd ; loop unrolling x4 32 | add a, a 33 | jr c, maicoe 34 | endif 35 | copbye: ldd ; copy literal byte 36 | add a, a ; read next bit, faster method 37 | jr z, mailab ; call routine only if need to load next byte 38 | jr c, maicoo ; next bit indicates either literal or sequence 39 | copbyo: ldd ; loop unrolling x2 40 | add a, a 41 | jr nc, copbye ; next bit indicates either literal or sequence 42 | 43 | ; determine number of bits used for length (Elias gamma coding) 44 | maicoe: ld bc, 2 ; BC=$0002 45 | maisie: push de ; store destination on stack 46 | ld d, b ; D= 0 47 | getbitm ; getbit with macro getbitm 48 | if speed=0 49 | jr c, contie 50 | else 51 | jp c, contie ; jp is 2 cycles faster when jump not taken 52 | endif 53 | dec c 54 | levale: add a, a 55 | rl c 56 | rl b ; insert bit on BC 57 | getbitm ; more bits? 58 | jr nc, levale ; repeat, final length on BC 59 | 60 | ; check escape sequence 61 | inc c ; detect escape sequence 62 | jr z, exitdz ; end of algorithm 63 | 64 | ; determine offset 65 | contie: ld e, (hl) ; load offset flag (1 bit) + offset value (7 bits) 66 | dec hl 67 | sll e 68 | jr nc, offnde ; if offset flag is set, load 4 extra bits 69 | add a, a ; load 4 bits by code 70 | rl d ; odd bits don't need end of byte checking 71 | getbitm 72 | rl d 73 | add a, a 74 | rl d 75 | getbitm 76 | ccf 77 | jr c, offnde 78 | inc d ; equivalent to adding 128 to DE 79 | offnde: rr e ; insert inverted fourth bit into E 80 | ex (sp), hl ; store source, restore destination 81 | ex de, hl ; destination from HL to DE 82 | adc hl, de ; HL = destination + offset + 1 83 | lddr 84 | pop hl ; restore source address (compressed data) 85 | add a, a 86 | if speed=0 87 | jr c, maicoe 88 | jr copbye 89 | else 90 | if speed=1 91 | jr c, maicoe 92 | jp copbye 93 | else 94 | jr nc, copbye 95 | ld c, 2 96 | jp maisie 97 | endif 98 | endif 99 | 100 | exitdz: pop hl ; exit path 101 | if speed=0 102 | getbit: ld a, (hl) ; load another group of 8 bits 103 | dec hl 104 | adc a, a 105 | endif 106 | ret 107 | 108 | mailab: ld a, (hl) ; save some cycles avoiding call getbit 109 | dec hl 110 | adc a, a 111 | jr nc, copbyo 112 | 113 | maicoo: ld bc, 2 ; repeats code above (maicoe), for odd bits 114 | push de 115 | ld d, b 116 | add a, a 117 | if speed=0 118 | jr c, contio 119 | else 120 | jp c, contio 121 | endif 122 | dec c 123 | levalo: getbitm 124 | rl c 125 | rl b 126 | add a, a 127 | jr nc, levalo 128 | inc c 129 | jr z, exitdz 130 | contio: ld e, (hl) 131 | dec hl 132 | sll e 133 | jr nc, offndo 134 | getbitm 135 | rl d 136 | add a, a 137 | rl d 138 | getbitm 139 | rl d 140 | add a, a 141 | ccf 142 | jr c, offndo 143 | inc d 144 | offndo: rr e 145 | ex (sp), hl 146 | ex de, hl 147 | adc hl, de 148 | lddr 149 | pop hl 150 | add a, a 151 | jr z, mailab 152 | jr c, maicoo 153 | jp copbyo 154 | -------------------------------------------------------------------------------- /shrinkler_onecall.asm: -------------------------------------------------------------------------------- 1 | ; Copyright 1999-2015 Aske Simon Christensen. 2 | ; Decompress Shrinkler-compressed data produced with the -d option. 3 | ; Uses 3 kilobytes of buffer anywhere in the memory 4 | ; Decompression code may read one longword beyond compressed data. 5 | ; The contents of this longword does not matter. 6 | ; 7 | ; Z80 demo version (one call) - 245 bytes 8 | ; Conversion by roudoudou 9 | ; size optimisations by Hicks, Antonio Villena & Urusergi 10 | ; 11 | ; usage 12 | ;------- 13 | ; LD IX,source 14 | ; LD DE,destination 15 | ; CALL shrinkler_decrunch 16 | 17 | define shrinkler_pr shrinkler_dr 18 | 19 | shrinkler_decrunch: 20 | ; Init range decoder state 21 | ld bc, shrinkler_pr 22 | xor a 23 | shrinkler_repeat: 24 | ld (bc), a 25 | xor $80 26 | inc bc 27 | ld h, ($f400-shrinkler_pr)>>8 28 | add hl, bc 29 | jr nc, shrinkler_repeat 30 | 31 | shrinkler_lit: 32 | ; Literal 33 | ld hl, shrinkler_d6+1 34 | inc (hl) 35 | shrinkler_getlit: 36 | call shrinkler_getbit 37 | rl (hl) 38 | jr nc, shrinkler_getlit 39 | ldi 40 | ; After literal 41 | call shrinkler_getkind 42 | jr nc, shrinkler_lit 43 | ; Reference 44 | call shrinkler_altgetbit 45 | jr nc, shrinkler_readoffset 46 | shrinkler_readlength: 47 | inc a 48 | call shrinkler_getnumber 49 | shrinkler_d5: 50 | ld hl, 0 51 | add hl, de 52 | ldir 53 | ; After reference 54 | call shrinkler_getkind 55 | jr nc, shrinkler_lit 56 | shrinkler_readoffset: 57 | call shrinkler_getnumber 58 | ld hl, 2 59 | sbc hl, bc 60 | ld (shrinkler_d5+1), hl 61 | jr nz, shrinkler_readlength 62 | 63 | shrinkler_getnumber: 64 | ; Out: Number in BC 65 | ld bc, 1 66 | ld hl, shrinkler_d6+2 67 | ld (hl), a 68 | dec hl 69 | ld (hl), c 70 | shrinkler_numberloop: 71 | inc (hl) 72 | call shrinkler_getbit 73 | inc (hl) 74 | jr c, shrinkler_numberloop 75 | shrinkler_bitsloop: 76 | dec (hl) 77 | dec (hl) 78 | ret m 79 | call shrinkler_getbit 80 | rl c 81 | rl b 82 | jr shrinkler_bitsloop 83 | 84 | ; Out: Bit in C 85 | shrinkler_readbit: 86 | ld (shrinkler_d3+1), hl 87 | shrinkler_d4l: 88 | ld hl, 0 89 | shrinkler_d4h: 90 | ld de, #8000 91 | shrinkler_rb0: 92 | adc hl, hl 93 | ld (shrinkler_d4l+1), hl 94 | ex de, hl 95 | jr nz, shrinkler_rb2-1 96 | adc hl, hl 97 | jr nz, shrinkler_rb3 98 | shrinkler_rb1: 99 | ld h, (ix) 100 | ld l, (ix+1) 101 | inc ix 102 | inc ix 103 | ccf 104 | jr c, shrinkler_rb0 105 | ex de, hl 106 | jr shrinkler_rb1-3 107 | shrinkler_rb2: 108 | ld l, d ; adc hl, hl with ed prefix 109 | shrinkler_rb3: 110 | ld (shrinkler_d4h+1), hl 111 | shrinkler_d2: 112 | ld hl, 0 113 | adc hl, hl 114 | ld (shrinkler_d2+1), hl 115 | jr shrinkler_getbit1 116 | 117 | shrinkler_getkind: 118 | ;Use parity as context 119 | ld l, 1 120 | shrinkler_altgetbit: 121 | ld a, l 122 | and e 123 | ld h, a 124 | dec hl 125 | ld (shrinkler_d6+1), hl 126 | 127 | shrinkler_getbit: 128 | exx 129 | shrinkler_getbit1: 130 | ld hl, (shrinkler_d3+1) 131 | add hl, hl 132 | jr nc, shrinkler_readbit 133 | shrinkler_d6: 134 | ld hl, 0 135 | add hl, hl 136 | ld de, shrinkler_pr+2 ; cause -1 context 137 | add hl, de 138 | push hl 139 | ld e, (hl) 140 | inc hl 141 | ld d, (hl) 142 | ; D1 = One prob 143 | ld b, d 144 | ld c, e ; bc=de=d1 / hl=a1 145 | ld a, $eb 146 | shrinkler_shift4: 147 | srl b 148 | rr c 149 | add a, a 150 | jr c, shrinkler_shift4-1 151 | sbc hl, bc ; hl=d1-d1/16 152 | ex de, hl 153 | ld b, (hl) 154 | ld (hl), d 155 | dec hl 156 | ld c, (hl) 157 | ld (hl), e 158 | shrinkler_d3: 159 | ld de, 1 160 | sbc hl, hl 161 | shrinkler_muluw: 162 | add hl, hl 163 | rl e 164 | rl d 165 | jr nc, shrinkler_cont 166 | add hl, bc 167 | jr nc, shrinkler_cont 168 | inc de 169 | shrinkler_cont: 170 | sub $b 171 | jr nz, shrinkler_muluw 172 | pop bc 173 | ld hl, (shrinkler_d2+1) 174 | sbc hl, de 175 | jr nc, shrinkler_zero 176 | 177 | shrinkler_one: 178 | ; onebrob = 1 - (1 - oneprob) * (1 - adjust) = oneprob - oneprob * adjust + adjust 179 | ld a, (bc) 180 | sub 1 181 | ld (bc), a 182 | inc bc 183 | ld a, (bc) 184 | sbc a, $f0 ; (a1)+#FFF 185 | ld (bc), a 186 | ex de, hl 187 | jr shrinkler_d3ret 188 | 189 | shrinkler_zero: 190 | ; oneprob = oneprob * (1 - adjust) = oneprob - oneprob * adjust 191 | ld (shrinkler_d2+1), hl 192 | ld hl, (shrinkler_d3+1) 193 | sbc hl, de 194 | 195 | shrinkler_d3ret: 196 | ld (shrinkler_d3+1), hl 197 | exx 198 | ld a, 3 199 | ret 200 | -------------------------------------------------------------------------------- /shr8k.asm: -------------------------------------------------------------------------------- 1 | ; Copyright 1999-2015 Aske Simon Christensen. 2 | ; Decompress Shrinkler-compressed data produced with the -d option. 3 | ; Uses 3 kilobytes of buffer anywhere in the memory 4 | ; Decompression code may read one longword beyond compressed data. 5 | ; The contents of this longword does not matter. 6 | ; 7 | ; Z80 version of 255 bytes 8 | ; roudoudou, Hicks, Antonio Villena & Urusergi 9 | ; 10 | ; usage 11 | ;------- 12 | ; sjasmplus demo.asm 13 | ; shr8k demo.bin demo.tap DEMO 14 | ; 15 | ; Original demo.asm must be org $8000. You can assume SP=$8000 16 | ; shrinkler http://www.pouet.net/prod.php?which=64851 17 | ; sjasmplus https://github.com/DSkywalk/fase/blob/master/engine/src/sjasmplus/sjasmplus.exe 18 | ; GenTape https://github.com/antoniovillena/zx7b/blob/master/GenTape/GenTape.exe 19 | 20 | output shr8k.bin 21 | org $5ccb 22 | define shrinkler_pr $7100 23 | 24 | l5ccb: ld de, $8000 25 | di 26 | defb $de, $c0, $37, $0e, $8f, $39, $96 ; paolo ferraris method, in basic jump to $5ccb 27 | shrinkler_repeat: 28 | push de 29 | ld h, c 30 | add hl, sp 31 | jr c, shrinkler_repeat 32 | ld sp, $7ffe 33 | ld ix, data 34 | 35 | shrinkler_lit: 36 | ; Literal 37 | ld hl, shrinkler_d6+1 38 | inc (hl) 39 | shrinkler_getlit: 40 | call shrinkler_getbit 41 | rl (hl) 42 | jr nc, shrinkler_getlit 43 | ldi 44 | ; After literal 45 | call shrinkler_getkind 46 | jr nc, shrinkler_lit 47 | ; Reference 48 | call shrinkler_altgetbit 49 | jr nc, shrinkler_readoffset 50 | shrinkler_readlength: 51 | inc a 52 | call shrinkler_getnumber 53 | shrinkler_d5: 54 | ld hl, 0 55 | add hl, de 56 | ldir 57 | ; After reference 58 | call shrinkler_getkind 59 | jr nc, shrinkler_lit 60 | shrinkler_readoffset: 61 | call shrinkler_getnumber 62 | ld hl, 2 63 | sbc hl, bc 64 | ld (shrinkler_d5+1), hl 65 | jr nz, shrinkler_readlength 66 | 67 | shrinkler_getnumber: 68 | ; Out: Number in BC 69 | ld bc, 1 70 | ld hl, shrinkler_d6+2 71 | ld (hl), a 72 | dec hl 73 | ld (hl), c 74 | shrinkler_numberloop: 75 | inc (hl) 76 | call shrinkler_getbit 77 | inc (hl) 78 | jr c, shrinkler_numberloop 79 | shrinkler_bitsloop: 80 | dec (hl) 81 | dec (hl) 82 | ret m 83 | call shrinkler_getbit 84 | rl c 85 | rl b 86 | jr shrinkler_bitsloop 87 | 88 | ;-------------------------------------------------- 89 | ; Out: Bit in C 90 | shrinkler_readbit: 91 | ld (shrinkler_d3+1), hl 92 | shrinkler_d4l: 93 | ld hl, 0 94 | shrinkler_d4h: 95 | ld de, #8000 96 | shrinkler_rb0: 97 | adc hl, hl 98 | ld (shrinkler_d4l+1), hl 99 | ex de, hl 100 | jr nz, shrinkler_rb2-1 101 | adc hl, hl 102 | jr nz, shrinkler_rb3 103 | shrinkler_rb1: 104 | ld h, (ix) 105 | ld l, (ix+1) 106 | inc ix 107 | inc ix 108 | ccf 109 | jr c, shrinkler_rb0 110 | ex de, hl 111 | jr shrinkler_rb1-3 112 | shrinkler_rb2: 113 | ld l, d ; adc hl, hl with ed prefix 114 | shrinkler_rb3: 115 | ld (shrinkler_d4h+1), hl 116 | shrinkler_d2: 117 | ld hl, 0 118 | adc hl, hl 119 | ld (shrinkler_d2+1), hl 120 | jr shrinkler_getbit1 121 | 122 | ;-------------------------------------------------- 123 | shrinkler_getkind: 124 | ;Use parity as context 125 | ld l, 1 126 | shrinkler_altgetbit: 127 | ld a, l 128 | and e 129 | ld h, a 130 | dec hl 131 | ld (shrinkler_d6+1), hl 132 | 133 | shrinkler_getbit: 134 | exx 135 | shrinkler_getbit1: 136 | ld hl, (shrinkler_d3+1) 137 | add hl, hl 138 | jr nc, shrinkler_readbit 139 | shrinkler_d6: 140 | ld hl, 0 141 | add hl, hl 142 | ld de, shrinkler_pr+2 ; cause -1 context 143 | add hl, de 144 | push hl 145 | ld e, (hl) 146 | inc hl 147 | ld d, (hl) 148 | ; D1 = One prob 149 | ld b, d 150 | ld c, e ; bc=de=d1 / hl=a1 151 | ld a, $eb 152 | shrinkler_shift4: 153 | srl b 154 | rr c 155 | add a, a 156 | jr c, shrinkler_shift4-1 157 | sbc hl, bc ; hl=d1-d1/16 158 | ex de, hl 159 | ld b, (hl) 160 | ld (hl), d 161 | dec hl 162 | ld c, (hl) 163 | ld (hl), e 164 | shrinkler_d3: 165 | ld de, 1 166 | sbc hl, hl 167 | shrinkler_muluw: 168 | add hl, hl 169 | rl e 170 | rl d 171 | jr nc, shrinkler_cont 172 | add hl, bc 173 | jr nc, shrinkler_cont 174 | inc de 175 | shrinkler_cont: 176 | sub $b 177 | jr nz, shrinkler_muluw 178 | pop bc 179 | ld hl, (shrinkler_d2+1) 180 | sbc hl, de 181 | jr nc, shrinkler_zero 182 | 183 | shrinkler_one: 184 | ; onebrob = 1 - (1 - oneprob) * (1 - adjust) = oneprob - oneprob * adjust + adjust 185 | ld a, (bc) 186 | sub 1 187 | ld (bc), a 188 | inc bc 189 | ld a, (bc) 190 | sbc a, $f0 ; (a1)+#FFF 191 | ld (bc), a 192 | ex de, hl 193 | jr shrinkler_d3ret 194 | 195 | shrinkler_zero: 196 | ; oneprob = oneprob * (1 - adjust) = oneprob - oneprob * adjust 197 | ld (shrinkler_d2+1), hl 198 | ld hl, (shrinkler_d3+1) 199 | sbc hl, de 200 | 201 | shrinkler_d3ret: 202 | ld (shrinkler_d3+1), hl 203 | exx 204 | ld a, 3 205 | ret 206 | data: incbin data.shr 207 | -------------------------------------------------------------------------------- /shrinkler.asm: -------------------------------------------------------------------------------- 1 | ; Copyright 1999-2015 Aske Simon Christensen. 2 | ; Decompress Shrinkler-compressed data produced with the -d option. 3 | ; Uses 3 kilobytes of buffer anywhere in the memory 4 | ; Decompression code may read one longword beyond compressed data. 5 | ; The contents of this longword does not matter. 6 | ; 7 | ; official Z80 release (recall) - 254 bytes 8 | ; Conversion by roudoudou 9 | ; size optimisations by Hicks, Antonio Villena & Urusergi 10 | ; 11 | ; usage 12 | ;------- 13 | ; LD IX,source 14 | ; LD DE,destination 15 | ; CALL shrinkler_decrunch 16 | 17 | define shrinkler_d5 shrinkler_dr 18 | define shrinkler_d3 shrinkler_d5+2 ; defw 0 19 | define shrinkler_d2 shrinkler_d3+2 ; defw 0 20 | define shrinkler_d6 shrinkler_d2+2 ; defw 0 21 | define shrinkler_d4 shrinkler_d6+2 ; defw 0,0 22 | define shrinkler_pr shrinkler_d4+4 ; 3072 bytes buffer!!! 23 | 24 | shrinkler_decrunch: 25 | ; Init range decoder state 26 | ld hl, shrinkler_dr+2 27 | xor a 28 | ex af, af' 29 | xor a 30 | ld bc, $070d 31 | ld (hl), 1 32 | shrinkler_repeat: 33 | inc hl 34 | ex af, af' 35 | ld (hl), a 36 | djnz shrinkler_repeat 37 | ld a, $80 38 | dec c 39 | jr nz, shrinkler_repeat 40 | 41 | shrinkler_lit: 42 | ld hl, shrinkler_d6 43 | inc (hl) 44 | shrinkler_getlit: 45 | call shrinkler_getbit 46 | rl (hl) 47 | jr nc, shrinkler_getlit 48 | ldi 49 | ; After literal 50 | call shrinkler_getkind 51 | jr nc, shrinkler_lit 52 | ; Reference 53 | call shrinkler_altgetbit 54 | jr nc, shrinkler_readoffset 55 | shrinkler_readlength: 56 | call shrinkler_getnumber 57 | ld hl, (shrinkler_d5) 58 | add hl, de 59 | ldir 60 | ; After reference 61 | call shrinkler_getkind 62 | jr nc, shrinkler_lit 63 | shrinkler_readoffset: 64 | dec a 65 | call shrinkler_getnumber 66 | ld hl, 2 67 | sbc hl, bc 68 | ld (shrinkler_d5), hl 69 | jr nz, shrinkler_readlength 70 | 71 | shrinkler_getnumber: 72 | ; Out: Number in BC 73 | ld bc, 1 74 | ld hl, shrinkler_d6+1 75 | ld (hl), a 76 | dec hl 77 | ld (hl), c 78 | shrinkler_numberloop: 79 | inc (hl) 80 | call shrinkler_getbit 81 | inc (hl) 82 | jr c, shrinkler_numberloop 83 | shrinkler_bitsloop: 84 | dec (hl) 85 | dec (hl) 86 | ret m 87 | call shrinkler_getbit 88 | rl c 89 | rl b 90 | jr shrinkler_bitsloop 91 | 92 | shrinkler_readbit: 93 | pop de 94 | ld (shrinkler_d3), hl 95 | ld hl, (shrinkler_d4) 96 | ld de, (shrinkler_d4+2) 97 | shrinkler_rb0: 98 | adc hl, hl 99 | ld (shrinkler_d4), hl 100 | ex de, hl 101 | jr nz, shrinkler_rb2-1 102 | 103 | shrinkler_rb4: 104 | adc hl, hl 105 | jr nz, shrinkler_rb3 106 | ; HL=DE=0 107 | shrinkler_rb1: 108 | ccf 109 | ld h, (ix) ; DEHL=(a4) big endian value read! 110 | ld l, (ix+1) 111 | inc ix 112 | inc ix 113 | jr c, shrinkler_rb0 114 | ex de, hl 115 | jr shrinkler_rb1-3 116 | 117 | shrinkler_rb2: 118 | ld l, d ; adc hl, hl with $ed 119 | shrinkler_rb3: 120 | ld (shrinkler_d4+2), hl ; written in little endian 121 | ld hl, (shrinkler_d2) 122 | adc hl, hl 123 | ld (shrinkler_d2), hl 124 | jr shrinkler_getbit1 125 | 126 | shrinkler_getkind: 127 | ;Use parity as context 128 | ld l, 1 129 | shrinkler_altgetbit: 130 | ld a, l 131 | and e 132 | ld h, a 133 | dec hl 134 | ld (shrinkler_d6), hl 135 | 136 | shrinkler_getbit: 137 | exx 138 | shrinkler_getbit1: 139 | ld hl, (shrinkler_d3) 140 | push hl 141 | add hl, hl 142 | jr nc, shrinkler_readbit 143 | ld hl, (shrinkler_d6) 144 | add hl, hl 145 | ld de, shrinkler_pr+4 ; cause -1 context 146 | add hl, de 147 | ld e, (hl) 148 | inc hl 149 | ld d, (hl) 150 | ld b, d 151 | ld c, e ; bc=de=d1 / hl=a1 152 | ld a, $eb 153 | shrinkler_shift4: 154 | srl b 155 | rr c 156 | add a, a 157 | jr c, shrinkler_shift4-1 158 | sbc hl, bc ; hl=d1-d1/16 159 | ex de, hl 160 | ld b, (hl) 161 | ld (hl), d 162 | dec hl 163 | ld c, (hl) 164 | ld (hl), e 165 | ex (sp), hl 166 | ex de, hl 167 | sbc hl, hl 168 | shrinkler_muluw: 169 | add hl, hl 170 | rl e 171 | rl d 172 | jr nc, shrinkler_cont 173 | add hl, bc 174 | jr nc, shrinkler_cont 175 | inc de 176 | shrinkler_cont: 177 | sub $b 178 | jr nz, shrinkler_muluw 179 | pop bc ; bc=d1 initial 180 | ld hl, (shrinkler_d2) 181 | sbc hl, de 182 | jr nc, shrinkler_zero 183 | 184 | shrinkler_one: 185 | ; onebrob = 1 - (1 - oneprob) * (1 - adjust) = oneprob - oneprob * adjust + adjust 186 | ld a, (bc) 187 | sub 1 188 | ld (bc), a 189 | inc bc 190 | ld a, (bc) 191 | sbc a, $f0 ; (a1)+#FFF 192 | ld (bc), a 193 | ex de, hl 194 | jr shrinkler_d3ret 195 | 196 | shrinkler_zero: 197 | ; oneprob = oneprob * (1 - adjust) = oneprob - oneprob * adjust 198 | ld (shrinkler_d2), hl 199 | ld hl, (shrinkler_d3) 200 | sbc hl, de 201 | ; oneprob*adjust < oneprob so carry is always cleared... 202 | shrinkler_d3ret: 203 | ld (shrinkler_d3), hl 204 | exx 205 | ld a, 4 206 | ret 207 | -------------------------------------------------------------------------------- /dzx7b.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (c) Copyright 2015 by Einar Saukas. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * * Redistributions of source code must retain the above copyright 7 | * notice, this list of conditions and the following disclaimer. 8 | * * Redistributions in binary form must reproduce the above copyright 9 | * notice, this list of conditions and the following disclaimer in the 10 | * documentation and/or other materials provided with the distribution. 11 | * * The name of its author may not be used to endorse or promote products 12 | * derived from this software without specific prior written permission. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 18 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | FILE *ifp; 31 | FILE *ofp; 32 | char *input_name; 33 | char *output_name; 34 | unsigned char *input_data; 35 | unsigned char *output_data; 36 | size_t input_index; 37 | size_t output_index; 38 | size_t input_size; 39 | size_t output_size; 40 | size_t partial_counter; 41 | size_t total_counter; 42 | int bit_mask; 43 | int bit_value; 44 | 45 | int read_byte() { 46 | return input_data[input_index++]; 47 | } 48 | 49 | int read_bit() { 50 | bit_mask >>= 1; 51 | if (bit_mask == 0) { 52 | bit_mask = 128; 53 | bit_value = read_byte(); 54 | } 55 | return bit_value & bit_mask ? 1 : 0; 56 | } 57 | 58 | int read_elias_gamma() { 59 | int i; 60 | int value; 61 | 62 | value = 1; 63 | while (!read_bit()) { 64 | value = value << 1 | read_bit(); 65 | } 66 | if( (value&255)==255 ) 67 | value= -1; 68 | return value; 69 | } 70 | 71 | int read_offset() { 72 | int value; 73 | int i; 74 | 75 | value = read_byte(); 76 | if (value < 128) { 77 | return value; 78 | } else { 79 | i = read_bit(); 80 | i = i << 1 | read_bit(); 81 | i = i << 1 | read_bit(); 82 | i = i << 1 | read_bit(); 83 | return (value & 127 | i << 7) + 128; 84 | } 85 | } 86 | 87 | void write_byte(int value) { 88 | output_data[output_index++] = value; 89 | } 90 | 91 | void write_bytes(int offset, int length) { 92 | if (offset > output_size+output_index) { 93 | fprintf(stderr, "Error: Invalid data in input file %s\n", input_name); 94 | exit(1); 95 | } 96 | while (length-- > 0) { 97 | write_byte(output_data[output_index-offset]); 98 | } 99 | } 100 | 101 | void decompress() { 102 | int length,i; 103 | 104 | input_index = 0; 105 | partial_counter = 0; 106 | output_index = 0; 107 | bit_mask = 0; 108 | 109 | write_byte(read_byte()); 110 | while (1) { 111 | if (!read_bit()) { 112 | write_byte(read_byte()); 113 | } else { 114 | length = read_elias_gamma()+1; 115 | if (length == 0) { 116 | return; 117 | } 118 | write_bytes(read_offset()+1, length); 119 | } 120 | } 121 | } 122 | 123 | int main(int argc, char *argv[]) { 124 | int forced_mode = 0; 125 | int i; 126 | 127 | printf("DZX7B: LZ77/LZSS decompression by Einar Saukas\n"); 128 | 129 | /* process hidden optional parameters */ 130 | for (i = 1; i < argc && *argv[i] == '-'; i++) { 131 | if (!strcmp(argv[i], "-f")) { 132 | forced_mode = 1; 133 | } else { 134 | fprintf(stderr, "Error: Invalid parameter %s\n", argv[i]); 135 | exit(1); 136 | } 137 | } 138 | 139 | /* determine output filename */ 140 | if (argc == i+1) { 141 | input_name = argv[i]; 142 | input_size = strlen(input_name); 143 | if (input_size > 4 && !strcmp(input_name+input_size-4, ".zx7")) { 144 | output_name = (char *)malloc(input_size); 145 | strcpy(output_name, input_name); 146 | output_name[input_size-4] = '\0'; 147 | } else { 148 | fprintf(stderr, "Error: Cannot infer output filename\n"); 149 | exit(1); 150 | } 151 | } else if (argc == i+2) { 152 | input_name = argv[i]; 153 | output_name = argv[i+1]; 154 | } else { 155 | fprintf(stderr, "Usage: %s [-f] input.zx7 [output]\n" 156 | " -f Force overwrite of output file\n", argv[0]); 157 | exit(1); 158 | } 159 | 160 | /* open input file */ 161 | ifp = fopen(input_name, "rb"); 162 | if (!ifp) { 163 | fprintf(stderr, "Error: Cannot access input file %s\n", input_name); 164 | exit(1); 165 | } 166 | 167 | /* determine input size */ 168 | fseek(ifp, 0L, SEEK_END); 169 | input_size = ftell(ifp); 170 | fseek(ifp, 0L, SEEK_SET); 171 | if (!input_size) { 172 | fprintf(stderr, "Error: Empty input file %s\n", argv[1]); 173 | exit(1); 174 | } 175 | 176 | /* allocate input buffer */ 177 | input_data = (unsigned char *)malloc(input_size); 178 | if (!input_data) { 179 | fprintf(stderr, "Error: Insufficient memory\n"); 180 | exit(1); 181 | } 182 | 183 | /* read input file */ 184 | total_counter = 0; 185 | do { 186 | partial_counter = fread(input_data+total_counter, sizeof(char), input_size-total_counter, ifp); 187 | total_counter += partial_counter; 188 | } while ( partial_counter > 0 ); 189 | 190 | if (total_counter != input_size) { 191 | fprintf(stderr, "Error: Cannot read input file %s\n", argv[1]); 192 | exit(1); 193 | } 194 | 195 | /* check output file */ 196 | if (!forced_mode && fopen(output_name, "rb") != NULL) { 197 | fprintf(stderr, "Error: Already existing output file %s\n", output_name); 198 | exit(1); 199 | } 200 | 201 | /* create output file */ 202 | ofp = fopen(output_name, "wb"); 203 | if (!ofp) { 204 | fprintf(stderr, "Error: Cannot create output file %s\n", output_name); 205 | exit(1); 206 | } 207 | 208 | /* reverse input data */ 209 | for ( i= 0; i>1; i++ ){ 210 | partial_counter = input_data[i]; 211 | input_data[i] = input_data[input_size-1-i]; 212 | input_data[input_size-1-i] = partial_counter; 213 | } 214 | 215 | /* calculate output file size and allocate memory */ 216 | output_size = 1; 217 | read_byte(); 218 | while (1) { 219 | if (!read_bit()) { 220 | output_size++; 221 | read_byte(); 222 | } else { 223 | i = read_elias_gamma()+1; 224 | if (i == 0) { 225 | break; 226 | } 227 | read_offset(); 228 | output_size+= i; 229 | } 230 | } 231 | output_data = (unsigned char *)malloc(output_size); 232 | if (!output_data) { 233 | fprintf(stderr, "Error: Insufficient memory\n"); 234 | exit(1); 235 | } 236 | 237 | /* decompress */ 238 | decompress(); 239 | 240 | /* reverse output data */ 241 | for ( i= 0; i>1; i++ ) { 242 | partial_counter= output_data[i]; 243 | output_data[i]= output_data[output_size-1-i]; 244 | output_data[output_size-1-i]= partial_counter; 245 | } 246 | 247 | /* write output file */ 248 | if (fwrite(output_data, sizeof(char), output_size, ofp) != output_size) { 249 | fprintf(stderr, "Error: Cannot write output file %s\n", output_name); 250 | exit(1); 251 | } 252 | 253 | /* close input file */ 254 | fclose(ifp); 255 | 256 | /* close output file */ 257 | fclose(ofp); 258 | 259 | /* done! */ 260 | printf("File converted from %lu to %lu bytes!\n", (unsigned long)input_size, (unsigned long)output_size); 261 | 262 | return 0; 263 | } 264 | -------------------------------------------------------------------------------- /zx7b.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ZX7b (c) Copyright 2013 by Antonio Villena. All rights reserved. 3 | * 4 | * Based on ZX7 5 | * (c) Copyright 2012 by Einar Saukas. All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * * Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * * The name of its author may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #define MAX_OFFSET 2176 /* range 1..2176 */ 34 | #define MAX_LEN 65536 /* range 2..65536 */ 35 | 36 | typedef struct match_t { 37 | size_t index; 38 | struct match_t *next; 39 | } Match; 40 | 41 | typedef struct optimal_t { 42 | size_t bits; 43 | int offset; 44 | int len; 45 | } Optimal; 46 | 47 | Optimal *optimize(unsigned char *input_data, size_t input_size); 48 | 49 | unsigned char *compress(Optimal *optimal, unsigned char *input_data, size_t input_size, size_t *output_size); 50 | 51 | int main(int argc, char *argv[]) { 52 | FILE *ifp; 53 | FILE *ofp; 54 | unsigned char *input_data; 55 | unsigned char *output_data; 56 | size_t input_size; 57 | size_t output_size; 58 | size_t partial_counter; 59 | size_t total_counter; 60 | char *output_name; 61 | int i, j; 62 | 63 | if( argc==1 ) 64 | printf("\nZX7 Backwards compressor v1.01 by Einar Saukas/AntonioVillena, 28 Dec 2013\n\n" 65 | " zx7b \n\n" 66 | " Raw input file\n" 67 | " Compressed output file\n\n" 68 | "Example: zx7b Cobra.scr Cobra.zx7b\n"), 69 | exit(0); 70 | if( argc!=3 ) 71 | printf("\nInvalid number of parameters\n"), 72 | exit(-1); 73 | 74 | /* open input file */ 75 | ifp= fopen(argv[1], "rb"); 76 | if( !ifp ) 77 | fprintf(stderr, "Error: Cannot access input file %s\n", argv[1]), 78 | exit(1); 79 | 80 | /* determine input size */ 81 | fseek(ifp, 0L, SEEK_END); 82 | input_size= ftell(ifp); 83 | fseek(ifp, 0L, SEEK_SET); 84 | if( !input_size ) 85 | fprintf(stderr, "Error: Empty input file %s\n", argv[1]), 86 | exit(1); 87 | 88 | /* allocate input buffer */ 89 | input_data= (unsigned char *)malloc(input_size); 90 | if( !input_data ) 91 | fprintf(stderr, "Error: Insufficient memory\n"), 92 | exit(1); 93 | 94 | /* read input file */ 95 | total_counter= 0; 96 | do { 97 | partial_counter = fread(input_data+total_counter, sizeof(char), input_size-total_counter, ifp); 98 | total_counter += partial_counter; 99 | } while ( partial_counter > 0 ); 100 | 101 | if( total_counter != input_size ) 102 | fprintf(stderr, "Error: Cannot read input file %s\n", argv[1]), 103 | exit(1); 104 | 105 | /* close input file */ 106 | fclose(ifp); 107 | 108 | /* create output file */ 109 | ofp= fopen(argv[2], "wb"); 110 | if( !ofp ) 111 | fprintf(stderr, "Error: Cannot create output file %s\n", argv[2]), 112 | exit(1); 113 | 114 | for ( i= 0; i>1; i++ ) 115 | j= input_data[i], 116 | input_data[i]= input_data[input_size-1-i], 117 | input_data[input_size-1-i]= j; 118 | 119 | /* generate output file */ 120 | output_data= compress(optimize(input_data, input_size), input_data, input_size, &output_size); 121 | 122 | for ( i= 0; i>1; i++ ) 123 | j= output_data[i], 124 | output_data[i]= output_data[output_size-1-i], 125 | output_data[output_size-1-i]= j; 126 | 127 | /* write output file */ 128 | if( fwrite(output_data, sizeof(char), output_size, ofp) != output_size ) 129 | fprintf(stderr, "Error: Cannot write output file %s\n", output_name), 130 | exit(1); 131 | 132 | /* close output file */ 133 | fclose(ofp); 134 | 135 | /* done! */ 136 | printf("\nFile %s compressed from %s (%d to %d bytes)\n", argv[2], argv[1], (int) input_size, (int) output_size); 137 | return 0; 138 | } 139 | 140 | unsigned char* output_data; 141 | size_t output_index; 142 | size_t bit_index; 143 | int bit_mask; 144 | 145 | void write_byte(int value) { 146 | output_data[output_index++] = value; 147 | } 148 | 149 | void write_bit(int value) { 150 | if( bit_mask == 0 ) 151 | bit_mask = 128, 152 | bit_index = output_index, 153 | write_byte(0); 154 | if( value > 0 ) 155 | output_data[bit_index] |= bit_mask; 156 | bit_mask >>= 1; 157 | } 158 | 159 | int elias_gamma_bits(int value) { 160 | int bits; 161 | bits= 1; 162 | while ( value > 1 ) 163 | bits+= 2, 164 | value>>= 1; 165 | return bits; 166 | } 167 | 168 | void write_elias_gamma(int value) { 169 | int bits= 0, rvalue= 0; 170 | while ( value>1 ) 171 | ++bits, 172 | rvalue<<= 1, 173 | rvalue|= value&1, 174 | value>>= 1; 175 | while ( bits-- ) 176 | write_bit(0), 177 | write_bit(rvalue & 1), 178 | rvalue>>= 1; 179 | write_bit(1); 180 | } 181 | 182 | unsigned char *compress(Optimal *optimal, unsigned char *input_data, size_t input_size, size_t *output_size) { 183 | size_t input_index; 184 | size_t input_prev; 185 | int offset1; 186 | int mask; 187 | int i; 188 | 189 | /* calculate and allocate output buffer */ 190 | input_index= input_size-1; 191 | *output_size= (optimal[input_index].bits+16+7)/8; 192 | output_data= (unsigned char *)malloc(*output_size); 193 | if( !output_data ) 194 | fprintf(stderr, "Error: Insufficient memory\n"), 195 | exit(1); 196 | 197 | /* un-reverse optimal sequence */ 198 | optimal[input_index].bits= 0; 199 | while ( input_index > 0 ) 200 | input_prev= input_index - (optimal[input_index].len > 0 ? optimal[input_index].len : 1), 201 | optimal[input_prev].bits= input_index, 202 | input_index= input_prev; 203 | 204 | output_index= 0; 205 | bit_mask= 0; 206 | 207 | /* first byte is always literal */ 208 | write_byte(input_data[0]); 209 | 210 | /* process remaining bytes */ 211 | while ( (input_index= optimal[input_index].bits) > 0) 212 | if( optimal[input_index].len == 0) 213 | write_bit(0), 214 | write_byte(input_data[input_index]); 215 | else{ 216 | /* sequence indicator */ 217 | write_bit(1); 218 | 219 | /* sequence length */ 220 | write_elias_gamma(optimal[input_index].len-1); 221 | 222 | /* sequence offset */ 223 | offset1= optimal[input_index].offset-1; 224 | if( offset1 < 128 ) 225 | write_byte(offset1); 226 | else{ 227 | offset1-= 128; 228 | write_byte((offset1 & 127) | 128); 229 | for ( mask= 1024; mask > 127; mask >>= 1) 230 | write_bit(offset1 & mask); 231 | } 232 | } 233 | 234 | /* end mark */ 235 | write_bit(1); 236 | write_elias_gamma(0xff); 237 | return output_data; 238 | } 239 | 240 | int count_bits(int offset, int len) { 241 | return 1 + (offset > 128 ? 12 : 8) + elias_gamma_bits(len-1); 242 | } 243 | 244 | Optimal* optimize(unsigned char *input_data, size_t input_size) { 245 | size_t *min; 246 | size_t *max; 247 | Match *matches; 248 | Match *match_slots; 249 | Optimal *optimal; 250 | Match *match; 251 | int match_index; 252 | int offset; 253 | size_t len; 254 | size_t best_len; 255 | size_t bits; 256 | size_t i; 257 | 258 | /* allocate all data structures at once */ 259 | min = (size_t *)calloc(MAX_OFFSET+1, sizeof(size_t)); 260 | max = (size_t *)calloc(MAX_OFFSET+1, sizeof(size_t)); 261 | matches = (Match *)calloc(256*256, sizeof(Match)); 262 | match_slots = (Match *)calloc(input_size, sizeof(Match)); 263 | optimal = (Optimal *)calloc(input_size, sizeof(Optimal)); 264 | 265 | if (!min || !max || !matches || !match_slots || !optimal) { 266 | fprintf(stderr, "Error: Insufficient memory\n"); 267 | exit(1); 268 | } 269 | 270 | /* first byte is always literal */ 271 | optimal[0].bits = 8; 272 | 273 | /* process remaining bytes */ 274 | for ( i= 1; i < input_size; i++ ){ 275 | optimal[i].bits= optimal[i-1].bits + 9; 276 | match_index= input_data[i-1] << 8 | input_data[i]; 277 | best_len= 1; 278 | for ( match= &matches[match_index]; match->next != NULL && best_len < MAX_LEN; match = match->next){ 279 | offset= i - match->next->index; 280 | if( offset > MAX_OFFSET ){ 281 | match->next = NULL; 282 | break; 283 | } 284 | for ( len= 2; len <= MAX_LEN; len++ ){ 285 | if( len > best_len && len&0xff ){ 286 | best_len= len; 287 | bits= optimal[i-len].bits + count_bits(offset, len); 288 | if( optimal[i].bits > bits ) 289 | optimal[i].bits= bits, 290 | optimal[i].offset= offset, 291 | optimal[i].len= len; 292 | } 293 | else if ( i+1 == max[offset]+len && max[offset] != 0 ){ 294 | len= i-min[offset]; 295 | if( len > best_len ) 296 | len= best_len; 297 | } 298 | if( i < offset+len || input_data[i-len] != input_data[i-len-offset] ) 299 | break; 300 | } 301 | min[offset]= i+1-len; 302 | max[offset]= i; 303 | } 304 | match_slots[i].index= i; 305 | match_slots[i].next= matches[match_index].next; 306 | matches[match_index].next= &match_slots[i]; 307 | } 308 | 309 | /* save time by releasing the largest block only, the O.S. will clean everything else later */ 310 | free(match_slots); 311 | return optimal; 312 | } 313 | -------------------------------------------------------------------------------- /benchmark_files/alice16k.txt: -------------------------------------------------------------------------------- 1 | ALICE'S ADVENTURES IN WONDERLAND Lewis Carroll THE MILLENNIUM FULCRUM EDITION 2.9 CHAPTER I Down the Rabbit-Hole Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, `and what is the use of a book,' thought Alice `without pictures or conversation?' So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her. There was nothing so VERY remarkable in that; nor did Alice think it so VERY much out of the way to hear the Rabbit say to itself, `Oh dear! Oh dear! I shall be late!' (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT- POCKET, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge. In another moment down went Alice after it, never once considering how in the world she was to get out again. The rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly down, so suddenly that Alice had not a moment to think about stopping herself before she found herself falling down a very deep well. Either the well was very deep, or she fell very slowly, for she had plenty of time as she went down to look about her and to wonder what was going to happen next. First, she tried to look down and make out what she was coming to, but it was too dark to see anything; then she looked at the sides of the well, and noticed that they were filled with cupboards and book-shelves; here and there she saw maps and pictures hung upon pegs. She took down a jar from one of the shelves as she passed; it was labelled `ORANGE MARMALADE', but to her great disappointment it was empty: she did not like to drop the jar for fear of killing somebody, so managed to put it into one of the cupboards as she fell past it. `Well!' thought Alice to herself, `after such a fall as this, I shall think nothing of tumbling down stairs! How brave they'll all think me at home! Why, I wouldn't say anything about it, even if I fell off the top of the house!' (Which was very likely true.) Down, down, down. Would the fall NEVER come to an end! `I wonder how many miles I've fallen by this time?' she said aloud. `I must be getting somewhere near the centre of the earth. Let me see: that would be four thousand miles down, I think--' (for, you see, Alice had learnt several things of this sort in her lessons in the schoolroom, and though this was not a VERY good opportunity for showing off her knowledge, as there was no one to listen to her, still it was good practice to say it over) `--yes, that's about the right distance--but then I wonder what Latitude or Longitude I've got to?' (Alice had no idea what Latitude was, or Longitude either, but thought they were nice grand words to say.) Presently she began again. `I wonder if I shall fall right THROUGH the earth! How funny it'll seem to come out among the people that walk with their heads downward! The Antipathies, I think--' (she was rather glad there WAS no one listening, this time, as it didn't sound at all the right word) `--but I shall have to ask them what the name of the country is, you know. Please, Ma'am, is this New Zealand or Australia?' (and she tried to curtsey as she spoke--fancy CURTSEYING as you're falling through the air! Do you think you could manage it?) `And what an ignorant little girl she'll think me for asking! No, it'll never do to ask: perhaps I shall see it written up somewhere.' Down, down, down. There was nothing else to do, so Alice soon began talking again. `Dinah'll miss me very much to-night, I should think!' (Dinah was the cat.) `I hope they'll remember her saucer of milk at tea-time. Dinah my dear! I wish you were down here with me! There are no mice in the air, I'm afraid, but you might catch a bat, and that's very like a mouse, you know. But do cats eat bats, I wonder?' And here Alice began to get rather sleepy, and went on saying to herself, in a dreamy sort of way, `Do cats eat bats? Do cats eat bats?' and sometimes, `Do bats eat cats?' for, you see, as she couldn't answer either question, it didn't much matter which way she put it. She felt that she was dozing off, and had just begun to dream that she was walking hand in hand with Dinah, and saying to her very earnestly, `Now, Dinah, tell me the truth: did you ever eat a bat?' when suddenly, thump! thump! down she came upon a heap of sticks and dry leaves, and the fall was over. Alice was not a bit hurt, and she jumped up on to her feet in a moment: she looked up, but it was all dark overhead; before her was another long passage, and the White Rabbit was still in sight, hurrying down it. There was not a moment to be lost: away went Alice like the wind, and was just in time to hear it say, as it turned a corner, `Oh my ears and whiskers, how late it's getting!' She was close behind it when she turned the corner, but the Rabbit was no longer to be seen: she found herself in a long, low hall, which was lit up by a row of lamps hanging from the roof. There were doors all round the hall, but they were all locked; and when Alice had been all the way down one side and up the other, trying every door, she walked sadly down the middle, wondering how she was ever to get out again. Suddenly she came upon a little three-legged table, all made of solid glass; there was nothing on it except a tiny golden key, and Alice's first thought was that it might belong to one of the doors of the hall; but, alas! either the locks were too large, or the key was too small, but at any rate it would not open any of them. However, on the second time round, she came upon a low curtain she had not noticed before, and behind it was a little door about fifteen inches high: she tried the little golden key in the lock, and to her great delight it fitted! Alice opened the door and found that it led into a small passage, not much larger than a rat-hole: she knelt down and looked along the passage into the loveliest garden you ever saw. How she longed to get out of that dark hall, and wander about among those beds of bright flowers and those cool fountains, but she could not even get her head though the doorway; `and even if my head would go through,' thought poor Alice, `it would be of very little use without my shoulders. Oh, how I wish I could shut up like a telescope! I think I could, if I only know how to begin.' For, you see, so many out-of-the-way things had happened lately, that Alice had begun to think that very few things indeed were really impossible. There seemed to be no use in waiting by the little door, so she went back to the table, half hoping she might find another key on it, or at any rate a book of rules for shutting people up like telescopes: this time she found a little bottle on it, (`which certainly was not here before,' said Alice,) and round the neck of the bottle was a paper label, with the words `DRINK ME' beautifully printed on it in large letters. It was all very well to say `Drink me,' but the wise little Alice was not going to do THAT in a hurry. `No, I'll look first,' she said, `and see whether it's marked "poison" or not'; for she had read several nice little histories about children who had got burnt, and eaten up by wild beasts and other unpleasant things, all because they WOULD not remember the simple rules their friends had taught them: such as, that a red-hot poker will burn you if you hold it too long; and that if you cut your finger VERY deeply with a knife, it usually bleeds; and she had never forgotten that, if you drink much from a bottle marked `poison,' it is almost certain to disagree with you, sooner or later. However, this bottle was NOT marked `poison,' so Alice ventured to taste it, and finding it very nice, (it had, in fact, a sort of mixed flavour of cherry-tart, custard, pine-apple, roast turkey, toffee, and hot buttered toast,) she very soon finished it off. * * * * * * * * * * * * * * * * * * * * `What a curious feeling!' said Alice; `I must be shutting up like a telescope.' And so it was indeed: she was now only ten inches high, and her face brightened up at the thought that she was now the right size for going though the little door into that lovely garden. First, however, she waited for a few minutes to see if she was going to shrink any further: she felt a little nervous about this; `for it might end, you know,' said Alice to herself, `in my going out altogether, like a candle. I wonder what I should be like then?' And she tried to fancy what the flame of a candle is like after the candle is blown out, for she could not remember ever having seen such a thing. After a while, finding that nothing more happened, she decided on going into the garden at once; but, alas for poor Alice! when she got to the door, she found he had forgotten the little golden key, and when she went back to the table for it, she found she could not possibly reach it: she could see it quite plainly through the glass, and she tried her best to climb up one of the legs of the table, but it was too slippery; and when she had tired herself out with trying, the poor little thing sat down and cried. `Come, there's no use in crying like that!' said Alice to herself, rather sharply; `I advise you to leave off this minute!' She generally gave herself very good advice, (though she very seldom followed it), and sometimes she scolded herself so severely as to bring tears into her eyes; and once she remembered trying to box her own ears for having cheated herself in a game of croquet she was playing against herself, for this curious child was very fond of pretending to be two people. `But it's no use now,' thought poor Alice, `to pretend to be two people! Why, there's hardly enough of me left to make ONE respectable person!' Soon her eye fell on a little glass box that was lying under the table: she opened it, and found in it a very small cake, on which the words `EAT ME' were beautifully marked in currants. `Well, I'll eat it,' said Alice, `and if it makes me grow larger, I can reach the key; and if it makes me grow smaller, I can creep under the door; so either way I'll get into the garden, and I don't care which happens!' She ate a little bit, and said anxiously to herself, `Which way? Which way?', holding her hand on the top of her head to feel which way it was growing, and she was quite surprised to find that she remained the same size: to be sure, this generally happens when one eats cake, but Alice had got so much into the way of expecting nothing but out-of-the-way things to happen, that it seemed quite dull and stupid for life to go on in the common way. So she set to work, and very soon finished off the cake. * * * * * * * * * * * * * * * * * * * * CHAPTER II The Pool of Tears `Curiouser and curiouser!' cried Alice (she was so much surprised, that for the moment she quite forgot how to speak good English); `now I'm opening out like the largest telescope that ever was! Good-bye, feet!' (for when she looked down at her feet, they seemed to be almost out of sight, they were getting so far off). `Oh, my poor little feet, I wonder who will put on your shoes and stockings for you now, dears? I'm sure _I_ shan't be able! I shall be a great deal too far off to trouble myself about you: you must manage the best way you can; --but I must be kind to them,' thought Alice, `or perhaps they won't walk the way I want to go! Let me see: I'll give them a new pair of boots every Christmas.' And she went on planning to herself how she would manage it. `They must go by the carrier,' she thought; `and how funny it'll seem, sending presents to one's own feet! And how odd the directions will look! ALICE'S RIGHT FOOT, ESQ. HEARTHRUG, NEAR THE FENDER, (WITH ALICE'S LOVE). Oh dear, what nonsense I'm talking!' Just then her head struck against the roof of the hall: in fact she was now more than nine feet high, and she at once took up the little golden key and hurried off to the garden door. Poor Alice! It was as much as she could do, lying down on one side, to look through into the garden with one eye; but to get through was more hopeless than ever: she sat down and began to cry again. `You ought to be ashamed of yourself,' said Alice, `a great girl like you,' (she might well say this), `to go on crying in this way! Stop this moment, I tell you!' But she went on all the same, shedding gallons of tears, until there was a large pool all round her, about four inches deep and reaching half down the hall. After a time she heard a little pattering of feet in the distance, and she hastily dried her eyes to see what was coming. It was the White Rabbit returning, splendidly dressed, with a pair of white kid gloves in one hand and a large fan in the other: he came trotting along in a great hurry, muttering to himself as he came, `Oh! the Duchess, the Duchess! Oh! won't she be savage if I've kept her waiting!' Alice felt so desperate that she was ready to ask help of any one; so, when the Rabbit came near her, she began, in a low, timid voice, `If you please, sir--' The Rabbit started violently, dropped the white kid gloves and the fan, and skurried away into the darkness as hard as he could go. Alice took up the fan and gloves, and, as the hall was very hot, she kept fanning herself all the time she went on talking: `Dear, dear! How queer everything is to-day! And yesterday things went on just as usual. I wonder if I've been changed in the night? Let me think: was I the same when I got up this morning? I almost think I can remember feeling a little different. But if I'm not the same, the next question is, Who in the world am I? Ah, THAT'S the great puzzle!' And she began thinking over all the children she knew that were of the same age as herself, to see if she could have been changed for any of them. `I'm sure I'm not Ada,' she said, `for her hair goes in such long ringlets, and mine doesn't go in ringlets at all; and I'm sure I can't be Mabel, for I know all sorts of things, and she, oh! she knows such a very little! Besides, SHE'S she, and I'm I, and--oh dear, how puzzling it all is! I'll try if I know all the things I used to know. Let me see: four times five is twelve, and four times six is thirteen, and four times seven is--oh dear! I shall never get to twenty at that rate! However, the Multiplication Table doesn't signify: let's try Geography. London is the capital of Paris, and Paris is the capital of Rome, and Rome--no, THAT'S all wrong, I'm certain! I must have been changed for Mabel! I'll try and say "How doth the little--"' and she crossed her hands on her lap as if she were saying lessons, and began to repeat it, but her voice sounded hoarse and strange, and the words did not come the same as they used to do:-- `How doth the little crocodile Improve his shining tail, And pour the waters of the Nile On every golden scale! `How cheerfully he seems to grin, How neatly spread his claws, And welcome little fishes in -------------------------------------------------------------------------------- /GenTape/GenTape.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | unsigned char *mem, *precalc; 5 | char *ext, *command; 6 | unsigned char rem= 0, inibit= 0, tzx= 0, wav= 0, channel_type= 1, 7 | checksum, turbo, mod; 8 | FILE *fi, *fo; 9 | int i, j, k, l, ind= 0, nextsilence= 0; 10 | float silence; 11 | unsigned short length, param, frequency= 44100; 12 | 13 | int strcasecmp(const char *s1, const char *s2){ 14 | register const unsigned char *us1= (const unsigned char *)s1, 15 | *us2= (const unsigned char *)s2; 16 | while ( (*us1 & 95) == (*us2++ & 95) ) 17 | if( *us1++ == '\0' ) 18 | return 0; 19 | return (*us1 & 95) - (*--us2 & 95); 20 | } 21 | 22 | void outbits( short val ){ 23 | for ( i= 0; i0xff000 ) 31 | fwrite( precalc, 1, ind, fo ), 32 | ind= 0; 33 | inibit^= 1; 34 | } 35 | 36 | void obgen( int nor ){ 37 | outbits( (nor+rem)/mod ); 38 | rem= (nor+rem)%mod; 39 | } 40 | 41 | char char2hex(char value, char * name){ 42 | if( value<'0' || value>'f' || value<'A' && value>'9' || value<'a' && value>'F' ) 43 | printf("\nInvalid character %c or '%s' not exists\n", value, name), 44 | exit(-1); 45 | return value>'9' ? 9+(value&7) : value-'0'; 46 | } 47 | 48 | int parseHex(char * name, int index){ 49 | int flen= strlen(name); 50 | if( name[0]!='-' ) 51 | for ( i= 0; i<10 && name[i]; i++ ) 52 | mem[i+7]= name[i]; 53 | else if( flen & 1 ){ 54 | flen>>= 1; 55 | flen>10 && index==7 && (flen= 10); 56 | for ( i= 0; i < flen; i++ ) 57 | mem[i+index]= char2hex(name[i+1<<1], name) | char2hex(name[i<<1|1], name) << 4; 58 | } 59 | while( ++i<11 ) 60 | mem[i+6]= ' '; 61 | return flen; 62 | } 63 | 64 | int wavsilence( float msecs ){ 65 | fwrite( precalc, 1, ind, fo ); 66 | rem= ind= 0; 67 | fwrite( precalc+0x100000, 1, frequency*(channel_type&3)*msecs/1000, fo); 68 | } 69 | 70 | void tapewrite( unsigned char *buff, int length ){ 71 | if( wav ){ 72 | buff+= 2; 73 | length-= 2; 74 | j= *buff>>7&1 ? 3223 : 8063; 75 | while( j-- ) 76 | obgen( 2168*2 ); 77 | obgen( 667*2 ); 78 | obgen( 735*2 ); 79 | while ( length-- ) 80 | for( k= 0, j= *buff++; k<8; k++, j<<= 1 ) 81 | obgen( l= 1710 << ((j & 0x80)>>7) ), 82 | obgen( l ); 83 | obgen( l ); 84 | } 85 | else 86 | fwrite(buff, 1, length, fo); 87 | } 88 | 89 | int main(int argc, char* argv[]){ 90 | mem= (unsigned char *) malloc (0x20000); 91 | if( argc==1 ) 92 | printf("\n" 93 | "GenTape v1.0, a Tape File Generator by Antonio Villena, 1 Jun 2015\n\n" 94 | " GenTape [] [] \n" 95 | " [ basic \n" 96 | " | hdata
\n" 97 | " | data \n" 98 | " | pilot \n" 99 | " | pulse .. \n" 100 | " | pause \n" 101 | " | pure \n" 102 | " | turbo \n" 103 | " \n" 104 | " | stop48\n" 105 | " | plug-xxx-N .. ]\n\n" 106 | " Target file, between TAP, TZX or WAV file\n" 107 | " Up to 10 chars name between single quotes or in hexadecimal\n" 108 | " In decimal, first BASIC line to execute\n" 109 | "
In hexadecimal, address of the binary block\n" 110 | " Hexadecimal string or filename as data origin of that block\n" 111 | " \n" 112 | " Length of zero/one/syncs/pilot pulses at 3.528MHz clock\n" 113 | " \n" 114 | " Duration of pilot/pause after block in milliseconds\n" 115 | " Number of pulses in the sequence of pulses\n" 116 | " Length of X-th pulse in the sequence at 3.528MHz clock\n" 117 | " External generator, must exists xxx.exe and accept N params\n" 118 | " stop48 Only TZX. Signal end of tape on 48K machines\n\n" 119 | " WAV options:\n" 120 | " Sample frequency, 44100 or 48000. Default is 44100\n" 121 | " Possible values are: mono (default), stereo or stereoinv\n\n"), 122 | exit(0); 123 | while( 1 ) 124 | if( !strcasecmp(argv[1], "mono") || !strcasecmp(argv[1], "44100") ) 125 | ++argv, --argc; 126 | else if( !strcasecmp(argv[1], "stereo") ) 127 | channel_type= 2, ++argv, --argc; 128 | else if( !strcasecmp(argv[1], "stereoinv") ) 129 | channel_type= 6, ++argv, --argc; 130 | else if( !strcasecmp(argv[1], "48000") ) 131 | frequency= 48000, ++argv, --argc; 132 | else 133 | break; 134 | mod= 7056000/frequency; 135 | if( !(ext= strchr(argv[1], '.')) ) 136 | printf("\nInvalid argument name: %s\n", argv[1]), 137 | exit(-1); 138 | fo= fopen(argv[1], "wb+"); 139 | if( !fo ) 140 | printf("\nCannot create output file: %s\n", argv[1]), 141 | exit(-1); 142 | char filename[40]; 143 | strcpy( filename, argv[1] ); 144 | precalc= (unsigned char *) malloc (0x200000); 145 | if( !strcasecmp((char *)strchr(argv[1], '.'), ".tzx" ) ) 146 | fprintf( fo, "ZXTape!" ), 147 | *(int*)mem= 0xa011a, 148 | fwrite(mem, ++tzx, 3, fo), 149 | mem[0]= 0x10; 150 | else if( !strcasecmp((char *)strchr(argv[1], '.'), ".wav" ) ){ 151 | memset(mem, wav++, 44); 152 | memset(precalc, 128, 0x200000); 153 | *(int*)mem= 0x46464952; 154 | *(int*)(mem+8)= 0x45564157; 155 | *(int*)(mem+12)= 0x20746d66; 156 | *(char*)(mem+16)= 0x10; 157 | *(char*)(mem+20)= 0x01; 158 | *(char*)(mem+22)= *(char*)(mem+32)= channel_type&3; 159 | *(short*)(mem+24)= frequency; 160 | *(int*)(mem+28)= frequency*(channel_type&3); 161 | *(char*)(mem+34)= 8; 162 | *(int*)(mem+36)= 0x61746164; 163 | fwrite(mem, 1, 44, fo); 164 | } 165 | while ( argc-- > 2 ){ 166 | wav && nextsilence && wavsilence( silence ); 167 | if( !strcasecmp(argv++[2], "basic")){ 168 | *(short*)(mem+1)= 1000; 169 | tzx && fwrite(mem, 1, 3, fo); 170 | param= atoi(argv[3]); 171 | fi= fopen(argv[4], "rb"); 172 | if( fi ) 173 | length= fread(mem+27, 1, 0x20000-27, fi); 174 | else 175 | length= parseHex(argv[4], 27); 176 | *(int*)(mem+3)= 19; 177 | parseHex(argv[2], 7); 178 | *(short*)(mem+17)= *(short*)(mem+21)= length; 179 | *(short*)(mem+19)= param; 180 | length+= 2; 181 | *(short*)(mem+24)= length; 182 | mem[26]= 255; 183 | for ( checksum= 0, i= 5; i<23; ++i ) 184 | checksum^= mem[i]; 185 | mem[23]= checksum; 186 | for ( checksum= 0, i= 26; i<26+length; ++i ) 187 | checksum^= mem[i]; 188 | mem[length+25]= checksum; 189 | tapewrite(mem+3, 21); 190 | wav && wavsilence( 1000 ); 191 | *(short*)(mem+1)= 2000; 192 | tzx && fwrite(mem, 1, 3, fo); 193 | tapewrite(mem+24, length+2); 194 | silence= nextsilence= 2000; 195 | fclose(fi); 196 | argc-= 3; 197 | argv+= 3; 198 | } 199 | else if( !strcasecmp(argv[1], "hdata")){ 200 | *(short*)(mem+1)= 1000; 201 | tzx && fwrite(mem, 1, 3, fo); 202 | param= strtol(argv[3], NULL, 16); 203 | fi= fopen(argv[4], "rb"); 204 | if( fi ) 205 | length= fread(mem+27, 1, 0x20000-27, fi); 206 | else 207 | length= parseHex(argv[4], 27); 208 | *(short*)(mem+1)= 1000; 209 | *(short*)(mem+3)= 19; 210 | *(short*)(mem+5)= 0x300; 211 | parseHex(argv[2], 7); 212 | *(short*)(mem+17)= length; 213 | *(short*)(mem+19)= param; 214 | *(unsigned short*)(mem+21)= 0x8000; 215 | length+= 2; 216 | *(short*)(mem+24)= length; 217 | mem[26]= 255; 218 | for ( checksum= 0, i= 5; i<23; ++i ) 219 | checksum^= mem[i]; 220 | mem[23]= checksum; 221 | for ( checksum= 0, i= 26; i<26+length-1; ++i ) 222 | checksum^= mem[i]; 223 | mem[length+25]= checksum; 224 | tapewrite(mem+3, 21); 225 | wav && wavsilence( 1000 ); 226 | *(short*)(mem+1)= 2000; 227 | tzx && fwrite(mem, 1, 3, fo); 228 | tapewrite(mem+24, length+2); 229 | silence= nextsilence= 2000; 230 | fclose(fi); 231 | argc-= 3; 232 | argv+= 3; 233 | } 234 | else if( !strcasecmp(argv[1], "data")){ 235 | *(short*)(mem+1)= 2000; 236 | tzx && fwrite(mem, 1, 3, fo); 237 | fi= fopen(argv[2], "rb"); 238 | if( fi ) 239 | length= fread(mem+6, 1, 0x20000-6, fi); 240 | else 241 | length= parseHex(argv[2], 6); 242 | *(short*)(mem+3)= length+= 2; 243 | mem[5]= 255; 244 | for ( checksum= 0, i= 5; i<5+length-1; ++i ) 245 | checksum^= mem[i]; 246 | mem[length+4]= checksum; 247 | tapewrite(mem+3, length+2); 248 | silence= nextsilence= 2000; 249 | fclose(fi); 250 | --argc; 251 | ++argv; 252 | } 253 | else if( !strcasecmp(argv[1], "pause")){ 254 | nextsilence= silence= atof(argv[2]); 255 | if( tzx ) 256 | mem[1]= 0x20, 257 | *(short*)(mem+2)= nextsilence, 258 | fwrite(mem+1, 1, 3, fo); 259 | else if( !wav ) 260 | printf("\nError: pause command not allowed in TAP files\n"), 261 | exit(-1); 262 | --argc; 263 | ++argv; 264 | } 265 | else if( !strcasecmp(argv[1], "pilot")){ 266 | k= atoi(argv[2]); 267 | if( tzx ) 268 | mem[1]= 0x12, 269 | *(short*)(mem+2)= k, 270 | *(unsigned short*)(mem+4)= atof(argv[3])*3500/k+0.5, 271 | fwrite(mem+1, 1, 5, fo); 272 | else if( wav ){ 273 | k<<= 1; 274 | j= atof(argv[3])*7056/k+0.5; 275 | while( j-- ) 276 | obgen( k ); 277 | } 278 | else 279 | printf("\nError: pilot command not allowed in TAP files\n"), 280 | exit(-1); 281 | nextsilence= 0; 282 | argc-= 2; 283 | argv+= 2; 284 | } 285 | else if( !strcasecmp(argv[1], "pulse")){ 286 | k= atoi(argv++[2]); 287 | if( tzx ){ 288 | mem[1]= 0x13; 289 | *(unsigned char*)(mem+2)= k; 290 | for ( j= 0; j>16; 330 | fwrite(mem+1, 1, length+19, fo); 331 | } 332 | else{ 333 | mem[1]= 0x14; 334 | *(short*)(mem+2)= atoi(argv++[2]); 335 | *(short*)(mem+4)= atoi(argv++[2]); 336 | *(char*)(mem+6)= 8; 337 | *(unsigned short*)(mem+7)= atoi(argv++[2]); 338 | if( fi ) 339 | length= fread(mem+12, 1, 0x20000-12, fi); 340 | else 341 | length= parseHex(argv[2], 12); 342 | *(unsigned short*)(mem+9)= length; 343 | *(unsigned char*)(mem+11)= length>>16; 344 | fwrite(mem+1, 1, length+11, fo); 345 | } 346 | ++argv; 347 | } 348 | else if( wav ){ 349 | if( turbo ){ 350 | k= atoi(argv[2]) << 1; 351 | j= atof(argv[7])*7056/k+0.5; 352 | while( j-- ) 353 | obgen( k ); 354 | obgen( atoi(argv[3]) << 1 ); 355 | obgen( atoi(argv[4]) << 1 ); 356 | } 357 | if( fi ) 358 | length= fread(mem, 1, 0x20000, fi); 359 | else 360 | length= parseHex(argv[5+turbo*4], 0); 361 | j= 0; 362 | param= atoi(argv[2+turbo*3]) << 1; 363 | k= atoi(argv[3+turbo*3]) << 1; 364 | while ( length-- ) 365 | for( wav= 0, checksum= mem[j++]; wav<8; wav++, checksum<<= 1 ) 366 | obgen( l= checksum & 0x80 ? k : param ), 367 | obgen( l ); 368 | obgen( l ); 369 | fclose(fi); 370 | argv+= turbo+1<<2; 371 | nextsilence= silence= atof(argv[0]); 372 | } 373 | else 374 | printf("\nError: pure or turbo command not allowed in TAP files\n"), 375 | exit(-1); 376 | argc-= turbo+1<<2; 377 | } 378 | else if( strchr(argv[1], '-') 379 | && (*strchr(argv[1], '-')= 0, !strcasecmp(argv[1], "plug")) ){ 380 | argv[1]+= 5; 381 | k= atoi(strstr(argv[1], "-")+1); 382 | *strchr(argv[1], '-')= 0; 383 | command= (char *) malloc (0x100); 384 | sprintf(command, "%s %d %s tmp.%s", argv[1], frequency, 385 | channel_type-1 ? (channel_type-2?"stereoinv":"stereo") : "mono", ext+1); 386 | argc-= k; 387 | while( k-- ) 388 | strcat(command, " "), 389 | strcat(command, argv++[2]); 390 | if( system(command) ) 391 | printf("\nError: plug error with command: %s\n", command), 392 | exit(-1); 393 | else{ 394 | fwrite( precalc, 1, ind, fo ); 395 | rem= ind= 0; 396 | sprintf(command, "tmp.%s", ext+1); 397 | fi= fopen(command, "rb"); 398 | if( fi ){ 399 | if( tzx ) 400 | fseek(fi, 0, SEEK_END), 401 | i= ftell(fi)-10, 402 | fseek(fi, 10, SEEK_SET); 403 | else 404 | 0!=fread(mem, 1, 44, fi), 405 | i= *(int*)(mem+40); 406 | j= i>>20; 407 | k= i&0xfffff; 408 | for ( int i= 0; i 5 | * (c) Copyright 2012 by Einar Saukas. All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * * Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * * The name of its author may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #define MAX_FILESIZE 65536 34 | 35 | typedef struct match_t { 36 | size_t index; 37 | struct match_t *next; 38 | } Match; 39 | 40 | typedef struct optimal_t { 41 | size_t bits; 42 | int offset; 43 | int len; 44 | } Optimal; 45 | 46 | int off_bits, max_offset, max_len, bit_mask; 47 | Match *matches, *match_slots; 48 | Optimal *optimal; 49 | size_t *min, *max, input_size, output_size, output_index, bit_index; 50 | unsigned char *output_data, *input_data; 51 | int (*eliasbits)(int); 52 | void (*eliaswrite)(int); 53 | 54 | int count_bits(int offset, int len) { 55 | return 1 + (offset > 128 ? off_bits : 8) + eliasbits(len-1); 56 | } 57 | 58 | void write_byte(int value) { 59 | output_data[output_index++] = value; 60 | } 61 | 62 | void write_bit(int value) { 63 | if( bit_mask == 0 ) 64 | bit_mask = 128, 65 | bit_index = output_index, 66 | write_byte(0); 67 | if( value > 0 ) 68 | output_data[bit_index] |= bit_mask; 69 | bit_mask >>= 1; 70 | } 71 | 72 | void compress() { 73 | size_t input_index; 74 | size_t input_prev; 75 | int offset1; 76 | int mask; 77 | int i; 78 | 79 | /* calculate and allocate output buffer */ 80 | input_index= input_size-1; 81 | output_size= (optimal[input_index].bits+16+7)/8; 82 | 83 | /* un-reverse optimal sequence */ 84 | optimal[input_index].bits= 0; 85 | while ( input_index > 0 ) 86 | input_prev= input_index - (optimal[input_index].len > 0 ? optimal[input_index].len : 1), 87 | optimal[input_prev].bits= input_index, 88 | input_index= input_prev; 89 | 90 | output_index= 0; 91 | bit_mask= 0; 92 | 93 | /* first byte is always literal */ 94 | write_byte(input_data[0]); 95 | 96 | /* process remaining bytes */ 97 | while ( (input_index= optimal[input_index].bits) > 0) 98 | if( optimal[input_index].len == 0) 99 | write_bit(0), 100 | write_byte(input_data[input_index]); 101 | else{ 102 | /* sequence indicator */ 103 | write_bit(1); 104 | 105 | /* sequence length */ 106 | eliaswrite(optimal[input_index].len-1); 107 | 108 | /* sequence offset */ 109 | offset1= optimal[input_index].offset-1; 110 | if( offset1 < 128 ) 111 | write_byte(offset1); 112 | else{ 113 | offset1-= 128; 114 | write_byte((offset1 & 127) | 128); 115 | if( off_bits==16 ) 116 | write_byte(offset1+128>>7); 117 | else 118 | for ( mask= 1<<(off_bits-2); mask > 127; mask >>= 1) 119 | write_bit(offset1 & mask); 120 | } 121 | } 122 | 123 | /* end mark */ 124 | write_bit(1); 125 | eliaswrite(0xff); 126 | } 127 | 128 | void optimize() { 129 | Match *match; 130 | int match_index; 131 | int offset; 132 | size_t len; 133 | size_t best_len; 134 | size_t bits; 135 | size_t i; 136 | 137 | memset(min, 0, (max_offset+1)*sizeof(size_t)); 138 | memset(max, 0, (max_offset+1)*sizeof(size_t)); 139 | memset(matches, 0, 256*256*sizeof(Match)); 140 | memset(match_slots, 0, input_size*sizeof(Match)); 141 | memset(optimal, 0, input_size*sizeof(Optimal)); 142 | optimal[0].bits = 8; 143 | for ( i= 1; i < input_size; i++ ){ 144 | optimal[i].bits= optimal[i-1].bits + 9; 145 | match_index= input_data[i-1] << 8 | input_data[i]; 146 | best_len= 1; 147 | for ( match= &matches[match_index]; match->next != NULL && best_len < max_len; match = match->next){ 148 | offset= i - match->next->index; 149 | if( offset > max_offset ){ 150 | match->next = NULL; 151 | break; 152 | } 153 | for ( len= 2; len <= max_len; len++ ){ 154 | if( len > best_len && len&0xff ){ 155 | best_len= len; 156 | bits= optimal[i-len].bits + count_bits(offset, len); 157 | if( optimal[i].bits > bits ) 158 | optimal[i].bits= bits, 159 | optimal[i].offset= offset, 160 | optimal[i].len= len; 161 | } 162 | else if ( i+1 == max[offset]+len && max[offset] != 0 ){ 163 | len= i-min[offset]; 164 | if( len > best_len ) 165 | len= best_len; 166 | } 167 | if( i < offset+len || input_data[i-len] != input_data[i-len-offset] ) 168 | break; 169 | } 170 | min[offset]= i+1-len; 171 | max[offset]= i; 172 | } 173 | match_slots[i].index= i; 174 | match_slots[i].next= matches[match_index].next; 175 | matches[match_index].next= &match_slots[i]; 176 | } 177 | } 178 | 179 | int elias_gamma_bits1(int value) { 180 | int bits= 1; 181 | while ( value > 1 ) 182 | bits+= 2, 183 | value>>= 1; 184 | return bits; 185 | } 186 | 187 | int elias_gamma_bits2(int value) { 188 | int bits= 2; 189 | --value; 190 | while ( value > 1 ) 191 | bits+= 2, 192 | value-= 2, 193 | value>>= 1; 194 | return bits; 195 | } 196 | 197 | void write_elias_gamma1(int value) { 198 | int bits= 0, rvalue= 0; 199 | while ( value>1 ) 200 | ++bits, 201 | rvalue<<= 1, 202 | rvalue|= value&1, 203 | value>>= 1; 204 | while ( bits-- ) 205 | write_bit(0), 206 | write_bit(rvalue & 1), 207 | rvalue>>= 1; 208 | write_bit(1); 209 | } 210 | 211 | void write_elias_gamma2(int value) { 212 | int bits= 0, rvalue= 0; 213 | --value; 214 | while ( value>1 ) 215 | ++bits, 216 | value-= 2, 217 | rvalue<<= 1, 218 | rvalue|= value&1, 219 | value>>= 1; 220 | rvalue<<= 1, 221 | rvalue|= value&1; 222 | write_bit(rvalue & 1); 223 | while ( bits-- ) 224 | rvalue>>= 1, 225 | write_bit(0), 226 | write_bit(rvalue & 1); 227 | write_bit(1); 228 | } 229 | 230 | int main(int argc, char *argv[]) { 231 | FILE *ifp, *ofp; 232 | size_t partial_counter, total_counter; 233 | char *output_name, type, back, speed; 234 | int i, j, fil, size[18]; 235 | 236 | if( argc==1 ) 237 | printf("\nsaukav compressor v1.00 by Einar Saukas/AntonioVillena, 1 Nov 2017\n\n" 238 | " saukav .. \n\n" 239 | " Target decruncher\n" 240 | " Origin files\n\n" 241 | "Valid values are: f0, f1, f2, f3, f4, b0, b1, b2, b3 and b4\n" 242 | "You can attach o?g? to and fix compression algorithm\n" 243 | "Every input file will be compressed in a .skv output file\n" 244 | "It will generate the decruncher into the file d.asm\n"), 245 | exit(0); 246 | if( argc<3 ) 247 | printf("\nInvalid number of parameters\n"), 248 | exit(-1); 249 | back= (~argv[1][0] & 4) >> 2; 250 | speed= argv[1][1] - '0'; 251 | matches= (Match *)calloc(256*256, sizeof(Match)); 252 | output_data= (unsigned char *)malloc(MAX_FILESIZE); 253 | input_data= (unsigned char *)malloc(MAX_FILESIZE); 254 | match_slots= (Match *)calloc(MAX_FILESIZE, sizeof(Match)); 255 | optimal= (Optimal *)calloc(MAX_FILESIZE, sizeof(Optimal)); 256 | min= (size_t *)calloc(65537, sizeof(size_t)); 257 | max= (size_t *)calloc(65537, sizeof(size_t)); 258 | if( !matches || !output_data || !input_data || !match_slots 259 | || !optimal || !min || !max ) 260 | fprintf(stderr, "Error: Insufficient memory\n"), 261 | exit(1); 262 | if( strlen(argv[1])!=6 ){ 263 | printf("Searching best parameters"); 264 | for ( off_bits= 0; off_bits<17; off_bits++ ) 265 | size[off_bits]= 0; 266 | for (fil= 2; fil < argc; fil++) { 267 | ifp= fopen(argv[fil], "rb"); 268 | if( !ifp ) 269 | fprintf(stderr, "Error: Cannot access input file %s\n", argv[1]), 270 | exit(1); 271 | fseek(ifp, 0L, SEEK_END); 272 | input_size= ftell(ifp); 273 | fseek(ifp, 0L, SEEK_SET); 274 | if( !input_size ) 275 | fprintf(stderr, "Error: Empty input file %s\n", argv[1]), 276 | exit(1); 277 | total_counter= 0; 278 | do { 279 | partial_counter = fread(input_data+total_counter, sizeof(char), input_size-total_counter, ifp); 280 | total_counter += partial_counter; 281 | } while ( partial_counter > 0 ); 282 | if( total_counter != input_size ) 283 | fprintf(stderr, "Error: Cannot read input file %s\n", argv[1]), 284 | exit(1); 285 | fclose(ifp); 286 | if (back) 287 | for ( i= 0; i>1; i++ ) 288 | j= input_data[i], 289 | input_data[i]= input_data[input_size-1-i], 290 | input_data[input_size-1-i]= j; 291 | for ( off_bits= 8; off_bits<17; off_bits++ ) 292 | printf("."), 293 | max_offset= (1<<(off_bits-1))+128, 294 | max_len= off_bits==8 ? 256 : 65536, 295 | eliasbits= &elias_gamma_bits1, 296 | eliaswrite= &write_elias_gamma1, 297 | optimize(), 298 | size[off_bits-8<<1]+= optimal[input_size-1].bits+23>>3, 299 | eliasbits= &elias_gamma_bits2, 300 | eliaswrite= &write_elias_gamma2, 301 | optimize(), 302 | size[off_bits-8<<1|1]+= optimal[input_size-1].bits+23>>3; 303 | } 304 | } 305 | if( strlen(argv[1])==6 ) 306 | type= (argv[1][3]-'0')<<1|(argv[1][5]-'0'); 307 | else{ 308 | for ( i= 2e9, j= 0; j<18; j++ ) 309 | if( i>size[j] ) 310 | i= size[j], 311 | type= j; 312 | printf("%c%do%dg%d choosen\n", back ? 'b' : 'f', speed, type>>1, type&1); 313 | } 314 | off_bits= type+16>>1; 315 | max_offset= (1<<(off_bits-1))+128; 316 | max_len= off_bits==8 ? 256 : 65536; 317 | eliasbits= type&1 ? elias_gamma_bits2 : elias_gamma_bits1; 318 | eliaswrite= type&1 ? write_elias_gamma2 : write_elias_gamma1; 319 | for (fil= 2; fil < argc; fil++) { 320 | ifp= fopen(argv[fil], "rb"); 321 | fseek(ifp, 0L, SEEK_END); 322 | input_size= ftell(ifp); 323 | fseek(ifp, 0L, SEEK_SET); 324 | total_counter= 0; 325 | do { 326 | partial_counter= fread(input_data+total_counter, sizeof(char), input_size-total_counter, ifp); 327 | total_counter+= partial_counter; 328 | } while ( partial_counter > 0 ); 329 | fclose(ifp); 330 | output_name= (char *)malloc(strlen(argv[fil]) + 5); 331 | strcpy(output_name, argv[fil]); 332 | strcat(output_name, ".skv"); 333 | ofp= fopen(output_name, "wb+"); 334 | if( !ofp ) 335 | fprintf(stderr, "Error: Cannot create output file %s\n", argv[2]), 336 | exit(1); 337 | if (back) 338 | for ( i= 0; i>1; i++ ) 339 | j= input_data[i], 340 | input_data[i]= input_data[input_size-1-i], 341 | input_data[input_size-1-i]= j; 342 | optimize(); 343 | compress(); 344 | if (back) 345 | for ( i= 0; i>1; i++ ) 346 | j= output_data[i], 347 | output_data[i]= output_data[output_size-1-i], 348 | output_data[output_size-1-i]= j; 349 | if( fwrite(output_data, sizeof(char), output_size, ofp) != output_size ) 350 | fprintf(stderr, "Error: Cannot write output file %s\n", output_name), 351 | exit(1); 352 | fclose(ofp); 353 | printf("File %s compressed (%d to %d bytes)\n", output_name, input_size, output_size); 354 | } 355 | ofp= fopen("d.asm", "wb+"); 356 | if( !ofp ) 357 | printf("\nCannot create d.asm file"), 358 | exit(-1); 359 | fprintf(ofp, ";%c%do%dg%d\n", back ? 'b' : 'f', speed, type>>1, type&1); 360 | if( speed<2 || type<2 ){ 361 | if( type&1 || type<2 ) 362 | fprintf(ofp, "sauk: ld a, 128\n" 363 | "copyby: ld%c\n", back?'d':'i'); 364 | else 365 | fprintf(ofp, "sauk: ld bc, 32768\n" 366 | " ld a, b\n" 367 | "copyby: inc c\n" 368 | " ld%c\n", back?'d':'i'); 369 | if( speed ) 370 | fprintf(ofp, "mainlo: add a, a\n" 371 | " call z, getbit\n"); 372 | else 373 | fprintf(ofp, "mainlo: call getbit\n"); 374 | fprintf(ofp, " jr nc, copyby\n"); 375 | if( type<2 ){ 376 | if( type&1 ){ 377 | fprintf(ofp, " ld bc, 1\n"); 378 | if( speed ) 379 | fprintf(ofp, "lenval: add a, a\n" 380 | " call z, getbit\n"); 381 | else 382 | fprintf(ofp, "lenval: call getbit\n"); 383 | fprintf(ofp, " rl c\n" 384 | " ret c\n"); 385 | if( speed ) 386 | fprintf(ofp, " add a, a\n" 387 | " call z, getbit\n"); 388 | else 389 | fprintf(ofp, " call getbit\n"); 390 | fprintf(ofp, " jr nc, lenval\n"); 391 | } 392 | else{ 393 | fprintf(ofp, " ld bc, 0\n"); 394 | if( speed ) 395 | fprintf(ofp, " defb $30\n" 396 | "lenval: add a, a\n" 397 | " call z, getbit\n"); 398 | else 399 | fprintf(ofp, "lenval: call nc, getbit\n"); 400 | fprintf(ofp, " rl c\n"); 401 | if( speed ) 402 | fprintf(ofp, " add a, a\n" 403 | " call z, getbit\n"); 404 | else 405 | fprintf(ofp, " call getbit\n"); 406 | fprintf(ofp, " jr nc, lenval\n" 407 | " inc c\n" 408 | " ret z\n"); 409 | } 410 | fprintf(ofp, " push hl\n" 411 | " ld l, (hl)\n" 412 | " ld h, b\n"); 413 | if( back ) 414 | fprintf(ofp, " adc hl, de\n" 415 | " lddr\n" 416 | " pop hl\n" 417 | " dec hl\n" 418 | " jr mainlo\n"); 419 | else 420 | fprintf(ofp, " push de\n" 421 | " ex de, hl\n" 422 | " sbc hl, de\n" 423 | " pop de\n" 424 | " ldir\n" 425 | " pop hl\n" 426 | " inc hl\n" 427 | " jr mainlo\n"); 428 | } 429 | else{ 430 | if( type&1 ){ 431 | fprintf(ofp, " ld bc, 1\n" 432 | " push de\n" 433 | " ld d, b\n"); 434 | if( speed ) 435 | fprintf(ofp, "lenval: add a, a\n" 436 | " call z, getbit\n"); 437 | else 438 | fprintf(ofp, "lenval: call getbit\n"); 439 | fprintf(ofp, " rl c\n" 440 | " jr z, exitdz\n" 441 | " rl b\n"); 442 | if( speed ) 443 | fprintf(ofp, " add a, a\n" 444 | " call z, getbit\n"); 445 | else 446 | fprintf(ofp, " call getbit\n"); 447 | fprintf(ofp, " jr nc, lenval\n"); 448 | } 449 | else{ 450 | fprintf(ofp, " push de\n" 451 | " ld d, c\n"); 452 | if( speed ) 453 | fprintf(ofp, " defb $30\n" 454 | "lenval: add a, a\n" 455 | " call z, getbit\n"); 456 | else 457 | fprintf(ofp, "lenval: call nc, getbit\n"); 458 | fprintf(ofp, " rl c\n" 459 | " rl b\n"); 460 | if( speed ) 461 | fprintf(ofp, " add a, a\n" 462 | " call z, getbit\n"); 463 | else 464 | fprintf(ofp, " call getbit\n"); 465 | fprintf(ofp, " jr nc, lenval\n" 466 | " inc c\n" 467 | " jr z, exitdz\n"); 468 | } 469 | fprintf(ofp, " ld e, (hl)\n" 470 | " %sc hl\n" 471 | " defb 203, 51\n" // sll e 472 | " jr nc, offend\n", back?"de":"in"); 473 | if( off_bits==16 ) 474 | fprintf(ofp, " ld d, (hl)\n" 475 | " %sc hl\n" 476 | " srl d\n", back?"de":"in"); 477 | else{ 478 | fprintf(ofp, " ld d, %d\n", 1<<(17-type>>1)); 479 | if( speed ) 480 | fprintf(ofp, "nexbit: add a, a\n" 481 | " call z, getbit\n"); 482 | else 483 | fprintf(ofp, "nexbit: call getbit\n"); 484 | fprintf(ofp, " rl d\n" 485 | " jr nc, nexbit\n" 486 | " inc d\n" 487 | " srl d\n"); 488 | } 489 | fprintf(ofp, "offend: rr e\n" 490 | " ex (sp), hl\n"); 491 | if( back ) 492 | fprintf(ofp, " ex de, hl\n" 493 | " adc hl, de\n" 494 | " lddr\n"); 495 | else 496 | fprintf(ofp, " push hl\n" 497 | " sbc hl, de\n" 498 | " pop de\n" 499 | " ldir\n"); 500 | fprintf(ofp, "exitdz: pop hl\n" 501 | " jr nc, mainlo\n"); 502 | } 503 | if( speed ) 504 | fprintf(ofp, "getbit: ld a, (hl)\n"); 505 | else 506 | fprintf(ofp, "getbit: add a, a\n" 507 | " ret nz\n" 508 | " ld a, (hl)\n"); 509 | fprintf(ofp, " %sc hl\n" 510 | " adc a, a\n" 511 | " ret\n", back?"de":"in"); 512 | } 513 | else{ 514 | fprintf(ofp, " macro getbitm\n" 515 | " add a, a\n"); 516 | if( speed==2 ) 517 | fprintf(ofp, " call z, getbit\n"); 518 | else 519 | fprintf(ofp, " jp nz, .gb1\n" 520 | " ld a, (hl)\n" 521 | " %sc hl\n" 522 | " adc a, a\n" 523 | ".gb1\n", back?"de":"in"); 524 | fprintf(ofp, " endm\n" 525 | "sauk: ld a, 128\n"); 526 | if( speed>3 ) 527 | fprintf(ofp, " ld%c\n" 528 | " add a, a\n" 529 | " jr z, mailao\n" 530 | " jr c, maicoo\n" 531 | " ld%c\n" 532 | " add a, a\n" 533 | " jr c, maicoe\n", back?'d':'i', back?'d':'i'); 534 | fprintf(ofp, "copbye: ld%c\n" 535 | " add a, a\n" 536 | " jr z, mailao\n" 537 | " jr c, maicoo\n" 538 | "copbyo: ld%c\n" 539 | " add a, a\n" 540 | " jr nc, copbye\n", back?'d':'i', back?'d':'i'); 541 | if( type&1 ) 542 | fprintf(ofp, "maicoe: ld bc, 1\n" 543 | "maisie: push de\n" 544 | " ld d, b\n" 545 | "levale: getbitm\n" 546 | " rl c\n" 547 | " jr z, exitdz\n" 548 | " rl b\n" 549 | " add a, a\n" 550 | " jr nc, levale\n" 551 | " ld e, (hl)\n", speed==2 ? 'r' : 'p'); 552 | else 553 | fprintf(ofp, "maicoe: ld bc, 2\n" 554 | "maisie: push de\n" 555 | " ld d, b\n" 556 | " getbitm\n" 557 | " j%c c, contie\n" 558 | " dec c\n" 559 | "levale: add a, a\n" 560 | " rl c\n" 561 | " rl b\n" 562 | " getbitm\n" 563 | " jr nc, levale\n" 564 | " inc c\n" 565 | " jr z, exitdz\n" 566 | "contie: ld e, (hl)\n", speed==2 ? 'r' : 'p'); 567 | fprintf(ofp, " %sc hl\n" 568 | " defb 203, 51\n" // sll e 569 | " j%c nc, offnd%c\n", back?"de":"in", 570 | off_bits==15?'p':'r', type&1?'o':'e'); 571 | if( off_bits==16 ) 572 | fprintf(ofp, " ld d, (hl)\n" 573 | " %sc hl\n" 574 | " srl d\n", back?"de":"in"); 575 | else{ 576 | for ( i=8; i3 ){ 605 | if( (off_bits^type)&1 ) 606 | fprintf(ofp, " jr z, mailao\n" 607 | " jr nc, copbyo\n" 608 | " ld c, %d\n" 609 | " jp maisio\n", 2-(type&1)); 610 | else 611 | fprintf(ofp, " jr nc, copbye\n" 612 | " ld c, %d\n" 613 | " jp maisie\n", 2-(type&1)); 614 | } 615 | else{ 616 | if( (off_bits^type)&1 ) 617 | fprintf(ofp, " jr z, mailao\n" 618 | " jr c, maicoo\n" 619 | " j%c copbyo\n", speed==2 ? 'r' : 'p'); 620 | else 621 | fprintf(ofp, " jr c, maicoe\n" 622 | " j%c copbye\n", speed==2 ? 'r' : 'p'); 623 | } 624 | fprintf(ofp, "exitdz: pop hl\n"); 625 | if( speed==2 ) 626 | fprintf(ofp, "getbit: ld a, (hl)\n" 627 | " %sc hl\n" 628 | " adc a, a\n", back?"de":"in"); 629 | fprintf(ofp, " ret\n" 630 | "mailao: ld a, (hl)\n" 631 | " %sc hl\n" 632 | " adc a, a\n" 633 | " jr nc, copbyo\n", back?"de":"in"); 634 | if( type&1 ) 635 | fprintf(ofp, "maicoo: ld bc, 1\n" 636 | "maisio: push de\n" 637 | " ld d, b\n" 638 | "levalo: add a, a\n" 639 | " rl c\n" 640 | " jr z, exitdz\n" 641 | " rl b\n" 642 | " getbitm\n" 643 | " jr nc, levalo\n" 644 | " ld e, (hl)\n"); 645 | else 646 | fprintf(ofp, "maicoo: ld bc, 2\n" 647 | "maisio: push de\n" 648 | " ld d, b\n" 649 | " add a, a\n" 650 | " j%c c, contio\n" 651 | " dec c\n" 652 | "levalo: getbitm\n" 653 | " rl c\n" 654 | " rl b\n" 655 | " add a, a\n" 656 | " jr nc, levalo\n" 657 | " inc c\n" 658 | " jr z, exitdz\n" 659 | "contio: ld e, (hl)\n", speed==2 ? 'r' : 'p'); 660 | fprintf(ofp, " %sc hl\n" 661 | " defb 203, 51\n" // sll e 662 | " jr nc, offnd%c\n", back?"de":"in", type&1?'e':'o'); 663 | if( off_bits==16 ) 664 | fprintf(ofp, " ld d, (hl)\n" 665 | " %sc hl\n" 666 | " srl d\n", back?"de":"in"); 667 | else{ 668 | for ( i=8; i