17#include "absl/status/status.h"
18#include "absl/status/statusor.h"
19#include "absl/strings/str_cat.h"
20#include "absl/strings/str_format.h"
21#include "absl/strings/string_view.h"
28#include <emscripten.h>
39#include <emscripten.h>
60constexpr size_t kBaseRomSize = 1048576;
66constexpr size_t kHeaderSize = 0x200;
95void MaybeStripSmcHeader(std::vector<uint8_t>& rom_data,
unsigned long& size) {
96 if (size % kBaseRomSize == kHeaderSize && size >= kHeaderSize &&
97 rom_data.size() >= kHeaderSize) {
98 rom_data.erase(rom_data.begin(), rom_data.begin() + kHeaderSize);
100 LOG_INFO(
"Rom",
"Stripped SMC header from ROM (new size: %lu)", size);
105inline void MaybeBroadcastChange(uint32_t offset,
106 const std::vector<uint8_t>& old_bytes,
107 const std::vector<uint8_t>& new_bytes) {
108 if (new_bytes.empty())
110 auto& collab = app::platform::GetWasmCollaborationInstance();
111 if (!collab.IsConnected() || collab.IsApplyingRemoteChange()) {
114 (void)collab.BroadcastChange(offset, old_bytes, new_bytes);
177 uint32_t ptr2, uint32_t ptr3,
size_t rom_size) {
181 if (ptr1 > UINT32_MAX - addr || ptr1 + addr >= rom_size ||
182 ptr2 > UINT32_MAX - addr || ptr2 + addr >= rom_size ||
183 ptr3 > UINT32_MAX - addr || ptr3 + addr >= rom_size) {
185 return static_cast<uint32_t
>(rom_size);
208 std::vector<uint8_t> sheet;
209 const uint8_t sheets[] = {0x71, 0x72, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE};
210 for (
const auto& sheet_id : sheets) {
216 if (offset >= rom.
size()) {
217 return absl::OutOfRangeError(absl::StrFormat(
218 "2BPP graphics sheet %u offset %u exceeds ROM size %zu", sheet_id,
219 offset, rom.
size()));
229 for (
const auto& each_pixel : converted_sheet) {
230 sheet.push_back(each_pixel);
238 const uint32_t kLinkGfxOffset = 0x80000;
239 const uint16_t kLinkGfxLength = 0x800;
240 std::array<gfx::Bitmap, kNumLinkSheets> link_graphics;
243 auto link_sheet_data,
254 return link_graphics;
259 constexpr uint32_t kFontGraphicsOffset = 0x70000;
260 constexpr uint32_t kFontGraphicsSize = 0x2000;
261 constexpr uint32_t kMinRomSizeForFont =
262 kFontGraphicsOffset + kFontGraphicsSize;
264 if (rom.
size() < kMinRomSizeForFont) {
265 return absl::FailedPreconditionError(absl::StrFormat(
266 "ROM too small for font graphics: %zu bytes (need at least %u bytes)",
267 rom.
size(), kMinRomSizeForFont));
271 std::vector<uint8_t> data(rom.
data() + 0x70000,
272 rom.
data() + 0x70000 + 0x2000);
274 std::vector<uint8_t> new_data(0x4000);
275 std::vector<uint8_t> mask = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
276 int sheet_position = 0;
279 for (
int s = 0; s < 4; s++) {
280 for (
int j = 0; j < 4; j++) {
281 for (
int i = 0; i < 16; i++) {
282 for (
int y = 0; y < 8; y++) {
284 data[(y * 2) + (i * 16) + (j * 256) + sheet_position];
286 data[(y * 2) + (i * 16) + (j * 256) + 1 + sheet_position];
288 for (
int x = 0; x < 4; x++) {
290 uint8_t pixdata2 = 0;
292 if ((line_bits0 & mask[(x * 2)]) == mask[(x * 2)]) {
295 if ((line_bits1 & mask[(x * 2)]) == mask[(x * 2)]) {
299 if ((line_bits0 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
302 if ((line_bits1 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
306 new_data[(y * 64) + (x) + (i * 4) + (j * 512) + (s * 2048)] =
307 (uint8_t)((pixdata << 4) | pixdata2);
313 sheet_position += 0x400;
318 font_gfx.
Create(128, 128, 64, std::move(new_data));
356 Rom& rom,
bool defer_render) {
358 EM_ASM({ console.log(
'[C++] LoadAllGraphicsData START - ROM size:', $0); },
362 std::array<gfx::Bitmap, kNumGfxSheets> graphics_sheets;
363 std::vector<uint8_t> sheet;
368 LOG_DEBUG(
"Graphics",
"Cleared graphics buffer, loading %d sheets",
376 LOG_INFO(
"Graphics",
"ROM size: %zu bytes (0x%zX)", rom.
size(), rom.
size());
377 LOG_INFO(
"Graphics",
"Pointer tables: ptr1=0x%X, ptr2=0x%X, ptr3=0x%X",
386 diag.header_stripped = (rom.
size() % (1024 * 1024) != 512);
388 if (rom.
size() > 0x7FDE) {
389 uint16_t c = rom.
data()[0x7FDC] | (rom.
data()[0x7FDD] << 8);
390 uint16_t k = rom.
data()[0x7FDE] | (rom.
data()[0x7FDF] << 8);
391 diag.checksum_valid = ((c ^ k) == 0xFFFF);
398 for (
int probe = 0; probe < 5; probe++) {
402 if (ptr1 + probe < rom.
size() && ptr2 + probe < rom.
size() &&
403 ptr3 + probe < rom.
size()) {
405 uint8_t bank = rom.
data()[ptr1 + probe];
406 uint8_t high = rom.
data()[ptr2 + probe];
407 uint8_t low = rom.
data()[ptr3 + probe];
409 uint32_t pc_addr =
SnesToPc(snes_addr);
411 "Sheet %d: bytes=[%02X,%02X,%02X] -> SNES=0x%06X -> PC=0x%X "
413 probe, bank, high, low, snes_addr, pc_addr,
414 pc_addr < rom.
size() ?
"yes" :
"NO");
420 auto loading_handle =
421 app::platform::WasmLoadingManager::BeginLoading(
"Loading Graphics");
422 app::platform::WasmLoadingManager::SetArenaHandle(loading_handle);
428 float progress =
static_cast<float>(i) /
static_cast<float>(
kNumGfxSheets);
429 app::platform::WasmLoadingManager::UpdateProgress(loading_handle, progress);
430 app::platform::WasmLoadingManager::UpdateMessage(
431 loading_handle, absl::StrFormat(
"Sheet %d/%d", i + 1,
kNumGfxSheets));
439 if (app::platform::WasmLoadingManager::IsCancelled(loading_handle)) {
440 app::platform::WasmLoadingManager::EndLoading(loading_handle);
441 app::platform::WasmLoadingManager::ClearArenaHandle();
444 return absl::CancelledError(
"Graphics loading cancelled by user");
449 diag.sheets[i].index = i;
451 bool offset_valid =
false;
458 if (i >= 115 && i <= 126) {
459 diag.sheets[i].is_compressed =
false;
467 diag.sheets[i].pc_offset = offset;
468 diag.sheets[i].snes_address =
PcToSnes(offset);
469 offset_valid = (offset < rom.
size());
472 diag.sheets[i].first_bytes.clear();
473 if (offset_valid && offset + 8 <= rom.
size()) {
474 for (
int b = 0; b < 8; ++b) {
475 diag.sheets[i].first_bytes.push_back(rom.
data()[offset + b]);
484 "Uncompressed sheet %u offset 0x%X out of bounds (ROM size 0x%zX)",
485 i, offset, rom.
size());
488 diag.sheets[i].decompression_succeeded =
false;
490 std::copy(rom.
data() + offset,
492 diag.sheets[i].decompression_succeeded =
true;
502 }
else if (i == 113 || i == 114 || i >= 218) {
503 diag.sheets[i].is_compressed =
true;
509 diag.sheets[i].pc_offset = offset;
510 diag.sheets[i].snes_address =
PcToSnes(offset);
511 offset_valid = (offset < rom.
size());
514 diag.sheets[i].first_bytes.clear();
515 if (offset_valid && offset + 8 <= rom.
size()) {
516 for (
int b = 0; b < 8; ++b) {
517 diag.sheets[i].first_bytes.push_back(rom.
data()[offset + b]);
528 diag.sheets[i].is_compressed =
true;
535 diag.sheets[i].pc_offset = offset;
536 diag.sheets[i].snes_address =
PcToSnes(offset);
537 offset_valid = (offset < rom.
size());
540 diag.sheets[i].first_bytes.clear();
541 if (offset_valid && offset + 8 <= rom.
size()) {
542 for (
int b = 0; b < 8; ++b) {
543 diag.sheets[i].first_bytes.push_back(rom.
data()[offset + b]);
547 if (offset >= rom.
size()) {
549 "Compressed sheet %u offset 0x%X exceeds ROM size 0x%zX", i,
552 diag.sheets[i].decompression_succeeded =
false;
553 diag.sheets[i].decomp_size_param = 0x800;
559 diag.sheets[i].decomp_size_param = 0x800;
562 if (decomp_result.ok()) {
563 sheet = *decomp_result;
565 diag.sheets[i].decompression_succeeded =
true;
566 diag.sheets[i].actual_decomp_size = sheet.size();
569 if (i == 73 || i == 115) {
571 "[LoadAllGraphicsData] Sheet %d: Decompressed successfully. "
572 "Size: %zu. Offset: 0x%X\n",
573 i, sheet.size(), offset);
577 LOG_WARN(
"Rom",
"Decompression failed for sheet %u: %s", i,
578 decomp_result.status().message());
580 if (i == 73 || i == 115) {
582 "[LoadAllGraphicsData] Sheet %d: Decompression FAILED. Offset: "
587 diag.sheets[i].decompression_succeeded =
false;
602 if (converted_sheet.size() != 4096) {
603 converted_sheet.resize(4096, 0);
617 if (palette_group.size() > 0) {
618 default_palette = palette_group[0];
620 }
else if (i < 128) {
623 if (palette_group.size() > 0) {
624 default_palette = palette_group[0];
629 if (palette_group.size() > 0) {
630 default_palette = palette_group.palette(0);
634 if (!default_palette.
empty()) {
635 graphics_sheets[i].SetPalette(default_palette);
642 const uint8_t* sheet_data = graphics_sheets[i].data();
643 buffer.insert(buffer.end(), sheet_data,
644 sheet_data + graphics_sheets[i].size());
651 constexpr size_t kPlaceholderSize = 4096;
652 std::vector<uint8_t> placeholder_data(kPlaceholderSize, 0xFF);
655 std::move(placeholder_data));
660 size_t old_size = buffer.size();
661 buffer.resize(old_size + kPlaceholderSize, 0xFF);
670 app::platform::WasmLoadingManager::UpdateProgress(loading_handle, 1.0f);
671 app::platform::WasmLoadingManager::EndLoading(loading_handle);
672 app::platform::WasmLoadingManager::ClearArenaHandle();
675 return graphics_sheets;
679 Rom& rom, std::array<gfx::Bitmap, kNumGfxSheets>& gfx_sheets) {
681 if (gfx_sheets[i].is_active()) {
683 std::vector<uint8_t> final_data;
684 bool compressed =
true;
685 if (i >= 115 && i <= 126) {
687 }
else if (i == 113 || i == 114 || i >= 218) {
692 std::cout <<
"Sheet ID " << i <<
" BPP: " << to_bpp << std::endl;
693 auto sheet_data = gfx_sheets[i].vector();
694 std::cout <<
"Sheet data size: " << sheet_data.size() << std::endl;
699 final_data.data(), final_data.size(), &size, 1);
700 for (
int j = 0; j < size; j++) {
701 sheet_data[j] = compressed_data[j];
708 std::copy(final_data.begin(), final_data.end(), rom.
begin() + offset);
711 return absl::OkStatus();
722 return absl::InvalidArgumentError(
723 "Could not load ROM: parameter `filename` is empty.");
732 std::ifstream test_file(
filename_, std::ios::binary);
733 if (!test_file.is_open()) {
734 return absl::NotFoundError(absl::StrCat(
735 "ROM file does not exist or cannot be opened: ",
filename_));
739 test_file.seekg(0, std::ios::end);
742 return absl::InternalError(
"Could not seek to end of ROM file");
744 size_ = test_file.tellg();
749 return absl::InvalidArgumentError(absl::StrFormat(
750 "ROM file too small (%zu bytes), minimum is 32KB",
size_));
752 if (
size_ > 8 * 1024 * 1024) {
753 return absl::InvalidArgumentError(absl::StrFormat(
754 "ROM file too large (%zu bytes), maximum is 8MB",
size_));
757 if (!std::filesystem::exists(
filename)) {
758 return absl::NotFoundError(
759 absl::StrCat(
"ROM file does not exist: ",
filename));
766 std::ifstream file(
filename_, std::ios::binary);
767 if (!file.is_open()) {
768 return absl::NotFoundError(
769 absl::StrCat(
"Could not open ROM file: ",
filename_));
773#ifndef __EMSCRIPTEN__
780 return absl::InvalidArgumentError(absl::StrFormat(
781 "ROM file too small (%zu bytes), minimum is 32KB",
size_));
783 if (
size_ > 8 * 1024 * 1024) {
784 return absl::InvalidArgumentError(absl::StrFormat(
785 "ROM file too large (%zu bytes), maximum is 8MB",
size_));
787 }
catch (
const std::filesystem::filesystem_error&
e) {
789 file.seekg(0, std::ios::end);
791 return absl::InternalError(absl::StrCat(
792 "Could not get file size: ",
filename_,
" - ",
e.what()));
794 size_ = file.tellg();
798 return absl::InvalidArgumentError(
799 absl::StrFormat(
"Invalid ROM size: %zu bytes",
size_));
808 file.seekg(0, std::ios::beg);
812 return absl::InternalError(
813 absl::StrFormat(
"Failed to read ROM data, read %zu of %zu bytes",
814 file.gcount(),
size_));
816 }
catch (
const std::bad_alloc&
e) {
817 return absl::ResourceExhaustedError(absl::StrFormat(
818 "Failed to allocate memory for ROM (%zu bytes)",
size_));
836 return absl::OkStatus();
847 return absl::InvalidArgumentError(
848 "Could not load ROM: parameter `data` is empty.");
862 return absl::OkStatus();
871 return absl::FailedPreconditionError(
"ROM data is empty");
887 LOG_INFO(
"Rom",
"LoadZelda3: Post-strip size=%zu bytes (0x%zX)",
size_,
891 if (
size_ > 0x7FE0) {
894 bool valid = (
complement ^ checksum) == 0xFFFF;
896 "SNES checksum at 0x7FDC: complement=0x%04X checksum=0x%04X "
897 "XOR=0x%04X valid=%s",
901 if (
size_ > 0x4F85) {
902 LOG_INFO(
"Rom",
"Bytes at 0x4F80 (ptr1): %02X %02X %02X %02X %02X %02X",
916 return absl::OutOfRangeError(
917 "ROM image is too small to contain title metadata.");
936 EM_ASM({
console.log(
'[C++] LoadZelda3 - Loading palettes...'); });
941 EM_ASM({
console.log(
'[C++] LoadZelda3 - Palettes loaded successfully'); });
949 EM_ASM({
console.log(
'[C++] LoadZelda3 - Loading gfx groups...'); });
954 {
console.log(
'[C++] LoadZelda3 - Gfx groups loaded successfully'); });
964 if (
rom_data_.size() < kBaseRomSize * 2) {
971 return absl::OkStatus();
976 return absl::FailedPreconditionError(
"ROM data is empty");
980 if (kVersionConstantsMap.find(
version_) == kVersionConstantsMap.end()) {
981 return absl::FailedPreconditionError(absl::StrFormat(
982 "Unsupported ROM version: %d",
static_cast<int>(
version_)));
987 LOG_WARN(
"Rom",
"Graphics groups pointer address out of bounds: %u >= %zu",
989 return absl::OkStatus();
994 return absl::OkStatus();
1000 LOG_WARN(
"Rom",
"Main blockset pointer out of bounds: %u >= %zu",
1002 return absl::OkStatus();
1008 LOG_WARN(
"Rom",
"Main blockset data out of bounds: %u > %zu",
1010 return absl::OkStatus();
1014 for (
int j = 0;
j < 8;
j++) {
1023 printf(
"[LoadGfxGroups] Blockset 0: %d %d %d %d %d %d %d %d\n",
1033 LOG_WARN(
"Rom",
"Entrance graphics group pointer out of bounds: %u >= %zu",
1039 LOG_WARN(
"Rom",
"Room blockset pointer would overflow: %u + %u",
1045 for (
int j = 0;
j < 4;
j++) {
1060 if (
vc.kSpriteBlocksetPointer <
rom_data_.size()) {
1064 LOG_WARN(
"Rom",
"Sprite blockset pointer would overflow: %u + %u",
1071 for (
int j = 0;
j < 4;
j++) {
1072 uint32_t idx =
vc.kSpriteBlocksetPointer + (i * 4) +
j;
1084 if (
vc.kDungeonPalettesGroups <
rom_data_.size()) {
1088 LOG_WARN(
"Rom",
"Palette groups pointer would overflow: %u + %u",
1094 for (
int j = 0;
j < 4;
j++) {
1095 uint32_t idx =
vc.kDungeonPalettesGroups + (i * 4) +
j;
1106 return absl::OkStatus();
1114 for (
int j = 0;
j < 8;
j++) {
1120 for (
int j = 0;
j < 4;
j++) {
1126 for (
int j = 0;
j < 4;
j++) {
1133 for (
int j = 0;
j < 4;
j++) {
1139 return absl::OkStatus();
1145 return absl::InternalError(
"ROM data is empty.");
1160 auto now = std::chrono::system_clock::now();
1161 auto now_c = std::chrono::system_clock::to_time_t(
now);
1176 std::filesystem::copy_options::overwrite_existing);
1177 }
catch (
const std::filesystem::filesystem_error&
e) {
1194 auto now = std::chrono::system_clock::now();
1195 auto now_c = std::chrono::system_clock::to_time_t(
now);
1207 std::cout <<
filename << std::endl;
1211 std::ofstream file(
filename.data(), std::ios::binary | std::ios::trunc);
1213 return absl::InternalError(
1214 absl::StrCat(
"Could not open ROM file for writing: ",
filename));
1220 static_cast<const char*
>(
static_cast<const void*
>(
rom_data_.data())),
1222 }
catch (
const std::ofstream::failure&
e) {
1223 return absl::InternalError(absl::StrCat(
1224 "Error while writing to ROM file: ",
filename,
" - ",
e.what()));
1229 return absl::InternalError(
1230 absl::StrCat(
"Error while writing to ROM file: ",
filename));
1240 for (
size_t j = 0;
j < palette.
size(); ++
j) {
1249 return absl::OkStatus();
1255 for (size_t i = 0; i < group.size(); ++i) {
1257 SavePalette(i, group.name(), *group.mutable_palette(i)));
1259 return absl::OkStatus();
1262 return absl::OkStatus();
1265absl::StatusOr<uint8_t> Rom::ReadByte(
int offset) {
1267 return absl::FailedPreconditionError(
"Offset cannot be negative");
1269 if (offset >=
static_cast<int>(rom_data_.size())) {
1270 return absl::FailedPreconditionError(
"Offset out of range");
1272 return rom_data_[offset];
1275absl::StatusOr<uint16_t> Rom::ReadWord(
int offset) {
1277 return absl::FailedPreconditionError(
"Offset cannot be negative");
1279 if (offset + 1 >=
static_cast<int>(rom_data_.size())) {
1280 return absl::FailedPreconditionError(
"Offset out of range");
1282 auto result = (uint16_t)(rom_data_[offset] | (rom_data_[offset + 1] << 8));
1286absl::StatusOr<uint32_t> Rom::ReadLong(
int offset) {
1288 return absl::OutOfRangeError(
"Offset cannot be negative");
1290 if (offset + 2 >=
static_cast<int>(rom_data_.size())) {
1291 return absl::OutOfRangeError(
"Offset out of range");
1293 auto result = (uint32_t)(rom_data_[offset] | (rom_data_[offset + 1] << 8) |
1294 (rom_data_[offset + 2] << 16));
1298absl::StatusOr<std::vector<uint8_t>> Rom::ReadByteVector(
1299 uint32_t offset, uint32_t length)
const {
1300 if (offset + length >
static_cast<uint32_t
>(rom_data_.size())) {
1301 return absl::OutOfRangeError(
"Offset and length out of range");
1303 std::vector<uint8_t> result;
1304 for (uint32_t i = offset; i < offset + length; i++) {
1305 result.push_back(rom_data_[i]);
1310absl::StatusOr<gfx::Tile16> Rom::ReadTile16(uint32_t tile16_id) {
1315 tile16.tile0_ = gfx::WordToTileInfo(new_tile0);
1318 tile16.tile1_ = gfx::WordToTileInfo(new_tile1);
1321 tile16.tile2_ = gfx::WordToTileInfo(new_tile2);
1324 tile16.tile3_ = gfx::WordToTileInfo(new_tile3);
1328absl::Status Rom::WriteTile16(
int tile16_id,
const gfx::Tile16& tile) {
1338 return absl::OkStatus();
1341absl::Status Rom::WriteByte(
int addr, uint8_t value) {
1342 if (addr >=
static_cast<int>(rom_data_.size())) {
1343 return absl::OutOfRangeError(absl::StrFormat(
1344 "Attempt to write byte %#02x value failed, address %d out of range",
1347 const uint8_t old_val = rom_data_[addr];
1348 rom_data_[addr] = value;
1349 LOG_DEBUG(
"Rom",
"WriteByte: %#06X: %s", addr, util::HexByte(value).data());
1351#ifdef __EMSCRIPTEN__
1352 MaybeBroadcastChange(addr, {old_val}, {value});
1354 return absl::OkStatus();
1357absl::Status Rom::WriteWord(
int addr, uint16_t value) {
1358 if (addr + 1 >=
static_cast<int>(rom_data_.size())) {
1359 return absl::OutOfRangeError(absl::StrFormat(
1360 "Attempt to write word %#04x value failed, address %d out of range",
1363 const uint8_t old0 = rom_data_[addr];
1364 const uint8_t old1 = rom_data_[addr + 1];
1365 rom_data_[addr] = (uint8_t)(value & 0xFF);
1366 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
1367 LOG_DEBUG(
"Rom",
"WriteWord: %#06X: %s", addr, util::HexWord(value).data());
1369#ifdef __EMSCRIPTEN__
1370 MaybeBroadcastChange(addr, {old0, old1},
1371 {
static_cast<uint8_t
>(value & 0xFF),
1372 static_cast<uint8_t
>((value >> 8) & 0xFF)});
1374 return absl::OkStatus();
1377absl::Status Rom::WriteShort(
int addr, uint16_t value) {
1378 if (addr + 1 >=
static_cast<int>(rom_data_.size())) {
1379 return absl::OutOfRangeError(absl::StrFormat(
1380 "Attempt to write short %#04x value failed, address %d out of range",
1383 const uint8_t old0 = rom_data_[addr];
1384 const uint8_t old1 = rom_data_[addr + 1];
1385 rom_data_[addr] = (uint8_t)(value & 0xFF);
1386 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
1387 LOG_DEBUG(
"Rom",
"WriteShort: %#06X: %s", addr, util::HexWord(value).data());
1389#ifdef __EMSCRIPTEN__
1390 MaybeBroadcastChange(addr, {old0, old1},
1391 {
static_cast<uint8_t
>(value & 0xFF),
1392 static_cast<uint8_t
>((value >> 8) & 0xFF)});
1394 return absl::OkStatus();
1397absl::Status Rom::WriteLong(uint32_t addr, uint32_t value) {
1398 if (addr + 2 >=
static_cast<uint32_t
>(rom_data_.size())) {
1399 return absl::OutOfRangeError(absl::StrFormat(
1400 "Attempt to write long %#06x value failed, address %d out of range",
1403 const uint8_t old0 = rom_data_[addr];
1404 const uint8_t old1 = rom_data_[addr + 1];
1405 const uint8_t old2 = rom_data_[addr + 2];
1406 rom_data_[addr] = (uint8_t)(value & 0xFF);
1407 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
1408 rom_data_[addr + 2] = (uint8_t)((value >> 16) & 0xFF);
1409 LOG_DEBUG(
"Rom",
"WriteLong: %#06X: %s", addr, util::HexLong(value).data());
1411#ifdef __EMSCRIPTEN__
1412 MaybeBroadcastChange(addr, {old0, old1, old2},
1413 {
static_cast<uint8_t
>(value & 0xFF),
1414 static_cast<uint8_t
>((value >> 8) & 0xFF),
1415 static_cast<uint8_t
>((value >> 16) & 0xFF)});
1417 return absl::OkStatus();
1420absl::Status Rom::WriteVector(
int addr, std::vector<uint8_t> data) {
1421 if (addr +
static_cast<int>(data.size()) >
1422 static_cast<int>(rom_data_.size())) {
1423 return absl::InvalidArgumentError(absl::StrFormat(
1424 "Attempt to write vector value failed, address %d out of range", addr));
1426 std::vector<uint8_t> old_data;
1427 old_data.reserve(data.size());
1428 for (
int i = 0; i < static_cast<int>(data.size()); i++) {
1429 old_data.push_back(rom_data_[addr + i]);
1431 for (
int i = 0; i < static_cast<int>(data.size()); i++) {
1432 rom_data_[addr + i] = data[i];
1434 LOG_DEBUG(
"Rom",
"WriteVector: %#06X: %s", addr,
1435 util::HexByte(data[0]).data());
1437#ifdef __EMSCRIPTEN__
1438 MaybeBroadcastChange(addr, old_data, data);
1440 return absl::OkStatus();
1443absl::Status Rom::WriteColor(uint32_t address,
const gfx::SnesColor& color) {
1444 uint16_t bgr = ((color.snes() >> 10) & 0x1F) | ((color.snes() & 0x1F) << 10) |
1445 (color.snes() & 0x7C00);
1448 LOG_DEBUG(
"Rom",
"WriteColor: %#06X: %s", address, util::HexWord(bgr).data());
1449 auto st = WriteShort(address, bgr);
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
absl::StatusOr< std::vector< uint8_t > > ReadByteVector(uint32_t offset, uint32_t length) const
auto mutable_graphics_buffer()
absl::Status LoadFromFile(const std::string &filename, const LoadOptions &options=LoadOptions::Defaults())
gfx::PaletteGroupMap palette_groups_
absl::Status WriteColor(uint32_t address, const gfx::SnesColor &color)
absl::Status LoadZelda3()
std::array< std::array< uint8_t, 4 >, kNumSpritesets > spriteset_ids
const auto & vector() const
std::array< std::array< uint8_t, 4 >, kNumPalettesets > paletteset_ids
absl::StatusOr< uint16_t > ReadWord(int offset)
const auto & palette_group() const
absl::Status SaveToFile(const SaveSettings &settings)
std::array< std::array< uint8_t, 4 >, kNumRoomBlocksets > room_blockset_ids
GraphicsLoadDiagnostics & GetMutableDiagnostics()
std::vector< uint8_t > rom_data_
zelda3_version_pointers version_constants() const
absl::Status SaveGfxGroups()
absl::Status LoadGfxGroups()
absl::Status LoadFromData(const std::vector< uint8_t > &data, const LoadOptions &options=LoadOptions::Defaults())
absl::Status SavePalette(int index, const std::string &group_name, gfx::SnesPalette &palette)
project::ResourceLabelManager resource_label_manager_
std::array< std::array< uint8_t, 8 >, kNumMainBlocksets > main_blockset_ids
absl::Status SaveAllPalettes()
Represents a bitmap image optimized for SNES ROM hacking.
void Create(int width, int height, int depth, std::span< uint8_t > data)
Create a bitmap with the given dimensions and data.
constexpr void set_modified(bool m)
constexpr bool is_modified() const
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
Tile composition of four 8x8 tiles.
#define LOG_DEBUG(category, format,...)
#define LOG_WARN(category, format,...)
#define LOG_INFO(category, format,...)
#define ASSIGN_OR_RETURN(type_variable_name, expression)
absl::StatusOr< std::vector< uint8_t > > DecompressV2(const uint8_t *data, int offset, int size, int mode, size_t rom_size)
Decompresses a buffer of data using the LC_LZ2 algorithm.
constexpr int kTilesheetHeight
constexpr int kTilesheetWidth
constexpr int kTilesheetDepth
std::vector< uint8_t > Bpp8SnesToIndexed(std::vector< uint8_t > data, uint64_t bpp)
std::vector< uint8_t > SnesTo8bppSheet(std::span< uint8_t > sheet, int bpp, int num_sheets)
uint32_t GetPaletteAddress(const std::string &group_name, size_t palette_index, size_t color_index)
std::vector< uint8_t > HyruleMagicCompress(uint8_t const *const src, int const oldsize, int *const size, int const flag)
absl::Status LoadAllPalettes(const std::vector< uint8_t > &rom_data, PaletteGroupMap &groups)
Loads all the palettes for the game.
uint32_t PcToSnes(uint32_t addr)
constexpr uint32_t kGfxGroupsPointer
absl::StatusOr< std::vector< uint8_t > > Load2BppGraphics(const Rom &rom)
Loads 2bpp graphics from Rom data.
absl::StatusOr< std::array< gfx::Bitmap, kNumLinkSheets > > LoadLinkGraphics(const Rom &rom)
Loads the players 4bpp graphics sheet from Rom data.
uint32_t GetGraphicsAddress(const uint8_t *data, uint8_t addr, uint32_t ptr1, uint32_t ptr2, uint32_t ptr3, size_t rom_size)
Calculates ROM offset for a graphics sheet using pointer tables.
constexpr int Uncompressed3BPPSize
constexpr uint32_t kNumLinkSheets
constexpr uint32_t kEntranceGfxGroup
constexpr uint32_t kNumSpritesets
int AddressFromBytes(uint8_t bank, uint8_t high, uint8_t low) noexcept
constexpr uint32_t kTile16Ptr
constexpr uint32_t kNumMainBlocksets
constexpr uint32_t kNumRoomBlocksets
absl::StatusOr< gfx::Bitmap > LoadFontGraphics(const Rom &rom)
constexpr uint32_t kNumGfxSheets
absl::StatusOr< std::array< gfx::Bitmap, kNumGfxSheets > > LoadAllGraphicsData(Rom &rom, bool defer_render)
This function iterates over all graphics sheets in the Rom and loads them into memory....
constexpr uint32_t kNumPalettesets
absl::Status SaveAllGraphicsData(Rom &rom, std::array< gfx::Bitmap, kNumGfxSheets > &gfx_sheets)
uint32_t SnesToPc(uint32_t addr) noexcept
#define RETURN_IF_ERROR(expr)
static RomLoadOptions AppDefaults()
bool expand_to_full_image
static RomLoadOptions CliDefaults()
static RomLoadOptions RawDataOnly()
bool load_resource_labels
absl::Status for_each(Func &&func)
Represents a group of palettes.
bool LoadLabels(const std::string &filename)
uint32_t kOverworldGfxPtr1
uint32_t kOverworldGfxPtr2
uint32_t kSpriteBlocksetPointer
uint32_t kOverworldGfxPtr3
uint32_t kDungeonPalettesGroups
The Legend of Zelda: A Link to the Past - Data Structures and Constants.