Skip to content
Merged
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/assets/hello-world.asm
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ CopyTilemap:
jp nz, CopyTilemap

; Turn the LCD on
ld a, LCDCF_ON | LCDCF_BGON
ld a, LCDC_ON | LCDCF_BG_ON
ld [rLCDC], a

; During the first (blank) frame, initialize display registers
Expand Down
96 changes: 48 additions & 48 deletions src/cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ WaitUntilVerticalBlankStart:

### Turn on/off the LCD

You can turn the LCD on and off by altering the most significant bit of the `rLCDC` register. hardware.inc a constant for this: `LCDCF_ON` .
You can turn the LCD on and off by altering the most significant bit of the `rLCDC` register. hardware.inc a constant for this: `LCDC_ON` .

**To turn the LCD on:**

```rgbasm,linenos
ld a, LCDCF_ON
ld a, LCDC_ON
ldh [rLCDC], a
```

Expand All @@ -83,20 +83,20 @@ Do not turn the LCD off outside of the Vertical Blank Phase. See "[Wait for the

```rgbasm,linenos
; Turn the LCD off
ld a, LCDCF_OFF
ld a, LCDC_OFF
ldh [rLCDC], a
```

### Turn on/off the background

To turn the background layer on and off, alter the least significant bit of the `rLCDC` register. You can use the `LCDCF_BGON` constant for this.
To turn the background layer on and off, alter the least significant bit of the `rLCDC` register. You can use the `LCDC_BG_ON` constant for this.

**To turn the background on:**

```rgbasm,linenos
; Turn the background on
ldh a, [rLCDC]
or a, LCDCF_BGON
or a, LCDC_BG_ON
ldh [rLCDC], a
```

Expand All @@ -105,20 +105,20 @@ ldh [rLCDC], a
```rgbasm,linenos
; Turn the background off
ldh a, [rLCDC]
and a, ~LCDCF_BGON
and a, ~LCDC_BG_ON
ldh [rLCDC], a
```

### Turn on/off the window

To turn the window layer on and off, alter the least significant bit of the `rLCDC` register. You can use the `LCDCF_WINON` and `LCDCF_WINOFF` constants for this.
To turn the window layer on and off, alter the least significant bit of the `rLCDC` register. You can use the `LCDC_WIN_ON` and `LCDC_WIN_OFF` constants for this.

**To turn the window on:**

```rgbasm,linenos
; Turn the window on
ldh a, [rLCDC]
or a, LCDCF_WINON
or a, LCDC_WIN_ON
ldh [rLCDC], a
```

Expand All @@ -127,7 +127,7 @@ ldh [rLCDC], a
```rgbasm,linenos
; Turn the window off
ldh a, [rLCDC]
and a, LCDCF_WINOFF
and a, LCDC_WIN_OFF
ldh [rLCDC], a
```

Expand All @@ -141,10 +141,10 @@ Which region the background uses is controlled by the 4th bit of the `rLCDC` reg

You can use one of the 4 constants to specify which layer uses which region:

- LCDCF_WIN9800
- LCDCF_WIN9C00
- LCDCF_BG9800
- LCDCF_BG9C00
- LCDC_WIN_9800
- LCDC_WIN_9C00
- LCDC_BG_9800
- LCDC_BG_9C00

:::tip Note

Expand All @@ -154,14 +154,14 @@ You still need to make sure the window and background are turned on when using t

### Turn on/off sprites

Sprites (or objects) can be toggled on and off using the 2nd bit of the `rLCDC` register. You can use the `LCDCF_OBJON` and `LCDCF_OBJOFF` constants for this.
Sprites (or objects) can be toggled on and off using the 2nd bit of the `rLCDC` register. You can use the `LCDC_OBJ_ON` and `LCDC_OBJ_OFF` constants for this.

**To turn sprite objects on:**

```rgbasm,linenos
; Turn the sprites on
ldh a, [rLCDC]
or a, LCDCF_OBJON
or a, LCDC_OBJ_ON
ldh [rLCDC], a
```

Expand All @@ -170,7 +170,7 @@ ldh [rLCDC], a
```rgbasm,linenos
; Turn the sprites off
ldh a, [rLCDC]
and a, LCDCF_OBJOFF
and a, LCDC_OBJ_OFF
ldh [rLCDC], a
```

Expand All @@ -182,7 +182,7 @@ Sprites are in 8x8 mode by default.

### Turn on/off tall (8x16) sprites

Once sprites are enabled, you can enable tall sprites using the 3rd bit of the `rLCDC` register: `LCDCF_OBJ16`
Once sprites are enabled, you can enable tall sprites using the 3rd bit of the `rLCDC` register: `LCDC_OBJ_16`

:::tip

Expand All @@ -193,15 +193,15 @@ You can not have some 8x8 sprites and some 8x16 sprites. All sprites must be of
```rgbasm,linenos
; Turn tall sprites on
ldh a, [rLCDC]
or a, LCDCF_OBJ16
or a, LCDC_OBJ_16
ldh [rLCDC], a
```

