Skip to content

Commit

Permalink
NES: Pass nes object to MMC handlers
Browse files Browse the repository at this point in the history
Now passing nes_t to hblank/vblank. This is more useful than just the scanline number.

Ideally I'd want to pass nes_t everywhere but I don't want to change 100 files right now...
  • Loading branch information
ducalex committed Feb 5, 2024
1 parent 9fbba7b commit 32dbcf6
Show file tree
Hide file tree
Showing 19 changed files with 36 additions and 40 deletions.
4 changes: 2 additions & 2 deletions retro-core/components/nofrendo/mappers/map004.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ static void map_write(uint32 address, uint8 value)
}
}

static void map_hblank(int scanline)
static void map_hblank(nes_t *nes)
{
if (scanline > 240)
if (nes->scanline > 240)
return;

if (!ppu_enabled())
Expand Down
12 changes: 4 additions & 8 deletions retro-core/components/nofrendo/mappers/map005.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ static uint16 chr_upper_bits;
static uint16 chr_banks_count;

static int split_tile, split_tile_number, split_region;
static int scanline;

#define IN_FRAME 0x40
#define IRQ_PENDING 0x80
Expand Down Expand Up @@ -171,19 +170,17 @@ static inline void nametable_fill()
memset(ppu_getnametable(3) + 32 * 30, (fill_mode.color | fill_mode.color << 2 | fill_mode.color << 4 | fill_mode.color << 6), 64);
}

