diff --git a/src/part2/serial-link.md b/src/part2/serial-link.md index b5e07971..21165de1 100644 --- a/src/part2/serial-link.md +++ b/src/part2/serial-link.md @@ -620,7 +620,7 @@ Copy these new tiles to the end of the tile data -- they should be immediately a ## Running the test ROM -Because we have an extra file (sio.asm) to compile now, the build commands will look a little different: +The build commands are as follows to build both `main.asm` and `sio.asm` into a single ROM (see [Title Screen](./title-screen.md)): ```console $ rgbasm -o sio.o sio.asm diff --git a/src/part2/title-screen.md b/src/part2/title-screen.md index 5d78453a..c56fb43f 100644 --- a/src/part2/title-screen.md +++ b/src/part2/title-screen.md @@ -16,3 +16,14 @@ Then copy and paste the following after waiting for VBlank: Note that we are using our `Memcopy` function from the [Functions](./functions.md) lesson! Isn't it handy to have reusable code? We are also using our `UpdateKeys` function from the [Input](./input.md) lesson to determine when to stop displaying the title screen and move on to the game itself. To do so, we loop until the start button has been pressed. And just like that we have ourselves a title screen! + +## Organizing Our Code +Our project is getting quite large with all the functionality we're building in! Let's briefly go over how to better organize things. Until now, we have always added new code into the same assembly file (`main.asm`). This file can get pretty large if we're not careful, making it harder to read and maintain. Instead, RGBDS has a handy feature for making [functions](./functions.md) or other labels visible to external files. This will help our codebase be nice and clean, making it more manageable (in the case where one might collaborate with others, this is essential!). As an example, let's take everything we added in our [input](./input.md) lesson, and put it in a separate file named [`input.asm`](https://github.com/gbdev/gb-asm-tutorial/raw/master/unbricked/title-screen/input.asm). + +Notice the use of the double colon (`::`) after the function and variable names. This is how we can export a label to other files, also known as broadening its [scope](https://en.wikipedia.org/wiki/Scope_%28computer_programming%29). Now that we have all of the input-related code in a separate file, and everything else in main, all that is left is to build the ROM. Now that we have multiple files, we have to assemble each assembly file, then link them together in one ROM, like so: + +```console,linenos,start={{#line_no_of "" ../../unbricked/title-screen/build.sh:multibuild}} +{{#include ../../unbricked/title-screen/build.sh:multibuild}} +``` + +Try doing the same with other assembly files to keep your code nice and tidy. Break up separate functionality into other files, don't forget to assemble them separately, then link them all together! diff --git a/unbricked/input/input.asm b/unbricked/input/input.asm deleted file mode 100644 index fc81de4b..00000000 --- a/unbricked/input/input.asm +++ /dev/null @@ -1,45 +0,0 @@ -; This is a simplified version of pads.z80 by PinoBatch for use in gb-asm-tutorial -; All labels are intentionally not exported to avoid confusing the reader with unfamiliar syntax. -; Once linking is introduced in part 3, a new, exported version of this file will be provided. - -SECTION "Input Variables", WRAM0 -wCurKeys: db -wNewKeys: db - -SECTION "UpdateKeys", ROM0 - -UpdateKeys: - ; Poll half the controller - ld a, JOYP_GET_BUTTONS - call .onenibble - ld b, a ; B7-4 = 1; B3-0 = unpressed buttons - - ; Poll the other half - ld a, JOYP_GET_CTRL_PAD - call .onenibble - swap a ; A3-0 = unpressed directions; A7-4 = 1 - xor a, b ; A = pressed buttons + directions - ld b, a ; B = pressed buttons + directions - - ; And release the controller - ld a, JOYP_GET_NONE - ldh [rJOYP], a - - ; Combine with previous wCurKeys to make wNewKeys - ld a, [wCurKeys] - xor a, b ; A = keys that changed state - and a, b ; A = keys that changed to pressed - ld [wNewKeys], a - ld a, b - ld [wCurKeys], a - ret - -.onenibble - ldh [rJOYP], a ; switch the key matrix - call .knownret ; burn 10 cycles calling a known ret - ldh a, [rJOYP] ; ignore value while waiting for the key matrix to settle - ldh a, [rJOYP] - ldh a, [rJOYP] ; this read counts - or a, $F0 ; A7-4 = 1; A3-0 = unpressed keys -.knownret - ret diff --git a/unbricked/title-screen/build.sh b/unbricked/title-screen/build.sh index 71cab60e..93a5dff4 100755 --- a/unbricked/title-screen/build.sh +++ b/unbricked/title-screen/build.sh @@ -1,6 +1,8 @@ #!/bin/sh +# ANCHOR: multibuild rgbasm -o main.o main.asm rgbasm -o input.o input.asm rgblink -o unbricked.gb main.o input.o rgbfix -v -p 0xFF unbricked.gb +# ANCHOR_END: multibuild diff --git a/unbricked/title-screen/input.asm b/unbricked/title-screen/input.asm index 2013a818..8e8c9648 100644 --- a/unbricked/title-screen/input.asm +++ b/unbricked/title-screen/input.asm @@ -11,37 +11,37 @@ wNewKeys:: db SECTION "UpdateKeys", ROM0 UpdateKeys:: - ; Poll half the controller - ld a, JOYP_GET_BUTTONS - call .onenibble - ld b, a ; B7-4 = 1; B3-0 = unpressed buttons - - ; Poll the other half - ld a, JOYP_GET_CTRL_PAD - call .onenibble - swap a ; A3-0 = unpressed directions; A7-4 = 1 - xor a, b ; A = pressed buttons + directions - ld b, a ; B = pressed buttons + directions - - ; And release the controller - ld a, JOYP_GET_NONE - ldh [rJOYP], a - - ; Combine with previous wCurKeys to make wNewKeys - ld a, [wCurKeys] - xor a, b ; A = keys that changed state - and a, b ; A = keys that changed to pressed - ld [wNewKeys], a - ld a, b - ld [wCurKeys], a - ret + ; Poll half the controller + ld a, JOYP_GET_BUTTONS + call .onenibble + ld b, a ; B7-4 = 1; B3-0 = unpressed buttons + + ; Poll the other half + ld a, JOYP_GET_CTRL_PAD + call .onenibble + swap a ; A3-0 = unpressed directions; A7-4 = 1 + xor a, b ; A = pressed buttons + directions + ld b, a ; B = pressed buttons + directions + + ; And release the controller + ld a, JOYP_GET_NONE + ldh [rJOYP], a + + ; Combine with previous wCurKeys to make wNewKeys + ld a, [wCurKeys] + xor a, b ; A = keys that changed state + and a, b ; A = keys that changed to pressed + ld [wNewKeys], a + ld a, b + ld [wCurKeys], a + ret .onenibble - ldh [rJOYP], a ; switch the key matrix - call .knownret ; burn 10 cycles calling a known ret - ldh a, [rJOYP] ; ignore value while waiting for the key matrix to settle - ldh a, [rJOYP] - ldh a, [rJOYP] ; this read counts - or a, $F0 ; A7-4 = 1; A3-0 = unpressed keys + ldh [rJOYP], a ; switch the key matrix + call .knownret ; burn 10 cycles calling a known ret + ldh a, [rJOYP] ; ignore value while waiting for the key matrix to settle + ldh a, [rJOYP] + ldh a, [rJOYP] ; this read counts + or a, $F0 ; A7-4 = 1; A3-0 = unpressed keys .knownret - ret + ret