chore: rearrange ROM routines by logical order
This commit is contained in:
308
src/app/rom.cc
308
src/app/rom.cc
@@ -107,31 +107,142 @@ void PrintCompressionChain(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge consecutive copy if possible
|
void CheckByteRepeat(const uchar* rom_data, DataSizeArray& data_size_taken,
|
||||||
std::shared_ptr<CompressionPiece> MergeCopy(
|
CommandArgumentArray& cmd_args, uint& src_data_pos,
|
||||||
std::shared_ptr<CompressionPiece>& start) {
|
const uint last_pos) {
|
||||||
std::shared_ptr<CompressionPiece> piece = start;
|
uint pos = src_data_pos;
|
||||||
|
char byte_to_repeat = rom_data[pos];
|
||||||
while (piece != nullptr) {
|
while (pos <= last_pos && rom_data[pos] == byte_to_repeat) {
|
||||||
if (piece->command == kCommandDirectCopy && piece->next != nullptr &&
|
data_size_taken[kCommandByteFill]++;
|
||||||
piece->next->command == kCommandDirectCopy &&
|
pos++;
|
||||||
piece->length + piece->next->length <= kMaxLengthCompression) {
|
}
|
||||||
uint previous_length = piece->length;
|
cmd_args[kCommandByteFill][0] = byte_to_repeat;
|
||||||
piece->length = piece->length + piece->next->length;
|
|
||||||
|
|
||||||
for (int i = 0; i < piece->next->argument_length; ++i) {
|
|
||||||
piece->argument[i + previous_length] = piece->next->argument[i];
|
|
||||||
}
|
}
|
||||||
piece->argument_length = piece->length;
|
|
||||||
PrintCompressionPiece(piece);
|
|
||||||
|
|
||||||
auto p_next_next = piece->next->next;
|
void CheckWordRepeat(const uchar* rom_data, DataSizeArray& data_size_taken,
|
||||||
piece->next = p_next_next;
|
CommandArgumentArray& cmd_args, uint& src_data_pos,
|
||||||
continue; // Next could be another copy
|
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;
|
||||||
|
char byte1 = rom_data[pos];
|
||||||
|
char byte2 = rom_data[pos + 1];
|
||||||
|
pos += 2;
|
||||||
|
data_size_taken[kCommandWordFill] = 2;
|
||||||
|
while (pos + 1 <= last_pos) {
|
||||||
|
if (rom_data[pos] == byte1 && rom_data[pos + 1] == byte2)
|
||||||
|
data_size_taken[kCommandWordFill] += 2;
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
pos += 2;
|
||||||
}
|
}
|
||||||
piece = piece->next;
|
cmd_args[kCommandWordFill][0] = byte1;
|
||||||
|
cmd_args[kCommandWordFill][1] = byte2;
|
||||||
}
|
}
|
||||||
return start;
|
}
|
||||||
|
|
||||||
|
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++;
|
||||||
|
data_size_taken[kCommandIncreasingFill] = 1;
|
||||||
|
byte++;
|
||||||
|
while (pos <= last_pos && byte == rom_data[pos]) {
|
||||||
|
data_size_taken[kCommandIncreasingFill]++;
|
||||||
|
byte++;
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
cmd_args[kCommandIncreasingFill][0] = rom_data[src_data_pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
uint copied_size = 0;
|
||||||
|
uint search_start = start;
|
||||||
|
|
||||||
|
while (searching_pos < src_data_pos && current_pos_u <= last_pos) {
|
||||||
|
while (rom_data[current_pos_u] != rom_data[searching_pos] &&
|
||||||
|
searching_pos < src_data_pos)
|
||||||
|
searching_pos++;
|
||||||
|
search_start = searching_pos;
|
||||||
|
while (current_pos_u <= last_pos &&
|
||||||
|
rom_data[current_pos_u] == rom_data[searching_pos] &&
|
||||||
|
searching_pos < src_data_pos) {
|
||||||
|
copied_size++;
|
||||||
|
current_pos_u++;
|
||||||
|
searching_pos++;
|
||||||
|
}
|
||||||
|
if (copied_size > data_size_taken[kCommandRepeatingBytes]) {
|
||||||
|
search_start -= start;
|
||||||
|
printf("- Found repeat of %d at %d\n", copied_size, search_start);
|
||||||
|
data_size_taken[kCommandRepeatingBytes] = copied_size;
|
||||||
|
cmd_args[kCommandRepeatingBytes][0] = search_start & SNES_BYTE_MAX;
|
||||||
|
cmd_args[kCommandRepeatingBytes][1] = search_start >> 8;
|
||||||
|
}
|
||||||
|
current_pos_u = src_data_pos;
|
||||||
|
copied_size = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if a command managed to pick up `max_win` or more bytes
|
||||||
|
// Avoids being even with copy command, since it's possible to merge copy
|
||||||
|
void ValidateForByteGain(const DataSizeArray& data_size_taken,
|
||||||
|
const CommandSizeArray& cmd_size, uint& max_win,
|
||||||
|
uint& cmd_with_max) {
|
||||||
|
for (uint cmd_i = 1; cmd_i < 5; cmd_i++) {
|
||||||
|
uint cmd_size_taken = data_size_taken[cmd_i];
|
||||||
|
// TODO(@scawful): Replace conditional with table of command sizes
|
||||||
|
// "Table that is even with copy but all other cmd are 2"
|
||||||
|
auto table_check =
|
||||||
|
!(cmd_i == kCommandRepeatingBytes && cmd_size_taken == 3);
|
||||||
|
if (cmd_size_taken > max_win && cmd_size_taken > cmd_size[cmd_i] &&
|
||||||
|
table_check) {
|
||||||
|
printf("==> C:%d / S:%d\n", cmd_i, cmd_size_taken);
|
||||||
|
cmd_with_max = cmd_i;
|
||||||
|
max_win = cmd_size_taken;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CompressionCommandAlternative(
|
||||||
|
const uchar* rom_data, std::shared_ptr<CompressionPiece>& compressed_chain,
|
||||||
|
const CommandSizeArray& cmd_size, const CommandArgumentArray& cmd_args,
|
||||||
|
uint& src_data_pos, uint& comp_accumulator, uint& cmd_with_max,
|
||||||
|
uint& max_win) {
|
||||||
|
printf("- Ok we get a gain from %d\n", cmd_with_max);
|
||||||
|
std::string buffer;
|
||||||
|
buffer.push_back(cmd_args[cmd_with_max][0]);
|
||||||
|
if (cmd_size[cmd_with_max] == 2) {
|
||||||
|
buffer.push_back(cmd_args[cmd_with_max][1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto new_comp_piece = std::make_shared<CompressionPiece>(
|
||||||
|
cmd_with_max, max_win, buffer, cmd_size[cmd_with_max]);
|
||||||
|
PrintCompressionPiece(new_comp_piece);
|
||||||
|
// If we let non compressed stuff, we need to add a copy chunk before
|
||||||
|
if (comp_accumulator != 0) {
|
||||||
|
std::string copy_buff;
|
||||||
|
copy_buff.resize(comp_accumulator);
|
||||||
|
for (int i = 0; i < comp_accumulator; ++i) {
|
||||||
|
copy_buff[i] = rom_data[i + src_data_pos - comp_accumulator];
|
||||||
|
}
|
||||||
|
auto copy_chunk = std::make_shared<CompressionPiece>(
|
||||||
|
kCommandDirectCopy, comp_accumulator, copy_buff, comp_accumulator);
|
||||||
|
compressed_chain->next = copy_chunk;
|
||||||
|
compressed_chain = copy_chunk;
|
||||||
|
} else {
|
||||||
|
compressed_chain->next = new_comp_piece;
|
||||||
|
compressed_chain = new_comp_piece;
|
||||||
|
}
|
||||||
|
src_data_pos += max_win;
|
||||||
|
comp_accumulator = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
absl::StatusOr<std::shared_ptr<CompressionPiece>> SplitCompressionPiece(
|
absl::StatusOr<std::shared_ptr<CompressionPiece>> SplitCompressionPiece(
|
||||||
@@ -245,110 +356,6 @@ Bytes CreateCompressionString(std::shared_ptr<CompressionPiece>& start,
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckByteRepeat(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_to_repeat = rom_data[pos];
|
|
||||||
while (pos <= last_pos && rom_data[pos] == byte_to_repeat) {
|
|
||||||
data_size_taken[kCommandByteFill]++;
|
|
||||||
pos++;
|
|
||||||
}
|
|
||||||
cmd_args[kCommandByteFill][0] = byte_to_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;
|
|
||||||
char byte1 = rom_data[pos];
|
|
||||||
char byte2 = rom_data[pos + 1];
|
|
||||||
pos += 2;
|
|
||||||
data_size_taken[kCommandWordFill] = 2;
|
|
||||||
while (pos + 1 <= last_pos) {
|
|
||||||
if (rom_data[pos] == byte1 && rom_data[pos + 1] == byte2)
|
|
||||||
data_size_taken[kCommandWordFill] += 2;
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
pos += 2;
|
|
||||||
}
|
|
||||||
cmd_args[kCommandWordFill][0] = byte1;
|
|
||||||
cmd_args[kCommandWordFill][1] = byte2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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++;
|
|
||||||
data_size_taken[kCommandIncreasingFill] = 1;
|
|
||||||
byte++;
|
|
||||||
while (pos <= last_pos && byte == rom_data[pos]) {
|
|
||||||
data_size_taken[kCommandIncreasingFill]++;
|
|
||||||
byte++;
|
|
||||||
pos++;
|
|
||||||
}
|
|
||||||
cmd_args[kCommandIncreasingFill][0] = rom_data[src_data_pos];
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
uint copied_size = 0;
|
|
||||||
uint search_start = start;
|
|
||||||
|
|
||||||
while (searching_pos < src_data_pos && current_pos_u <= last_pos) {
|
|
||||||
while (rom_data[current_pos_u] != rom_data[searching_pos] &&
|
|
||||||
searching_pos < src_data_pos)
|
|
||||||
searching_pos++;
|
|
||||||
search_start = searching_pos;
|
|
||||||
while (current_pos_u <= last_pos &&
|
|
||||||
rom_data[current_pos_u] == rom_data[searching_pos] &&
|
|
||||||
searching_pos < src_data_pos) {
|
|
||||||
copied_size++;
|
|
||||||
current_pos_u++;
|
|
||||||
searching_pos++;
|
|
||||||
}
|
|
||||||
if (copied_size > data_size_taken[kCommandRepeatingBytes]) {
|
|
||||||
search_start -= start;
|
|
||||||
printf("- Found repeat of %d at %d\n", copied_size, search_start);
|
|
||||||
data_size_taken[kCommandRepeatingBytes] = copied_size;
|
|
||||||
cmd_args[kCommandRepeatingBytes][0] = search_start & SNES_BYTE_MAX;
|
|
||||||
cmd_args[kCommandRepeatingBytes][1] = search_start >> 8;
|
|
||||||
}
|
|
||||||
current_pos_u = src_data_pos;
|
|
||||||
copied_size = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if a command managed to pick up `max_win` or more bytes
|
|
||||||
// Avoids being even with copy command, since it's possible to merge copy
|
|
||||||
void ValidateForByteGain(const DataSizeArray& data_size_taken,
|
|
||||||
const CommandSizeArray& cmd_size, uint& max_win,
|
|
||||||
uint& cmd_with_max) {
|
|
||||||
for (uint cmd_i = 1; cmd_i < 5; cmd_i++) {
|
|
||||||
uint cmd_size_taken = data_size_taken[cmd_i];
|
|
||||||
// TODO(@scawful): Replace conditional with table of command sizes
|
|
||||||
// "Table that is even with copy but all other cmd are 2"
|
|
||||||
auto table_check =
|
|
||||||
!(cmd_i == kCommandRepeatingBytes && cmd_size_taken == 3);
|
|
||||||
if (cmd_size_taken > max_win && cmd_size_taken > cmd_size[cmd_i] &&
|
|
||||||
table_check) {
|
|
||||||
printf("==> C:%d / S:%d\n", cmd_i, cmd_size_taken);
|
|
||||||
cmd_with_max = cmd_i;
|
|
||||||
max_win = cmd_size_taken;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
absl::Status ValidateCompressionResult(
|
absl::Status ValidateCompressionResult(
|
||||||
std::shared_ptr<CompressionPiece>& compressed_chain_start, int mode,
|
std::shared_ptr<CompressionPiece>& compressed_chain_start, int mode,
|
||||||
int start, int src_data_pos) {
|
int start, int src_data_pos) {
|
||||||
@@ -375,38 +382,31 @@ absl::Status ValidateCompressionResult(
|
|||||||
return absl::OkStatus();
|
return absl::OkStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CompressionCommandAlternative(
|
// Merge consecutive copy if possible
|
||||||
const uchar* rom_data, std::shared_ptr<CompressionPiece>& compressed_chain,
|
std::shared_ptr<CompressionPiece> MergeCopy(
|
||||||
const CommandSizeArray& cmd_size, const CommandArgumentArray& cmd_args,
|
std::shared_ptr<CompressionPiece>& start) {
|
||||||
uint& src_data_pos, uint& comp_accumulator, uint& cmd_with_max,
|
std::shared_ptr<CompressionPiece> piece = start;
|
||||||
uint& max_win) {
|
|
||||||
printf("- Ok we get a gain from %d\n", cmd_with_max);
|
|
||||||
std::string buffer;
|
|
||||||
buffer.push_back(cmd_args[cmd_with_max][0]);
|
|
||||||
if (cmd_size[cmd_with_max] == 2) {
|
|
||||||
buffer.push_back(cmd_args[cmd_with_max][1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto new_comp_piece = std::make_shared<CompressionPiece>(
|
while (piece != nullptr) {
|
||||||
cmd_with_max, max_win, buffer, cmd_size[cmd_with_max]);
|
if (piece->command == kCommandDirectCopy && piece->next != nullptr &&
|
||||||
PrintCompressionPiece(new_comp_piece);
|
piece->next->command == kCommandDirectCopy &&
|
||||||
// If we let non compressed stuff, we need to add a copy chunk before
|
piece->length + piece->next->length <= kMaxLengthCompression) {
|
||||||
if (comp_accumulator != 0) {
|
uint previous_length = piece->length;
|
||||||
std::string copy_buff;
|
piece->length = piece->length + piece->next->length;
|
||||||
copy_buff.resize(comp_accumulator);
|
|
||||||
for (int i = 0; i < comp_accumulator; ++i) {
|
for (int i = 0; i < piece->next->argument_length; ++i) {
|
||||||
copy_buff[i] = rom_data[i + src_data_pos - comp_accumulator];
|
piece->argument[i + previous_length] = piece->next->argument[i];
|
||||||
}
|
}
|
||||||
auto copy_chunk = std::make_shared<CompressionPiece>(
|
piece->argument_length = piece->length;
|
||||||
kCommandDirectCopy, comp_accumulator, copy_buff, comp_accumulator);
|
PrintCompressionPiece(piece);
|
||||||
compressed_chain->next = copy_chunk;
|
|
||||||
compressed_chain = copy_chunk;
|
auto p_next_next = piece->next->next;
|
||||||
} else {
|
piece->next = p_next_next;
|
||||||
compressed_chain->next = new_comp_piece;
|
continue; // Next could be another copy
|
||||||
compressed_chain = new_comp_piece;
|
|
||||||
}
|
}
|
||||||
src_data_pos += max_win;
|
piece = piece->next;
|
||||||
comp_accumulator = 0;
|
}
|
||||||
|
return start;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
Reference in New Issue
Block a user