static void map_hblank(int _scanline)
static void map_hblank(nes_t *nes)
{
scanline = _scanline;

if (scanline >= 241)
if (nes->scanline >= 241)
{
irq.status &= ~IN_FRAME; // (IN_FRAME|IRQ_PENDING)
}
else
{
irq.status |= IN_FRAME;

if (scanline == irq.scanline)
if (nes->scanline == irq.scanline)
{
if (irq.enabled) nes6502_irq();
irq.status |= IRQ_PENDING;
Expand Down Expand Up @@ -363,6 +360,7 @@ static uint8 map_vram_read(uint32 address, uint8 value)

// Reference: https://github.com/SourMesen/Mesen/blob/master/Core/MMC5.h
#if 0
int scanline = nes_getptr()->scanline;
if (exram.mode <= 1 && scanline < 240)
{
if (vert_split.enabled)
Expand Down Expand Up @@ -478,8 +476,6 @@ static void map_init(rom_t *cart)
fill_mode.color = 0xFF;
fill_mode.tile = 0xFF;

scanline = 241;

prg_mode = 3;
chr_mode = 3;

Expand Down
2 changes: 1 addition & 1 deletion retro-core/components/nofrendo/mappers/map016.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static void map_write(uint32 address, uint8 value)
}
}

static void map_hblank(int scanline)
static void map_hblank(nes_t *nes)
{
if (irq.enabled && irq.counter)
{
Expand Down
4 changes: 2 additions & 2 deletions retro-core/components/nofrendo/mappers/map019.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ static uint8 map_read(uint32 address)
}
}

static void map_hblank(int scanline)
static void map_hblank(nes_t *nes)
{
if (irq.enabled)
{
irq.counter += nes_getptr()->cycles_per_scanline;
irq.counter += nes->cycles_per_scanline;

if (irq.counter >= 0x7FFF)
{
Expand Down
4 changes: 2 additions & 2 deletions retro-core/components/nofrendo/mappers/map020.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ static void fds_cpu_timer(int cycles)
}
}

static void fds_hblank(int scanline)
static void fds_hblank(nes_t *nes)
{
fds_cpu_timer(nes_getptr()->cycles_per_scanline);
fds_cpu_timer(nes->cycles_per_scanline);
}

static uint8 fds_read(uint32 address)
Expand Down
2 changes: 1 addition & 1 deletion retro-core/components/nofrendo/mappers/map021.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ static void map_write(uint32 address, uint8 value)
}
}

static void map_hblank(int scanline)
static void map_hblank(nes_t *nes)
{
if (irq.enabled)
{
Expand Down
2 changes: 1 addition & 1 deletion retro-core/components/nofrendo/mappers/map023.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static void map_write(uint32 address, uint8 value)
}
}

static void map_hblank(int scanline)
static void map_hblank(nes_t *nes)
{
if (irq.enabled)
{
Expand Down
2 changes: 1 addition & 1 deletion retro-core/components/nofrendo/mappers/map024.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static struct
} irq;


static void map_hblank(int scanline)
static void map_hblank(nes_t *nes)
{
if (irq.enabled)
{
Expand Down
6 changes: 3 additions & 3 deletions retro-core/components/nofrendo/mappers/map031.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ static void setup_song(int song)
}

// That whole thing should be written in 6502 assembly above instead but for now I'm lazy
static void map_vblank(void)
static void map_vblank(nes_t *nes)
{
if (playing && nes_getptr()->input->state)
if (playing && nes->input->state)
{
setup_song(++current_song);
nes6502_reset();
apu_reset();
nes6502_burn(NES_CPU_CLOCK_NTSC / 2);
nes6502_burn(nes->cpu_clock / 2);
}
}

Expand Down
2 changes: 1 addition & 1 deletion retro-core/components/nofrendo/mappers/map040.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static struct
} irq;


static void map_hblank(int scanline)
static void map_hblank(nes_t *nes)
{
if (irq.enabled && irq.counter)
{
Expand Down
4 changes: 2 additions & 2 deletions retro-core/components/nofrendo/mappers/map042.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ static void map_irq_reset(void)
irq.counter = 0x0000;
}

static void map_hblank(int scanline)
static void map_hblank(nes_t *nes)
{
/* Increment the counter if it is enabled and check for strike */
if (irq.enabled)
{
irq.counter += nes_getptr()->cycles_per_scanline;
irq.counter += nes->cycles_per_scanline;

/* IRQ is triggered after 24576 M2 cycles */
if (irq.counter >= 0x6000)
Expand Down
4 changes: 2 additions & 2 deletions retro-core/components/nofrendo/mappers/map050.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ static void map_irq_reset(void)
irq.counter = 0x0000;
}

static void map_hblank(int scanline)
static void map_hblank(nes_t *nes)
{
/* Increment the counter if it is enabled and check for strike */
if (irq.enabled)
{
irq.counter += nes_getptr()->cycles_per_scanline;
irq.counter += nes->cycles_per_scanline;

/* IRQ line is hooked to Q12 of the counter */
if (irq.counter & 0x1000)
Expand Down
4 changes: 2 additions & 2 deletions retro-core/components/nofrendo/mappers/map064.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ static uint16 command = 0;
static uint16 vrombase = 0x0000;


static void map_hblank(int scanline)
static void map_hblank(nes_t *nes)
{
if (scanline >= 241)
if (nes->scanline >= 241)
return;

irq.reset = false;
Expand Down
4 changes: 2 additions & 2 deletions retro-core/components/nofrendo/mappers/map073.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ static struct
} irq;


static void map_hblank(int scanline)
static void map_hblank(nes_t *nes)
{
/* Increment the counter if it is enabled and check for strike */
if (irq.enabled)
{
irq.counter += nes_getptr()->cycles_per_scanline;
irq.counter += nes->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/map085.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ static void map_write(uint32 address, uint8 value)
}
}

static void map_hblank(int scanline)
static void map_hblank(nes_t *nes)
{
if (!irq.enabled)
return;
Expand Down
4 changes: 2 additions & 2 deletions retro-core/components/nofrendo/mappers/map160.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ static void map_write(uint32 address, uint8 value)
}
}

static void map_hblank(int scanline)
static void map_hblank(nes_t *nes)
{
if (scanline < 241)
if (nes->scanline < 241)
{
if (irq.enabled && ppu_enabled())
{
Expand Down
6 changes: 3 additions & 3 deletions retro-core/components/nofrendo/mappers/map162.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ static void map_update()
// mmc_bankvrom(8, 0x0000, 0);
}

static void map_hblank(int scanline)
static void map_hblank(nes_t *nes)
{
if ((reg5000 & 0x80))
{
if (scanline == 127)
if (nes->scanline == 127)
{
mmc_bankvrom(4, 0x0000, 1);
mmc_bankvrom(4, 0x1000, 1);
}
else if (scanline == 239)
else if (nes->scanline == 239)
{
mmc_bankvrom(4, 0x0000, 0);
mmc_bankvrom(4, 0x1000, 0);
Expand Down
4 changes: 2 additions & 2 deletions retro-core/components/nofrendo/nes/mmc.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ struct mapper_s
int number; /* mapper number */
const char *name; /* mapper name */
void (*init)(rom_t *cart); /* init routine */
void (*vblank)(void); /* vblank callback */
void (*hblank)(int scanline); /* hblank callback */
void (*vblank)(nes_t *nes); /* vblank callback */
void (*hblank)(nes_t *nes); /* hblank callback */
void (*get_state)(uint8 *state); /* get state (uint8 *(state[128])) */
void (*set_state)(uint8 *state); /* set state (uint8 *(state[128])) */
mem_read_handler_t mem_read[4]; /* memory read structure */
Expand Down
4 changes: 2 additions & 2 deletions retro-core/components/nofrendo/nes/nes.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ void nes_emulate(bool draw)
nes6502_nmi();

if (nes.mapper->vblank)
nes.mapper->vblank();
nes.mapper->vblank(&nes);
}

if (nes.mapper->hblank)
{
// Mappers use various techniques to detect horizontal blank and we can't accommodate
// all of them unfortunately. But ~86 cycles seems to work fine for everything tested.
elapsed_cycles += nes6502_execute(86 - elapsed_cycles);
nes.mapper->hblank(nes.scanline);
nes.mapper->hblank(&nes);
}

nes.cycles += nes.cycles_per_scanline;
Expand Down

0 comments on commit 32dbcf6

Please sign in to comment.