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"
20#include "app/core/window.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 }
226 }
227 return absl::OkStatus();
228}
229
230absl::Status Rom::LoadFromFile(const std::string &filename, bool z3_load) {
231 if (filename.empty()) {
232 return absl::InvalidArgumentError(
233 "Could not load ROM: parameter `filename` is empty.");
234 }
235 filename_ = std::filesystem::absolute(filename).string();
236 short_name_ = filename_.substr(filename_.find_last_of("/\\") + 1);
237
238 std::ifstream file(filename_, std::ios::binary);
239 if (!file.is_open()) {
240 return absl::NotFoundError(
241 absl::StrCat("Could not open ROM file: ", filename_));
242 }
243
244 // Get file size and resize rom_data_
245 try {
246 size_ = std::filesystem::file_size(filename_);
247 } catch (const std::filesystem::filesystem_error &e) {
248 // Try to get the file size from the open file stream
249 file.seekg(0, std::ios::end);
250 if (!file) {
251 return absl::InternalError(absl::StrCat(
252 "Could not get file size: ", filename_, " - ", e.what()));
253 }
254 size_ = file.tellg();
255 }
256 rom_data_.resize(size_);
257
258 // Read file into rom_data_
259 file.read(reinterpret_cast<char *>(rom_data_.data()), size_);
260 file.close();
261
262 if (z3_load) {
264 resource_label_manager_.LoadLabels(absl::StrFormat("%s.labels", filename));
265 }
266
267 return absl::OkStatus();
268}
269
270absl::Status Rom::LoadFromData(const std::vector<uint8_t> &data, bool z3_load) {
271 if (data.empty()) {
272 return absl::InvalidArgumentError(
273 "Could not load ROM: parameter `data` is empty.");
274 }
275 rom_data_ = data;
276 size_ = data.size();
277 if (z3_load) {
279 }
280 return absl::OkStatus();
281}
282
283absl::Status Rom::LoadZelda3() {
284 // Check if the ROM has a header
285 constexpr size_t kBaseRomSize = 1048576; // 1MB
286 constexpr size_t kHeaderSize = 0x200; // 512 bytes
287 if (size_ % kBaseRomSize == kHeaderSize) {
288 auto header = std::vector<uint8_t>(rom_data_.begin(),
289 rom_data_.begin() + kHeaderSize);
290 rom_data_.erase(rom_data_.begin(), rom_data_.begin() + kHeaderSize);
291 size_ -= 0x200;
292 }
293
294 // Copy ROM title
295 constexpr uint32_t kTitleStringOffset = 0x7FC0;
296 constexpr uint32_t kTitleStringLength = 20;
297 title_.resize(kTitleStringLength);
298 std::copy(rom_data_.begin() + kTitleStringOffset,
299 rom_data_.begin() + kTitleStringOffset + kTitleStringLength,
300 title_.begin());
301 if (rom_data_[kTitleStringOffset + 0x19] == 0) {
303 } else {
305 }
306
307 // Load additional resources
309 // TODO Load gfx groups or expanded ZS values
311
312 // Expand the ROM data to 2MB without changing the data in the first 1MB
313 rom_data_.resize(kBaseRomSize * 2);
314 size_ = kBaseRomSize * 2;
315
316 return absl::OkStatus();
317}
318
319absl::Status Rom::LoadGfxGroups() {
320 ASSIGN_OR_RETURN(auto main_blockset_ptr, ReadWord(kGfxGroupsPointer));
321 main_blockset_ptr = SnesToPc(main_blockset_ptr);
322
323 for (uint32_t i = 0; i < kNumMainBlocksets; i++) {
324 for (int j = 0; j < 8; j++) {
325 main_blockset_ids[i][j] = rom_data_[main_blockset_ptr + (i * 8) + j];
326 }
327 }
328
329 for (uint32_t i = 0; i < kNumRoomBlocksets; i++) {
330 for (int j = 0; j < 4; j++) {
331 room_blockset_ids[i][j] = rom_data_[kEntranceGfxGroup + (i * 4) + j];
332 }
333 }
334
335 for (uint32_t i = 0; i < kNumSpritesets; i++) {
336 for (int j = 0; j < 4; j++) {
337 spriteset_ids[i][j] =
339 }
340 }
341
342 for (uint32_t i = 0; i < kNumPalettesets; i++) {
343 for (int j = 0; j < 4; j++) {
344 paletteset_ids[i][j] =
346 }
347 }
348
349 return absl::OkStatus();
350}
351
352absl::Status Rom::SaveGfxGroups() {
353 ASSIGN_OR_RETURN(auto main_blockset_ptr, ReadWord(kGfxGroupsPointer));
354 main_blockset_ptr = SnesToPc(main_blockset_ptr);
355
356 for (uint32_t i = 0; i < kNumMainBlocksets; i++) {
357 for (int j = 0; j < 8; j++) {
358 rom_data_[main_blockset_ptr + (i * 8) + j] = main_blockset_ids[i][j];
359 }
360 }
361
362 for (uint32_t i = 0; i < kNumRoomBlocksets; i++) {
363 for (int j = 0; j < 4; j++) {
364 rom_data_[kEntranceGfxGroup + (i * 4) + j] = room_blockset_ids[i][j];
365 }
366 }
367
368 for (uint32_t i = 0; i < kNumSpritesets; i++) {
369 for (int j = 0; j < 4; j++) {
371 spriteset_ids[i][j];
372 }
373 }
374
375 for (uint32_t i = 0; i < kNumPalettesets; i++) {
376 for (int j = 0; j < 4; j++) {
378 paletteset_ids[i][j];
379 }
380 }
381
382 return absl::OkStatus();
383}
384
385absl::Status Rom::SaveToFile(const SaveSettings &settings) {
386 absl::Status non_firing_status;
387 if (rom_data_.empty()) {
388 return absl::InternalError("ROM data is empty.");
389 }
390
391 std::string filename = settings.filename;
392 auto backup = settings.backup;
393 auto save_new = settings.save_new;
394
395 // Check if filename is empty
396 if (filename == "") {
398 }
399
400 // Check if backup is enabled
401 if (backup) {
402 // Create a backup file with timestamp in its name
403 auto now = std::chrono::system_clock::now();
404 auto now_c = std::chrono::system_clock::to_time_t(now);
405 std::string backup_filename =
406 absl::StrCat(filename, "_backup_", std::ctime(&now_c));
407
408 // Remove newline character from ctime()
409 backup_filename.erase(
410 std::remove(backup_filename.begin(), backup_filename.end(), '\n'),
411 backup_filename.end());
412
413 // Replace spaces with underscores
414 std::replace(backup_filename.begin(), backup_filename.end(), ' ', '_');
415
416 // Now, copy the original file to the backup file
417 try {
418 std::filesystem::copy(filename_, backup_filename,
419 std::filesystem::copy_options::overwrite_existing);
420 } catch (const std::filesystem::filesystem_error &e) {
421 non_firing_status = absl::InternalError(absl::StrCat(
422 "Could not create backup file: ", backup_filename, " - ", e.what()));
423 }
424 }
425
426 // Run the other save functions
427 if (settings.z3_save) {
428 if (core::FeatureFlags::get().kSaveAllPalettes)
430 if (core::FeatureFlags::get().kSaveGfxGroups)
432 }
433
434 if (save_new) {
435 // Create a file of the same name and append the date between the filename
436 // and file extension
437 auto now = std::chrono::system_clock::now();
438 auto now_c = std::chrono::system_clock::to_time_t(now);
439 auto filename_no_ext = filename.substr(0, filename.find_last_of("."));
440 std::cout << filename_no_ext << std::endl;
441 filename = absl::StrCat(filename_no_ext, "_", std::ctime(&now_c));
442 // Remove spaces from new_filename and replace with _
443 filename.erase(std::remove(filename.begin(), filename.end(), ' '),
444 filename.end());
445 // Remove newline character from ctime()
446 filename.erase(std::remove(filename.begin(), filename.end(), '\n'),
447 filename.end());
448 // Add the file extension back to the new_filename
449 filename = filename + ".sfc";
450 std::cout << filename << std::endl;
451 }
452
453 // Open the file for writing and truncate existing content
454 std::ofstream file(filename.data(), std::ios::binary | std::ios::trunc);
455 if (!file) {
456 return absl::InternalError(
457 absl::StrCat("Could not open ROM file for writing: ", filename));
458 }
459
460 // Save the data to the file
461 try {
462 file.write(
463 static_cast<const char *>(static_cast<const void *>(rom_data_.data())),
464 rom_data_.size());
465 } catch (const std::ofstream::failure &e) {
466 return absl::InternalError(absl::StrCat(
467 "Error while writing to ROM file: ", filename, " - ", e.what()));
468 }
469
470 // Check for write errors
471 if (!file) {
472 return absl::InternalError(
473 absl::StrCat("Error while writing to ROM file: ", filename));
474 }
475
476 if (non_firing_status.ok()) dirty_ = false;
477 return non_firing_status.ok() ? absl::OkStatus() : non_firing_status;
478}
479
480absl::Status Rom::SavePalette(int index, const std::string &group_name,
481 gfx::SnesPalette &palette) {
482 for (size_t j = 0; j < palette.size(); ++j) {
483 gfx::SnesColor color = palette[j];
484 // If the color is modified, save the color to the ROM
485 if (color.is_modified()) {
487 WriteColor(gfx::GetPaletteAddress(group_name, index, j), color));
488 color.set_modified(false); // Reset the modified flag after saving
489 }
490 }
491 return absl::OkStatus();
492}
493
494absl::Status Rom::SaveAllPalettes() {
496 palette_groups_.for_each([&](gfx::PaletteGroup &group) -> absl::Status {
497 for (size_t i = 0; i < group.size(); ++i) {
498 RETURN_IF_ERROR(
499 SavePalette(i, group.name(), *group.mutable_palette(i)));
500 }
501 return absl::OkStatus();
502 }));
503
504 return absl::OkStatus();
505}
506
507absl::StatusOr<uint8_t> Rom::ReadByte(int offset) {
508 if (offset >= static_cast<int>(rom_data_.size())) {
509 return absl::FailedPreconditionError("Offset out of range");
510 }
511 return rom_data_[offset];
512}
513
514absl::StatusOr<uint16_t> Rom::ReadWord(int offset) {
515 if (offset + 1 >= static_cast<int>(rom_data_.size())) {
516 return absl::FailedPreconditionError("Offset out of range");
517 }
518 auto result = (uint16_t)(rom_data_[offset] | (rom_data_[offset + 1] << 8));
519 return result;
520}
521
522absl::StatusOr<uint32_t> Rom::ReadLong(int offset) {
523 if (offset + 2 >= static_cast<int>(rom_data_.size())) {
524 return absl::OutOfRangeError("Offset out of range");
525 }
526 auto result = (uint32_t)(rom_data_[offset] | (rom_data_[offset + 1] << 8) |
527 (rom_data_[offset + 2] << 16));
528 return result;
529}
530
531absl::StatusOr<std::vector<uint8_t>> Rom::ReadByteVector(
532 uint32_t offset, uint32_t length) const {
533 if (offset + length > static_cast<uint32_t>(rom_data_.size())) {
534 return absl::OutOfRangeError("Offset and length out of range");
535 }
536 std::vector<uint8_t> result;
537 for (uint32_t i = offset; i < offset + length; i++) {
538 result.push_back(rom_data_[i]);
539 }
540 return result;
541}
542
543absl::StatusOr<gfx::Tile16> Rom::ReadTile16(uint32_t tile16_id) {
544 // Skip 8 bytes per tile.
545 auto tpos = kTile16Ptr + (tile16_id * 0x08);
546 gfx::Tile16 tile16 = {};
547 ASSIGN_OR_RETURN(auto new_tile0, ReadWord(tpos));
548 tile16.tile0_ = gfx::WordToTileInfo(new_tile0);
549 tpos += 2;
550 ASSIGN_OR_RETURN(auto new_tile1, ReadWord(tpos));
551 tile16.tile1_ = gfx::WordToTileInfo(new_tile1);
552 tpos += 2;
553 ASSIGN_OR_RETURN(auto new_tile2, ReadWord(tpos));
554 tile16.tile2_ = gfx::WordToTileInfo(new_tile2);
555 tpos += 2;
556 ASSIGN_OR_RETURN(auto new_tile3, ReadWord(tpos));
557 tile16.tile3_ = gfx::WordToTileInfo(new_tile3);
558 return tile16;
559}
560
561absl::Status Rom::WriteTile16(int tile16_id, const gfx::Tile16 &tile) {
562 // Skip 8 bytes per tile.
563 auto tpos = kTile16Ptr + (tile16_id * 0x08);
565 tpos += 2;
567 tpos += 2;
569 tpos += 2;
571 return absl::OkStatus();
572}
573
574absl::Status Rom::WriteByte(int addr, uint8_t value) {
575 if (addr >= static_cast<int>(rom_data_.size())) {
576 return absl::OutOfRangeError(absl::StrFormat(
577 "Attempt to write byte %#02x value failed, address %d out of range",
578 value, addr));
579 }
580 rom_data_[addr] = value;
581 util::logf("WriteByte: %#06X: %s", addr, util::HexByte(value).data());
582 dirty_ = true;
583 return absl::OkStatus();
584}
585
586absl::Status Rom::WriteWord(int addr, uint16_t value) {
587 if (addr + 1 >= static_cast<int>(rom_data_.size())) {
588 return absl::OutOfRangeError(absl::StrFormat(
589 "Attempt to write word %#04x value failed, address %d out of range",
590 value, addr));
591 }
592 rom_data_[addr] = (uint8_t)(value & 0xFF);
593 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
594 util::logf("WriteWord: %#06X: %s", addr, util::HexWord(value).data());
595 dirty_ = true;
596 return absl::OkStatus();
597}
598
599absl::Status Rom::WriteShort(int addr, uint16_t value) {
600 if (addr + 1 >= static_cast<int>(rom_data_.size())) {
601 return absl::OutOfRangeError(absl::StrFormat(
602 "Attempt to write short %#04x value failed, address %d out of range",
603 value, addr));
604 }
605 rom_data_[addr] = (uint8_t)(value & 0xFF);
606 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
607 util::logf("WriteShort: %#06X: %s", addr, util::HexWord(value).data());
608 dirty_ = true;
609 return absl::OkStatus();
610}
611
612absl::Status Rom::WriteLong(uint32_t addr, uint32_t value) {
613 if (addr + 2 >= static_cast<uint32_t>(rom_data_.size())) {
614 return absl::OutOfRangeError(absl::StrFormat(
615 "Attempt to write long %#06x value failed, address %d out of range",
616 value, addr));
617 }
618 rom_data_[addr] = (uint8_t)(value & 0xFF);
619 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
620 rom_data_[addr + 2] = (uint8_t)((value >> 16) & 0xFF);
621 util::logf("WriteLong: %#06X: %s", addr, util::HexLong(value).data());
622 dirty_ = true;
623 return absl::OkStatus();
624}
625
626absl::Status Rom::WriteVector(int addr, std::vector<uint8_t> data) {
627 if (addr + static_cast<int>(data.size()) >
628 static_cast<int>(rom_data_.size())) {
629 return absl::InvalidArgumentError(absl::StrFormat(
630 "Attempt to write vector value failed, address %d out of range", addr));
631 }
632 for (int i = 0; i < static_cast<int>(data.size()); i++) {
633 rom_data_[addr + i] = data[i];
634 }
635 util::logf("WriteVector: %#06X: %s", addr, util::HexByte(data[0]).data());
636 dirty_ = true;
637 return absl::OkStatus();
638}
639
640absl::Status Rom::WriteColor(uint32_t address, const gfx::SnesColor &color) {
641 uint16_t bgr = ((color.snes() >> 10) & 0x1F) | ((color.snes() & 0x1F) << 10) |
642 (color.snes() & 0x7C00);
643
644 // Write the 16-bit color value to the ROM at the specified address
645 util::logf("WriteColor: %#06X: %s", address, util::HexWord(bgr).data());
646 auto st = WriteShort(address, bgr);
647 if (st.ok()) dirty_ = true;
648 return st;
649}
650
651} // namespace yaze
static Renderer & Get()
Definition window.h:37
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:57
absl::StatusOr< std::vector< uint8_t > > ReadByteVector(uint32_t offset, uint32_t length) const
Definition rom.cc:531
zelda3_version version_
Definition rom.h:231
absl::Status LoadFromFile(const std::string &filename, bool z3_load=true)
Definition rom.cc:230
auto mutable_graphics_buffer()
Definition rom.h:187
auto begin()
Definition rom.h:180
std::vector< uint8_t > rom_data_
Definition rom.h:219
gfx::PaletteGroupMap palette_groups_
Definition rom.h:228
auto palette_group() const
Definition rom.h:188
absl::StatusOr< gfx::Tile16 > ReadTile16(uint32_t tile16_id)
Definition rom.cc:543
absl::Status WriteColor(uint32_t address, const gfx::SnesColor &color)
Definition rom.cc:640
auto filename() const
Definition rom.h:183
absl::Status LoadFromData(const std::vector< uint8_t > &data, bool z3_load=true)
Definition rom.cc:270
absl::Status WriteByte(int addr, uint8_t value)
Definition rom.cc:574
absl::Status LoadZelda3()
Definition rom.cc:283
std::array< std::array< uint8_t, 4 >, kNumSpritesets > spriteset_ids
Definition rom.h:202
std::array< std::array< uint8_t, 4 >, kNumPalettesets > paletteset_ids
Definition rom.h:203
absl::StatusOr< uint16_t > ReadWord(int offset)
Definition rom.cc:514
absl::Status WriteVector(int addr, std::vector< uint8_t > data)
Definition rom.cc:626
std::string title_
Definition rom.h:210
absl::Status WriteTile16(int tile16_id, const gfx::Tile16 &tile)
Definition rom.cc:561
absl::Status SaveToFile(const SaveSettings &settings)
Definition rom.cc:385
std::array< std::array< uint8_t, 4 >, kNumRoomBlocksets > room_blockset_ids
Definition rom.h:201
absl::StatusOr< uint32_t > ReadLong(int offset)
Definition rom.cc:522
auto data() const
Definition rom.h:178
zelda3_version_pointers version_constants() const
Definition rom.h:196
absl::Status SaveGfxGroups()
Definition rom.cc:352
ResourceLabelManager resource_label_manager_
Definition rom.h:225
absl::Status LoadGfxGroups()
Definition rom.cc:319
bool dirty_
Definition rom.h:234
std::string filename_
Definition rom.h:213
unsigned long size_
Definition rom.h:207
absl::Status SavePalette(int index, const std::string &group_name, gfx::SnesPalette &palette)
Definition rom.cc:480
absl::StatusOr< uint8_t > ReadByte(int offset)
Definition rom.cc:507
absl::Status WriteShort(int addr, uint16_t value)
Definition rom.cc:599
std::string short_name_
Definition rom.h:216
absl::Status WriteWord(int addr, uint16_t value)
Definition rom.cc:586
absl::Status WriteLong(uint32_t addr, uint32_t value)
Definition rom.cc:612
std::array< std::array< uint8_t, 8 >, kNumMainBlocksets > main_blockset_ids
Definition rom.h:200
absl::Status SaveAllPalettes()
Definition rom.cc:494
static Flags & get()
Definition features.h:66
The Renderer class represents the renderer for the Yaze application.
Definition window.h:35
void RenderBitmap(gfx::Bitmap *bitmap)
Definition window.h:56
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:53
#define ASSIGN_OR_RETURN(type_variable_name, expression)
Definition macro.h:61
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:12
constexpr uint32_t kGfxGroupsPointer
Definition rom.h:34
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:29
constexpr uint32_t kEntranceGfxGroup
Definition rom.h:40
constexpr uint32_t kNumSpritesets
Definition rom.h:38
int AddressFromBytes(uint8_t bank, uint8_t high, uint8_t low) noexcept
Definition snes.h:39
constexpr uint32_t kTile16Ptr
Definition rom.h:30
constexpr uint32_t kNumMainBlocksets
Definition rom.h:36
constexpr uint32_t kNumRoomBlocksets
Definition rom.h:37
absl::StatusOr< gfx::Bitmap > LoadFontGraphics(const Rom &rom)
Definition rom.cc:77
constexpr uint32_t kNumGfxSheets
Definition rom.h:28
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:39
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:63
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