You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 line
18KB

  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. Checks, per scenario:
  19. - the view moves at most one pixel per axis per frame, only in the direction of the
  20. keys read the frame before, and stays inside
  21. vx 0-336, vy 0-244; the fine-scroll registers match the view
  22. - every scenario reaches its last phase; edges: the view reaches vx 336 and vy 244,
  23. then 0 and 0, and does not move while the keys push against an edge
  24. - the main loop never misses a frame and the people update ends before raster 55
  25. - frames where a held key did not move the view although no edge was reached are
  26. counted as stalls and reported (they happen while the hidden buffer is not ready)
  27. - at the end, the buffer about to be shown (bitmap and screen RAM) equals a fresh
  28. render of the world from the view's top-left cell (39 columns, 21 rows) with this
  29. frame's dots ORed in; when a flip is pending, the shown buffer has last frame's dots
  30. Exit codes: 0 = pass, 1 = fail, 2 = a tool is missing.
  31. .EXAMPLE
  32. pwsh -File tools/check-scroll.ps1
  33. #>
  34. [CmdletBinding()]
  35. param([switch]$NoBuild, [int]$TimeoutSeconds = 300, [int]$MaxStallFrames = 40)
  36. . (Join-Path $PSScriptRoot 'lib.ps1')
  37. function Get-Label([hashtable]$Target, [string]$Symbol) {
  38. foreach ($line in Get-Content -LiteralPath (Get-RepoPath $Target.build.labels)) {
  39. $parts = $line -split '\s+'
  40. if ($parts.Count -ge 3 -and $parts[2].TrimStart('.') -eq $Symbol) { return [Convert]::ToInt32($parts[1], 16) }
  41. }
  42. throw "Symbol '$Symbol' not found in $($Target.build.labels)."
  43. }
  44. # Tile data from src/tiles.inc: patterns in file order, colours as declared there.
  45. function Get-TileData {
  46. $src = Get-Content -LiteralPath (Get-RepoPath 'src/tiles.inc') -Raw
  47. $block = $src.Substring($src.IndexOf('tile_patterns:'))
  48. $block = $block.Substring(0, $block.IndexOf('.assert * - tile_patterns'))
  49. $patterns = @(foreach ($m in [regex]::Matches($block, '(?m)^\s*\.byte\s+([^;\r\n]+)')) {
  50. , @($m.Groups[1].Value -split ',' | ForEach-Object { [Convert]::ToByte($_.Trim().TrimStart('$'), 16) })
  51. })
  52. $colors = [System.Collections.Generic.List[byte]]::new()
  53. $cblock = $src.Substring($src.IndexOf('tile_colors:'))
  54. $cblock = $cblock.Substring(0, $cblock.IndexOf('.assert * - tile_colors'))
  55. $repeat = 1
  56. foreach ($line in $cblock -split "`n") {
  57. if ($line -match '\.repeat\s+(\d+)') { $repeat = [int]$Matches[1]; continue }
  58. if ($line -match '\.endrepeat') { $repeat = 1; continue }
  59. if ($line -match '\.byte\s+([^;\r\n]+)') {
  60. $vals = $Matches[1] -split ',' | ForEach-Object { [Convert]::ToByte($_.Trim().TrimStart('$'), 16) }
  61. for ($r = 0; $r -lt $repeat; $r++) { foreach ($v in $vals) { $colors.Add($v) } }
  62. }
  63. }
  64. @{ patterns = $patterns; colors = $colors.ToArray() }
  65. }
  66. try {
  67. $project = Get-ProjectConfig
  68. $target = Get-ProjectTargets $project 'c64'
  69. if (-not $NoBuild) { Invoke-TargetBuild $project $target @{} }
  70. $emu = Get-ToolPath $project $target.emulator.binary
  71. } catch [MissingToolException] {
  72. Write-Host $_.Exception.Message -ForegroundColor Yellow
  73. exit 2
  74. }
  75. $tiles = Get-TileData
  76. $L = @{}
  77. foreach ($s in 'smoke_ready', 'people_moved', 'world_tiles', 'people_cx', 'visible', 'view_cx', 'map_d011', 'map_d016',
  78. 'move_frame', 'people_flip_pending', 'view_fx', 'view_cy', 'view_fy',
  79. 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space') { $L[$s] = Get-Label $target $s }
  80. # Key bits (active low): cursor column RIGHT = bit 2, DOWN = bit 7; left SHIFT = bit 7.
  81. $RIGHT_DOWN = '7b'; $DOWN = '7f'; $RIGHTK = 'fb'; $NONE = 'ff'; $SHIFT = '7f'
  82. $PHASE = 0xCFF0 # free RAM (the program ends below $2700)
  83. # Phase end conditions: frame = move_frame value (wraps every 256 frames); view = cell and fine.
  84. function Frame([int]$n) { , (, @($L.move_frame, $n)) }
  85. function View([int]$cx, [int]$fx, [int]$cy, [int]$fy) {
  86. $c = @()
  87. if ($cx -ge 0) { $c += , @($L.view_cx, $cx); $c += , @($L.view_fx, $fx) }
  88. if ($cy -ge 0) { $c += , @($L.view_cy, $cy); $c += , @($L.view_fy, $fy) }
  89. $c
  90. }
  91. $scenarios = @(
  92. @{ name = 'edges'; cycles = 30000000; phases = @(
  93. @{ cursor = $NONE; lshift = $NONE; until = (Frame 30) }
  94. @{ cursor = $RIGHT_DOWN; lshift = $NONE; until = (View 42 0 30 4) } # vx 336, vy 244
  95. @{ cursor = $RIGHT_DOWN; lshift = $NONE; until = (Frame 0); edge = $true }
  96. @{ cursor = $RIGHT_DOWN; lshift = $SHIFT; until = (View 0 0 0 0) }
  97. @{ cursor = $RIGHT_DOWN; lshift = $SHIFT; until = (Frame 128); edge = $true }
  98. @{ cursor = $NONE; lshift = $NONE }) }
  99. @{ name = 'middle'; cycles = 12000000; phases = @(
  100. @{ cursor = $NONE; lshift = $NONE; until = (Frame 30) }
  101. @{ cursor = $DOWN; lshift = $NONE; until = (View -1 0 12 3) } # vy 99
  102. @{ cursor = $RIGHTK; lshift = $NONE; until = (View 16 5 -1 0) } # vx 133
  103. @{ cursor = $NONE; lshift = $NONE }) }
  104. @{ name = 'reverse'; cycles = 12000000; phases = @(
  105. @{ cursor = $NONE; lshift = $NONE; until = (Frame 30) }
  106. @{ cursor = $RIGHTK; lshift = $NONE; until = (View 9 0 -1 0) } # just stepped into cell 9
  107. @{ cursor = $RIGHTK; lshift = $SHIFT; until = (View 3 2 -1 0) }
  108. @{ cursor = $RIGHTK; lshift = $NONE; until = (View 5 0 -1 0) }
  109. @{ cursor = $DOWN; lshift = $NONE; until = (View -1 0 6 0) } # just stepped into row 6
  110. @{ cursor = $DOWN; lshift = $SHIFT; until = (View -1 0 2 1) }
  111. @{ cursor = $NONE; lshift = $NONE }) }
  112. )
  113. $root = Get-RepoPath 'build/check-scroll'
  114. if (Test-Path -LiteralPath $root) { Remove-Item -LiteralPath $root -Recurse -Force }
  115. $failedScenarios = 0
  116. foreach ($sc in $scenarios) {
  117. $dir = Join-Path $root $sc.name
  118. New-Item -ItemType Directory -Force -Path $dir | Out-Null
  119. $mon = [System.Collections.Generic.List[string]]::new()
  120. $mon.Add('logname "monitor.log"'); $mon.Add('log on')
  121. $n = 0
  122. $add = { param($addr, $cmd, $cond)
  123. $script:n++
  124. $line = 'trace exec ${0:x4}' -f $addr
  125. if ($cond) { $line += ' if ' + (($cond | ForEach-Object { '(@cpu:${0:x4} == ${1:x2})' -f $_[0], $_[1] }) -join ' && ') }
  126. $mon.Add($line); $mon.Add(('command {0} "{1}"' -f $script:n, $cmd))
  127. }
  128. $lastPhase = $sc.phases.Count - 1
  129. & $add $L.smoke_ready ('bsave \"world.bin\" 0 {0:x4} {1:x4}' -f $L.world_tiles, ($L.world_tiles + 3999)) $null
  130. & $add $L.smoke_ready ('> {0:x4} 00' -f $PHASE) $null
  131. foreach ($row in 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space') { & $add $L[$row] 'r a = ff' $null }
  132. for ($ph = 0; $ph -le $lastPhase; $ph++) { # later checkpoints at one address run after earlier ones
  133. $k = $sc.phases[$ph]
  134. if ($k.cursor -ne $NONE) { & $add $L.key_row_cursor "r a = $($k.cursor)" @(, @($PHASE, $ph)) }
  135. if ($k.lshift -ne $NONE) { & $add $L.key_row_lshift "r a = $($k.lshift)" @(, @($PHASE, $ph)) }
  136. }
  137. for ($ph = $lastPhase - 1; $ph -ge 0; $ph--) { # descending, so one hit moves on at most one phase
  138. & $add $L.people_moved ('> {0:x4} {1:x2}' -f $PHASE, ($ph + 1)) (@(, @($PHASE, $ph)) + $sc.phases[$ph].until)
  139. }
  140. $viewCheckpoint = $n + 1
  141. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.view_cx, ($L.view_cx + 3)) $null
  142. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.map_d011, ($L.map_d016)) $null
  143. & $add $L.people_moved ('m {0:x4} {0:x4}' -f $L.visible) $null
  144. & $add $L.people_moved ('m {0:x4} {0:x4}' -f $L.people_flip_pending) $null
  145. & $add $L.people_moved ('m {0:x4} {0:x4}' -f $PHASE) $null
  146. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.people_cx, ($L.people_cx + 79)) $null
  147. foreach ($save in @('bitmap-a.bin 6000 7a3f', 'bitmap-b.bin a000 ba3f', 'screen-a.bin 4000 4347', 'screen-b.bin 8000 8347')) {
  148. $f = $save -split ' '
  149. & $add $L.people_moved ('bsave \"{0}\" 0 {1} {2}' -f $f[0], $f[1], $f[2]) @(, @($PHASE, $lastPhase))
  150. }
  151. Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $mon
  152. Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
  153. $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target)
  154. $vargs += @('-limitcycles', [string]$sc.cycles, '-exitscreenshot', 'screen.png', '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target))
  155. $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } }
  156. $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru `
  157. -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err')
  158. if (-not $p.WaitForExit($TimeoutSeconds * 1000)) { $p.Kill($true) }
  159. # Parse frames: view (4 bytes), d011/d016, visible, people table; clock and raster line.
  160. $frames = [System.Collections.Generic.List[hashtable]]::new()
  161. $cur = $null; $want = $false
  162. foreach ($line in Get-Content -LiteralPath (Join-Path $dir 'monitor.log') -Encoding Latin1) {
  163. if ($line -match "^#$viewCheckpoint \(Trace\s+exec [0-9a-fA-F]+\)\s+(\d+)/") {
  164. $cur = @{ line = [int]$Matches[1]; people = [System.Collections.Generic.List[byte]]::new() }; $frames.Add($cur); $want = $true; continue
  165. }
  166. if ($want -and $line -match '^\.C:.*\s(\d+)\s*$') { $cur.clock = [long]$Matches[1]; $want = $false; continue }
  167. if ($null -eq $cur -or $line -notmatch '^>C:([0-9a-fA-F]{4}) ((?:[0-9a-fA-F]{2} )+)') { continue }
  168. $addr = [Convert]::ToInt32($Matches[1], 16)
  169. $bytes = @($Matches[2].Trim() -split ' ' | ForEach-Object { [Convert]::ToByte($_, 16) })
  170. if ($addr -eq $L.view_cx) { $cur.cx = $bytes[0]; $cur.fx = $bytes[1]; $cur.cy = $bytes[2]; $cur.fy = $bytes[3] }
  171. elseif ($addr -eq $L.map_d011) { $cur.d011 = $bytes[0]; $cur.d016 = $bytes[1] }
  172. elseif ($addr -eq $L.visible) { $cur.visible = $bytes[0] }
  173. elseif ($addr -eq $PHASE) { $cur.phase = $bytes[0] }
  174. elseif ($addr -eq $L.people_flip_pending) { $cur.pending = $bytes[0] }
  175. 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) } } }
  176. }
  177. $failures = [System.Collections.Generic.List[string]]::new()
  178. # A rejected checkpoint shifts the numbers of the ones after it, so commands would land on the wrong ones.
  179. $monErrors = @(Select-String -LiteralPath (Join-Path $dir 'monitor.log') -Pattern '^ERROR|not a valid checkpoint' | Select-Object -First 3)
  180. foreach ($e in $monErrors) { $failures.Add("monitor script error: $($e.Line.Trim())") }
  181. if ($frames.Count -lt 100) { $failures.Add("only $($frames.Count) frames logged") }
  182. $stalls = 0; $steps = 0; $late = 0; $lastLine = 0
  183. $maxVx = 0; $maxVy = 0; $minAfterMaxVx = 999; $minAfterMaxVy = 999
  184. $phaseFrames = @(0) * $sc.phases.Count
  185. for ($i = 0; $i -lt $frames.Count; $i++) {
  186. $f = $frames[$i]
  187. $vx = 8 * $f.cx + $f.fx; $vy = 8 * $f.cy + $f.fy
  188. $f.vx = $vx; $f.vy = $vy
  189. if ($f.phase -lt $sc.phases.Count) { $phaseFrames[$f.phase]++ } else { $failures.Add("frame ${i}: phase $($f.phase) out of range") }
  190. 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)") }
  191. 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)) }
  192. if ($f.line -ge 55 -and $f.line -lt 251) { $failures.Add("frame ${i}: people update ended at raster line $($f.line)") }
  193. elseif ($f.line -lt 55) { $lastLine = [Math]::Max($lastLine, $f.line) }
  194. if ($i -gt 0) {
  195. $p = $frames[$i - 1]
  196. $dx = $vx - $p.vx; $dy = $vy - $p.vy
  197. if ($dx -ne 0 -or $dy -ne 0) { $steps++ }
  198. if ($f.clock - $p.clock -gt 1.5 * 17095) { $late++ }
  199. # The keys read after people_moved in frame i-1 (that frame's phase) give frame i's step.
  200. $k = $sc.phases[[Math]::Min($p.phase, $lastPhase)]
  201. $bits = [Convert]::ToByte($k.cursor, 16)
  202. $sign = if ($k.lshift -eq $SHIFT) { -1 } else { 1 }
  203. $wantX = if (($bits -band 0x04) -eq 0) { $sign } else { 0 }
  204. $wantY = if (($bits -band 0x80) -eq 0) { $sign } else { 0 }
  205. if (($dx -ne 0 -and $dx -ne $wantX) -or ($dy -ne 0 -and $dy -ne $wantY)) {
  206. $failures.Add("frame ${i}: view moved by ($dx,$dy) with keys asking for ($wantX,$wantY)")
  207. }
  208. 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") }
  209. $canX = ($wantX -gt 0 -and $p.vx -lt 336) -or ($wantX -lt 0 -and $p.vx -gt 0)
  210. $canY = ($wantY -gt 0 -and $p.vy -lt 244) -or ($wantY -lt 0 -and $p.vy -gt 0)
  211. if (($canX -or $canY) -and $dx -eq 0 -and $dy -eq 0) { $stalls++ }
  212. }
  213. if ($vx -gt $maxVx) { $maxVx = $vx; $minAfterMaxVx = $vx }
  214. if ($vy -gt $maxVy) { $maxVy = $vy; $minAfterMaxVy = $vy }
  215. if ($vx -lt $minAfterMaxVx) { $minAfterMaxVx = $vx }
  216. if ($vy -lt $minAfterMaxVy) { $minAfterMaxVy = $vy }
  217. }
  218. if ($late -gt 0) { $failures.Add("the main loop missed $late frame(s)") }
  219. if ($phaseFrames[$lastPhase] -lt 10) { $failures.Add("reached the last phase for only $($phaseFrames[$lastPhase]) frame(s) (frames per phase: $($phaseFrames -join ', '))") }
  220. if ($sc.name -eq 'edges') {
  221. if ($maxVx -ne 336 -or $maxVy -ne 244) { $failures.Add("did not reach the bottom-right edge (max vx $maxVx, vy $maxVy)") }
  222. if ($minAfterMaxVx -ne 0 -or $minAfterMaxVy -ne 0) { $failures.Add("did not return to the top-left edge (min vx $minAfterMaxVx, vy $minAfterMaxVy)") }
  223. }
  224. if ($stalls -gt $MaxStallFrames) { $failures.Add("$stalls stall frames (allowed $MaxStallFrames)") }
  225. # Final frames: at people_moved, the buffer that will be shown (the hidden one when a flip is
  226. # pending, otherwise the shown one) must equal a fresh render at the view origin plus this
  227. # frame's dots; with a flip pending, the shown buffer must hold the previous frame's dots.
  228. $world = [IO.File]::ReadAllBytes((Join-Path $dir 'world.bin'))
  229. $last = $frames[$frames.Count - 1]
  230. $checkBuffer = {
  231. param([int]$buf, $people, [string]$what)
  232. $suffix = @('a', 'b')[$buf]
  233. $bitmap = [IO.File]::ReadAllBytes((Join-Path $dir "bitmap-$suffix.bin"))
  234. $screen = [IO.File]::ReadAllBytes((Join-Path $dir "screen-$suffix.bin"))
  235. $dots = @{}
  236. for ($i = 0; $i -lt 16; $i++) {
  237. $col = $people[$i] - $last.cx; $row = $people[32 + $i] - $last.cy
  238. if ($col -ge 0 -and $col -lt 39 -and $row -ge 0 -and $row -lt 21) {
  239. $o = $row * 320 + $col * 8 + $people[48 + $i]
  240. $dots[$o] = [int]$dots[$o] -bor (0xC0 -shr (2 * $people[16 + $i]))
  241. }
  242. }
  243. $badBitmap = 0; $badScreen = 0
  244. $rows = [Math]::Min(21, 50 - $last.cy)
  245. for ($r = 0; $r -lt $rows; $r++) {
  246. for ($c = 0; $c -lt 39; $c++) {
  247. $t = $world[($last.cy + $r) * 80 + $last.cx + $c]
  248. if ($screen[$r * 40 + $c] -ne $tiles.colors[$t]) { $badScreen++ }
  249. for ($k = 0; $k -lt 8; $k++) {
  250. $o = $r * 320 + $c * 8 + $k
  251. if ($bitmap[$o] -ne ($tiles.patterns[$t][$k] -bor [int]$dots[$o])) { $badBitmap++ }
  252. }
  253. }
  254. }
  255. if ($badBitmap -gt 0 -or $badScreen -gt 0) { $failures.Add("buffer $suffix ($what) differs from the world plus dots: $badBitmap bitmap bytes, $badScreen colour bytes") }
  256. }
  257. if ($last.pending) {
  258. & $checkBuffer (1 - $last.visible) $last.people 'shown next frame'
  259. $prev = $frames[$frames.Count - 2]
  260. if ($prev.cx -eq $last.cx -and $prev.cy -eq $last.cy) { & $checkBuffer $last.visible $prev.people 'shown, previous dots' }
  261. } else {
  262. & $checkBuffer $last.visible $last.people 'shown'
  263. }
  264. $summary = "{0}: {1} frames (per phase {9}), {2} moving, {3} stall frames, update done by line {4}, final view ({5},{6}) cell ({7},{8})" -f `
  265. $sc.name, $frames.Count, $steps, $stalls, $lastLine, $last.vx, $last.vy, $last.cx, $last.cy, ($phaseFrames -join '/')
  266. if ($failures.Count -gt 0) {
  267. $failedScenarios++
  268. Write-Host " FAIL $summary" -ForegroundColor Red
  269. $failures | Select-Object -First 8 | ForEach-Object { Write-Host " $_" }
  270. } else {
  271. Write-Host " ok $summary"
  272. }
  273. }
  274. if ($failedScenarios -gt 0) { Write-Host "[FAIL] $failedScenarios of $($scenarios.Count) scroll scenarios" -ForegroundColor Red; exit 1 }
  275. Write-Host "[PASS] all $($scenarios.Count) scroll scenarios: smooth one-pixel steps, edges respected, shown buffer matches the world" -ForegroundColor Green
  276. exit 0

Powered by TurnKey Linux.