22#include <stdio.h>
33#include <stdlib.h>
44#include <string.h>
5- #include "sfx_small.h"
6- #include "sfx_fast.h"
7- #include "sfx_effect.h"
5+ #include "sfx.h"
86
97#define BUFFER_SIZE 65536 /* must be > MAX_OFFSET */
108#define INITIAL_OFFSET 1
1311#define TRUE 1
1412#define DALI_BITS_LEFT 1
1513#define DALI_ELIAS_LE 1
14+ #define DALI_VARS_SIZE 18
1615
1716//include salvador and rename main
1817#define main salvador_main
@@ -57,7 +56,7 @@ typedef struct ctx {
5756 int sfx_small ;
5857 int sfx_effect ;
5958 int sfx_size ;
60- char * sfx_code ;
59+ unsigned char * sfx_code ;
6160 int lz_bits ;
6261
6362 int exit_on_warn ;
@@ -310,8 +309,32 @@ void reencode_packed_stream(ctx* ctx) {
310309 }
311310}
312311
312+ unsigned int get_var (ctx * ctx , unsigned int pos ) {
313+ return ctx -> sfx_code [pos ] + (ctx -> sfx_code [pos + 1 ] << 8 );
314+ }
315+
313316void write_reencoded_stream (ctx * ctx ) {
314317 FILE * fp = NULL ;
318+ unsigned int dali_sfx_src ;
319+ unsigned int dali_src ;
320+ unsigned int dali_dst ;
321+ unsigned int dali_sfx_addr ;
322+ unsigned int dali_data_end ;
323+ unsigned int dali_data_size_hi ;
324+ unsigned int dali_01 ;
325+ unsigned int dali_cli ;
326+ //int dali_effect_code;
327+
328+ unsigned int var_dali_sfx_src ;
329+ unsigned int var_dali_src ;
330+ unsigned int var_dali_dst ;
331+ unsigned int var_dali_sfx_addr ;
332+ unsigned int var_dali_data_end ;
333+ unsigned int var_dali_data_size_hi ;
334+ unsigned int var_dali_01 ;
335+ unsigned int var_dali_cli ;
336+ //unsigned int var_dali_effect_code;
337+
315338 /* write reencoded output file */
316339 fp = fopen (ctx -> output_name , "wb" );
317340 if (!fp ) {
@@ -323,98 +346,98 @@ void write_reencoded_stream(ctx* ctx) {
323346 if (ctx -> sfx ) {
324347 printf ("Creating sfx with start-address $%04x\n" , ctx -> sfx_addr );
325348 if (ctx -> sfx_small ) {
326- ctx -> sfx_size = sizeof (decruncher_small );
327- /* copy over to change values in code */
328- ctx -> sfx_code = (char * )malloc (ctx -> sfx_size );
329- memcpy (ctx -> sfx_code , decruncher_small , ctx -> sfx_size );
330-
331- /* setup jmp target after decompression */
332- ctx -> sfx_code [DALI_SMALL_SFX_ADDR + 0 ] = ctx -> sfx_addr & 0xff ;
333- ctx -> sfx_code [DALI_SMALL_SFX_ADDR + 1 ] = (ctx -> sfx_addr >> 8 ) & 0xff ;
334-
335- /* setup decompression destination */
336- ctx -> sfx_code [DALI_SMALL_DST + 0 ] = ctx -> cbm_orig_addr & 0xff ;
337- ctx -> sfx_code [DALI_SMALL_DST + 1 ] = (ctx -> cbm_orig_addr >> 8 ) & 0xff ;
338-
339- /* setup compressed data src */
340- ctx -> sfx_code [DALI_SMALL_SRC + 0 ] = (0x10000 - ctx -> reencoded_index ) & 0xff ;
341- ctx -> sfx_code [DALI_SMALL_SRC + 1 ] = ((0x10000 - ctx -> reencoded_index ) >> 8 ) & 0xff ;
342-
343- /* setup compressed data end */
344- ctx -> sfx_code [DALI_SMALL_DATA_END + 0 ] = (0x801 + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 ) & 0xff ;
345- ctx -> sfx_code [DALI_SMALL_DATA_END + 1 ] = ((0x801 + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 ) >> 8 ) & 0xff ;
346-
347- ctx -> sfx_code [DALI_SMALL_DATA_SIZE_HI ] = 0xff - (((ctx -> reencoded_index + 0x100 ) >> 8 ) & 0xff );
348- } else if (ctx -> sfx_effect ) {
349- ctx -> sfx_size = sizeof (decruncher_effect );
350- /* copy over to change values in code */
351- ctx -> sfx_code = (char * )malloc (ctx -> sfx_size );
352- memcpy (ctx -> sfx_code , decruncher_effect , ctx -> sfx_size );
353-
354- /* setup jmp target after decompression */
355- ctx -> sfx_code [DALI_EFFECT_SFX_ADDR + 0 ] = ctx -> sfx_addr & 0xff ;
356- ctx -> sfx_code [DALI_EFFECT_SFX_ADDR + 1 ] = (ctx -> sfx_addr >> 8 ) & 0xff ;
357-
358- /* setup decompression destination */
359- ctx -> sfx_code [DALI_EFFECT_DST + 0 ] = ctx -> cbm_orig_addr & 0xff ;
360- ctx -> sfx_code [DALI_EFFECT_DST + 1 ] = (ctx -> cbm_orig_addr >> 8 ) & 0xff ;
361-
362- /* setup compressed data src */
363- ctx -> sfx_code [DALI_EFFECT_SRC + 0 ] = (0x10000 - ctx -> reencoded_index ) & 0xff ;
364- ctx -> sfx_code [DALI_EFFECT_SRC + 1 ] = ((0x10000 - ctx -> reencoded_index ) >> 8 ) & 0xff ;
365-
366- /* setup compressed data end */
367- ctx -> sfx_code [DALI_EFFECT_DATA_END + 0 ] = (0x801 + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 ) & 0xff ;
368- ctx -> sfx_code [DALI_EFFECT_DATA_END + 1 ] = ((0x801 + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 ) >> 8 ) & 0xff ;
369-
370- ctx -> sfx_code [DALI_EFFECT_DATA_SIZE_HI ] = 0xff - (((ctx -> reencoded_index + 0x100 ) >> 8 ) & 0xff );
349+ if (ctx -> sfx_effect ) {
350+ ctx -> sfx_size = sizeof (decruncher_small_effect );
351+ /* copy over to change values in code */
352+ ctx -> sfx_code = (unsigned char * )malloc (ctx -> sfx_size );
353+ memcpy (ctx -> sfx_code , decruncher_small_effect , ctx -> sfx_size );
354+ } else {
355+ ctx -> sfx_size = sizeof (decruncher_small );
356+ /* copy over to change values in code */
357+ ctx -> sfx_code = (unsigned char * )malloc (ctx -> sfx_size );
358+ memcpy (ctx -> sfx_code , decruncher_small , ctx -> sfx_size );
359+ }
371360 } else {
372- ctx -> sfx_size = sizeof (decruncher );
373- /* copy over to change values in code */
374- ctx -> sfx_code = (char * )malloc (ctx -> sfx_size );
375- memcpy (ctx -> sfx_code , decruncher , ctx -> sfx_size );
361+ if (ctx -> sfx_effect ) {
362+ ctx -> sfx_size = sizeof (decruncher_effect );
363+ /* copy over to change values in code */
364+ ctx -> sfx_code = (unsigned char * )malloc (ctx -> sfx_size );
365+ memcpy (ctx -> sfx_code , decruncher_effect , ctx -> sfx_size );
366+ } else {
367+ ctx -> sfx_size = sizeof (decruncher );
368+ /* copy over to change values in code */
369+ ctx -> sfx_code = (unsigned char * )malloc (ctx -> sfx_size );
370+ memcpy (ctx -> sfx_code , decruncher , ctx -> sfx_size );
371+ }
372+ }
373+ ctx -> sfx_size -= DALI_VARS_SIZE ;
374+
375+ /* fetch vars from binary */
376+
377+ var_dali_sfx_src = ctx -> sfx_size + 0 ;
378+ var_dali_src = ctx -> sfx_size + 2 ;
379+ var_dali_dst = ctx -> sfx_size + 4 ;
380+ var_dali_sfx_addr = ctx -> sfx_size + 6 ;
381+ var_dali_data_end = ctx -> sfx_size + 8 ;
382+ var_dali_data_size_hi = ctx -> sfx_size + 10 ;
383+ var_dali_01 = ctx -> sfx_size + 12 ;
384+ var_dali_cli = ctx -> sfx_size + 14 ;
385+ //var_dali_effect_code = ctx->sfx_size + 16;
386+
387+ dali_sfx_src = get_var (ctx , var_dali_sfx_src );
388+ dali_src = get_var (ctx , var_dali_src );
389+ dali_dst = get_var (ctx , var_dali_dst );
390+ dali_sfx_addr = get_var (ctx , var_dali_sfx_addr );
391+ dali_data_end = get_var (ctx , var_dali_data_end );
392+ dali_data_size_hi = get_var (ctx , var_dali_data_size_hi );
393+ dali_01 = get_var (ctx , var_dali_01 );
394+ dali_cli = get_var (ctx , var_dali_cli );
395+ //dali_effect_code = get_var(ctx, var_dali_effect_code);
396+
397+ /* setup jmp target after decompression */
398+ ctx -> sfx_code [dali_sfx_addr + 0 ] = ctx -> sfx_addr & 0xff ;
399+ ctx -> sfx_code [dali_sfx_addr + 1 ] = (ctx -> sfx_addr >> 8 ) & 0xff ;
400+
401+ /* setup decompression destination */
402+ ctx -> sfx_code [dali_dst + 0 ] = ctx -> cbm_orig_addr & 0xff ;
403+ ctx -> sfx_code [dali_dst + 1 ] = (ctx -> cbm_orig_addr >> 8 ) & 0xff ;
404+
405+ /* setup compressed data src */
406+ ctx -> sfx_code [dali_src + 0 ] = (0x10000 - ctx -> reencoded_index ) & 0xff ;
407+ ctx -> sfx_code [dali_src + 1 ] = ((0x10000 - ctx -> reencoded_index ) >> 8 ) & 0xff ;
408+
409+ /* setup compressed data end */
410+ ctx -> sfx_code [dali_data_end + 0 ] = (0x801 + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 ) & 0xff ;
411+ ctx -> sfx_code [dali_data_end + 1 ] = ((0x801 + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 ) >> 8 ) & 0xff ;
412+
413+ ctx -> sfx_code [dali_data_size_hi ] = 0xff - (((ctx -> reencoded_index + 0x100 ) >> 8 ) & 0xff );
376414
415+ if (ctx -> sfx_small ) {
416+ if (ctx -> sfx_effect ) {
417+ } else {
418+ }
419+ } else {
377420 if (ctx -> sfx_01 < 0 ) ctx -> sfx_01 = 0x37 ;
378-
379- /* setup jmp target after decompression */
380- ctx -> sfx_code [DALI_FAST_SFX_ADDR + 0 ] = ctx -> sfx_addr & 0xff ;
381- ctx -> sfx_code [DALI_FAST_SFX_ADDR + 1 ] = (ctx -> sfx_addr >> 8 ) & 0xff ;
382-
383- /* setup decompression destination */
384- ctx -> sfx_code [DALI_FAST_DST + 0 ] = ctx -> cbm_orig_addr & 0xff ;
385- ctx -> sfx_code [DALI_FAST_DST + 1 ] = (ctx -> cbm_orig_addr >> 8 ) & 0xff ;
386-
387- /* setup compressed data src */
388- ctx -> sfx_code [DALI_FAST_SRC + 0 ] = (0x10000 - ctx -> reencoded_index ) & 0xff ;
389- ctx -> sfx_code [DALI_FAST_SRC + 1 ] = ((0x10000 - ctx -> reencoded_index ) >> 8 ) & 0xff ;
390-
391- /* setup compressed data end */
392- ctx -> sfx_code [DALI_FAST_DATA_END + 0 ] = (0x801 + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 ) & 0xff ;
393- ctx -> sfx_code [DALI_FAST_DATA_END + 1 ] = ((0x801 + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 ) >> 8 ) & 0xff ;
394-
395- ctx -> sfx_code [DALI_FAST_DATA_SIZE_HI ] = 0xff - (((ctx -> reencoded_index + 0x100 ) >> 8 ) & 0xff );
396-
397- ctx -> sfx_code [DALI_FAST_01 ] = ctx -> sfx_01 ;
398- if (ctx -> sfx_cli ) ctx -> sfx_code [DALI_FAST_CLI ] = 0x58 ;
421+ ctx -> sfx_code [dali_01 ] = ctx -> sfx_01 ;
422+ if (ctx -> sfx_cli ) ctx -> sfx_code [dali_cli ] = 0x58 ;
423+ if (ctx -> sfx_effect ) {
424+ } else {
425+ }
399426 }
427+
400428 printf ("original: $%04x-$%04x ($%04x) 100%%\n" , (int )ctx -> cbm_orig_addr , (int )ctx -> cbm_orig_addr + (int )ctx -> unpacked_size , (int )ctx -> unpacked_size );
401429 if (ctx -> cbm_relocate_sfx_addr >= 0 ) {
402430 printf ("packed: $%04x-$%04x ($%04x) %3.2f%%\n" , ctx -> cbm_relocate_sfx_addr , ctx -> cbm_relocate_sfx_addr + (int )ctx -> sfx_size + (int )ctx -> packed_index , (int )ctx -> sfx_size + (int )ctx -> packed_index , ((float )(ctx -> packed_index + (int )ctx -> sfx_size ) / (float )(ctx -> unpacked_size ) * 100.0 ));
403431 if (ctx -> sfx_small ) {
404- ctx -> sfx_code [DALI_SMALL_SFX_SRC + 0 ] = (ctx -> cbm_relocate_sfx_addr + 0xd ) & 255 ;
405- ctx -> sfx_code [DALI_SMALL_SFX_SRC + 1 ] = (ctx -> cbm_relocate_sfx_addr + 0xd ) >> 8 ;
406- ctx -> sfx_code [DALI_SMALL_DATA_END + 0 ] = (ctx -> cbm_relocate_sfx_addr + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 - 0x0c ) & 0xff ;
407- ctx -> sfx_code [DALI_SMALL_DATA_END + 1 ] = ((ctx -> cbm_relocate_sfx_addr + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 - 0x0c ) >> 8 ) & 0xff ;
408- } else if (ctx -> sfx_effect ) {
409- ctx -> sfx_code [DALI_EFFECT_SFX_SRC + 0 ] = (ctx -> cbm_relocate_sfx_addr + 0xd ) & 255 ;
410- ctx -> sfx_code [DALI_EFFECT_SFX_SRC + 1 ] = (ctx -> cbm_relocate_sfx_addr + 0xd ) >> 8 ;
411- ctx -> sfx_code [DALI_EFFECT_DATA_END + 0 ] = (ctx -> cbm_relocate_sfx_addr + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 - 0x0c ) & 0xff ;
412- ctx -> sfx_code [DALI_EFFECT_DATA_END + 1 ] = ((ctx -> cbm_relocate_sfx_addr + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 - 0x0c ) >> 8 ) & 0xff ;
432+ ctx -> sfx_code [dali_sfx_src + 0 ] = (ctx -> cbm_relocate_sfx_addr + 0xd ) & 255 ;
433+ ctx -> sfx_code [dali_sfx_src + 1 ] = (ctx -> cbm_relocate_sfx_addr + 0xd ) >> 8 ;
434+ ctx -> sfx_code [dali_data_end + 0 ] = (ctx -> cbm_relocate_sfx_addr + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 - 0x0c ) & 0xff ;
435+ ctx -> sfx_code [dali_data_end + 1 ] = ((ctx -> cbm_relocate_sfx_addr + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 - 0x0c ) >> 8 ) & 0xff ;
413436 } else {
414- ctx -> sfx_code [DALI_FAST_SFX_SRC + 0 ] = (ctx -> cbm_relocate_sfx_addr + 0x13 ) & 255 ;
415- ctx -> sfx_code [DALI_FAST_SFX_SRC + 1 ] = (ctx -> cbm_relocate_sfx_addr + 0x13 ) >> 8 ;
416- ctx -> sfx_code [DALI_FAST_DATA_END + 0 ] = (ctx -> cbm_relocate_sfx_addr + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 - 0x0c ) & 0xff ;
417- ctx -> sfx_code [DALI_FAST_DATA_END + 1 ] = ((ctx -> cbm_relocate_sfx_addr + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 - 0x0c ) >> 8 ) & 0xff ;
437+ ctx -> sfx_code [dali_sfx_src + 0 ] = (ctx -> cbm_relocate_sfx_addr + 0x13 ) & 255 ;
438+ ctx -> sfx_code [dali_sfx_src + 1 ] = (ctx -> cbm_relocate_sfx_addr + 0x13 ) >> 8 ;
439+ ctx -> sfx_code [dali_data_end + 0 ] = (ctx -> cbm_relocate_sfx_addr + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 - 0x0c ) & 0xff ;
440+ ctx -> sfx_code [dali_data_end + 1 ] = ((ctx -> cbm_relocate_sfx_addr + ctx -> sfx_size - 2 + ctx -> reencoded_index - 0x100 - 0x0c ) >> 8 ) & 0xff ;
418441 }
419442 fputc (ctx -> cbm_relocate_sfx_addr & 255 , fp );
420443 fputc (ctx -> cbm_relocate_sfx_addr >> 8 , fp );
@@ -766,7 +789,7 @@ int main(int argc, char *argv[]) {
766789 }
767790 }
768791
769- printf ("dali v0.3.4 - a zx0-reencoder for bitfire by Tobias Bindhammer\n" );
792+ printf ("dali v0.3.5 - a zx0-reencoder for bitfire by Tobias Bindhammer\n" );
770793 printf ("underlying zx0-packer salvador by Emmanuel Marty\n" );
771794
772795 if (argc == 1 ) {
@@ -776,7 +799,7 @@ int main(int argc, char *argv[]) {
776799 " --01 [num] Set 01 to [num] after sfx.\n"
777800 " --cli [num] Do a CLI after sfx, default is SEI.\n"
778801 " --small Use a very small depacker that fits into zeropage, but --01 and --cli are ignored and it trashes zeropage (!)\n"
779- " --effect Use a very small depacker that fits into zeropage, but --01 and --cli are ignored and it trashes zeropage (!), a decrunch effect is applied\n"
802+ " --effect A decrunch effect is applied\n"
780803 " --no-inplace Disable inplace-decompression.\n"
781804 " --binfile Input file is a raw binary without load-address.\n"
782805 " --from [num] Compress file from [num] on.\n"
0 commit comments