## Backgrounds

### Put background/window tile data into VRAM

The region in VRAM dedicated for the background/window tilemaps is from $9000 to $97FF. hardware.inc defines a `_VRAM9000` constant you can use for that.
The region in VRAM dedicated for the background/window tilemaps is from $9000 to $97FF. The assembly language defines a `STARTOF` function you can use that will help to determine the value at link time.

```rgbasm, lineno
MyBackground:
Expand All @@ -211,7 +211,7 @@ MyBackground:
CopyBackgroundWindowTileDataIntoVram:
; Copy the tile data
ld de, myBackground
ld hl, \_VRAM
ld hl, STARTOF(VRAM) + $1000
ld bc, MyBackground.end - MyBackground
.loop:
ld a, [de]
Expand Down Expand Up @@ -328,20 +328,20 @@ call UpdateKeys

You can check if a button is down using any of the following constants from hardware.inc:

- PADF_DOWN
- PADF_UP
- PADF_LEFT
- PADF_RIGHT
- PADF_START
- PADF_SELECT
- PADF_B
- PADF_A
- PAD_DOWN
- PAD_UP
- PAD_LEFT
- PAD_RIGHT
- PAD_START
- PAD_SELECT
- PAD_B
- PAD_A

You can check if the associataed button is down using the `wCurKeys` variable:

```rgbasm,linenos
ld a, [wCurKeys]
and a, PADF_LEFT
and a, PAD_LEFT
jp nz, LeftIsPressedDown
```

Expand All @@ -351,7 +351,7 @@ You can tell if a button was JUST pressed using the `wNewKeys` variable

```rgbasm,linenos
ld a, [wNewKeys]
and a, PADF_A
and a, PAD_A
jp nz, AWasJustPressed
```

Expand All @@ -374,7 +374,7 @@ This will halt all other logic (outside of interrupts), be careful if you need a
```rgbasm, linenos
WaitForAButtonToBePressed:
ld a, [wNewKeys]
and a, PADF_A
and a, PAD_A
ret nz
WaitUntilVerticalBlankStart:
ld a, [rLY]
Expand Down Expand Up @@ -515,7 +515,7 @@ Sprites will still show over the window. To fully prevent that, you can use STAT

### Put sprite tile data in VRAM

The region in VRAM dedicated for sprites is from `$8000` to `$87F0`. Hardware.inc defines a `_VRAM` constant you can use for that. To copy sprite tile data into VRAM, you can use a loop to copy the bytes.
The region in VRAM dedicated for sprites is from `$8000` to `$87F0`. The assembly language defines a `STARTOF` function you can use for that. To copy sprite tile data into VRAM, you can use a loop to copy the bytes.

```rgbasm,linenos
mySprite: INCBIN "src/path/to/my/sprite.2bpp"
Expand All @@ -524,7 +524,7 @@ mySpriteEnd:
CopySpriteTileDataIntoVram:
; Copy the tile data
ld de, Paddle
ld hl, _VRAM
ld hl, STARTOF(VRAM)
ld bc, mySpriteEnd - mySprite
CopySpriteTileDataIntoVram_Loop:
ld a, [de]
Expand All @@ -547,29 +547,29 @@ Each hardware sprite has 4 bytes: (in this order)

Check out the Pan Docs page on [Object Attribute Memory (OAM)](https://gbdev.io/pandocs/OAM.html) for more info.

The bytes controlling hardware OAM sprites start at `$FE00`, for which hardware.inc has defined a constant as `_OAMRAM`.
The bytes controlling hardware OAM sprites start at `$FE00`, for which the assembly language has a section type `OAM`.

**Moving (the first) OAM sprite, one pixel downwards:**

```rgbasm, linenos
ld a, [_OAMRAM]
ld a, [STARTOF(OAM)]
inc a
ld [_OAMRAM], a
ld [STARTOF(OAM)], a
```

**Moving (the first) OAM sprite, one pixel to the right:**

```rgbasm, linenos
ld a, [_OAMRAM + 1]
ld a, [ + 1]
inc a
ld [_OAMRAM + 1], a
ld [STARTOF(OAM) + 1], a
```

**Setting the tile for the first OAM sprite:**

```rgbasm, linenos
ld a, 3
ld [_OAMRAM+2], a
ld [STARTOF(OAM)+2], a
```

**Moving (the fifth) OAM sprite, one pixel downwards:**
Expand Down Expand Up @@ -605,7 +605,7 @@ The library is relatively simple to get set up. First, put the following in your
; Reset hardware OAM
xor a, a
ld b, 160
ld hl, _OAMRAM
ld hl, STARTOF(OAM)
.resetOAM
ld [hli], a
dec b
Expand All @@ -623,7 +623,7 @@ call hOAMDMA

### Manipulate Shadow OAM OAM sprites

Once you've set up @eievui5's Sprite Object Library, you can manipulate shadow OAM sprites the exact same way you would manipulate normal hardware OAM sprites. Except, this time you would use the library's `wShadowOAM` constant instead of the `_OAMRAM` register.
Once you've set up @eievui5's Sprite Object Library, you can manipulate shadow OAM sprites the exact same way you would manipulate normal hardware OAM sprites. Except, this time you would use the library's `wShadowOAM` constant instead of the `OAM` register.

**Moving (the first) OAM sprite, one pixel downwards:**

Expand Down Expand Up @@ -656,13 +656,13 @@ wCurrentLevel:: db

```

