match cpu registers on reset to hardware, cleanup

This commit is contained in:
scawful
2024-04-25 00:50:39 -04:00
parent b4f2fdc57e
commit d76525201f
6 changed files with 18 additions and 79 deletions

View File

@@ -21,8 +21,8 @@ void Cpu::Reset(bool hard) {
PB = 0;
D = 0;
DB = 0;
E = 0;
status = 0;
E = 1;
status = 0x34;
irq_wanted_ = false;
}
@@ -34,24 +34,6 @@ void Cpu::Reset(bool hard) {
int_delay_ = false;
}
void Cpu::Update(UpdateMode mode, int stepCount) {
int cycles = (mode == UpdateMode::Run) ? clock.GetCycleCount() : stepCount;
// Execute the calculated number of cycles
for (int i = 0; i < cycles; i++) {
if (IsBreakpoint(PC)) {
break;
}
// Fetch and execute an instruction
ExecuteInstruction(ReadByte((PB << 16) + PC));
if (mode == UpdateMode::Step) {
break;
}
}
}
void Cpu::RunOpcode() {
if (reset_wanted_) {
reset_wanted_ = false;

View File

@@ -36,18 +36,12 @@ class InstructionEntry {
std::string instruction; // Human-readable instruction text
};
const int kCpuClockSpeed = 21477272; // 21.477272 MHz
class Cpu : public Loggable, public core::ExperimentFlags {
public:
explicit Cpu(memory::Memory& mem, Clock& vclock,
memory::CpuCallbacks& callbacks)
: memory(mem), clock(vclock), callbacks_(callbacks) {}
enum class UpdateMode { Run, Step, Pause };
void Init(bool verbose = false) { clock.SetFrequency(kCpuClockSpeed); }
void Reset(bool hard = false);
void Update(UpdateMode mode = UpdateMode::Run, int stepCount = 1);
void RunOpcode();

View File

@@ -65,9 +65,9 @@ void Emulator::Run() {
wanted_samples_ = 48000 / (snes_.Memory().pal_timing() ? 50 : 60);
loaded = true;
countFreq = SDL_GetPerformanceFrequency();
lastCount = SDL_GetPerformanceCounter();
timeAdder = 0.0;
count_frequency = SDL_GetPerformanceFrequency();
last_count = SDL_GetPerformanceCounter();
time_adder = 0.0;
}
RenderNavBar();
@@ -75,14 +75,14 @@ void Emulator::Run() {
if (running_) {
HandleEvents();
uint64_t curCount = SDL_GetPerformanceCounter();
uint64_t delta = curCount - lastCount;
lastCount = curCount;
float seconds = delta / (float)countFreq;
timeAdder += seconds;
uint64_t current_count = SDL_GetPerformanceCounter();
uint64_t delta = current_count - last_count;
last_count = current_count;
float seconds = delta / (float)count_frequency;
time_adder += seconds;
// allow 2 ms earlier, to prevent skipping due to being just below wanted
while (timeAdder >= wanted_frames_ - 0.002) {
timeAdder -= wanted_frames_;
while (time_adder >= wanted_frames_ - 0.002) {
time_adder -= wanted_frames_;
if (loaded) {
if (turbo_mode_) {
@@ -220,9 +220,9 @@ void Emulator::RenderNavBar() {
// About Debugger logic
}
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("About Debugger");
}
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("About Debugger");
}
SameLine();
ImGui::Checkbox("Logging", snes_.cpu().mutable_log_instructions());

View File

@@ -133,9 +133,9 @@ class Emulator : public SharedRom {
uint16_t manual_pc_ = 0;
// timing
uint64_t countFreq;
uint64_t lastCount;
float timeAdder = 0.0;
uint64_t count_frequency;
uint64_t last_count;
float time_adder = 0.0;
int16_t* audio_buffer_;
SDL_AudioDeviceID audio_device_;

View File

@@ -36,7 +36,6 @@ uint8_t input_read(Input* input) {
void SNES::Init(std::vector<uint8_t>& rom_data) {
// Initialize the CPU, PPU, and APU
cpu_.Init();
ppu_.Init();
apu_.Init();

View File

@@ -25,39 +25,6 @@ class PpuInterface {
// Memory Interactions
virtual void Write(uint16_t address, uint8_t data) = 0;
virtual uint8_t Read(uint16_t address) const = 0;
// Rendering Controls
virtual void RenderFrame() = 0;
virtual void RenderScanline() = 0;
virtual void RenderBackground(int layer) = 0;
virtual void RenderSprites() = 0;
// State Management
virtual void Init() = 0;
virtual void Reset() = 0;
virtual void Update(double deltaTime) = 0;
virtual void UpdateClock(double deltaTime) = 0;
virtual void UpdateInternalState(int cycles) = 0;
// Data Access
virtual const std::vector<uint8_t>& GetFrameBuffer() const = 0;
virtual std::shared_ptr<gfx::Bitmap> GetScreen() const = 0;
// Mode and Setting Updates
virtual void UpdateModeSettings() = 0;
virtual void UpdateTileData() = 0;
virtual void UpdateTileMapData() = 0;
virtual void UpdatePaletteData() = 0;
// Layer Composition
virtual void ApplyEffects() = 0;
virtual void ComposeLayers() = 0;
// Display Output
virtual void DisplayFrameBuffer() = 0;
// Notification (Observer pattern)
virtual void Notify(uint32_t address, uint8_t data) = 0;
};
// Enum representing different background modes
@@ -289,8 +256,6 @@ struct BackgroundLayer {
bool enabled; // Whether the background layer is enabled
};
const int kPpuClockSpeed = 5369318; // 5.369318 MHz
class Ppu : public SharedRom {
public:
// Initializes the PPU with the necessary resources and dependencies
@@ -298,7 +263,6 @@ class Ppu : public SharedRom {
// Initialize the frame buffer
void Init() {
clock_.SetFrequency(kPpuClockSpeed);
frame_buffer_.resize(256 * 240, 0);
pixelOutputFormat = 1;
}