docs_using_gbdk.html (17608B)
1<!-- HTML header for doxygen 1.8.14--> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4<head> 5<meta http-equiv="cache-control" content="max-age=86400"/> 6<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> 7<meta http-equiv="X-UA-Compatible" content="IE=9"/> 8<meta name="generator" content="Doxygen 1.8.20"/> 9<meta name="viewport" content="width=device-width, initial-scale=1"/> 10<title>GBDK 2020 Docs: Using GBDK</title> 11<link href="tabs.css" rel="stylesheet" type="text/css"/> 12<script type="text/javascript" src="jquery.js"></script> 13<script type="text/javascript" src="dynsections.js"></script> 14<link href="navtree.css" rel="stylesheet" type="text/css"/> 15<script type="text/javascript" src="resize.js"></script> 16<script type="text/javascript" src="navtreedata.js"></script> 17<script type="text/javascript" src="navtree.js"></script> 18<link href="search/search.css" rel="stylesheet" type="text/css"/> 19<script type="text/javascript" src="search/searchdata.js"></script> 20<script type="text/javascript" src="search/search.js"></script> 21<link href="doxygen.css" rel="stylesheet" type="text/css" /> 22<link href="doxygen_extra.css" rel="stylesheet" type="text/css"/> 23</head> 24<body> 25<div id="top"><!-- do not remove this div, it is closed by doxygen! --> 26<div id="titlearea"> 27<table cellspacing="0" cellpadding="0"> 28 <tbody> 29 <tr style="height: 56px;"> 30 <td id="projectalign" style="padding-left: 0.5em;"> 31 <div id="projectname">GBDK 2020 Docs 32  <span id="projectnumber">4.0.6</span> 33 </div> 34 <div id="projectbrief">API Documentation for GBDK 2020</div> 35 </td> 36 </tr> 37 </tbody> 38</table> 39</div> 40<!-- end header part --> 41<!-- Generated by Doxygen 1.8.20 --> 42<script type="text/javascript"> 43/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ 44var searchBox = new SearchBox("searchBox", "search",false,'Search'); 45/* @license-end */ 46</script> 47<script type="text/javascript" src="menudata.js"></script> 48<script type="text/javascript" src="menu.js"></script> 49<script type="text/javascript"> 50/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ 51$(function() { 52 initMenu('',true,false,'search.php','Search'); 53 $(document).ready(function() { init_search(); }); 54}); 55/* @license-end */</script> 56<div id="main-nav"></div> 57</div><!-- top --> 58<div id="side-nav" class="ui-resizable side-nav-resizable"> 59 <div id="nav-tree"> 60 <div id="nav-tree-contents"> 61 <div id="nav-sync" class="sync"></div> 62 </div> 63 </div> 64 <div id="splitbar" style="-moz-user-select:none;" 65 class="ui-resizable-handle"> 66 </div> 67</div> 68<script type="text/javascript"> 69/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ 70$(document).ready(function(){initNavTree('docs_using_gbdk.html',''); initResizable(); }); 71/* @license-end */ 72</script> 73<div id="doc-content"> 74<!-- window showing the filter options --> 75<div id="MSearchSelectWindow" 76 onmouseover="return searchBox.OnSearchSelectShow()" 77 onmouseout="return searchBox.OnSearchSelectHide()" 78 onkeydown="return searchBox.OnSearchSelectKey(event)"> 79</div> 80 81<!-- iframe showing the search results (closed by default) --> 82<div id="MSearchResultsWindow"> 83<iframe src="javascript:void(0)" frameborder="0" 84 name="MSearchResults" id="MSearchResults"> 85</iframe> 86</div> 87 88<div class="PageDoc"><div class="header"> 89 <div class="headertitle"> 90<div class="title">Using GBDK </div> </div> 91</div><!--header--> 92<div class="contents"> 93<div class="textblock"><h1><a class="anchor" id="autotoc_md40"></a> 94Interrupts</h1> 95<p>Interrupts allow execution to jump to a different part of your code as soon as an external event occurs - for example the LCD entering the vertical blank period, serial data arriving or the timer reaching its end count. For an example see the irq.c sample project.</p> 96<p>Interrupts in GBDK are handled using the functions <a class="el" href="gb_8h.html#ad77796783b3a601b6f3781dfc3983499">disable_interrupts()</a>, <a class="el" href="gb_8h.html#ae0b13f17609b26c86fc33944aeb6e867">enable_interrupts()</a>, <a class="el" href="sms_8h.html#aefda0091b2934571a11e07b512735f50">set_interrupts(uint8_t ier)</a> and the interrupt service routine (ISR) linkers <a class="el" href="gb_8h.html#a0d29659d08708143dd8bc720278e06b5">add_VBL()</a>, <a class="el" href="sms_8h.html#a6c66a583a8f0744e3985c89725e3dc10">add_TIM</a>, <a class="el" href="sms_8h.html#a51add93356a25c71e8c37a73c9065c9d">add_LCD</a>, <a class="el" href="sms_8h.html#a3372d61a07e0466bdb909a27f3aaaca9">add_SIO</a> and <a class="el" href="sms_8h.html#a48163816121cd669526817d3e6266fd9">add_JOY</a> which add interrupt handlers for the vertical blank, timer, LCD, serial link and joypad interrupts respectively.</p> 97<p>Since an interrupt can occur at any time an Interrupt Service Request (ISR) cannot take any arguments or return anything. Its only way of communicating with the greater program is through the global variables. When interacting with those shared ISR global variables from main code outside the interrupt, it is a good idea to wrap them in a <code>critical {}</code> section in case the interrupt occurs and modifies the variable while it is being used.</p> 98<p>Interrupts should be disabled before adding ISRs. To use multiple interrupts, <em>logical OR</em> the relevant IFLAGs together.</p> 99<p>ISRs should be kept as small and short as possible, do not write an ISR so long that the Game Boy hardware spends all of its time servicing interrupts and has no time spare for the main code.</p> 100<p>For more detail on the Game Boy interrupts consider reading about them in the <a class="el" href="docs_links_and_tools.html#Pandocs">Pandocs</a>.</p> 101<h2><a class="anchor" id="autotoc_md41"></a> 102Available Interrupts</h2> 103<p>The GameBoy hardware can generate 5 types of interrupts. Custom Interrupt Service Routines (ISRs) can be added in addition to the built-in ones available in GBDK.</p> 104<ul> 105<li>VBL : LCD Vertical Blanking period start<ul> 106<li>The default VBL ISR is installed automatically.<ul> 107<li>See <a class="el" href="gb_8h.html#a0d29659d08708143dd8bc720278e06b5">add_VBL()</a> and <a class="el" href="gb_8h.html#ad43fdfdb1e157b141f3fc48b78bf4386">remove_VBL()</a></li> 108</ul> 109</li> 110</ul> 111</li> 112<li>LCD : LCDC status (such as the start of a horizontal line)<ul> 113<li>See <a class="el" href="gb_8h.html#a9f9f77105099a34556247d5bb03368d1">add_LCD()</a> and <a class="el" href="gb_8h.html#a32683767caa2a263a1f494b3605786e7">remove_LCD()</a></li> 114<li>Example project: <code>lcd_isr_wobble</code></li> 115</ul> 116</li> 117<li>TIM : Timer overflow<ul> 118<li>See <a class="el" href="gb_8h.html#a028d1a2e820951bb4f103d6469975ffb">add_TIM()</a> and <a class="el" href="gb_8h.html#a142f6c7755fce8b1148faf658d8ec147">remove_TIM()</a></li> 119<li>Example project: <code>tim</code></li> 120</ul> 121</li> 122<li>SIO : Serial Link I/O transfer end<ul> 123<li>The default SIO ISR gets installed automatically if any of the standard SIO calls are used. These calls include <a class="el" href="gb_8h.html#aa82422752016328ed0765879e286019f">add_SIO()</a>, <a class="el" href="gb_8h.html#a5b821b31215361265d8b7894a9ae7118">remove_SIO()</a>, <a class="el" href="gb_8h.html#ae339d7d8d7e0ebd6691b42608c416964">send_byte()</a>, <a class="el" href="gb_8h.html#a9a7fd7be44bb12bc85a144b732ce02f7">receive_byte()</a>.</li> 124<li>The default SIO ISR cannot be removed once installed. Only secondary chained SIO ISRs (added with <a class="el" href="gb_8h.html#aa82422752016328ed0765879e286019f">add_SIO()</a> ) can be removed.</li> 125<li>See <a class="el" href="gb_8h.html#aa82422752016328ed0765879e286019f">add_SIO()</a> and <a class="el" href="gb_8h.html#a5b821b31215361265d8b7894a9ae7118">remove_SIO()</a></li> 126<li>Example project: <code>comm</code></li> 127</ul> 128</li> 129<li>JOY : Transition from high to low of a joypad button<ul> 130<li>See <a class="el" href="gb_8h.html#aa2f0235e78da2d1d94d3628d7a1afc30">add_JOY()</a> and <a class="el" href="gb_8h.html#a4a3e87e0917d5efb6bc7c94e9754fcd0">remove_JOY()</a></li> 131</ul> 132</li> 133</ul> 134<h2><a class="anchor" id="autotoc_md42"></a> 135Adding your own interrupt handler</h2> 136<p>It is possible to install your own interrupt handlers (in C or in assembly) for any of these interrupts. Up to 4 chained handlers may be added, with the last added being called last. If the <a class="el" href="gb_8h.html#ad43fdfdb1e157b141f3fc48b78bf4386">remove_VBL()</a> function is to be called, only three may be added for VBL.</p> 137<p>Interrupt handlers are called in sequence. To install a new interrupt handler, do the following:</p> 138<ol type="1"> 139<li>Write a function (say foo()) that takes no parameters, and that returns nothing. Remember that the code executed in an interrupt handler must be short.</li> 140<li>Inside a <code>__critical { ... }</code> section, install your interrupt handling routines using the add_XXX() function, where XXX is the interrupt that you want to handle.</li> 141<li>Enable interrupts for the IRQ you want to handle, using the <a class="el" href="gb_8h.html#a9312e7ec34162d6b6ed0875631fa6fe3">set_interrupts()</a> function. Note that the VBL interrupt is already enabled before the main() function is called. If you want to set the interrupts before main() is called, you must install an initialization routine.</li> 142</ol> 143<p>See the <code>irq</code> example project for additional details for a complete example.</p> 144<h2><a class="anchor" id="autotoc_md43"></a> 145Using your own Interrupt Dispatcher</h2> 146<p>If you want to use your own Interrupt Dispatcher instead of the GBDK chained dispatcher (for improved performance), then don't call the <code>add_...()</code> function for the respective interrupt and it's dispatcher won't be installed.</p><ul> 147<li>Exception: the VBL dispatcher will always be linked in at compile time.</li> 148<li>For the SIO interrupt, also do not make any standard SIO calls to avoid having it's dispatcher installed.</li> 149</ul> 150<p>Then, <a class="el" href="isr_8h.html#a73769fed9338af86fdb7df35d7b82620">ISR_VECTOR()</a> or <a class="el" href="isr_8h.html#a78f9ef588aaf221023e48899898d566b">ISR_NESTED_VECTOR()</a> can be used to install a custom ISR handler.</p> 151<h2><a class="anchor" id="autotoc_md44"></a> 152Returning from Interrupts and STAT mode</h2> 153<p>By default when an Interrupt handler completes and is ready to exit it will check STAT_REG and only return at the BEGINNING of either LCD Mode 0 or Mode 1. This helps prevent graphical glitches caused when an ISR interrupts a graphics operation in one mode but returns in a different mode for which that graphics operation is not allowed.</p> 154<p>You can change this behavior using <a class="el" href="gb_8h.html#a695c6d0e8fd7cf11dae0d4c67bc058f9">nowait_int_handler()</a> which does not check <a class="el" href="gb_2hardware_8h.html#ad40ebf3b29add46cdd310a7e0802bc6b">STAT_REG</a> before returning. Also see <a class="el" href="gb_8h.html#acc9afd0cb72e763a1213d256b942a68f">wait_int_handler()</a>.</p> 155<h1><a class="anchor" id="autotoc_md45"></a> 156What GBDK does automatically and behind the scenes</h1> 157<h2><a class="anchor" id="autotoc_md46"></a> 158OAM (VRAM Sprite Attribute Table)</h2> 159<p>GBDK sets up a Shadow OAM which gets copied automatically to the hardware OAM by the default V-Blank ISR. The Shadow OAM allows updating sprites without worrying about whether it is safe to write to them or not based on the hardware LCD mode.</p> 160<h2><a class="anchor" id="autotoc_md47"></a> 161Font tiles when using stdio.h</h2> 162<p>Including <a class="el" href="stdio_8h.html">stdio.h</a> and using functions such as <a class="el" href="stdio_8h.html#ab403fa700816f08631ba75bc536f74d4">printf()</a> 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.</p> 163<h2><a class="anchor" id="autotoc_md48"></a> 164Default Interrupt Service Handlers (ISRs)</h2> 165<ul> 166<li>V-Blank: A default V-Blank ISR is installed on startup which copies the Shadow OAM to the hardware OAM and increments the global <a class="el" href="sms_8h.html#a78d2fd18666afec116f176d46debb4e7">sys_time</a> variable once per frame.</li> 167<li>Serial Link I/O: If any of the GBDK serial link functions are used such as <a class="el" href="gb_8h.html#ae339d7d8d7e0ebd6691b42608c416964">send_byte()</a> and <a class="el" href="gb_8h.html#a9a7fd7be44bb12bc85a144b732ce02f7">receive_byte()</a>, the default SIO serial link handler will be installed automatically at compile-time.</li> 168</ul> 169<h1><a class="anchor" id="autotoc_md49"></a> 170Copying Functions to RAM and HIRAM</h1> 171<p>See the <code>ram_function</code> example project included with GBDK demonstrates copying functions to RAM and HIRAM.</p> 172<p><code>Warning!</code> Copying of functions is generally not safe since they may contain jumps to absolute addresses that will not be converted to match the new location.</p> 173<p>It is possible to copy functions to RAM and HIRAM (using the <a class="el" href="asm_2gbz80_2string_8h.html#a6a1a6cbe7fb6102819098340a4986574">memcpy()</a> and <a class="el" href="gb_8h.html#a97b9f2fc6ac7cae97656aca940d65d44">hiramcpy()</a> functions), and execute them from C. Ensure you have enough free space in RAM or HIRAM for copying a function.</p> 174<p>There are basically two ways for calling a function located in RAM, HIRAM, or ROM:</p> 175<ul> 176<li>Declare a pointer-to-function variable, and set it to the address of the function to call.</li> 177<li>Declare the function as extern, and set its address at link time using the -Wl-gXXX=# flag (where XXX is the name of the function, and # is its address).</li> 178</ul> 179<p>The second approach is slightly more efficient. Both approaches are demonstrated in the <code>ram_function.c</code> example.</p> 180<h1><a class="anchor" id="autotoc_md50"></a> 181Mixing C and Assembly</h1> 182<p>You can mix C and assembly (ASM) in two ways as described below. For additional detail see the <a class="el" href="docs_links_and_tools.html#links_sdcc_docs">links_sdcc_docs</a>.</p> 183<h2><a class="anchor" id="autotoc_md51"></a> 184Inline ASM within C source files</h2> 185<p>Example: <br /> 186 </p><pre class="fragment"> __asm__("nop"); 187</pre><p>Another Example: <br /> 188 </p><pre class="fragment"> void some_c_function() 189 { 190 // Optionally do something 191 __asm 192 (ASM code goes here) 193 __endasm; 194 } 195</pre><h2><a class="anchor" id="autotoc_md52"></a> 196In Separate ASM files</h2> 197<dl class="todo"><dt><b><a class="el" href="todo.html#_todo000002">Todo:</a></b></dt><dd>This is from GBDK 2.x docs, verify it with GBDK-2020 and modern SDCC</dd></dl> 198<p>It is possible to assemble and link files written in ASM alongside files written in C.</p> 199<ul> 200<li>A C identifier <code>i</code> will be called <code>_i</code> in assembly.</li> 201<li>Results are always returned into the <code>DE</code> register.</li> 202<li>Parameters are passed on the stack (starting at <code>SP+2</code> because the return address is also saved on the stack).</li> 203<li>Assembly identifier are exported using the <code>.globl</code> directive.</li> 204<li>You can access GameBoy hardware registers using <code>_reg_0xXX</code> where <code>XX</code> is the register number (see <code>sound.c</code> for an example).</li> 205<li>Registers must be preserved across function calls (you must store them at function begin, and restore them at the end), except <code>HL</code> (and <code>DE</code> when the function returns a result).</li> 206</ul> 207<p>Here is an example of how to mix assembly with C:</p> 208<p><code>main.c</code> </p><pre class="fragment">main() 209{ 210 int16_t i; 211 int16_t add(int16_t, int16_t); 212 213 i = add(1, 3); 214} 215</pre><p><code>add.s</code> </p><pre class="fragment">.globl _add 216_add: ; int16_t add(int16_t a, int16_t b) 217 ; There is no register to save: 218 ; BC is not used 219 ; DE is the return register 220 ; HL needs never to be saved 221LDA HL,2(SP) 222LD E,(HL) ; Get a in DE 223INC HL 224LD D,(HL) 225INC HL 226LD A,(HL) ; Get b in HL 227INC HL 228LD H,(HL) 229LD L,A 230ADD HL,DE ; Add DE to HL 231LD D,H 232LD E,L 233 ; There is no register to restore 234RET ; Return result in DE 235</pre><h1><a class="anchor" id="autotoc_md53"></a> 236Including binary files in C source with incbin</h1> 237<p>Data from binary files can be included in C source files as a const array using the <a class="el" href="incbin_8h.html#af34047ed60abd6453f819c2a5230cd2b">INCBIN()</a> macro.</p> 238<p>See the <code>incbin</code> example project for a demo of how to use it.</p> 239<h1><a class="anchor" id="autotoc_md54"></a> 240Known Issues and Limitations</h1> 241<h2><a class="anchor" id="autotoc_md55"></a> 242SDCC</h2> 243<ul> 244<li>Const arrays declared with <code>somevar[n] = {x}</code> will <b>NOT</b> get initialized with value <code>x</code>. This may change when the SDCC RLE initializer is fixed. Use memset for now if you need it.</li> 245<li>SDCC banked calls and <a class="el" href="docs_rombanking_mbcs.html#far_pointers">far_pointers</a> in GBDK only save one byte for the ROM bank, so for example they are limtied to <b>bank 15</b> max for MBC1 and <b>bank 255</b> max for MBC5. See <a class="el" href="docs_rombanking_mbcs.html#banked_calls">banked_calls</a> for more details. </li> 246</ul> 247</div></div><!-- contents --> 248</div><!-- PageDoc --> 249</div><!-- doc-content --> 250<!-- HTML footer for doxygen 1.8.14--> 251<!-- start footer part --> 252<div id="nav-path" class="navpath"><!-- id is needed for treeview function! --> 253</div> 254</body> 255</html>