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.

#1 2020-04-25 10:27:44

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

transparent far function calls in sdcc4 and gbdk

hi!

I was digging into the sdcc, and found out, that transparent bank switching is almost working!
It was always! The only  thing that was missing - a correct bank number in the structure, that
is generated for  every far call. The trampoline functions always existed in crt, and there is
no problem with them. You may even call a subroutine  in bank2 from a subroutine in bank1, and
that all works perfectly, the banks  are saved, switch is done at the proper moment, and banks
restored after all correctly!

So, i wrote a very simple postprocessor, that identifies the bank number of a symbol and fills
the compiler-magic structure with the correct values.

This postprocessor takes names of object files, which are passed through a command line, loads
symbols from them, make a "name --> bank number" dict, then takes the last object file, parses
it (it has a trivial structure) and patches the bank number in the object file and outputs the
result to stdout.

The patch target is:

Code:

call banked_call
dw <far_function_offset>
dw 0                     <--- HERE

Postprocessor identifies a call  to banked_call, ensures that  it is really a call, identifies
that there is a

Code:

dw <far_function_offset>

after it (another reloc), finds a bank in the "name --> bank number" dict, patches the

Code:

dw <bank>

with a bank number.

This is the early alpha version  of a postprocessor, it may contain some bugs, so i appriciate
your help with testing and fixing it.

So. In your code, everything you should do is to declare a function, with a  __banked keyword.
Every call to such function is made by SDCC through a trampoline function call generated by it
and is fixed by my postprocessor then. Watch  through the make.bat  script for  the details on
how it is done.

Your code in some .c file that is may be compiled, say, with -bo2 switch or a #pragma bank 2:

Code:

int some_bank2_proc(int a, int b, int c) __banked {
    printf("  in %s\n", hello2);
    return a + b + c;
}

and in the other place, say main(), you just call this function:

Code:

int somevar = some_bank2_proc(16, 32, 64);

Compile both files. Fix the  object file from which  the  far call is made, with far_fixer.py,
by passing BOTH objects to it in right order. You must fix EVERY object file, that contain far
calls (only one in this example). If you forget one, there will be a crash. Then you just link
patched files.

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

any questions? welcome.

Last edited by toxa (2020-04-25 15:59:57)

Offline

 

#2 2020-04-25 16:18:38

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

Re: transparent far function calls in sdcc4 and gbdk

this is the difference, between the sdcc-generated object file and a patched object file:

https://raw.githubusercontent.com/untoxa/sdcc4_farcalls_example/master/screenshot.png

Offline

 

#3 2020-04-25 17:32:37

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

Re: transparent far function calls in sdcc4 and gbdk

i just fixed a vexing bug in far_fixer

Offline

 

#4 2020-04-28 13:51:28

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

Re: transparent far function calls in sdcc4 and gbdk

i fixed one more issue, when the target function is in the same file. not an extern. v0.2 now.

Offline

 

#5 2020-05-04 14:55:13

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

Re: transparent far function calls in sdcc4 and gbdk

one more issue with object-local banked functions fixed. v0.3a now.

Offline

 

#6 2020-05-16 05:03:28

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

Re: transparent far function calls in sdcc4 and gbdk

I have added this patch to the link-gbz80 in gbdk. Right now it patches all .o and .rel files and all files declared withing libs

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson