gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-lib/examples/cross-platform/banks_farptr/src/banks_farptr.c
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <gbdk/platform.h>
4 #include <gbdk/far_ptr.h>
5
6 // functions from bank2code.c
7 BANKREF_EXTERN(some_bank2_proc0)
8 extern void some_bank2_proc0(void) BANKED;
9
10 BANKREF_EXTERN(some_bank2_proc1)
11 extern int some_bank2_proc1(uint8_t param1, uint8_t param2) BANKED;
12 typedef int (*some_bank2_proc_t)(uint8_t, uint8_t) BANKED; // define type for some_bank2_proc1() function
13
14 BANKREF_EXTERN(some_bank2_proc2)
15 extern int some_bank2_proc2(uint8_t param1, uint8_t param2, uint8_t param3) BANKED REENTRANT;
16
17 // far pointers
18 FAR_PTR farptr_var0, farptr_var1, farptr_var2, farptr_var3;
19
20 // result of a function call
21 int res;
22
23 void run(void) {
24 // compose far pointer at runtime
25 farptr_var0 = to_far_ptr(some_bank2_proc1, BANK(some_bank2_proc1));
26 farptr_var1 = to_far_ptr(some_bank2_proc1, BANK(some_bank2_proc1));
27 farptr_var2 = to_far_ptr(some_bank2_proc0, BANK(some_bank2_proc0));
28 farptr_var2 = to_far_ptr(some_bank2_proc0, BANK(some_bank2_proc0));
29
30 // output far pointers (must be identical)
31 printf("FAR PTR0: %x:%x\n", (int)FAR_SEG(farptr_var0), (int)FAR_OFS(farptr_var0));
32 printf("FAR PTR1: %x:%x\n", (int)FAR_SEG(farptr_var1), (int)FAR_OFS(farptr_var1));
33
34 // try calling far function by far pointer without params
35 FAR_CALL(farptr_var2, void (*)(void));
36
37 // try calling far function directly
38 res = some_bank2_proc1(100, 50);
39 printf("CALL DIR: %d\n", res);
40
41 // try calling reentrant far function directly
42 res = some_bank2_proc2(100, 50, 1);
43 printf("CALL DIR (RE): %d\n", res);
44
45 // try calling far function by far pointer
46 #ifdef __PORT_mos6502
47 // SDCC mos6502 port does not appear to handle cast to typedef:ed function type some_bank2_proc_t correctly.
48 // Address of __call__banked incorrectly evaluates to zero. Needs further investigation.
49 // As a work-around, supply the function type directly for now.
50 res = FAR_CALL(farptr_var1, int (*)(uint8_t, uint8_t), 100, 50);
51 #else
52 // For other targets, just use convenience typedef some_bank2_proc_t
53 res = FAR_CALL(farptr_var1, some_bank2_proc_t, 100, 50);
54 #endif
55
56 printf("CALL IND: %d\n", res);
57 }
58
59 void main(void) {
60 ENABLE_RAM;
61 printf("START (bank=%d)\n", (int)CURRENT_BANK);
62 run();
63 printf("DONE! (bank=%d)\n", (int)CURRENT_BANK);
64 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.