Possible optimization for UNROM code

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
User avatar
DRW
Posts: 2225
Joined: Sat Sep 07, 2013 2:59 pm

Possible optimization for UNROM code

Post by DRW »

In https://wiki.nesdev.com/w/index.php/Programming_UNROM there is the following code:

Code: Select all

bankswitch_y:
  sty current_bank      ; save the current bank in RAM so the NMI handler can restore it
bankswitch_nosave:
  lda banktable, y      ; read a byte from the banktable
  sta banktable, y      ; and write it back, switching banks
  rts
In line 4, wouldn't TYA do the same (since the values in banktable equal their offset value), but be shorter?
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Possible optimization for UNROM code

Post by lidnariq »

For mappers that are not UNROM, where there are bits in the mapper register that are not contiguous (for example, GNROM), there may be value in keeping a densely-packed version in memory, and using the look-up table to expand the bits at run-time, allowing for a smaller total look-up table.

This is irrelevant for UNROM, though.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Possible optimization for UNROM code

Post by tepples »

True, tya can be faster. But reading the bank table before writing lets you use a non-identity mapping. For example, you can watermark your ROM by shuffling banks within the ROM, not unlike source code shuffling.

In my most ambitious UNROM production (240p Test Suite), I've tended to switch banks by overwriting the operand of an immediate load instruction (LDA, LDX, or LDY), like this:

Code: Select all

lda #<.BANK(some_label)
sta *-1
Or when the bank number is in a table (the BNROM version of Action 53 does this a lot):

Code: Select all

  lda sb53_files,y
  sta ciSrc
  lda sb53_files+1,y
  sta ciSrc+1
  lda sb53_files+2,y
  sta sb53_files+2,y  ; switch bank
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Possible optimization for UNROM code

Post by tokumaru »

Look at AxROM for example, where there's a gap between the PRG bank bits and the mirroring bit. You can get rid of that gap and use a 16-byte look-up table instead of using a 32-byte table if you do it like in the example code you posted.
User avatar
Bregalad
Posts: 8056
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Re: Possible optimization for UNROM code

Post by Bregalad »

DRW wrote:In https://wiki.nesdev.com/w/index.php/Programming_UNROM there is the following code:

Code: Select all

bankswitch_y:
  sty current_bank      ; save the current bank in RAM so the NMI handler can restore it
bankswitch_nosave:
  lda banktable, y      ; read a byte from the banktable
  sta banktable, y      ; and write it back, switching banks
  rts
In line 4, wouldn't TYA do the same (since the values in banktable equal their offset value), but be shorter?
Code examples on the wiki are consistently shitty and I wouldn't bother using it. You should really code your own bankswitch routine based on a case-by-case scenario.
Look at AxROM for example, where there's a gap between the PRG bank bits and the mirroring bit. You can get rid of that gap and use a 16-byte look-up table instead of using a 32-byte table if you do it like in the example code you posted.
Which is probably why bus-conflicts elimination is often implemented on AxROM boards - with a 74HC02 chip on ANROM board and with a special mask ROM on some AOROM boards.
User avatar
DRW
Posts: 2225
Joined: Sat Sep 07, 2013 2:59 pm

Re: Possible optimization for UNROM code

Post by DRW »

lidnariq wrote:For mappers that are not UNROM
Yeah, but that page is specifically for programming UNROM, that's why I mentioned it.
Bregalad wrote:Code examples on the wiki are consistently shitty and I wouldn't bother using it. You should really code your own bankswitch routine based on a case-by-case scenario.
Yeah, I mentioned it as a possible code improvement for the wiki.
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
Post Reply