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.

236 line
14KB

  1. <#
  2. .SYNOPSIS
  3. Check pixel-smooth scrolling of the C64 build headless in VICE.
  4. .DESCRIPTION
  5. Each scenario holds keys on a schedule (faked as in tools/check-keys.ps1: tracepoints
  6. on the key_row_* instructions set the row bits; host key presses cannot interfere),
  7. logs the view position, buffer state and scroll registers at people_moved every frame,
  8. and saves both buffers (bitmap and screen RAM) each frame; the last save is checked.
  9. Scenarios:
  10. edges right+down until the bottom-right corner, then SHIFT (up+left) until
  11. the top-left corner, then release
  12. middle down, then right, then release in the middle of the world
  13. reverse right for a while, reverse to left right after a cell step, release
  14. Checks, per scenario:
  15. - the view moves at most one pixel per axis per frame and stays inside
  16. vx 0-336, vy 0-244; the fine-scroll registers match the view
  17. - edges: the view reaches vx 336 and vy 244, then 0 and 0, and stops there
  18. - the main loop never misses a frame and the people update ends before raster 55
  19. - frames where a held key did not move the view although no edge was reached are
  20. counted as stalls and reported (they happen while the hidden buffer is not ready)
  21. - at the end, the shown buffer's bitmap and screen RAM equal a fresh render of the
  22. world from the view's top-left cell (39 columns, 21 rows), except bytes with dots
  23. Exit codes: 0 = pass, 1 = fail, 2 = a tool is missing.
  24. .EXAMPLE
  25. pwsh -File tools/check-scroll.ps1
  26. #>
  27. [CmdletBinding()]
  28. param([switch]$NoBuild, [int]$TimeoutSeconds = 300, [int]$MaxStallFrames = 40)
  29. . (Join-Path $PSScriptRoot 'lib.ps1')
  30. function Get-Label([hashtable]$Target, [string]$Symbol) {
  31. foreach ($line in Get-Content -LiteralPath (Get-RepoPath $Target.build.labels)) {
  32. $parts = $line -split '\s+'
  33. if ($parts.Count -ge 3 -and $parts[2].TrimStart('.') -eq $Symbol) { return [Convert]::ToInt32($parts[1], 16) }
  34. }
  35. throw "Symbol '$Symbol' not found in $($Target.build.labels)."
  36. }
  37. # Tile data from src/tiles.inc: patterns in file order, colours as declared there.
  38. function Get-TileData {
  39. $src = Get-Content -LiteralPath (Get-RepoPath 'src/tiles.inc') -Raw
  40. $block = $src.Substring($src.IndexOf('tile_patterns:'))
  41. $block = $block.Substring(0, $block.IndexOf('.assert * - tile_patterns'))
  42. $patterns = @(foreach ($m in [regex]::Matches($block, '(?m)^\s*\.byte\s+([^;\r\n]+)')) {
  43. , @($m.Groups[1].Value -split ',' | ForEach-Object { [Convert]::ToByte($_.Trim().TrimStart('$'), 16) })
  44. })
  45. $colors = [System.Collections.Generic.List[byte]]::new()
  46. $cblock = $src.Substring($src.IndexOf('tile_colors:'))
  47. $cblock = $cblock.Substring(0, $cblock.IndexOf('.assert * - tile_colors'))
  48. $repeat = 1
  49. foreach ($line in $cblock -split "`n") {
  50. if ($line -match '\.repeat\s+(\d+)') { $repeat = [int]$Matches[1]; continue }
  51. if ($line -match '\.endrepeat') { $repeat = 1; continue }
  52. if ($line -match '\.byte\s+([^;\r\n]+)') {
  53. $vals = $Matches[1] -split ',' | ForEach-Object { [Convert]::ToByte($_.Trim().TrimStart('$'), 16) }
  54. for ($r = 0; $r -lt $repeat; $r++) { foreach ($v in $vals) { $colors.Add($v) } }
  55. }
  56. }
  57. @{ patterns = $patterns; colors = $colors.ToArray() }
  58. }
  59. try {
  60. $project = Get-ProjectConfig
  61. $target = Get-ProjectTargets $project 'c64'
  62. if (-not $NoBuild) { Invoke-TargetBuild $project $target @{} }
  63. $emu = Get-ToolPath $project $target.emulator.binary
  64. } catch [MissingToolException] {
  65. Write-Host $_.Exception.Message -ForegroundColor Yellow
  66. exit 2
  67. }
  68. $tiles = Get-TileData
  69. $L = @{}
  70. foreach ($s in 'smoke_ready', 'people_moved', 'world_tiles', 'people_cx', 'visible', 'view_cx', 'map_d011', 'map_d016',
  71. 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space') { $L[$s] = Get-Label $target $s }
  72. # Key bits (active low): cursor column RIGHT = bit 2, DOWN = bit 7; left SHIFT = bit 7.
  73. $RIGHT_DOWN = '7b'; $DOWN = '7f'; $RIGHTK = 'fb'; $NONE = 'ff'; $SHIFT = '7f'
  74. $scenarios = @(
  75. @{ name = 'edges'; cycles = 30000000; schedule = @(
  76. @{ at = 30; cursor = $RIGHT_DOWN; lshift = $NONE }
  77. @{ at = 520; cursor = $RIGHT_DOWN; lshift = $SHIFT }
  78. @{ at = 1010; cursor = $NONE; lshift = $NONE }) }
  79. @{ name = 'middle'; cycles = 12000000; schedule = @(
  80. @{ at = 30; cursor = $DOWN; lshift = $NONE }
  81. @{ at = 130; cursor = $RIGHTK; lshift = $NONE }
  82. @{ at = 263; cursor = $NONE; lshift = $NONE }) }
  83. @{ name = 'reverse'; cycles = 12000000; schedule = @(
  84. @{ at = 30; cursor = $RIGHTK; lshift = $NONE }
  85. @{ at = 104; cursor = $RIGHTK; lshift = $SHIFT }
  86. @{ at = 200; cursor = $NONE; lshift = $NONE }) }
  87. )
  88. $root = Get-RepoPath 'build/check-scroll'
  89. if (Test-Path -LiteralPath $root) { Remove-Item -LiteralPath $root -Recurse -Force }
  90. $failedScenarios = 0
  91. foreach ($sc in $scenarios) {
  92. $dir = Join-Path $root $sc.name
  93. New-Item -ItemType Directory -Force -Path $dir | Out-Null
  94. $mon = [System.Collections.Generic.List[string]]::new()
  95. $mon.Add('logname "monitor.log"'); $mon.Add('log on')
  96. $n = 0
  97. $add = { param($addr, $cmd, $ignore)
  98. $script:n++
  99. $mon.Add(('trace exec ${0:x4}' -f $addr)); $mon.Add(('command {0} "{1}"' -f $script:n, $cmd))
  100. if ($ignore) { $mon.Add(('ignore {0} {1}' -f $script:n, $ignore)) }
  101. }
  102. & $add $L.smoke_ready ('bsave \"world.bin\" 0 {0:x4} {1:x4}' -f $L.world_tiles, ($L.world_tiles + 3999)) 0
  103. & $add $L.key_row_cursor 'r a = ff' 0
  104. & $add $L.key_row_lshift 'r a = ff' 0
  105. & $add $L.key_row_rshift 'r a = ff' 0
  106. & $add $L.key_row_space 'r a = ff' 0
  107. foreach ($step in $sc.schedule) { # later checkpoints run after earlier ones at the same address
  108. & $add $L.key_row_cursor "r a = $($step.cursor)" $step.at
  109. & $add $L.key_row_lshift "r a = $($step.lshift)" $step.at
  110. }
  111. $viewCheckpoint = $n + 1
  112. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.view_cx, ($L.view_cx + 3)) 0
  113. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.map_d011, ($L.map_d016)) 0
  114. & $add $L.people_moved ('m {0:x4} {0:x4}' -f $L.visible) 0
  115. & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.people_cx, ($L.people_cx + 79)) 0
  116. & $add $L.people_moved 'bsave \"bitmap-a.bin\" 0 6000 7a3f' 0
  117. & $add $L.people_moved 'bsave \"bitmap-b.bin\" 0 a000 ba3f' 0
  118. & $add $L.people_moved 'bsave \"screen-a.bin\" 0 4000 4347' 0
  119. & $add $L.people_moved 'bsave \"screen-b.bin\" 0 8000 8347' 0
  120. Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $mon
  121. Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
  122. $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target)
  123. $vargs += @('-limitcycles', [string]$sc.cycles, '-exitscreenshot', 'screen.png', '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target))
  124. $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } }
  125. $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru `
  126. -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err')
  127. if (-not $p.WaitForExit($TimeoutSeconds * 1000)) { $p.Kill($true) }
  128. # Parse frames: view (4 bytes), d011/d016, visible, people table; clock and raster line.
  129. $frames = [System.Collections.Generic.List[hashtable]]::new()
  130. $cur = $null; $want = $false
  131. foreach ($line in Get-Content -LiteralPath (Join-Path $dir 'monitor.log') -Encoding Latin1) {
  132. if ($line -match "^#$viewCheckpoint \(Trace\s+exec [0-9a-fA-F]+\)\s+(\d+)/") {
  133. $cur = @{ line = [int]$Matches[1]; people = [System.Collections.Generic.List[byte]]::new() }; $frames.Add($cur); $want = $true; continue
  134. }
  135. if ($want -and $line -match '^\.C:.*\s(\d+)\s*$') { $cur.clock = [long]$Matches[1]; $want = $false; continue }
  136. if ($null -eq $cur -or $line -notmatch '^>C:([0-9a-fA-F]{4}) ((?:[0-9a-fA-F]{2} )+)') { continue }
  137. $addr = [Convert]::ToInt32($Matches[1], 16)
  138. $bytes = @($Matches[2].Trim() -split ' ' | ForEach-Object { [Convert]::ToByte($_, 16) })
  139. if ($addr -eq $L.view_cx) { $cur.cx = $bytes[0]; $cur.fx = $bytes[1]; $cur.cy = $bytes[2]; $cur.fy = $bytes[3] }
  140. elseif ($addr -eq $L.map_d011) { $cur.d011 = $bytes[0]; $cur.d016 = $bytes[1] }
  141. elseif ($addr -eq $L.visible) { $cur.visible = $bytes[0] }
  142. 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) } } }
  143. }
  144. $failures = [System.Collections.Generic.List[string]]::new()
  145. if ($frames.Count -lt 100) { $failures.Add("only $($frames.Count) frames logged") }
  146. $stalls = 0; $steps = 0; $late = 0; $lastLine = 0
  147. $maxVx = 0; $maxVy = 0; $minAfterMaxVx = 999; $minAfterMaxVy = 999
  148. $keysFor = { param($i) $k = @{ cursor = $NONE; lshift = $NONE }; foreach ($s in $sc.schedule) { if ($i -ge $s.at) { $k = $s } }; $k }
  149. for ($i = 0; $i -lt $frames.Count; $i++) {
  150. $f = $frames[$i]
  151. $vx = 8 * $f.cx + $f.fx; $vy = 8 * $f.cy + $f.fy
  152. $f.vx = $vx; $f.vy = $vy
  153. 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)") }
  154. 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)) }
  155. if ($f.line -ge 55 -and $f.line -lt 251) { $failures.Add("frame ${i}: people update ended at raster line $($f.line)") }
  156. elseif ($f.line -lt 55) { $lastLine = [Math]::Max($lastLine, $f.line) }
  157. if ($i -gt 0) {
  158. $p = $frames[$i - 1]
  159. $dx = $vx - $p.vx; $dy = $vy - $p.vy
  160. if ([Math]::Abs($dx) -gt 1 -or [Math]::Abs($dy) -gt 1) { $failures.Add("frame ${i}: view jumped by ($dx,$dy)") }
  161. if ($dx -ne 0 -or $dy -ne 0) { $steps++ }
  162. if ($f.clock - $p.clock -gt 1.5 * 17095) { $late++ }
  163. # Keys read in frame i-2 drive the step applied in frame i-1's vertical blank... the
  164. # key row hooks count from the first read; allow two frames of latency.
  165. $k = & $keysFor ($i - 2)
  166. if ($i -ge 2 -and $k.cursor -ne $NONE) {
  167. $wantX = if (([Convert]::ToByte($k.cursor, 16) -band 0x04) -eq 0) { if ($k.lshift -eq $SHIFT) { -1 } else { 1 } } else { 0 }
  168. $wantY = if (([Convert]::ToByte($k.cursor, 16) -band 0x80) -eq 0) { if ($k.lshift -eq $SHIFT) { -1 } else { 1 } } else { 0 }
  169. $canX = ($wantX -gt 0 -and $p.vx -lt 336) -or ($wantX -lt 0 -and $p.vx -gt 0)
  170. $canY = ($wantY -gt 0 -and $p.vy -lt 244) -or ($wantY -lt 0 -and $p.vy -gt 0)
  171. if (($canX -or $canY) -and $dx -eq 0 -and $dy -eq 0) { $stalls++ }
  172. }
  173. }
  174. if ($vx -gt $maxVx) { $maxVx = $vx; $minAfterMaxVx = $vx }
  175. if ($vy -gt $maxVy) { $maxVy = $vy; $minAfterMaxVy = $vy }
  176. if ($vx -lt $minAfterMaxVx) { $minAfterMaxVx = $vx }
  177. if ($vy -lt $minAfterMaxVy) { $minAfterMaxVy = $vy }
  178. }
  179. if ($late -gt 0) { $failures.Add("the main loop missed $late frame(s)") }
  180. if ($sc.name -eq 'edges') {
  181. if ($maxVx -ne 336 -or $maxVy -ne 244) { $failures.Add("did not reach the bottom-right edge (max vx $maxVx, vy $maxVy)") }
  182. if ($minAfterMaxVx -ne 0 -or $minAfterMaxVy -ne 0) { $failures.Add("did not return to the top-left edge (min vx $minAfterMaxVx, vy $minAfterMaxVy)") }
  183. }
  184. if ($stalls -gt $MaxStallFrames) { $failures.Add("$stalls stall frames (allowed $MaxStallFrames)") }
  185. # Final frame: shown buffer = fresh render at the view origin, except dot bytes.
  186. $last = $frames[$frames.Count - 1]
  187. $suffix = @('a', 'b')[$last.visible]
  188. $bitmap = [IO.File]::ReadAllBytes((Join-Path $dir "bitmap-$suffix.bin"))
  189. $screen = [IO.File]::ReadAllBytes((Join-Path $dir "screen-$suffix.bin"))
  190. $world = [IO.File]::ReadAllBytes((Join-Path $dir 'world.bin'))
  191. $dotBytes = @{}
  192. for ($i = 0; $i -lt 16; $i++) {
  193. $col = $last.people[$i] - $last.cx; $row = $last.people[32 + $i] - $last.cy
  194. if ($col -ge 0 -and $col -lt 39 -and $row -ge 0 -and $row -lt 21) { $dotBytes[$row * 320 + $col * 8 + $last.people[48 + $i]] = 1 }
  195. }
  196. $badBitmap = 0; $badScreen = 0
  197. $rows = [Math]::Min(21, 50 - $last.cy)
  198. for ($r = 0; $r -lt $rows; $r++) {
  199. for ($c = 0; $c -lt 39; $c++) {
  200. $t = $world[($last.cy + $r) * 80 + $last.cx + $c]
  201. if ($screen[$r * 40 + $c] -ne $tiles.colors[$t]) { $badScreen++ }
  202. for ($k = 0; $k -lt 8; $k++) {
  203. $o = $r * 320 + $c * 8 + $k
  204. if ($bitmap[$o] -ne $tiles.patterns[$t][$k] -and -not $dotBytes.ContainsKey($o)) { $badBitmap++ }
  205. }
  206. }
  207. }
  208. if ($badBitmap -gt 0 -or $badScreen -gt 0) { $failures.Add("final shown buffer differs from the world at the view: $badBitmap bitmap bytes, $badScreen colour bytes") }
  209. $summary = "{0}: {1} frames, {2} moving, {3} stall frames, update done by line {4}, final view ({5},{6}) cell ({7},{8})" -f `
  210. $sc.name, $frames.Count, $steps, $stalls, $lastLine, $last.vx, $last.vy, $last.cx, $last.cy
  211. if ($failures.Count -gt 0) {
  212. $failedScenarios++
  213. Write-Host " FAIL $summary" -ForegroundColor Red
  214. $failures | Select-Object -First 8 | ForEach-Object { Write-Host " $_" }
  215. } else {
  216. Write-Host " ok $summary"
  217. }
  218. }
  219. if ($failedScenarios -gt 0) { Write-Host "[FAIL] $failedScenarios of $($scenarios.Count) scroll scenarios" -ForegroundColor Red; exit 1 }
  220. Write-Host "[PASS] all $($scenarios.Count) scroll scenarios: smooth one-pixel steps, edges respected, shown buffer matches the world" -ForegroundColor Green
  221. exit 0

Powered by TurnKey Linux.