Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
aaaaaa123456789 committed Sep 23, 2020
2 parents 4a47df0 + aed4048 commit 974be29
Show file tree
Hide file tree
Showing 9 changed files with 357 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/basic.asm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ BasicTests:
.write
db "Register writes@"
.increment
db "Second increment@"
db "Seconds increment@"
.rollovers
db "Rollovers@"
.overflow
Expand Down
2 changes: 1 addition & 1 deletion src/header.asm
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Init:
ldh [c], a
ldh [c], a
.no_color
ld a, %11101100
ld a, %11011100
ldh [rBGP], a
ld de, $8000
ld b, %11111111
Expand Down
12 changes: 1 addition & 11 deletions src/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ MainMenu:
add hl, de
inc b
dec a
jr z, .got_selection
jr nz, .got_selection
dec b
jr nz, .no_top_wrap
ld b, c
Expand Down Expand Up @@ -210,13 +210,3 @@ RunTests:

.return
db "* Return@"

Menus:
dw .basic_tests, BasicTests
dw .range_tests, RangeTests
.end

.basic_tests
db "Basic tests@"
.range_tests
db "Range tests@"
3 changes: 3 additions & 0 deletions src/rom.asm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ INCLUDE "joypad.asm"
INCLUDE "rtc.asm"
INCLUDE "text.asm"
INCLUDE "timing.asm"
INCLUDE "wait.asm"

; Tests
INCLUDE "tests.asm"
INCLUDE "basic.asm"
INCLUDE "range.asm"
INCLUDE "subsec.asm"
201 changes: 201 additions & 0 deletions src/subsec.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
SubsecondTests:
dw .short_second_write, ShortSecondWrite
dw .long_second_write, LongSecondWrite
dw .short_minute_write, ShortMinuteWrite
dw .long_minute_write, LongMinuteWrite
dw .hour_write, HourWrite
dw .day_low_write, DayLowWrite
dw .day_high_write, DayHighWrite
dw .rtc_off_write, RTCOffTimingTest
dw -1

.short_second_write
db "RTCS/500@"
.long_second_write
db "RTCS/900@"
.short_minute_write
db "RTCM/50@"
.long_minute_write
db "RTCM/600@"
.hour_write
db "RTCH/200@"
.day_low_write
db "RTCDL/800@"
.day_high_write
db "RTCDH/300@"
.rtc_off_write
db "RTC off/400@"

ShortSecondWrite:
write_RTC_register RTCDH, 0
call WaitNextRTCTick ;ensure that the RTC is actually working!
jp c, TimeoutResult
ld a, RTCS
ld [rRAMB], a
call PrepareTimer
latch_RTC
ld hl, $a000
ld b, [hl]
.wait
latch_RTC
ld a, [hl]
cp b
jr z, .wait
ld b, a
ld a, 10
call WaitATimes50ms
ld [hl], b
start_timer
.check
latch_RTC
ld a, [hl]
cp b
check_timer .check, nz
jr LongSecondWrite.done

LongSecondWrite:
call WaitNextRTCTick
jp c, TimeoutResult
ld a, RTCS
ld [rRAMB], a
ld hl, $a000
.reject
call Random
and 63
cp 58
jr nc, .reject
inc a
ld b, a
call PrepareTimer
ld [hl], b
.wait
latch_RTC
ld a, [hl]
cp b
jr z, .wait
ld a, 2
call WaitATimes50ms
ld [hl], 0
start_timer
.check
latch_RTC
ld a, [hl]
and a
check_timer .check, nz
.done
ld hl, hTestResult
call PrintTime
cpw de, 9920
ret c
cpw de, 10081
ccf
ret

; These tests are all identical. I only want to write this thing once.
___test_sub_second_register_write: MACRO
; in: \1: register, \2: time to tick when the register is written (in 50ms increments)
; no \@ in local labels because this is meant to be used as a function body
call WaitNextRTCTick
jp c, TimeoutResult
ld a, \1
ld [rRAMB], a
ld hl, $a000
ld [hl], 1
if (\1) == RTCM
dec a
else
ld a, RTCS
endc
ld [rRAMB], a
call PrepareTimer
latch_RTC
ld b, [hl]
.wait
latch_RTC
ld a, [hl]
cp b
jr z, .wait
ld b, a
ld a, 20 - (\2)
call WaitATimes50ms
start_timer ;start it a few cycles early to make up for the earlier delay
ld a, \1
ld [rRAMB], a
ld [hl], 0
if (\1) == RTCM
dec a
else
ld a, RTCS
endc
ld [rRAMB], a
.check
latch_RTC
ld a, [hl]
cp b
check_timer .check, nz
ld hl, hTestResult
call PrintTime
cpw de, (\2) * 500 - 80
ret c
cpw de, (\2) * 500 + 81
ccf
ret
ENDM

ShortMinuteWrite:
___test_sub_second_register_write RTCM, 1

LongMinuteWrite:
___test_sub_second_register_write RTCM, 12

