start implementing PPU

This commit is contained in:
Vineet K 2024-07-21 13:53:04 -04:00
parent 2df7df5f5b
commit 7d148924e2
4 changed files with 64 additions and 6 deletions

27
cpu.c
View File

@ -8,6 +8,7 @@
#include "cpu.h" #include "cpu.h"
#include "opcodes.h" #include "opcodes.h"
#include "rom.h" #include "rom.h"
#include "ppu.h"
#define MAX(a, b) ((a > b) ? a : b) #define MAX(a, b) ((a > b) ? a : b)
@ -145,15 +146,24 @@ opcode_mem(enum addressing_mode mode)
return val; return val;
} }
static void
tick(void)
{
cycles++;
ppu_tick();
ppu_tick();
ppu_tick();
}
static void static void
branch(uint16_t addr, bool cond) branch(uint16_t addr, bool cond)
{ {
if (!cond) if (!cond)
return; return;
cycles++; tick();
if (((regs.pc + 1) & 0xFF00) != (addr & 0xFF00)) if (((regs.pc + 1) & 0xFF00) != (addr & 0xFF00))
cycles++; tick();
regs.pc = addr; regs.pc = addr;
} }
@ -946,18 +956,21 @@ interpret(void)
while (spaces--) putchar(' '); while (spaces--) putchar(' ');
printf("A:%02X X:%02X Y:%02X P:%02X SP:%02X CYC:%d\n", printf("A:%02X X:%02X Y:%02X P:%02X SP:%02X PPU:%3d,%3d CYC:%d\n",
regs.a, regs.x, regs.y, STATUS_TO_INT(), regs.sp, cycles); regs.a, regs.x, regs.y, STATUS_TO_INT(), regs.sp,
ppu.scanlines, ppu.cycles, cycles);
if (opcodes[op].memread) if (opcodes[op].memread)
opcodes[op].instr(peek(arg)); opcodes[op].instr(peek(arg));
else else
opcodes[op].instr(arg); opcodes[op].instr(arg);
cycles += opcodes[op].cycles; for (uint8_t i = 0; i < opcodes[op].cycles; i++)
tick();
if (page_crossed) { if (page_crossed) {
if (opcodes[op].page_cross) if (opcodes[op].page_cross)
cycles++; tick();
page_crossed = false; page_crossed = false;
} }
} }
@ -976,6 +989,8 @@ cpu_init(void)
regs.status.unused = 1; regs.status.unused = 1;
cycles += 7; cycles += 7;
for (uint8_t i = 0; i < 7 * 3; i++)
ppu_tick();
} }
int int

15
ppu.c Normal file
View File

@ -0,0 +1,15 @@
#include "ppu.h"
struct ppu ppu = {0};
void
ppu_tick(void)
{
ppu.cycles++;
if (ppu.cycles >= 341) {
ppu.cycles -= 341;
ppu.scanlines++;
}
if (ppu.scanlines >= 262)
ppu.scanlines -= 262;
}

23
ppu.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef PPU_H
#define PPU_H
#include <stdio.h>
#include <stdint.h>
#include "rom.h"
struct ppu {
uint8_t oam[64*4];
uint8_t vram[2048];
uint8_t palette[16*2];
uint16_t cycles, scanlines;
/* copied from rom */
uint8_t *chr_rom;
size_t chr_rom_size;
enum screen_mirroring mirror;
};
extern struct ppu ppu;
void ppu_tick(void);
#endif /* PPU_H */

5
rom.h
View File

@ -1,3 +1,6 @@
#ifndef ROM_H
#define ROM_H
enum screen_mirroring { enum screen_mirroring {
M_HORIZONTAL, M_HORIZONTAL,
M_VERTICAL, M_VERTICAL,
@ -15,3 +18,5 @@ struct Rom {
void parse_rom(const uint8_t *data, size_t data_len, struct Rom *rom); void parse_rom(const uint8_t *data, size_t data_len, struct Rom *rom);
void free_rom(struct Rom *rom); void free_rom(struct Rom *rom);
#endif /* ROM_H */