Difference between revisions of "Free call to BIOS routine"

From MSX Game Library

 
(4 intermediate revisions by the same user not shown)
Line 15: Line 15:
 
Here is a summary of all possible the combinations:
 
Here is a summary of all possible the combinations:
  
−
<html><img width="900px" src="https://cdn.discordapp.com/attachments/574951023374761988/995082329997443184/unknown.png" /></html>
+
{| class="wikitable"
 +
|-
 +
! !!__sdcccall(0) !!__sdcccall(1) !!__z88dk_fastcall
 +
|-
 +
|colspan="4" style="text-align:center;"|RETURN
 +
|-
 +
|8 bits ||L ||A ||L
 +
|-
 +
|16 bits ||HL ||DE ||HL
 +
|-
 +
|32 bits ||DE-HL ||HL-DE ||DE-HL
 +
|-
 +
|colspan="4" style="text-align:center;"|PARAMETERS
 +
|-
 +
|8 bits ||Stack ||A ||L
 +
|-
 +
|16 bits ||Stack ||HL ||HL
 +
|-
 +
|32 bits ||Stack ||HL-DE ||DE-HL
 +
|-
 +
|8 + 8 bits ||Stack ||A + L ||''Invalid''
 +
|-
 +
|8 + 16 bits ||Stack ||A + DE ||''Invalid''
 +
|-
 +
|16 + 16 bits ||Stack ||HL + DE ||''Invalid''
 +
|-
 +
|16 + 8 bits ||Stack ||HL + Stack ||''Invalid''
 +
|}
 +
 
  
 
You can found many examples in <tt>engine\src\bios.h</tt>.
 
You can found many examples in <tt>engine\src\bios.h</tt>.

Latest revision as of 20:35, 22 September 2026

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.

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 register A, call the GTSTCK function, then read the result into register 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:

__sdcccall(0) __sdcccall(1) __z88dk_fastcall
RETURN
8 bits L A L
16 bits HL DE HL
32 bits DE-HL HL-DE DE-HL
PARAMETERS
8 bits Stack A L
16 bits Stack HL HL
32 bits Stack HL-DE DE-HL
8 + 8 bits Stack A + L Invalid
8 + 16 bits Stack A + DE Invalid
16 + 16 bits Stack HL + DE Invalid
16 + 8 bits Stack HL + Stack Invalid


You can found many examples in engine\src\bios.h.