summaryrefslogtreecommitdiff
path: root/cpu.c
diff options
context:
space:
mode:
authorPeter Fors <peter.fors@mindkiller.com>2025-10-09 22:07:52 +0200
committerPeter Fors <peter.fors@mindkiller.com>2025-10-09 22:07:52 +0200
commit030724a9aea346e4a9843d5842fb28c6d6c4cf1a (patch)
treef06fb84aaef64b2f4e2d81b3d2d3eef71bad83ec /cpu.c
parent412b2ef851516c1de8ba5006ddd284192cbcaf9b (diff)
Rearrangement and refactoring and optimizations and more accuracy
Diffstat (limited to 'cpu.c')
-rw-r--r--cpu.c91
1 files changed, 0 insertions, 91 deletions
diff --git a/cpu.c b/cpu.c
deleted file mode 100644
index e3e83e6..0000000
--- a/cpu.c
+++ /dev/null
@@ -1,91 +0,0 @@
-#include <stdint.h>
-#include <stdio.h>
-#include <string.h>
-
-// DO NOT ENABLE FOR NES!!!!!
-// #define ENABLE_DECIMAL_MODE
-
-#define PAGE_CROSSED(base, addr) (((base ^ addr) > 0xff))
-
-static inline uint8_t pack_flags(struct cpu_state *cpu) {
- return (cpu->n << 7) | (cpu->v << 6) | (1 << 5) | (cpu->d << 3) | (cpu->i << 2) | (cpu->z << 1) | cpu->c;
-}
-
-static inline void unpack_flags(struct cpu_state *cpu, uint8_t value) {
- cpu->n = (value >> 7) & 1;
- cpu->v = (value >> 6) & 1;
- cpu->d = (value >> 3) & 1;
- cpu->i = (value >> 2) & 1;
- cpu->z = (value >> 1) & 1;
- cpu->c = value & 1;
-}
-
-static inline void update_zn(struct cpu_state *cpu, uint8_t result) {
- cpu->z = (result == 0);
- cpu->n = (result & 0x80) != 0;
-}
-
-static void (*opcode_lut[256*2])(struct nes_state *) __attribute__((aligned(4096)));
-
-#include "cpu_opcodes.c"
-#include "cpu_opcodes_ud.c"
-
-static inline void do_nmi(struct nes_state *state) {
- struct cpu_state * restrict cpu = &state->cpu;
-
- memory_read_dummy(state, cpu->pc); // T1: dummy read (fetch suppressed)
-
- uint8_t pcl = cpu->pc & 0xff;
- uint8_t pch = cpu->pc >> 8;
-
- memory_write(state, 0x0100 + cpu->sp--, pch); // T2
- memory_write(state, 0x0100 + cpu->sp--, pcl); // T3
- memory_write(state, 0x0100 + cpu->sp--, pack_flags(cpu) & ~0x10); // T4: push P (B flag clear)
-
- uint8_t lo = memory_read(state, 0xfffa); // T5
- uint8_t hi = memory_read(state, 0xfffb); // T6
-
- cpu->pc = lo | (hi << 8); // T7
- cpu->i = 1;
-}
-
-static inline void do_irq(struct nes_state *state) {
- struct cpu_state * restrict cpu = &state->cpu;
-
- memory_read_dummy(state, cpu->pc); // T1: dummy read (fetch suppressed)
-
- uint8_t pcl = cpu->pc & 0xff;
- uint8_t pch = cpu->pc >> 8;
-
- memory_write(state, 0x0100 + cpu->sp--, pch); // T2
- memory_write(state, 0x0100 + cpu->sp--, pcl); // T3
- memory_write(state, 0x0100 + cpu->sp--, pack_flags(cpu) & ~0x10); // T4: push P (B flag clear)
-
- uint8_t lo = memory_read(state, 0xfffe); // T5
- uint8_t hi = memory_read(state, 0xffff); // T6
-
- cpu->pc = lo | (hi << 8); // T7
- cpu->i = 1;
-}
-
-static inline void check_interrupts(struct nes_state *state) {
- struct cpu_state * restrict cpu = &state->cpu;
-
- if(state->cpu.nmi_pending) {
- state->cpu.nmi_pending = 0;
- do_nmi(state);
- } else if(state->cpu.irq_pending && cpu->i == 0) {
- state->cpu.irq_pending = 0;
- do_irq(state);
- }
-}
-
-static inline void cpu_tick(struct nes_state *state) {
- struct cpu_state * restrict cpu = &state->cpu;
- check_interrupts(state);
-
- // printf("%4.4x: ", cpu->pc);
- uint8_t opcode = memory_read(state, cpu->pc++);
- // printf("%2.2x a:%2.2x x:%2.2x y:%2.2x p:%2.2x sp:%2.2x cycle: %ld\n", opcode, cpu->a, cpu->x, cpu->y, pack_flags(cpu), cpu->sp, state->cycles);
- opcode_lut[opcode](state);
-}