summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbuild.sh7
-rw-r--r--cpu.c5
-rw-r--r--mknes.c11
-rw-r--r--ppu.c12
-rw-r--r--ppu_registers.c4
5 files changed, 23 insertions, 16 deletions
diff --git a/build.sh b/build.sh
index 6ab9fc1..3e878d3 100755
--- a/build.sh
+++ b/build.sh
@@ -6,7 +6,8 @@ PROJECT_NAME="mknes" # Change this for each new project
# Base configuration common to all builds
CFLAGS="-std=gnu11 "
CFLAGS+="-mavx2 -mbmi2 -march=native "
-CFLAGS+="-mfunction-return=keep -mindirect-branch=keep "
+CFLAGS+="-mfunction-return=keep "
+CFLAGS+="-mindirect-branch=keep "
CFLAGS+="-fwrapv -ffast-math -fno-trapping-math -fwhole-program "
CFLAGS+="-fno-stack-protector -fno-PIE -no-pie -fno-strict-aliasing -ffunction-sections -fdata-sections "
# CFLAGS+="-fno-exceptions -fno-rtti -fno-use-cxa-atexit "
@@ -37,8 +38,8 @@ fi
case "$BUILD_TYPE" in
"normal")
- CFLAGS+=" -g -O2 -DDEBUG_INTERNAL"
-# -fsanitize=address,undefined -fno-omit-frame-pointer"
+ CFLAGS+=" -ggdb -fno-omit-frame-pointer -O2 -DDEBUG_INTERNAL"
+# -fsanitize=address,undefined,alignment,object-size,unreachable -fno-omit-frame-pointer"
;;
"release")
CFLAGS+=" -s -Wl,--strip-all -O2"
diff --git a/cpu.c b/cpu.c
index 34d5bed..ec7b5ec 100644
--- a/cpu.c
+++ b/cpu.c
@@ -5,12 +5,12 @@
// REMOVE FOR NES!!!!!
// #define ENABLE_DECIMAL_MODE
-__attribute__((always_inline))
+__attribute__((hot, always_inline))
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;
}
-__attribute__((always_inline))
+__attribute__((hot, always_inline))
static inline void unpack_flags(struct cpu_state *cpu, uint8_t value) {
cpu->n = (value >> 7) & 1;
cpu->v = (value >> 6) & 1;
@@ -20,6 +20,7 @@ static inline void unpack_flags(struct cpu_state *cpu, uint8_t value) {
cpu->c = value & 1;
}
+__attribute__((hot))
static inline void update_zn(struct cpu_state *cpu, uint8_t result) {
cpu->z = (result == 0);
cpu->n = (result & 0x80) != 0;
diff --git a/mknes.c b/mknes.c
index 255b123..ea735f6 100644
--- a/mknes.c
+++ b/mknes.c
@@ -75,7 +75,7 @@ static GLFWwindow *window;
#define DEBUG_PRINT printf
#include "timer.c"
-#include "audio.c"
+// #include "audio.c"
#include "opengl_loader.c"
#include "opengl.c"
@@ -117,7 +117,7 @@ static struct nes_state *allocate_nes_state(void) {
memset(m, 0, total_size);
size_t offset = 0;
- state->prg_rom = m + offset; offset += PRG_ROM_SIZE;
+ state->prg_rom = m + offset; offset += PRG_ROM_SIZE; // DO NOT MOVE THIS, as this is the pointer we free later.
state->chr_rom = m + offset; offset += CHR_ROM_SIZE;
state->pixels = m + offset; offset += PIXELS_SIZE;
state->ram = m + offset; offset += RAM_SIZE;
@@ -127,6 +127,10 @@ static struct nes_state *allocate_nes_state(void) {
return state;
}
+static void free_nes_state(struct nes_state *s) {
+ free(s->prg_rom);
+ free(s);
+}
int main(int argc, char **argv) {
#ifdef _WIN32
@@ -135,7 +139,6 @@ int main(int argc, char **argv) {
state.toggle_crt_emulation = 1;
struct nes_state *nstate = allocate_nes_state();
- // ppu_generate_full_timeline();
setbuf(stdout, 0);
init_opcode_lut();
@@ -274,7 +277,7 @@ printf("total frames: %6.6d total cycles: %ld\n", frames, nstate->cycles);
fprintf(stderr, "Failed to initialize GLFW\n");
}
timer_destroy(timer);
-
+ free_nes_state(nstate);
#ifdef _WIN32
timeEndPeriod(1);
#endif
diff --git a/ppu.c b/ppu.c
index 2015b1d..cc223b7 100644
--- a/ppu.c
+++ b/ppu.c
@@ -219,12 +219,14 @@ static void ppu_tick(struct nes_state *state) {
uint32_t nt_addr = 0x2000 | (ppu->vram_addr & 0x0fff);
ppu->bg_next_tile_id = state->mapper.ciram_read(state, nt_addr);
} break;
+
case 3: {
uint32_t attr_addr = 0x23c0 | (ppu->vram_addr & 0x0c00) | ((ppu->vram_addr >> 4) & 0x38) | ((ppu->vram_addr >> 2) & 0x07);
uint8_t attr = state->mapper.ciram_read(state, attr_addr & 0x0fff);
uint8_t shift = ((ppu->vram_addr >> 4) & 4) | (ppu->vram_addr & 2);
ppu->bg_next_tile_attrib = (attr >> shift) & 3;
} break;
+
case 5: {
uint32_t base = (ppu->reg_ctrl & 0x10) ? 0x1000 : 0x0000;
uint32_t tile = ppu->bg_next_tile_id;
@@ -232,6 +234,7 @@ static void ppu_tick(struct nes_state *state) {
uint32_t addr_lsb = (base + tile * 16 + fine_y) & 0x1fff;
ppu->bg_next_tile_lsb = state->mapper.chr_read(state, addr_lsb);
} break;
+
case 7: {
uint32_t base = (ppu->reg_ctrl & 0x10) ? 0x1000 : 0x0000;
uint32_t tile = ppu->bg_next_tile_id;
@@ -239,6 +242,7 @@ static void ppu_tick(struct nes_state *state) {
uint32_t addr_msb = (base + tile * 16 + fine_y + 8) & 0x1fff;
ppu->bg_next_tile_msb = state->mapper.chr_read(state, addr_msb);
} break;
+
case 0: {
ppu->bg_shift_pattern_low = (ppu->bg_shift_pattern_low & 0xff00) | ppu->bg_next_tile_lsb;
ppu->bg_shift_pattern_high = (ppu->bg_shift_pattern_high & 0xff00) | ppu->bg_next_tile_msb;
@@ -339,12 +343,12 @@ static void ppu_tick(struct nes_state *state) {
} break;
case 0: {
- ppu->bg_shift_pattern_low = (ppu->bg_shift_pattern_low & 0xff00) | ppu->bg_next_tile_lsb;
- ppu->bg_shift_pattern_high = (ppu->bg_shift_pattern_high & 0xff00) | ppu->bg_next_tile_msb;
+ ppu->bg_shift_pattern_low = (ppu->bg_shift_pattern_low & 0xff00) | ppu->bg_next_tile_lsb;
+ ppu->bg_shift_pattern_high = (ppu->bg_shift_pattern_high & 0xff00) | ppu->bg_next_tile_msb;
uint8_t a = ppu->bg_next_tile_attrib;
- ppu->bg_shift_attrib_low = (ppu->bg_shift_attrib_low & 0xff00) | ((a & 1) ? 0xff : 0x00);
- ppu->bg_shift_attrib_high = (ppu->bg_shift_attrib_high & 0xff00) | ((a & 2) ? 0xff : 0x00);
+ ppu->bg_shift_attrib_low = (ppu->bg_shift_attrib_low & 0xff00) | ((a & 1) ? 0xff : 0x00);
+ ppu->bg_shift_attrib_high = (ppu->bg_shift_attrib_high & 0xff00) | ((a & 2) ? 0xff : 0x00);
if((ppu->vram_addr & 0x001f) == 31) {
ppu->vram_addr &= ~0x001f;
diff --git a/ppu_registers.c b/ppu_registers.c
index bd85cd8..dd8c7fa 100644
--- a/ppu_registers.c
+++ b/ppu_registers.c
@@ -19,8 +19,7 @@ static inline void ppu_write(struct nes_state *state, uint32_t offset, uint8_t v
} break;
case 4: { // 2004
- ppu->oam[ppu->oam_addr] = value;
- ppu->oam_addr++;
+ ppu->oam[ppu->oam_addr++] = value;
} break;
case 5: { // 2005
@@ -83,7 +82,6 @@ static inline uint8_t ppu_read(struct nes_state *state, uint32_t offset) {
case 7: { // 2007
uint32_t addr = ppu->vram_addr & 0x3fff;
- result = 0;
if(addr < 0x2000) {
result = ppu->vram_read_buffer;