git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

commit 7c424be4cd03b878b5778e6234b294a19ea94d59
parent 0eb91a20849503e83c34f95aa2b9d1fb0e8b9fc7
Author: bbbbbr <bbbbbr@users.noreply.github.com>
Date:   Sun, 26 Jun 2022 00:10:18 -0700

Merge pull request #386 from bbbbbr/gbdecompress_banknum

gbdecompress: add --bank=<num> param, improve error signals
Diffstat:
Agbdk-support/gbcompress/common.h9+++++++++
Mgbdk-support/gbcompress/files_c_source.c48++++++++++++++++++++++++++++++++++++------------
Mgbdk-support/gbcompress/files_c_source.h2+-
Mgbdk-support/gbcompress/main.c28++++++++++++++++++++--------
4 files changed, 66 insertions(+), 21 deletions(-)

diff --git a/gbdk-support/gbcompress/common.h b/gbdk-support/gbcompress/common.h @@ -0,0 +1,9 @@ + +#ifndef _COMMON_H +#define _COMMON_H + +#define BANK_NUM_ROM_UNSET 0 +#define BANK_NUM_ROM_MIN 1 +#define BANK_NUM_ROM_MAX 511 + +#endif // _COMMON_H diff --git a/gbdk-support/gbcompress/files_c_source.c b/gbdk-support/gbcompress/files_c_source.c @@ -5,6 +5,7 @@ #include <string.h> #include <stdlib.h> +#include "common.h" #include "files.h" #define STR_FFWD_MATCH_ANY NULL @@ -43,7 +44,7 @@ static char * str_ffwd_to(char * str_in, uint32_t * max_len, char char_match, ch // signal failure if any non-allowed character was found return NULL; } - } + } str_in++; (*max_len)--; @@ -60,7 +61,7 @@ static char * str_ffwd_to(char * str_in, uint32_t * max_len, char char_match, ch // static uint8_t * str_array_to_buf(char * str_in, uint32_t * p_ret_len) { - uint32_t buf_size = BUF_DEFAULT_SIZE; + uint32_t buf_size = BUF_DEFAULT_SIZE; uint8_t * p_buf = malloc(buf_size); uint8_t * p_buf_last; char * end_ptr; @@ -70,7 +71,7 @@ static uint8_t * str_array_to_buf(char * str_in, uint32_t * p_ret_len) { char * str_cur = strtok(str_in,","); if (p_buf) { - + // Start with size zero *p_ret_len = 0; @@ -114,7 +115,7 @@ static uint8_t * str_array_to_buf(char * str_in, uint32_t * p_ret_len) { // // Returns NULL if reading file didn't succeed // -uint8_t * file_read_c_input_into_buffer(char * filename, uint32_t *ret_size) { +uint8_t * file_read_c_input_into_buffer(char * filename, uint32_t *p_ret_size) { char * filedata = NULL; uint32_t file_size; @@ -123,10 +124,12 @@ uint8_t * file_read_c_input_into_buffer(char * filename, uint32_t *ret_size) { char * str_c_array_start = NULL; uint8_t * p_array_data = NULL; + *p_ret_size = 0; + filedata = file_read_into_buffer_char(filename, &file_size); if (filedata) { - str_c_array = filedata; + str_c_array = filedata; // Find array opening bracket `[` str_c_array = str_ffwd_to(str_c_array, &file_size, '[', STR_FFWD_MATCH_ANY); @@ -139,17 +142,17 @@ uint8_t * file_read_c_input_into_buffer(char * filename, uint32_t *ret_size) { if (str_c_array) { // Save start of array str_c_array_start = str_c_array + 1; - + // Find end of array str_c_array = str_ffwd_to(str_c_array, &file_size, '}', "{xX0123456789ABCDEFabcdef,\t\n\r "); if (str_c_array) { // Terminate string at end of array *str_c_array = '\0'; - // If conversion fails: p_array_data == NULL, ret_size == 0 - p_array_data = str_array_to_buf(str_c_array_start, ret_size); + // If conversion fails: p_array_data == NULL, p_ret_size == 0 + p_array_data = str_array_to_buf(str_c_array_start, p_ret_size); - // printf("C array length in = %d", *ret_size); + // printf("C array length in = %d", *p_ret_size); } } } @@ -158,8 +161,13 @@ uint8_t * file_read_c_input_into_buffer(char * filename, uint32_t *ret_size) { // Free file data if allocated free(filedata); filedata = NULL; + + if (*p_ret_size == 0) { + printf("gbcompress: ERROR: Failed to read any bytes in\n"); + } } + return p_array_data; } @@ -168,7 +176,7 @@ uint8_t * file_read_c_input_into_buffer(char * filename, uint32_t *ret_size) { // Writes a buffer to a file in C source format // Adds a matching .h if possible // -bool file_write_c_output_from_buffer(char * filename, uint8_t * p_buf, uint32_t data_len, char * var_name, bool var_is_const) { +bool file_write_c_output_from_buffer(char * filename, uint8_t * p_buf, uint32_t data_len, char * var_name, bool var_is_const, uint16_t bank_num) { bool status = false; size_t wrote_bytes; @@ -176,6 +184,14 @@ bool file_write_c_output_from_buffer(char * filename, uint8_t * p_buf, uint32_t int i; if (file_out) { + // C Source array output + + // If Bank Num is set add a .h bank ref + if (bank_num != BANK_NUM_ROM_UNSET) { + fprintf(file_out, "#include <gbdk/platform.h>\n\n"); + fprintf(file_out, "#pragma bank %d\n", bank_num); + fprintf(file_out, "BANKREF(%s)\n\n", var_name); + } // Array entry with variable name fprintf(file_out, "\n\n%s unsigned char %s[] = {", (var_is_const) ? "const" : "", var_name); @@ -188,7 +204,7 @@ bool file_write_c_output_from_buffer(char * filename, uint8_t * p_buf, uint32_t // Write current byte fprintf(file_out, "0x%.2X", p_buf[i]); - + // Add comma after every entry except last one if (i != data_len -1) fprintf(file_out, ", "); @@ -203,7 +219,7 @@ bool file_write_c_output_from_buffer(char * filename, uint8_t * p_buf, uint32_t if (strlen(filename) >= 2) { if ((strcmp(&filename[strlen(filename) - 2],".c") == 0) || (strcmp(&filename[strlen(filename) - 2],".C") == 0)) { - + // Replace file extension filename[strlen(filename) - 2] = '.'; filename[strlen(filename) - 1] = 'h'; @@ -212,6 +228,12 @@ bool file_write_c_output_from_buffer(char * filename, uint8_t * p_buf, uint32_t if (file_out) { + // If Bank Num is set add a .h bank ref + if (bank_num != BANK_NUM_ROM_UNSET) { + fprintf(file_out, "#include <gbdk/metasprites.h>\n\n"); + fprintf(file_out, "BANKREF_EXTERN(%s)\n\n", var_name); + } + fprintf(file_out, "\n\n#define %s_sz_comp %d\n", var_name, size_compressed); fprintf(file_out, "#define %s_sz_decomp %d\n", var_name, size_decompressed); @@ -222,6 +244,8 @@ bool file_write_c_output_from_buffer(char * filename, uint8_t * p_buf, uint32_t } } } + } else { + printf("gbcompress: Error: Failed to open output file: %s\n", filename); } return status; diff --git a/gbdk-support/gbcompress/files_c_source.h b/gbdk-support/gbcompress/files_c_source.h @@ -3,7 +3,7 @@ void c_source_set_sizes(uint32_t, uint32_t); -bool file_write_c_output_from_buffer(char *, uint8_t *, uint32_t, char *, bool); +bool file_write_c_output_from_buffer(char *, uint8_t *, uint32_t, char *, bool, uint16_t bank_num); uint8_t * file_read_c_input_into_buffer(char * filename, uint32_t *ret_size); #endif // _FILES_C_SOURCE_H diff --git a/gbdk-support/gbcompress/main.c b/gbdk-support/gbcompress/main.c @@ -8,6 +8,7 @@ #include <stdbool.h> #include <stdint.h> +#include "common.h" #include "gbcompress.h" #include "rlecompress.h" #include "files.h" @@ -31,6 +32,7 @@ bool opt_compression_type = COMPRESSION_TYPE_DEFAULT; bool opt_c_source_input = false; bool opt_c_source_output = false; char opt_c_source_output_varname[MAX_STR_LEN] = "var_name"; +uint16_t opt_bank_num = BANK_NUM_ROM_UNSET; static void display_help(void); static int handle_args(int argc, char * argv[]); @@ -45,19 +47,21 @@ static void display_help(void) { "Use: compress a binary file and write it out.\n" "\n" "Options\n" - "-h : Show this help screen\n" - "-d : Decompress (default is compress)\n" - "-v : Verbose output\n" - "--cin : Read input as .c source format (8 bit char ONLY, uses first array found)\n" - "--cout : Write output in .c / .h source format (8 bit char ONLY) \n" + "-h : Show this help screen\n" + "-d : Decompress (default is compress)\n" + "-v : Verbose output\n" + "--cin : Read input as .c source format (8 bit char ONLY, uses first array found)\n" + "--cout : Write output in .c / .h source format (8 bit char ONLY) \n" "--varname=<NAME> : specify variable name for c source output\n" "--alg=<type> : specify compression type: 'rle', 'gb' (default)\n" + "--bank=<num> : Add Bank Ref: %d - %d (default is none, with --cout only)\n" "Example: \"gbcompress binaryfile.bin compressed.bin\"\n" "Example: \"gbcompress -d compressedfile.bin decompressed.bin\"\n" "Example: \"gbcompress --alg=rle binaryfile.bin compressed.bin\"\n" "\n" "The default compression (gb) is the type used by gbtd/gbmb\n" - "The rle compression is Amiga IFF style\n" + "The rle compression is Amiga IFF style\n", + BANK_NUM_ROM_MIN, BANK_NUM_ROM_MAX ); } @@ -93,6 +97,12 @@ int handle_args(int argc, char * argv[]) { opt_compression_type = COMPRESSION_TYPE_RLE_BLOCK; } else if (strstr(argv[i], "-d") == argv[i]) { opt_mode_compress = false; + } else if (strstr(argv[i], "--bank=") == argv[i]) { + opt_bank_num = atoi(argv[i] + strlen("--bank=")); + if ((opt_bank_num < BANK_NUM_ROM_MIN) || (opt_bank_num > BANK_NUM_ROM_MAX)) { + printf("gbcompress: Warning: Bank Num %d outside of range %d - %d\n", opt_bank_num, BANK_NUM_ROM_MIN, BANK_NUM_ROM_MAX); + return false; + } } else printf("gbcompress: Warning: Ignoring unknown option %s\n", argv[i]); } @@ -156,7 +166,7 @@ static int compress() { if (opt_c_source_output) { c_source_set_sizes(out_len, buf_size_in); // compressed, decompressed - result = file_write_c_output_from_buffer(filename_out, p_buf_out, out_len, opt_c_source_output_varname, true); + result = file_write_c_output_from_buffer(filename_out, p_buf_out, out_len, opt_c_source_output_varname, true, opt_bank_num); } else result = file_write_from_buffer(filename_out, p_buf_out, out_len); @@ -203,7 +213,7 @@ static int decompress() { if (opt_c_source_output) { c_source_set_sizes(buf_size_in, out_len); // compressed, decompressed - result = file_write_c_output_from_buffer(filename_out, p_buf_out, out_len, opt_c_source_output_varname, true); + result = file_write_c_output_from_buffer(filename_out, p_buf_out, out_len, opt_c_source_output_varname, true, opt_bank_num); } else { result = file_write_from_buffer(filename_out, p_buf_out, out_len); @@ -214,6 +224,8 @@ static int decompress() { printf("Decompressed: %d bytes -> %d bytes (compression was %%%.2f)\n", buf_size_in, out_len, ((double)buf_size_in / (double)out_len) * 100); return EXIT_SUCCESS; } + } else if (out_len == 0) { + printf("gbcompress: ERROR: Failed to decompress any bytes (from %d input). Data may not be compressed\n", buf_size_in); } }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.