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-05-25 10:16:10

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

far pointer support for GBDK-2020.

GBDK-2020 v3.1.1 now supports transparent bank switching when calling __banked functions directly. Unfortunately it is impossible to call those functions by pointer, because pointers do not contain information about the banks. I made a library, that allows to contruct "far pointers" and call __banked functions indirectly with the help of some macros.

Far pointer type and helper macros are defined in far_ptr.h.

FAR_PTR is a "far pointer" type. low word contains a regular "near pointer", and high
    word - the "segment" (or bank number).

TO_FAR_PTR(ofs, seg) macro that constructs a far pointer at compile time

FAR_SEG(ptr) gets a segment from a far pointer
FAR_OFS(ptr) gets an offset from a far pointer
FAR_FUNC(ptr, type) gets an offset from a far pointer and casts it to the given type

FAR_CALL(ptr, typ, ...) calls the function of a given type indirect by the given far
    pointer and with provided parameters. banks are switched and restored
    transparently.

to_far_ptr(ofs, seg) function that constructs a far pointer at runtime
Usage:

Code:

FAR_PTR farptr_var0 = TO_FAR_PTR(&some_bank1_proc1, sdcc4bank1code_bank);
res = FAR_CALL(farptr_var1, some_bank1_proc_t, 100, 50);

where some_bank1_proc1 is a __banked function located in sdcc4bank1code_bank, and some_bank1_proc_t is a typedef of that function. You may specify the type directly like this:

Code:

FAR_CALL(farptr_var2, void (*)(void));

where farptr_var2 is a far pointer to some __banked function without parameters and which is not returning result.

Link to github: https://github.com/untoxa/sdcc4_farptr_example

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson