|
| 1 | +# Header |
| 2 | + |
| 3 | +Let's go back to a certain line near the top of `hello-world.asm`. |
| 4 | + |
| 5 | +```rgbasm,linenos,start=7 |
| 6 | +{{#include ../assets/hello-world.asm:10}} |
| 7 | +``` |
| 8 | + |
| 9 | +What is this mysterious header, why are we making room for it, and more questions answered in this lesson! |
| 10 | + |
| 11 | +## What is the header? |
| 12 | + |
| 13 | +First order of business is explaining what the header *is*. |
| 14 | +It's the region of memory from $0104 to $014F (inclusive). |
| 15 | +It contains metadata about the ROM, such as its title, Game Boy Color compatibility, size, |
| 16 | +two checksums, and interestingly, the Nintendo logo that is displayed during the power-on animation. |
| 17 | + |
| 18 | +::: tip |
| 19 | + |
| 20 | +You can find this information and more [in the Pan Docs](https://gbdev.io/pandocs/The_Cartridge_Header). |
| 21 | + |
| 22 | +::: |
| 23 | + |
| 24 | +Interestingly, most of the information in the header does not matter on real hardware (the ROM's size is determined only by the capacity of the ROM chip in the cartridge, not the header byte). |
| 25 | +In fact, some prototype ROMs actually have incorrect header info! |
| 26 | + |
| 27 | +Most of the header was only used by Nintendo's manufacturing department to know what components to put in the cartridge when publishing a ROM. |
| 28 | +Thus, only ROMs sent to Nintendo had to have a fully correct header; ROMs used for internal testing only needed to pass the boot ROM's checks, explained further below. |
| 29 | + |
| 30 | +However, in our "modern" day and age, the header actually matters a lot. |
| 31 | +Emulators (including hardware emulators such as flashcarts) must emulate the hardware present in the cartridge. |
| 32 | +The header being the only source of information about what hardware the ROM's cartridge should contain, they rely on some of the values in the header. |
| 33 | + |
| 34 | +## Boot ROM |
| 35 | + |
| 36 | +The header is intimately tied to what is called the **boot ROM**. |
| 37 | + |
| 38 | +The most observant and/or nostalgic of you may have noticed the lack of the boot-up animation and the Game Boy's signature "ba-ding!" in BGB. |
| 39 | +When the console powers up, the CPU does not begin executing instructions at address $0100 (where our ROM's entry point is), but at $0000. |
| 40 | + |
| 41 | +However, at that time, a small program called the *boot ROM*, burned within the CPU's silicon, is "overlaid" on top of our ROM! |
| 42 | +The boot ROM is responsible for the startup animation, but it also checks the ROM's header! |
| 43 | +Specifically, it verifies that the Nintendo logo and header checksums are correct; if either check fails, the boot ROM intentionally *locks up*, and our game never gets to run :( |
| 44 | + |
| 45 | +::: tip For the curious |
| 46 | + |
| 47 | +You can find a more detailed description of what the boot ROM does [in the Pan Docs](https://gbdev.io/pandocs/Power_Up_Sequence), as well as an explanation of the logo check. |
| 48 | +Beware that it is quite advanced, though. |
| 49 | + |
| 50 | +If you want to enable the boot ROMs in BGB, you must obtain a copy of the boot ROM(s), whose SHA256 checksums can be found [in their disassembly](https://github.com/ISSOtm/gb-bootroms/blob/master/sha256sums.txt) for verification. |
| 51 | +If you wish, you can also compile [SameBoy's boot ROMs](https://github.com/LIJI32/SameBoy#compilation) and use those instead, as a free-software substitute. |
| 52 | + |
| 53 | +Then, in BGB's options, go to the `System` tab, set the paths to the boot ROMs you wish to use, tick `Enable bootroms`, select the appropriate system, and click `OK` or `Apply`. |
| 54 | +Now, just reset the emulator, and voilà! |
| 55 | + |
| 56 | +::: |
| 57 | + |
| 58 | +A header is typically called "valid" if it would pass the boot ROM's checks, and "invalid" otherwise. |
| 59 | + |
| 60 | +## RGBFIX |
| 61 | + |
| 62 | +RGBFIX is the third component of RGBDS, whose purpose is to write a ROM's header. |
| 63 | +It is separate from RGBLINK so that it can be used as a stand-alone tool. |
| 64 | +Its name comes from that RGBLINK typically does not produce a ROM with a valid header, so the ROM must be "fixed" before it's production-ready. |
| 65 | + |
| 66 | +RGBFIX has [a bunch of options](https://rgbds.gbdev.io/docs/rgbfix.1) to set various parts of the header; but the only two that we are using here are `-v`, which produces a **v**alid header (so, correct [Nintendo logo](https://gbdev.io/pandocs/The_Cartridge_Header.html#0104-0133---nintendo-logo) and [checksums](https://gbdev.io/pandocs/The_Cartridge_Header.html#014d---header-checksum)), and <code>-p 0xFF</code>, which **p**ads the ROM to the next valid size (using $FF as the filler byte), and writes the appropriate value to the [ROM size byte](https://gbdev.io/pandocs/The_Cartridge_Header.html#0148---rom-size). |
| 67 | + |
| 68 | +If you look at other projects, you may find RGBFIX invocations with more options, but these two should almost always be present. |
| 69 | + |
| 70 | +## So, what's the deal with that line? |
| 71 | + |
| 72 | +Right! |
| 73 | +This line. |
| 74 | + |
| 75 | +```rgbasm,linenos,start=7 |
| 76 | +{{#include ../assets/hello-world.asm:10}} |
| 77 | +``` |
| 78 | + |
| 79 | +Well, let's see what happens if we remove it (or comment it out). |
| 80 | + |
| 81 | +```console |
| 82 | +$ rgbasm -L -o hello-world.o hello-world.asm |
| 83 | +$ rgblink -o hello-world.gb -n hello-world.sym hello-world.o |
| 84 | +``` |
| 85 | + |
| 86 | +(I am intentionally not running RGBFIX; we will see why in a minute.) |
| 87 | + |
| 88 | +::: danger |
| 89 | + |
| 90 | +Make sure the boot ROMs are not enabled for this! |
| 91 | +If they are, make sure to disable them (untick their box in the options, click `OK` or `Apply`, and reset the emulator). |
| 92 | + |
| 93 | +::: |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +As I explained, RGBFIX is responsible for writing the header, so we should use it to fix these warnings. |
| 98 | + |
| 99 | +```console |
| 100 | +$ rgbfix -v -p 0xFF hello-world.gb |
| 101 | +warning: Overwrote a non-zero byte in the Nintendo logo |
| 102 | +warning: Overwrote a non-zero byte in the header checksum |
| 103 | +``` |
| 104 | + |
| 105 | +*I'm sure these warnings are nothing to be worried about...* |
| 106 | +(Depending on your version of RGBDS, you may have gotten different warnings, or none at all.) |
| 107 | + |
| 108 | +Let's run the ROM... |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +... dismiss this pesky warning, and... |
| 113 | + |
| 114 | +<figure> |
| 115 | + <img src="../assets/img/invalid_opcode.png" alt="Screenshot of BGB's debugger, the title bar reads "invalid opcode""> |
| 116 | + <figcaption> |
| 117 | + When the debugger pops open on its own, and the title bar reads "invalid opcode", you <em>might</em> have screwed up somewhere. |
| 118 | + </figcaption> |
| 119 | +</figure> |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +Okay, so, what happened? |
| 124 | + |
| 125 | +As we can see from the screenshot, PC is at $0105. |
| 126 | +What is it doing there? |
| 127 | + |
| 128 | +...Oh, `EntryPoint` is at $0103. |
| 129 | +So the `jp` at $0100 went there, and started executing instructions (`3E CE` is the raw form of `ld a, $CE`), but then $ED does not encode any valid instruction, so the CPU locks up. |
| 130 | + |
| 131 | +But why is `EntryPoint` there? |
| 132 | +Well, as you may have figured out from the warnings RGBFIX printed, it *overwrites* the header area in the ROM. |
| 133 | +However, RGBLINK is **not** aware of the header (because RGBLINK is not only used to generate ROMs!), so you must explicitly reserve space for the header area. |
| 134 | + |
| 135 | +::: danger:🥴 |
| 136 | + |
| 137 | +Forgetting to reserve this space, and having a piece of code or data ending up there then overwritten, is a common beginner mistake that can be quite puzzling. |
| 138 | +Fortunately, RGBFIX since version 0.5.1 warns when it detects this mistake, as shown above. |
| 139 | + |
| 140 | +::: |
| 141 | + |
| 142 | +So, we prevent disaster like this: |
| 143 | + |
| 144 | +```rgbasm,linenos,start=3 |
| 145 | +{{#include ../assets/hello-world.asm:6:10}} |
| 146 | +``` |
| 147 | + |
| 148 | +The `ds` fills all bytes from $103 to $14F (inclusive) with the value $00. |
| 149 | +Since different pieces of code and/or data cannot overlap, this ensures that the header's memory range can safely be overwritten by RGBFIX, and that nothing else accidentally gets steamrolled instead. |
| 150 | + |
| 151 | +It may not be obvious how the `ds` fills is told to fill this specific memory range. |
| 152 | +The 3-byte `jp` covers memory addresses $100, $101, and $102. |
| 153 | +(We start at $100 because that's where the `SECTION` is hardcoded to be.) |
| 154 | +When RGBASM processes the `ds` directive, `@` (which is a special symbol that evaluates to "the current address") thus has the value $103, so it fills `$150 - $103 = $4D` bytes with zeros, so $103, $104, ..., $14E, $14F. |
| 155 | + |
| 156 | +## Bonus: the infinite loop |
| 157 | + |
| 158 | +(This is not really linked to the header, but I need to explain it somewhere, and here is as good a place as any.) |
| 159 | + |
| 160 | +You may also be wondering what the point of the infinite loop at the end of the code is for. |
| 161 | + |
| 162 | +```rgbasm |
| 163 | +{{#include ../assets/hello-world.asm:68:69}} |
| 164 | +``` |
| 165 | + |
| 166 | +Well, simply enough, the CPU never stops executing instructions; so when our little Hello World is done and there is nothing left to do, we must still give the CPU some busy-work: so we make it do nothing, forever. |
| 167 | + |
| 168 | +We cannot let the CPU just run off, as it would then start executing other parts of memory as code, possibly crashing. |
| 169 | +(See for yourself: remove or comment out these two lines, re-[compile the ROM](hello_world.md), and see what happens!) |
0 commit comments