Refactor Emulator and Snes classes for improved memory access

- Updated Emulator class to remove inheritance from SharedRom and streamline ROM handling.
- Refactored memory access methods in Emulator and Snes classes to use consistent naming conventions.
- Enhanced DungeonObjectRenderer to utilize the updated Snes class for CPU and memory operations, improving clarity and maintainability.
- Cleaned up unnecessary comments and improved code formatting for better readability.
This commit is contained in:
scawful
2025-05-15 22:53:37 -04:00
parent 86d72fe0ca
commit 1031509e8a
6 changed files with 78 additions and 116 deletions

View File

@@ -58,8 +58,8 @@ void Emulator::Run() {
}
rom_data_ = rom()->vector();
snes_.Init(rom_data_);
wanted_frames_ = 1.0 / (snes_.Memory().pal_timing() ? 50.0 : 60.0);
wanted_samples_ = 48000 / (snes_.Memory().pal_timing() ? 50 : 60);
wanted_frames_ = 1.0 / (snes_.memory().pal_timing() ? 50.0 : 60.0);
wanted_samples_ = 48000 / (snes_.memory().pal_timing() ? 50 : 60);
loaded = true;
count_frequency = SDL_GetPerformanceFrequency();
@@ -491,8 +491,8 @@ void Emulator::RenderMemoryViewer() {
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoScrollWithMouse)) {
mem_edit.DrawContents((void*)snes_.Memory().rom_.data(),
snes_.Memory().rom_.size());
mem_edit.DrawContents((void*)snes_.memory().rom_.data(),
snes_.memory().rom_.size());
ImGui::EndChild();
}

View File

@@ -36,7 +36,7 @@ struct EmulatorKeybindings {
* @class Emulator
* @brief A class for emulating and debugging SNES games.
*/
class Emulator : public SharedRom {
class Emulator {
public:
Emulator() {
std::string emulator_layout = R"(
@@ -92,7 +92,7 @@ class Emulator : public SharedRom {
{"cpu.PC", &snes_.cpu().PC},
{"cpu.status", &snes_.cpu().status},
{"snes.cycle_count", &snes_.mutable_cycles()},
{"cpu.SP", &snes_.Memory().mutable_sp()},
{"cpu.SP", &snes_.memory().mutable_sp()},
{"spc.A", &snes_.apu().spc700().A},
{"spc.X", &snes_.apu().spc700().X},
{"spc.Y", &snes_.apu().spc700().Y},
@@ -115,6 +115,8 @@ class Emulator : public SharedRom {
audio_device_ = audio_device;
}
auto wanted_samples() const -> int { return wanted_samples_; }
auto rom() { return rom_; }
auto mutable_rom() { return rom_; }
private:
void RenderNavBar();
@@ -153,6 +155,7 @@ class Emulator : public SharedRom {
int16_t* audio_buffer_;
SDL_AudioDeviceID audio_device_;
Rom* rom_;
Snes snes_;
SDL_Texture* ppu_texture_;

View File

@@ -30,27 +30,27 @@ typedef struct DmaChannel {
uint8_t b_addr;
uint16_t a_addr;
uint8_t a_bank;
uint16_t size; // also indirect hdma adr
uint8_t ind_bank; // hdma
uint16_t table_addr; // hdma
uint8_t rep_count; // hdma
uint16_t size; // also indirect hdma adr
uint8_t ind_bank; // hdma
uint16_t table_addr; // hdma
uint8_t rep_count; // hdma
uint8_t unusedByte;
bool dma_active;
bool hdma_active;
uint8_t mode;
bool fixed;
bool decrement;
bool indirect; // hdma
bool indirect; // hdma
bool from_b;
bool unusedBit;
bool do_transfer; // hdma
bool terminated; // hdma
bool do_transfer; // hdma
bool terminated; // hdma
} DmaChannel;
typedef struct CpuCallbacks {
std::function<uint8_t(uint32_t)> read_byte;
std::function<void(uint32_t, uint8_t)> write_byte;
std::function<void(bool waiting)> idle;
std::function<uint8_t(uint32_t)> read_byte = nullptr;
std::function<void(uint32_t, uint8_t)> write_byte = nullptr;
std::function<void(bool waiting)> idle = nullptr;
} CpuCallbacks;
constexpr uint32_t kROMStart = 0x008000;
@@ -62,7 +62,7 @@ constexpr uint32_t kRAMSize = 0x20000;
* @brief Memory interface
*/
class Memory {
public:
public:
virtual ~Memory() = default;
virtual uint8_t ReadByte(uint32_t address) const = 0;
virtual uint16_t ReadWord(uint32_t address) const = 0;
@@ -116,25 +116,25 @@ public:
*
*/
class MemoryImpl : public Memory {
public:
public:
void Initialize(const std::vector<uint8_t> &romData, bool verbose = false);
uint16_t GetHeaderOffset() {
uint16_t offset;
switch (memory_[(0x00 << 16) + 0xFFD5] & 0x07) {
case 0: // LoROM
offset = 0x7FC0;
break;
case 1: // HiROM
offset = 0xFFC0;
break;
case 5: // ExHiROM
offset = 0x40;
break;
default:
throw std::invalid_argument(
"Unable to locate supported ROM mapping mode in the provided ROM "
"file. Please try another ROM file.");
case 0: // LoROM
offset = 0x7FC0;
break;
case 1: // HiROM
offset = 0xFFC0;
break;
case 5: // ExHiROM
offset = 0x40;
break;
default:
throw std::invalid_argument(
"Unable to locate supported ROM mapping mode in the provided ROM "
"file. Please try another ROM file.");
}
return offset;
@@ -285,7 +285,7 @@ public:
std::vector<uint8_t> rom_;
std::vector<uint8_t> ram_;
private:
private:
uint32_t GetMappedAddress(uint32_t address) const;
bool verbose_ = false;
@@ -323,7 +323,7 @@ private:
std::vector<uint8_t> memory_;
};
} // namespace emu
} // namespace yaze
} // namespace emu
} // namespace yaze
#endif // YAZE_APP_EMU_MEMORY_H
#endif // YAZE_APP_EMU_MEMORY_H

View File

@@ -57,7 +57,7 @@ class Snes {
auto cpu() -> Cpu& { return cpu_; }
auto ppu() -> Ppu& { return ppu_; }
auto apu() -> Apu& { return apu_; }
auto Memory() -> MemoryImpl& { return memory_; }
auto memory() -> MemoryImpl& { return memory_; }
auto get_ram() -> uint8_t* { return ram; }
auto mutable_cycles() -> uint64_t& { return cycles_; }