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
29 changes: 19 additions & 10 deletions src/part2/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,27 @@ There are actually two object palettes, but we're only going to use one.

Now that you have an object on the screen, let's move it around.
Previously, the `Done` loop did nothing; let's rename it to `Main` and use it to move our object.
We're going to wait for VBlank before changing OAM, just like we did before turning off the screen.
We're going to use the VBlank interrupt to wait for the next frame before changing OAM.

```rgbasm,linenos,start={{#line_no_of "^Main:" ../../unbricked/objects/main.asm}}
First, add an interrupt handler at the start of the file:

```rgbasm,linenos,start={{#line_no_of "" ../../unbricked/objects/main.asm:vblank-interrupt}}
{{#include ../../unbricked/objects/main.asm:vblank-interrupt}}
```

Next, add this helper function before the graphics data:

```rgbasm,linenos,start={{#line_no_of "" ../../unbricked/objects/main.asm:wait-for-vblank}}
{{#include ../../unbricked/objects/main.asm:wait-for-vblank}}
```

For now, the VBlank interrupt handler just returns. This is enough for `halt` to stop the CPU until the next VBlank interrupt wakes it back up.

Now rename the `Done` loop to `Main` and call that helper before moving the paddle:

```rgbasm
Main:
; Wait until it's *not* VBlank
ld a, [rLY]
cp 144
jp nc, Main
WaitVBlank2:
ld a, [rLY]
cp 144
jp c, WaitVBlank2
call WaitForVBlank

; Move the paddle one pixel to the right.
ld a, [STARTOF(OAM) + 1]
Expand Down
23 changes: 16 additions & 7 deletions unbricked/audio/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ DEF BRICK_RIGHT EQU $06
DEF BLANK_TILE EQU $08
; ANCHOR_END: constants

SECTION "VBlank Interrupt", ROM0[$40]
VBlankInterrupt:
reti

SECTION "Header", ROM0[$100]

jp EntryPoint
Expand Down Expand Up @@ -93,14 +97,15 @@ ClearOam:
ld a, 0
ld [wFrameCounter], a

; Enable the VBlank interrupt
xor a, a
ld [rIF], a
ld a, IE_VBLANK
ld [rIE], a
ei

Main:
ld a, [rLY]
cp 144
jp nc, Main
WaitVBlank2:
ld a, [rLY]
cp 144
jp c, WaitVBlank2
call WaitForVBlank

; Add the ball's momentum to its position in OAM.
ld a, [wBallMomentumX]
Expand Down Expand Up @@ -347,6 +352,10 @@ Input:
.knownret
ret

WaitForVBlank:
halt
ret

; Copy bytes from one area to another.
; @param de: Source
; @param hl: Destination
Expand Down
23 changes: 16 additions & 7 deletions unbricked/bcd/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ DEF SCORE_TENS EQU $9870
DEF SCORE_ONES EQU $9871
; ANCHOR_END: score-tile-location

SECTION "VBlank Interrupt", ROM0[$40]
VBlankInterrupt:
reti

SECTION "Header", ROM0[$100]

jp EntryPoint
Expand Down Expand Up @@ -102,14 +106,15 @@ ClearOam:
ld [wScore], a
; ANCHOR_END: init-variables

; Enable the VBlank interrupt
xor a, a
ld [rIF], a
ld a, IE_VBLANK
ld [rIE], a
ei

Main:
ld a, [rLY]
cp 144
jp nc, Main
WaitVBlank2:
ld a, [rLY]
cp 144
jp c, WaitVBlank2
call WaitForVBlank

; Add the ball's momentum to its position in OAM.
ld a, [wBallMomentumX]
Expand Down Expand Up @@ -378,6 +383,10 @@ Input:
.knownret
ret

WaitForVBlank:
halt
ret

; Copy bytes from one area to another.
; @param de: Source
; @param hl: Destination
Expand Down
23 changes: 16 additions & 7 deletions unbricked/bricks/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ DEF BRICK_RIGHT EQU $06
DEF BLANK_TILE EQU $08
; ANCHOR_END: constants

SECTION "VBlank Interrupt", ROM0[$40]
VBlankInterrupt:
reti

SECTION "Header", ROM0[$100]

jp EntryPoint
Expand Down Expand Up @@ -96,14 +100,15 @@ ClearOam:
ld [wCurKeys], a
ld [wNewKeys], a

; Enable the VBlank interrupt
xor a, a
ld [rIF], a
ld a, IE_VBLANK
ld [rIE], a
ei

Main:
ld a, [rLY]
cp 144
jp nc, Main
WaitVBlank2:
ld a, [rLY]
cp 144
jp c, WaitVBlank2
call WaitForVBlank

; Add the ball's momentum to its position in OAM.
ld a, [wBallMomentumX]
Expand Down Expand Up @@ -343,6 +348,10 @@ Input:
.knownret
ret

WaitForVBlank:
halt
ret

; Copy bytes from one area to another.
; @param de: Source
; @param hl: Destination
Expand Down
23 changes: 16 additions & 7 deletions unbricked/collision/main.asm
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
INCLUDE "hardware.inc"

SECTION "VBlank Interrupt", ROM0[$40]
VBlankInterrupt:
reti

SECTION "Header", ROM0[$100]

jp EntryPoint
Expand Down Expand Up @@ -96,15 +100,16 @@ ClearOam:
ld [wCurKeys], a
ld [wNewKeys], a

; Enable the VBlank interrupt
xor a, a
ld [rIF], a
ld a, IE_VBLANK
ld [rIE], a
ei

; ANCHOR: momentum
Main:
ld a, [rLY]
cp 144
jp nc, Main
WaitVBlank2:
ld a, [rLY]
cp 144
jp c, WaitVBlank2
call WaitForVBlank

; Add the ball's momentum to its position in OAM.
ld a, [wBallMomentumX]
Expand Down Expand Up @@ -330,6 +335,10 @@ UpdateKeys:
.knownret
ret

WaitForVBlank:
halt
ret

; Copy bytes from one area to another.
; @param de: Source
; @param hl: Destination
Expand Down
23 changes: 16 additions & 7 deletions unbricked/functions/main.asm
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
INCLUDE "hardware.inc"

SECTION "VBlank Interrupt", ROM0[$40]
VBlankInterrupt:
reti

SECTION "Header", ROM0[$100]

jp EntryPoint
Expand Down Expand Up @@ -72,15 +76,16 @@ ClearOam:
ld a, 0
ld [wFrameCounter], a

; Enable the VBlank interrupt
xor a, a
ld [rIF], a
ld a, IE_VBLANK
ld [rIE], a
ei

; ANCHOR: main
Main:
ld a, [rLY]
cp 144
jp nc, Main
WaitVBlank2:
ld a, [rLY]
cp 144
jp c, WaitVBlank2
call WaitForVBlank

ld a, [wFrameCounter]
inc a
Expand All @@ -99,6 +104,10 @@ WaitVBlank2:
jp Main
; ANCHOR_END: main

WaitForVBlank:
halt
ret

; ANCHOR: memcpy
; Copy bytes from one area to another.
; @param de: Source
Expand Down
23 changes: 16 additions & 7 deletions unbricked/input/main.asm
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
INCLUDE "hardware.inc"

SECTION "VBlank Interrupt", ROM0[$40]
VBlankInterrupt:
reti

SECTION "Header", ROM0[$100]

jp EntryPoint
Expand Down Expand Up @@ -76,15 +80,16 @@ ClearOam:
ld [wNewKeys], a
; ANCHOR_END: initialize-vars

; Enable the VBlank interrupt
xor a, a
ld [rIF], a
ld a, IE_VBLANK
ld [rIE], a
ei

; ANCHOR: main
Main:
ld a, [rLY]
cp 144
jp nc, Main
WaitVBlank2:
ld a, [rLY]
cp 144
jp c, WaitVBlank2
call WaitForVBlank

; Check the current keys every frame and move left or right.
call UpdateKeys
Expand Down Expand Up @@ -120,6 +125,10 @@ Right:
jp Main
; ANCHOR_END: main

WaitForVBlank:
halt
ret

; ANCHOR: input-routine
UpdateKeys:
; Poll half the controller
Expand Down
27 changes: 20 additions & 7 deletions unbricked/objects/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
; ANCHOR_END: dummy Note that lines matching /^; ANCHOR/ are stripped from the online version
INCLUDE "hardware.inc"

; ANCHOR: vblank-interrupt
SECTION "VBlank Interrupt", ROM0[$40]
VBlankInterrupt:
reti
; ANCHOR_END: vblank-interrupt

SECTION "Header", ROM0[$100]

jp EntryPoint
Expand Down Expand Up @@ -98,14 +104,15 @@ ClearOam:
ld a, 0
ld [wFrameCounter], a

; Enable the VBlank interrupt
xor a, a
ld [rIF], a
ld a, IE_VBLANK
ld [rIE], a
ei

Main:
ld a, [rLY]
cp 144
jp nc, Main
WaitVBlank2:
ld a, [rLY]
cp 144
jp c, WaitVBlank2
call WaitForVBlank

ld a, [wFrameCounter]
inc a
Expand All @@ -124,6 +131,12 @@ WaitVBlank2:
jp Main
; ANCHOR_END: main-loop

; ANCHOR: wait-for-vblank
WaitForVBlank:
halt
ret
; ANCHOR_END: wait-for-vblank

Tiles:
dw `33333333
dw `33333333
Expand Down
Loading