To access SRAM, you need to write `CART_SRAM_ENABLE` to the `rRAMG` register. When done, you can disable SRAM using the `CART_SRAM_DISABLE` constant.
To access SRAM, you need to write `RAMG_SRAM_ENABLE` to the `rRAMG` register. When done, you can disable SRAM using the `RAMG_SRAM_DISABLE` constant.

**To enable read/write access to SRAM:**

```rgbasm, linenos

ld a, CART_SRAM_ENABLE
ld a, RAMG_SRAM_ENABLE
ld [rRAMG], a

```
Expand All @@ -671,7 +671,7 @@ ld [rRAMG], a

```rgbasm, linenos

ld a, CART_SRAM_DISABLE
ld a, RAMG_SRAM_DISABLE
ld [rRAMG], a

```
Expand Down Expand Up @@ -703,7 +703,7 @@ When initializing your save data, you'll need to
;; Setup our save data
InitSaveData::

ld a, CART_SRAM_ENABLE
ld a, RAMG_SRAM_ENABLE
ld [rRAMG], a

ld a, 123
Expand All @@ -718,7 +718,7 @@ InitSaveData::
ld a, 0
ld [wCurrentLevel], a

ld a, CART_SRAM_DISABLE
ld a, RAMG_SRAM_DISABLE
ld [rRAMG], a

ret
Expand All @@ -731,14 +731,14 @@ Once your save file has been initialized, you can access any variable normally o
;; Setup our save data
StartNextLevel::

ld a, CART_SRAM_ENABLE
ld a, RAMG_SRAM_ENABLE
ld [rRAMG], a

ld a, [wCurrentLevel]
cp a, 3
call z, StartLevel3

ld a, CART_SRAM_DISABLE
ld a, RAMG_SRAM_DISABLE
ld [rRAMG], a

ret
Expand Down
2 changes: 1 addition & 1 deletion src/part1/hello_world.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ It's a good idea to create a new directory (`mkdir gb_hello_world`, for example,

Grab the following files (right-click each link, "Save Link As..."), and place them all in this new directory:
- [`hello-world.asm`](../assets/hello-world.asm)
- [`hardware.inc`](https://raw.githubusercontent.com/gbdev/hardware.inc/v4.0/hardware.inc)
- [`hardware.inc v5.3.0`](https://raw.githubusercontent.com/gbdev/hardware.inc/v5.3.0/hardware.inc)

Then, still from a terminal within that directory, run the following three commands.

Expand Down
16 changes: 8 additions & 8 deletions src/part2/bricks.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ It should go right **before** the momentum is modified.
BounceOnTop:
; Remember to offset the OAM position!
; (8, 16) in OAM coordinates is (0, 0) on the screen.
ld a, [_OAMRAM + 4]
ld a, [STARTOF(OAM) + 4]
sub a, 16 + 1
ld c, a
ld a, [_OAMRAM + 5]
ld a, [STARTOF(OAM) + 5]
sub a, 8
ld b, a
call GetTileByPixel ; Returns tile address in hl
Expand All @@ -47,10 +47,10 @@ BounceOnTop:
ld [wBallMomentumY], a

BounceOnRight:
ld a, [_OAMRAM + 4]
ld a, [STARTOF(OAM) + 4]
sub a, 16
ld c, a
ld a, [_OAMRAM + 5]
ld a, [STARTOF(OAM) + 5]
sub a, 8 - 1
ld b, a
call GetTileByPixel
Expand All @@ -62,10 +62,10 @@ BounceOnRight:
ld [wBallMomentumX], a

BounceOnLeft:
ld a, [_OAMRAM + 4]
ld a, [STARTOF(OAM) + 4]
sub a, 16
ld c, a
ld a, [_OAMRAM + 5]
ld a, [STARTOF(OAM) + 5]
sub a, 8 + 1
ld b, a
call GetTileByPixel
Expand All @@ -77,10 +77,10 @@ BounceOnLeft:
ld [wBallMomentumX], a

BounceOnBottom:
ld a, [_OAMRAM + 4]
ld a, [STARTOF(OAM) + 4]
sub a, 16 - 1
ld c, a
ld a, [_OAMRAM + 5]
ld a, [STARTOF(OAM) + 5]
sub a, 8
ld b, a
call GetTileByPixel
Expand Down
Loading