git.y1.nz

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_nonintrinsic/src/banks_nonintrinsic.c

      1 #include <gbdk/platform.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 
      5 // force bank switching macro
      6 uint8_t __dummy_variable;
      7 #define switch_to(x) (__dummy_variable = (char)((x)[0]), (void *)(x))
      8 
      9 uint8_t _current_ram_bank = 0;
     10 #define SWITCH_RAM_BANK(x) (_current_ram_bank = (SWITCH_RAM(x), (x)))
     11 
     12 // constant in base ROM
     13 const char const hello_code[] = "hello from CODE\n";
     14 
     15 // variable in base RAM
     16 char data[20] = "DATA";
     17 int  add_num_wram = 1;
     18 
     19 // constants in ROM banks
     20 
     21 void set_ROM_bank1(void) NONBANKED { SWITCH_ROM(1); }
     22 void set_ROM_bank2(void) NONBANKED { SWITCH_ROM(2); }
     23 __addressmod set_ROM_bank1 const CODE_1;
     24 __addressmod set_ROM_bank2 const CODE_2;
     25 
     26 CODE_1 const char hello_rom_1[] = "hello from CODE_1\n";
     27 CODE_2 const char hello_rom_2[] = "hello from CODE_2\n";
     28 
     29 // variables in RAM banks
     30 
     31 void set_RAM_bank0(void) NONBANKED { SWITCH_RAM_BANK(0); }
     32 void set_RAM_bank1(void) NONBANKED { SWITCH_RAM_BANK(1); }
     33 __addressmod set_RAM_bank0 DATA_0;
     34 __addressmod set_RAM_bank1 DATA_1;
     35 
     36 // Variables in SRAM Bank 0
     37 DATA_0 char hello_sram_0[20];
     38 DATA_0 int  add_num_sram_0;
     39 
     40 // Variables in SRAM Bank 1
     41 DATA_1 char hello_sram_1[20];
     42 DATA_1 int  add_num_sram_1a;
     43 DATA_1 int  add_num_sram_1b;
     44 
     45 // define array of pointers in RAM1 to the variables that are RAM2
     46 // there is a flaw in compiler that disallows pointers into banks to be in the other banks
     47 // details: https://sourceforge.net/p/sdcc/bugs/2995/
     48 DATA_1 int * const CODE_1 add_num__ptr[2] = {&add_num_sram_1a, &add_num_sram_1b};
     49 
     50 void main(void) {
     51     ENABLE_RAM;
     52 
     53     add_num_sram_0 = 2;
     54     add_num_sram_1a = 4;
     55     add_num_sram_1b = 8;
     56 
     57     // say hello
     58     for (int8_t i = 0; (hello_code[i]); i++) putchar(hello_code[i]);
     59     for (int8_t i = 0; (hello_rom_1[i]); i++) putchar(hello_rom_1[i]);
     60     for (int8_t i = 0; (hello_rom_2[i]); i++) putchar(hello_rom_2[i]);
     61 
     62     // prepare and say hello from rom bank1 to sram bank0
     63     for (int8_t i = 0; (i < sizeof(hello_rom_1)); i++) hello_sram_0[i] = hello_rom_1[i];
     64     for (int8_t i = 0; (i < 4); i++) hello_sram_0[i + 11] = data[i];
     65     for (int8_t i = 0; (hello_sram_0[i]); i++) putchar(hello_sram_0[i]);
     66 
     67     // prepare and say hello from rom bank2 to sram bank1
     68     for (int8_t i = 0; (i < sizeof(hello_rom_2)); i++) hello_sram_1[i] = hello_rom_2[i];
     69     for (int8_t i = 0; (i < 4); i++) hello_sram_1[i + 11] = data[i];
     70     for (int8_t i = 0; (hello_sram_1[i]); i++) putchar(hello_sram_1[i]);
     71 
     72     printf("once more...\n");
     73     // say hello again; just use the vars to access them, the switching if needed is handled automatically
     74     for (int8_t i = 0; (hello_code[i]); i++) putchar(hello_code[i]);
     75     for (int8_t i = 0; (hello_rom_1[i]); i++) putchar(hello_rom_1[i]);
     76     for (int8_t i = 0; (hello_rom_2[i]); i++) putchar(hello_rom_2[i]);
     77     for (int8_t i = 0; (hello_sram_0[i]); i++) putchar(hello_sram_0[i]);
     78     for (int8_t i = 0; (hello_sram_1[i]); i++) putchar(hello_sram_1[i]);
     79 
     80     printf("once more...\n");
     81     // if we need an address, then we use a macro switch_to()
     82     printf("%s", hello_code);
     83     printf("%s", switch_to(hello_rom_1));
     84     printf("%s", switch_to(hello_rom_2));
     85     printf("%s", switch_to(hello_sram_0));
     86     printf("%s", switch_to(hello_sram_1));
     87 
     88     // Add the RAM variables from different address spaces together
     89     printf("1+2+4+8=0x%x", (int)(add_num_wram + add_num_sram_0 + (*add_num__ptr[0]) + (*add_num__ptr[1])));
     90 
     91     // stop
     92     while(1);
     93 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.