I've been thinking about making a generic template CA65 (like I posted here), one where the user could switch mapper, PRG size, CHR size etc easily. I'm tentatively calling this "xnes". It would consist of two files: xnes.inc and xnes.cfg. Thanks to Wave for the inspiration.
It works something like this:
xnes_build_ines is a macro that takes PRG and CHR size (in kilobytes) and flags as parameters. Based on these it builds iNES header and exports symbols for the linker config file which will make the output file size match the given PRG and CHR size. The macro can give diagnostic warnings for stuff like non-power-of-two PRG sizes or invalid mapper configs.
There are some design decisions to be made, though. I was wondering if anybody would have opinions on them:
It works something like this:
Code:
.include "xnes.inc"
xnes_build_ines 64, 0, xnes::mapper::UxROM | xnes::mirroring::VERTICAL | xnes::SRAM_BATTERY
xnes_build_ines 64, 0, xnes::mapper::UxROM | xnes::mirroring::VERTICAL | xnes::SRAM_BATTERY
xnes_build_ines is a macro that takes PRG and CHR size (in kilobytes) and flags as parameters. Based on these it builds iNES header and exports symbols for the linker config file which will make the output file size match the given PRG and CHR size. The macro can give diagnostic warnings for stuff like non-power-of-two PRG sizes or invalid mapper configs.
There are some design decisions to be made, though. I was wondering if anybody would have opinions on them:
- Should bank (segment) size stay same regardless of mapper (e.g. 8K) or change depending on the chosen mapper? IMO it should vary, otherwise using, for example, NROM would require placing stuff in 4 different segments, possibly wasting space. The con is that switching mappers becomes harder.
- AxROM has 32K banking, so boot code (for switching to known bank) and IRQ vectors need to be placed in every bank. The macro can automatize all of that if NMI, RESET and IRQ vectors were passed to it... also passing them to the macro is kinda cool because placeholders can be provided if user leaves them empty. This would change the code to something like: Code:.include "xnes.inc"
.import my_nmi, my_reset, my_irq
xnes_build_ines 64, 0, xnes::mapper::UxROM | xnes::mirroring::VERTICAL | xnes::SRAM_BATTERY, my_nmi, my_reset, my_irq - xnes could provide size optimized, well tested reset/init routine that initializes EVERYTHING to known state (incl. PPU/APU) before jumping to user specified entry point. This could be overridden if user so desires.
- Another problem with 32K banking: IRQ handlers would also need to be duplicated across banks unless the programmer makes sure IRQs can only trip when the correct bank is mapped in. One solution for this would be to have xnes duplicate some trampoline code (indirect JMP, 5 cycles isn't too bad).
- MMC1 problem: should the programmer be allowed to switch between PRG config (16K/32K and what memory area is fixed) at runtime? One negative side to this is that there's no way to detect (without CPU cost) whether user is using the mapping macros/functions incorrectly.
- How should the mapping macros be named? I was thinking something like map_4K_CHR_at_0000, map_8K_PRG_at_8000, etc. I like verbose names. User gets a compile time error if the chosen mapper doesn't support the requested functionality.
- Problem I thought of just now: the mapping macros need to come from the .inc file, so when including it it needs to know what mapper was selected to define the correct macros. So the mapper might have to be specified with a .define/something before including xnes.inc. This sucks because it'd have to be done every time xnes.inc was included. However programmer could place all of that to a general include file of his own...Code:XNES_MAPPER = 2 ; bohoo, don't have .enums to use :(
.include "xnes.inc"
; mapping macros are now defined
This wouldn't be needed if functions were used instead of macros. I think it might be the best solution.. it's not too often that you can't spare couple of cycles for JSR/RTS. And in that case you can always write to the mapper regs manually.