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

View File

@@ -24,18 +24,20 @@ int AddressFromBytes(uchar addr1, uchar addr2, uchar addr3);
class ROM {
public:
ROM() = default;
~ROM();
void LoadFromFile(const std::string& path);
std::vector<tile8> ExtractTiles(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_; }
const uchar* getTitle() const { return title; }
long int getSize() const { return size_; }
char getVersion() const { return version_; }
bool isLoaded() const { return loaded; }
char* Decompress(int pos, bool reversed = false);
private:
bool loaded = false;
@@ -50,6 +52,8 @@ class ROM {
enum rom_type type_ = LoROM;
std::shared_ptr<uchar> rom_ptr_;
std::unordered_map<unsigned int, std::shared_ptr<uchar[2048]>>
decompressed_sheets;
};
} // namespace Data