ams-hackers / gbforth

👾 A Forth-based Game Boy development kit
https://gbforth.org
MIT License
128 stars 24 forks source link

Research bank switching #36

Open davazp opened 6 years ago

davazp commented 6 years ago

Use vocabularies?

davazp commented 6 years ago

Another option very Forth-like would be to use blocks

ekipan commented 1 year ago

How much consideration have you given this? I don't know about vocabularies but I do have a sketch in my head for a design. Bearing in mind that I've never programmed a Game Boy before, gbforth's compiler design is still way over my head, and ideas are cheap of course:

The bulk of the code for each word should live in ROM banks 1-N, and every public word in every bank will have a corresponding stub in bank 0 containing only the instructions needed to bankswitch and jump to that word's main code. That way words can call each other across banks at the cost of a handful of ROM 0 bytes.

You'd only store a word's main code in ROM 0 if it were smaller than a stub, or if it needed to access data across banks (loading and initialization code?), or if the words were very common and you wanted to save the cost of a jump.

If you ever decide to provide an interpreter and compiler on target, presumably it would work similarly: compiling most code to WRAM banks 1-7 and putting stubs and the nametoken list in WRAM 0. I also have a sketch for a text input method using the 8 buttons. With a real terminal and maybe a blocks editor the Game Boy could be a self-contained forth devel environment.

tkers commented 1 year ago

@ekipan I don't think we ever got deep into this as we managed to fit most demos into 32KB anyway. I once tried to see if we could easily support some basic bank-switching words (i.e. even without the compiler automatically handle the splitting), but got stuck trying to (unsuccessfully) flash an MBC cart :')

Would love to try and support it in the future (also the on-device interpreter you mentioned, although that should probably be behind a flag/on a different branch?), but we're not actively working on this right now.

ekipan commented 1 year ago

A little more thought and I guess a better way to do it would be to have something like a bankcall in ROM0 that takes a callee bank number and address and saves the bank of the calling word onto the rstack before switching and calling. Probably there are even better ways to do it. I was just excited about my design and felt like I needed to share it. It wasn't a good one in hindsight.