Skip to content

Commit

Permalink
[NSF] "Kirby's Adventure.nsfe" tries to read the memory areas $FFFA a…
Browse files Browse the repository at this point in the history
…nd $FFFB even though it shouldn't since they are reserved for the NSF Player ("The vector table at $FFFA-FFFF should not be filled with code or data by the NSF. These can be overridden by hardware NSF players."). I have added the management of this eventuality (#386).
  • Loading branch information
punesemu committed Mar 18, 2024
1 parent 1c9567a commit a51c9f4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 37 deletions.
14 changes: 10 additions & 4 deletions src/core/cpu_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,19 @@ INLINE static void nsf_rd_mem(BYTE nidx, WORD address, BYTE made_tick) {
case 0xFFFA:
if (nsf.state & NSF_CHANGE_SONG) {
nes[nidx].c.cpu.openbus = NSF_ROUTINE_NSF_INIT & 0xFF;
} else {
return;
} else if (nsf.nmi.exec) {
nes[nidx].c.cpu.openbus = NSF_ROUTINE_NMI & 0xFF;
return;
}
return;
break;
case 0xFFFB:
nes[nidx].c.cpu.openbus = NSF_ROUTINE_START >> 8;
return;
if ((nsf.state & NSF_CHANGE_SONG) || nsf.nmi.exec) {
nes[nidx].c.cpu.openbus = NSF_ROUTINE_START >> 8;
nsf.nmi.exec = FALSE;
return;
}
break;
case 0xFFFC:
nes[nidx].c.cpu.openbus = NSF_ROUTINE_RESET & 0xFF;
return;
Expand Down
3 changes: 3 additions & 0 deletions src/core/mappers/mapper_NSF.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void map_init_NSF(void) {
select_region_NSF();
nsf.rate.count = nsf.rate.reload;
nsf.nmi.count = nsf.rate.reload;
nsf.nmi.exec = FALSE;
nsf.nmi.in_use = FALSE;

nsf.adr.loop = NSF_ROUTINE_LOOP;
Expand Down Expand Up @@ -129,6 +130,7 @@ BYTE extcl_save_mapper_NSF(BYTE mode, BYTE slot, FILE *fp) {

save_slot_ele(mode, slot, nsf.rate.count);

save_slot_ele(mode, slot, nsf.nmi.exec);
save_slot_ele(mode, slot, nsf.nmi.in_use);
save_slot_ele(mode, slot, nsf.nmi.count);

Expand Down Expand Up @@ -219,6 +221,7 @@ void extcl_cpu_every_cycle_NSF(BYTE nidx) {
nsf.nmi.count = nsf.nmi.reload;

nes[0].c.nmi.high = !nsf.nmi.in_use;
nsf.nmi.exec = nes[0].c.nmi.high;
nes[0].p.ppu.odd_frame = !nes[0].p.ppu.odd_frame;
nes[0].p.ppu.frames++;

Expand Down
69 changes: 36 additions & 33 deletions src/core/nsf.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ enum nsf_states {
NSF_RESTART_SONG = 0x400,
};
enum nsf_routine_addresses {
NSF_ROUTINE_SIZE = 0x54,
NSF_ROUTINE_SIZE = 0x56,
NSF_ROUTINE_START = 0x2500,
NSF_ROUTINE_END = NSF_ROUTINE_START + (NSF_ROUTINE_SIZE - 1),
NSF_ROUTINE_CLEAR_ALL = 0x2518,
NSF_ROUTINE_LOOP = 0x2551,
NSF_ROUTINE_CLEAR_ALL = 0x251A,
NSF_ROUTINE_LOOP = 0x2553,
NSF_ROUTINE_NMI = 0x2500,
NSF_ROUTINE_NMI_RTI = 0x2514,
NSF_ROUTINE_NORMAL = 0x253A,
NSF_ROUTINE_NSF_INIT = 0x2525,
NSF_ROUTINE_RESET = 0x2515,
NSF_ROUTINE_YES_PLAY = 0x2521,
NSF_ROUTINE_NMI_RTI = 0x2516,
NSF_ROUTINE_NORMAL = 0x253C,
NSF_ROUTINE_NSF_INIT = 0x2527,
NSF_ROUTINE_RESET = 0x2517,
NSF_ROUTINE_YES_PLAY = 0x2523,

NSF_DATA_SIZE = 0x0B,
NSF_DATA_START = 0x2600,
Expand Down Expand Up @@ -110,6 +110,7 @@ typedef struct _nsf {
DBWORD reload;
} rate;
struct _nsf_nmi {
BYTE exec;
BYTE in_use;
DBWORD count;
DBWORD reload;
Expand Down Expand Up @@ -211,7 +212,8 @@ static const BYTE nsf_routine[NSF_ROUTINE_SIZE] = {
;$2609-$260A - loop address
.org $2500
NMI: PHA
NMI: PHP
PHA
LDA $2605
BEQ NMI_END
NMI_NRET: TXA
Expand All @@ -226,6 +228,7 @@ NMI_NRET: TXA
PLA
TAX
NMI_END: PLA
PLP
NMI_RTI: RTI
RESET: JMP LOOP
CLEAR_ALL: RTS
Expand Down Expand Up @@ -255,32 +258,32 @@ INIT_NRET: LDY #$80
LOOP: JMP ($2609)
.end
CLEAR_ALL $2518
INIT_NRET $2540
LOOP $2551
CLEAR_ALL $251A
INIT_NRET $2542
LOOP $2553
NMI $2500
NMI_END $2513
NMI_NRET $2506
NMI_RTI $2514
NORMAL $253A
NSF_INIT $2525
RESET $2515
_INIT $2519
_NO_PLAY $2524
_PLAY $251C
_YES_PLAY $2521
NMI_END $2514
NMI_NRET $2507
NMI_RTI $2516
NORMAL $253C
NSF_INIT $2527
RESET $2517
_INIT $251B
_NO_PLAY $2526
_PLAY $251E
_YES_PLAY $2523
*/
0x48, 0xAD, 0x05, 0x26, 0xF0, 0x0D, 0x8A, 0x48,
0x98, 0x48, 0x78, 0x20, 0x1C, 0x25, 0x58, 0x68,
0xA8, 0x68, 0xAA, 0x68, 0x40, 0x4C, 0x51, 0x25,
0x60, 0x6C, 0x00, 0x26, 0xAD, 0x06, 0x26, 0xD0,
0x03, 0x6C, 0x02, 0x26, 0x60, 0x20, 0x18, 0x25,
0xA2, 0xFF, 0x9A, 0x18, 0xAD, 0x07, 0x26, 0xAE,
0x08, 0x26, 0xAC, 0x05, 0x26, 0xD0, 0x09, 0x20,
0x19, 0x25, 0x20, 0x1C, 0x25, 0x4C, 0x51, 0x25,
0xA0, 0x80, 0x20, 0x19, 0x25, 0x58, 0xAD, 0x07,
0x26, 0xAE, 0x08, 0x26, 0xA0, 0x81, 0x20, 0x19,
0x25, 0x6C, 0x09, 0x26
0x08, 0x48, 0xAD, 0x05, 0x26, 0xF0, 0x0D, 0x8A,
0x48, 0x98, 0x48, 0x78, 0x20, 0x1E, 0x25, 0x58,
0x68, 0xA8, 0x68, 0xAA, 0x68, 0x28, 0x40, 0x4C,
0x53, 0x25, 0x60, 0x6C, 0x00, 0x26, 0xAD, 0x06,
0x26, 0xD0, 0x03, 0x6C, 0x02, 0x26, 0x60, 0x20,
0x1A, 0x25, 0xA2, 0xFF, 0x9A, 0x18, 0xAD, 0x07,
0x26, 0xAE, 0x08, 0x26, 0xAC, 0x05, 0x26, 0xD0,
0x09, 0x20, 0x1B, 0x25, 0x20, 0x1E, 0x25, 0x4C,
0x53, 0x25, 0xA0, 0x80, 0x20, 0x1B, 0x25, 0x58,
0xAD, 0x07, 0x26, 0xAE, 0x08, 0x26, 0xA0, 0x81,
0x20, 0x1B, 0x25, 0x6C, 0x09, 0x26
};
#endif

Expand Down

0 comments on commit a51c9f4

Please sign in to comment.