HourWrite:
___test_sub_second_register_write RTCH, 4

DayLowWrite:
___test_sub_second_register_write RTCDL, 16

DayHighWrite:
___test_sub_second_register_write RTCDH, 6

RTCOffTimingTest:
write_RTC_register RTCDH, 0
call WaitNextRTCTick
jp c, TimeoutResult
ld a, RTCS
ld [rRAMB], a
latch_RTC
ld hl, $a000
ld b, [hl]
.wait
latch_RTC
ld a, [hl]
cp b
jr z, .wait
ld b, a
ld a, 12
call WaitATimes50ms
ld a, RTCDH
ld [rRAMB], a
ld [hl], $40
ld a, 30
.loop
rst WaitVBlank
dec a
jr nz, .loop
call PrepareTimer
ld [hl], 0
start_timer
ld a, RTCS
ld [rRAMB], a
.check
latch_RTC
ld a, [hl]
cp b
check_timer .check, nz
ld hl, hTestResult
call PrintTime
cpw de, 3920
ret c
cpw de, 4081
ccf
ret
28 changes: 28 additions & 0 deletions src/tests.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
; Test menus:
; - each entry contains two pointers: one to the label and one to the actual test list
; - the list ends with the .end label; no final entry is needed

Menus:
dw .basic_tests, BasicTests
dw .range_tests, RangeTests
dw .subsecond_tests, SubsecondTests
.end

.basic_tests
db "Basic tests@"
.range_tests
db "Range tests@"
.subsecond_tests
db "Sub-second writes@"

; Each test menu entry (defined at the top of its own file) defines the tests in order:
; SampleTests:
; dw .first_test, FirstTest
; dw .second_test, SecondTest
; dw -1
; Each entry contains a pointer to the label (that will be printed in front of its result) and a
; pointer to the actual test routine. If bit 15 of the label is set (by ORing $8000 into the label),
; the label will be ended with a colon and the result will be printed on the following line. If bit
; 15 of the test routine is set, the test will be skipped (with a result of N/A) if the previous
; test either failed or was also skipped.
; The test routine returns the result message in hTestResult and carry if the test failed.
13 changes: 11 additions & 2 deletions src/text.asm
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,22 @@ PrintTime:
push hl
ld d, h
ld e, l
ld hl, .timeout_string
ld hl, TimeoutString
rst Print
ld a, "@"
ld [de], a
pop hl
pop de
ret

.timeout_string
TimeoutString:
db "TIMEOUT@"

TimeoutResult:
ld hl, TimeoutString
ld de, hTestResult
rst Print
ld a, "@"
ld [de], a
scf
ret
84 changes: 84 additions & 0 deletions src/wait.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
WaitATimes50ms:
; exactly what it says on the can (assumes a > 0)
; preserves everything but af
; 50 ms = 52428.8 cycles. This is not a nice number by any means.
; all indicated loop timings are one cycle too long (because the last jump is not taken)
push bc
push hl
ld l, a
ld b, a
ld c, 0
; running total (including call): 18 cycles
.longwait
ld a, 49
.inner
dec a
jr nz, .inner
dec bc
ld a, b
or c
jr nz, .longwait
; loop total: 204 cycles (times 256 * N iterations)
; running total: 52224 * N + 17 cycles
; remaining: 204.8 * (N - 1) + 187.8 cycles
ld c, l
ld b, a ;a = 0 here
ld a, 2
; this weird way of dividing by 5 ensures that the time taken is a linear function of N
.divloop
inc a
cp 5
jr nz, .first_div_skip
inc b
.first_div_skip
cp 5
jr nz, .second_div_skip
xor a
.second_div_skip
dec c
jr nz, .divloop
; loop total: 15 cycles (times N iterations)
; remaining: 189.8 * (N - 1) + 169.8 cycles
; = 190 * (N - 1) + 170 - b cycles (because b = N / 5 at this point, rounded to nearest)
dec l
jr z, .no_extra
.extraloop
ld a, 46
.extrainner
dec a
jr nz, .extrainner
nop
dec l
jr nz, .extraloop
; loop total: 190 cycles (times N - 1 iterations)
add hl, hl ;dummy instruction to make up for the two lost cycles for jumps not taken
.no_extra
; remaining: 166 - b cycles (b <= 51)
ld a, 138
sub b
; remaining: 163 - b = a + 25 cycles
ld b, a
srl b
srl b
.final_loop
dec b
jr nz, .final_loop
; loop total: 4 cycles (times a & ~3 iterations)
cpl
and 3
; remaining: (a ^ 3) + 18 cycles
add a, LOW(.exit)
ld l, a
adc HIGH(.exit)
sub l
ld h, a
jp hl
; remaining: (old a ^ 3) + 10 cycles - exactly enough for 0-3 nops, two pops and a ret

.exit
nop
nop
nop
pop hl
pop bc
ret
Loading

0 comments on commit 974be29

Please sign in to comment.