Skip to content

Commit 876e8c7

Browse files
authored
Add Project Organization Text to Title Screen Section (#168)
* Remove unnecessary input.asm, fix style to match other asm files * Add code organization text to Title-Screen section * Change serial link wording for multi-object linking * Add justification for favoring smaller files
1 parent 47f51b5 commit 876e8c7

File tree

5 files changed

+45
-77
lines changed

5 files changed

+45
-77
lines changed

src/part2/serial-link.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ Copy these new tiles to the end of the tile data -- they should be immediately a
620620

621621

622622
## Running the test ROM
623-
Because we have an extra file (sio.asm) to compile now, the build commands will look a little different:
623+
The build commands are as follows to build both `main.asm` and `sio.asm` into a single ROM (see [Title Screen](./title-screen.md)):
624624

625625
```console
626626
$ rgbasm -o sio.o sio.asm

src/part2/title-screen.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,14 @@ Then copy and paste the following after waiting for VBlank:
1616
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.
1717

1818
And just like that we have ourselves a title screen!
19+
20+
## Organizing Our Code
21+
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).
22+
23+
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:
24+
25+
```console,linenos,start={{#line_no_of "" ../../unbricked/title-screen/build.sh:multibuild}}
26+
{{#include ../../unbricked/title-screen/build.sh:multibuild}}
27+
```
28+
29+
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!

unbricked/input/input.asm

Lines changed: 0 additions & 45 deletions
This file was deleted.

unbricked/title-screen/build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

3+
# ANCHOR: multibuild
34
rgbasm -o main.o main.asm
45
rgbasm -o input.o input.asm
56
rgblink -o unbricked.gb main.o input.o
67
rgbfix -v -p 0xFF unbricked.gb
8+
# ANCHOR_END: multibuild

unbricked/title-screen/input.asm

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,37 @@ wNewKeys:: db
1111
SECTION "UpdateKeys", ROM0
1212

1313
UpdateKeys::
14-
; Poll half the controller
15-
ld a, JOYP_GET_BUTTONS
16-
call .onenibble
17-
ld b, a ; B7-4 = 1; B3-0 = unpressed buttons
18-
19-
; Poll the other half
20-
ld a, JOYP_GET_CTRL_PAD
21-
call .onenibble
22-
swap a ; A3-0 = unpressed directions; A7-4 = 1
23-
xor a, b ; A = pressed buttons + directions
24-
ld b, a ; B = pressed buttons + directions
25-
26-
; And release the controller
27-
ld a, JOYP_GET_NONE
28-
ldh [rJOYP], a
29-
30-
; Combine with previous wCurKeys to make wNewKeys
31-
ld a, [wCurKeys]
32-
xor a, b ; A = keys that changed state
33-
and a, b ; A = keys that changed to pressed
34-
ld [wNewKeys], a
35-
ld a, b
36-
ld [wCurKeys], a
37-
ret
14+
; Poll half the controller
15+
ld a, JOYP_GET_BUTTONS
16+
call .onenibble
17+
ld b, a ; B7-4 = 1; B3-0 = unpressed buttons
18+
19+
; Poll the other half
20+
ld a, JOYP_GET_CTRL_PAD
21+
call .onenibble
22+
swap a ; A3-0 = unpressed directions; A7-4 = 1
23+
xor a, b ; A = pressed buttons + directions
24+
ld b, a ; B = pressed buttons + directions
25+
26+
; And release the controller
27+
ld a, JOYP_GET_NONE
28+
ldh [rJOYP], a
29+
30+
; Combine with previous wCurKeys to make wNewKeys
31+
ld a, [wCurKeys]
32+
xor a, b ; A = keys that changed state
33+
and a, b ; A = keys that changed to pressed
34+
ld [wNewKeys], a
35+
ld a, b
36+
ld [wCurKeys], a
37+
ret
3838

3939
.onenibble
40-
ldh [rJOYP], a ; switch the key matrix
41-
call .knownret ; burn 10 cycles calling a known ret
42-
ldh a, [rJOYP] ; ignore value while waiting for the key matrix to settle
43-
ldh a, [rJOYP]
44-
ldh a, [rJOYP] ; this read counts
45-
or a, $F0 ; A7-4 = 1; A3-0 = unpressed keys
40+
ldh [rJOYP], a ; switch the key matrix
41+
call .knownret ; burn 10 cycles calling a known ret
42+
ldh a, [rJOYP] ; ignore value while waiting for the key matrix to settle
43+
ldh a, [rJOYP]
44+
ldh a, [rJOYP] ; this read counts
45+
or a, $F0 ; A7-4 = 1; A3-0 = unpressed keys
4646
.knownret
47-
ret
47+
ret

0 commit comments

Comments
 (0)