yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
apu_ipl_handshake_test.cc
Go to the documentation of this file.
1#include <gtest/gtest.h>
2
3#include "app/emu/audio/apu.h"
6
7namespace yaze {
8namespace emu {
9
10class ApuIplHandshakeTest : public ::testing::Test {
11protected:
14
15 void SetUp() override {
16 std::vector<uint8_t> dummy_rom(0x200000, 0);
17 mem.Initialize(dummy_rom);
18 apu = new Apu(mem);
19 apu->Init();
20 apu->Reset();
21 }
22
23 void TearDown() override { delete apu; }
24};
25
26TEST_F(ApuIplHandshakeTest, SPC700StartsAtIplRomEntry) {
27 // After reset, PC should be at IPL ROM reset vector
28 uint16_t reset_vector = apu->spc700().read(0xFFFE) |
29 (apu->spc700().read(0xFFFF) << 8);
30
31 // The IPL ROM reset vector should point to 0xFFC0 (start of IPL ROM)
32 EXPECT_EQ(reset_vector, 0xFFC0);
33}
34
35TEST_F(ApuIplHandshakeTest, IplRomReadable) {
36 // IPL ROM should be readable at 0xFFC0-0xFFFF after reset
37 uint8_t first_byte = apu->Read(0xFFC0);
38
39 // First byte of IPL ROM should be 0xCD (CMP Y, #$EF)
40 EXPECT_EQ(first_byte, 0xCD);
41}
42
43TEST_F(ApuIplHandshakeTest, CycleTrackingWorks) {
44 // Execute one SPC700 opcode
45 apu->spc700().RunOpcode();
46
47 // GetLastOpcodeCycles should return a valid cycle count (2-12 typically)
48 int cycles = apu->spc700().GetLastOpcodeCycles();
49 EXPECT_GT(cycles, 0);
50 EXPECT_LE(cycles, 12);
51}
52
53TEST_F(ApuIplHandshakeTest, PortReadWrite) {
54 // Write to input port from CPU side (simulating CPU writes to $2140-$2143)
55 apu->in_ports_[0] = 0xAA;
56 apu->in_ports_[1] = 0xBB;
57
58 // SPC should be able to read these ports at $F4-$F7
59 EXPECT_EQ(apu->Read(0xF4), 0xAA);
60 EXPECT_EQ(apu->Read(0xF5), 0xBB);
61
62 // Write to output ports from SPC side
63 apu->Write(0xF4, 0xCC);
64 apu->Write(0xF5, 0xDD);
65
66 // CPU should be able to read these (simulating reads from $2140-$2143)
67 EXPECT_EQ(apu->out_ports_[0], 0xCC);
68 EXPECT_EQ(apu->out_ports_[1], 0xDD);
69}
70
71TEST_F(ApuIplHandshakeTest, IplRomDisableViaControlRegister) {
72 // IPL ROM is readable by default
73 EXPECT_EQ(apu->Read(0xFFC0), 0xCD);
74
75 // Write to control register ($F1) to disable IPL ROM (bit 7 = 1)
76 apu->Write(0xF1, 0x80);
77
78 // Now $FFC0-$FFFF should read from RAM instead of IPL ROM
79 // RAM is initialized to 0, so we should read 0
80 EXPECT_EQ(apu->Read(0xFFC0), 0x00);
81
82 // Write something to RAM
83 apu->ram[0xFFC0] = 0x42;
84 EXPECT_EQ(apu->Read(0xFFC0), 0x42);
85
86 // Re-enable IPL ROM (bit 7 = 0)
87 apu->Write(0xF1, 0x00);
88
89 // Should read IPL ROM again
90 EXPECT_EQ(apu->Read(0xFFC0), 0xCD);
91}
92
93TEST_F(ApuIplHandshakeTest, TimersEnableAndCount) {
94 // Enable timer 0 via control register
95 apu->Write(0xF1, 0x01);
96
97 // Set timer 0 target to 4
98 apu->Write(0xFA, 0x04);
99
100 // Run enough cycles to trigger timer
101 for (int i = 0; i < 1000; ++i) {
102 apu->Cycle();
103 }
104
105 // Read timer 0 counter (auto-clears on read)
106 uint8_t counter = apu->Read(0xFD);
107
108 // Counter should be non-zero if timer is working
109 EXPECT_GT(counter, 0);
110 EXPECT_LE(counter, 0x0F);
111}
112
113TEST_F(ApuIplHandshakeTest, IplBootSequenceProgresses) {
114 // This test verifies that the IPL ROM boot sequence can actually progress
115 // without getting stuck in an infinite loop
116
117 uint16_t initial_pc = apu->spc700().PC;
118
119 // Run multiple opcodes to let the IPL boot sequence progress
120 for (int i = 0; i < 100; ++i) {
121 apu->spc700().RunOpcode();
122 apu->Cycle();
123 }
124
125 uint16_t final_pc = apu->spc700().PC;
126
127 // PC should have advanced (boot sequence is progressing)
128 // If it's stuck in a tight loop, PC won't change much
129 EXPECT_NE(initial_pc, final_pc);
130}
131
132TEST_F(ApuIplHandshakeTest, AccurateCycleCountsForCommonOpcodes) {
133 // Test that specific opcodes return correct cycle counts
134
135 // NOP (0x00) should take 2 cycles
136 apu->spc700().PC = 0x0000;
137 apu->ram[0x0000] = 0x00; // NOP
138 apu->spc700().RunOpcode();
139 apu->spc700().RunOpcode(); // Execute
140 EXPECT_EQ(apu->spc700().GetLastOpcodeCycles(), 2);
141
142 // MOV A, #imm (0xE8) should take 2 cycles
143 apu->spc700().PC = 0x0002;
144 apu->ram[0x0002] = 0xE8; // MOV A, #imm
145 apu->ram[0x0003] = 0x42; // immediate value
146 apu->spc700().RunOpcode();
147 apu->spc700().RunOpcode();
148 EXPECT_EQ(apu->spc700().GetLastOpcodeCycles(), 2);
149}
150
151} // namespace emu
152} // namespace yaze
153
The Apu class represents the Audio Processing Unit (APU) of a system.
Definition apu.h:53
auto spc700() -> Spc700 &
Definition apu.h:71
void Init()
Definition apu.cc:45
void Reset()
Definition apu.cc:52
Implementation of the Memory interface for emulating memory in a SNES system.
Definition memory.h:118
void Initialize(const std::vector< uint8_t > &romData, bool verbose=false)
Definition memory.cc:12
TEST_F(ApuDspTest, DspRegistersReadWriteMirror)
Main namespace for the application.