Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

511 rindas
34KB

  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') { $L[$s] = Get-Label $target $s }
  120. $CHANGE_SLOTS = 8 # src/paths.inc
  121. foreach ($s in 'chg_count', 'chg_done', 'path_changed') { $L[$s] = Get-Label $target $s }
  122. $IRQ_ENTRY = 0xFF48; $IRQ_EXIT = 0xEA81 # KERNAL IRQ entry and the exit our handler jumps to
  123. $FRAME_CYCLES = 17095
  124. # Key bits (active low): cursor column RIGHT = bit 2, DOWN = bit 7; left SHIFT = bit 7.
  125. $RIGHT_DOWN = '7b'; $DOWN = '7f'; $RIGHTK = 'fb'; $NONE = 'ff'; $SHIFT = '7f'
  126. $PHASE = 0xCFF0 # free RAM (the program ends below $2700)
  127. # Phase end conditions: frame = move_frame value (wraps every 256 frames); view = cell and fine.
  128. function Frame([int]$n) { , (, @($L.move_frame, $n)) }
  129. function View([int]$cx, [int]$fx, [int]$cy, [int]$fy) {
  130. $c = @()
  131. if ($cx -ge 0) { $c += , @($L.view_cx, $cx); $c += , @($L.view_fx, $fx) }
  132. if ($cy -ge 0) { $c += , @($L.view_cy, $cy); $c += , @($L.view_fy, $fy) }
  133. $c
  134. }
  135. $scenarios = @(
  136. @{ name = 'edges'; cycles = 36000000; phases = @(
  137. @{ cursor = $NONE; lshift = $NONE; until = (Frame 30) }
  138. @{ cursor = $RIGHT_DOWN; lshift = $NONE; until = (View 42 0 30 4) } # vx 336, vy 244
  139. @{ cursor = $RIGHT_DOWN; lshift = $NONE; until = (Frame 0); edge = $true }
  140. @{ cursor = $RIGHT_DOWN; lshift = $SHIFT; until = (View 0 0 0 0) }
  141. @{ cursor = $RIGHT_DOWN; lshift = $SHIFT; until = (Frame 128); edge = $true }
  142. @{ cursor = $NONE; lshift = $NONE }) }
  143. @{ name = 'middle'; cycles = 18000000; phases = @(
  144. @{ cursor = $NONE; lshift = $NONE; until = (Frame 30) }
  145. @{ cursor = $DOWN; lshift = $NONE; until = (View -1 0 12 3) } # vy 99
  146. @{ cursor = $RIGHTK; lshift = $NONE; until = (View 16 5 -1 0) } # vx 133
  147. @{ cursor = $NONE; lshift = $NONE }) }
  148. @{ name = 'reverse'; cycles = 18000000; phases = @(
  149. @{ cursor = $NONE; lshift = $NONE; until = (Frame 30) }
  150. @{ cursor = $RIGHTK; lshift = $NONE; until = (View 9 0 -1 0) } # just stepped into cell 9
  151. @{ cursor = $RIGHTK; lshift = $SHIFT; until = (View 3 2 -1 0) }
  152. @{ cursor = $RIGHTK; lshift = $NONE; until = (View 5 0 -1 0) }
  153. @{ cursor = $DOWN; lshift = $NONE; until = (View -1 0 6 0) } # just stepped into row 6
  154. @{ cursor = $DOWN; lshift = $SHIFT; until = (View -1 0 2 1) }
  155. @{ cursor = $NONE; lshift = $NONE }) }
  156. @{ name = 'restart'; cycles = 30000000; phases = @(
  157. @{ cursor = $NONE; lshift = $NONE; until = (Frame 30) }
  158. @{ cursor = $RIGHTK; lshift = $NONE; until = (View 10 0 -1 0) } # stop on a cell edge
  159. @{ cursor = $NONE; lshift = $NONE; until = (Frame 160) }
  160. @{ cursor = $RIGHTK; lshift = $SHIFT; until = (View 8 0 -1 0) } # left from rest at fx 0
  161. @{ cursor = $NONE; lshift = $NONE; until = (Frame 250) }
  162. @{ cursor = $RIGHTK; lshift = $NONE; until = (View 10 0 -1 0) } # right from rest at fx 0
  163. @{ cursor = $DOWN; lshift = $NONE; until = (View -1 0 6 0) }
  164. @{ cursor = $NONE; lshift = $NONE; until = (Frame 120) }
  165. @{ cursor = $DOWN; lshift = $SHIFT; until = (View -1 0 4 0) } # up from rest at fy 0
  166. @{ cursor = $NONE; lshift = $NONE; until = (Frame 220) }
  167. @{ cursor = $DOWN; lshift = $NONE; until = (View -1 0 6 0) } # down from rest at fy 0
  168. @{ cursor = $NONE; lshift = $NONE }) }
  169. )
  170. if ($Long) {
  171. $R = @{ cursor = $RIGHTK; lshift = $NONE }; $Lf = @{ cursor = $RIGHTK; lshift = $SHIFT }
  172. $D = @{ cursor = $DOWN; lshift = $NONE }; $U = @{ cursor = $DOWN; lshift = $SHIFT }
  173. $UL = @{ cursor = $RIGHT_DOWN; lshift = $SHIFT }
  174. $tour = [System.Collections.Generic.List[hashtable]]::new()
  175. $leg = { param($keys, $until) $tour.Add(@{ cursor = $keys.cursor; lshift = $keys.lshift; until = $until }) }
  176. $stop = { $tour.Add(@{ cursor = $NONE; lshift = $NONE; until = (Frame ((61 * $tour.Count + 17) % 256)); snap = $true }) }
  177. $tour.Add(@{ cursor = $NONE; lshift = $NONE; until = (Frame 30) })
  178. for ($lap = 0; $lap -lt 6; $lap++) {
  179. if ($lap % 2 -eq 0) { # rows: ends at the top-left
  180. foreach ($row in 5, 10, 15, 20, 25) {
  181. $right = (($row / 5) % 2) -eq 1
  182. if ($right) { & $leg $R (View 42 0 -1 0) } else { & $leg $Lf (View 0 0 -1 0) }
  183. & $leg $D (View -1 0 $row 0)
  184. if ($row % 10 -eq 0) { & $stop }
  185. }
  186. & $leg $Lf (View 0 0 -1 0); & $leg $D (View -1 0 30 4); & $leg $R (View 42 0 -1 0); & $stop
  187. & $leg $UL (View 0 0 0 0); & $stop
  188. } else { # columns: ends at the top-left
  189. foreach ($col in 8, 16, 24, 32, 42) {
  190. $down = (($col -eq 8) -or ($col -eq 24) -or ($col -eq 42))
  191. if ($down) { & $leg $D (View -1 0 30 4) } else { & $leg $U (View -1 0 0 0) }
  192. & $leg $R (View $col 0 -1 0)
  193. if ($col -eq 16 -or $col -eq 42) { & $stop }
  194. }
  195. & $leg $U (View -1 0 0 0); & $leg $Lf (View 0 0 -1 0); & $stop
  196. }
  197. }
  198. $tour.Add(@{ cursor = $NONE; lshift = $NONE })
  199. $scenarios += @{ name = 'tour'; cycles = 400000000; timeout = 3600; phases = $tour.ToArray(); maxStalls = 6 * $tour.Count }
  200. }
  201. $root = Get-RepoPath 'build/check-scroll'
  202. if ((Test-Path -LiteralPath $root) -and -not $ParseOnly) { Remove-Item -LiteralPath $root -Recurse -Force }
  203. $failedScenarios = 0
  204. foreach ($sc in $scenarios) {
  205. $dir = Join-Path $root $sc.name
  206. if (-not $ParseOnly) { New-Item -ItemType Directory -Force -Path $dir | Out-Null }
  207. $mon = [System.Collections.Generic.List[string]]::new()
  208. $mon.Add('logname "monitor.log"'); $mon.Add('log on')
  209. $n = 0
  210. $add = { param($addr, $cmd, $cond)
  211. $script:n++
  212. $line = 'trace exec ${0:x4}' -f $addr
  213. if ($cond) { $line += ' if ' + (($cond | ForEach-Object { '(@cpu:${0:x4} == ${1:x2})' -f $_[0], $_[1] }) -join ' && ') }
  214. $mon.Add($line); $mon.Add(('command {0} "{1}"' -f $script:n, $cmd))
  215. }
  216. $lastPhase = $sc.phases.Count - 1
  217. & $add $L.smoke_ready ('bsave \"world.bin\" 0 {0:x4} {1:x4}' -f $L.world_tiles, ($L.world_tiles + 3999)) $null
  218. & $add $L.smoke_ready ('> {0:x4} 00' -f $PHASE) $null
  219. foreach ($row in 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space') { & $add $L[$row] 'r a = ff' $null }
  220. for ($ph = 0; $ph -le $lastPhase; $ph++) { # later checkpoints at one address run after earlier ones
  221. $k = $sc.phases[$ph]
  222. if ($k.cursor -ne $NONE) { & $add $L.key_row_cursor "r a = $($k.cursor)" @(, @($PHASE, $ph)) }
  223. if ($k.lshift -ne $NONE) { & $add $L.key_row_lshift "r a = $($k.lshift)" @(, @($PHASE, $ph)) }
  224. }
  225. # Snapshots at the end of each "snap" stop: before the phase changes, so they run in the same hit.
  226. $saves = @('bitmap-a.bin 6000 7a3f', 'bitmap-b.bin a000 ba3f', 'screen-a.bin 4000 4347', 'screen-b.bin 8000 8347')
  227. # With them, the world and change queue (chg_count .. chg_done) as they were at the save.
  228. $saves += ('world-now.bin {0:x4} {1:x4}' -f $L.world_tiles, ($L.world_tiles + 3999))
  229. $saves += ('queue.bin {0:x4} {1:x4}' -f $L.chg_count, ($L.chg_done + $CHANGE_SLOTS - 1))
  230. for ($ph = 0; $ph -lt $lastPhase; $ph++) {
  231. if (-not $sc.phases[$ph].snap) { continue }
  232. foreach ($save in $saves) {
  233. $f = $save -split ' '
  234. & $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)
  235. }
  236. }
  237. for ($ph = $lastPhase - 1; $ph -ge 0; $ph--) { # descending, so one hit moves on at most one phase
  238. & $add $L.people_moved ('> {0:x4} {1:x2}' -f $PHASE, ($ph + 1)) (@(, @($PHASE, $ph)) + $sc.phases[$ph].until)
  239. }
  240. $viewCheckpoint = $n + 1
  241. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.view_cx, ($L.view_cx + 3)) $null
  242. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.map_d011, ($L.map_d016)) $null
  243. & $add $L.people_moved ('m {0:x4} {0:x4}' -f $L.visible) $null
  244. & $add $L.people_moved ('m {0:x4} {0:x4}' -f $L.people_flip_pending) $null
  245. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.buf_cx, ($L.buf_cx + 1)) $null
  246. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.buf_cy, ($L.buf_cy + 1)) $null
  247. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.buf_ready, ($L.buf_ready + 1)) $null
  248. & $add $L.people_moved ('m {0:x4} {0:x4}' -f $PHASE) $null
  249. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.people_cx, ($L.people_cx + 79)) $null
  250. foreach ($save in $saves) {
  251. $f = $save -split ' '
  252. & $add $L.people_moved ('bsave \"{0}\" 0 {1} {2}' -f $f[0], $f[1], $f[2]) @(, @($PHASE, $lastPhase))
  253. }
  254. # Stores into the shown buffer's map (screen RAM rows 0-20, bitmap) while the map is displayed
  255. # (raster 55-210) would tear the picture. Armed at smoke_ready, so start-up is not counted.
  256. $firstStore = $n + 1
  257. foreach ($range in @(@(0, 0x4000, 0x4347), @(0, 0x6000, 0x7a3f), @(1, 0x8000, 0x8347), @(1, 0xa000, 0xba3f))) {
  258. $n++
  259. $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]))
  260. $mon.Add("disable $n")
  261. }
  262. $lastStore = $n
  263. for ($c = $firstStore; $c -le $lastStore; $c++) { & $add $L.smoke_ready "enable $c" $null }
  264. # Timing events: a trace without a command prints the raster position and the CPU clock.
  265. $events = @{}
  266. foreach ($ev in @(@('start', $L.frame_start), @('done', $L.frame_done), @('irq', $IRQ_ENTRY), @('irqexit', $IRQ_EXIT),
  267. @('rbstart', $L.start_rebuild), @('rbready', $L.rebuild_ready))) {
  268. $n++; $mon.Add(('trace exec ${0:x4}' -f $ev[1])); $events[$n] = $ev[0]
  269. }
  270. $n++; $mon.Add(('trace exec ${0:x4}' -f $L.path_changed)); $events[$n] = 'path' # A = new tile
  271. $pathAppeared = 0; $pathFaded = 0; $script:pathCells = 0; $script:pendingSkipped = 0
  272. if (-not $ParseOnly) {
  273. Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $mon
  274. Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
  275. $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target)
  276. $vargs += @('-limitcycles', [string]$sc.cycles, '-exitscreenshot', 'screen.png', '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target))
  277. $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } }
  278. $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru `
  279. -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err')
  280. $limit = if ($sc.timeout) { [Math]::Max($sc.timeout, $TimeoutSeconds) } else { $TimeoutSeconds }
  281. if (-not $p.WaitForExit($limit * 1000)) { $p.Kill($true) }
  282. }
  283. # Parse frames: view (4 bytes), d011/d016, visible, people table; clock and raster line.
  284. $frames = [System.Collections.Generic.List[hashtable]]::new()
  285. $timeline = [System.Collections.Generic.List[object]]::new() # (kind, clock, line)
  286. $cur = $null; $want = $null; $wantLine = 0
  287. foreach ($line in [IO.File]::ReadLines((Join-Path $dir 'monitor.log'), [Text.Encoding]::Latin1)) {
  288. if ($line.StartsWith('#') -and $line -match '^#(\d+) \(Trace\s+exec [0-9a-fA-F]+\)\s+(\d+)/') {
  289. $num = [int]$Matches[1]
  290. if ($num -eq $viewCheckpoint) {
  291. $cur = @{ line = [int]$Matches[2]; people = [System.Collections.Generic.List[byte]]::new() }; $frames.Add($cur); $want = 'view'; continue
  292. }
  293. if ($events.ContainsKey($num)) { $want = $events[$num]; $wantLine = [int]$Matches[2] }
  294. continue
  295. }
  296. if ($want -eq 'path' -and $line -match '^\.C:.*\sA:([0-9a-fA-F]{2})\s') {
  297. if ([Convert]::ToInt32($Matches[1], 16) -ge $tiles.pathLand) { $pathAppeared++ } else { $pathFaded++ }
  298. $want = $null; continue
  299. }
  300. if ($want -and $line -match '^\.C:.*\s(\d+)\s*$') {
  301. if ($want -eq 'view') { $cur.clock = [long]$Matches[1]; $timeline.Add(@('view', $cur.clock, $cur.line)) }
  302. else { $timeline.Add(@($want, [long]$Matches[1], $wantLine)) }
  303. $want = $null; continue
  304. }
  305. if ($null -eq $cur -or $line -notmatch '^>C:([0-9a-fA-F]{4}) ((?:[0-9a-fA-F]{2} )+)') { continue }
  306. $addr = [Convert]::ToInt32($Matches[1], 16)
  307. $bytes = @($Matches[2].Trim() -split ' ' | ForEach-Object { [Convert]::ToByte($_, 16) })
  308. if ($addr -eq $L.view_cx) { $cur.cx = $bytes[0]; $cur.fx = $bytes[1]; $cur.cy = $bytes[2]; $cur.fy = $bytes[3] }
  309. elseif ($addr -eq $L.map_d011) { $cur.d011 = $bytes[0]; $cur.d016 = $bytes[1] }
  310. elseif ($addr -eq $L.visible) { $cur.visible = $bytes[0] }
  311. elseif ($addr -eq $PHASE) { $cur.phase = $bytes[0] }
  312. elseif ($addr -eq $L.people_flip_pending) { $cur.pending = $bytes[0] }
  313. elseif ($addr -eq $L.buf_cx) { $cur.bufcx = $bytes[0..1] }
  314. elseif ($addr -eq $L.buf_cy) { $cur.bufcy = $bytes[0..1] }
  315. elseif ($addr -eq $L.buf_ready) { $cur.ready = $bytes[0..1] }
  316. 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) } } }
  317. }
  318. # The run stops at a cycle limit, possibly while the last frame was being logged.
  319. $fields = 'cx', 'd011', 'visible', 'pending', 'bufcx', 'bufcy', 'ready', 'phase', 'clock'
  320. while ($frames.Count -gt 0) {
  321. $lf = $frames[$frames.Count - 1]
  322. if ($lf.people.Count -eq 80 -and -not @($fields | Where-Object { $null -eq $lf[$_] })) { break }
  323. $frames.RemoveAt($frames.Count - 1)
  324. }
  325. $failures = [System.Collections.Generic.List[string]]::new()
  326. # A rejected checkpoint shifts the numbers of the ones after it, so commands would land on the wrong ones.
  327. $monErrors = @(Select-String -LiteralPath (Join-Path $dir 'monitor.log') -Pattern '^ERROR|not a valid checkpoint' | Select-Object -First 3)
  328. foreach ($e in $monErrors) { $failures.Add("monitor script error: $($e.Line.Trim())") }
  329. if ($frames.Count -lt 100) { $failures.Add("only $($frames.Count) frames logged") }
  330. $tears = @(Select-String -LiteralPath (Join-Path $dir 'monitor.log') -Pattern '^#(\d+) \(Trace\s+store ([0-9a-fA-F]{4})\)\s+(\d+)/' |
  331. Where-Object { [int]$_.Matches[0].Groups[1].Value -ge $firstStore -and [int]$_.Matches[0].Groups[1].Value -lt $firstStore + 4 })
  332. if ($tears.Count -gt 0) {
  333. $t = $tears[0].Matches[0].Groups
  334. $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))")
  335. }
  336. $stalls = 0; $steps = 0; $late = 0; $lastLine = 0
  337. $maxVx = 0; $maxVy = 0; $minAfterMaxVx = 999; $minAfterMaxVy = 999
  338. $phaseFrames = @(0) * $sc.phases.Count
  339. for ($i = 0; $i -lt $frames.Count; $i++) {
  340. $f = $frames[$i]
  341. $vx = 8 * $f.cx + $f.fx; $vy = 8 * $f.cy + $f.fy
  342. $f.vx = $vx; $f.vy = $vy
  343. if ($f.phase -lt $sc.phases.Count) { $phaseFrames[$f.phase]++ } else { $failures.Add("frame ${i}: phase $($f.phase) out of range") }
  344. 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)") }
  345. 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)) }
  346. $v = $f.visible
  347. if (-not $f.ready[$v] -or $f.bufcx[$v] -ne $f.cx -or $f.bufcy[$v] -ne $f.cy) {
  348. $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))")
  349. }
  350. if ($f.line -ge 55 -and $f.line -lt 251) { $failures.Add("frame ${i}: people update ended at raster line $($f.line)") }
  351. elseif ($f.line -lt 55) { $lastLine = [Math]::Max($lastLine, $f.line) }
  352. if ($i -gt 0) {
  353. $p = $frames[$i - 1]
  354. $dx = $vx - $p.vx; $dy = $vy - $p.vy
  355. if ($dx -ne 0 -or $dy -ne 0) { $steps++ }
  356. 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++ }
  357. # The keys read after people_moved in frame i-1 (that frame's phase) give frame i's step.
  358. $k = $sc.phases[[Math]::Min($p.phase, $lastPhase)]
  359. $bits = [Convert]::ToByte($k.cursor, 16)
  360. $sign = if ($k.lshift -eq $SHIFT) { -1 } else { 1 }
  361. $wantX = if (($bits -band 0x04) -eq 0) { $sign } else { 0 }
  362. $wantY = if (($bits -band 0x80) -eq 0) { $sign } else { 0 }
  363. if (($dx -ne 0 -and $dx -ne $wantX) -or ($dy -ne 0 -and $dy -ne $wantY)) {
  364. $failures.Add("frame ${i}: view moved by ($dx,$dy) with keys asking for ($wantX,$wantY)")
  365. }
  366. 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") }
  367. $canX = ($wantX -gt 0 -and $p.vx -lt 336) -or ($wantX -lt 0 -and $p.vx -gt 0)
  368. $canY = ($wantY -gt 0 -and $p.vy -lt 244) -or ($wantY -lt 0 -and $p.vy -gt 0)
  369. if (($canX -or $canY) -and $dx -eq 0 -and $dy -eq 0) { $stalls++ }
  370. }
  371. if ($vx -gt $maxVx) { $maxVx = $vx; $minAfterMaxVx = $vx }
  372. if ($vy -gt $maxVy) { $maxVy = $vy; $minAfterMaxVy = $vy }
  373. if ($vx -lt $minAfterMaxVx) { $minAfterMaxVx = $vx }
  374. if ($vy -lt $minAfterMaxVy) { $minAfterMaxVy = $vy }
  375. }
  376. if ($late -gt 0) { $failures.Add("the main loop missed $late frame(s), $firstLate") }
  377. if ($phaseFrames[$lastPhase] -lt 10) { $failures.Add("reached the last phase for only $($phaseFrames[$lastPhase]) frame(s) (frames per phase: $($phaseFrames -join ', '))") }
  378. if ($sc.name -eq 'edges') {
  379. if ($maxVx -ne 336 -or $maxVy -ne 244) { $failures.Add("did not reach the bottom-right edge (max vx $maxVx, vy $maxVy)") }
  380. if ($minAfterMaxVx -ne 0 -or $minAfterMaxVy -ne 0) { $failures.Add("did not return to the top-left edge (min vx $minAfterMaxVx, vy $minAfterMaxVy)") }
  381. }
  382. $allowedStalls = if ($sc.maxStalls) { $sc.maxStalls } else { $MaxStallFrames }
  383. if ($stalls -gt $allowedStalls) { $failures.Add("$stalls stall frames (allowed $allowedStalls)") }
  384. # Frame budget from the timeline. Events before the first frame_start (start-up) are skipped.
  385. $fr = $null; $budget = [System.Collections.Generic.List[hashtable]]::new(); $irqAt = $null
  386. $rebuilds = [System.Collections.Generic.List[int]]::new(); $abandoned = 0; $rbOpen = $null
  387. foreach ($e in $timeline) {
  388. switch ($e[0]) {
  389. 'start' { if ($fr) { $fr.period = $e[1] - $fr.start }; $fr = @{ start = $e[1]; irq = 0 }; $budget.Add($fr) }
  390. 'view' { if ($fr) { $fr.vblank = $e[1] - $fr.start; $fr.viewLine = $e[2] } }
  391. 'done' { if ($fr) { $fr.busy = $e[1] - $fr.start; $fr.doneLine = $e[2] } }
  392. 'irq' { $irqAt = $e[1] }
  393. 'irqexit' { if ($fr -and $null -ne $irqAt) { $fr.irq += $e[1] - $irqAt + 7 + 22 }; $irqAt = $null } # + interrupt entry, + exit code
  394. 'rbstart' { if ($fr) { if ($null -ne $rbOpen) { $abandoned++ }; $rbOpen = $budget.Count } }
  395. 'rbready' { if ($fr -and $null -ne $rbOpen) { $rebuilds.Add($budget.Count - $rbOpen + 1); $rbOpen = $null } }
  396. }
  397. }
  398. $full = @($budget | Where-Object { $null -ne $_.period -and $null -ne $_.busy -and $null -ne $_.vblank })
  399. if ($full.Count -lt 100) { $failures.Add("only $($full.Count) frames with complete timing") }
  400. $periods = $full | ForEach-Object { $_.period } | Measure-Object -Minimum -Maximum
  401. $stat = { param($key) $v = @($full | ForEach-Object { $_[$key] }); @{ max = ($v | Measure-Object -Maximum).Maximum; avg = [int](($v | Measure-Object -Average).Average) } }
  402. $sv = & $stat 'vblank'; $sb = & $stat 'busy'; $si = & $stat 'irq'
  403. $idle = @($full | ForEach-Object { $_.period - $_.busy }); $minIdle = ($idle | Measure-Object -Minimum).Minimum
  404. $maxDone = ($full | ForEach-Object { $_.doneLine } | Measure-Object -Maximum).Maximum
  405. $rbMax = if ($rebuilds.Count) { ($rebuilds | Measure-Object -Maximum).Maximum } else { 0 }
  406. $rbAvg = if ($rebuilds.Count) { [Math]::Round(($rebuilds | Measure-Object -Average).Average, 1) } else { 0 }
  407. $rbHist = ($rebuilds | Group-Object | Sort-Object { [int]$_.Name } | ForEach-Object { "$($_.Name):$($_.Count)" }) -join ' '
  408. $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}; " +
  409. "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,
  410. $rebuilds.Count, $rbMax, $rbAvg, $abandoned, $periods.Minimum, $periods.Maximum, $rbHist
  411. # Final frames: at people_moved, the buffer that will be shown (the hidden one when a flip is
  412. # pending, otherwise the shown one) must equal a fresh render at the view origin plus this
  413. # frame's dots; with a flip pending, the shown buffer must hold the previous frame's dots.
  414. $last = $frames[$frames.Count - 1]
  415. $checkBuffer = {
  416. param([string]$prefix, [int]$buf, $at, $people, [string]$what)
  417. $suffix = @('a', 'b')[$buf]
  418. $bitmap = [IO.File]::ReadAllBytes((Join-Path $dir "${prefix}bitmap-$suffix.bin"))
  419. # The world as it was at the save; cells still queued for this buffer are skipped.
  420. $now = [IO.File]::ReadAllBytes((Join-Path $dir "${prefix}world-now.bin"))
  421. $q = [IO.File]::ReadAllBytes((Join-Path $dir "${prefix}queue.bin"))
  422. $pending = @{} # queue: chg_count, new_cell, chg_tile, chg_ty, chg_cx[8], chg_cy[8], chg_done[8]
  423. for ($i = 0; $i -lt [Math]::Min($q[0], $CHANGE_SLOTS); $i++) {
  424. if (($q[4 + 2 * $CHANGE_SLOTS + $i] -band (1 -shl $buf)) -eq 0) { $pending["$($q[4 + $i]),$($q[4 + $CHANGE_SLOTS + $i])"] = $true }
  425. }
  426. $script:pathCells += @($now | Where-Object { $_ -ge $tiles.pathLand -and $_ -le $tiles.pathLand + 2 }).Count
  427. $script:pendingSkipped += $pending.Count
  428. $screen = [IO.File]::ReadAllBytes((Join-Path $dir "${prefix}screen-$suffix.bin"))
  429. $dots = @{}
  430. for ($i = 0; $i -lt 16; $i++) {
  431. $col = $people[$i] - $at.cx; $row = $people[32 + $i] - $at.cy
  432. if ($col -ge 0 -and $col -lt 39 -and $row -ge 0 -and $row -lt 21) {
  433. $o = $row * 320 + $col * 8 + $people[48 + $i]
  434. $dots[$o] = [int]$dots[$o] -bor (0xC0 -shr (2 * $people[16 + $i]))
  435. }
  436. }
  437. $badBitmap = 0; $badScreen = 0
  438. # Only cells inside the world: at the bottom edge row 21, at the right edge (view cell 42) the
  439. # 39th column lie outside it; neither is ever visible there (fine scroll is 0 at those edges).
  440. $rows = [Math]::Min(21, 50 - $at.cy); $cols = [Math]::Min(39, 80 - $at.cx)
  441. for ($r = 0; $r -lt $rows; $r++) {
  442. for ($c = 0; $c -lt $cols; $c++) {
  443. if ($pending.Count -and $pending["$($at.cx + $c),$($at.cy + $r)"]) { continue }
  444. $t = $now[($at.cy + $r) * 80 + $at.cx + $c]
  445. if ($screen[$r * 40 + $c] -ne $tiles.colors[$t]) { $badScreen++ }
  446. for ($k = 0; $k -lt 8; $k++) {
  447. $o = $r * 320 + $c * 8 + $k
  448. if ($bitmap[$o] -ne ($tiles.patterns[$t][$k] -bor [int]$dots[$o])) { $badBitmap++ }
  449. }
  450. }
  451. }
  452. 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") }
  453. }
  454. $checkFrame = {
  455. param([string]$prefix, [int]$i)
  456. $at = $frames[$i]; $prev = $frames[$i - 1]
  457. if ($at.pending) {
  458. & $checkBuffer $prefix (1 - $at.visible) $at $at.people 'shown next frame'
  459. if ($prev.cx -eq $at.cx -and $prev.cy -eq $at.cy) { & $checkBuffer $prefix $at.visible $at $prev.people 'shown, previous dots' }
  460. } else {
  461. & $checkBuffer $prefix $at.visible $at $at.people 'shown'
  462. }
  463. }
  464. 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') }
  465. # Snapshots: saved in the hit that ended stop k, which is logged as the first frame of phase k+1.
  466. $snaps = 0
  467. for ($ph = 0; $ph -lt $lastPhase; $ph++) {
  468. if (-not $sc.phases[$ph].snap) { continue }
  469. $i = -1
  470. 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 } }
  471. 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 }
  472. & $checkFrame "snap$ph-" $i
  473. $snaps++
  474. }
  475. $perPhase = if ($phaseFrames.Count -le 12) { $phaseFrames -join '/' } else { "$($phaseFrames.Count) phases" }
  476. $snapText = if ($snaps) { ", $snaps stops checked byte for byte" } else { '' }
  477. if ($PathTest -or $pathAppeared) {
  478. $snapText += ", paths appeared $pathAppeared and faded $pathFaded times, $($script:pathCells) path cells in checked saves ($($script:pendingSkipped) queued cells skipped)"
  479. if ($sc.name -eq 'tour' -and $pathAppeared -eq 0) { $failures.Add('no path appeared during the tour') }
  480. }
  481. $summary = "{0}: {1} frames ({9}), {2} moving, {3} stall frames, update done by line {4}, final view ({5},{6}) cell ({7},{8}){10}" -f `
  482. $sc.name, $frames.Count, $steps, $stalls, $lastLine, $last.vx, $last.vy, $last.cx, $last.cy, $perPhase, $snapText
  483. if ($failures.Count -gt 0) {
  484. $failedScenarios++
  485. Write-Host " FAIL $summary" -ForegroundColor Red
  486. $failures | Select-Object -First 8 | ForEach-Object { Write-Host " $_" }
  487. } else {
  488. Write-Host " ok $summary"
  489. }
  490. Write-Host " $budgetText"
  491. }
  492. if ($failedScenarios -gt 0) { Write-Host "[FAIL] $failedScenarios of $($scenarios.Count) scroll scenarios" -ForegroundColor Red; exit 1 }
  493. Write-Host "[PASS] all $($scenarios.Count) scroll scenarios: smooth one-pixel steps, edges respected, shown buffer matches the world" -ForegroundColor Green
  494. exit 0

Powered by TurnKey Linux.