git.y1.nz

gbdk-2020

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

commit e4731e9324e37e82d857c12e041ba19bfb7686c6
parent 008ba83fbf1dc4156dc69a7e33275155e8dd3ba4
Author: bbbbbr <bbbbbr@users.noreply.github.com>
Date:   Tue, 13 Sep 2022 13:22:19 -0700

Merge pull request #415 from bbbbbr/gbcompress_fix_u16_token_reset

gbcompress: fix u16 token
Diffstat:
Mgbdk-support/gbcompress/Makefile13++++++++++++-
Mgbdk-support/gbcompress/gbcompress.c31++++++++++++++++++++++++-------
Agbdk-support/gbcompress/test_data/test_no_u16_align_end_of_buf.c21+++++++++++++++++++++
3 files changed, 57 insertions(+), 8 deletions(-)

diff --git a/gbdk-support/gbcompress/Makefile b/gbdk-support/gbcompress/Makefile @@ -31,6 +31,7 @@ clean: # round trip the executable through compression and de-compression as a brief test test: + # Compression test on the executable itself rm -f tmp.cmp; rm -f tmp.dcmp cp $(BIN) tmp.in; ./gbcompress -v tmp.in tmp.cmp; ./gbcompress -v -d tmp.cmp tmp.dcmp; diff -s tmp.in tmp.dcmp rm -f tmp.cmp.c; rm -f tmp.dcmp.c; rm -f tmp.cmp; rm -f tmp.dcmp @@ -40,4 +41,14 @@ test: cp $(BIN) tmp.in; ./gbcompress --alg=rle -v tmp.in tmp.cmp; ./gbcompress --alg=rle -v -d tmp.cmp tmp.dcmp; diff -s tmp.in tmp.dcmp rm -f tmp.cmp.c; rm -f tmp.dcmp.c; rm -f tmp.cmp; rm -f tmp.dcmp cp $(BIN) tmp.in; ./gbcompress --alg=rle -v --cout --varname=some_array tmp.in tmp.cmp.c; ./gbcompress --alg=rle -v -d --cin tmp.cmp.c tmp.dcmp; diff -s tmp.in tmp.dcmp - rm tmp.* + rm -f tmp.* + # test_no_u16_align_end_of_buf.c + rm -f tmp.* + cp test_data/test_no_u16_align_end_of_buf.c tmp.in.c; ./gbcompress --cin -v --cout tmp.in.c tmp.cmp.c; ./gbcompress -v -d --cin --cout tmp.cmp.c tmp.dcmp.c; diff -s tmp.in.c tmp.dcmp.c + rm -f tmp.* + cp test_data/test_no_u16_align_end_of_buf.c tmp.in.c; ./gbcompress --alg=rle --cin -v --cout tmp.in.c tmp.cmp.c; ./gbcompress --alg=rle -v -d --cin --cout tmp.cmp.c tmp.dcmp.c; diff -s tmp.in.c tmp.dcmp.c + rm -f tmp.* + + + + diff --git a/gbdk-support/gbcompress/gbcompress.c b/gbdk-support/gbcompress/gbcompress.c @@ -54,6 +54,7 @@ static void check_write_size(uint8_t len) { static void write_byte(uint8_t len, uint8_t data) { check_write_size(2); // writing 2 bytes + // printf("* write_byte len: %2d , data: %02x \n", len, data); FoutBuf[FoutIndex++] = ((len - 1) & len_mask); FoutBuf[FoutIndex++] = data; @@ -63,6 +64,7 @@ static void write_byte(uint8_t len, uint8_t data) { static void write_word( uint8_t len, uint16_t data ) { check_write_size(3); // writing 3 bytes + // printf("* write_word len: %2d , data: %02x, %02x \n", len, (uint8_t)((data >> 8) & 0xFF), (uint8_t)(data & 0xFF)); FoutBuf[FoutIndex++] = (((len - 1) & len_mask) | token_word); FoutBuf[FoutIndex++] = (uint8_t)((data >> 8) & 0xFF); @@ -74,8 +76,9 @@ static void write_string( uint8_t len, uint16_t data) { check_write_size(3); // writing 3 bytes - // conver's complement does not give the negation, see § Most negative number below. t back-ref offset from positive unsigned to negative signed + // Convert back-ref offset from positive unsigned to negative signed to match format data = (data ^ 0xFFFF) + 1; + // printf("* write_string len: %2d , data: %02x, %02x \n", len, (uint8_t)((data >> 8) & 0xFF), (uint8_t)(data & 0xFF)); FoutBuf[FoutIndex++] = (((len - 1) & len_mask) | token_str); FoutBuf[FoutIndex++] = (uint8_t)(data & 0xFF); @@ -88,6 +91,10 @@ static void write_trash( uint8_t len, uint8_t * pos) { uint8_t i; check_write_size(len); // writing len bytes + // printf("* write_string len: %2d , data: ", len); + // for (i=0; i < len; i++) + // printf("%02x, ", pos[i]); + // printf("\n"); FoutBuf[FoutIndex++] = (((len-1) & len_mask) | token_trash); for (i=0; i < len; i++) @@ -130,10 +137,10 @@ uint32_t gbcompress_buf(uint8_t * inBuf, uint32_t size_in, uint8_t ** pp_outBuf, uint8_t rle_u8_match; // x uint16_t rle_u16_match; // y - uint32_t rle_u8_len; // r_rb - uint32_t rle_u16_len; // r_rw - uint32_t rle_str_len; // r_rs - uint32_t trash_len; // tb (by "trash" the original author meant, "non-rle sequence of bytes") + uint32_t rle_u8_len = 0; // r_rb + uint32_t rle_u16_len = 0; // r_rw + uint32_t rle_str_len = 0; // r_rs + uint32_t trash_len = 0; // tb (by "trash" the original author meant, "non-rle sequence of bytes") uint32_t rle_str_start; // rr uint32_t rle_str_back_offset; // sr (this is signed in original code, handled differently to be unsigned now) @@ -148,10 +155,10 @@ uint32_t gbcompress_buf(uint8_t * inBuf, uint32_t size_in, uint8_t ** pp_outBuf, Fsize_out = size_out; FoutIndex = 0; - trash_len = 0; - while (FinIndex < Fsize_in) { + // printf("@%3d / %3d = %02x\n", FinIndex, Fsize_in, FinBuf[FinIndex]); + // Check for u8 RLE run up to 63 bytes max rle_u8_match = FinBuf[FinIndex]; rle_u8_len = 1; @@ -177,6 +184,10 @@ uint32_t gbcompress_buf(uint8_t * inBuf, uint32_t size_in, uint8_t ** pp_outBuf, } else break; } + } else { + // If failed to read a u16 worth of data reset u16 token length + // (meaning: near end of buffer and only 1 byte was available) + rle_u16_len = 0; } // Check for matching sequences starting at current position @@ -224,22 +235,26 @@ uint32_t gbcompress_buf(uint8_t * inBuf, uint32_t size_in, uint8_t ** pp_outBuf, if ((rle_u8_len > 2) && (rle_u8_len > rle_u16_len) && (rle_u8_len > rle_str_len)) { + flush_trash(&FinIndex, &trash_len); write_byte(rle_u8_len, rle_u8_match); FinIndex = FinIndex + rle_u8_len; } else if ((rle_u16_len > 2) && ((rle_u16_len*2) > rle_str_len)) { + flush_trash(&FinIndex, &trash_len); write_word(rle_u16_len, rle_u16_match); FinIndex = FinIndex + rle_u16_len*2; } else if (rle_str_len > 3) { + flush_trash(&FinIndex, &trash_len); write_string(rle_str_len, rle_str_back_offset); FinIndex = FinIndex + rle_str_len; } else if (trash_len >= 64) { + write_trash(trash_len, &FinBuf[FinIndex-trash_len]); trash_len = 0; } @@ -250,6 +265,8 @@ uint32_t gbcompress_buf(uint8_t * inBuf, uint32_t size_in, uint8_t ** pp_outBuf, } + // printf("End of compression, flushing unwritten bytes\n"); + // Flush any remaining "trash" bytes flush_trash(&FinIndex, &trash_len); diff --git a/gbdk-support/gbcompress/test_data/test_no_u16_align_end_of_buf.c b/gbdk-support/gbcompress/test_data/test_no_u16_align_end_of_buf.c @@ -0,0 +1,21 @@ + + +const unsigned char var_name[] = { + 0x10, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x15, + 0x07, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x07, + 0x07, 0x30, 0x30, 0x30, 0x30, 0x3F, 0x30, 0xE0, 0x30, 0x3E, 0x30, 0x30, 0x30, 0x30, 0x30, 0x07, + 0x07, 0x30, 0x30, 0x30, 0x30, 0x3F, 0x30, 0x30, 0x30, 0x3E, 0x30, 0x30, 0x30, 0x30, 0x30, 0x07, + 0x07, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3C, 0x3C, 0x3D, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x07, + 0x07, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3C, 0x30, 0x3D, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x07, + 0x07, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3C, 0x30, 0x3D, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x07, + 0x07, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3C, 0x30, 0x3D, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x07, + 0x07, 0x30, 0x3F, 0x3F, 0x3F, 0x3F, 0x3C, 0x30, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3D, 0x30, 0x07, + 0x07, 0x30, 0x3C, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3D, 0x30, 0x07, + 0x07, 0x30, 0x3C, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3D, 0x30, 0x07, + 0x07, 0x30, 0x3C, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3D, 0x30, 0x07, + 0x07, 0x30, 0x3C, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x30, 0x07, + 0x07, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x07, + 0x1C, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x1D, + 0x20, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x21 +}; +

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