// // Created by william on 12/30/23. // https://www.reddit.com/r/EmuDev/comments/evu3u2/comment/fgr03ms/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button // // 1. Make sure you have NMI implemented on CPU (pretty straightforward) // 2. Implement PPUSTATUS vblank flag (simple) and PPUCTRL NMI flag + background address flag (simple) // 3. Implement PPUADDR/PPUDATA so that the nametables are filled out // 4. Now you have some data your PPU can actually read for rendering background. Render it scanline by scanline - just follow the wiki on this. Maybe the timing will be bad, it doesn't matter for this game. Start off with rendering tiles based on the pattern table ID, don't try and fetch patterns. // 5. Fix the inevitable bugs with your PPUDATA implementation until you see a blocky version of the Donkey Kong screen. // 6. Now fetch pattern table data using the nametable data. If it looks "wrong" make sure you are consuming the background address flag. Start off with black and white, then pick two colors to mix for the two bits. Now you should have something like https://i.imgur.com/7OIpHgd.png // 7. (Optional) implement palette reads (I'm skipping this for now). // 8. Implement OAMDMA (and OAMDATA I guess, I implemented one on top of the other) // 9. Now you should have sprite data to render. Implement the logic for copying from primary OAM to scanline OAM. I'm doing it all as one step (not smearing it over up to 256 cycles like the actual hardware). Skip the confusing sprite overflow junk. // 10. This is where I'm stuck. I think I need to read the "sprites" section of https://wiki.nesdev.com/w/index.php/PPU_rendering very carefully. // #include "../include/ppu.h" PPU ppu_state; void ppu_init(byte *registers_ram, byte *oam_dma_register) { ppu_state.registers = registers_ram; ppu_state.registers[PPU_REGISTER_CTRL] = 0x00; ppu_state.registers[PPU_REGISTER_MASK] = 0x00; ppu_state.registers[PPU_REGISTER_STATUS] = 0x00; ppu_state.registers[PPU_REGISTER_OAM_ADDR] = 0x00; ppu_state.registers[PPU_REGISTER_OAM_DATA] = 0x00; ppu_state.registers[PPU_REGISTER_SCROLL] = 0x00; ppu_state.registers[PPU_REGISTER_ADDR] = 0x00; ppu_state.registers[PPU_REGISTER_DATA] = 0x00; ppu_state.oam_dma_register = oam_dma_register; ppu_state.odd_frame = false; } PPU *ppu_get_state() { return &ppu_state; } void ppu_status_set(byte mask, bool enabled) { if (enabled) { ppu_state.registers[PPU_REGISTER_STATUS] |= mask; } else { ppu_state.registers[PPU_REGISTER_STATUS] &= ~mask; } } long frame = 0; int x, y = 0; void ppu_cycle() { if (x == 1) { if (y == 241) { // VBlank start ppu_status_set(PPU_STATUS_VBLANK, true); } if (y == 261) { // VBlank clear ppu_status_set(PPU_STATUS_VBLANK, false); } } int frame_width = 341; int frame_height = 262; bool rendering_enabled = ppu_read_flag(PPU_REGISTER_MASK, PPU_MASK_SHOW_BG | PPU_MASK_SHOW_SP); if (rendering_enabled && ppu_state.odd_frame) { // With rendering enabled, the odd frames are shorter // TODO: and doing the last cycle of the last dummy nametable fetch there instead frame_width = 339; frame_height = 261; } x++; if (x >= frame_width) { x = 0; y++; } if (y >= frame_height) { y = 0; frame++; ppu_state.odd_frame = !ppu_state.odd_frame; } } bool ppu_read_flag(size_t reg, byte mask) { return ppu_state.registers[reg] & mask; } //byte ppu_read_register(size_t reg, byte mask) { // return ppu_state.registers[reg] & mask; //} void ppu_read_register(byte reg) { if (reg == PPU_REGISTER_STATUS) { ppu_state.w = false; } } void ppu_write_register(byte reg) { if (reg == PPU_REGISTER_SCROLL || reg == PPU_REGISTER_ADDR) { ppu_state.w = !ppu_state.w; } if (reg == PPU_REGISTER_OAM_DATA) { ppu_state.registers[PPU_REGISTER_OAM_ADDR]++; } }