Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

Ads

#1 2020-04-22 07:43:37

toxa
Member
Registered: 2020-02-13
Posts: 305

an easy way to deal with rom and ram banks in gbdk+sdcc4

there is a nice way to declare and use constants and variables that are located in various ROM and RAM banks and access the data in them transparently. all you need is to declare them in special way and use an old linker because the one in sdcc is broken.

GITHUB: https://github.com/untoxa/sdcc4_banks_example

any questions? welcome!

Last edited by toxa (2020-04-23 06:03:07)

Offline

 

#2 2020-04-22 09:34:29

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 103
Website

Re: an easy way to deal with rom and ram banks in gbdk+sdcc4

This should just work in gbdk 3.0, right?

Offline

 

#3 2020-04-22 09:49:33

toxa
Member
Registered: 2020-02-13
Posts: 305

Re: an easy way to deal with rom and ram banks in gbdk+sdcc4

this depends on the version of sdcc only. not the library. the answer is yes, it should work just perfectly with gbdk3.0. all this is all about the elimination of the "#pragma bank N" and "-b0N". you just declare constants/variables and that's all. it simplifys the build scripts also.

the only that make.bat do - produces just two commands:

Code:

sdcc -mgbz80 --no-std-crt0 -Dnonbanked= -I ..\..\gbdk\include -I ..\..\gbdk\include\asm -I src\include -c -DUSE_SFR_FOR_REG sdcc4banks.c -o build\sdcc4banks.rel

..\..\gbdk\bin\link-gbz80.exe -n -- -z -m -j -yt2 -yo4 -ya4 -k..\..\gbdk\lib\small\asxxxx\gbz80\ -lgbz80.lib -k..\..\gbdk\lib\small\asxxxx\gb\ -lgb.lib  sdcc4banks.gb ..\..\gbdk\lib\small\asxxxx\gb\crt0.o build\sdcc4banks.rel

where sdcc is an sdcc4, and link-gbz80 is a ye olde linker

ps: if you were asking if this works with a new gbdk ONLY? no. this example will work with ANY gbdk. only modern SDCC is needed.

Last edited by toxa (2020-04-22 09:53:27)

Offline

 

#4 2020-04-22 09:57:03

toxa
Member
Registered: 2020-02-13
Posts: 305

Re: an easy way to deal with rom and ram banks in gbdk+sdcc4

pps: AH! MY BAD!! to make this work with an aincient GBDK, like 2.95, please remove the -DUSE_SFR_FOR_REG because the syntax of declaring sfr's had changed in new sdcc.

Last edited by toxa (2020-04-22 09:57:35)

Offline

 

#5 2020-04-22 12:32:45

toxa
Member
Registered: 2020-02-13
Posts: 305

Re: an easy way to deal with rom and ram banks in gbdk+sdcc4

added switch_to() macro, see above.

Offline

 

#6 2020-04-22 15:28:30

toxa
Member
Registered: 2020-02-13
Posts: 305

Re: an easy way to deal with rom and ram banks in gbdk+sdcc4

added RESTORE_BANKS macro. current bank numbers are saved, and if you have an interrupt (gbt_player, for example), that switches the bank voluntarily, you just add RESTORE_BANKS; at the end of an interrupt routine that rollbacks all bank switching were made during the interrupt.

if you want to switch banks IN THE INTERRUPT, you MUST use regular SWITCH_ROM_MBC1/SWITCH_RAM_MBC1, and if you want to access the "banked" variables declared with __addressmod IN THE INTERRUPT, you MUST ALWAYS cast them directly (char *)hello2 to avoid transparent bank switching. never use: a = hello2[0]; always use: a = ((char *)hello2)[0];

Last edited by toxa (2020-04-22 15:57:08)

Offline

 

#7 2020-04-23 06:03:52

toxa
Member
Registered: 2020-02-13
Posts: 305

Re: an easy way to deal with rom and ram banks in gbdk+sdcc4

i made a repo to keep this up-to-date: https://github.com/untoxa/sdcc4_banks_example

Offline

 

#8 2020-04-25 11:15:08

toxa
Member
Registered: 2020-02-13
Posts: 305

Re: an easy way to deal with rom and ram banks in gbdk+sdcc4

hi, guys!

you know an old anecdote about one man, who prayed all his life about the winning the lottery, and before his death, he asked god "why didn't you do this for me?". and the voice from the skies said: "why didn't you ever buy a ticket"?!

I found a bug in sdcc, that results in the incorrect bank switching: https://sourceforge.net/p/sdcc/bugs/2995/, posted a bug-report two days ago, it is already fixed today. I wonder, did anyone reported bugs of an original linker?!

May be we just need to buy (create) a ticket?

Offline

 

#9 2020-04-26 11:12:22

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 103
Website

Re: an easy way to deal with rom and ram banks in gbdk+sdcc4

That sounds like a good idea actually

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson