Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

519 řádky
35KB

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

Powered by TurnKey Linux.