Difference between revisions of "Free call to BIOS routine"

From MSX Game Library

(Created page with "Here is a technique to remove 100% of the C language overhead on calling some of the [https://www.msx.org/wiki/Main-ROM_BIOS BIOS routines]. The idea is to "cast" an address...")
(No difference)

Revision as of 22:57, 8 July 2022

Here is a technique to remove 100% of the C language overhead on calling some of the BIOS routines.

The idea is to "cast" an address with the signature of a C function so that the calling code initializes the registers properly before calling the BIOS function. For example, the function GTSTCK (00D5h) which returns the status of the joystick, takes its input parameter in A and returns its final value in A. Among the possible function signatures, (u8(*)(u8)) uses these same registers (u8 is unsigned char). Thus, with this function definition...

inline u8 Bios_GetJoystickDirection(u8 port) { return ((u8(*)(u8))R_GTSTCK)(port); }

...the calling code will put the port number into A, call the GTSTCK function, then read the result into A. Since the function is inline, we gain C typing verification, while having zero C overhead.

Obviously, this only works with BIOS routines that use registers for which there is a C function signature. There are few of them, but by playing with the sdcccall1 and z88dk_fastcall calling conventions, we still have some possibilities. In the case of a z88dk_fastcall signature you have to use a typedef to define the signature before you can use it. Here is a summary of all possible the combinations: