yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
rom.cc
Go to the documentation of this file.
1#include "rom.h"
2
3#include <algorithm>
4#include <array>
5#include <chrono>
6#include <cstddef>
7#include <cstdint>
8#include <cstring>
9#include <ctime>
10#include <filesystem>
11#include <fstream>
12#include <string>
13#include <vector>
14
15#include "absl/status/status.h"
16#include "absl/status/statusor.h"
17#include "absl/strings/str_cat.h"
18#include "absl/strings/string_view.h"
19#include "app/core/features.h"
21#include "app/gfx/compression.h"
22#include "app/gfx/snes_color.h"
24#include "app/gfx/snes_tile.h"
25#include "app/snes.h"
26#include "util/hex.h"
27#include "util/log.h"
28#include "util/macro.h"
29
30namespace yaze {
31using core::Renderer;
32constexpr int Uncompressed3BPPSize = 0x0600;
33
34uint32_t GetGraphicsAddress(const uint8_t *data, uint8_t addr, uint32_t ptr1,
35 uint32_t ptr2, uint32_t ptr3) {
36 return SnesToPc(AddressFromBytes(data[ptr1 + addr], data[ptr2 + addr],
37 data[ptr3 + addr]));
38}
39
40absl::StatusOr<std::vector<uint8_t>> Load2BppGraphics(const Rom &rom) {
41 std::vector<uint8_t> sheet;
42 const uint8_t sheets[] = {0x71, 0x72, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE};
43 for (const auto &sheet_id : sheets) {
44 auto offset = GetGraphicsAddress(rom.data(), sheet_id,
48 ASSIGN_OR_RETURN(auto decomp_sheet,
49 gfx::lc_lz2::DecompressV2(rom.data(), offset));
50 auto converted_sheet = gfx::SnesTo8bppSheet(decomp_sheet, 2);
51 for (const auto &each_pixel : converted_sheet) {
52 sheet.push_back(each_pixel);
53 }
54 }
55 return sheet;
56}
57
58absl::StatusOr<std::array<gfx::Bitmap, kNumLinkSheets>> LoadLinkGraphics(
59 const Rom &rom) {
60 const uint32_t kLinkGfxOffset = 0x80000; // $10:8000
61 const uint16_t kLinkGfxLength = 0x800; // 0x4000 or 0x7000?
62 std::array<gfx::Bitmap, kNumLinkSheets> link_graphics;
63 for (uint32_t i = 0; i < kNumLinkSheets; i++) {
65 auto link_sheet_data,
66 rom.ReadByteVector(/*offset=*/kLinkGfxOffset + (i * kLinkGfxLength),
67 /*length=*/kLinkGfxLength));
68 auto link_sheet_8bpp = gfx::SnesTo8bppSheet(link_sheet_data, /*bpp=*/4);
69 link_graphics[i].Create(gfx::kTilesheetWidth, gfx::kTilesheetHeight,
70 gfx::kTilesheetDepth, link_sheet_8bpp);
71 link_graphics[i].SetPalette(rom.palette_group().armors[0]);
72 Renderer::Get().RenderBitmap(&link_graphics[i]);
73 }
74 return link_graphics;
75}
76
77absl::StatusOr<gfx::Bitmap> LoadFontGraphics(const Rom &rom) {
78 std::vector<uint8_t> data(0x2000);
79 for (int i = 0; i < 0x2000; i++) {
80 data[i] = rom.data()[0x70000 + i];
81 }
82
83 std::vector<uint8_t> new_data(0x4000);
84 std::vector<uint8_t> mask = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
85 int sheet_position = 0;
86
87 // 8x8 tile
88 for (int s = 0; s < 4; s++) { // Per Sheet
89 for (int j = 0; j < 4; j++) { // Per Tile Line Y
90 for (int i = 0; i < 16; i++) { // Per Tile Line X
91 for (int y = 0; y < 8; y++) { // Per Pixel Line
92 uint8_t line_bits0 =
93 data[(y * 2) + (i * 16) + (j * 256) + sheet_position];
94 uint8_t line_bits1 =
95 data[(y * 2) + (i * 16) + (j * 256) + 1 + sheet_position];
96
97 for (int x = 0; x < 4; x++) { // Per Pixel X
98 uint8_t pixdata = 0;
99 uint8_t pixdata2 = 0;
100
101 if ((line_bits0 & mask[(x * 2)]) == mask[(x * 2)]) {
102 pixdata += 1;
103 }
104 if ((line_bits1 & mask[(x * 2)]) == mask[(x * 2)]) {
105 pixdata += 2;
106 }
107
108 if ((line_bits0 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
109 pixdata2 += 1;
110 }
111 if ((line_bits1 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
112 pixdata2 += 2;
113 }
114
115 new_data[(y * 64) + (x) + (i * 4) + (j * 512) + (s * 2048)] =
116 (uint8_t)((pixdata << 4) | pixdata2);
117 }
118 }
119 }
120 }
121
122 sheet_position += 0x400;
123 }
124
125 std::vector<uint8_t> fontgfx16_data(0x4000);
126 for (int i = 0; i < 0x4000; i++) {
127 fontgfx16_data[i] = new_data[i];
128 }
129
130 gfx::Bitmap font_gfx;
131 font_gfx.Create(128, 128, 64, fontgfx16_data);
132 return font_gfx;
133}
134
135absl::StatusOr<std::array<gfx::Bitmap, kNumGfxSheets>> LoadAllGraphicsData(
136 Rom &rom, bool defer_render) {
137 std::array<gfx::Bitmap, kNumGfxSheets> graphics_sheets;
138 std::vector<uint8_t> sheet;
139 bool bpp3 = false;
140
141 for (uint32_t i = 0; i < kNumGfxSheets; i++) {
142 if (i >= 115 && i <= 126) { // uncompressed sheets
143 sheet.resize(Uncompressed3BPPSize);
144 auto offset = GetGraphicsAddress(
148 std::copy(rom.data() + offset, rom.data() + offset + Uncompressed3BPPSize,
149 sheet.begin());
150 bpp3 = true;
151 } else if (i == 113 || i == 114 || i >= 218) {
152 bpp3 = false;
153 } else {
154 auto offset = GetGraphicsAddress(
158 ASSIGN_OR_RETURN(sheet, gfx::lc_lz2::DecompressV2(rom.data(), offset));
159 bpp3 = true;
160 }
161
162 if (bpp3) {
163 auto converted_sheet = gfx::SnesTo8bppSheet(sheet, 3);
164 graphics_sheets[i].Create(gfx::kTilesheetWidth, gfx::kTilesheetHeight,
165 gfx::kTilesheetDepth, converted_sheet);
166 if (graphics_sheets[i].is_active()) {
167 if (i > 115) {
168 // Apply sprites palette
169 graphics_sheets[i].SetPaletteWithTransparent(
170 rom.palette_group().global_sprites[0], 0);
171 } else {
172 graphics_sheets[i].SetPaletteWithTransparent(
173 rom.palette_group().dungeon_main[0], 0);
174 }
175 }
176
177 if (!defer_render) {
178 graphics_sheets[i].CreateTexture(Renderer::Get().renderer());
179 }
180
181 for (int j = 0; j < graphics_sheets[i].size(); ++j) {
182 rom.mutable_graphics_buffer()->push_back(graphics_sheets[i].at(j));
183 }
184
185 } else {
186 for (int j = 0; j < graphics_sheets[0].size(); ++j) {
187 rom.mutable_graphics_buffer()->push_back(0xFF);
188 }
189 }
190 }
191 return graphics_sheets;
192}
193
195 Rom &rom, std::array<gfx::Bitmap, kNumGfxSheets> &gfx_sheets) {
196 for (int i = 0; i < kNumGfxSheets; i++) {
197 if (gfx_sheets[i].is_active()) {
198 int to_bpp = 3;
199 std::vector<uint8_t> final_data;
200 bool compressed = true;
201 if (i >= 115 && i <= 126) {
202 compressed = false;
203 } else if (i == 113 || i == 114 || i >= 218) {
204 to_bpp = 2;
205 continue;
206 }
207
208 std::cout << "Sheet ID " << i << " BPP: " << to_bpp << std::endl;
209 auto sheet_data = gfx_sheets[i].vector();
210 std::cout << "Sheet data size: " << sheet_data.size() << std::endl;
211 final_data = gfx::Bpp8SnesToIndexed(sheet_data, 8);
212 int size = 0;
213 if (compressed) {
214 auto compressed_data = gfx::HyruleMagicCompress(
215 final_data.data(), final_data.size(), &size, 1);
216 for (int j = 0; j < size; j++) {
217 sheet_data[j] = compressed_data[j];
218 }
219 }
220 auto offset = GetGraphicsAddress(
224 std::copy(final_data.begin(), final_data.end(), rom.begin() + offset);
225 std::ranges::copy(final_data, rom.begin() + offset);
226 }
227 }
228 return absl::OkStatus();
229}
230
231absl::Status Rom::LoadFromFile(const std::string &filename, bool z3_load) {
232 if (filename.empty()) {
233 return absl::InvalidArgumentError(
234 "Could not load ROM: parameter `filename` is empty.");
235 }
236 filename_ = std::filesystem::absolute(filename).string();
237 short_name_ = filename_.substr(filename_.find_last_of("/\\") + 1);
238
239 std::ifstream file(filename_, std::ios::binary);
240 if (!file.is_open()) {
241 return absl::NotFoundError(
242 absl::StrCat("Could not open ROM file: ", filename_));
243 }
244
245 // Get file size and resize rom_data_
246 try {
247 size_ = std::filesystem::file_size(filename_);
248 } catch (const std::filesystem::filesystem_error &e) {
249 // Try to get the file size from the open file stream
250 file.seekg(0, std::ios::end);
251 if (!file) {
252 return absl::InternalError(absl::StrCat(
253 "Could not get file size: ", filename_, " - ", e.what()));
254 }
255 size_ = file.tellg();
256 }
257 rom_data_.resize(size_);
258
259 // Read file into rom_data_
260 file.read(reinterpret_cast<char *>(rom_data_.data()), size_);
261 file.close();
262
263 if (z3_load) {
265 resource_label_manager_.LoadLabels(absl::StrFormat("%s.labels", filename));
266 }
267
268 return absl::OkStatus();
269}
270
271absl::Status Rom::LoadFromData(const std::vector<uint8_t> &data, bool z3_load) {
272 if (data.empty()) {
273 return absl::InvalidArgumentError(
274 "Could not load ROM: parameter `data` is empty.");
275 }
276 rom_data_ = data;
277 size_ = data.size();
278 if (z3_load) {
280 }
281 return absl::OkStatus();
282}
283
284absl::Status Rom::LoadZelda3() {
285 // Check if the ROM has a header
286 constexpr size_t kBaseRomSize = 1048576; // 1MB
287 constexpr size_t kHeaderSize = 0x200; // 512 bytes
288 if (size_ % kBaseRomSize == kHeaderSize) {
289 auto header = std::vector<uint8_t>(rom_data_.begin(),
290 rom_data_.begin() + kHeaderSize);
291 rom_data_.erase(rom_data_.begin(), rom_data_.begin() + kHeaderSize);
292 size_ -= 0x200;
293 }
294
295 // Copy ROM title
296 constexpr uint32_t kTitleStringOffset = 0x7FC0;
297 constexpr uint32_t kTitleStringLength = 20;
298 title_.resize(kTitleStringLength);
299 std::copy(rom_data_.begin() + kTitleStringOffset,
300 rom_data_.begin() + kTitleStringOffset + kTitleStringLength,
301 title_.begin());
302 if (rom_data_[kTitleStringOffset + 0x19] == 0) {
304 } else {
306 }
307
308 // Load additional resources
310 // TODO Load gfx groups or expanded ZS values
312
313 // Expand the ROM data to 2MB without changing the data in the first 1MB
314 rom_data_.resize(kBaseRomSize * 2);
315 size_ = kBaseRomSize * 2;
316
317 return absl::OkStatus();
318}
319
320absl::Status Rom::LoadGfxGroups() {
321 ASSIGN_OR_RETURN(auto main_blockset_ptr, ReadWord(kGfxGroupsPointer));
322 main_blockset_ptr = SnesToPc(main_blockset_ptr);
323
324 for (uint32_t i = 0; i < kNumMainBlocksets; i++) {
325 for (int j = 0; j < 8; j++) {
326 main_blockset_ids[i][j] = rom_data_[main_blockset_ptr + (i * 8) + j];
327 }
328 }
329
330 for (uint32_t i = 0; i < kNumRoomBlocksets; i++) {
331 for (int j = 0; j < 4; j++) {
332 room_blockset_ids[i][j] = rom_data_[kEntranceGfxGroup + (i * 4) + j];
333 }
334 }
335
336 for (uint32_t i = 0; i < kNumSpritesets; i++) {
337 for (int j = 0; j < 4; j++) {
338 spriteset_ids[i][j] =
340 }
341 }
342
343 for (uint32_t i = 0; i < kNumPalettesets; i++) {
344 for (int j = 0; j < 4; j++) {
345 paletteset_ids[i][j] =
347 }
348 }
349
350 return absl::OkStatus();
351}
352
353absl::Status Rom::SaveGfxGroups() {
354 ASSIGN_OR_RETURN(auto main_blockset_ptr, ReadWord(kGfxGroupsPointer));
355 main_blockset_ptr = SnesToPc(main_blockset_ptr);
356
357 for (uint32_t i = 0; i < kNumMainBlocksets; i++) {
358 for (int j = 0; j < 8; j++) {
359 rom_data_[main_blockset_ptr + (i * 8) + j] = main_blockset_ids[i][j];
360 }
361 }
362
363 for (uint32_t i = 0; i < kNumRoomBlocksets; i++) {
364 for (int j = 0; j < 4; j++) {
365 rom_data_[kEntranceGfxGroup + (i * 4) + j] = room_blockset_ids[i][j];
366 }
367 }
368
369 for (uint32_t i = 0; i < kNumSpritesets; i++) {
370 for (int j = 0; j < 4; j++) {
372 spriteset_ids[i][j];
373 }
374 }
375
376 for (uint32_t i = 0; i < kNumPalettesets; i++) {
377 for (int j = 0; j < 4; j++) {
379 paletteset_ids[i][j];
380 }
381 }
382
383 return absl::OkStatus();
384}
385
386absl::Status Rom::SaveToFile(const SaveSettings &settings) {
387 absl::Status non_firing_status;
388 if (rom_data_.empty()) {
389 return absl::InternalError("ROM data is empty.");
390 }
391
392 std::string filename = settings.filename;
393 auto backup = settings.backup;
394 auto save_new = settings.save_new;
395
396 // Check if filename is empty
397 if (filename == "") {
399 }
400
401 // Check if backup is enabled
402 if (backup) {
403 // Create a backup file with timestamp in its name
404 auto now = std::chrono::system_clock::now();
405 auto now_c = std::chrono::system_clock::to_time_t(now);
406 std::string backup_filename =
407 absl::StrCat(filename, "_backup_", std::ctime(&now_c));
408
409 // Remove newline character from ctime()
410 backup_filename.erase(
411 std::remove(backup_filename.begin(), backup_filename.end(), '\n'),
412 backup_filename.end());
413
414 // Replace spaces with underscores
415 std::replace(backup_filename.begin(), backup_filename.end(), ' ', '_');
416
417 // Now, copy the original file to the backup file
418 try {
419 std::filesystem::copy(filename_, backup_filename,
420 std::filesystem::copy_options::overwrite_existing);
421 } catch (const std::filesystem::filesystem_error &e) {
422 non_firing_status = absl::InternalError(absl::StrCat(
423 "Could not create backup file: ", backup_filename, " - ", e.what()));
424 }
425 }
426
427 // Run the other save functions
428 if (settings.z3_save) {
429 if (core::FeatureFlags::get().kSaveAllPalettes)
431 if (core::FeatureFlags::get().kSaveGfxGroups)
433 }
434
435 if (save_new) {
436 // Create a file of the same name and append the date between the filename
437 // and file extension
438 auto now = std::chrono::system_clock::now();
439 auto now_c = std::chrono::system_clock::to_time_t(now);
440 auto filename_no_ext = filename.substr(0, filename.find_last_of("."));
441 std::cout << filename_no_ext << std::endl;
442 filename = absl::StrCat(filename_no_ext, "_", std::ctime(&now_c));
443 // Remove spaces from new_filename and replace with _
444 filename.erase(std::remove(filename.begin(), filename.end(), ' '),
445 filename.end());
446 // Remove newline character from ctime()
447 filename.erase(std::remove(filename.begin(), filename.end(), '\n'),
448 filename.end());
449 // Add the file extension back to the new_filename
450 filename = filename + ".sfc";
451 std::cout << filename << std::endl;
452 }
453
454 // Open the file that we know exists for writing
455 std::ofstream file(filename.data(), std::ios::binary | std::ios::app);
456 if (!file) {
457 // Create the file if it does not exist
458 file.open(filename.data(), std::ios::binary);
459 if (!file) {
460 return absl::InternalError(
461 absl::StrCat("Could not open or create ROM file: ", filename));
462 }
463 }
464
465 // Save the data to the file
466 try {
467 file.write(
468 static_cast<const char *>(static_cast<const void *>(rom_data_.data())),
469 rom_data_.size());
470 } catch (const std::ofstream::failure &e) {
471 return absl::InternalError(absl::StrCat(
472 "Error while writing to ROM file: ", filename, " - ", e.what()));
473 }
474
475 // Check for write errors
476 if (!file) {
477 return absl::InternalError(
478 absl::StrCat("Error while writing to ROM file: ", filename));
479 }
480
481 if (!non_firing_status.ok()) {
482 return non_firing_status;
483 }
484
485 return absl::OkStatus();
486}
487
488absl::Status Rom::SavePalette(int index, const std::string &group_name,
489 gfx::SnesPalette &palette) {
490 for (size_t j = 0; j < palette.size(); ++j) {
491 gfx::SnesColor color = palette[j];
492 // If the color is modified, save the color to the ROM
493 if (color.is_modified()) {
495 WriteColor(gfx::GetPaletteAddress(group_name, index, j), color));
496 color.set_modified(false); // Reset the modified flag after saving
497 }
498 }
499 return absl::OkStatus();
500}
501
502absl::Status Rom::SaveAllPalettes() {
504 palette_groups_.for_each([&](gfx::PaletteGroup &group) -> absl::Status {
505 for (size_t i = 0; i < group.size(); ++i) {
506 RETURN_IF_ERROR(
507 SavePalette(i, group.name(), *group.mutable_palette(i)));
508 }
509 return absl::OkStatus();
510 }));
511
512 return absl::OkStatus();
513}
514
515absl::StatusOr<uint8_t> Rom::ReadByte(int offset) {
516 if (offset >= static_cast<int>(rom_data_.size())) {
517 return absl::FailedPreconditionError("Offset out of range");
518 }
519 return rom_data_[offset];
520}
521
522absl::StatusOr<uint16_t> Rom::ReadWord(int offset) {
523 if (offset + 1 >= static_cast<int>(rom_data_.size())) {
524 return absl::FailedPreconditionError("Offset out of range");
525 }
526 auto result = (uint16_t)(rom_data_[offset] | (rom_data_[offset + 1] << 8));
527 return result;
528}
529
530absl::StatusOr<uint32_t> Rom::ReadLong(int offset) {
531 if (offset + 2 >= static_cast<int>(rom_data_.size())) {
532 return absl::OutOfRangeError("Offset out of range");
533 }
534 auto result = (uint32_t)(rom_data_[offset] | (rom_data_[offset + 1] << 8) |
535 (rom_data_[offset + 2] << 16));
536 return result;
537}
538
539absl::StatusOr<std::vector<uint8_t>> Rom::ReadByteVector(
540 uint32_t offset, uint32_t length) const {
541 if (offset + length > static_cast<uint32_t>(rom_data_.size())) {
542 return absl::OutOfRangeError("Offset and length out of range");
543 }
544 std::vector<uint8_t> result;
545 for (uint32_t i = offset; i < offset + length; i++) {
546 result.push_back(rom_data_[i]);
547 }
548 return result;
549}
550
551absl::StatusOr<gfx::Tile16> Rom::ReadTile16(uint32_t tile16_id) {
552 // Skip 8 bytes per tile.
553 auto tpos = kTile16Ptr + (tile16_id * 0x08);
554 gfx::Tile16 tile16 = {};
555 ASSIGN_OR_RETURN(auto new_tile0, ReadWord(tpos));
556 tile16.tile0_ = gfx::WordToTileInfo(new_tile0);
557 tpos += 2;
558 ASSIGN_OR_RETURN(auto new_tile1, ReadWord(tpos));
559 tile16.tile1_ = gfx::WordToTileInfo(new_tile1);
560 tpos += 2;
561 ASSIGN_OR_RETURN(auto new_tile2, ReadWord(tpos));
562 tile16.tile2_ = gfx::WordToTileInfo(new_tile2);
563 tpos += 2;
564 ASSIGN_OR_RETURN(auto new_tile3, ReadWord(tpos));
565 tile16.tile3_ = gfx::WordToTileInfo(new_tile3);
566 return tile16;
567}
568
569absl::Status Rom::WriteTile16(int tile16_id, const gfx::Tile16 &tile) {
570 // Skip 8 bytes per tile.
571 auto tpos = kTile16Ptr + (tile16_id * 0x08);
573 tpos += 2;
575 tpos += 2;
577 tpos += 2;
579 return absl::OkStatus();
580}
581
582absl::Status Rom::WriteByte(int addr, uint8_t value) {
583 if (addr >= static_cast<int>(rom_data_.size())) {
584 return absl::OutOfRangeError(absl::StrFormat(
585 "Attempt to write byte %#02x value failed, address %d out of range",
586 value, addr));
587 }
588 rom_data_[addr] = value;
589 util::logf("WriteByte: %#06X: %s", addr, util::HexByte(value).data());
590 return absl::OkStatus();
591}
592
593absl::Status Rom::WriteWord(int addr, uint16_t value) {
594 if (addr + 1 >= static_cast<int>(rom_data_.size())) {
595 return absl::OutOfRangeError(absl::StrFormat(
596 "Attempt to write word %#04x value failed, address %d out of range",
597 value, addr));
598 }
599 rom_data_[addr] = (uint8_t)(value & 0xFF);
600 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
601 util::logf("WriteWord: %#06X: %s", addr, util::HexWord(value).data());
602 return absl::OkStatus();
603}
604
605absl::Status Rom::WriteShort(int addr, uint16_t value) {
606 if (addr + 1 >= static_cast<int>(rom_data_.size())) {
607 return absl::OutOfRangeError(absl::StrFormat(
608 "Attempt to write short %#04x value failed, address %d out of range",
609 value, addr));
610 }
611 rom_data_[addr] = (uint8_t)(value & 0xFF);
612 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
613 util::logf("WriteShort: %#06X: %s", addr, util::HexWord(value).data());
614 return absl::OkStatus();
615}
616
617absl::Status Rom::WriteLong(uint32_t addr, uint32_t value) {
618 if (addr + 2 >= static_cast<uint32_t>(rom_data_.size())) {
619 return absl::OutOfRangeError(absl::StrFormat(
620 "Attempt to write long %#06x value failed, address %d out of range",
621 value, addr));
622 }
623 rom_data_[addr] = (uint8_t)(value & 0xFF);
624 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
625 rom_data_[addr + 2] = (uint8_t)((value >> 16) & 0xFF);
626 util::logf("WriteLong: %#06X: %s", addr, util::HexLong(value).data());
627 return absl::OkStatus();
628}
629
630absl::Status Rom::WriteVector(int addr, std::vector<uint8_t> data) {
631 if (addr + static_cast<int>(data.size()) >
632 static_cast<int>(rom_data_.size())) {
633 return absl::InvalidArgumentError(absl::StrFormat(
634 "Attempt to write vector value failed, address %d out of range", addr));
635 }
636 for (int i = 0; i < static_cast<int>(data.size()); i++) {
637 rom_data_[addr + i] = data[i];
638 }
639 util::logf("WriteVector: %#06X: %s", addr, util::HexByte(data[0]).data());
640 return absl::OkStatus();
641}
642
643absl::Status Rom::WriteColor(uint32_t address, const gfx::SnesColor &color) {
644 uint16_t bgr = ((color.snes() >> 10) & 0x1F) | ((color.snes() & 0x1F) << 10) |
645 (color.snes() & 0x7C00);
646
647 // Write the 16-bit color value to the ROM at the specified address
648 util::logf("WriteColor: %#06X: %s", address, util::HexWord(bgr).data());
649 return WriteShort(address, bgr);
650}
651
652std::shared_ptr<Rom> SharedRom::shared_rom_ = nullptr;
653
654} // namespace yaze
static Renderer & Get()
Definition renderer.h:26
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:58
absl::StatusOr< std::vector< uint8_t > > ReadByteVector(uint32_t offset, uint32_t length) const
Definition rom.cc:539
zelda3_version version_
Definition rom.h:230
absl::Status LoadFromFile(const std::string &filename, bool z3_load=true)
Definition rom.cc:231
auto mutable_graphics_buffer()
Definition rom.h:186
auto begin()
Definition rom.h:179
std::vector< uint8_t > rom_data_
Definition rom.h:218
gfx::PaletteGroupMap palette_groups_
Definition rom.h:227
auto palette_group() const
Definition rom.h:187
absl::StatusOr< gfx::Tile16 > ReadTile16(uint32_t tile16_id)
Definition rom.cc:551
absl::Status WriteColor(uint32_t address, const gfx::SnesColor &color)
Definition rom.cc:643
auto filename() const
Definition rom.h:182
absl::Status LoadFromData(const std::vector< uint8_t > &data, bool z3_load=true)
Definition rom.cc:271
absl::Status WriteByte(int addr, uint8_t value)
Definition rom.cc:582
absl::Status LoadZelda3()
Definition rom.cc:284
std::array< std::array< uint8_t, 4 >, kNumSpritesets > spriteset_ids
Definition rom.h:201
std::array< std::array< uint8_t, 4 >, kNumPalettesets > paletteset_ids
Definition rom.h:202
absl::StatusOr< uint16_t > ReadWord(int offset)
Definition rom.cc:522
absl::Status WriteVector(int addr, std::vector< uint8_t > data)
Definition rom.cc:630
std::string title_
Definition rom.h:209
absl::Status WriteTile16(int tile16_id, const gfx::Tile16 &tile)
Definition rom.cc:569
absl::Status SaveToFile(const SaveSettings &settings)
Definition rom.cc:386
std::array< std::array< uint8_t, 4 >, kNumRoomBlocksets > room_blockset_ids
Definition rom.h:200
absl::StatusOr< uint32_t > ReadLong(int offset)
Definition rom.cc:530
auto data() const
Definition rom.h:177
zelda3_version_pointers version_constants() const
Definition rom.h:195
absl::Status SaveGfxGroups()
Definition rom.cc:353
ResourceLabelManager resource_label_manager_
Definition rom.h:224
absl::Status LoadGfxGroups()
Definition rom.cc:320
std::string filename_
Definition rom.h:212
unsigned long size_
Definition rom.h:206
absl::Status SavePalette(int index, const std::string &group_name, gfx::SnesPalette &palette)
Definition rom.cc:488
absl::StatusOr< uint8_t > ReadByte(int offset)
Definition rom.cc:515
absl::Status WriteShort(int addr, uint16_t value)
Definition rom.cc:605
std::string short_name_
Definition rom.h:215
absl::Status WriteWord(int addr, uint16_t value)
Definition rom.cc:593
absl::Status WriteLong(uint32_t addr, uint32_t value)
Definition rom.cc:617
std::array< std::array< uint8_t, 8 >, kNumMainBlocksets > main_blockset_ids
Definition rom.h:199
absl::Status SaveAllPalettes()
Definition rom.cc:502
static std::shared_ptr< Rom > shared_rom_
Definition rom.h:306
static Flags & get()
Definition features.h:66
The Renderer class represents the renderer for the Yaze application.
Definition renderer.h:24
void RenderBitmap(gfx::Bitmap *bitmap)
Definition renderer.h:45
Represents a bitmap image.
Definition bitmap.h:59
void Create(int width, int height, int depth, std::span< uint8_t > data)
Create a bitmap with the given dimensions and data.
Definition bitmap.cc:218
SNES Color container.
Definition snes_color.h:38
constexpr void set_modified(bool m)
Definition snes_color.h:80
constexpr bool is_modified() const
Definition snes_color.h:77
constexpr uint16_t snes() const
Definition snes_color.h:76
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
Tile composition of four 8x8 tiles.
Definition snes_tile.h:137
#define RETURN_IF_ERROR(expression)
Definition macro.h:51
#define ASSIGN_OR_RETURN(type_variable_name, expression)
Definition macro.h:59
absl::StatusOr< std::vector< uint8_t > > DecompressV2(const uint8_t *data, int offset, int size, int mode)
Decompresses a buffer of data using the LC_LZ2 algorithm.
constexpr int kTilesheetHeight
Definition snes_tile.h:17
constexpr int kTilesheetWidth
Definition snes_tile.h:16
uint16_t TileInfoToWord(TileInfo tile_info)
Definition snes_tile.cc:291
constexpr int kTilesheetDepth
Definition snes_tile.h:18
std::vector< uint8_t > Bpp8SnesToIndexed(std::vector< uint8_t > data, uint64_t bpp)
Definition snes_tile.cc:189
std::vector< uint8_t > SnesTo8bppSheet(std::span< uint8_t > sheet, int bpp, int num_sheets)
Definition snes_tile.cc:129
uint32_t GetPaletteAddress(const std::string &group_name, size_t palette_index, size_t color_index)
TileInfo WordToTileInfo(uint16_t word)
Definition snes_tile.cc:308
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.
std::string HexWord(uint16_t word, HexStringParams params)
Definition hex.cc:43
std::string HexByte(uint8_t byte, HexStringParams params)
Definition hex.cc:30
std::string HexLong(uint32_t dword, HexStringParams params)
Definition hex.cc:56
Main namespace for the application.
Definition controller.cc:18
constexpr uint32_t kGfxGroupsPointer
Definition rom.h:35
absl::StatusOr< std::vector< uint8_t > > Load2BppGraphics(const Rom &rom)
Loads 2bpp graphics from Rom data.
Definition rom.cc:40
absl::StatusOr< std::array< gfx::Bitmap, kNumLinkSheets > > LoadLinkGraphics(const Rom &rom)
Loads the players 4bpp graphics sheet from Rom data.
Definition rom.cc:58
constexpr int Uncompressed3BPPSize
Definition rom.cc:32
constexpr uint32_t kNumLinkSheets
Definition rom.h:30
constexpr uint32_t kEntranceGfxGroup
Definition rom.h:41
constexpr uint32_t kNumSpritesets
Definition rom.h:39
int AddressFromBytes(uint8_t bank, uint8_t high, uint8_t low) noexcept
Definition snes.h:39
constexpr uint32_t kTile16Ptr
Definition rom.h:31
constexpr uint32_t kNumMainBlocksets
Definition rom.h:37
constexpr uint32_t kNumRoomBlocksets
Definition rom.h:38
absl::StatusOr< gfx::Bitmap > LoadFontGraphics(const Rom &rom)
Definition rom.cc:77
constexpr uint32_t kNumGfxSheets
Definition rom.h:29
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....
Definition rom.cc:135
uint32_t GetGraphicsAddress(const uint8_t *data, uint8_t addr, uint32_t ptr1, uint32_t ptr2, uint32_t ptr3)
Definition rom.cc:34
constexpr uint32_t kNumPalettesets
Definition rom.h:40
absl::Status SaveAllGraphicsData(Rom &rom, std::array< gfx::Bitmap, kNumGfxSheets > &gfx_sheets)
Definition rom.cc:194
uint32_t SnesToPc(uint32_t addr) noexcept
Definition snes.h:8
std::string filename
Definition rom.h:64
Represents a group of palettes.
uint32_t kOverworldGfxPtr1
Definition zelda.h:34
uint32_t kOverworldGfxPtr2
Definition zelda.h:35
uint32_t kSpriteBlocksetPointer
Definition zelda.h:41
uint32_t kOverworldGfxPtr3
Definition zelda.h:36
uint32_t kDungeonPalettesGroups
Definition zelda.h:42
@ US
Definition zelda.h:15
@ JP
Definition zelda.h:16