cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

banks_farptr.c (1500B)


      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
      7BANKREF_EXTERN(some_bank2_proc0)
      8extern void some_bank2_proc0() __banked;
      9
     10BANKREF_EXTERN(some_bank2_proc1)
     11extern int some_bank2_proc1(int param1, int param2) __banked;
     12typedef int (*some_bank2_proc_t)(int, int) __banked; // define type for some_bank2_proc1() function
     13
     14// far pointers
     15FAR_PTR farptr_var0, farptr_var1, farptr_var2;
     16
     17// result of a function call
     18int res;
     19
     20void run() {
     21    // compose far pointer at runtime
     22    farptr_var0 = to_far_ptr(some_bank2_proc1, BANK(some_bank2_proc1));
     23    farptr_var1 = to_far_ptr(some_bank2_proc1, BANK(some_bank2_proc1));
     24    farptr_var2 = to_far_ptr(some_bank2_proc0, BANK(some_bank2_proc0));
     25
     26    // output far pointers (must be identical)
     27    printf("FAR PTR0: %x:%x\n", (int)FAR_SEG(farptr_var0), (int)FAR_OFS(farptr_var0));
     28    printf("FAR PTR1: %x:%x\n", (int)FAR_SEG(farptr_var1), (int)FAR_OFS(farptr_var1));
     29
     30    // try calling far function by far pointer without params
     31    FAR_CALL(farptr_var2, void (*)(void));
     32
     33    // try calling far function directly
     34    res = some_bank2_proc1(100, 50);
     35    printf("CALL DIR: %d\n", res);
     36
     37    // try calling far function by far pointer
     38    res = FAR_CALL(farptr_var1, some_bank2_proc_t, 100, 50);
     39
     40    printf("CALL IND: %d\n", res);
     41}
     42
     43void main() {
     44    ENABLE_RAM;
     45    printf("START (bank=%d)\n", (int)CURRENT_BANK);
     46    run();
     47    printf("DONE! (bank=%d)\n", (int)CURRENT_BANK);
     48}