From b97aa85e98f80ff030cbd0d20e4ae62476ecbed1 Mon Sep 17 00:00:00 2001 From: mid-kid Date: Sun, 8 Aug 2021 22:20:44 +0200 Subject: [PATCH] Add ezflash testing program --- .gitignore | 1 + Makefile | 4 +- source/home/math.asm | 19 ++ source/home/text.asm | 37 +++- source/home/vblank.asm | 8 + source/main.asm | 7 +- source/prog/ezflash_test.asm | 393 +++++++++++++++++++++++++++++++++++ source/prog/serial_test.asm | 11 +- source/utils.inc | 23 ++ source/video.inc | 26 ++- 10 files changed, 510 insertions(+), 19 deletions(-) create mode 100644 source/prog/ezflash_test.asm create mode 100644 source/utils.inc diff --git a/.gitignore b/.gitignore index e3cedb4..55fb818 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /*.gb +/*.sav /*.map /*.sym /build diff --git a/Makefile b/Makefile index db67608..26ba308 100644 --- a/Makefile +++ b/Makefile @@ -9,9 +9,9 @@ RGBGFX := rgbgfx RGBLINK := rgblink RGBFIX := rgbfix -RGBASMFLAGS := -p 0xff -L +RGBASMFLAGS := -p 0xff -L -Weverything RGBLINKFLAGS := -p 0xff -d -t -RGBFIXFLAGS := -p 0xff -j -m 0 -r 0 -n 0 -k "HB" -i "SMTH" -t "Something" +RGBFIXFLAGS := -p 0xff -j -m 1 -r 0 -n 0 -k "HB" -i "SMTH" -t "Something" rwildcard = $(foreach d, $(wildcard $1*), $(filter $(subst *, %, $2), $d) $(call rwildcard, $d/, $2)) objects := $(patsubst $(dir_source)/%.asm, $(dir_build)/%.o, $(call rwildcard, $(dir_source)/, *.asm)) diff --git a/source/home/math.asm b/source/home/math.asm index 111adbc..646936b 100644 --- a/source/home/math.asm +++ b/source/home/math.asm @@ -13,7 +13,26 @@ bits_swap:: jr nc, .loop ret +; TODO: There was a more optimal way to do this with max 16 iterations +array_index:: +; hl: array base +; c: entry size +; a: index + and a + ret z + ld b, 0 +.loop + add hl, bc + dec a + jr nz, .loop + ret + divide:: +; a: value +; c: divisor +; Returns: +; b: result +; a: remainder ld b, 0 .loop inc b diff --git a/source/home/text.asm b/source/home/text.asm index 335cf54..bb70a08 100644 --- a/source/home/text.asm +++ b/source/home/text.asm @@ -30,7 +30,7 @@ print:: inc de jr print -print_num_3digit:: +print_dec_3digit:: ; a: Number ; de: Tilemap positon push af @@ -58,6 +58,41 @@ print_num_3digit:: jr nz, .loop ret +print_hex_4digit:: +; hl: Number +; de: Tilemap position + ld a, h + swap a + call print_hex_1digit + ld a, h + call print_hex_1digit + ld a, l + swap a + call print_hex_1digit + ld a, l + jr print_hex_1digit + +print_hex_2digit:: +; a: Number +; de: Tilemap position + ld b, a + swap a + call print_hex_1digit + ld a, b + jr print_hex_1digit + +print_hex_1digit: + and $f + add "0" + cp "9" + 1 + jr c, .skip + add "A" - "0" - $a +.skip + call lcd_blank_wait + ld [de], a + inc de + ret + font_load:: ld hl, gfx_font ld de, v_font_numbers diff --git a/source/home/vblank.asm b/source/home/vblank.asm index 0335ad9..6391900 100644 --- a/source/home/vblank.asm +++ b/source/home/vblank.asm @@ -34,3 +34,11 @@ vblank_wait:: and a jr z, .loop ret + +vblank_wait_frames:: + ld c, a +.loop + call vblank_wait + dec c + jr nz, .loop + ret diff --git a/source/main.asm b/source/main.asm index d98159f..ab24e10 100644 --- a/source/main.asm +++ b/source/main.asm @@ -1,21 +1,20 @@ include "charmap.inc" -include "defines.inc" include "video.inc" section "main.asm", rom0 main:: + ;call prog_serial_test + call prog_ezflash_test + call lcd_disable call font_load call pals_set_bw - ;call vram_clear_bgmap call lcd_enable ld hl, string_something bgcoord de, (screen_width - string_something.end + string_something + 1) / 2, 2 call print - - ;call prog_serial_test jp _halt_ string_something: db "SOMETHING$" diff --git a/source/prog/ezflash_test.asm b/source/prog/ezflash_test.asm new file mode 100644 index 0000000..6407d43 --- /dev/null +++ b/source/prog/ezflash_test.asm @@ -0,0 +1,393 @@ +include "charmap.inc" +include "video.inc" +include "defines.inc" +include "utils.inc" +include "hardware.inc" + +cursor_max equ 5 + +section "prog/ezflash_test.asm@wram", wram0 + +w_cursor: db +w_write_val: db +w_write_addr: dw +w_write2_val: db +w_write2_addr: dw +w_read_addr: dw + +section "prog/ezflash_test.asm", rom0 + +prog_ezflash_test:: + call lcd_disable + call font_load + call pals_set_bw + call vram_clear_bgmap + call lcd_enable + + ld hl, string_title + bgcoord de, (screen_width - string_title.end + string_title + 1) / 2, 2 + call print + + ld hl, string_write + bgcoord de, 2, 5 + call print + ld hl, string_write + bgcoord de, 2, 7 + call print + ld hl, string_read + bgcoord de, 2, 10 + call print + + xor a + ld [w_cursor], a + ld [w_write_val], a + ld [w_write2_val], a + ld [w_write2_addr + 0], a + ld [w_read_addr + 0], a + ld a, $a0 + ld [w_read_addr + 1], a + ld a, $c0 + ld [w_write_addr + 0], a + ld a, $7f + ld [w_write_addr + 1], a + ld a, $a0 + ld [w_write2_addr + 1], a + + ld hl, ezf_write_code + ld de, ezf_write_ram + ld bc, ezf_write_end - ezf_write_ram + call memcpy + + call ezf_unlock + + call update_write_addr + call update_write_val + call update_write2_addr + call update_write2_val + call update_read_addr + xor a + call update_cursor + +main: + call update_read_view + call vblank_wait + call joypad_update + ldh a, [h_joypad_pressed] + rra ; joy_a + jr c, .joy_a + rra ; joy_b + rra ; joy_select + rra ; joy_start + rra ; joy_right + jr c, .joy_right + rra ; joy_left + jr c, .joy_left + rra ; joy_up + jr c, .joy_up + rra ; joy_down + jr c, .joy_down + jr main + +.joy_a + ld a, [w_cursor] + srl a + and a + jr z, .joy_a_write + dec a + jr z, .joy_a_write2 + jr main + +.joy_a_write + ld hl, w_write_addr + ld a, [hl+] + ld h, [hl] + ld l, a + ld a, [w_write_val] + call ezf_write + jr main + +.joy_a_write2 + ld hl, w_write2_addr + ld a, [hl+] + ld h, [hl] + ld l, a + ld a, [w_write2_val] + call ezf_write + jr main + +.joy_right + ld a, 1 + call update_cursor + jr main + +.joy_left + ld a, cursor_max - 1 + call update_cursor + jr main + +.joy_up + ld de, $10 + ldh a, [h_joypad_down] + bit joy_b_f, a + jr nz, .joy_up_done + ld de, 1 +.joy_up_done + call update_val + jr main + +.joy_down + ld de, -$10 + ldh a, [h_joypad_down] + bit joy_b_f, a + jr nz, .joy_down_done + ld de, -1 +.joy_down_done + call update_val + jr main + +update_val: +; de: value increment + ld a, [w_cursor] + ld hl, .jumptable + ld c, 2 + call array_index + ld a, [hl+] + ld h, [hl] + ld l, a + jp hl + +.jumptable + dw .write_addr + dw .write_val + dw .write2_addr + dw .write2_val + dw .read_addr + +.write_addr + ld hl, w_write_addr + ld a, [hl+] + ld h, [hl] + ld l, a + add hl, de + ld a, l + ld [w_write_addr + 0], a + ld a, h + ld [w_write_addr + 1], a + jp update_write_addr + +.write_val + ld hl, w_write_val + ld a, [hl] + add e + ld [hl], a + jp update_write_val + +.write2_addr + ld hl, w_write2_addr + ld a, [hl+] + ld h, [hl] + ld l, a + add hl, de + ld a, l + ld [w_write2_addr + 0], a + ld a, h + ld [w_write2_addr + 1], a + jp update_write2_addr + +.write2_val + ld hl, w_write2_val + ld a, [hl] + add e + ld [hl], a + jp update_write2_val + +.read_addr + ld hl, w_read_addr + ld a, [hl+] + ld h, [hl] + ld l, a + add hl, de + ld a, l + ld [w_read_addr + 0], a + ld a, h + ld [w_read_addr + 1], a + jp update_read_addr + +update_cursor: +; a: cursor increment + ld hl, w_cursor + ld e, [hl] + ld d, e + add e + ld c, cursor_max + call divide + ld [hl], a + ld e, a + ; d: old, e: new + + ld a, d + ld d, 0 + call .set + ld a, e + ld d, "-" + +.set +; a: position +; d: character + ld hl, .coords + ld c, 2 + call array_index + ld a, [hl+] + ld h, [hl] + ld l, a + call lcd_blank_wait + ld [hl], d + ret + +.coords + dbgcoord 8, 5 + dbgcoord 8 + 6, 5 + dbgcoord 8, 7 + dbgcoord 8 + 6, 7 + dbgcoord 8, 10 + +update_write_addr: + ld hl, w_write_addr + ld a, [hl+] + ld h, [hl] + ld l, a + bgcoord de, 9, 5 + jp print_hex_4digit + +update_write_val: + ld a, [w_write_val] + bgcoord de, 9 + 6, 5 + jp print_hex_2digit + +update_write2_addr: + ld hl, w_write2_addr + ld a, [hl+] + ld h, [hl] + ld l, a + bgcoord de, 9, 7 + jp print_hex_4digit + +update_write2_val: + ld a, [w_write2_val] + bgcoord de, 9 + 6, 7 + jp print_hex_2digit + +update_read_addr: + ld hl, w_read_addr + ld a, [hl+] + ld h, [hl] + ld l, a + bgcoord de, 9, 10 + jp print_hex_4digit + +update_read_view: + ld hl, w_read_addr + ld a, [hl+] + ld h, [hl] + ld l, a + bgcoord de, 2, 12 + ld b, 4 +.loop + ld c, 8 +.loop_inner + push bc + ld a, [hl+] + call print_hex_2digit + pop bc + dec c + jr nz, .loop_inner + add_r16_8 de, 32 - 16 + dec b + jr nz, .loop + ret + +ezf_unlock: + xor a + ld hl, $7f31 + call ezf_write + ld a, $80 + ld hl, $7f32 + jp ezf_write + +string_title: db "EZFLASH TESTER$" +.end +string_write: db "WRITE$" +string_read: db "READ$" + +ezf_write: +; a: value +; hl: address + ld b, a + ld a, h + cp $7f + jp nz, ezf_write_do + ld a, l + cp $d2 + jp z, .wait + cp $36 + jp nz, ezf_write_do + +.wait + push bc + push hl + ld hl, $7fc0 + ld b, $05 + call ezf_write_do + bgcoord de, 15, 10 + ld a, "." + call lcd_blank_wait + ld [de], a + pop hl + pop bc + call ezf_write_7fd2 + bgcoord de, 15, 10 + xor a + call lcd_blank_wait + ld [de], a + ret + +ezf_write_code: +load "prog/ezflash_test.asm@ezf_write", wram0 +ezf_write_ram: +ezf_write_do: + ld de, $7f00 + ld a, $e1 + ld [de], a + ld e, $10 + inc a + ld [de], a + ld e, $20 + inc a + ld [de], a + ld [hl], b + ld e, $f0 + inc a + ld [de], a + ret + +ezf_write_7fd2: + di + call ezf_write_do + ld de, $a000 +.loop + ld a, [de] + and a + jr nz, .loop + + jr @ ; TEST + + ld b, 0 + call ezf_write_do + ei + ret + +; 7f32:01 -> 7fd2:01 (hang) +; 7f36:02 (hang) +; 7f36:03 (hang) + +ezf_write_end: diff --git a/source/prog/serial_test.asm b/source/prog/serial_test.asm index 9ea4a10..6662535 100644 --- a/source/prog/serial_test.asm +++ b/source/prog/serial_test.asm @@ -8,6 +8,7 @@ prog_serial_test:: call lcd_disable call font_load call pals_set_bw + call vram_clear_bgmap call lcd_enable ld hl, string_title @@ -16,8 +17,9 @@ prog_serial_test:: call serial_init_master -main_loop: +main: call joypad_update + ldh a, [h_joypad_released] bit joy_a_f, a jr nz, .send_shit @@ -27,14 +29,9 @@ main_loop: .continue call vblank_wait - jr main_loop + jr main .send_shit - call vblank_wait - call joypad_update - bit joy_a_f, a - jr nz, .send_shit - bgcoord hl, 7, 5 call lcd_blank_wait ld [hl], "A" diff --git a/source/utils.inc b/source/utils.inc new file mode 100644 index 0000000..50aa96c --- /dev/null +++ b/source/utils.inc @@ -0,0 +1,23 @@ +if !def(_utils_inc_) +_utils_inc_ equ 0 + +_add_r16_8: macro + ld a, \2 + add (\3) + ld \2, a + adc \1 + sub \2 + ld \1, a +endm + +add_r16_8: macro +if "\1" == "hl" + _add_r16_8 h, l, (\2) +elif "\1" == "bc" + _add_r16_8 b, c, (\2) +elif "\1" == "de" + _add_r16_8 d, e, (\2) +endc +endm + +endc diff --git a/source/video.inc b/source/video.inc index 9f1bd66..274661a 100644 --- a/source/video.inc +++ b/source/video.inc @@ -12,20 +12,36 @@ len_2bpp_tile equ 2 * tile_height tile equs "+ len_2bpp_tile *" tiles equs "* len_2bpp_tile" -coord: MACRO +coord: macro if _NARG < 4 ld \1, (\3) * screen_width + (\2) + w_tilemap else - ld \1, (\3) * screen_height + (\2) + (\4) + ld \1, (\3) * screen_width + (\2) + (\4) endc -ENDM +endm -bgcoord: MACRO +dcoord: macro +if _NARG < 4 + dw (\2) * screen_width + (\1) + w_tilemap +else + dw (\2) * screen_width + (\1) + (\3) +endc +endm + +bgcoord: macro if _NARG < 4 ld \1, (\3) * bgmap_width + (\2) + v_bgmap1 else ld \1, (\3) * bgmap_width + (\2) + (\4) endc -ENDM +endm + +dbgcoord: macro +if _NARG < 3 + dw (\2) * bgmap_width + (\1) + v_bgmap1 +else + dw (\2) * bgmap_width + (\1) + (\3) +endc +endm endc