chore: decompose TestAllCommands

This commit is contained in:
Justin Scofield
2022-08-17 10:13:13 -04:00
parent 1f50a6a61a
commit 1bcbd9e1f3

View File

@@ -245,11 +245,9 @@ Bytes CreateCompressionString(std::shared_ptr<CompressionPiece>& start,
return output;
}
// Test every command to see the gain with current position
void TestAllCommands(const uchar* rom_data, DataSizeArray& data_size_taken,
void CheckByteRepeat(const uchar* rom_data, DataSizeArray& data_size_taken,
CommandArgumentArray& cmd_args, uint& src_data_pos,
const uint last_pos, uint start) {
{ // BYTE REPEAT
const uint last_pos) {
uint pos = src_data_pos;
char byte_to_repeat = rom_data[pos];
while (pos <= last_pos && rom_data[pos] == byte_to_repeat) {
@@ -257,8 +255,11 @@ void TestAllCommands(const uchar* rom_data, DataSizeArray& data_size_taken,
pos++;
}
cmd_args[kCommandByteFill][0] = byte_to_repeat;
}
{ // WORD REPEAT
}
void CheckWordRepeat(const uchar* rom_data, DataSizeArray& data_size_taken,
CommandArgumentArray& cmd_args, uint& src_data_pos,
const uint last_pos) {
if (src_data_pos + 2 <= last_pos &&
rom_data[src_data_pos] != rom_data[src_data_pos + 1]) {
uint pos = src_data_pos;
@@ -276,8 +277,11 @@ void TestAllCommands(const uchar* rom_data, DataSizeArray& data_size_taken,
cmd_args[kCommandWordFill][0] = byte1;
cmd_args[kCommandWordFill][1] = byte2;
}
}
{ // INC BYTE
}
void CheckIncByte(const uchar* rom_data, DataSizeArray& data_size_taken,
CommandArgumentArray& cmd_args, uint& src_data_pos,
const uint last_pos) {
uint pos = src_data_pos;
char byte = rom_data[pos];
pos++;
@@ -289,8 +293,11 @@ void TestAllCommands(const uchar* rom_data, DataSizeArray& data_size_taken,
pos++;
}
cmd_args[kCommandIncreasingFill][0] = rom_data[src_data_pos];
}
{ // INTRA CPY
}
void CheckIntraCopy(const uchar* rom_data, DataSizeArray& data_size_taken,
CommandArgumentArray& cmd_args, uint& src_data_pos,
const uint last_pos, uint start) {
if (src_data_pos != start) {
uint searching_pos = start;
uint current_pos_u = src_data_pos;
@@ -320,7 +327,6 @@ void TestAllCommands(const uchar* rom_data, DataSizeArray& data_size_taken,
copied_size = 0;
}
}
}
}
// Check if a command managed to pick up `max_win` or more bytes
@@ -424,7 +430,13 @@ absl::StatusOr<Bytes> ROM::Compress(const int start, const int length, int mode,
data_size_taken.fill({});
cmd_args.fill({{}});
TestAllCommands(rom_data_.data(), data_size_taken, cmd_args, src_data_pos,
CheckByteRepeat(rom_data_.data(), data_size_taken, cmd_args, src_data_pos,
last_pos);
CheckWordRepeat(rom_data_.data(), data_size_taken, cmd_args, src_data_pos,
last_pos);
CheckIncByte(rom_data_.data(), data_size_taken, cmd_args, src_data_pos,
last_pos);
CheckIntraCopy(rom_data_.data(), data_size_taken, cmd_args, src_data_pos,
last_pos, start);
uint max_win = 2;
@@ -512,19 +524,19 @@ absl::StatusOr<Bytes> ROM::Decompress(int offset, int size, bool reversed) {
buffer_pos += length;
offset += length;
break;
case kCommandByteFill: // Advances 1 byte in the ROM
case kCommandByteFill:
for (int i = 0; i < length; i++)
buffer[buffer_pos++] = rom_data_[offset];
offset += 1;
offset += 1; // Advances 1 byte in the ROM
break;
case kCommandWordFill: // Advance 2 byte in the ROM
case kCommandWordFill:
for (int i = 0; i < length; i += 2) {
buffer[buffer_pos] = rom_data_[offset];
buffer_pos++;
buffer[buffer_pos] = rom_data_[offset + 1];
buffer_pos++;
}
offset += 2;
offset += 2; // Advance 2 byte in the ROM
break;
case kCommandIncreasingFill: {
uchar inc_byte = rom_data_[offset];
@@ -541,7 +553,7 @@ absl::StatusOr<Bytes> ROM::Decompress(int offset, int size, bool reversed) {
auto addr = (rom_data_[offset + 2]) | ((rom_data_[offset + 1]) << 8);
if (addr > offset) {
return absl::InternalError(absl::StrFormat(
"DecompressOverworldV2: Offset for command copy exceeds "
"DecompressOverworld: Offset for command copy exceeds "
"current position (Offset : %#04x | Pos : %#06x)\n",
addr, offset));
}