<# .SYNOPSIS Check pixel-smooth scrolling of the C64 build headless in VICE. .DESCRIPTION Each scenario holds keys on a schedule (faked as in tools/check-keys.ps1: tracepoints on the key_row_* instructions set the row bits; host key presses cannot interfere), logs the view position, buffer state and scroll registers at people_moved every frame, and saves both buffers (bitmap and screen RAM) each frame; the last save is checked. Scenarios: edges right+down until the bottom-right corner, then SHIFT (up+left) until the top-left corner, then release middle down, then right, then release in the middle of the world reverse right for a while, reverse to left right after a cell step, release Checks, per scenario: - the view moves at most one pixel per axis per frame and stays inside vx 0-336, vy 0-244; the fine-scroll registers match the view - edges: the view reaches vx 336 and vy 244, then 0 and 0, and stops there - 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 shown buffer's bitmap and screen RAM equal a fresh render of the world from the view's top-left cell (39 columns, 21 rows), except bytes with 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', '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' $scenarios = @( @{ name = 'edges'; cycles = 30000000; schedule = @( @{ at = 30; cursor = $RIGHT_DOWN; lshift = $NONE } @{ at = 520; cursor = $RIGHT_DOWN; lshift = $SHIFT } @{ at = 1010; cursor = $NONE; lshift = $NONE }) } @{ name = 'middle'; cycles = 12000000; schedule = @( @{ at = 30; cursor = $DOWN; lshift = $NONE } @{ at = 130; cursor = $RIGHTK; lshift = $NONE } @{ at = 263; cursor = $NONE; lshift = $NONE }) } @{ name = 'reverse'; cycles = 12000000; schedule = @( @{ at = 30; cursor = $RIGHTK; lshift = $NONE } @{ at = 104; cursor = $RIGHTK; lshift = $SHIFT } @{ at = 200; 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, $ignore) $script:n++ $mon.Add(('trace exec ${0:x4}' -f $addr)); $mon.Add(('command {0} "{1}"' -f $script:n, $cmd)) if ($ignore) { $mon.Add(('ignore {0} {1}' -f $script:n, $ignore)) } } & $add $L.smoke_ready ('bsave \"world.bin\" 0 {0:x4} {1:x4}' -f $L.world_tiles, ($L.world_tiles + 3999)) 0 & $add $L.key_row_cursor 'r a = ff' 0 & $add $L.key_row_lshift 'r a = ff' 0 & $add $L.key_row_rshift 'r a = ff' 0 & $add $L.key_row_space 'r a = ff' 0 foreach ($step in $sc.schedule) { # later checkpoints run after earlier ones at the same address & $add $L.key_row_cursor "r a = $($step.cursor)" $step.at & $add $L.key_row_lshift "r a = $($step.lshift)" $step.at } $viewCheckpoint = $n + 1 & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.view_cx, ($L.view_cx + 3)) 0 & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.map_d011, ($L.map_d016)) 0 & $add $L.people_moved ('m {0:x4} {0:x4}' -f $L.visible) 0 & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.people_cx, ($L.people_cx + 79)) 0 & $add $L.people_moved 'bsave \"bitmap-a.bin\" 0 6000 7a3f' 0 & $add $L.people_moved 'bsave \"bitmap-b.bin\" 0 a000 ba3f' 0 & $add $L.people_moved 'bsave \"screen-a.bin\" 0 4000 4347' 0 & $add $L.people_moved 'bsave \"screen-b.bin\" 0 8000 8347' 0 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 -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() 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 $keysFor = { param($i) $k = @{ cursor = $NONE; lshift = $NONE }; foreach ($s in $sc.schedule) { if ($i -ge $s.at) { $k = $s } }; $k } 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 ($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 ([Math]::Abs($dx) -gt 1 -or [Math]::Abs($dy) -gt 1) { $failures.Add("frame ${i}: view jumped by ($dx,$dy)") } if ($dx -ne 0 -or $dy -ne 0) { $steps++ } if ($f.clock - $p.clock -gt 1.5 * 17095) { $late++ } # Keys read in frame i-2 drive the step applied in frame i-1's vertical blank... the # key row hooks count from the first read; allow two frames of latency. $k = & $keysFor ($i - 2) if ($i -ge 2 -and $k.cursor -ne $NONE) { $wantX = if (([Convert]::ToByte($k.cursor, 16) -band 0x04) -eq 0) { if ($k.lshift -eq $SHIFT) { -1 } else { 1 } } else { 0 } $wantY = if (([Convert]::ToByte($k.cursor, 16) -band 0x80) -eq 0) { if ($k.lshift -eq $SHIFT) { -1 } else { 1 } } else { 0 } $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 ($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 frame: shown buffer = fresh render at the view origin, except dot bytes. $last = $frames[$frames.Count - 1] $suffix = @('a', 'b')[$last.visible] $bitmap = [IO.File]::ReadAllBytes((Join-Path $dir "bitmap-$suffix.bin")) $screen = [IO.File]::ReadAllBytes((Join-Path $dir "screen-$suffix.bin")) $world = [IO.File]::ReadAllBytes((Join-Path $dir 'world.bin')) $dotBytes = @{} for ($i = 0; $i -lt 16; $i++) { $col = $last.people[$i] - $last.cx; $row = $last.people[32 + $i] - $last.cy 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 } } $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] -and -not $dotBytes.ContainsKey($o)) { $badBitmap++ } } } } 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") } $summary = "{0}: {1} frames, {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 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