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())
return;
109 auto& collab = app::platform::GetWasmCollaborationInstance();
110 if (!collab.IsConnected() || collab.IsApplyingRemoteChange()) {
113 (void)collab.BroadcastChange(offset, old_bytes, new_bytes);
176 uint32_t ptr2, uint32_t ptr3,
size_t rom_size) {
180 if (ptr1 > UINT32_MAX - addr || ptr1 + addr >= rom_size ||
181 ptr2 > UINT32_MAX - addr || ptr2 + addr >= rom_size ||
182 ptr3 > UINT32_MAX - addr || ptr3 + addr >= rom_size) {
184 return static_cast<uint32_t
>(rom_size);
207 std::vector<uint8_t> sheet;
208 const uint8_t sheets[] = {0x71, 0x72, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE};
209 for (
const auto& sheet_id : sheets) {
216 if (offset >= rom.
size()) {
217 return absl::OutOfRangeError(
218 absl::StrFormat(
"2BPP graphics sheet %u offset %u exceeds ROM size %zu",
219 sheet_id, offset, rom.
size()));
228 for (
const auto& each_pixel : converted_sheet) {
229 sheet.push_back(each_pixel);
237 const uint32_t kLinkGfxOffset = 0x80000;
238 const uint16_t kLinkGfxLength = 0x800;
239 std::array<gfx::Bitmap, kNumLinkSheets> link_graphics;
242 auto link_sheet_data,
253 return link_graphics;
258 constexpr uint32_t kFontGraphicsOffset = 0x70000;
259 constexpr uint32_t kFontGraphicsSize = 0x2000;
260 constexpr uint32_t kMinRomSizeForFont = kFontGraphicsOffset + kFontGraphicsSize;
262 if (rom.
size() < kMinRomSizeForFont) {
263 return absl::FailedPreconditionError(
264 absl::StrFormat(
"ROM too small for font graphics: %zu bytes (need at least %u bytes)",
265 rom.
size(), kMinRomSizeForFont));
269 std::vector<uint8_t> data(rom.
data() + 0x70000, rom.
data() + 0x70000 + 0x2000);
271 std::vector<uint8_t> new_data(0x4000);
272 std::vector<uint8_t> mask = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
273 int sheet_position = 0;
276 for (
int s = 0; s < 4; s++) {
277 for (
int j = 0; j < 4; j++) {
278 for (
int i = 0; i < 16; i++) {
279 for (
int y = 0; y < 8; y++) {
281 data[(y * 2) + (i * 16) + (j * 256) + sheet_position];
283 data[(y * 2) + (i * 16) + (j * 256) + 1 + sheet_position];
285 for (
int x = 0; x < 4; x++) {
287 uint8_t pixdata2 = 0;
289 if ((line_bits0 & mask[(x * 2)]) == mask[(x * 2)]) {
292 if ((line_bits1 & mask[(x * 2)]) == mask[(x * 2)]) {
296 if ((line_bits0 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
299 if ((line_bits1 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
303 new_data[(y * 64) + (x) + (i * 4) + (j * 512) + (s * 2048)] =
304 (uint8_t)((pixdata << 4) | pixdata2);
310 sheet_position += 0x400;
315 font_gfx.
Create(128, 128, 64, std::move(new_data));
353 Rom& rom,
bool defer_render) {
355 EM_ASM({ console.log(
'[C++] LoadAllGraphicsData START - ROM size:', $0); }, rom.
size());
358 std::array<gfx::Bitmap, kNumGfxSheets> graphics_sheets;
359 std::vector<uint8_t> sheet;
364 LOG_DEBUG(
"Graphics",
"Cleared graphics buffer, loading %d sheets",
372 LOG_INFO(
"Graphics",
"ROM size: %zu bytes (0x%zX)", rom.
size(), rom.
size());
373 LOG_INFO(
"Graphics",
"Pointer tables: ptr1=0x%X, ptr2=0x%X, ptr3=0x%X",
382 diag.header_stripped = (rom.
size() % (1024 * 1024) != 512);
384 if (rom.
size() > 0x7FDE) {
385 uint16_t c = rom.
data()[0x7FDC] | (rom.
data()[0x7FDD] << 8);
386 uint16_t k = rom.
data()[0x7FDE] | (rom.
data()[0x7FDF] << 8);
387 diag.checksum_valid = ((c ^ k) == 0xFFFF);
394 for (
int probe = 0; probe < 5; probe++) {
398 if (ptr1 + probe < rom.
size() && ptr2 + probe < rom.
size() && ptr3 + probe < rom.
size()) {
400 uint8_t bank = rom.
data()[ptr1 + probe];
401 uint8_t high = rom.
data()[ptr2 + probe];
402 uint8_t low = rom.
data()[ptr3 + probe];
404 uint32_t pc_addr =
SnesToPc(snes_addr);
405 LOG_INFO(
"Graphics",
"Sheet %d: bytes=[%02X,%02X,%02X] -> SNES=0x%06X -> PC=0x%X (valid=%s)",
406 probe, bank, high, low, snes_addr, pc_addr,
407 pc_addr < rom.
size() ?
"yes" :
"NO");
413 auto loading_handle =
414 app::platform::WasmLoadingManager::BeginLoading(
"Loading Graphics");
415 app::platform::WasmLoadingManager::SetArenaHandle(loading_handle);
421 float progress =
static_cast<float>(i) /
static_cast<float>(
kNumGfxSheets);
422 app::platform::WasmLoadingManager::UpdateProgress(loading_handle, progress);
423 app::platform::WasmLoadingManager::UpdateMessage(
424 loading_handle, absl::StrFormat(
"Sheet %d/%d", i + 1,
kNumGfxSheets));
432 if (app::platform::WasmLoadingManager::IsCancelled(loading_handle)) {
433 app::platform::WasmLoadingManager::EndLoading(loading_handle);
434 app::platform::WasmLoadingManager::ClearArenaHandle();
437 return absl::CancelledError(
"Graphics loading cancelled by user");
442 diag.sheets[i].index = i;
444 bool offset_valid =
false;
451 if (i >= 115 && i <= 126) {
452 diag.sheets[i].is_compressed =
false;
461 diag.sheets[i].pc_offset = offset;
462 diag.sheets[i].snes_address =
PcToSnes(offset);
463 offset_valid = (offset < rom.
size());
466 diag.sheets[i].first_bytes.clear();
467 if (offset_valid && offset + 8 <= rom.
size()) {
468 for (
int b = 0; b < 8; ++b) {
469 diag.sheets[i].first_bytes.push_back(rom.
data()[offset + b]);
476 LOG_WARN(
"Rom",
"Uncompressed sheet %u offset 0x%X out of bounds (ROM size 0x%zX)",
477 i, offset, rom.
size());
480 diag.sheets[i].decompression_succeeded =
false;
484 diag.sheets[i].decompression_succeeded =
true;
494 }
else if (i == 113 || i == 114 || i >= 218) {
495 diag.sheets[i].is_compressed =
true;
502 diag.sheets[i].pc_offset = offset;
503 diag.sheets[i].snes_address =
PcToSnes(offset);
504 offset_valid = (offset < rom.
size());
507 diag.sheets[i].first_bytes.clear();
508 if (offset_valid && offset + 8 <= rom.
size()) {
509 for (
int b = 0; b < 8; ++b) {
510 diag.sheets[i].first_bytes.push_back(rom.
data()[offset + b]);
521 diag.sheets[i].is_compressed =
true;
529 diag.sheets[i].pc_offset = offset;
530 diag.sheets[i].snes_address =
PcToSnes(offset);
531 offset_valid = (offset < rom.
size());
534 diag.sheets[i].first_bytes.clear();
535 if (offset_valid && offset + 8 <= rom.
size()) {
536 for (
int b = 0; b < 8; ++b) {
537 diag.sheets[i].first_bytes.push_back(rom.
data()[offset + b]);
541 if (offset >= rom.
size()) {
542 LOG_WARN(
"Rom",
"Compressed sheet %u offset 0x%X exceeds ROM size 0x%zX",
543 i, offset, rom.
size());
545 diag.sheets[i].decompression_succeeded =
false;
546 diag.sheets[i].decomp_size_param = 0x800;
552 diag.sheets[i].decomp_size_param = 0x800;
554 if (decomp_result.ok()) {
555 sheet = *decomp_result;
557 diag.sheets[i].decompression_succeeded =
true;
558 diag.sheets[i].actual_decomp_size = sheet.size();
561 if (i == 73 || i == 115) {
562 printf(
"[LoadAllGraphicsData] Sheet %d: Decompressed successfully. Size: %zu. Offset: 0x%X\n",
563 i, sheet.size(), offset);
567 LOG_WARN(
"Rom",
"Decompression failed for sheet %u: %s", i, decomp_result.status().message());
569 if (i == 73 || i == 115) {
570 printf(
"[LoadAllGraphicsData] Sheet %d: Decompression FAILED. Offset: 0x%X\n", i, offset);
573 diag.sheets[i].decompression_succeeded =
false;
588 if (converted_sheet.size() != 4096) {
589 converted_sheet.resize(4096, 0);
603 if (palette_group.size() > 0) {
604 default_palette = palette_group[0];
606 }
else if (i < 128) {
609 if (palette_group.size() > 0) {
610 default_palette = palette_group[0];
615 if (palette_group.size() > 0) {
616 default_palette = palette_group.palette(0);
620 if (!default_palette.
empty()) {
621 graphics_sheets[i].SetPalette(default_palette);
628 const uint8_t* sheet_data = graphics_sheets[i].data();
629 buffer.insert(buffer.end(), sheet_data,
630 sheet_data + graphics_sheets[i].size());
637 constexpr size_t kPlaceholderSize = 4096;
638 std::vector<uint8_t> placeholder_data(kPlaceholderSize, 0xFF);
645 size_t old_size = buffer.size();
646 buffer.resize(old_size + kPlaceholderSize, 0xFF);
655 app::platform::WasmLoadingManager::UpdateProgress(loading_handle, 1.0f);
656 app::platform::WasmLoadingManager::EndLoading(loading_handle);
657 app::platform::WasmLoadingManager::ClearArenaHandle();
660 return graphics_sheets;
664 Rom& rom, std::array<gfx::Bitmap, kNumGfxSheets>& gfx_sheets) {
666 if (gfx_sheets[i].is_active()) {
668 std::vector<uint8_t> final_data;
669 bool compressed =
true;
670 if (i >= 115 && i <= 126) {
672 }
else if (i == 113 || i == 114 || i >= 218) {
677 std::cout <<
"Sheet ID " << i <<
" BPP: " << to_bpp << std::endl;
678 auto sheet_data = gfx_sheets[i].vector();
679 std::cout <<
"Sheet data size: " << sheet_data.size() << std::endl;
684 final_data.data(), final_data.size(), &size, 1);
685 for (
int j = 0; j < size; j++) {
686 sheet_data[j] = compressed_data[j];
694 std::copy(final_data.begin(), final_data.end(), rom.
begin() + offset);
697 return absl::OkStatus();
708 return absl::InvalidArgumentError(
709 "Could not load ROM: parameter `filename` is empty.");
718 std::ifstream test_file(
filename_, std::ios::binary);
719 if (!test_file.is_open()) {
720 return absl::NotFoundError(
721 absl::StrCat(
"ROM file does not exist or cannot be opened: ",
filename_));
725 test_file.seekg(0, std::ios::end);
728 return absl::InternalError(
"Could not seek to end of ROM file");
730 size_ = test_file.tellg();
735 return absl::InvalidArgumentError(absl::StrFormat(
736 "ROM file too small (%zu bytes), minimum is 32KB",
size_));
738 if (
size_ > 8 * 1024 * 1024) {
739 return absl::InvalidArgumentError(absl::StrFormat(
740 "ROM file too large (%zu bytes), maximum is 8MB",
size_));
743 if (!std::filesystem::exists(
filename)) {
744 return absl::NotFoundError(
745 absl::StrCat(
"ROM file does not exist: ",
filename));
752 std::ifstream file(
filename_, std::ios::binary);
753 if (!file.is_open()) {
754 return absl::NotFoundError(
755 absl::StrCat(
"Could not open ROM file: ",
filename_));
759#ifndef __EMSCRIPTEN__
766 return absl::InvalidArgumentError(absl::StrFormat(
767 "ROM file too small (%zu bytes), minimum is 32KB",
size_));
769 if (
size_ > 8 * 1024 * 1024) {
770 return absl::InvalidArgumentError(absl::StrFormat(
771 "ROM file too large (%zu bytes), maximum is 8MB",
size_));
773 }
catch (
const std::filesystem::filesystem_error&
e) {
775 file.seekg(0, std::ios::end);
777 return absl::InternalError(absl::StrCat(
778 "Could not get file size: ",
filename_,
" - ",
e.what()));
780 size_ = file.tellg();
784 return absl::InvalidArgumentError(
785 absl::StrFormat(
"Invalid ROM size: %zu bytes",
size_));
794 file.seekg(0, std::ios::beg);
798 return absl::InternalError(
799 absl::StrFormat(
"Failed to read ROM data, read %zu of %zu bytes",
800 file.gcount(),
size_));
802 }
catch (
const std::bad_alloc&
e) {
803 return absl::ResourceExhaustedError(absl::StrFormat(
804 "Failed to allocate memory for ROM (%zu bytes)",
size_));
822 return absl::OkStatus();
833 return absl::InvalidArgumentError(
834 "Could not load ROM: parameter `data` is empty.");
848 return absl::OkStatus();
857 return absl::FailedPreconditionError(
"ROM data is empty");
876 if (
size_ > 0x7FE0) {
879 bool valid = (
complement ^ checksum) == 0xFFFF;
880 LOG_INFO(
"Rom",
"SNES checksum at 0x7FDC: complement=0x%04X checksum=0x%04X XOR=0x%04X valid=%s",
884 if (
size_ > 0x4F85) {
885 LOG_INFO(
"Rom",
"Bytes at 0x4F80 (ptr1): %02X %02X %02X %02X %02X %02X",
899 return absl::OutOfRangeError(
900 "ROM image is too small to contain title metadata.");
919 EM_ASM({
console.log(
'[C++] LoadZelda3 - Loading palettes...'); });
924 EM_ASM({
console.log(
'[C++] LoadZelda3 - Palettes loaded successfully'); });
932 EM_ASM({
console.log(
'[C++] LoadZelda3 - Loading gfx groups...'); });
936 EM_ASM({
console.log(
'[C++] LoadZelda3 - Gfx groups loaded successfully'); });
946 if (
rom_data_.size() < kBaseRomSize * 2) {
953 return absl::OkStatus();
958 return absl::FailedPreconditionError(
"ROM data is empty");
962 if (kVersionConstantsMap.find(
version_) == kVersionConstantsMap.end()) {
963 return absl::FailedPreconditionError(
964 absl::StrFormat(
"Unsupported ROM version: %d",
static_cast<int>(
version_)));
969 LOG_WARN(
"Rom",
"Graphics groups pointer address out of bounds: %u >= %zu",
971 return absl::OkStatus();
975 if (!
ptr_status.ok())
return absl::OkStatus();
981 LOG_WARN(
"Rom",
"Main blockset pointer out of bounds: %u >= %zu",
983 return absl::OkStatus();
989 LOG_WARN(
"Rom",
"Main blockset data out of bounds: %u > %zu",
991 return absl::OkStatus();
995 for (
int j = 0;
j < 8;
j++) {
1004 printf(
"[LoadGfxGroups] Blockset 0: %d %d %d %d %d %d %d %d\n",
1014 LOG_WARN(
"Rom",
"Entrance graphics group pointer out of bounds: %u >= %zu",
1020 LOG_WARN(
"Rom",
"Room blockset pointer would overflow: %u + %u",
1026 for (
int j = 0;
j < 4;
j++) {
1041 if (
vc.kSpriteBlocksetPointer <
rom_data_.size()) {
1045 LOG_WARN(
"Rom",
"Sprite blockset pointer would overflow: %u + %u",
1051 for (
int j = 0;
j < 4;
j++) {
1052 uint32_t idx =
vc.kSpriteBlocksetPointer + (i * 4) +
j;
1064 if (
vc.kDungeonPalettesGroups <
rom_data_.size()) {
1068 LOG_WARN(
"Rom",
"Palette groups pointer would overflow: %u + %u",
1074 for (
int j = 0;
j < 4;
j++) {
1075 uint32_t idx =
vc.kDungeonPalettesGroups + (i * 4) +
j;
1086 return absl::OkStatus();
1094 for (
int j = 0;
j < 8;
j++) {
1100 for (
int j = 0;
j < 4;
j++) {
1106 for (
int j = 0;
j < 4;
j++) {
1113 for (
int j = 0;
j < 4;
j++) {
1119 return absl::OkStatus();
1125 return absl::InternalError(
"ROM data is empty.");
1140 auto now = std::chrono::system_clock::now();
1141 auto now_c = std::chrono::system_clock::to_time_t(
now);
1156 std::filesystem::copy_options::overwrite_existing);
1157 }
catch (
const std::filesystem::filesystem_error&
e) {
1174 auto now = std::chrono::system_clock::now();
1175 auto now_c = std::chrono::system_clock::to_time_t(
now);
1187 std::cout <<
filename << std::endl;
1191 std::ofstream file(
filename.data(), std::ios::binary | std::ios::trunc);
1193 return absl::InternalError(
1194 absl::StrCat(
"Could not open ROM file for writing: ",
filename));
1200 static_cast<const char*
>(
static_cast<const void*
>(
rom_data_.data())),
1202 }
catch (
const std::ofstream::failure&
e) {
1203 return absl::InternalError(absl::StrCat(
1204 "Error while writing to ROM file: ",
filename,
" - ",
e.what()));
1209 return absl::InternalError(
1210 absl::StrCat(
"Error while writing to ROM file: ",
filename));
1220 for (
size_t j = 0;
j < palette.
size(); ++
j) {
1229 return absl::OkStatus();
1235 for (size_t i = 0; i < group.size(); ++i) {
1237 SavePalette(i, group.name(), *group.mutable_palette(i)));
1239 return absl::OkStatus();
1242 return absl::OkStatus();
1245absl::StatusOr<uint8_t> Rom::ReadByte(
int offset) {
1247 return absl::FailedPreconditionError(
"Offset cannot be negative");
1249 if (offset >=
static_cast<int>(rom_data_.size())) {
1250 return absl::FailedPreconditionError(
"Offset out of range");
1252 return rom_data_[offset];
1255absl::StatusOr<uint16_t> Rom::ReadWord(
int offset) {
1257 return absl::FailedPreconditionError(
"Offset cannot be negative");
1259 if (offset + 1 >=
static_cast<int>(rom_data_.size())) {
1260 return absl::FailedPreconditionError(
"Offset out of range");
1262 auto result = (uint16_t)(rom_data_[offset] | (rom_data_[offset + 1] << 8));
1266absl::StatusOr<uint32_t> Rom::ReadLong(
int offset) {
1268 return absl::OutOfRangeError(
"Offset cannot be negative");
1270 if (offset + 2 >=
static_cast<int>(rom_data_.size())) {
1271 return absl::OutOfRangeError(
"Offset out of range");
1273 auto result = (uint32_t)(rom_data_[offset] | (rom_data_[offset + 1] << 8) |
1274 (rom_data_[offset + 2] << 16));
1278absl::StatusOr<std::vector<uint8_t>> Rom::ReadByteVector(
1279 uint32_t offset, uint32_t length)
const {
1280 if (offset + length >
static_cast<uint32_t
>(rom_data_.size())) {
1281 return absl::OutOfRangeError(
"Offset and length out of range");
1283 std::vector<uint8_t> result;
1284 for (uint32_t i = offset; i < offset + length; i++) {
1285 result.push_back(rom_data_[i]);
1290absl::StatusOr<gfx::Tile16> Rom::ReadTile16(uint32_t tile16_id) {
1295 tile16.tile0_ = gfx::WordToTileInfo(new_tile0);
1298 tile16.tile1_ = gfx::WordToTileInfo(new_tile1);
1301 tile16.tile2_ = gfx::WordToTileInfo(new_tile2);
1304 tile16.tile3_ = gfx::WordToTileInfo(new_tile3);
1308absl::Status Rom::WriteTile16(
int tile16_id,
const gfx::Tile16& tile) {
1318 return absl::OkStatus();
1321absl::Status Rom::WriteByte(
int addr, uint8_t value) {
1322 if (addr >=
static_cast<int>(rom_data_.size())) {
1323 return absl::OutOfRangeError(absl::StrFormat(
1324 "Attempt to write byte %#02x value failed, address %d out of range",
1327 const uint8_t old_val = rom_data_[addr];
1328 rom_data_[addr] = value;
1329 LOG_DEBUG(
"Rom",
"WriteByte: %#06X: %s", addr, util::HexByte(value).data());
1331#ifdef __EMSCRIPTEN__
1332 MaybeBroadcastChange(addr, {old_val}, {value});
1334 return absl::OkStatus();
1337absl::Status Rom::WriteWord(
int addr, uint16_t value) {
1338 if (addr + 1 >=
static_cast<int>(rom_data_.size())) {
1339 return absl::OutOfRangeError(absl::StrFormat(
1340 "Attempt to write word %#04x value failed, address %d out of range",
1343 const uint8_t old0 = rom_data_[addr];
1344 const uint8_t old1 = rom_data_[addr + 1];
1345 rom_data_[addr] = (uint8_t)(value & 0xFF);
1346 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
1347 LOG_DEBUG(
"Rom",
"WriteWord: %#06X: %s", addr, util::HexWord(value).data());
1349#ifdef __EMSCRIPTEN__
1350 MaybeBroadcastChange(addr, {old0, old1},
1351 {
static_cast<uint8_t
>(value & 0xFF),
1352 static_cast<uint8_t
>((value >> 8) & 0xFF)});
1354 return absl::OkStatus();
1357absl::Status Rom::WriteShort(
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 short %#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",
"WriteShort: %#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::WriteLong(uint32_t addr, uint32_t value) {
1378 if (addr + 2 >=
static_cast<uint32_t
>(rom_data_.size())) {
1379 return absl::OutOfRangeError(absl::StrFormat(
1380 "Attempt to write long %#06x 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 const uint8_t old2 = rom_data_[addr + 2];
1386 rom_data_[addr] = (uint8_t)(value & 0xFF);
1387 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
1388 rom_data_[addr + 2] = (uint8_t)((value >> 16) & 0xFF);
1389 LOG_DEBUG(
"Rom",
"WriteLong: %#06X: %s", addr, util::HexLong(value).data());
1391#ifdef __EMSCRIPTEN__
1392 MaybeBroadcastChange(addr, {old0, old1, old2},
1393 {
static_cast<uint8_t
>(value & 0xFF),
1394 static_cast<uint8_t
>((value >> 8) & 0xFF),
1395 static_cast<uint8_t
>((value >> 16) & 0xFF)});
1397 return absl::OkStatus();
1400absl::Status Rom::WriteVector(
int addr, std::vector<uint8_t> data) {
1401 if (addr +
static_cast<int>(data.size()) >
1402 static_cast<int>(rom_data_.size())) {
1403 return absl::InvalidArgumentError(absl::StrFormat(
1404 "Attempt to write vector value failed, address %d out of range", addr));
1406 std::vector<uint8_t> old_data;
1407 old_data.reserve(data.size());
1408 for (
int i = 0; i < static_cast<int>(data.size()); i++) {
1409 old_data.push_back(rom_data_[addr + i]);
1411 for (
int i = 0; i < static_cast<int>(data.size()); i++) {
1412 rom_data_[addr + i] = data[i];
1414 LOG_DEBUG(
"Rom",
"WriteVector: %#06X: %s", addr,
1415 util::HexByte(data[0]).data());
1417#ifdef __EMSCRIPTEN__
1418 MaybeBroadcastChange(addr, old_data, data);
1420 return absl::OkStatus();
1423absl::Status Rom::WriteColor(uint32_t address,
const gfx::SnesColor& color) {
1424 uint16_t bgr = ((color.snes() >> 10) & 0x1F) | ((color.snes() & 0x1F) << 10) |
1425 (color.snes() & 0x7C00);
1428 LOG_DEBUG(
"Rom",
"WriteColor: %#06X: %s", address, util::HexWord(bgr).data());
1429 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.