cscg22-gearboy

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

incbin.h (2372B)


      1/** @file gbdk/incbin.h
      2
      3    Allows binary data from other files to be included
      4    into a C source file.
      5
      6    It is implemented using asm .incbin and macros.
      7
      8    See the `incbin` example project for a demo of how to use it.
      9*/
     10#ifndef _INCBIN_H
     11#define _INCBIN_H
     12
     13#include <stdint.h>
     14
     15
     16/** Creates extern entries for accessing a INCBIN() generated
     17    variable and it's size in another source file.
     18
     19    @param VARNAME Name of the variable used with INCBIN
     20
     21    An entry is created for the variable and it's size variable.
     22
     23    @ref INCBIN(), INCBIN_SIZE()
     24*/
     25#define INCBIN_EXTERN(VARNAME)  extern const uint8_t VARNAME[]; \
     26extern const void __size_ ## VARNAME; \
     27extern const void __bank_ ## VARNAME;
     28
     29/** Obtains the __size in bytes__ of the INCBIN() generated data
     30
     31    @param VARNAME Name of the variable used with INCBIN
     32
     33    Requires @ref INCBIN_EXTERN() to have been called earlier in the source file
     34
     35    @ref INCBIN(), INCBIN_EXTERN()
     36*/
     37#define INCBIN_SIZE(VARNAME) ( (uint16_t) & __size_ ## VARNAME )
     38
     39/** Obtains the __bank number__ of the INCBIN() generated data
     40
     41    @param VARNAME Name of the variable used with INCBIN
     42
     43    Requires @ref INCBIN_EXTERN() to have been called earlier in the source file
     44
     45    @ref INCBIN(), INCBIN_EXTERN()
     46*/
     47#ifndef BANK
     48#define BANK(VARNAME) ( (uint8_t) & __bank_ ## VARNAME )
     49#endif
     50
     51/** Includes binary data into a C source file
     52
     53    @param VARNAME Variable name to use
     54    @param FILEPATH Path to the file which will be binary included into the C source file
     55
     56    __filepath__ is relative to the working directory of the tool
     57    that is calling it (often a makefile's working directory), __NOT__
     58    to the file it's being included into.
     59
     60    The variable name is not modified and can be used as-is.
     61
     62    @see INCBIN_SIZE() for obtaining the size of the included data.
     63    @see BANK() for obtaining the bank number of the included data.
     64
     65    Use @ref INCBIN_EXTERN() within another source file
     66    to make the variable and it's data accesible there.
     67*/
     68#define INCBIN(VARNAME, FILEPATH) void __func_ ## VARNAME() __banked __naked { \
     69__asm \
     70_ ## VARNAME:: \
     711$: \
     72    .incbin FILEPATH \
     732$: \
     74    ___size_ ## VARNAME = (2$-1$) \
     75    .globl ___size_ ## VARNAME \
     76    .local b___func_ ## VARNAME \
     77    ___bank_ ## VARNAME = b___func_ ## VARNAME \
     78    .globl ___bank_ ## VARNAME \
     79__endasm; \
     80}
     81
     82#endif // _INCBIN_H
     83
     84