leuat / TRSE

Turbo Rascal Syntax Error full repo
GNU General Public License v3.0
241 stars 44 forks source link

Support for Bank swapping (or Far Calling in 65816) #668

Closed TheRealHamtaro126 closed 3 years ago

TheRealHamtaro126 commented 3 years ago

There is no Bank swapping/Far Calling support currently, So I decided to point it out. Can there be support? Thanks!

leuat commented 3 years ago

Hi there!

I just added support for the 65816 this week, so everything is still kind of fresh with this CPU in TRSE. Could you provide an assembly example of far calling / bank swapping? if so, I can easily add it to TRSE - either as built-in functions natively or in a unit

TheRealHamtaro126 commented 3 years ago

For Far Calling in 65816, Jumps are as simple as:

... JSL SomeCode ...

And then gets the call from the following address:

SomeCode: PHB ;REQUIRED to save bank regs PHK PLB ;Insert code here!!! PLB ;REQUIRED to restore bank regs` RTL

For 6502 it requires a more complicated measure called Bank Switching, Which requires a few register writes, differing depending on system and the mapper used. such as NES and MMC1/MMC3/VRC6/UxROM regs (can only use one), The CPU Selection map of C64, ect.

Examples can be found for https://wiki.nesdev.com/w/index.php?title=Programming_MMC1 and https://wiki.nesdev.com/w/index.php?title=Programming_UNROM

leuat commented 3 years ago

Hi there!

While TRSE now supports some new wdc65c816 stuff such as lpointers (3-byte long pointers), and data being placed at bank(3) etc, I'm still not sure how I should approach this request. Could you provide some more information about how you envision various commands / how to structure this? I mean, I can easily implement a "fcall" to simply produce a "jsl", but I need some more input on actual implementations. For instance, what about "far" procedures declared at other ?

procedure MyFarProcedure(SomeValue:byte) bank(3); <- by being declared at another bank, it automatically gets "phb, phk" etc + "plb rtl" at the end.. is this something you intended?

leuat commented 3 years ago

I got this almost working - implementing all jumps as far jumps in TRSE - but I'm experiencing some strange discrepancies when writing to various SNES registers, even when having all code in bank 0. Everything compiles and runs, but data gets scrambled - but if I simply replace all the "jsl/rtl" with "jsr/rts", then all the data is presented correctly. Any ideas what might be the case?

leuat commented 3 years ago

well - seems to work now - even though there are some irregularities. Will fix this when those show up. if you notice anything incorrect, don't hesitate reopening this issue!

Here's how you put code far away:

procedure MyFarCode() bank(3) ; // specify bank

all calls are now jsl/rtl, and all methods start with phb / plb etc. Will fix up all the extra sep/rep etc later