added zarby decompress and tilesheet code (not fully implemented)

This commit is contained in:
Justin Scofield
2022-06-15 23:32:00 -04:00
parent 6b9e7b00d7
commit a9cbc70e8f
2 changed files with 74 additions and 68 deletions

View File

@@ -41,7 +41,7 @@ std::vector<tile8> ROM::ExtractTiles(Graphics::TilePreset &preset) {
uint size = preset.length_; uint size = preset.length_;
int tilePos = preset.pc_tiles_location_; int tilePos = preset.pc_tiles_location_;
std::vector<tile8> rawTiles; std::vector<tile8> rawTiles;
filePos = GetRomPosition(preset, tilePos, preset.SNESTilesLocation); filePos = GetRomPosition(tilePos, preset.SNESTilesLocation);
std::cout << "ROM Position: " << filePos << " from " std::cout << "ROM Position: " << filePos << " from "
<< preset.SNESTilesLocation << std::endl; << preset.SNESTilesLocation << std::endl;
@@ -55,6 +55,7 @@ std::vector<tile8> ROM::ExtractTiles(Graphics::TilePreset &preset) {
std::cout << alttp_decompression_error << std::endl; std::cout << alttp_decompression_error << std::endl;
return rawTiles; return rawTiles;
} }
// data = Decompress(filePos);
// unpack the tiles based on their depth // unpack the tiles based on their depth
unsigned tileCpt = 0; unsigned tileCpt = 0;
@@ -73,10 +74,10 @@ std::vector<tile8> ROM::ExtractTiles(Graphics::TilePreset &preset) {
} }
Graphics::SNESPalette ROM::ExtractPalette(Graphics::TilePreset &preset) { Graphics::SNESPalette ROM::ExtractPalette(Graphics::TilePreset &preset) {
unsigned int filePos = GetRomPosition(preset, preset.pc_palette_location_, uint filePos =
preset.SNESPaletteLocation); GetRomPosition(preset.pc_palette_location_, preset.SNESPaletteLocation);
std::cout << "Palette pos : " << filePos << std::endl; // TODO: make this hex std::cout << "Palette pos : " << filePos << std::endl; // TODO: make this hex
unsigned int palette_size = pow(2, preset.bits_per_pixel_); // - 1; uint palette_size = pow(2, preset.bits_per_pixel_); // - 1;
auto palette_data = std::make_unique<unsigned char[]>(palette_size * 2); auto palette_data = std::make_unique<unsigned char[]>(palette_size * 2);
memcpy(palette_data.get(), current_rom_ + filePos, palette_size * 2); memcpy(palette_data.get(), current_rom_ + filePos, palette_size * 2);
@@ -102,66 +103,66 @@ Graphics::SNESPalette ROM::ExtractPalette(Graphics::TilePreset &preset) {
return pal; return pal;
} }
uint32_t ROM::GetRomPosition(const Graphics::TilePreset &preset, int directAddr, uint32_t ROM::GetRomPosition(int direct_addr, uint snes_addr) const {
unsigned int snesAddr) const {
unsigned int filePos = -1; unsigned int filePos = -1;
std::cout << "directAddr:" << directAddr << std::endl; std::cout << "directAddr:" << direct_addr << std::endl;
if (directAddr == -1) { if (direct_addr == -1) {
filePos = rommapping_snes_to_pc(snesAddr, type_, has_header_); filePos = rommapping_snes_to_pc(snes_addr, type_, has_header_);
} else { } else {
filePos = directAddr; filePos = direct_addr;
if (has_header_) filePos += 0x200; if (has_header_) filePos += 0x200;
} }
std::cout << "filePos:" << filePos << std::endl; std::cout << "filePos:" << filePos << std::endl;
return filePos; return filePos;
} }
unsigned char *sheetBuffer = (unsigned char *)malloc(0x1000); // unsigned char *sheetBuffer = (unsigned char *)malloc(0x1000);
unsigned char bitmask[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01}; // unsigned char bitmask[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
void SNES3bppTo8bppSheet() // 128x32 // void SNES3bppTo8bppSheet() // 128x32
{ // {
char *buffer = new char[0x800]; // char *buffer = new char[0x800];
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;
int ypos = 0; // int ypos = 0;
for (int i = 0; i < 64; i++) // for each tiles //16 per lines // for (int i = 0; i < 64; i++) // for each tiles //16 per lines
{ // {
for (int y = 0; y < 8; y++) // for each lines // for (int y = 0; y < 8; y++) // for each lines
{ // {
//[0] + [1] + [16] // //[0] + [1] + [16]
for (int x = 0; x < 8; x++) { // for (int x = 0; x < 8; x++) {
unsigned char b1 = // unsigned char b1 =
(unsigned char)((buffer[(y * 2) + (24 * pos)] & (bitmask[x]))); // (unsigned char)((buffer[(y * 2) + (24 * pos)] & (bitmask[x])));
unsigned char b2 = // unsigned char b2 =
(unsigned char)(buffer[((y * 2) + (24 * pos)) + 1] & (bitmask[x])); // (unsigned char)(buffer[((y * 2) + (24 * pos)) + 1] &
unsigned char b3 = // (bitmask[x]));
(unsigned char)(buffer[(16 + y) + (24 * pos)] & (bitmask[x])); // unsigned char b3 =
unsigned char b = 0; // (unsigned char)(buffer[(16 + y) + (24 * pos)] & (bitmask[x]));
if (b1 != 0) { // unsigned char b = 0;
b |= 1; // if (b1 != 0) {
}; // b |= 1;
if (b2 != 0) { // };
b |= 2; // if (b2 != 0) {
}; // b |= 2;
if (b3 != 0) { // };
b |= 4; // if (b3 != 0) {
}; // b |= 4;
sheetBuffer[x + (xx) + (y * 128) + (yy * 1024)] = b; // };
} // sheetBuffer[x + (xx) + (y * 128) + (yy * 1024)] = b;
} // }
pos++; // }
ypos++; // pos++;
xx += 8; // ypos++;
if (ypos >= 16) { // xx += 8;
yy++; // if (ypos >= 16) {
xx = 0; // yy++;
ypos = 0; // xx = 0;
} // ypos = 0;
} // }
} // }
// }
char *Decompress(int pos, bool reversed = false) { char *ROM::Decompress(int pos, bool reversed) {
char *buffer = new char[0x800]; char *buffer = new char[0x800];
for (int i = 0; i < 0x800; i++) { for (int i = 0; i < 0x800; i++) {
buffer[i] = 0; buffer[i] = 0;
@@ -169,10 +170,10 @@ char *Decompress(int pos, bool reversed = false) {
unsigned int bufferPos = 0; unsigned int bufferPos = 0;
unsigned char cmd = 0; unsigned char cmd = 0;
unsigned int length = 0; unsigned int length = 0;
char romData[256];
unsigned char databyte = (unsigned char)romData[pos]; unsigned char databyte = (unsigned char)current_rom_[pos];
while (true) { while (true) {
databyte = (unsigned char)romData[pos]; databyte = (unsigned char)current_rom_[pos];
if (databyte == 0xFF) // End of decompression if (databyte == 0xFF) // End of decompression
{ {
break; break;
@@ -182,7 +183,8 @@ char *Decompress(int pos, bool reversed = false) {
{ {
cmd = (unsigned char)((databyte >> 2) & 0x07); cmd = (unsigned char)((databyte >> 2) & 0x07);
length = length =
(unsigned short)(((romData[pos] << 8) | romData[pos + 1]) & 0x3FF); (unsigned short)(((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
{ {
@@ -194,26 +196,26 @@ char *Decompress(int pos, bool reversed = false) {
switch (cmd) { switch (cmd) {
case 00: // Direct Copy (Could be replaced with a MEMCPY) case 00: // Direct Copy (Could be replaced with a MEMCPY)
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
buffer[bufferPos++] = (unsigned char)romData[pos++]; buffer[bufferPos++] = (unsigned char)current_rom_[pos++];
} }
// Do not advance in the ROM // Do not advance in the ROM
break; break;
case 01: // Byte Fill case 01: // Byte Fill
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
buffer[bufferPos++] = (unsigned char)romData[pos]; buffer[bufferPos++] = (unsigned char)current_rom_[pos];
} }
pos += 1; // Advance 1 byte in the ROM pos += 1; // Advance 1 byte in the ROM
break; break;
case 02: // Word Fill case 02: // Word Fill
for (int i = 0; i < length; i += 2) { for (int i = 0; i < length; i += 2) {
buffer[bufferPos++] = (unsigned char)romData[pos]; buffer[bufferPos++] = (unsigned char)current_rom_[pos];
buffer[bufferPos++] = (unsigned char)romData[pos + 1]; buffer[bufferPos++] = (unsigned char)current_rom_[pos + 1];
} }
pos += 2; // Advance 2 byte in the ROM pos += 2; // Advance 2 byte in the ROM
break; break;
case 03: // Increasing Fill case 03: // Increasing Fill
{ {
unsigned char incByte = (unsigned char)romData[pos]; unsigned char incByte = (unsigned char)current_rom_[pos];
for (int i = 0; i < (unsigned int)length; i++) { for (int i = 0; i < (unsigned int)length; i++) {
buffer[bufferPos++] = (unsigned char)incByte++; buffer[bufferPos++] = (unsigned char)incByte++;
} }
@@ -221,8 +223,8 @@ char *Decompress(int pos, bool reversed = false) {
} break; } break;
case 04: // Repeat (Reversed byte order for maps) case 04: // Repeat (Reversed byte order for maps)
{ {
unsigned short s1 = ((romData[pos + 1] & 0xFF) << 8); unsigned short s1 = ((current_rom_[pos + 1] & 0xFF) << 8);
unsigned short s2 = ((romData[pos] & 0xFF)); unsigned short s2 = ((current_rom_[pos] & 0xFF));
unsigned short Addr = (unsigned short)(s1 | s2); unsigned short Addr = (unsigned short)(s1 | s2);
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
buffer[bufferPos] = (unsigned char)buffer[Addr + i]; buffer[bufferPos] = (unsigned char)buffer[Addr + i];

View File

@@ -24,18 +24,20 @@ int AddressFromBytes(uchar addr1, uchar addr2, uchar addr3);
class ROM { class ROM {
public: public:
ROM() = default;
~ROM(); ~ROM();
void LoadFromFile(const std::string& path); void LoadFromFile(const std::string& path);
std::vector<tile8> ExtractTiles(Graphics::TilePreset& preset); std::vector<tile8> ExtractTiles(Graphics::TilePreset& preset);
Graphics::SNESPalette ExtractPalette(Graphics::TilePreset& preset); Graphics::SNESPalette ExtractPalette(Graphics::TilePreset& preset);
uint32_t GetRomPosition(const Graphics::TilePreset& preset, int directAddr,
unsigned int snesAddr) const; uint32_t GetRomPosition(int direct_addr, uint snes_addr) const;
inline uchar* GetRawData() { return current_rom_; } inline uchar* GetRawData() { return current_rom_; }
const uchar* getTitle() const { return title; } const uchar* getTitle() const { return title; }
long int getSize() const { return size_; } long int getSize() const { return size_; }
char getVersion() const { return version_; } char getVersion() const { return version_; }
bool isLoaded() const { return loaded; } bool isLoaded() const { return loaded; }
char* Decompress(int pos, bool reversed = false);
private: private:
bool loaded = false; bool loaded = false;
@@ -50,6 +52,8 @@ class ROM {
enum rom_type type_ = LoROM; enum rom_type type_ = LoROM;
std::shared_ptr<uchar> rom_ptr_; std::shared_ptr<uchar> rom_ptr_;
std::unordered_map<unsigned int, std::shared_ptr<uchar[2048]>>
decompressed_sheets;
}; };
} // namespace Data } // namespace Data