Skip to content

Commit

Permalink
NES: Replaced timing pseudo-constants with the real function calls
Browse files Browse the repository at this point in the history
When I introduced PAL support I needed to overload several #defines and to reduce code changes I just made them call functions underneath.

But now it's just confusing.
  • Loading branch information
ducalex committed Feb 3, 2024
1 parent bce5633 commit ac6c23f
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 93 deletions.
2 changes: 1 addition & 1 deletion retro-core/components/nofrendo/mappers/map019.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static void map_hblank(int scanline)
{
if (irq.enabled)
{
irq.counter += NES_CYCLES_PER_SCANLINE;
irq.counter += nes_getptr()->cycles_per_scanline;

if (irq.counter >= 0x7FFF)
{
Expand Down
2 changes: 1 addition & 1 deletion retro-core/components/nofrendo/mappers/map020.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ static void fds_cpu_timer(int cycles)

static void fds_hblank(int scanline)
{
fds_cpu_timer(NES_CYCLES_PER_SCANLINE);
fds_cpu_timer(nes_getptr()->cycles_per_scanline);
}

static uint8 fds_read(uint32 address)
Expand Down
4 changes: 2 additions & 2 deletions retro-core/components/nofrendo/mappers/map031.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static void map_vblank(void)
setup_song(++current_song);
nes6502_reset();
apu_reset();
nes6502_burn(NES_CPU_CLOCK / 2);
nes6502_burn(NES_CPU_CLOCK_NTSC / 2);
}
}

Expand All @@ -109,7 +109,7 @@ static void map_write(uint32 address, uint8 value)
else if (address == 0x5800) // playback sync
{
#if !SYNC_TO_VBLANK
nes6502_burn(header->ntsc_speed * (NES_CPU_CLOCK / 1000000.f) - 214);
nes6502_burn(header->ntsc_speed * (NES_CPU_CLOCK_NTSC / 1000000.f) - 214);
#endif
playing = true;
}
Expand Down
8 changes: 4 additions & 4 deletions retro-core/components/nofrendo/mappers/map040.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@

#include "nes/nes.h"

#define MAP40_IRQ_PERIOD (4096 / NES_CYCLES_PER_SCANLINE)

static struct
{
bool enabled;
uint8 counter;
uint8 reload;
} irq;


Expand All @@ -52,7 +51,7 @@ static void map_write(uint32 address, uint8 value)
{
case 0: /* 0x8000-0x9FFF */
irq.enabled = false;
irq.counter = MAP40_IRQ_PERIOD;
irq.counter = irq.reload;
break;

case 1: /* 0xA000-0xBFFF */
Expand Down Expand Up @@ -88,7 +87,8 @@ static void map_init(rom_t *cart)
mmc_bankrom(8, 0xE000, 7);

irq.enabled = false;
irq.counter = MAP40_IRQ_PERIOD;
irq.reload = 4096 / nes_getptr()->cycles_per_scanline;
irq.counter = irq.reload;
}


Expand Down
2 changes: 1 addition & 1 deletion retro-core/components/nofrendo/mappers/map042.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static void map_hblank(int scanline)
/* Increment the counter if it is enabled and check for strike */
if (irq.enabled)
{
irq.counter += NES_CYCLES_PER_SCANLINE;
irq.counter += nes_getptr()->cycles_per_scanline;

/* IRQ is triggered after 24576 M2 cycles */
if (irq.counter >= 0x6000)
Expand Down
2 changes: 1 addition & 1 deletion retro-core/components/nofrendo/mappers/map050.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static void map_hblank(int scanline)
/* Increment the counter if it is enabled and check for strike */
if (irq.enabled)
{
irq.counter += NES_CYCLES_PER_SCANLINE;
irq.counter += nes_getptr()->cycles_per_scanline;

/* IRQ line is hooked to Q12 of the counter */
if (irq.counter & 0x1000)
Expand Down
2 changes: 1 addition & 1 deletion retro-core/components/nofrendo/mappers/map073.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static void map_hblank(int scanline)
/* Increment the counter if it is enabled and check for strike */
if (irq.enabled)
{
irq.counter += NES_CYCLES_PER_SCANLINE;
irq.counter += nes_getptr()->cycles_per_scanline;

/* Counter triggered on overflow into Q16 */
if (irq.counter & 0x10000)
Expand Down
2 changes: 1 addition & 1 deletion retro-core/components/nofrendo/mappers/map162.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static void map_reg_write(uint32 address, uint8 value)
{
case 0x5000:
reg5000 = value;
if (!(reg5000 & 0x80) && NES_CURRENT_SCANLINE < 128)
if (!(reg5000 & 0x80) && nes_getptr()->scanline < 128)
{
mmc_bankvrom(8, 0x0000, 0);
}
Expand Down
5 changes: 3 additions & 2 deletions retro-core/components/nofrendo/nes/apu.c
Original file line number Diff line number Diff line change
Expand Up @@ -758,8 +758,9 @@ int apu_getopt(apu_option_t n)
void apu_reset(void)
{
/* Update region if needed */
apu.samples_per_frame = apu.sample_rate / NES_REFRESH_RATE;
apu.cycle_rate = (float)NES_CPU_CLOCK / apu.sample_rate;
nes_t *nes = nes_getptr();
apu.samples_per_frame = apu.sample_rate / nes->refresh_rate;
apu.cycle_rate = (float)nes->cpu_clock / apu.sample_rate;
apu.noise.shift_reg = 0x4000;
apu_build_luts(apu.samples_per_frame);

Expand Down
47 changes: 6 additions & 41 deletions retro-core/components/nofrendo/nes/nes.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,11 @@ void nes_emulate(bool draw)
{
int elapsed_cycles = 0;

if (nes.input_func)
{
nes.input_func();
}

while (nes.scanline < nes.scanlines_per_frame)
{
nes.cycles += nes.cycles_per_scanline;

ppu_scanline(nes.vidbuf, nes.scanline, draw);
ppu_renderline(nes.vidbuf, nes.scanline, draw);

if (nes.scanline == 241)
{
Expand All @@ -62,24 +57,11 @@ void nes_emulate(bool draw)
if (nes.mapper->hblank)
nes.mapper->hblank(nes.scanline);

if (nes.timer_func == NULL)
{
elapsed_cycles = nes6502_execute(nes.cycles);
apu_fc_advance(elapsed_cycles);
nes.cycles -= elapsed_cycles;
}
else
{
while (nes.cycles >= 1)
{
elapsed_cycles = nes6502_execute(MIN(nes.timer_period, nes.cycles));
apu_fc_advance(elapsed_cycles);
nes.timer_func(elapsed_cycles);
nes.cycles -= elapsed_cycles;
}
}
elapsed_cycles = nes6502_execute(nes.cycles);
apu_fc_advance(elapsed_cycles);
nes.cycles -= elapsed_cycles;

ppu_endscanline();
ppu_endline();
nes.scanline++;
}

Expand All @@ -92,30 +74,15 @@ void nes_emulate(bool draw)
}

apu_emulate();

if (nes.vsync_func)
{
nes.vsync_func();
}
}

/* This sets a timer to be fired every `period` cpu cycles. It is NOT accurate. */
void nes_settimer(nes_timer_t *func, long period)
void nes_settimer(nes_timer_t *func, int period)
{
nes.timer_func = func;
nes.timer_period = period;
}

void nes_poweroff(void)
{
nes.poweroff = true;
}

void nes_togglepause(void)
{
nes.pause ^= true;
}

void nes_setcompathacks(void)
{
// Hack to fix many MMC3 games with status bar vertical alignment issues
Expand Down Expand Up @@ -268,8 +235,6 @@ nes_t *nes_init(nes_type_t system, int sample_rate, bool stereo)
{
memset(&nes, 0, sizeof(nes_t));

nes.poweroff = false;
nes.pause = false;
nes.system = system;
nes.refresh_rate = 60;

Expand Down
27 changes: 7 additions & 20 deletions retro-core/components/nofrendo/nes/nes.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;

typedef struct nes_s nes_t;

#include <nofrendo.h>
#include "apu.h"
#include "cpu.h"
Expand All @@ -51,19 +53,13 @@ typedef uint32_t uint32;

#define NES_SCREEN_GETPTR(buf, x, y) ((buf) + ((y) * NES_SCREEN_PITCH) + (x) + NES_SCREEN_OVERDRAW)

#define NES_CPU_CLOCK_NTSC (1789772.72727)
#define NES_CPU_CLOCK_PAL (1662607.03125)
#define NES_CPU_CLOCK_NTSC (1789773)
#define NES_CPU_CLOCK_PAL (1662607)
#define NES_REFRESH_RATE_NTSC (60)
#define NES_REFRESH_RATE_PAL (50)
#define NES_SCANLINES_NTSC (262)
#define NES_SCANLINES_PAL (312)

#define NES_REFRESH_RATE (nes_getptr()->refresh_rate)
#define NES_SCANLINES (nes_getptr()->scanlines_per_frame)
#define NES_CPU_CLOCK (nes_getptr()->cpu_clock)
#define NES_CYCLES_PER_SCANLINE (nes_getptr()->cycles_per_scanline)
#define NES_CURRENT_SCANLINE (nes_getptr()->scanline)

typedef enum
{
SYS_UNKNOWN,
Expand All @@ -75,7 +71,7 @@ typedef enum

typedef void (nes_timer_t)(int cycles);

typedef struct
typedef struct nes_s
{
/* Hardware */
nes6502_t *cpu;
Expand Down Expand Up @@ -107,26 +103,17 @@ typedef struct

/* Periodic timer */
nes_timer_t *timer_func;
long timer_period;
int timer_period;

/* Port functions */
void (*blit_func)(uint8 *);
void (*vsync_func)(void);
void (*input_func)(void); // uint8 *, uint8 *

/* Control */
bool frameskip;
bool poweroff;
bool pause;
} nes_t;

nes_t *nes_getptr(void);
nes_t *nes_init(nes_type_t system, int sample_rate, bool stereo);
void nes_shutdown(void);
int nes_insertcart(const char *filename, const char *biosfile);
int nes_insertdisk(const char *filename, const char *biosfile);
void nes_settimer(nes_timer_t *func, long period);
void nes_settimer(nes_timer_t *func, int period);
void nes_emulate(bool draw);
void nes_reset(bool hard_reset);
void nes_poweroff(void);
void nes_togglepause(void);
13 changes: 6 additions & 7 deletions retro-core/components/nofrendo/nes/ppu.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ IRAM_ATTR uint8 ppu_read(uint32 address)
{
ppu.vdata_latch = 0xFF;
MESSAGE_DEBUG("VRAM read at $%04X, scanline %d\n",
ppu.vaddr, NES_CURRENT_SCANLINE);
ppu.vaddr, nes_getptr()->scanline);
}
else
{
Expand Down Expand Up @@ -302,7 +302,7 @@ IRAM_ATTR void ppu_write(uint32 address, uint8 value)
if ((ppu.bg_on || ppu.obj_on) && !ppu.vram_accessible)
{
MESSAGE_DEBUG("VRAM write to $%04X, scanline %d\n",
ppu.vaddr, NES_CURRENT_SCANLINE);
ppu.vaddr, nes_getptr()->scanline);
PPU_MEM_WRITE(ppu.vaddr, 0xFF); /* corrupt */
}
else
Expand Down Expand Up @@ -641,7 +641,7 @@ bool ppu_inframe(void)
return (ppu.scanline < 240);
}

IRAM_ATTR void ppu_endscanline()
IRAM_ATTR void ppu_endline()
{
/* modify vram address at end of scanline */
if (ppu.scanline < 240 && (ppu.bg_on || ppu.obj_on))
Expand Down Expand Up @@ -670,7 +670,7 @@ IRAM_ATTR void ppu_endscanline()
}
}

IRAM_ATTR void ppu_scanline(uint8 *bmp, int scanline, bool draw_flag)
IRAM_ATTR void ppu_renderline(uint8 *bmp, int scanline, bool draw_flag)
{
ppu.scanline = scanline;

Expand Down Expand Up @@ -705,10 +705,9 @@ IRAM_ATTR void ppu_scanline(uint8 *bmp, int scanline, bool draw_flag)
{
ppu.stat |= PPU_STATF_VBLANK;
ppu.vram_accessible = true;
ppu.last_scanline = NES_SCANLINES - 1;
}
// End of frame
else if (scanline == ppu.last_scanline)
else if (scanline == ppu.scanlines - 1)
{
ppu.stat &= ~PPU_STATF_VBLANK;
ppu.strikeflag = false;
Expand All @@ -731,7 +730,7 @@ void ppu_reset()
ppu.tile_xofs = 0;
ppu.latch = 0;
ppu.vram_accessible = true;
ppu.last_scanline = NES_SCANLINES - 1;
ppu.scanlines = nes_getptr()->scanlines_per_frame;
}

ppu_t *ppu_init(void)
Expand Down
6 changes: 3 additions & 3 deletions retro-core/components/nofrendo/nes/ppu.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ typedef struct
bool strikeflag;
uint32 strike_cycle;

int scanlines;
int scanline;
int last_scanline;

/* Determines if left column can be cropped/blanked */
int left_bg_counter;
Expand Down Expand Up @@ -163,8 +163,8 @@ uint8 ppu_read(uint32 address);
void ppu_write(uint32 address, uint8 value);

/* Rendering */
void ppu_scanline(uint8 *bmp, int scanline, bool draw_flag);
void ppu_endscanline(void);
void ppu_renderline(uint8 *bmp, int scanline, bool draw_flag);
void ppu_endline(void);

/* Debugging */
void ppu_dumppattern(uint8 *bmp, int table_num, int x_loc, int y_loc, int col);
Expand Down
Loading

0 comments on commit ac6c23f

Please sign in to comment.