diff --git a/Makefile b/Makefile index c08b912..f048059 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ dirs: cpu: cpuDirs \ ${BIN_DIR}/cpu/callRet.gb \ ${BIN_DIR}/cpu/memcpy.gb \ + ${BIN_DIR}/cpu/miscInstrs.gb \ ${BIN_DIR}/cpu/rst.gb cpuDirs: @@ -40,6 +41,11 @@ ${BIN_DIR}/cpu/memcpy.gb: dirs cpu/memcpy.asm ${LD} -o ${BIN_DIR}/cpu/memcpy.gb ${OBJ_DIR}/cpu/memcpy.o ${FIX} ${BIN_DIR}/cpu/memcpy.gb +${BIN_DIR}/cpu/miscInstrs.gb: dirs cpu/miscInstrs.asm + ${AS} -o ${OBJ_DIR}/cpu/miscInstrs.o cpu/miscInstrs.asm + ${LD} -o ${BIN_DIR}/cpu/miscInstrs.gb ${OBJ_DIR}/cpu/miscInstrs.o + ${FIX} ${BIN_DIR}/cpu/miscInstrs.gb + ${BIN_DIR}/cpu/rst.gb: dirs cpu/rst.asm ${AS} -o ${OBJ_DIR}/cpu/rst.o cpu/rst.asm ${LD} -o ${BIN_DIR}/cpu/rst.gb ${OBJ_DIR}/cpu/rst.o diff --git a/common/functions.inc b/common/functions.inc index 6c34ebd..d858c26 100644 --- a/common/functions.inc +++ b/common/functions.inc @@ -1,5 +1,20 @@ SECTION "functions", ROM0[$2000] +_lcdDisable: MACRO + call waitVBlank + ld hl, $ff40 + ld a, [hl] + res 7, a + ld [hl], a + ENDM + +_lcdEnable: MACRO + ld hl, $ff40 + ld a, [hl] + set 7, a + ld [hl], a + ENDM + ; 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: @@ -7,11 +22,7 @@ clearLogo: push hl ; disable the LCD - call waitVBlank - ld hl, $ff40 - ld a, [hl] - res 7, a - ld [hl], a + _lcdDisable ; wipe the logo ld hl, $81A0 @@ -26,10 +37,7 @@ clearLogo: jr nz, .loop ; enable the LCD and return - ld hl, $ff40 - ld a, [hl] - set 7, a - ld [hl], a + _lcdEnable pop af pop hl @@ -41,16 +49,10 @@ clearLogo: ; Modifies: ; HL - End result: HL + 16 fillTile: - push af push de ld d, 16 - ld a, b -.loop: - ld [hli], a - dec d - jr nz, .loop + call memset pop de - pop af ret ; Copies up to 256 bytes of memory to another location. @@ -70,6 +72,24 @@ memcpy: 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 diff --git a/cpu/callRet.asm b/cpu/callRet.asm index 464dd08..5d6ee6c 100644 --- a/cpu/callRet.asm +++ b/cpu/callRet.asm @@ -19,6 +19,7 @@ ProgramStart: call waitVBlank ld b, $ff ; Fill pattern. + ld sp, $e000 call clearLogo @@ -76,10 +77,10 @@ ProgramStart: ; Stack pointer validation. ld hl, sp+$00 ld a, h - cp $ff + cp $e0 jr nz, loop ld a, l - cp $fe + cp $00 jr nz, loop ld hl, $8180 call waitVBlank diff --git a/cpu/miscInstrs.asm b/cpu/miscInstrs.asm new file mode 100644 index 0000000..3257849 --- /dev/null +++ b/cpu/miscInstrs.asm @@ -0,0 +1,35 @@ +HeaderTitle EQUS "\"MISCINSTRS\"" + +include "common/defines.inc" +include "common/header.inc" +include "common/functions.inc" + +SECTION "home", ROM0 +ProgramStart: + call clearLogo + + _lcdDisable + + ; Set a proper grayscale palette. + ld a, %11100100 + ldh [$ff47], a + + ; TODO: Write actual tests. :) + ld a, $ff + ld hl, $8014 + ld [hl], a + ld hl, $8016 + ld [hl], a + ld hl, $8019 + ld [hl], a + ld hl, $801b + ld [hli], a + ld [hli], a + ld [hli], a + ld [hli], a + ld [hli], a + call memset + _lcdEnable + +.loop: + jr .loop