JohnEarnest / Octo

A Chip8 IDE
MIT License
664 stars 53 forks source link

Solving the difficulty of writing program's code beyond 4k with banking #165

Closed Kouzeru closed 2 years ago

Kouzeru commented 2 years ago

A new keyword ":offset NNNN" will subtract any new addresses and branches assigned after it by the specified NNNN until the compiler met another ":offset", hence one don't need to write separate program in order to do multiple banks of "program code".

For example, when program code located on some long address "0x2000", and one wish to execute it on "0x800", using keyword ":offset 0x1800" will subtract the new addresses by "0x1800":

: main
  i := long codeonlong  load vF
  i := bankedroutine00  save vF
  i := long lockuponlong  load v4
  i := bankedroutine10  save v4
  jump bankedroutine00

: getrandom
  v0 := random 63
  v1 := random 31
  return

:org 0x800
: bankedroutine00
:org 0x900
: bankedroutine10

:org 0x2000 # place the routine at beyond 0xFFF
:offset 0x1800 # subtract addresses and branches by 0x1800 [0x2000-0x800], to run the routine at 0x800
: codeonlong
  loop
    getrandom # call a predefined subroutine below 0x1000
    if v0 == v1 then
      jump outloop # jump to address which meant to exit the loop
    i := hex v0
    sprite v0 v1 5
  again
: outloop
  jump bankedroutine01

:org 0x3000 # place another routine at beyond 0xFFF
:offset 0x2700 # subtract addresses and branches by 0x2700 [0x3000-0x900], to run the routine at 0x900
: lockuponlong
  loop  again  # lockup

The offset could be reset anytime by ":offset 0", as the offset is 0 by default on the start of compilation.

The ":offset" also may accept constant, or any variables defined by ":calc".


In case of some forward references, ":offset" only affects new resolved addresses.

JohnEarnest commented 2 years ago

I've given this some thought, and I've decided it's too application-specific. In practice very few people have written programs that need bank-switching, and there are already several techniques available. I'm not adding this feature at this time.