|
- <#
- .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
- restart stop on a cell edge, then start again from rest in each direction (left,
- right, up, down): the view must leave the edge (a held key once never
- moved it there)
- tour (-Long only) about 5 minutes: six laps over the whole world, alternately
- row by row (right, down, left, ...; then diagonally up-left home) and
- column by column (down, right, up, ...), with still stops between legs.
- At the end of every stop both buffers are saved and checked like the
- final frame (people aligned with the terrain, no trails: R5.3, R3.2).
-
- 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
- - in every frame the shown buffer is complete (buf_ready) and built for the view's
- cell (buf_cx, buf_cy); a half-rebuilt buffer on screen tears the top of the map
- - the main loop never misses a frame and the people update ends before raster 55
- - nothing is stored into the shown buffer's map screen RAM or bitmap while the map
- is displayed (raster 55-210): such a store tears the picture
- - 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
- - frame budget (from the CPU clock at frame_start, people_moved, frame_done, the
- raster interrupt's entry $FF48 and exit $EA81, start_rebuild and rebuild_ready):
- reported per scenario: frame start to frame start (17,095 cycles give or take the
- wait loop's exit point), the vertical blank work (scroll step, flip, people update),
- the main loop's busy and idle cycles, the interrupt cycles, and how many frames each
- hidden-buffer rebuild took (assumption A4: 8 frames allow 1 pixel per frame).
-
- Exit codes: 0 = pass, 1 = fail, 2 = a tool is missing.
-
- .EXAMPLE
- pwsh -File tools/check-scroll.ps1
- pwsh -File tools/check-scroll.ps1 -Long # also the 5-minute tour (several minutes)
- pwsh -File tools/check-scroll.ps1 -Long -ParseOnly -NoBuild # re-check the last run's logs
- #>
- [CmdletBinding()]
- param([switch]$NoBuild, [switch]$Long, [switch]$ParseOnly, [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', 'buf_cx', 'buf_cy', 'buf_ready', 'view_fx', 'view_cy', 'view_fy',
- 'frame_start', 'frame_done', 'start_rebuild', 'rebuild_ready',
- 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space') { $L[$s] = Get-Label $target $s }
- $IRQ_ENTRY = 0xFF48; $IRQ_EXIT = 0xEA81 # KERNAL IRQ entry and the exit our handler jumps to
- $FRAME_CYCLES = 17095
-
- # 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 = 36000000; 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 = 18000000; 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 = 18000000; 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 }) }
- @{ name = 'restart'; cycles = 30000000; phases = @(
- @{ cursor = $NONE; lshift = $NONE; until = (Frame 30) }
- @{ cursor = $RIGHTK; lshift = $NONE; until = (View 10 0 -1 0) } # stop on a cell edge
- @{ cursor = $NONE; lshift = $NONE; until = (Frame 160) }
- @{ cursor = $RIGHTK; lshift = $SHIFT; until = (View 8 0 -1 0) } # left from rest at fx 0
- @{ cursor = $NONE; lshift = $NONE; until = (Frame 250) }
- @{ cursor = $RIGHTK; lshift = $NONE; until = (View 10 0 -1 0) } # right from rest at fx 0
- @{ cursor = $DOWN; lshift = $NONE; until = (View -1 0 6 0) }
- @{ cursor = $NONE; lshift = $NONE; until = (Frame 120) }
- @{ cursor = $DOWN; lshift = $SHIFT; until = (View -1 0 4 0) } # up from rest at fy 0
- @{ cursor = $NONE; lshift = $NONE; until = (Frame 220) }
- @{ cursor = $DOWN; lshift = $NONE; until = (View -1 0 6 0) } # down from rest at fy 0
- @{ cursor = $NONE; lshift = $NONE }) }
- )
- if ($Long) {
- $R = @{ cursor = $RIGHTK; lshift = $NONE }; $Lf = @{ cursor = $RIGHTK; lshift = $SHIFT }
- $D = @{ cursor = $DOWN; lshift = $NONE }; $U = @{ cursor = $DOWN; lshift = $SHIFT }
- $UL = @{ cursor = $RIGHT_DOWN; lshift = $SHIFT }
- $tour = [System.Collections.Generic.List[hashtable]]::new()
- $leg = { param($keys, $until) $tour.Add(@{ cursor = $keys.cursor; lshift = $keys.lshift; until = $until }) }
- $stop = { $tour.Add(@{ cursor = $NONE; lshift = $NONE; until = (Frame ((61 * $tour.Count + 17) % 256)); snap = $true }) }
- $tour.Add(@{ cursor = $NONE; lshift = $NONE; until = (Frame 30) })
- for ($lap = 0; $lap -lt 6; $lap++) {
- if ($lap % 2 -eq 0) { # rows: ends at the top-left
- foreach ($row in 5, 10, 15, 20, 25) {
- $right = (($row / 5) % 2) -eq 1
- if ($right) { & $leg $R (View 42 0 -1 0) } else { & $leg $Lf (View 0 0 -1 0) }
- & $leg $D (View -1 0 $row 0)
- if ($row % 10 -eq 0) { & $stop }
- }
- & $leg $Lf (View 0 0 -1 0); & $leg $D (View -1 0 30 4); & $leg $R (View 42 0 -1 0); & $stop
- & $leg $UL (View 0 0 0 0); & $stop
- } else { # columns: ends at the top-left
- foreach ($col in 8, 16, 24, 32, 42) {
- $down = (($col -eq 8) -or ($col -eq 24) -or ($col -eq 42))
- if ($down) { & $leg $D (View -1 0 30 4) } else { & $leg $U (View -1 0 0 0) }
- & $leg $R (View $col 0 -1 0)
- if ($col -eq 16 -or $col -eq 42) { & $stop }
- }
- & $leg $U (View -1 0 0 0); & $leg $Lf (View 0 0 -1 0); & $stop
- }
- }
- $tour.Add(@{ cursor = $NONE; lshift = $NONE })
- $scenarios += @{ name = 'tour'; cycles = 400000000; timeout = 3600; phases = $tour.ToArray(); maxStalls = 6 * $tour.Count }
- }
-
- $root = Get-RepoPath 'build/check-scroll'
- if ((Test-Path -LiteralPath $root) -and -not $ParseOnly) { Remove-Item -LiteralPath $root -Recurse -Force }
- $failedScenarios = 0
- foreach ($sc in $scenarios) {
- $dir = Join-Path $root $sc.name
- if (-not $ParseOnly) { 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)) }
- }
- # Snapshots at the end of each "snap" stop: before the phase changes, so they run in the same hit.
- $saves = @('bitmap-a.bin 6000 7a3f', 'bitmap-b.bin a000 ba3f', 'screen-a.bin 4000 4347', 'screen-b.bin 8000 8347')
- for ($ph = 0; $ph -lt $lastPhase; $ph++) {
- if (-not $sc.phases[$ph].snap) { continue }
- foreach ($save in $saves) {
- $f = $save -split ' '
- & $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)
- }
- }
- 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} {1:x4}' -f $L.buf_cx, ($L.buf_cx + 1)) $null
- & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.buf_cy, ($L.buf_cy + 1)) $null
- & $add $L.people_moved ('m {0:x4} {1:x4}' -f $L.buf_ready, ($L.buf_ready + 1)) $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 $saves) {
- $f = $save -split ' '
- & $add $L.people_moved ('bsave \"{0}\" 0 {1} {2}' -f $f[0], $f[1], $f[2]) @(, @($PHASE, $lastPhase))
- }
- # Stores into the shown buffer's map (screen RAM rows 0-20, bitmap) while the map is displayed
- # (raster 55-210) would tear the picture. Armed at smoke_ready, so start-up is not counted.
- $firstStore = $n + 1
- foreach ($range in @(@(0, 0x4000, 0x4347), @(0, 0x6000, 0x7a3f), @(1, 0x8000, 0x8347), @(1, 0xa000, 0xba3f))) {
- $n++
- $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]))
- $mon.Add("disable $n")
- }
- $lastStore = $n
- for ($c = $firstStore; $c -le $lastStore; $c++) { & $add $L.smoke_ready "enable $c" $null }
- # Timing events: a trace without a command prints the raster position and the CPU clock.
- $events = @{}
- foreach ($ev in @(@('start', $L.frame_start), @('done', $L.frame_done), @('irq', $IRQ_ENTRY), @('irqexit', $IRQ_EXIT),
- @('rbstart', $L.start_rebuild), @('rbready', $L.rebuild_ready))) {
- $n++; $mon.Add(('trace exec ${0:x4}' -f $ev[1])); $events[$n] = $ev[0]
- }
- if (-not $ParseOnly) {
- 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')
- $limit = if ($sc.timeout) { [Math]::Max($sc.timeout, $TimeoutSeconds) } else { $TimeoutSeconds }
- if (-not $p.WaitForExit($limit * 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()
- $timeline = [System.Collections.Generic.List[object]]::new() # (kind, clock, line)
- $cur = $null; $want = $null; $wantLine = 0
- foreach ($line in [IO.File]::ReadLines((Join-Path $dir 'monitor.log'), [Text.Encoding]::Latin1)) {
- if ($line.StartsWith('#') -and $line -match '^#(\d+) \(Trace\s+exec [0-9a-fA-F]+\)\s+(\d+)/') {
- $num = [int]$Matches[1]
- if ($num -eq $viewCheckpoint) {
- $cur = @{ line = [int]$Matches[2]; people = [System.Collections.Generic.List[byte]]::new() }; $frames.Add($cur); $want = 'view'; continue
- }
- if ($events.ContainsKey($num)) { $want = $events[$num]; $wantLine = [int]$Matches[2] }
- continue
- }
- if ($want -and $line -match '^\.C:.*\s(\d+)\s*$') {
- if ($want -eq 'view') { $cur.clock = [long]$Matches[1]; $timeline.Add(@('view', $cur.clock, $cur.line)) }
- else { $timeline.Add(@($want, [long]$Matches[1], $wantLine)) }
- $want = $null; 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 -eq $L.buf_cx) { $cur.bufcx = $bytes[0..1] }
- elseif ($addr -eq $L.buf_cy) { $cur.bufcy = $bytes[0..1] }
- elseif ($addr -eq $L.buf_ready) { $cur.ready = $bytes[0..1] }
- 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) } } }
- }
- # The run stops at a cycle limit, possibly while the last frame was being logged.
- $fields = 'cx', 'd011', 'visible', 'pending', 'bufcx', 'bufcy', 'ready', 'phase', 'clock'
- while ($frames.Count -gt 0) {
- $lf = $frames[$frames.Count - 1]
- if ($lf.people.Count -eq 80 -and -not @($fields | Where-Object { $null -eq $lf[$_] })) { break }
- $frames.RemoveAt($frames.Count - 1)
- }
- $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") }
- $tears = @(Select-String -LiteralPath (Join-Path $dir 'monitor.log') -Pattern '^#(\d+) \(Trace\s+store ([0-9a-fA-F]{4})\)\s+(\d+)/' |
- Where-Object { [int]$_.Matches[0].Groups[1].Value -ge $firstStore -and [int]$_.Matches[0].Groups[1].Value -lt $firstStore + 4 })
- if ($tears.Count -gt 0) {
- $t = $tears[0].Matches[0].Groups
- $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))")
- }
- $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)) }
- $v = $f.visible
- if (-not $f.ready[$v] -or $f.bufcx[$v] -ne $f.cx -or $f.bufcy[$v] -ne $f.cy) {
- $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))")
- }
- 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) { if ($late -eq 0) { $firstLate = "first at frame $i, phase $($f.phase), view ($vx,$vy) from ($($p.vx),$($p.vy))" }; $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), $firstLate") }
- 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)") }
- }
- $allowedStalls = if ($sc.maxStalls) { $sc.maxStalls } else { $MaxStallFrames }
- if ($stalls -gt $allowedStalls) { $failures.Add("$stalls stall frames (allowed $allowedStalls)") }
-
- # Frame budget from the timeline. Events before the first frame_start (start-up) are skipped.
- $fr = $null; $budget = [System.Collections.Generic.List[hashtable]]::new(); $irqAt = $null
- $rebuilds = [System.Collections.Generic.List[int]]::new(); $abandoned = 0; $rbOpen = $null
- foreach ($e in $timeline) {
- switch ($e[0]) {
- 'start' { if ($fr) { $fr.period = $e[1] - $fr.start }; $fr = @{ start = $e[1]; irq = 0 }; $budget.Add($fr) }
- 'view' { if ($fr) { $fr.vblank = $e[1] - $fr.start; $fr.viewLine = $e[2] } }
- 'done' { if ($fr) { $fr.busy = $e[1] - $fr.start; $fr.doneLine = $e[2] } }
- 'irq' { $irqAt = $e[1] }
- 'irqexit' { if ($fr -and $null -ne $irqAt) { $fr.irq += $e[1] - $irqAt + 7 + 22 }; $irqAt = $null } # + interrupt entry, + exit code
- 'rbstart' { if ($fr) { if ($null -ne $rbOpen) { $abandoned++ }; $rbOpen = $budget.Count } }
- 'rbready' { if ($fr -and $null -ne $rbOpen) { $rebuilds.Add($budget.Count - $rbOpen + 1); $rbOpen = $null } }
- }
- }
- $full = @($budget | Where-Object { $null -ne $_.period -and $null -ne $_.busy -and $null -ne $_.vblank })
- if ($full.Count -lt 100) { $failures.Add("only $($full.Count) frames with complete timing") }
- $periods = $full | ForEach-Object { $_.period } | Measure-Object -Minimum -Maximum
- $stat = { param($key) $v = @($full | ForEach-Object { $_[$key] }); @{ max = ($v | Measure-Object -Maximum).Maximum; avg = [int](($v | Measure-Object -Average).Average) } }
- $sv = & $stat 'vblank'; $sb = & $stat 'busy'; $si = & $stat 'irq'
- $idle = @($full | ForEach-Object { $_.period - $_.busy }); $minIdle = ($idle | Measure-Object -Minimum).Minimum
- $maxDone = ($full | ForEach-Object { $_.doneLine } | Measure-Object -Maximum).Maximum
- $rbMax = if ($rebuilds.Count) { ($rebuilds | Measure-Object -Maximum).Maximum } else { 0 }
- $rbAvg = if ($rebuilds.Count) { [Math]::Round(($rebuilds | Measure-Object -Average).Average, 1) } else { 0 }
- $rbHist = ($rebuilds | Group-Object | Sort-Object { [int]$_.Name } | ForEach-Object { "$($_.Name):$($_.Count)" }) -join ' '
- $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}; " +
- "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,
- $rebuilds.Count, $rbMax, $rbAvg, $abandoned, $periods.Minimum, $periods.Maximum, $rbHist
-
- # 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([string]$prefix, [int]$buf, $at, $people, [string]$what)
- $suffix = @('a', 'b')[$buf]
- $bitmap = [IO.File]::ReadAllBytes((Join-Path $dir "${prefix}bitmap-$suffix.bin"))
- $screen = [IO.File]::ReadAllBytes((Join-Path $dir "${prefix}screen-$suffix.bin"))
- $dots = @{}
- for ($i = 0; $i -lt 16; $i++) {
- $col = $people[$i] - $at.cx; $row = $people[32 + $i] - $at.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
- # Only cells inside the world: at the bottom edge row 21, at the right edge (view cell 42) the
- # 39th column lie outside it; neither is ever visible there (fine scroll is 0 at those edges).
- $rows = [Math]::Min(21, 50 - $at.cy); $cols = [Math]::Min(39, 80 - $at.cx)
- for ($r = 0; $r -lt $rows; $r++) {
- for ($c = 0; $c -lt $cols; $c++) {
- $t = $world[($at.cy + $r) * 80 + $at.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("${prefix}buffer $suffix ($what) at view cell ($($at.cx),$($at.cy)) differs from the world plus dots: $badBitmap bitmap bytes, $badScreen colour bytes") }
- }
- $checkFrame = {
- param([string]$prefix, [int]$i)
- $at = $frames[$i]; $prev = $frames[$i - 1]
- if ($at.pending) {
- & $checkBuffer $prefix (1 - $at.visible) $at $at.people 'shown next frame'
- if ($prev.cx -eq $at.cx -and $prev.cy -eq $at.cy) { & $checkBuffer $prefix $at.visible $at $prev.people 'shown, previous dots' }
- } else {
- & $checkBuffer $prefix $at.visible $at $at.people 'shown'
- }
- }
- 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') }
- # Snapshots: saved in the hit that ended stop k, which is logged as the first frame of phase k+1.
- $snaps = 0
- for ($ph = 0; $ph -lt $lastPhase; $ph++) {
- if (-not $sc.phases[$ph].snap) { continue }
- $i = -1
- 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 } }
- 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 }
- & $checkFrame "snap$ph-" $i
- $snaps++
- }
-
- $perPhase = if ($phaseFrames.Count -le 12) { $phaseFrames -join '/' } else { "$($phaseFrames.Count) phases" }
- $snapText = if ($snaps) { ", $snaps stops checked byte for byte" } else { '' }
- $summary = "{0}: {1} frames ({9}), {2} moving, {3} stall frames, update done by line {4}, final view ({5},{6}) cell ({7},{8}){10}" -f `
- $sc.name, $frames.Count, $steps, $stalls, $lastLine, $last.vx, $last.vy, $last.cx, $last.cy, $perPhase, $snapText
- 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"
- }
- Write-Host " $budgetText"
- }
- 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
|