summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cpu.c27
-rw-r--r--ppu.c15
-rw-r--r--ppu.h23
-rw-r--r--rom.h5
4 files changed, 64 insertions, 6 deletions
diff --git a/cpu.c b/cpu.c
index 8df2bf9..621fe5b 100644
--- a/cpu.c
+++ b/cpu.c
@@ -8,6 +8,7 @@
8#include "cpu.h" 8#include "cpu.h"
9#include "opcodes.h" 9#include "opcodes.h"
10#include "rom.h" 10#include "rom.h"
11#include "ppu.h"
11 12
12#define MAX(a, b) ((a > b) ? a : b) 13#define MAX(a, b) ((a > b) ? a : b)
13 14
@@ -146,14 +147,23 @@ opcode_mem(enum addressing_mode mode)
146} 147}
147 148
148static void 149static void
150tick(void)
151{
152 cycles++;
153 ppu_tick();
154 ppu_tick();
155 ppu_tick();
156}
157
158static void
149branch(uint16_t addr, bool cond) 159branch(uint16_t addr, bool cond)
150{ 160{
151 if (!cond) 161 if (!cond)
152 return; 162 return;
153 cycles++; 163 tick();
154 164
155 if (((regs.pc + 1) & 0xFF00) != (addr & 0xFF00)) 165 if (((regs.pc + 1) & 0xFF00) != (addr & 0xFF00))
156 cycles++; 166 tick();
157 167
158 regs.pc = addr; 168 regs.pc = addr;
159} 169}
@@ -946,18 +956,21 @@ interpret(void)
946 956
947 while (spaces--) putchar(' '); 957 while (spaces--) putchar(' ');
948 958
949 printf("A:%02X X:%02X Y:%02X P:%02X SP:%02X CYC:%d\n", 959 printf("A:%02X X:%02X Y:%02X P:%02X SP:%02X PPU:%3d,%3d CYC:%d\n",
950 regs.a, regs.x, regs.y, STATUS_TO_INT(), regs.sp, cycles); 960 regs.a, regs.x, regs.y, STATUS_TO_INT(), regs.sp,
961 ppu.scanlines, ppu.cycles, cycles);
951 962
952 if (opcodes[op].memread) 963 if (opcodes[op].memread)
953 opcodes[op].instr(peek(arg)); 964 opcodes[op].instr(peek(arg));
954 else 965 else
955 opcodes[op].instr(arg); 966 opcodes[op].instr(arg);
956 967
957 cycles += opcodes[op].cycles; 968 for (uint8_t i = 0; i < opcodes[op].cycles; i++)
969 tick();
970
958 if (page_crossed) { 971 if (page_crossed) {
959 if (opcodes[op].page_cross) 972 if (opcodes[op].page_cross)
960 cycles++; 973 tick();
961 page_crossed = false; 974 page_crossed = false;
962 } 975 }
963 } 976 }
@@ -976,6 +989,8 @@ cpu_init(void)
976 regs.status.unused = 1; 989 regs.status.unused = 1;
977 990
978 cycles += 7; 991 cycles += 7;
992 for (uint8_t i = 0; i < 7 * 3; i++)
993 ppu_tick();
979} 994}
980 995
981int 996int
diff --git a/ppu.c b/ppu.c
new file mode 100644
index 0000000..4ae0162
--- /dev/null
+++ b/ppu.c
@@ -0,0 +1,15 @@
1#include "ppu.h"
2
3struct ppu ppu = {0};
4
5void
6ppu_tick(void)
7{
8 ppu.cycles++;
9 if (ppu.cycles >= 341) {
10 ppu.cycles -= 341;
11 ppu.scanlines++;
12 }
13 if (ppu.scanlines >= 262)
14 ppu.scanlines -= 262;
15}
diff --git a/ppu.h b/ppu.h
new file mode 100644
index 0000000..1527b93
--- /dev/null
+++ b/ppu.h
@@ -0,0 +1,23 @@
1#ifndef PPU_H
2#define PPU_H
3
4#include <stdio.h>
5#include <stdint.h>
6
7#include "rom.h"
8
9struct ppu {
10 uint8_t oam[64*4];
11 uint8_t vram[2048];
12 uint8_t palette[16*2];
13 uint16_t cycles, scanlines;
14 /* copied from rom */
15 uint8_t *chr_rom;
16 size_t chr_rom_size;
17 enum screen_mirroring mirror;
18};
19extern struct ppu ppu;
20
21void ppu_tick(void);
22
23#endif /* PPU_H */
diff --git a/rom.h b/rom.h
index a52bda1..703fabd 100644
--- a/rom.h
+++ b/rom.h
@@ -1,3 +1,6 @@
1#ifndef ROM_H
2#define ROM_H
3
1enum screen_mirroring { 4enum screen_mirroring {
2 M_HORIZONTAL, 5 M_HORIZONTAL,
3 M_VERTICAL, 6 M_VERTICAL,
@@ -15,3 +18,5 @@ struct Rom {
15 18
16void parse_rom(const uint8_t *data, size_t data_len, struct Rom *rom); 19void parse_rom(const uint8_t *data, size_t data_len, struct Rom *rom);
17void free_rom(struct Rom *rom); 20void free_rom(struct Rom *rom);
21
22#endif /* ROM_H */