37 absl::Status
Init(
const std::string& rom_path) {
39 if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_EVENTS) < 0) {
40 return absl::InternalError(
41 absl::StrCat(
"SDL_Init failed: ", SDL_GetError()));
46 FILE* file = fopen(rom_path.c_str(),
"rb");
48 return absl::NotFoundError(
49 absl::StrCat(
"Failed to open ROM: ", rom_path));
52 fseek(file, 0, SEEK_END);
53 size_t size = ftell(file);
54 fseek(file, 0, SEEK_SET);
57 if (fread(
rom_data_.data(), 1, size, file) != size) {
59 return absl::InternalError(
"Failed to read ROM data");
63 printf(
"Loaded ROM: %zu bytes\n", size);
66 snes_ = std::make_unique<Snes>();
70 printf(
"SNES initialized and reset\n");
71 printf(
"APU PC after reset: $%04X\n",
snes_->apu().spc700().PC);
72 printf(
"APU cycles: %llu\n",
snes_->apu().GetCycles());
74 return absl::OkStatus();
127 auto& apu =
snes_->apu();
128 auto& spc = apu.spc700();
129 auto& tracker =
snes_->apu_handshake_tracker();
131 printf(
"=== Frame %d APU State ===\n", frame);
132 printf(
" SPC700 PC: $%04X\n", spc.PC);
133 printf(
" SPC700 A: $%02X\n", spc.A);
134 printf(
" SPC700 X: $%02X\n", spc.X);
135 printf(
" SPC700 Y: $%02X\n", spc.Y);
136 printf(
" SPC700 SP: $%02X\n", spc.SP);
137 printf(
" SPC700 PSW: N=%d V=%d P=%d B=%d H=%d I=%d Z=%d C=%d\n", spc.PSW.N,
138 spc.PSW.V, spc.PSW.P, spc.PSW.B, spc.PSW.H, spc.PSW.I, spc.PSW.Z,
140 printf(
" APU Cycles: %llu\n", apu.GetCycles());
143 printf(
" Input Ports: F4=$%02X F5=$%02X F6=$%02X F7=$%02X\n",
144 apu.in_ports_[0], apu.in_ports_[1], apu.in_ports_[2],
146 printf(
" Output Ports: F4=$%02X F5=$%02X F6=$%02X F7=$%02X\n",
147 apu.out_ports_[0], apu.out_ports_[1], apu.out_ports_[2],
151 const char* handshake_phase =
"UNKNOWN";
152 switch (tracker.GetPhase()) {
154 handshake_phase =
"RESET";
157 handshake_phase =
"IPL_BOOT";
160 handshake_phase =
"WAITING_BBAA";
163 handshake_phase =
"HANDSHAKE_CC";
166 handshake_phase =
"TRANSFER_ACTIVE";
169 handshake_phase =
"TRANSFER_DONE";
172 handshake_phase =
"RUNNING";
175 printf(
" Handshake: %s\n", handshake_phase);
179 printf(
" Zero Page: $00=$%02X $01=$%02X $02=$%02X $03=$%02X\n", ram[0x00],
180 ram[0x01], ram[0x02], ram[0x03]);
183 uint16_t reset_vector =
184 static_cast<uint16_t
>(ram[0xFFFE] | (ram[0xFFFF] << 8));
185 printf(
" Reset Vector ($FFFE-$FFFF): $%04X\n", reset_vector);