04_coding_guidelines.md (13891B)
1@page docs_coding_guidelines Coding Guidelines 2 3# Learning C / C fundamentals 4Writing games and other programs with GBDK will be much easier with a basic understanding of the C language. In particular, understanding how to use C on "Embedded Platforms" (small computing systems, such as the Game Boy) can help you write better code (smaller, faster, less error prone) and avoid common pitfalls. 5 6 7@anchor docs_c_tutorials 8## General C tutorials 9 - https://www.learn-c.org/ 10 - https://www.tutorialspoint.com/cprogramming/index.htm 11 12## Embedded C introductions 13 14 - http://dsp-book.narod.ru/CPES.pdf 15 - https://www.phaedsys.com/principals/bytecraft/bytecraftdata/bcfirststeps.pdf 16 17## Game Boy games in C 18 19 - https://gbdev.io/list.html#c 20 21# Understanding the hardware 22In addition to understanding the C language it's important to learn how the Game Boy hardware works. What it is capable of doing, what it isn't able to do, and what resources are available to work with. A good way to do this is by reading the @ref Pandocs and checking out the @ref awesome_gb list. 23 24 25# Writing optimal C code for the Game Boy and SDCC 26The following guidelines can result in better code for the Game Boy, even though some of the guidance may be contrary to typical advice for general purpose computers that have more resources and speed. 27 28 29## Tools 30 31@anchor const_gbtd_gbmb 32### GBTD / GBMB, Arrays and the "const" keyword 33__Important__: The old @ref gbtd_gbmb "GBTD/GBMB" fails to include the `const` keyword when exporting to C source files for GBDK. That causes arrays to be created in RAM instead of ROM, which wastes RAM, uses a lot of ROM to initialize the RAM arrays and slows the compiler down a lot. 34 35__Use of @ref toxa_gbtd_gbmb "toxa's updated GBTD/GBMB" is highly recommended.__ 36 37If you wish to use the original tools, you must add the `const` keyword every time the graphics are re-exported to C source files. 38 39 40## Variables 41 - Use 8-bit values as much as possible. They will be much more efficient and compact than 16 and 32 bit types. 42 43 - Prefer unsigned variables to signed ones: the code generated will be generally more efficient, especially when comparing two values. 44 45 - Use explicit types so you always know the size of your variables. `int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t` and `bool`. 46 These are standard types defined in `stdint.h` (`#include <stdint.h>`) and `stdbool.h` (`#include <stdbool.h>`). 47 48 - Global and local static variables are generally more efficient than local non-static variables (which go on the stack and are slower and can result in slower code). 49 50 - @anchor const_array_data 51 `const` keyword: use const for arrays, structs and variables with read-only (constant) data. It will reduce ROM, RAM and CPU usage significantly. Non-`const` values are loaded from ROM into RAM inefficiently, and there is no benefit in loading them into the limited available RAM if they aren't going to be changed. 52 53 - Here is how to delcare `const` pointers and variables: 54 - non-const pointer to a const variable: `const uint8_t * some_pointer;` 55 - const pointer to a non-const variable: `uint8_t * const some_pointer;` 56 - const pointer to a const variable: `const uint8_t * const some_pointer;` 57 - https://codeforwin.org/2017/11/constant-pointer-and-pointer-to-constant-in-c.html 58 - https://stackoverflow.com/questions/21476869/constant-pointer-vs-pointer-to-constant 59 60 - For calculated values that don't change, pre-compute results once and store the result. Using lookup-tables and the like can improve speed and reduce code size. Macros can sometimes help. It may be beneficial to do the calculations with an outside tool and then include the result as C code in a const array. 61 62 - Use an advancing pointer (`someStruct->var = x; someStruct++`) to loop through arrays of structs instead of using indexing each time in the loop `someStruct[i].var = x`. 63 64 - When modifying variables that are also changed in an Interrupt Service Routine (ISR), wrap them the relevant code block in a `__critical { }` block. See http://sdcc.sourceforge.net/doc/sdccman.pdf#section.3.9 65 66 - When using constants and literals the `U`, `L` and `UL` postfixes can be used. 67 - `U` specifies that the constant is unsigned 68 - `L` specifies that the constant is long. 69 70 - NOTE: In SDCC 3.6.0, the default for char changed from signed to unsigned. The manual says to use `--fsigned-char` for the old behavior, this option flag is included by default when compiling through @ref lcc. 71 72 @anchor fixed_point_type 73 - A fixed point type (`fixed`) is included with GBDK when precision greater than whole numbers is required for 8 bit range values (since floating point is not included in GBDK). 74 75 See the "Simple Physics" sub-pixel example project. 76 77 Code example: 78 79 fixed player[2]; 80 ... 81 // Modify player position using its 16 bit representation 82 player[0].w += player_speed_x; 83 player[1].w += player_speed_y; 84 ... 85 // Use only the upper 8 bits for setting the sprite position 86 move_sprite(0, player[0].h ,player[1].h); 87 88 89## Code structure 90 91 - Do not `#include` `.c` source files into other `.c` source files. Instead create `.h` header files for them and include those. 92 https://www.tutorialspoint.com/cprogramming/c_header_files.htm 93 94 - Instead of using a blocking @ref delay() for things such as sprite animations/etc (which can prevent the rest of the game from continuing) many times it's better to use a counter which performs an action once every N frames. @ref sys_time may be useful in these cases. 95 96 - When processing for a given frame is done and it is time to wait before starting the next frame, @ref wait_vbl_done() can be used. It uses HALT to put the CPU into a low power state until processing resumes. The CPU will wake up and resume processing at the end of the current frame when the Vertical Blanking interrupt is triggered. 97 98 - Minimize use of multiplication, modulo with non-powers of 2, and division with non-powers of 2. These operations have no corresponding CPU instructions (software functions), and hence are time costly. 99 - SDCC has some optimizations for: 100 - Division by powers of 2. For example `n /= 4u` will be optimized to `n >>= 2`. 101 - Modulo by powers of 2. For example: `(n % 8)` will be optimized to `(n & 0x7)`. 102 - If you need decimal numbers to count or display a score, you can use the GBDK BCD ([binary coded decimal](https://en.wikipedia.org/wiki/Binary-coded_decimal)) number functions. See: @ref bcd.h and the `BCD` example project included with GBDK. 103 104 - Avoid long lists of function parameters. Passing many parameters can add overhead, especially if the function is called often. Globals and local static vars can be used instead when applicable. 105 106 - Use inline functions if the function is short (with the `inline` keyword, such as `inline uint8_t myFunction() { ... }`). 107 108 - Do not use recursive functions. 109<!-- This entry needs re-work. Signed vs unsigned, current SDCC optimizations... 110 111 - Prefer `==` and `!=` comparison operators to `<`, `<=`, `>`, and `>=`. The code will be shorter and quicker. 112 113 It is even faster to check if a variable is 0 than if it is equal to some other value, so looping from _N down to zero_ is faster than looping _from zero up to N_. 114 115 For instance: 116 117 for(i = 0; i < 10; i++) 118 119 is less efficient than: 120 121 for(i = 0; i != 10; i++) 122 123 and if possible, even better: 124 125 for(i = 10; i != 0; i--) 126--> 127 128## GBDK API/Library 129 130 - stdio.h: If you have other ways of printing text, avoid including @ref stdio.h and using functions such as @ref printf(). Including it will use a large number of the background tiles for font characters. If stdio.h is not included then that space will be available for use with other tiles instead. 131 132 - drawing.h: The Game Boy graphics hardware is not well suited to frame-buffer style graphics such as the kind provided in @ref drawing.h. Due to that, most drawing functions (rectangles, circles, etc) will be slow . When possible it's much faster and more efficient to work with the tiles and tile maps that the Game Boy hardware is built around. 133 134 - @ref waitpad() and @ref waitpadup check for input in a loop that doesn't HALT at all, so the CPU will be maxed out until it returns. One alternative is to write a function with a loop that checks input with @ref joypad() and then waits a frame using @ref wait_vbl_done() (which idles the CPU while waiting) before checking input again. 135 136 - @ref joypad(): When testing for multiple different buttons, it's best to read the joypad state *once* into a variable and then test using that variable (instead of making multiple calls). 137 138 139## Toolchain 140 141 - See SDCC optimizations: http://sdcc.sourceforge.net/doc/sdccman.pdf#section.8.1 142 143 - Use profiling. Look at the ASM generated by the compiler, write several versions of a function, compare them and choose the faster one. 144 145 - Use the SDCC `--max-allocs-per-node` flag with large values, such as `50000`. `--opt-code-speed` has a much smaller effect. 146 - GBDK-2020 (after v4.0.1) compiles the library with `--max-allocs-per-node 50000`, but it must be turned on for your own code. 147 (example: `lcc ... -Wf--max-allocs-per-node50000` or `sdcc ... --max-allocs-per-node 50000`). 148 149 - The other code/speed flags are `--opt-code-speed` or `--opt-code-size `. 150 151 - Use current SDCC builds from http://sdcc.sourceforge.net/snap.php 152 The minimum required version of SDCC will depend on the GBDK-2020 release. See @ref docs_releases 153 154 - Learn some ASM and inspect the compiler output to understand what the compiler is doing and how your code gets translated. This can help with writing better C code and with debugging. 155 156 157@anchor docs_chars_varargs 158## chars and vararg functions 159 160In standard C when `chars` are passed to a function with variadic arguments (varargs, those declared with `...` as a parameter), such as @ref printf(), those `chars` get automatically promoted to `ints`. For an 8 bit CPU such as the Game Boy's, this is not as efficient or desirable in most cases. So the default SDCC behavior, which GBDK-2020 expects, is that chars will remain chars and _not_ get promoted to ints when **explicitly cast as chars while calling a varargs function**. 161 162 - They must be explicitly re-cast when passing them to a varargs function, even though they are already declared as chars. 163 164 - Discussion in SDCC manual: 165 http://sdcc.sourceforge.net/doc/sdccman.pdf#section.1.5 166 http://sdcc.sourceforge.net/doc/sdccman.pdf#subsection.3.5.10 167 168 - If SDCC is invoked with -std-cxx (--std-c89, --std-c99, --std-c11, etc) then it will conform to standard C behavior and calling functions such as @ref printf() with chars may not work as expected. 169 170For example: 171 172 unsigned char i = 0x5A; 173 174 // NO: 175 // The char will get promoted to an int, producing incorrect printf output 176 // The output will be: 5A 00 177 printf("%hx %hx", i, i); 178 179 // YES: 180 // The char will remain a char and printf output will be as expected 181 // The output will be: 5A 5A 182 printf("%hx %hx", (unsigned char)i, (unsigned char)i); 183 184Some functions that accept varargs: 185 - @ref EMU_printf, @ref gprintf(), @ref printf(), @ref sprintf() 186 187Also See: 188 - Other cases of char to int promotion: http://sdcc.sourceforge.net/doc/sdccman.pdf#chapter.6 189 190 191# When C isn't fast enough 192@todo Update and verify this section for the modernized SDCC and toolchain 193 194For many applications C is fast enough but in intensive functions are sometimes better written in assembly. This section deals with interfacing your core C program with fast assembly sub routines. 195 196 197## Calling convention 198sdcc in common with almost all C compilers prepends a '_' to any function names. For example the function printf(...) begins at the label _printf::. Note that all functions are declared global. 199 200The parameters to a function are pushed in right to left order with no aligning - so a byte takes up a byte on the stack instead of the more natural word. So for example the function int store_byte( uint16_t addr, uint8_t byte) would push 'byte' onto the stack first then addr using a total of three bytes. As the return address is also pushed, the stack would contain: 201 202 At SP+0 - the return address 203 204 At SP+2 - addr 205 206 At SP+4 - byte 207 208Note that the arguments that are pushed first are highest in the stack due to how the Game Boy's stack grows downwards. 209 210The function returns in DE. 211 212 213## Variables and registers 214C normally expects registers to be preserved across a function call. However in the case above as DE is used as the return value and HL is used for anything, only BC needs to be preserved. 215 216Getting at C variables is slightly tricky due to how local variables are allocated on the stack. However you shouldn't be using the local variables of a calling function in any case. Global variables can be accessed by name by adding an underscore. 217 218 219## Segments 220The use of segments for code, data and variables is more noticeable in assembler. GBDK and SDCC define a number of default segments - `_CODE`, `_DATA` and `_BSS`. Two extra segments `_HEADER` and `_HEAP` exist for the Game Boy header and malloc heap respectively. 221 222The order these segments are linked together is determined by crt0.s and is currently `_CODE` in ROM, then `_DATA`, `_BSS`, `_HEAP` in WRAM, with `STACK` at the top of WRAM. `_HEAP` is placed after `_BSS` so that all spare memory is available for the malloc routines. To place code in other than the first two banks, use the segments `_CODE_x` where x is the 16kB bank number. 223 224As the `_BSS` segment occurs outside the ROM area you can only use .ds to reserve space in it. 225 226While you don't have to use the `_CODE` and `_DATA` distinctions in assembler you may wish to do so to maintain consistency. 227