You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
219 lines
3.6 KiB
219 lines
3.6 KiB
SECTION "functions", ROMX[$4000] |
|
|
|
; Disables the LCD during the next vblank period. |
|
; Clobbers HL. |
|
_lcdDisable: MACRO |
|
call waitVBlank |
|
ld hl, $ff40 |
|
ld a, [hl] |
|
res 7, a |
|
ld [hl], a |
|
ENDM |
|
|
|
; Enables the LCD immediately. |
|
; Clobbers HL. |
|
_lcdEnable: MACRO |
|
ld hl, $ff40 |
|
ld a, [hl] |
|
set 7, a |
|
ld [hl], a |
|
ENDM |
|
|
|
; Completely the flags register. |
|
; Modifies: |
|
; F - End result: 0 |
|
clearFlags: |
|
push bc |
|
ld b, a |
|
ld c, 0 |
|
push bc |
|
pop af |
|
pop bc |
|
ret |
|
|
|
; Clears the Nintendo logo tile data. This is intended to leave the screen open |
|
; for simple (i.e. a bunch of boxes drawn with fillTile) test output. |
|
clearLogo: |
|
push af |
|
push hl |
|
|
|
; disable the LCD |
|
_lcdDisable |
|
|
|
; wipe the logo |
|
ld hl, $81A0 |
|
.loop: |
|
ld a, $0 |
|
ld [hld], a |
|
ld a, h |
|
cp $80 |
|
jr nz, .loop |
|
ld a, l |
|
cp $0f |
|
jr nz, .loop |
|
|
|
; enable the LCD and return |
|
_lcdEnable |
|
|
|
pop af |
|
pop hl |
|
ret |
|
|
|
; Draws an inverted grid over where the Nintendo logo tile data normally |
|
; resides. This is useful for tests that use a grid output, as it will make the |
|
; tile number more obvious. This should be called after the tests are performed, |
|
; as fillTile and fillTileEx will draw over the grid. |
|
drawGrid: |
|
push hl |
|
push de |
|
push bc |
|
ld hl, $8010 ; Start address. |
|
ld de, $8190 ; End address. |
|
ld b, $f ; Used to find the top of each file. |
|
_gridLoop: |
|
ld a, l ; Check for the top of the tiles. |
|
and b |
|
jr z, _handleTop |
|
|
|
; Invert the right side of the tile. |
|
ld c, $01 |
|
ld a, [hl] |
|
xor c |
|
ld [hli], a |
|
|
|
; See if we've reached the end. |
|
ld a, h |
|
cp d |
|
jr nz, _gridLoop |
|
ld a, l |
|
cp e |
|
jr nz, _gridLoop |
|
|
|
; Fin. |
|
pop bc |
|
pop de |
|
pop hl |
|
ret |
|
|
|
_handleTop: |
|
; Each row is stuffed into two bytes, so two writes are required to fully |
|
; invert the top row. |
|
ld a, [hl] |
|
cpl |
|
ld [hli], a |
|
ld a, [hl] |
|
cpl |
|
ld [hli], a |
|
jr _gridLoop |
|
|
|
; Writes a value across 16 bytes, starting at HL. Intended to fill a single tile. |
|
; Parameters: |
|
; B - The value to set the bytes to. |
|
; HL - Destintion location. |
|
; Modifies: |
|
; HL - End result: HL + 16 |
|
fillTile: |
|
push de |
|
ld d, 16 |
|
call memset |
|
pop de |
|
ret |
|
|
|
; Fills a 16 byte block with a specific pattern. Intended to fill a single tile |
|
; with a specific grayscale or color shade. |
|
; Parameters: |
|
; B - The pattern to write: |
|
; $00: $00 $00 |
|
; $01: $FF $00 |
|
; $02: $00 $FF |
|
; $03: $FF $FF |
|
; HL - Destintion location. |
|
; Modifies: |
|
; B - Set to $00 or $FF. |
|
; HL - End result: HL + 16 |
|
fillTileEx: |
|
push af |
|
ld a, b |
|
ld b, %11 |
|
and b |
|
jr nz, _check3 |
|
|
|
; b = $00. Fill with palette entry 0. |
|
ld b, $00 |
|
call fillTile |
|
pop af |
|
ret |
|
|
|
_check3: |
|
cp b |
|
jr nz, _handleAlt |
|
|
|
; b = $ff. Fill with palette entry 3. |
|
ld b, $ff |
|
call fillTile |
|
pop af |
|
ret |
|
|
|
_handleAlt: |
|
push de |
|
ld b, %01 |
|
ld d, 16 |
|
ld e, $ff |
|
cp b |
|
ld a, 0 |
|
jr nz, _fillLoop |
|
xor e |
|
_fillLoop: |
|
ld [hli], a |
|
xor e |
|
dec d |
|
jr nz, _fillLoop |
|
|
|
pop de |
|
pop af |
|
ret |
|
|
|
; Copies up to 256 bytes of memory to another location. |
|
; Parameter: |
|
; BC - Source location. |
|
; HL - Destination location. |
|
; D - The number of bytes to copy. |
|
; Modifies: |
|
; BC - End result: BC + D |
|
; HL - End result: BC + d |
|
; D - End result: $00 |
|
memcpy: |
|
ld a, [bc] |
|
ld [hli], a |
|
inc bc |
|
dec d |
|
ret z |
|
jr memcpy |
|
|
|
; Fills up to 256 bytes of memory with a set value, starting at HL. |
|
; Parameters: |
|
; B - The value to set the bytes to. |
|
; D - The number of bytes to modify. |
|
; HL - Destination location. |
|
; Modifies: |
|
; D - End result: 0 |
|
; HL - End result: HL + D |
|
memset: |
|
push af |
|
ld a, b |
|
.loop: |
|
ld [hli], a |
|
dec d |
|
jr nz, .loop |
|
pop af |
|
ret |
|
|
|
; Wait for vblank without using interrupts. |
|
waitVBlank: |
|
push af |
|
.loop: |
|
ldh a, [$ff44] |
|
cp $90 |
|
jr nz, .loop |
|
pop af |
|
ret
|
|
|