sverx/devkitSMS

Using PSGlib with banked code

MorenoIgor opened this issue · 4 comments

Hey there! I'd like to ask for some help with integrating PSGlib onto a project while using banked code.

Up until now I had been compiling my project like this:
sdcc -c -mz80 vania.c sdcc -c -mz80 --constseg BANK2 assets.c sdcc -c -mz80 --codeseg BANK1 xtra.c sdcc -o vania.ihx -mz80 --no-std-crt0 --opt-code-speed --data-loc 0xC000 -Wl-b_BANK1=0x14000 -Wl-b_BANK2=0x28000 crt0b_sms.rel SMSlib.lib PSGlib.rel vania.rel xtra.rel assets.rel makesms vania.ihx build/vania.sms

So bank1 contains banked code and bank2 contains assets. It was working perfectly, but I decided to add one extra bank for code and another for data, so I could better understand the process. So it went:

sdcc -c -mz80 vania.c sdcc -c -mz80 --codeseg CODEBANK1 xtra.c sdcc -c -mz80 --codeseg CODEBANK2 newcode.c sdcc -c -mz80 --constseg DATABANK3 assets.c sdcc -c -mz80 --constseg DATABANK4 tilemap.c sdcc -o vania.ihx -mz80 --no-std-crt0 --data-loc 0xC000 -Wl-b_CODEBANK1=0x14000 -Wl-b_CODEBANK2=0x24000 -Wl-b_DATABANK3=0x38000 -Wl-b_DATABANK4=0x48000 crt0b_sms.rel SMSlib.lib vania.rel xtra.rel newcode.rel assets.rel tilemap.rel makesms vania.ihx build/vania.sms

However, I found out (after way too long, haha) that the only way I could get the ROM to boot at all was by removing PSGlib from the project. It does boot, however there are some glitches due to (I guess) some overlap in RAM due to the lack of PSGlib.

So upon checking the readme for devkitSMS, there are no examples in which banked code is used alongside PSGlib. I've tried inserting the code for the library manually into my main file, to no avail. However, I copied the small example for banked code on SMSPower, added PSGlib to the command line and got it to work fine. (I should note however that I did not test if the library itself worked)

I noticed that apparently PSGlib located itself on bank0 in this test project, so I thought that maybe there was not enough room on bank0, although I doubt this is the case since theres is 4K+ left, and when using PSGlib before it still had 3K+.

So finally, my question is whether it's even possible to use PSGlib with banked code, if there's some detail I'm missing, etc.

Thanks in advance! =D

Hi - I'm not sure if this will help but I had some initial issues getting the banked code to work but managed to get a "Hello World" type sample working with SDCC 4.1.0. The code can be found here: https://github.com/StevePro7/SegaMasterSystemLinux/tree/main/NewBanking/04-Banks/dev

Thanks, Steve! I had the banked code working before, and it still works if I remove PSGlib, but I had only one codeseg.

After messing around a bit more I was able to run the start of the code with PSGlib present: I uploaded some data to VRAM by mapping the appropriate data bank using SMS_mapROMBank(), using both local and banked code. In both instances the data was garbled. By removing PSGlib once again, it worked fine.

So I guess it's something to do with where in the code PSGlib resides...?

As an update, I managed to include PSGlib by using the source files - it appears that my problem had to do with the headers, although I couldn't figure out what exactly went wrong, just redid some things. But the question still remains: whether there is a way to compile PSGlib directly from the library when using banked code or if the source is the better option.

sverx commented

If you want to use PSGlib and banked code there are two options:
The easy one is: you use the provided PSGlib.rel and you link that with no specific instructions to the linker, so that the library code will end up in bank number 0, that is the code that never gets paged out.
If your code in bank number 0 doesn't allow PSGlib to fit, then you either have to move some of your code to a separate code bank OR you have to declare PSGlib as banked code, but that requires fixing the sources, adding the keyword __banked to each PSGlib functions.
I would try the first option first.