#pragma once #include #include "NintendulatorLog.h" #include "BoardLog.h" namespace Breaknes { struct RGB_Triplet { uint8_t r; uint8_t g; uint8_t b; }; class Board { protected: // These basic chips are on all variations of motherboards, so we make them available to all inherited classes. M6502Core::M6502* core = nullptr; APUSim::APU* apu = nullptr; /// /// All PPUs of the motherboard. The primary PPU (the one that is wired into /// the board logic, drives NMI, VRAM, etc.) is ppus[0] and is aliased as `ppu`. /// Additional PPUs are simulated in parallel with the same inputs and buses, /// which allows comparing video output of different PPU revisions side by side. /// std::vector ppus; PPUSim::PPU* ppu = nullptr; const uint16_t MappedAPUBase = 0x4000; const uint16_t MappedAPUMask = 0x1f; const uint16_t MappedPPUBase = 0x2000; const uint16_t MappedPPUMask = 0x7; const uint64_t JustAboutOneSecond = 20'000'000; BaseLogic::TriState CLK = BaseLogic::TriState::Zero; // CPU Bus uint8_t data_bus = 0; bool data_bus_dirty = false; uint16_t addr_bus = 0; APUSim::AudioOutSignal aux{}; /// /// One video sample per PPU (filled by the board Step()). /// std::vector vidSamples{}; // The cartridge slot supports hotplugging during simulation. CartPcb::Cartridge* cart = nullptr; CartPcb::ConnectorType p1_type = CartPcb::ConnectorType::None; // Pre-calculated PPU palettes (one per PPU, since the palette depends on the PPU revision) std::vector pals{}; std::vector pal_cached{}; /// /// TV Set binding: tv_binding[tv] = index of the PPU whose video signal is /// displayed on this TV, or -1 if the TV is not connected. Up to 2 TVs are /// supported. By default TV[i] shows PPU[i]; for debugging one PPU can be /// bound to both TVs at once. /// int tv_binding[2] = { -1, -1 }; BaseLogic::TriState gnd = BaseLogic::TriState::Zero; BaseLogic::TriState vdd = BaseLogic::TriState::One; RegDumper* ppu_regdump = nullptr; RegDumper* apu_regdump = nullptr; size_t prev_phi_counter_for_ppuregdump = 0; size_t prev_phi_counter_for_apuregdump = 0; size_t prev_phi_counter_for_apulog = 0; // one APU register log line per CPU cycle // Nintendulator-compatible instruction trace log NintendulatorLog* nintendulator_log = nullptr; BaseLogic::TriState prev_phi0_for_nintendulator_log = BaseLogic::TriState::Zero; static uint8_t ReadCPUMemCallback(void* opaque, uint16_t addr); void TreatCoreForNintendulatorLog(uint16_t addr_bus); void TreatCoreForRegdump(uint16_t addr_bus, uint8_t data_bus, BaseLogic::TriState phi2, BaseLogic::TriState rnw); public: Board(APUSim::Revision apu_rev, std::vector ppu_revs, CartPcb::ConnectorType p1); virtual ~Board(); /// /// Create one more PPU instance on the board (the first one is the primary PPU). /// void AddPPU(PPUSim::Revision rev); /// /// IO Subsystem /// IO::IOSubsystem* io = nullptr; /// /// Simulate 1 half cycle of the board. The simulation of the signal edge is not supported, this is overkill. /// virtual void Step() = 0; /// /// "Insert" the cartridge as a .nes ROM. In this implementation we are simply trying to instantiate an NROM, but in a more advanced emulation, Cartridge Factory will take care of "inserting" the cartridge. /// /// A pointer to the ROM image in memory. /// ROM image size in bytes. /// virtual int InsertCartridge(uint8_t* nesImage, size_t nesImageSize); /// /// Remove the cartridge. Logically this means that all terminals associated with the cartridge take the value of `z`. /// virtual void EjectCartridge(); /// /// Make the board /RES pins = 0 for a few CLK half cycles so that the APU/PPU resets all of its internal circuits. /// virtual void Reset(); /// /// The parent application can check that the board is in the reset process and ignore the audio/video signal for that time. /// /// virtual bool InResetState(); /// /// Get the values of the ACLK cycle counter. /// /// virtual size_t GetACLKCounter(); /// /// Get the value of the 6502 core cycle counter (PHI Counter) /// /// virtual size_t GetPHICounter(); /// /// Get the current resulting AUX value in normalized [0.0; 1.0] format. /// /// virtual void SampleAudioSignal(float* sample); /// /// Load APU/PPU registers dump (APUPlayer/PPUPlayer only) /// /// RegDumpEntry records /// Dump size (bytes) virtual void LoadRegDump(uint8_t* data, size_t data_size); /// /// Enable/disable saving the history of PPU register accesses. /// virtual void EnablePpuRegDump(bool enable, char* regdump_dir); /// /// Enable/disable saving the history of APU register accesses. /// virtual void EnableApuRegDump(bool enable, char* regdump_dir); /// /// Enable/disable writing the Nintendulator-compatible instruction trace log /// to the "Nintendulator.log" file in the working directory. /// virtual void EnableNintendulatorLog(bool enable); /// /// Get audio signal settings that help with its rendering on the consumer side. /// /// virtual void GetApuSignalFeatures(APUSim::AudioSignalFeatures* features); /// /// Get the "pixel" counter. Keep in mind that pixels refers to an abstract entity representing the visible or invisible part of the video signal. /// /// virtual size_t GetPCLKCounter(); /// /// Get the number of PPUs on the board. /// /// size_t GetPPUCount(); /// /// Bind the video signal of a PPU to a virtual TV Set (up to 2 TVs). /// `bind == false` disconnects the TV (it shows black). /// By default TV[i] is bound to PPU[i]. /// void BindPPUToTV(size_t ppu_index, size_t tv_index, bool bind); /// /// Get the PPU index currently bound to the TV, or -1 if the TV is disconnected. /// int GetTVBinding(size_t tv_index); /// /// Get the number of connected (bound) TVs. Unbound trailing TVs are not counted. /// int GetTVCount(); /// /// Get 1 sample of the video signal displayed on the given TV. /// /// TV index (0..1) /// void SampleVideoSignal(size_t tv_index, PPUSim::VideoOutSignal* sample); /// /// Get 1 sample of the video signal (of the TV bound to the primary PPU). /// /// void SampleVideoSignal(PPUSim::VideoOutSignal* sample); /// /// Get the direct value from the PPU H counter. /// /// virtual size_t GetHCounter(); /// /// Get the direct value from the PPU V counter. /// /// virtual size_t GetVCounter(); /// /// Forcibly enable rendering ($2001[3] = $2001[4] always equals 1). /// Used for debugging PPU signals, when the CPU I/F register dump is limited, or when you want to get faster simulation results. /// Keep in mind that with permanently enabled rendering the PPU becomes unstable and this hack should be applied when you know what you're doing. /// /// virtual void RenderAlwaysEnabled(bool enable); /// /// Get video signal settings of the given PPU that help with its rendering on the consumer side. /// /// PPU index /// void GetPpuSignalFeatures(size_t ppu_index, PPUSim::VideoSignalFeatures* features); /// /// Get video signal settings of the primary PPU that help with its rendering on the consumer side. /// /// void GetPpuSignalFeatures(PPUSim::VideoSignalFeatures* features); /// /// Convert the raw color of the given PPU to RGB. Can be used for palette generation or PPU video output in RAW mode. /// The SYNC level (RAW.Sync) check must be done from the outside. /// void ConvertRAWToRGB(size_t ppu_index, uint16_t raw, uint8_t* r, uint8_t* g, uint8_t* b); /// /// Convert the raw color of the primary PPU to RGB. Can be used for palette generation or PPU video output in RAW mode. /// The SYNC level (RAW.Sync) check must be done from the outside. /// void ConvertRAWToRGB(uint16_t raw, uint8_t* r, uint8_t* g, uint8_t* b); /// /// Use RAW color output. /// RAW color refers to the Chroma/Luma combination that comes to the video generator and the Emphasis bit combination. /// /// virtual void SetRAWColorMode(bool enable); /// /// Set one of the ways to decay OAM cells. /// virtual void SetOamDecayBehavior(PPUSim::OAMDecayBehavior behavior); /// /// Set the noise for the composite video signal (in volts). /// /// virtual void SetNoiseLevel(float volts); /// /// Use externally set DAC signal levels. /// /// true: Use externally set table, false: Use internal values (see video_out.cpp class constructor) /// signal value table virtual void PpuUseExternalDacLevels(bool use, PPUSim::DacLevels& tab); /// /// Return all core debugging information for BreaksDebug. /// /// virtual void GetAllCoreDebugInfo(M6502Core::DebugInfo* info); /// /// Side-effect-free read of one byte from the CPU address space. Used by the /// Nintendulator log disassembler. Implemented on top of the debug interfaces, /// so it does not disturb the ongoing simulation. /// uint8_t ReadCPUMem(uint16_t addr); /// /// Side-effect-free read of one byte from the board WRAM. The address is a /// CPU bus address inside the $0000-$1FFF window. /// virtual uint8_t ReadWRAM(uint16_t addr); /// /// Set the log category mask of one subsystem (one bit per subsystem category). /// The chips (core/APU/PPU/MMC1) are configured through their public methods; /// the other sources are configured through the global Log manager. /// void SetLogMask(Log::Source src, uint64_t mask); }; }