Skip to content

Commit

Permalink
Add timeouts to some LY waits
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekkio committed Sep 26, 2024
1 parent 7c364c9 commit 443f6e1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions common/common.s
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
.include "lib/quit_dump_mem.s"
.include "lib/reset_screen.s"
.include "lib/serial_send_byte.s"
.include "lib/wait_ly_with_timeout.s"

; --- Cartridge header ---

Expand Down
6 changes: 5 additions & 1 deletion common/lib/disable_ppu_safe.s
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ disable_ppu_safe:
ld hl, LCDC
bit 7, (hl)
ret z
wait_ly 144

push de
ld a, 144
call wait_ly_with_timeout
res 7, (hl)
pop de
ret
.ends
14 changes: 12 additions & 2 deletions common/lib/quit.s
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,25 @@ quit:
ret

@cb_return:
push de
call is_ppu_broken
jr c, @report_result

enable_ppu
wait_vblank

ld a, 143
call wait_ly_with_timeout
jr c, @report_result
ld a, 144
call wait_ly_with_timeout
; Extra vblank to account for initial (invisible) frame
wait_vblank
ld a, 143
call wait_ly_with_timeout
ld a, 144
call wait_ly_with_timeout

@report_result:
pop de
ld a, d
and a
jr nz, @failure
Expand Down
45 changes: 45 additions & 0 deletions common/lib/wait_ly_with_timeout.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
; Copyright (C) 2014-2024 Joonas Javanainen <joonas.javanainen@gmail.com>
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; SOFTWARE.

.ifdef FORCE_SECTIONS
.section "wait_ly_with_timeout" FORCE
.else
.section "wait_ly_with_timeout"
.endif
; Inputs:
; A expected LY value
; Outputs:
; cf 0 if LY value was seen, 1 if the wait timed out
; Preserved: E, HL
wait_ly_with_timeout:
ld d, a
ld bc, $0000
- ldh a, (<LY)
cp d
ret z ; cf=0 if the right LY value was seen
inc bc
ld a, b
or c
jr nz, -

@timeout:
scf
ret
.ends
1 change: 1 addition & 0 deletions utils/bootrom_dumper.s
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ tilemap_sadface:
.include "lib/reset_screen.s"
.include "lib/serial_memcpy.s"
.include "lib/serial_send_byte.s"
.include "lib/wait_ly_with_timeout.s"

.org $4000 - 3
jp $7D00
Expand Down

0 comments on commit 443f6e1

Please sign in to comment.