<# .SYNOPSIS Check the map/status raster split of the C64 build headless in VICE. .DESCRIPTION Runs x64sc (NTSC, warp, private config) with monitor tracepoints: - at split_store (src/status.inc): logs the raster line and cycle where the three mode writes start, the map's y-scroll (map_d011) and the patched split delay, every frame; - on the key_row_* instructions (src/keys.inc): fakes "no key" (keys typed on the PC while a headless VICE has focus would otherwise reach the program), then holds CRSR DOWN from frame 30 until the view is at cell row 3, then SHIFT+CRSR DOWN until it is back at row 0, then no key, so every map y-scroll value (0-7) is split; - at people_moved: logs the view position, the people table with move_frame, and the people-in-view digits of text screen 1 in both banks. Then checks: - every split starts between line 210 cycle 53 and line 210 cycle 64, for every y-scroll value, and every y-scroll value 0-7 was seen. This window was measured at y-scroll 3 by sweeping the delay: starts at 210:40-51 damaged the last map line, 211:0-11 damaged the first text line, 210:50-211:1 were clean; - a split happens on every frame (no missed interrupt); - the status is updated every 4th frame (move_frame a multiple of 4, after people_moved): in the frame after each update, the digits in both banks equal the number of people whose dot is at least partly inside the visible map window (24-row, 38-column display; buffer pixel rows fy..fy+155 and hires pixel columns fx..fx+303), counted here from the people table and view of the updating frame; - in the exit screenshot, the 40 text lines (raster 211-250) contain only the background colour and white, all five status rows 20-24 are lit (row 23: the shown person's scores, R8), and the last map line (raster 210) uses no colour foreign to the map. This is a coarse check: in the delay sweep it caught damage from writes at 210:40-46 but not subtler damage, so the timing window above is the main guard. Exit codes: 0 = pass, 1 = fail, 2 = a tool is missing. .EXAMPLE pwsh -File tools/check-split.ps1 #> [CmdletBinding()] param([int]$Cycles = 20000000, [switch]$NoBuild, [int]$TimeoutSeconds = 180) . (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)." } 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 } $dir = Get-RepoPath 'build/check-split' if (Test-Path -LiteralPath $dir) { Remove-Item -LiteralPath $dir -Recurse -Force } New-Item -ItemType Directory -Force -Path $dir | Out-Null $L = @{} foreach ($sym in 'split_store', 'split_delay', 'people_moved', 'smoke_ready', 'people_cx', 'map_d011', 'move_frame', 'view_cx', 'view_cy', 'view_fy', 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space', 'key_row_p') { $L[$sym] = Get-Label $target $sym } $digits = @(0x4400, 0x8400) | ForEach-Object { $_ + 22 * 40 + 15 } # row 22, after "PEOPLE IN VIEW " $PHASE = 0xCFF0 # free RAM: scroll phase 0 still, 1 down, 2 up, 3 still $cond = { param($pairs) ($pairs | ForEach-Object { '(@cpu:${0:x4} == ${1:x2})' -f $_[0], $_[1] }) -join ' && ' } if ($L.move_frame -ne $L.people_cx + 80) { throw 'move_frame must follow the five people tables (src/people.inc)' } # Checkpoints 1-5 keep their numbers: the parser below relies on them. $mon = @( 'logname "monitor.log"', 'log on' ('trace exec ${0:x4}' -f $L.split_store), ('command 1 "m {0:x4} {0:x4}"' -f $L.map_d011) ('trace exec ${0:x4}' -f $L.people_moved), ('command 2 "m {0:x4} {1:x4}"' -f $L.view_cx, ($L.view_cx + 3)) ('trace exec ${0:x4}' -f $L.people_moved), ('command 3 "m {0:x4} {1:x4}"' -f $L.people_cx, ($L.people_cx + 80)) ('trace exec ${0:x4}' -f $L.people_moved), ('command 4 "m {0:x4} {1:x4}"' -f $digits[0], ($digits[0] + 1)) ('trace exec ${0:x4}' -f $L.people_moved), ('command 5 "m {0:x4} {1:x4}"' -f $digits[1], ($digits[1] + 1)) ('trace exec ${0:x4}' -f $L.split_store), ('command 6 "m {0:x4} {0:x4}"' -f ($L.split_delay + 1)) ('trace exec ${0:x4}' -f $L.smoke_ready), ('command 7 "> {0:x4} 00"' -f $PHASE) ) $n = 7 foreach ($row in 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space', 'key_row_p') { $n++; $mon += ('trace exec ${0:x4}' -f $L[$row]); $mon += ('command {0} "r a = ff"' -f $n) } $n++; $mon += ('trace exec ${0:x4} if {1}' -f $L.key_row_cursor, (& $cond @(, @($PHASE, 1)))); $mon += ('command {0} "r a = 7f"' -f $n) $n++; $mon += ('trace exec ${0:x4} if {1}' -f $L.key_row_cursor, (& $cond @(, @($PHASE, 2)))); $mon += ('command {0} "r a = 7f"' -f $n) $n++; $mon += ('trace exec ${0:x4} if {1}' -f $L.key_row_lshift, (& $cond @(, @($PHASE, 2)))); $mon += ('command {0} "r a = 7f"' -f $n) # Phase changes, latest first so one frame moves on at most one phase. $n++; $mon += ('trace exec ${0:x4} if {1}' -f $L.people_moved, (& $cond @(@($PHASE, 2), @($L.view_cy, 0), @($L.view_fy, 0)))); $mon += ('command {0} "> {1:x4} 03"' -f $n, $PHASE) $n++; $mon += ('trace exec ${0:x4} if {1}' -f $L.people_moved, (& $cond @(@($PHASE, 1), @($L.view_cy, 3), @($L.view_fy, 0)))); $mon += ('command {0} "> {1:x4} 02"' -f $n, $PHASE) $n++; $mon += ('trace exec ${0:x4} if {1}' -f $L.people_moved, (& $cond @(@($PHASE, 0), @($L.move_frame, 30)))); $mon += ('command {0} "> {1:x4} 01"' -f $n, $PHASE) 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]$Cycles, '-exitscreenshot', 'screen.png', '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target)) $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } } Write-Host "[c64] running x64sc headless for $Cycles cycles" $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); Write-Host '[FAIL] VICE did not exit in time.' -ForegroundColor Red; exit 1 } $failures = [System.Collections.Generic.List[string]]::new() $log = Join-Path $dir 'monitor.log' if (-not (Test-Path -LiteralPath $log)) { Write-Host '[FAIL] no monitor log' -ForegroundColor Red; exit 1 } # Split timing. $starts = [System.Collections.Generic.List[string]]::new() $splits = [System.Collections.Generic.List[hashtable]]::new() $split = $null $splitFrames = 0; $moveFrames = 0; $badCounts = 0; $checkedCounts = 0 $lastCheckpoint = 0 $frame = $null; $prev = $null # Independent count: a person is in view if any part of their dot is in the visible window. function Get-InView($f) { $count = 0 for ($i = 0; $i -lt 16; $i++) { $y = 8 * ($f.people[32 + $i] - $f.cy) + $f.people[48 + $i] - $f.fy $x = 8 * ($f.people[$i] - $f.cx) + 2 * $f.people[16 + $i] if ($y -ge 0 -and $y -le 155 -and $x + 1 -ge $f.fx -and $x -le $f.fx + 303) { $count++ } } $count } foreach ($line in Get-Content -LiteralPath $log) { if ($line -match '^#(\d+) \(Trace\s+exec [0-9a-fA-F]+\)\s+(\d+)/\$\w+,\s+(\d+)/') { $lastCheckpoint = [int]$Matches[1] if ($lastCheckpoint -eq 1) { $splitFrames++ $split = @{ line = [int]$Matches[2]; cycle = [int]$Matches[3] } $splits.Add($split) } if ($lastCheckpoint -eq 2) { $prev = $frame; $frame = @{ people = [System.Collections.Generic.List[byte]]::new(); digits = @() } } continue } if ($line -match '^>C:[0-9a-fA-F]{4} ((?:[0-9a-fA-F]{2} )+)') { $bytes = @($Matches[1].Trim() -split ' ' | ForEach-Object { [Convert]::ToByte($_, 16) }) if ($lastCheckpoint -eq 1 -and $null -ne $split) { $split.y = $bytes[0] -band 7 } if ($lastCheckpoint -eq 6 -and $null -ne $split) { $split.delay = $bytes[0] } if ($null -eq $frame) { continue } if ($lastCheckpoint -eq 2) { $frame.cx = $bytes[0]; $frame.fx = $bytes[1]; $frame.cy = $bytes[2]; $frame.fy = $bytes[3] } if ($lastCheckpoint -eq 3) { foreach ($b in $bytes) { if ($frame.people.Count -lt 81) { $frame.people.Add($b) } } } if ($lastCheckpoint -eq 4 -or $lastCheckpoint -eq 5) { $frame.digits += , (($bytes[0] - 0x30) * 10 + ($bytes[1] - 0x30)) } if ($lastCheckpoint -eq 5) { $moveFrames++ if ($null -ne $prev -and $prev.people.Count -eq 81 -and ($prev.people[80] -band 3) -eq 0) { $checkedCounts++ $want = Get-InView $prev if ($frame.digits[0] -ne $want -or $frame.digits[1] -ne $want) { $badCounts++ } } } } if ($failures.Count -gt 10) { break } } $monErrors = @(Select-String -LiteralPath $log -Pattern '^ERROR|not a valid checkpoint' | Select-Object -First 3) foreach ($e in $monErrors) { $failures.Add("monitor script error: $($e.Line.Trim())") } foreach ($sp in $splits) { $starts.Add("y$($sp.y) d$($sp.delay) $($sp.line):$($sp.cycle)") if ($sp.line -ne 210 -or $sp.cycle -lt 53 -or $sp.cycle -gt 64) { $failures.Add("split at y-scroll $($sp.y) started at line $($sp.line) cycle $($sp.cycle) (allowed 210:53-210:64)") } } $seenY = @($splits | ForEach-Object { $_.y } | Where-Object { $null -ne $_ } | Sort-Object -Unique) if ($seenY.Count -ne 8) { $failures.Add("splits seen only at y-scroll $($seenY -join ', ') (expected 0-7)") } if ($splitFrames -lt 100) { $failures.Add("only $splitFrames splits logged") } if ([Math]::Abs($splitFrames - $moveFrames) -gt 2) { $failures.Add("$splitFrames splits for $moveFrames frames: an interrupt was missed") } if ($checkedCounts -lt 50) { $failures.Add("only $checkedCounts status updates checked") } if ($badCounts -gt 0) { $failures.Add("people-in-view digits differed from the people in the visible window after $badCounts of $checkedCounts status updates") } # Screenshot: raster line r is at y = r - 28 in x64sc's NTSC screenshot. Add-Type -AssemblyName System.Drawing $shot = Join-Path $dir 'screen.png' if (-not (Test-Path -LiteralPath $shot)) { $failures.Add('no exit screenshot') } else { $bmp = [System.Drawing.Bitmap]::new($shot) try { $brown = $bmp.GetPixel(2, 2).ToArgb() $textColours = @{} $textLit = @{} for ($y = 211 - 28; $y -le 250 - 28; $y++) { for ($x = 0; $x -lt $bmp.Width; $x++) { $c = $bmp.GetPixel($x, $y).ToArgb() if ($c -ne $brown) { $textColours[$c] = 1; $textLit[[int][Math]::Floor(($y - 183) / 8)] = 1 } } } if ($textColours.Count -gt 1) { $failures.Add("status area shows $($textColours.Count) colours besides the background (expected white only)") } foreach ($row in 0..4) { if (-not $textLit.ContainsKey($row)) { $failures.Add("status row $($row + 20) is empty in the screenshot") } } # Map colours: background, white, grey, green, blue, black as rendered in the map area above. $mapColours = @{} for ($y = 51 - 28; $y -le 209 - 28; $y++) { for ($x = 0; $x -lt $bmp.Width; $x++) { $mapColours[$bmp.GetPixel($x, $y).ToArgb()] = 1 } } for ($x = 0; $x -lt $bmp.Width; $x++) { if (-not $mapColours.ContainsKey($bmp.GetPixel($x, 210 - 28).ToArgb())) { $failures.Add("last map line has a colour not used elsewhere in the map at x=$x"); break } } } finally { $bmp.Dispose() } } $distinct = ($starts | Group-Object | Sort-Object Name | ForEach-Object { "$($_.Name) x$($_.Count)" }) -join ', ' Write-Host "Split starts (y-scroll, delay, line:cycle): $distinct" Write-Host "Frames: $splitFrames splits, $moveFrames people updates; people-in-view digits wrong after $badCounts of $checkedCounts status updates." if ($failures.Count -gt 0) { $failures | Select-Object -First 10 | ForEach-Object { Write-Host " $_" } Write-Host '[FAIL] split check' -ForegroundColor Red exit 1 } Write-Host '[PASS] split writes land in the measured border window every frame at every y-scroll; status digits match; no foreign colours at the boundary.' -ForegroundColor Green exit 0