ROM housekeeping

This commit is contained in:
Justin Scofield
2022-07-12 21:33:04 -04:00
parent d10a36ad78
commit ea01262232
2 changed files with 31 additions and 20 deletions

View File

@@ -12,6 +12,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "app/core/common.h"
#include "app/core/constants.h" #include "app/core/constants.h"
#include "app/gfx/snes_tile.h" #include "app/gfx/snes_tile.h"
@@ -20,10 +21,10 @@ namespace app {
void ROM::Close() { void ROM::Close() {
if (is_loaded_) { if (is_loaded_) {
SDL_free(current_rom_); delete[] current_rom_;
for (auto i = 0; i < num_sheets_; i++) { for (auto i = 0; i < num_sheets_; i++) {
SDL_free(decompressed_graphic_sheets_[i]); delete[] decompressed_graphic_sheets_[i];
SDL_free(converted_graphic_sheets_[i]); delete[] converted_graphic_sheets_[i];
} }
} }
} }
@@ -34,13 +35,13 @@ void ROM::SetupRenderer(std::shared_ptr<SDL_Renderer> renderer) {
// TODO: check if the rom has a header on load // TODO: check if the rom has a header on load
void ROM::LoadFromFile(const std::string &path) { void ROM::LoadFromFile(const std::string &path) {
size_ = std::filesystem::file_size(path.c_str());
std::ifstream file(path.c_str(), std::ios::binary); std::ifstream file(path.c_str(), std::ios::binary);
if (!file.is_open()) { if (!file.is_open()) {
std::cout << "Error: Could not open ROM file " << path << std::endl; std::cout << "Error: Could not open ROM file " << path << std::endl;
return; return;
} }
current_rom_ = (uchar *)SDL_malloc(size_); size_ = std::filesystem::file_size(path.c_str());
current_rom_ = new uchar[size_];
for (uint i = 0; i < size_; i++) { for (uint i = 0; i < size_; i++) {
char byte_read_ = ' '; char byte_read_ = ' ';
file.read(&byte_read_, sizeof(char)); file.read(&byte_read_, sizeof(char));
@@ -51,8 +52,16 @@ void ROM::LoadFromFile(const std::string &path) {
is_loaded_ = true; is_loaded_ = true;
} }
uchar *ROM::DecompressGraphics(int pos, int size) {
return Decompress(pos, size, false);
}
uchar *ROM::DecompressOverworld(int pos, int size) {
return Decompress(pos, size, true);
}
uchar *ROM::Decompress(int pos, int size, bool reversed) { uchar *ROM::Decompress(int pos, int size, bool reversed) {
auto buffer = (uchar *)SDL_malloc(size); auto buffer = new uchar[size];
uint length = 0; uint length = 0;
uint buffer_pos = 0; uint buffer_pos = 0;
uchar cmd = 0; uchar cmd = 0;
@@ -67,7 +76,7 @@ uchar *ROM::Decompress(int pos, int size, bool reversed) {
length = length =
(ushort)(((current_rom_[pos] << 8) | current_rom_[pos + 1]) & 0x3FF); (ushort)(((current_rom_[pos] << 8) | current_rom_[pos + 1]) & 0x3FF);
pos += 2; // Advance 2 bytes in ROM pos += 2; // Advance 2 bytes in ROM
} else { // Normal Command } else { // Normal Command
cmd = (uchar)((databyte >> 5) & 0x07); cmd = (uchar)((databyte >> 5) & 0x07);
length = (uchar)(databyte & 0x1F); length = (uchar)(databyte & 0x1F);
pos += 1; // Advance 1 byte in ROM pos += 1; // Advance 1 byte in ROM
@@ -96,9 +105,9 @@ uchar *ROM::Decompress(int pos, int size, bool reversed) {
break; break;
case 03: // Increasing Fill case 03: // Increasing Fill
{ {
uchar incByte = current_rom_[pos]; uchar inc_byte = current_rom_[pos];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
buffer[buffer_pos++] = incByte++; buffer[buffer_pos++] = inc_byte++;
} }
pos += 1; // Advance 1 byte in the ROM pos += 1; // Advance 1 byte in the ROM
} break; } break;
@@ -106,12 +115,12 @@ uchar *ROM::Decompress(int pos, int size, bool reversed) {
{ {
ushort s1 = ((current_rom_[pos + 1] & 0xFF) << 8); ushort s1 = ((current_rom_[pos + 1] & 0xFF) << 8);
ushort s2 = ((current_rom_[pos] & 0xFF)); ushort s2 = ((current_rom_[pos] & 0xFF));
auto Addr = (ushort)(s1 | s2); auto addr = (ushort)(s1 | s2);
if (reversed) { if (reversed) {
Addr = (ushort)(s2 | s1); addr = (ushort)(s2 | s1);
} }
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
buffer[buffer_pos] = buffer[Addr + i]; buffer[buffer_pos] = buffer[addr + i];
buffer_pos++; buffer_pos++;
} }
pos += 2; // Advance 2 bytes in the ROM pos += 2; // Advance 2 bytes in the ROM
@@ -126,7 +135,7 @@ uchar *ROM::Decompress(int pos, int size, bool reversed) {
// 128x32 // 128x32
uchar *ROM::SNES3bppTo8bppSheet(uchar *buffer_in, int sheet_id, int size) { uchar *ROM::SNES3bppTo8bppSheet(uchar *buffer_in, int sheet_id, int size) {
// 8bpp sheet out // 8bpp sheet out
auto sheet_buffer_out = (uchar *)SDL_malloc(size); auto sheet_buffer_out = new uchar[size];
int xx = 0; // positions where we are at on the sheet int xx = 0; // positions where we are at on the sheet
int yy = 0; int yy = 0;
int pos = 0; int pos = 0;
@@ -213,14 +222,14 @@ SDL_Texture *ROM::DrawGraphicsSheet(int offset) {
} }
void ROM::DrawAllGraphicsData() { void ROM::DrawAllGraphicsData() {
auto buffer = (uchar*) SDL_malloc(346624); auto buffer = new uchar[346624];
auto data = (uchar *)SDL_malloc(2048); auto data = new uchar[2048];
int buffer_pos = 0; int buffer_pos = 0;
for (int i = 0; i < core::constants::NumberOfSheets; i++) { for (int i = 0; i < core::constants::NumberOfSheets; i++) {
// uncompressed sheets // uncompressed sheets
if (i >= 115 && i <= 126) { if (i >= 115 && i <= 126) {
data = new char[core::constants::Uncompressed3BPPSize]; data = new uchar[core::constants::Uncompressed3BPPSize];
int startAddress = GetGraphicsAddress(i); int startAddress = GetGraphicsAddress(i);
for (int j = 0; j < core::constants::Uncompressed3BPPSize; j++) { for (int j = 0; j < core::constants::Uncompressed3BPPSize; j++) {
data[j] = current_rom_[j + startAddress]; data[j] = current_rom_[j + startAddress];

View File

@@ -29,6 +29,8 @@ class ROM {
void SetupRenderer(std::shared_ptr<SDL_Renderer> renderer); void SetupRenderer(std::shared_ptr<SDL_Renderer> renderer);
void LoadFromFile(const std::string& path); void LoadFromFile(const std::string& path);
uchar* DecompressGraphics(int pos, int size);
uchar* DecompressOverworld(int pos, int size);
uchar* Decompress(int pos, int size = 0x800, bool reversed = false); uchar* Decompress(int pos, int size = 0x800, bool reversed = false);
uchar* SNES3bppTo8bppSheet(uchar* buffer_in, int sheet_id = 0, uchar* SNES3bppTo8bppSheet(uchar* buffer_in, int sheet_id = 0,
int size = 0x1000); int size = 0x1000);