Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

451 lines
29KB

  1. <#
  2. .SYNOPSIS
  3. Check pixel-smooth scrolling of the C64 build headless in VICE.
  4. .DESCRIPTION
  5. Each scenario is a list of phases. A phase holds keys (faked as in tools/check-keys.ps1:
  6. tracepoints on the key_row_* instructions set the row bits, so host key presses cannot
  7. interfere) until a condition on the program state is met at people_moved (a frame
  8. number or a view position); the phase number lives in free RAM at $CFF0 and VICE
  9. conditional checkpoints move it on. Every frame the script logs the phase, view
  10. position, buffer state and scroll registers at people_moved; in the last phase it
  11. saves both buffers (bitmap and screen RAM), and the last save is checked.
  12. Scenarios:
  13. edges right+down to the bottom-right corner, hold there, then SHIFT (up+left)
  14. to the top-left corner, hold there, then release
  15. middle down, then right, then release in the middle of the world
  16. reverse right, reverse to left in the frame after a cell step, right again;
  17. then down, reverse to up right after a cell step; release
  18. tour (-Long only) about 5 minutes: six laps over the whole world, alternately
  19. row by row (right, down, left, ...; then diagonally up-left home) and
  20. column by column (down, right, up, ...), with still stops between legs.
  21. At the end of every stop both buffers are saved and checked like the
  22. final frame (people aligned with the terrain, no trails: R5.3, R3.2).
  23. Checks, per scenario:
  24. - the view moves at most one pixel per axis per frame, only in the direction of the
  25. keys read the frame before, and stays inside
  26. vx 0-336, vy 0-244; the fine-scroll registers match the view
  27. - every scenario reaches its last phase; edges: the view reaches vx 336 and vy 244,
  28. then 0 and 0, and does not move while the keys push against an edge
  29. - in every frame the shown buffer is complete (buf_ready) and built for the view's
  30. cell (buf_cx, buf_cy); a half-rebuilt buffer on screen tears the top of the map
  31. - the main loop never misses a frame and the people update ends before raster 55
  32. - nothing is stored into the shown buffer's map screen RAM or bitmap while the map
  33. is displayed (raster 55-210): such a store tears the picture
  34. - frames where a held key did not move the view although no edge was reached are
  35. counted as stalls and reported (they happen while the hidden buffer is not ready)
  36. - at the end, the buffer about to be shown (bitmap and screen RAM) equals a fresh
  37. render of the world from the view's top-left cell (39 columns, 21 rows) with this
  38. frame's dots ORed in; when a flip is pending, the shown buffer has last frame's dots
  39. - frame budget (from the CPU clock at frame_start, people_moved, frame_done, the
  40. raster interrupt's entry $FF48 and exit $EA81, start_rebuild and rebuild_ready):
  41. reported per scenario: frame start to frame start (17,095 cycles give or take the
  42. wait loop's exit point), the vertical blank work (scroll step, flip, people update),
  43. the main loop's busy and idle cycles, the interrupt cycles, and how many frames each
  44. hidden-buffer rebuild took (assumption A4: 8 frames allow 1 pixel per frame).
  45. Exit codes: 0 = pass, 1 = fail, 2 = a tool is missing.
  46. .EXAMPLE
  47. pwsh -File tools/check-scroll.ps1
  48. pwsh -File tools/check-scroll.ps1 -Long # also the 5-minute tour (several minutes)
  49. pwsh -File tools/check-scroll.ps1 -Long -ParseOnly -NoBuild # re-check the last run's logs
  50. #>
  51. [CmdletBinding()]
  52. param([switch]$NoBuild, [switch]$Long, [switch]$ParseOnly, [int]$TimeoutSeconds = 300, [int]$MaxStallFrames = 40)
  53. . (Join-Path $PSScriptRoot 'lib.ps1')
  54. function Get-Label([hashtable]$Target, [string]$Symbol) {
  55. foreach ($line in Get-Content -LiteralPath (Get-RepoPath $Target.build.labels)) {
  56. $parts = $line -split '\s+'
  57. if ($parts.Count -ge 3 -and $parts[2].TrimStart('.') -eq $Symbol) { return [Convert]::ToInt32($parts[1], 16) }
  58. }
  59. throw "Symbol '$Symbol' not found in $($Target.build.labels)."
  60. }
  61. # Tile data from src/tiles.inc: patterns in file order, colours as declared there.
  62. function Get-TileData {
  63. $src = Get-Content -LiteralPath (Get-RepoPath 'src/tiles.inc') -Raw
  64. $block = $src.Substring($src.IndexOf('tile_patterns:'))
  65. $block = $block.Substring(0, $block.IndexOf('.assert * - tile_patterns'))
  66. $patterns = @(foreach ($m in [regex]::Matches($block, '(?m)^\s*\.byte\s+([^;\r\n]+)')) {
  67. , @($m.Groups[1].Value -split ',' | ForEach-Object { [Convert]::ToByte($_.Trim().TrimStart('$'), 16) })
  68. })
  69. $colors = [System.Collections.Generic.List[byte]]::new()
  70. $cblock = $src.Substring($src.IndexOf('tile_colors:'))
  71. $cblock = $cblock.Substring(0, $cblock.IndexOf('.assert * - tile_colors'))
  72. $repeat = 1
  73. foreach ($line in $cblock -split "`n") {
  74. if ($line -match '\.repeat\s+(\d+)') { $repeat = [int]$Matches[1]; continue }
  75. if ($line -match '\.endrepeat') { $repeat = 1; continue }
  76. if ($line -match '\.byte\s+([^;\r\n]+)') {
  77. $vals = $Matches[1] -split ',' | ForEach-Object { [Convert]::ToByte($_.Trim().TrimStart('$'), 16) }
  78. for ($r = 0; $r -lt $repeat; $r++) { foreach ($v in $vals) { $colors.Add($v) } }
  79. }
  80. }
  81. @{ patterns = $patterns; colors = $colors.ToArray() }
  82. }
  83. try {
  84. $project = Get-ProjectConfig
  85. $target = Get-ProjectTargets $project 'c64'
  86. if (-not $NoBuild) { Invoke-TargetBuild $project $target @{} }
  87. $emu = Get-ToolPath $project $target.emulator.binary
  88. } catch [MissingToolException] {
  89. Write-Host $_.Exception.Message -ForegroundColor Yellow
  90. exit 2
  91. }
  92. $tiles = Get-TileData
  93. $L = @{}
  94. foreach ($s in 'smoke_ready', 'people_moved', 'world_tiles', 'people_cx', 'visible', 'view_cx', 'map_d011', 'map_d016',
  95. 'move_frame', 'people_flip_pending', 'buf_cx', 'buf_cy', 'buf_ready', 'view_fx', 'view_cy', 'view_fy',
  96. 'frame_start', 'frame_done', 'start_rebuild', 'rebuild_ready',
  97. 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space') { $L[$s] = Get-Label $target $s }
  98. $IRQ_ENTRY = 0xFF48; $IRQ_EXIT = 0xEA81 # KERNAL IRQ entry and the exit our handler jumps to
  99. $FRAME_CYCLES = 17095
  100. # Key bits (active low): cursor column RIGHT = bit 2, DOWN = bit 7; left SHIFT = bit 7.
  101. $RIGHT_DOWN = '7b'; $DOWN = '7f'; $RIGHTK = 'fb'; $NONE = 'ff'; $SHIFT = '7f'
  102. $PHASE = 0xCFF0 # free RAM (the program ends below $2700)
  103. # Phase end conditions: frame = move_frame value (wraps every 256 frames); view = cell and fine.
  104. function Frame([int]$n) { , (, @($L.move_frame, $n)) }
  105. function View([int]$cx, [int]$fx, [int]$cy, [int]$fy) {
  106. $c = @()
  107. if ($cx -ge 0) { $c += , @($L.view_cx, $cx); $c += , @($L.view_fx, $fx) }
  108. if ($cy -ge 0) { $c += , @($L.view_cy, $cy); $c += , @($L.view_fy, $fy) }
  109. $c
  110. }
  111. $scenarios = @(
  112. @{ name = 'edges'; cycles = 30000000; phases = @(
  113. @{ cursor = $NONE; lshift = $NONE; until = (Frame 30) }
  114. @{ cursor = $RIGHT_DOWN; lshift = $NONE; until = (View 42 0 30 4) } # vx 336, vy 244
  115. @{ cursor = $RIGHT_DOWN; lshift = $NONE; until = (Frame 0); edge = $true }
  116. @{ cursor = $RIGHT_DOWN; lshift = $SHIFT; until = (View 0 0 0 0) }
  117. @{ cursor = $RIGHT_DOWN; lshift = $SHIFT; until = (Frame 128); edge = $true }
  118. @{ cursor = $NONE; lshift = $NONE }) }
  119. @{ name = 'middle'; cycles = 12000000; phases = @(
  120. @{ cursor = $NONE; lshift = $NONE; until = (Frame 30) }
  121. @{ cursor = $DOWN; lshift = $NONE; until = (View -1 0 12 3) } # vy 99
  122. @{ cursor = $RIGHTK; lshift = $NONE; until = (View 16 5 -1 0) } # vx 133
  123. @{ cursor = $NONE; lshift = $NONE }) }
  124. @{ name = 'reverse'; cycles = 12000000; phases = @(
  125. @{ cursor = $NONE; lshift = $NONE; until = (Frame 30) }
  126. @{ cursor = $RIGHTK; lshift = $NONE; until = (View 9 0 -1 0) } # just stepped into cell 9
  127. @{ cursor = $RIGHTK; lshift = $SHIFT; until = (View 3 2 -1 0) }
  128. @{ cursor = $RIGHTK; lshift = $NONE; until = (View 5 0 -1 0) }
  129. @{ cursor = $DOWN; lshift = $NONE; until = (View -1 0 6 0) } # just stepped into row 6
  130. @{ cursor = $DOWN; lshift = $SHIFT; until = (View -1 0 2 1) }
  131. @{ cursor = $NONE; lshift = $NONE }) }
  132. )
  133. if ($Long) {
  134. $R = @{ cursor = $RIGHTK; lshift = $NONE }; $Lf = @{ cursor = $RIGHTK; lshift = $SHIFT }
  135. $D = @{ cursor = $DOWN; lshift = $NONE }; $U = @{ cursor = $DOWN; lshift = $SHIFT }
  136. $UL = @{ cursor = $RIGHT_DOWN; lshift = $SHIFT }
  137. $tour = [System.Collections.Generic.List[hashtable]]::new()
  138. $leg = { param($keys, $until) $tour.Add(@{ cursor = $keys.cursor; lshift = $keys.lshift; until = $until }) }
  139. $stop = { $tour.Add(@{ cursor = $NONE; lshift = $NONE; until = (Frame ((61 * $tour.Count + 17) % 256)); snap = $true }) }
  140. $tour.Add(@{ cursor = $NONE; lshift = $NONE; until = (Frame 30) })
  141. for ($lap = 0; $lap -lt 6; $lap++) {
  142. if ($lap % 2 -eq 0) { # rows: ends at the top-left
  143. foreach ($row in 5, 10, 15, 20, 25) {
  144. $right = (($row / 5) % 2) -eq 1
  145. if ($right) { & $leg $R (View 42 0 -1 0) } else { & $leg $Lf (View 0 0 -1 0) }
  146. & $leg $D (View -1 0 $row 0)
  147. if ($row % 10 -eq 0) { & $stop }
  148. }
  149. & $leg $Lf (View 0 0 -1 0); & $leg $D (View -1 0 30 4); & $leg $R (View 42 0 -1 0); & $stop
  150. & $leg $UL (View 0 0 0 0); & $stop
  151. } else { # columns: ends at the top-left
  152. foreach ($col in 8, 16, 24, 32, 42) {
  153. $down = (($col -eq 8) -or ($col -eq 24) -or ($col -eq 42))
  154. if ($down) { & $leg $D (View -1 0 30 4) } else { & $leg $U (View -1 0 0 0) }
  155. & $leg $R (View $col 0 -1 0)
  156. if ($col -eq 16 -or $col -eq 42) { & $stop }
  157. }
  158. & $leg $U (View -1 0 0 0); & $leg $Lf (View 0 0 -1 0); & $stop
  159. }
  160. }
  161. $tour.Add(@{ cursor = $NONE; lshift = $NONE })
  162. $scenarios += @{ name = 'tour'; cycles = 400000000; timeout = 3600; phases = $tour.ToArray(); maxStalls = 6 * $tour.Count }
  163. }
  164. $root = Get-RepoPath 'build/check-scroll'
  165. if ((Test-Path -LiteralPath $root) -and -not $ParseOnly) { Remove-Item -LiteralPath $root -Recurse -Force }
  166. $failedScenarios = 0
  167. foreach ($sc in $scenarios) {
  168. $dir = Join-Path $root $sc.name
  169. if (-not $ParseOnly) { New-Item -ItemType Directory -Force -Path $dir | Out-Null }
  170. $mon = [System.Collections.Generic.List[string]]::new()
  171. $mon.Add('logname "monitor.log"'); $mon.Add('log on')
  172. $n = 0
  173. $add = { param($addr, $cmd, $cond)
  174. $script:n++
  175. $line = 'trace exec ${0:x4}' -f $addr
  176. if ($cond) { $line += ' if ' + (($cond | ForEach-Object { '(@cpu:${0:x4} == ${1:x2})' -f $_[0], $_[1] }) -join ' && ') }
  177. $mon.Add($line); $mon.Add(('command {0} "{1}"' -f $script:n, $cmd))
  178. }
  179. $lastPhase = $sc.phases.Count - 1
  180. & $add $L.smoke_ready ('bsave \"world.bin\" 0 {0:x4} {1:x4}' -f $L.world_tiles, ($L.world_tiles + 3999)) $null
  181. & $add $L.smoke_ready ('> {0:x4} 00' -f $PHASE) $null
  182. foreach ($row in 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space') { & $add $L[$row] 'r a = ff' $null }
  183. for ($ph = 0; $ph -le $lastPhase; $ph++) { # later checkpoints at one address run after earlier ones
  184. $k = $sc.phases[$ph]
  185. if ($k.cursor -ne $NONE) { & $add $L.key_row_cursor "r a = $($k.cursor)" @(, @($PHASE, $ph)) }
  186. if ($k.lshift -ne $NONE) { & $add $L.key_row_lshift "r a = $($k.lshift)" @(, @($PHASE, $ph)) }
  187. }
  188. # Snapshots at the end of each "snap" stop: before the phase changes, so they run in the same hit.
  189. $saves = @('bitmap-a.bin 6000 7a3f', 'bitmap-b.bin a000 ba3f', 'screen-a.bin 4000 4347', 'screen-b.bin 8000 8347')
  190. for ($ph = 0; $ph -lt $lastPhase; $ph++) {
  191. if (-not $sc.phases[$ph].snap) { continue }
  192. foreach ($save in $saves) {
  193. $f = $save -split ' '
  194. & $add $L.people_moved ('bsave \"snap{3}-{0}\" 0 {1} {2}' -f $f[0], $f[1], $f[2], $ph) (@(, @($PHASE, $ph)) + $sc.phases[$ph].until)
  195. }
  196. }
  197. for ($ph = $lastPhase - 1; $ph -ge 0; $ph--) { # descending, so one hit moves on at most one phase
  198. & $add $L.people_moved ('> {0:x4} {1:x2}' -f $PHASE, ($ph + 1)) (@(, @($PHASE, $ph)) + $sc.phases[$ph].until)
  199. }
  200. $viewCheckpoint = $n + 1
  201. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.view_cx, ($L.view_cx + 3)) $null
  202. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.map_d011, ($L.map_d016)) $null
  203. & $add $L.people_moved ('m {0:x4} {0:x4}' -f $L.visible) $null
  204. & $add $L.people_moved ('m {0:x4} {0:x4}' -f $L.people_flip_pending) $null
  205. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.buf_cx, ($L.buf_cx + 1)) $null
  206. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.buf_cy, ($L.buf_cy + 1)) $null
  207. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.buf_ready, ($L.buf_ready + 1)) $null
  208. & $add $L.people_moved ('m {0:x4} {0:x4}' -f $PHASE) $null
  209. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.people_cx, ($L.people_cx + 79)) $null
  210. foreach ($save in $saves) {
  211. $f = $save -split ' '
  212. & $add $L.people_moved ('bsave \"{0}\" 0 {1} {2}' -f $f[0], $f[1], $f[2]) @(, @($PHASE, $lastPhase))
  213. }
  214. # Stores into the shown buffer's map (screen RAM rows 0-20, bitmap) while the map is displayed
  215. # (raster 55-210) would tear the picture. Armed at smoke_ready, so start-up is not counted.
  216. $firstStore = $n + 1
  217. foreach ($range in @(@(0, 0x4000, 0x4347), @(0, 0x6000, 0x7a3f), @(1, 0x8000, 0x8347), @(1, 0xa000, 0xba3f))) {
  218. $n++
  219. $mon.Add(('trace store ${0:x4} ${1:x4} if (@cpu:${2:x4} == ${3:x2}) && (RL > $36) && (RL < $d3)' -f $range[1], $range[2], $L.visible, $range[0]))
  220. $mon.Add("disable $n")
  221. }
  222. $lastStore = $n
  223. for ($c = $firstStore; $c -le $lastStore; $c++) { & $add $L.smoke_ready "enable $c" $null }
  224. # Timing events: a trace without a command prints the raster position and the CPU clock.
  225. $events = @{}
  226. foreach ($ev in @(@('start', $L.frame_start), @('done', $L.frame_done), @('irq', $IRQ_ENTRY), @('irqexit', $IRQ_EXIT),
  227. @('rbstart', $L.start_rebuild), @('rbready', $L.rebuild_ready))) {
  228. $n++; $mon.Add(('trace exec ${0:x4}' -f $ev[1])); $events[$n] = $ev[0]
  229. }
  230. if (-not $ParseOnly) {
  231. Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $mon
  232. Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
  233. $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target)
  234. $vargs += @('-limitcycles', [string]$sc.cycles, '-exitscreenshot', 'screen.png', '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target))
  235. $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } }
  236. $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru `
  237. -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err')
  238. $limit = if ($sc.timeout) { [Math]::Max($sc.timeout, $TimeoutSeconds) } else { $TimeoutSeconds }
  239. if (-not $p.WaitForExit($limit * 1000)) { $p.Kill($true) }
  240. }
  241. # Parse frames: view (4 bytes), d011/d016, visible, people table; clock and raster line.
  242. $frames = [System.Collections.Generic.List[hashtable]]::new()
  243. $timeline = [System.Collections.Generic.List[object]]::new() # (kind, clock, line)
  244. $cur = $null; $want = $null; $wantLine = 0
  245. foreach ($line in [IO.File]::ReadLines((Join-Path $dir 'monitor.log'), [Text.Encoding]::Latin1)) {
  246. if ($line.StartsWith('#') -and $line -match '^#(\d+) \(Trace\s+exec [0-9a-fA-F]+\)\s+(\d+)/') {
  247. $num = [int]$Matches[1]
  248. if ($num -eq $viewCheckpoint) {
  249. $cur = @{ line = [int]$Matches[2]; people = [System.Collections.Generic.List[byte]]::new() }; $frames.Add($cur); $want = 'view'; continue
  250. }
  251. if ($events.ContainsKey($num)) { $want = $events[$num]; $wantLine = [int]$Matches[2] }
  252. continue
  253. }
  254. if ($want -and $line -match '^\.C:.*\s(\d+)\s*$') {
  255. if ($want -eq 'view') { $cur.clock = [long]$Matches[1]; $timeline.Add(@('view', $cur.clock, $cur.line)) }
  256. else { $timeline.Add(@($want, [long]$Matches[1], $wantLine)) }
  257. $want = $null; continue
  258. }
  259. if ($null -eq $cur -or $line -notmatch '^>C:([0-9a-fA-F]{4}) ((?:[0-9a-fA-F]{2} )+)') { continue }
  260. $addr = [Convert]::ToInt32($Matches[1], 16)
  261. $bytes = @($Matches[2].Trim() -split ' ' | ForEach-Object { [Convert]::ToByte($_, 16) })
  262. if ($addr -eq $L.view_cx) { $cur.cx = $bytes[0]; $cur.fx = $bytes[1]; $cur.cy = $bytes[2]; $cur.fy = $bytes[3] }
  263. elseif ($addr -eq $L.map_d011) { $cur.d011 = $bytes[0]; $cur.d016 = $bytes[1] }
  264. elseif ($addr -eq $L.visible) { $cur.visible = $bytes[0] }
  265. elseif ($addr -eq $PHASE) { $cur.phase = $bytes[0] }
  266. elseif ($addr -eq $L.people_flip_pending) { $cur.pending = $bytes[0] }
  267. elseif ($addr -eq $L.buf_cx) { $cur.bufcx = $bytes[0..1] }
  268. elseif ($addr -eq $L.buf_cy) { $cur.bufcy = $bytes[0..1] }
  269. elseif ($addr -eq $L.buf_ready) { $cur.ready = $bytes[0..1] }
  270. elseif ($addr -ge $L.people_cx -and $addr -lt $L.people_cx + 80) { foreach ($b in $bytes) { if ($cur.people.Count -lt 80) { $cur.people.Add($b) } } }
  271. }
  272. # The run stops at a cycle limit, possibly while the last frame was being logged.
  273. $fields = 'cx', 'd011', 'visible', 'pending', 'bufcx', 'bufcy', 'ready', 'phase', 'clock'
  274. while ($frames.Count -gt 0) {
  275. $lf = $frames[$frames.Count - 1]
  276. if ($lf.people.Count -eq 80 -and -not @($fields | Where-Object { $null -eq $lf[$_] })) { break }
  277. $frames.RemoveAt($frames.Count - 1)
  278. }
  279. $failures = [System.Collections.Generic.List[string]]::new()
  280. # A rejected checkpoint shifts the numbers of the ones after it, so commands would land on the wrong ones.
  281. $monErrors = @(Select-String -LiteralPath (Join-Path $dir 'monitor.log') -Pattern '^ERROR|not a valid checkpoint' | Select-Object -First 3)
  282. foreach ($e in $monErrors) { $failures.Add("monitor script error: $($e.Line.Trim())") }
  283. if ($frames.Count -lt 100) { $failures.Add("only $($frames.Count) frames logged") }
  284. $tears = @(Select-String -LiteralPath (Join-Path $dir 'monitor.log') -Pattern '^#(\d+) \(Trace\s+store ([0-9a-fA-F]{4})\)\s+(\d+)/' |
  285. Where-Object { [int]$_.Matches[0].Groups[1].Value -ge $firstStore -and [int]$_.Matches[0].Groups[1].Value -lt $firstStore + 4 })
  286. if ($tears.Count -gt 0) {
  287. $t = $tears[0].Matches[0].Groups
  288. $failures.Add("$($tears.Count) store(s) into the shown buffer while the map was displayed (first: `$$($t[2].Value) at raster line $($t[3].Value))")
  289. }
  290. $stalls = 0; $steps = 0; $late = 0; $lastLine = 0
  291. $maxVx = 0; $maxVy = 0; $minAfterMaxVx = 999; $minAfterMaxVy = 999
  292. $phaseFrames = @(0) * $sc.phases.Count
  293. for ($i = 0; $i -lt $frames.Count; $i++) {
  294. $f = $frames[$i]
  295. $vx = 8 * $f.cx + $f.fx; $vy = 8 * $f.cy + $f.fy
  296. $f.vx = $vx; $f.vy = $vy
  297. if ($f.phase -lt $sc.phases.Count) { $phaseFrames[$f.phase]++ } else { $failures.Add("frame ${i}: phase $($f.phase) out of range") }
  298. if ($vx -gt 336 -or $vy -gt 244 -or $f.fx -gt 7 -or $f.fy -gt 7) { $failures.Add("frame ${i}: view out of range ($vx,$vy)") }
  299. if ($f.d016 -ne (0x10 -bor (7 - $f.fx)) -or $f.d011 -ne (0x30 -bor (7 - $f.fy))) { $failures.Add(("frame {0}: registers d011={1:x2} d016={2:x2} do not match view fine scroll ({3},{4})" -f $i, $f.d011, $f.d016, $f.fx, $f.fy)) }
  300. $v = $f.visible
  301. if (-not $f.ready[$v] -or $f.bufcx[$v] -ne $f.cx -or $f.bufcy[$v] -ne $f.cy) {
  302. $failures.Add("frame ${i}: shown buffer $v is not a complete render at the view cell (ready $($f.ready[$v]), origin $($f.bufcx[$v]),$($f.bufcy[$v]), view cell $($f.cx),$($f.cy))")
  303. }
  304. if ($f.line -ge 55 -and $f.line -lt 251) { $failures.Add("frame ${i}: people update ended at raster line $($f.line)") }
  305. elseif ($f.line -lt 55) { $lastLine = [Math]::Max($lastLine, $f.line) }
  306. if ($i -gt 0) {
  307. $p = $frames[$i - 1]
  308. $dx = $vx - $p.vx; $dy = $vy - $p.vy
  309. if ($dx -ne 0 -or $dy -ne 0) { $steps++ }
  310. if ($f.clock - $p.clock -gt 1.5 * 17095) { $late++ }
  311. # The keys read after people_moved in frame i-1 (that frame's phase) give frame i's step.
  312. $k = $sc.phases[[Math]::Min($p.phase, $lastPhase)]
  313. $bits = [Convert]::ToByte($k.cursor, 16)
  314. $sign = if ($k.lshift -eq $SHIFT) { -1 } else { 1 }
  315. $wantX = if (($bits -band 0x04) -eq 0) { $sign } else { 0 }
  316. $wantY = if (($bits -band 0x80) -eq 0) { $sign } else { 0 }
  317. if (($dx -ne 0 -and $dx -ne $wantX) -or ($dy -ne 0 -and $dy -ne $wantY)) {
  318. $failures.Add("frame ${i}: view moved by ($dx,$dy) with keys asking for ($wantX,$wantY)")
  319. }
  320. if ($k.edge -and $f.phase -eq $p.phase -and ($dx -ne 0 -or $dy -ne 0)) { $failures.Add("frame ${i}: view moved by ($dx,$dy) while held against an edge") }
  321. $canX = ($wantX -gt 0 -and $p.vx -lt 336) -or ($wantX -lt 0 -and $p.vx -gt 0)
  322. $canY = ($wantY -gt 0 -and $p.vy -lt 244) -or ($wantY -lt 0 -and $p.vy -gt 0)
  323. if (($canX -or $canY) -and $dx -eq 0 -and $dy -eq 0) { $stalls++ }
  324. }
  325. if ($vx -gt $maxVx) { $maxVx = $vx; $minAfterMaxVx = $vx }
  326. if ($vy -gt $maxVy) { $maxVy = $vy; $minAfterMaxVy = $vy }
  327. if ($vx -lt $minAfterMaxVx) { $minAfterMaxVx = $vx }
  328. if ($vy -lt $minAfterMaxVy) { $minAfterMaxVy = $vy }
  329. }
  330. if ($late -gt 0) { $failures.Add("the main loop missed $late frame(s)") }
  331. if ($phaseFrames[$lastPhase] -lt 10) { $failures.Add("reached the last phase for only $($phaseFrames[$lastPhase]) frame(s) (frames per phase: $($phaseFrames -join ', '))") }
  332. if ($sc.name -eq 'edges') {
  333. if ($maxVx -ne 336 -or $maxVy -ne 244) { $failures.Add("did not reach the bottom-right edge (max vx $maxVx, vy $maxVy)") }
  334. if ($minAfterMaxVx -ne 0 -or $minAfterMaxVy -ne 0) { $failures.Add("did not return to the top-left edge (min vx $minAfterMaxVx, vy $minAfterMaxVy)") }
  335. }
  336. $allowedStalls = if ($sc.maxStalls) { $sc.maxStalls } else { $MaxStallFrames }
  337. if ($stalls -gt $allowedStalls) { $failures.Add("$stalls stall frames (allowed $allowedStalls)") }
  338. # Frame budget from the timeline. Events before the first frame_start (start-up) are skipped.
  339. $fr = $null; $budget = [System.Collections.Generic.List[hashtable]]::new(); $irqAt = $null
  340. $rebuilds = [System.Collections.Generic.List[int]]::new(); $abandoned = 0; $rbOpen = $null
  341. foreach ($e in $timeline) {
  342. switch ($e[0]) {
  343. 'start' { if ($fr) { $fr.period = $e[1] - $fr.start }; $fr = @{ start = $e[1]; irq = 0 }; $budget.Add($fr) }
  344. 'view' { if ($fr) { $fr.vblank = $e[1] - $fr.start; $fr.viewLine = $e[2] } }
  345. 'done' { if ($fr) { $fr.busy = $e[1] - $fr.start; $fr.doneLine = $e[2] } }
  346. 'irq' { $irqAt = $e[1] }
  347. 'irqexit' { if ($fr -and $null -ne $irqAt) { $fr.irq += $e[1] - $irqAt + 7 + 22 }; $irqAt = $null } # + interrupt entry, + exit code
  348. 'rbstart' { if ($fr) { if ($null -ne $rbOpen) { $abandoned++ }; $rbOpen = $budget.Count } }
  349. 'rbready' { if ($fr -and $null -ne $rbOpen) { $rebuilds.Add($budget.Count - $rbOpen + 1); $rbOpen = $null } }
  350. }
  351. }
  352. $full = @($budget | Where-Object { $null -ne $_.period -and $null -ne $_.busy -and $null -ne $_.vblank })
  353. if ($full.Count -lt 100) { $failures.Add("only $($full.Count) frames with complete timing") }
  354. $periods = $full | ForEach-Object { $_.period } | Measure-Object -Minimum -Maximum
  355. $stat = { param($key) $v = @($full | ForEach-Object { $_[$key] }); @{ max = ($v | Measure-Object -Maximum).Maximum; avg = [int](($v | Measure-Object -Average).Average) } }
  356. $sv = & $stat 'vblank'; $sb = & $stat 'busy'; $si = & $stat 'irq'
  357. $idle = @($full | ForEach-Object { $_.period - $_.busy }); $minIdle = ($idle | Measure-Object -Minimum).Minimum
  358. $maxDone = ($full | ForEach-Object { $_.doneLine } | Measure-Object -Maximum).Maximum
  359. $rbMax = if ($rebuilds.Count) { ($rebuilds | Measure-Object -Maximum).Maximum } else { 0 }
  360. $rbAvg = if ($rebuilds.Count) { [Math]::Round(($rebuilds | Measure-Object -Average).Average, 1) } else { 0 }
  361. $rbHist = ($rebuilds | Group-Object | Sort-Object { [int]$_.Name } | ForEach-Object { "$($_.Name):$($_.Count)" }) -join ' '
  362. $budgetText = ("budget: frame start to frame start {12}-{13} cycles; vblank work max {0} avg {1}; main loop busy max {2} avg {3}, idle min {4}, done by line {5}; " +
  363. "IRQ max {6} avg {7}; {8} rebuilds, frames each (frames:count) {14}, {11} abandoned") -f $sv.max, $sv.avg, $sb.max, $sb.avg, $minIdle, $maxDone, $si.max, $si.avg,
  364. $rebuilds.Count, $rbMax, $rbAvg, $abandoned, $periods.Minimum, $periods.Maximum, $rbHist
  365. # Final frames: at people_moved, the buffer that will be shown (the hidden one when a flip is
  366. # pending, otherwise the shown one) must equal a fresh render at the view origin plus this
  367. # frame's dots; with a flip pending, the shown buffer must hold the previous frame's dots.
  368. $world = [IO.File]::ReadAllBytes((Join-Path $dir 'world.bin'))
  369. $last = $frames[$frames.Count - 1]
  370. $checkBuffer = {
  371. param([string]$prefix, [int]$buf, $at, $people, [string]$what)
  372. $suffix = @('a', 'b')[$buf]
  373. $bitmap = [IO.File]::ReadAllBytes((Join-Path $dir "${prefix}bitmap-$suffix.bin"))
  374. $screen = [IO.File]::ReadAllBytes((Join-Path $dir "${prefix}screen-$suffix.bin"))
  375. $dots = @{}
  376. for ($i = 0; $i -lt 16; $i++) {
  377. $col = $people[$i] - $at.cx; $row = $people[32 + $i] - $at.cy
  378. if ($col -ge 0 -and $col -lt 39 -and $row -ge 0 -and $row -lt 21) {
  379. $o = $row * 320 + $col * 8 + $people[48 + $i]
  380. $dots[$o] = [int]$dots[$o] -bor (0xC0 -shr (2 * $people[16 + $i]))
  381. }
  382. }
  383. $badBitmap = 0; $badScreen = 0
  384. # Only cells inside the world: at the bottom edge row 21, at the right edge (view cell 42) the
  385. # 39th column lie outside it; neither is ever visible there (fine scroll is 0 at those edges).
  386. $rows = [Math]::Min(21, 50 - $at.cy); $cols = [Math]::Min(39, 80 - $at.cx)
  387. for ($r = 0; $r -lt $rows; $r++) {
  388. for ($c = 0; $c -lt $cols; $c++) {
  389. $t = $world[($at.cy + $r) * 80 + $at.cx + $c]
  390. if ($screen[$r * 40 + $c] -ne $tiles.colors[$t]) { $badScreen++ }
  391. for ($k = 0; $k -lt 8; $k++) {
  392. $o = $r * 320 + $c * 8 + $k
  393. if ($bitmap[$o] -ne ($tiles.patterns[$t][$k] -bor [int]$dots[$o])) { $badBitmap++ }
  394. }
  395. }
  396. }
  397. if ($badBitmap -gt 0 -or $badScreen -gt 0) { $failures.Add("${prefix}buffer $suffix ($what) at view cell ($($at.cx),$($at.cy)) differs from the world plus dots: $badBitmap bitmap bytes, $badScreen colour bytes") }
  398. }
  399. $checkFrame = {
  400. param([string]$prefix, [int]$i)
  401. $at = $frames[$i]; $prev = $frames[$i - 1]
  402. if ($at.pending) {
  403. & $checkBuffer $prefix (1 - $at.visible) $at $at.people 'shown next frame'
  404. if ($prev.cx -eq $at.cx -and $prev.cy -eq $at.cy) { & $checkBuffer $prefix $at.visible $at $prev.people 'shown, previous dots' }
  405. } else {
  406. & $checkBuffer $prefix $at.visible $at $at.people 'shown'
  407. }
  408. }
  409. & $checkFrame '' ($frames.Count - 1)
  410. # Snapshots: saved in the hit that ended stop k, which is logged as the first frame of phase k+1.
  411. $snaps = 0
  412. for ($ph = 0; $ph -lt $lastPhase; $ph++) {
  413. if (-not $sc.phases[$ph].snap) { continue }
  414. $i = -1
  415. for ($j = 1; $j -lt $frames.Count; $j++) { if ($frames[$j].phase -eq $ph + 1 -and $frames[$j - 1].phase -eq $ph) { $i = $j; break } }
  416. if ($i -lt 0 -or -not (Test-Path -LiteralPath (Join-Path $dir "snap$ph-bitmap-a.bin"))) { $failures.Add("stop $ph was not reached or not saved"); continue }
  417. & $checkFrame "snap$ph-" $i
  418. $snaps++
  419. }
  420. $perPhase = if ($phaseFrames.Count -le 12) { $phaseFrames -join '/' } else { "$($phaseFrames.Count) phases" }
  421. $snapText = if ($snaps) { ", $snaps stops checked byte for byte" } else { '' }
  422. $summary = "{0}: {1} frames ({9}), {2} moving, {3} stall frames, update done by line {4}, final view ({5},{6}) cell ({7},{8}){10}" -f `
  423. $sc.name, $frames.Count, $steps, $stalls, $lastLine, $last.vx, $last.vy, $last.cx, $last.cy, $perPhase, $snapText
  424. if ($failures.Count -gt 0) {
  425. $failedScenarios++
  426. Write-Host " FAIL $summary" -ForegroundColor Red
  427. $failures | Select-Object -First 8 | ForEach-Object { Write-Host " $_" }
  428. } else {
  429. Write-Host " ok $summary"
  430. }
  431. Write-Host " $budgetText"
  432. }
  433. if ($failedScenarios -gt 0) { Write-Host "[FAIL] $failedScenarios of $($scenarios.Count) scroll scenarios" -ForegroundColor Red; exit 1 }
  434. Write-Host "[PASS] all $($scenarios.Count) scroll scenarios: smooth one-pixel steps, edges respected, shown buffer matches the world" -ForegroundColor Green
  435. exit 0

Powered by TurnKey Linux.