From de271ec3dd9f2473a1cc16eac93d00d80cfa4296 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Tue, 31 Dec 2019 20:05:54 +0100 Subject: [PATCH] Save to C64 PRG autoload format still to do : packbits packing --- src/c64formats.c | 98 +++++++++++++++++++++++++++++++++++++++++ src/c64picview_inc.h | 30 +++++++++++++ src/fileformats.h | 1 + src/loadsave.c | 2 +- src/tests/testformats.c | 2 +- 5 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 src/c64picview_inc.h diff --git a/src/c64formats.c b/src/c64formats.c index 7af60cad..e1a94146 100644 --- a/src/c64formats.c +++ b/src/c64formats.c @@ -1646,6 +1646,104 @@ void Save_C64(T_IO_Context * context) } } +#include "c64picview_inc.h" + +/** + * Pack a stream of nibbles (ignore the high 4 bits) to a file. + * This is designed to pack the color RAM data for VIC-II, as + * the color RAM is only 4 bits. + * + * The output format is a stream of bytes of the following format : + * CD C = (16 - count), D = DATA (4bits) + */ +static int C64_color_ram_pack(FILE * f, const byte * data, int count) +{ + byte previous = 0; + int repeat_count = 0; + while (count-- > 0) + { + if (repeat_count == 0) + { + previous = *data & 0x0f; + repeat_count = 1; + } + else if ((*data & 0x0f) == previous) + { + repeat_count++; + if (repeat_count >= 16) + { + if (!Write_byte(f, previous)) + return 0; + repeat_count = 0; + } + } + else + { + if (!Write_byte(f, ((16 - repeat_count) << 4) | previous)) + return 0; + previous = *data & 0x0f; + repeat_count = 1; + } + data++; + } + if (repeat_count > 0) + { + if (!Write_byte(f, ((16 - repeat_count) << 4) | previous)) + return 0; + } + return 1; +} + +/** + * Save autoloading C64 picture + * + * @todo handle more modes than multicolor + */ +void Save_PRG(T_IO_Context * context) +{ + FILE *file; + byte background = 0; + byte bitmap[8000], screen_ram[1000], color_ram[1000]; + enum c64_format saveFormat = F_invalid; + + if (((context->Width != 320) && (context->Width != 160)) || context->Height != 200) + { + Warning_message("must be 320x200 or 160x200"); + File_error = 1; + return; + } + + saveFormat = (context->Width == 320) ? F_hires : F_multi; + File_error = 1; + Set_saving_layer(context, 0); + File_error = Encode_C64_multicolor(context, bitmap, screen_ram, color_ram, &background); + if (File_error == 0) + { + file = Open_file_write(context); + if (file == NULL) + { + File_error = 2; + return; + } + if (!Write_bytes(file, picview_prg, sizeof(picview_prg))) + File_error = 2; + Write_byte(file, 0x30); // Mode : Bitmap + Multicolor + // TODO : use packbits + Write_byte(file, 0x10); // bitmap / no packing + Write_bytes(file, bitmap, 8000); + Write_byte(file, 0x20); // screen RAM / no packing + Write_bytes(file, screen_ram, 1000); + //Write_byte(file, 0x30); // color RAM / no packing + //Write_bytes(file, color_ram, 1000); + Write_byte(file, 0x32); // color RAM / special color RAM packing + C64_color_ram_pack(file, color_ram, 1000); + Write_byte(file, 0x42); // border/background/etc. / color ram RLE packing + Write_byte(file, background | 0x10); + Write_byte(file, 0); // end of file + fclose(file); + } +} + /////////////////////////// pixcen *.GPX /////////////////////////// void Test_GPX(T_IO_Context * context, FILE * file) diff --git a/src/c64picview_inc.h b/src/c64picview_inc.h new file mode 100644 index 00000000..dc7df1e9 --- /dev/null +++ b/src/c64picview_inc.h @@ -0,0 +1,30 @@ +static unsigned char picview_prg[] = { + 0x01, 0x08, 0x0b, 0x08, 0x77, 0x07, 0x9e, 0x32, 0x30, 0x36, 0x31, 0x00, + 0x00, 0x00, 0xa9, 0x48, 0x85, 0xfb, 0xa9, 0x09, 0x85, 0xfc, 0xa2, 0x10, + 0xbd, 0x1f, 0xd0, 0x9d, 0xef, 0xc3, 0xca, 0xd0, 0xf7, 0xbd, 0x00, 0xd8, + 0x9d, 0x00, 0xc4, 0xbd, 0x00, 0xd9, 0x9d, 0x00, 0xc5, 0xbd, 0x00, 0xda, + 0x9d, 0x00, 0xc6, 0xbd, 0x00, 0xdb, 0x9d, 0x00, 0xc7, 0xe8, 0xd0, 0xe5, + 0x20, 0x21, 0x09, 0x48, 0x20, 0x21, 0x09, 0x48, 0x29, 0x0f, 0xaa, 0xbd, + 0x45, 0x09, 0x8d, 0x68, 0x08, 0x68, 0x29, 0xf0, 0xf0, 0x1c, 0x4a, 0x4a, + 0xaa, 0xbd, 0x31, 0x09, 0x85, 0xfd, 0xbd, 0x32, 0x09, 0x85, 0xfe, 0xbd, + 0x33, 0x09, 0x85, 0x02, 0xbd, 0x34, 0x09, 0xaa, 0x20, 0xfb, 0x08, 0x4c, + 0x3f, 0x08, 0x68, 0xaa, 0x09, 0x1b, 0x8d, 0x11, 0xd0, 0x8a, 0x29, 0x10, + 0x09, 0xc8, 0x8d, 0x16, 0xd0, 0xad, 0x00, 0xdd, 0x48, 0x29, 0xfc, 0x8d, + 0x00, 0xdd, 0xad, 0x18, 0xd0, 0x48, 0xa9, 0x08, 0x8d, 0x18, 0xd0, 0x20, + 0xe4, 0xff, 0xf0, 0xfb, 0xa9, 0x1b, 0x8d, 0x11, 0xd0, 0xa9, 0xc8, 0x8d, + 0x16, 0xd0, 0x68, 0x8d, 0x18, 0xd0, 0x68, 0x8d, 0x00, 0xdd, 0xa2, 0x10, + 0xbd, 0xef, 0xc3, 0x9d, 0x1f, 0xd0, 0xca, 0xd0, 0xf7, 0xbd, 0x00, 0xc4, + 0x9d, 0x00, 0xd8, 0xbd, 0x00, 0xc5, 0x9d, 0x00, 0xd9, 0xbd, 0x00, 0xc6, + 0x9d, 0x00, 0xda, 0xbd, 0x00, 0xc7, 0x9d, 0x00, 0xdb, 0xe8, 0xd0, 0xe5, + 0x60, 0x20, 0x21, 0x09, 0x20, 0x2c, 0x09, 0xc6, 0x02, 0xd0, 0x04, 0xca, + 0x10, 0x01, 0x60, 0x18, 0x69, 0x10, 0xb0, 0xed, 0x90, 0xee, 0x20, 0x21, + 0x09, 0xaa, 0x30, 0x06, 0xe8, 0x20, 0x0d, 0x09, 0xf0, 0xf4, 0xca, 0x10, + 0x30, 0x20, 0x23, 0x09, 0x20, 0x2c, 0x09, 0xe8, 0xd0, 0xfa, 0xf0, 0xe6, + 0xb1, 0xfb, 0x91, 0xfd, 0xc8, 0xd0, 0xf9, 0xe6, 0xfc, 0xe6, 0xfe, 0xca, + 0xd0, 0xf2, 0xa6, 0x02, 0xf0, 0x13, 0xb1, 0xfb, 0x91, 0xfd, 0xe6, 0xfb, + 0xd0, 0x02, 0xe6, 0xfc, 0xe6, 0xfd, 0xd0, 0x02, 0xe6, 0xfe, 0xca, 0xd0, + 0xed, 0x60, 0xa0, 0x00, 0xb1, 0xfb, 0xe6, 0xfb, 0xd0, 0x02, 0xe6, 0xfc, + 0x60, 0x91, 0xfd, 0xe6, 0xfd, 0xd0, 0x02, 0xe6, 0xfe, 0x60, 0x00, 0xe0, + 0x40, 0x1f, 0x00, 0xc0, 0xe8, 0x03, 0x00, 0xd8, 0xe8, 0x03, 0x20, 0xd0, + 0x0f, 0x00, 0xfb, 0xe1, 0xcc +}; diff --git a/src/fileformats.h b/src/fileformats.h index 05031a9a..0498f4a6 100644 --- a/src/fileformats.h +++ b/src/fileformats.h @@ -118,6 +118,7 @@ void Save_C64(T_IO_Context *); void Test_PRG(T_IO_Context *, FILE *); void Load_PRG(T_IO_Context *); +void Save_PRG(T_IO_Context *); // -- GPX (pixcen C64) void Test_GPX(T_IO_Context *, FILE *); diff --git a/src/loadsave.c b/src/loadsave.c index 1c9a3ab1..5fecd051 100644 --- a/src/loadsave.c +++ b/src/loadsave.c @@ -147,7 +147,7 @@ const T_Format File_formats[] = { {FORMAT_GPL, " gpl", Test_GPL, Load_GPL, Save_GPL, 1, 0, 0, "gpl", "gpl"}, {FORMAT_C64, " c64", Test_C64, Load_C64, Save_C64, 0, 1, 1, "c64", "c64;p64;a64;pi;rp;aas;art;dd;iph;ipt;hpc;ocp;koa;koala;fli;bml;cdu;pmg;rpm"}, - {FORMAT_PRG, " prg", Test_PRG, Load_PRG, NULL, 0, 1, 1, "prg", "prg"}, + {FORMAT_PRG, " prg", Test_PRG, Load_PRG, Save_PRG, 0, 0, 1, "prg", "prg"}, {FORMAT_GPX, " gpx", Test_GPX, Load_GPX, NULL, 0, 0, 0, "gpx", "gpx"}, {FORMAT_SCR, " cpc", Test_SCR, Load_SCR, Save_SCR, 0, 0, 0, "scr", "cpc;scr;win"}, {FORMAT_CM5, " cm5", Test_CM5, Load_CM5, Save_CM5, 0, 0, 1, "cm5", "cm5"}, diff --git a/src/tests/testformats.c b/src/tests/testformats.c index 78aa73ad..338a7ccf 100644 --- a/src/tests/testformats.c +++ b/src/tests/testformats.c @@ -74,7 +74,7 @@ static const struct { TESTFMT(BMP, "bmp/test8.bmp") TESTFMTL(ICO, "ico/punzip.ico") // Format with limitations TESTFMTF(C64, "c64/multicolor/ARKANOID.KOA", FLAG_C64) - TESTFMTL(PRG, "c64/multicolor/speedball2_loading_jonegg.prg") + TESTFMTF(PRG, "c64/multicolor/speedball2_loading_jonegg.prg", FLAG_C64) TESTFMTL(GPX, "c64/pixcen/Cyberbird.gpx") TESTFMTF(SCR, "cpc/scr/DANCEOFF.SCR", FLAG_CPCO) TESTFMTL(CM5, "cpc/mode5/spidey.cm5") // Format with limitations