|
- <#
- .SYNOPSIS
- Check pixel-smooth scrolling of the C64 build headless in VICE.
-
- .DESCRIPTION
- Each scenario is a list of phases. A phase holds keys (faked as in tools/check-keys.ps1:
- tracepoints on the key_row_* instructions set the row bits, so host key presses cannot
- interfere) until a condition on the program state is met at people_moved (a frame
- number or a view position); the phase number lives in free RAM at $CFF0 and VICE
- conditional checkpoints move it on. Every frame the script logs the phase, view
- position, buffer state and scroll registers at people_moved; in the last phase it
- saves both buffers (bitmap and screen RAM), and the last save is checked.
-
- Scenarios:
- edges right+down to the bottom-right corner, hold there, then SHIFT (up+left)
- to the top-left corner, hold there, then release
- middle down, then right, then release in the middle of the world
- reverse right, reverse to left in the frame after a cell step, right again;
- then down, reverse to up right after a cell step; release
-
- Checks, per scenario:
- - the view moves at most one pixel per axis per frame, only in the direction of the
- keys read the frame before, and stays inside
- vx 0-336, vy 0-244; the fine-scroll registers match the view
- - every scenario reaches its last phase; edges: the view reaches vx 336 and vy 244,
- then 0 and 0, and does not move while the keys push against an edge
- - the main loop never misses a frame and the people update ends before raster 55
- - frames where a held key did not move the view although no edge was reached are
- counted as stalls and reported (they happen while the hidden buffer is not ready)
- - at the end, the buffer about to be shown (bitmap and screen RAM) equals a fresh
- render of the world from the view's top-left cell (39 columns, 21 rows) with this
- frame's dots ORed in; when a flip is pending, the shown buffer has last frame's dots
-
- Exit codes: 0 = pass, 1 = fail, 2 = a tool is missing.
-
- .EXAMPLE
- pwsh -File tools/check-scroll.ps1
- #>
- [CmdletBinding()]
- param([switch]$NoBuild, [int]$TimeoutSeconds = 300, [int]$MaxStallFrames = 40)
- . (Join-Path $PSScriptRoot 'lib.ps1')
-
- function Get-Label([hashtable]$Target, [string]$Symbol) {
- foreach ($line in Get-Content -LiteralPath (Get-RepoPath $Target.build.labels)) {
- $parts = $line -split '\s+'
- if ($parts.Count -ge 3 -and $parts[2].TrimStart('.') -eq $Symbol) { return [Convert]::ToInt32($parts[1], 16) }
- }
- throw "Symbol '$Symbol' not found in $($Target.build.labels)."
- }
-
- # Tile data from src/tiles.inc: patterns in file order, colours as declared there.
- function Get-TileData {
- $src = Get-Content -LiteralPath (Get-RepoPath 'src/tiles.inc') -Raw
- $block = $src.Substring($src.IndexOf('tile_patterns:'))
- $block = $block.Substring(0, $block.IndexOf('.assert * - tile_patterns'))
- $patterns = @(foreach ($m in [regex]::Matches($block, '(?m)^\s*\.byte\s+([^;\r\n]+)')) {
- , @($m.Groups[1].Value -split ',' | ForEach-Object { [Convert]::ToByte($_.Trim().TrimStart('$'), 16) })
- })
- $colors = [System.Collections.Generic.List[byte]]::new()
- $cblock = $src.Substring($src.IndexOf('tile_colors:'))
- $cblock = $cblock.Substring(0, $cblock.IndexOf('.assert * - tile_colors'))
- $repeat = 1
- foreach ($line in $cblock -split "`n") {
- if ($line -match '\.repeat\s+(\d+)') { $repeat = [int]$Matches[1]; continue }
- if ($line -match '\.endrepeat') { $repeat = 1; continue }
- if ($line -match '\.byte\s+([^;\r\n]+)') {
- $vals = $Matches[1] -split ',' | ForEach-Object { [Convert]::ToByte($_.Trim().TrimStart('$'), 16) }
- for ($r = 0; $r -lt $repeat; $r++) { foreach ($v in $vals) { $colors.Add($v) } }
- }
- }
- @{ patterns = $patterns; colors = $colors.ToArray() }
- }
-
- try {
- $project = Get-ProjectConfig
- $target = Get-ProjectTargets $project 'c64'
- if (-not $NoBuild) { Invoke-TargetBuild $project $target @{} }
- $emu = Get-ToolPath $project $target.emulator.binary
- } catch [MissingToolException] {
- Write-Host $_.Exception.Message -ForegroundColor Yellow
- exit 2
- }
-
- $tiles = Get-TileData
- $L = @{}
- foreach ($s in 'smoke_ready', 'people_moved', 'world_tiles', 'people_cx', 'visible', 'view_cx', 'map_d011', 'map_d016',
- 'move_frame', 'people_flip_pending', 'view_fx', 'view_cy', 'view_fy',
- 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space') { $L[$s] = Get-Label $target $s }
-
- # Key bits (active low): cursor column RIGHT = bit 2, DOWN = bit 7; left SHIFT = bit 7.
- $RIGHT_DOWN = '7b'; $DOWN = '7f'; $RIGHTK = 'fb'; $NONE = 'ff'; $SHIFT = '7f'
- $PHASE = 0xCFF0 # free RAM (the program ends below $2700)
- # Phase end conditions: frame = move_frame value (wraps every 256 frames); view = cell and fine.
- function Frame([int]$n) { , (, @($L.move_frame, $n)) }
- function View([int]$cx, [int]$fx, [int]$cy, [int]$fy) {
- $c = @()
- if ($cx -ge 0) { $c += , @($L.view_cx, $cx); $c += , @($L.view_fx, $fx) }
- if ($cy -ge 0) { $c += , @($L.view_cy, $cy); $c += , @($L.view_fy, $fy) }
- $c
- }
- $scenarios = @(
- @{ name = 'edges'; cycles = 30000000; phases = @(
- @{ cursor = $NONE; lshift = $NONE; until = (Frame 30) }
- @{ cursor = $RIGHT_DOWN; lshift = $NONE; until = (View 42 0 30 4) } # vx 336, vy 244
- @{ cursor = $RIGHT_DOWN; lshift = $NONE; until = (Frame 0); edge = $true }
- @{ cursor = $RIGHT_DOWN; lshift = $SHIFT; until = (View 0 0 0 0) }
- @{ cursor = $RIGHT_DOWN; lshift = $SHIFT; until = (Frame 128); edge = $true }
- @{ cursor = $NONE; lshift = $NONE }) }
- @{ name = 'middle'; cycles = 12000000; phases = @(
- @{ cursor = $NONE; lshift = $NONE; until = (Frame 30) }
- @{ cursor = $DOWN; lshift = $NONE; until = (View -1 0 12 3) } # vy 99
- @{ cursor = $RIGHTK; lshift = $NONE; until = (View 16 5 -1 0) } # vx 133
- @{ cursor = $NONE; lshift = $NONE }) }
- @{ name = 'reverse'; cycles = 12000000; phases = @(
- @{ cursor = $NONE; lshift = $NONE; until = (Frame 30) }
- @{ cursor = $RIGHTK; lshift = $NONE; until = (View 9 0 -1 0) } # just stepped into cell 9
- @{ cursor = $RIGHTK; lshift = $SHIFT; until = (View 3 2 -1 0) }
- @{ cursor = $RIGHTK; lshift = $NONE; until = (View 5 0 -1 0) }
- @{ cursor = $DOWN; lshift = $NONE; until = (View -1 0 6 0) } # just stepped into row 6
- @{ cursor = $DOWN; lshift = $SHIFT; until = (View -1 0 2 1) }
- @{ cursor = $NONE; lshift = $NONE }) }
- )
-
- $root = Get-RepoPath 'build/check-scroll'
- if (Test-Path -LiteralPath $root) { Remove-Item -LiteralPath $root -Recurse -Force }
- $failedScenarios = 0
- foreach ($sc in $scenarios) {
- $dir = Join-Path $root $sc.name
- New-Item -ItemType Directory -Force -Path $dir | Out-Null
- $mon = [System.Collections.Generic.List[string]]::new()
- $mon.Add('logname "monitor.log"'); $mon.Add('log on')
- $n = 0
- $add = { param($addr, $cmd, $cond)
- $script:n++
- $line = 'trace exec ${0:x4}' -f $addr
- if ($cond) { $line += ' if ' + (($cond | ForEach-Object { '(@cpu:${0:x4} == ${1:x2})' -f $_[0], $_[1] }) -join ' && ') }
- $mon.Add($line); $mon.Add(('command {0} "{1}"' -f $script:n, $cmd))
- }
- $lastPhase = $sc.phases.Count - 1
- & $add $L.smoke_ready ('bsave \"world.bin\" 0 {0:x4} {1:x4}' -f $L.world_tiles, ($L.world_tiles + 3999)) $null
- & $add $L.smoke_ready ('> {0:x4} 00' -f $PHASE) $null
- foreach ($row in 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space') { & $add $L[$row] 'r a = ff' $null }
- for ($ph = 0; $ph -le $lastPhase; $ph++) { # later checkpoints at one address run after earlier ones
- $k = $sc.phases[$ph]
- if ($k.cursor -ne $NONE) { & $add $L.key_row_cursor "r a = $($k.cursor)" @(, @($PHASE, $ph)) }
- if ($k.lshift -ne $NONE) { & $add $L.key_row_lshift "r a = $($k.lshift)" @(, @($PHASE, $ph)) }
- }
- for ($ph = $lastPhase - 1; $ph -ge 0; $ph--) { # descending, so one hit moves on at most one phase
- & $add $L.people_moved ('> {0:x4} {1:x2}' -f $PHASE, ($ph + 1)) (@(, @($PHASE, $ph)) + $sc.phases[$ph].until)
- }
- $viewCheckpoint = $n + 1
- & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.view_cx, ($L.view_cx + 3)) $null
- & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.map_d011, ($L.map_d016)) $null
- & $add $L.people_moved ('m {0:x4} {0:x4}' -f $L.visible) $null
- & $add $L.people_moved ('m {0:x4} {0:x4}' -f $L.people_flip_pending) $null
- & $add $L.people_moved ('m {0:x4} {0:x4}' -f $PHASE) $null
- & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.people_cx, ($L.people_cx + 79)) $null
- foreach ($save in @('bitmap-a.bin 6000 7a3f', 'bitmap-b.bin a000 ba3f', 'screen-a.bin 4000 4347', 'screen-b.bin 8000 8347')) {
- $f = $save -split ' '
- & $add $L.people_moved ('bsave \"{0}\" 0 {1} {2}' -f $f[0], $f[1], $f[2]) @(, @($PHASE, $lastPhase))
- }
- Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $mon
- Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
- $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target)
- $vargs += @('-limitcycles', [string]$sc.cycles, '-exitscreenshot', 'screen.png', '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target))
- $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } }
- $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru `
- -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err')
- if (-not $p.WaitForExit($TimeoutSeconds * 1000)) { $p.Kill($true) }
-
- # Parse frames: view (4 bytes), d011/d016, visible, people table; clock and raster line.
- $frames = [System.Collections.Generic.List[hashtable]]::new()
- $cur = $null; $want = $false
- foreach ($line in Get-Content -LiteralPath (Join-Path $dir 'monitor.log') -Encoding Latin1) {
- if ($line -match "^#$viewCheckpoint \(Trace\s+exec [0-9a-fA-F]+\)\s+(\d+)/") {
- $cur = @{ line = [int]$Matches[1]; people = [System.Collections.Generic.List[byte]]::new() }; $frames.Add($cur); $want = $true; continue
- }
- if ($want -and $line -match '^\.C:.*\s(\d+)\s*$') { $cur.clock = [long]$Matches[1]; $want = $false; continue }
- if ($null -eq $cur -or $line -notmatch '^>C:([0-9a-fA-F]{4}) ((?:[0-9a-fA-F]{2} )+)') { continue }
- $addr = [Convert]::ToInt32($Matches[1], 16)
- $bytes = @($Matches[2].Trim() -split ' ' | ForEach-Object { [Convert]::ToByte($_, 16) })
- if ($addr -eq $L.view_cx) { $cur.cx = $bytes[0]; $cur.fx = $bytes[1]; $cur.cy = $bytes[2]; $cur.fy = $bytes[3] }
- elseif ($addr -eq $L.map_d011) { $cur.d011 = $bytes[0]; $cur.d016 = $bytes[1] }
- elseif ($addr -eq $L.visible) { $cur.visible = $bytes[0] }
- elseif ($addr -eq $PHASE) { $cur.phase = $bytes[0] }
- elseif ($addr -eq $L.people_flip_pending) { $cur.pending = $bytes[0] }
- 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) } } }
- }
- $failures = [System.Collections.Generic.List[string]]::new()
- # A rejected checkpoint shifts the numbers of the ones after it, so commands would land on the wrong ones.
- $monErrors = @(Select-String -LiteralPath (Join-Path $dir 'monitor.log') -Pattern '^ERROR|not a valid checkpoint' | Select-Object -First 3)
- foreach ($e in $monErrors) { $failures.Add("monitor script error: $($e.Line.Trim())") }
- if ($frames.Count -lt 100) { $failures.Add("only $($frames.Count) frames logged") }
- $stalls = 0; $steps = 0; $late = 0; $lastLine = 0
- $maxVx = 0; $maxVy = 0; $minAfterMaxVx = 999; $minAfterMaxVy = 999
- $phaseFrames = @(0) * $sc.phases.Count
- for ($i = 0; $i -lt $frames.Count; $i++) {
- $f = $frames[$i]
- $vx = 8 * $f.cx + $f.fx; $vy = 8 * $f.cy + $f.fy
- $f.vx = $vx; $f.vy = $vy
- if ($f.phase -lt $sc.phases.Count) { $phaseFrames[$f.phase]++ } else { $failures.Add("frame ${i}: phase $($f.phase) out of range") }
- 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)") }
- 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)) }
- if ($f.line -ge 55 -and $f.line -lt 251) { $failures.Add("frame ${i}: people update ended at raster line $($f.line)") }
- elseif ($f.line -lt 55) { $lastLine = [Math]::Max($lastLine, $f.line) }
- if ($i -gt 0) {
- $p = $frames[$i - 1]
- $dx = $vx - $p.vx; $dy = $vy - $p.vy
- if ($dx -ne 0 -or $dy -ne 0) { $steps++ }
- if ($f.clock - $p.clock -gt 1.5 * 17095) { $late++ }
- # The keys read after people_moved in frame i-1 (that frame's phase) give frame i's step.
- $k = $sc.phases[[Math]::Min($p.phase, $lastPhase)]
- $bits = [Convert]::ToByte($k.cursor, 16)
- $sign = if ($k.lshift -eq $SHIFT) { -1 } else { 1 }
- $wantX = if (($bits -band 0x04) -eq 0) { $sign } else { 0 }
- $wantY = if (($bits -band 0x80) -eq 0) { $sign } else { 0 }
- if (($dx -ne 0 -and $dx -ne $wantX) -or ($dy -ne 0 -and $dy -ne $wantY)) {
- $failures.Add("frame ${i}: view moved by ($dx,$dy) with keys asking for ($wantX,$wantY)")
- }
- 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") }
- $canX = ($wantX -gt 0 -and $p.vx -lt 336) -or ($wantX -lt 0 -and $p.vx -gt 0)
- $canY = ($wantY -gt 0 -and $p.vy -lt 244) -or ($wantY -lt 0 -and $p.vy -gt 0)
- if (($canX -or $canY) -and $dx -eq 0 -and $dy -eq 0) { $stalls++ }
- }
- if ($vx -gt $maxVx) { $maxVx = $vx; $minAfterMaxVx = $vx }
- if ($vy -gt $maxVy) { $maxVy = $vy; $minAfterMaxVy = $vy }
- if ($vx -lt $minAfterMaxVx) { $minAfterMaxVx = $vx }
- if ($vy -lt $minAfterMaxVy) { $minAfterMaxVy = $vy }
- }
- if ($late -gt 0) { $failures.Add("the main loop missed $late frame(s)") }
- if ($phaseFrames[$lastPhase] -lt 10) { $failures.Add("reached the last phase for only $($phaseFrames[$lastPhase]) frame(s) (frames per phase: $($phaseFrames -join ', '))") }
- if ($sc.name -eq 'edges') {
- if ($maxVx -ne 336 -or $maxVy -ne 244) { $failures.Add("did not reach the bottom-right edge (max vx $maxVx, vy $maxVy)") }
- if ($minAfterMaxVx -ne 0 -or $minAfterMaxVy -ne 0) { $failures.Add("did not return to the top-left edge (min vx $minAfterMaxVx, vy $minAfterMaxVy)") }
- }
- if ($stalls -gt $MaxStallFrames) { $failures.Add("$stalls stall frames (allowed $MaxStallFrames)") }
-
- # Final frames: at people_moved, the buffer that will be shown (the hidden one when a flip is
- # pending, otherwise the shown one) must equal a fresh render at the view origin plus this
- # frame's dots; with a flip pending, the shown buffer must hold the previous frame's dots.
- $world = [IO.File]::ReadAllBytes((Join-Path $dir 'world.bin'))
- $last = $frames[$frames.Count - 1]
- $checkBuffer = {
- param([int]$buf, $people, [string]$what)
- $suffix = @('a', 'b')[$buf]
- $bitmap = [IO.File]::ReadAllBytes((Join-Path $dir "bitmap-$suffix.bin"))
- $screen = [IO.File]::ReadAllBytes((Join-Path $dir "screen-$suffix.bin"))
- $dots = @{}
- for ($i = 0; $i -lt 16; $i++) {
- $col = $people[$i] - $last.cx; $row = $people[32 + $i] - $last.cy
- if ($col -ge 0 -and $col -lt 39 -and $row -ge 0 -and $row -lt 21) {
- $o = $row * 320 + $col * 8 + $people[48 + $i]
- $dots[$o] = [int]$dots[$o] -bor (0xC0 -shr (2 * $people[16 + $i]))
- }
- }
- $badBitmap = 0; $badScreen = 0
- $rows = [Math]::Min(21, 50 - $last.cy)
- for ($r = 0; $r -lt $rows; $r++) {
- for ($c = 0; $c -lt 39; $c++) {
- $t = $world[($last.cy + $r) * 80 + $last.cx + $c]
- if ($screen[$r * 40 + $c] -ne $tiles.colors[$t]) { $badScreen++ }
- for ($k = 0; $k -lt 8; $k++) {
- $o = $r * 320 + $c * 8 + $k
- if ($bitmap[$o] -ne ($tiles.patterns[$t][$k] -bor [int]$dots[$o])) { $badBitmap++ }
- }
- }
- }
- 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") }
- }
- if ($last.pending) {
- & $checkBuffer (1 - $last.visible) $last.people 'shown next frame'
- $prev = $frames[$frames.Count - 2]
- if ($prev.cx -eq $last.cx -and $prev.cy -eq $last.cy) { & $checkBuffer $last.visible $prev.people 'shown, previous dots' }
- } else {
- & $checkBuffer $last.visible $last.people 'shown'
- }
-
- $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 `
- $sc.name, $frames.Count, $steps, $stalls, $lastLine, $last.vx, $last.vy, $last.cx, $last.cy, ($phaseFrames -join '/')
- if ($failures.Count -gt 0) {
- $failedScenarios++
- Write-Host " FAIL $summary" -ForegroundColor Red
- $failures | Select-Object -First 8 | ForEach-Object { Write-Host " $_" }
- } else {
- Write-Host " ok $summary"
- }
- }
- if ($failedScenarios -gt 0) { Write-Host "[FAIL] $failedScenarios of $($scenarios.Count) scroll scenarios" -ForegroundColor Red; exit 1 }
- Write-Host "[PASS] all $($scenarios.Count) scroll scenarios: smooth one-pixel steps, edges respected, shown buffer matches the world" -ForegroundColor Green
- exit 0
|