Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/part2/serial-link.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/part2/title-screen.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. Instead, RGBDS has a handy feature for making [functions](./functions.md) or other labels visible to external files. 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!
45 changes: 0 additions & 45 deletions unbricked/input/input.asm

This file was deleted.

2 changes: 2 additions & 0 deletions unbricked/title-screen/build.sh
Original file line number Diff line number Diff line change
@@ -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
62 changes: 31 additions & 31 deletions unbricked/title-screen/input.asm
Original file line number Diff line number Diff line change
Expand Up @@ -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