|
2 | 2 | #include "bus.h"
|
3 | 3 | #include "logging.h"
|
4 | 4 |
|
5 |
| -Bus::Bus(BootROM& bootrom, Cartridge& cartridge, PPU& ppu) |
6 |
| - : bootrom(bootrom), cartridge(cartridge), ppu(ppu) { |
| 5 | +Bus::Bus(BootROM& bootrom, Cartridge& cartridge, PPU& ppu, Timer& timer) |
| 6 | + : bootrom(bootrom), cartridge(cartridge), ppu(ppu), timer(timer) { |
7 | 7 | boot_rom_enabled = true;
|
8 | 8 | memory = std::array<u8, 0x10000>();
|
9 | 9 | std::fill(memory.begin(), memory.end(), 0xFF);
|
@@ -46,7 +46,7 @@ u8 Bus::Read8(u16 addr) {
|
46 | 46 | LDEBUG("reading 0x%02X from 0x%04X (Cartridge RAM)", memory[addr], addr);
|
47 | 47 | return memory[addr];
|
48 | 48 | } else {
|
49 |
| - LWARN("attempted to read from cartridge RAM while it is disabled (from 0x%04X)", addr); |
| 49 | + // LWARN("attempted to read from cartridge RAM while it is disabled (from 0x%04X)", addr); |
50 | 50 | return 0xFF;
|
51 | 51 | }
|
52 | 52 | }
|
@@ -260,6 +260,30 @@ u8 Bus::ReadIO(u16 addr) {
|
260 | 260 | LDEBUG("reading 0x%02X from 0xFF00 (Joypad)", 0xCF);
|
261 | 261 | // LWARN("(hack) all reads from 0xFF00 are currently stubbed to 0xCF");
|
262 | 262 | return 0xCF;
|
| 263 | + case 0xFF04: |
| 264 | + { |
| 265 | + u8 div = timer.GetDivider(); |
| 266 | + LDEBUG("reading 0x%02X from DIV (0xFF04)", div); |
| 267 | + return div; |
| 268 | + } |
| 269 | + case 0xFF05: |
| 270 | + { |
| 271 | + u8 tima = timer.GetTIMA(); |
| 272 | + LDEBUG("reading 0x%02X from TIMA (0xFF05)", tima); |
| 273 | + return tima; |
| 274 | + } |
| 275 | + case 0xFF06: |
| 276 | + { |
| 277 | + u8 tma = timer.GetTMA(); |
| 278 | + LDEBUG("reading 0x%02X from TMA (0xFF06)", tma); |
| 279 | + return tma; |
| 280 | + } |
| 281 | + case 0xFF07: |
| 282 | + { |
| 283 | + u8 tac = timer.GetTAC(); |
| 284 | + LDEBUG("reading 0x%02X from TAC (0xFF07)", tac); |
| 285 | + return tac; |
| 286 | + } |
263 | 287 | case 0xFF0F:
|
264 | 288 | // Interrupt fetch
|
265 | 289 | return memory[0xFF0F];
|
@@ -316,6 +340,30 @@ void Bus::WriteIO(u16 addr, u8 value) {
|
316 | 340 | // putchar(value);
|
317 | 341 | LDEBUG("writing 0x%02X to Serial data (0xFF01)", value);
|
318 | 342 | return;
|
| 343 | + case 0xFF04: |
| 344 | + // Timer divider |
| 345 | + // All writes to this set it to 0. |
| 346 | + timer.ResetDivider(); |
| 347 | + memory[0xFF04] = 0x00; |
| 348 | + return; |
| 349 | + case 0xFF05: |
| 350 | + // Timer counter |
| 351 | + LDEBUG("writing 0x%02X to TIMA (0xFF05)", value); |
| 352 | + timer.SetTIMA(value); |
| 353 | + memory[0xFF05] = value; |
| 354 | + return; |
| 355 | + case 0xFF06: |
| 356 | + // Timer modulo |
| 357 | + LDEBUG("writing 0x%02X to TMA (0xFF06)", value); |
| 358 | + timer.SetTMA(value); |
| 359 | + memory[0xFF06] = value; |
| 360 | + return; |
| 361 | + case 0xFF07: |
| 362 | + // Timer control |
| 363 | + LDEBUG("writing 0x%02X to TAC (0xFF07)", value); |
| 364 | + timer.SetTAC(value); |
| 365 | + memory[0xFF07] = value; |
| 366 | + return; |
319 | 367 | case 0xFF0F:
|
320 | 368 | // Interrupt fetch
|
321 | 369 | memory[0xFF0F] = value;
|
|
0 commit comments