|
- <#
- .SYNOPSIS
- Run the C64 build headless in VICE and check that the 16 people move, never
- stand on water or stone, and are redrawn in both buffers without trails.
-
- .DESCRIPTION
- Starts x64sc (NTSC, warp, private config) like tools/smoke.ps1. Monitor tracepoints:
- - at smoke_ready: save the world tiles and both clean bitmaps (no people yet);
- - at people_moved (once per frame, after drawing into the hidden buffer and
- before the flip): print the people table and the visible-buffer flag to the
- monitor log, and save both bitmaps (each frame overwrites the last).
- VICE quits after -Cycles cycles. The script then checks every logged frame:
- 16 people inside the 80x50 world, every one on a walkable tile (not water or stone)
- and on a pixel that is not blue or grey (the water pixels of bridge tiles),
- every step at most one pixel, and every person moving during the run. It checks
- that the main loop never misses a frame (x64sc NTSC frame = 263 lines x 65 =
- 17,095 cycles; a gap over 1.5 frames fails), and that
- in the final frame the hidden bitmap equals the clean map plus the people's current
- dots and the visible bitmap equals the clean map plus the previous frame's dots:
- no trails, no damaged terrain.
-
- Exit codes: 0 = pass, 1 = fail, 2 = a tool is missing.
-
- .EXAMPLE
- pwsh -File tools/check-people.ps1
- #>
- [CmdletBinding()]
- param(
- [int]$Cycles = 40000000,
- [switch]$NoBuild,
- [int]$TimeoutSeconds = 180
- )
- . (Join-Path $PSScriptRoot 'lib.ps1')
-
- $count = 16
- $width = 80
- $height = 50
- $blocking = 3..12 # STONE..WATER_SE in src/tiles.inc
- $waterColors = 6, 12 # blue and grey: no dot may stand on these pixels
-
- 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() }
- }
-
- 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-people'
- if (Test-Path -LiteralPath $dir) { Remove-Item -LiteralPath $dir -Recurse -Force }
- New-Item -ItemType Directory -Force -Path $dir | Out-Null
-
- $ready = Get-Label $target 'smoke_ready'
- $moved = Get-Label $target 'people_moved'
- $world = Get-Label $target 'world_tiles'
- $people = Get-Label $target 'people_cx'
- $visibleFlag = Get-Label $target 'visible'
- $tableEnd = $people + 5 * $count - 1
- $bitmapSize = 20 * 320 # the 20 map rows; rows 20-24 lie under the status text
- $buffers = @(0x6000, 0xA000) # bitmaps of buffers A and B (src/main.s)
- $monitor = [System.Collections.Generic.List[string]]::new()
- $monitor.Add('logname "monitor.log"')
- $monitor.Add('log on')
- $n = 0
- function Add-Trace([int]$Address, [string]$Command) {
- $script:n++
- $monitor.Add(('trace exec ${0:x4}' -f $Address))
- $monitor.Add(('command {0} "{1}"' -f $script:n, $Command))
- }
- Add-Trace $ready ('bsave \"world.bin\" 0 {0:x4} {1:x4}' -f $world, ($world + $width * $height - 1))
- Add-Trace $ready ('bsave \"clean-a.bin\" 0 {0:x4} {1:x4}' -f $buffers[0], ($buffers[0] + $bitmapSize - 1))
- Add-Trace $ready ('bsave \"clean-b.bin\" 0 {0:x4} {1:x4}' -f $buffers[1], ($buffers[1] + $bitmapSize - 1))
- Add-Trace $moved ('m {0:x4} {1:x4}' -f $people, $tableEnd)
- $peopleCheckpoint = $n
- Add-Trace $moved ('m {0:x4} {0:x4}' -f $visibleFlag)
- Add-Trace $moved ('bsave \"final-a.bin\" 0 {0:x4} {1:x4}' -f $buffers[0], ($buffers[0] + $bitmapSize - 1))
- Add-Trace $moved ('bsave \"final-b.bin\" 0 {0:x4} {1:x4}' -f $buffers[1], ($buffers[1] + $bitmapSize - 1))
- # No key is held during this check: keys typed on the PC while VICE has focus
- # would otherwise reach the program (the arrow keys scroll the view).
- foreach ($row in 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space') { Add-Trace (Get-Label $target $row) 'r a = ff' }
- Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $monitor
- Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
-
- $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target)
- $vargs += @('-limitcycles', [string]$Cycles, '-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()
- $worldFile = Join-Path $dir 'world.bin'
- if (-not (Test-Path -LiteralPath $worldFile)) { Write-Host '[FAIL] Program never reached smoke_ready.' -ForegroundColor Red; exit 1 }
- $tiles = [IO.File]::ReadAllBytes($worldFile)
-
- # Monitor memory dumps look like ">C:1e4c 00 01 02 ... text". Collect bytes per dump.
- $logFile = @('monitor.log', 'vice.log') | ForEach-Object { Join-Path $dir $_ } |
- Where-Object { (Test-Path -LiteralPath $_) -and (Select-String -LiteralPath $_ -Pattern '^>C:' -Quiet) } | Select-Object -First 1
- if (-not $logFile) { Write-Host '[FAIL] No people table was logged (people_moved never reached?).' -ForegroundColor Red; exit 1 }
- $frames = [System.Collections.Generic.List[byte[]]]::new()
- $visibleLog = [System.Collections.Generic.List[int]]::new()
- $clocks = [System.Collections.Generic.List[long]]::new()
- $current = [System.Collections.Generic.List[byte]]::new()
- $wantClock = $false
- $lines = [System.Collections.Generic.List[int]]::new()
- foreach ($line in Get-Content -LiteralPath $logFile) {
- if ($line -match "^#$peopleCheckpoint \(Trace\s+exec [0-9a-fA-F]+\)\s+(\d+)/") { $lines.Add([int]$Matches[1]); $wantClock = $true; continue }
- if ($wantClock -and $line -match '^\.C:.*\s(\d+)\s*$') { $clocks.Add([long]$Matches[1]); $wantClock = $false; continue }
- # Hex column only: each byte is two digits and one space; the ASCII column
- # after it can itself look like hex (for example bytes $34 $42 print as "4B").
- if ($line -notmatch '^>C:([0-9a-fA-F]{4}) ((?:[0-9a-fA-F]{2} )+)') { continue }
- $addr = [Convert]::ToInt32($Matches[1], 16)
- $hexBytes = $Matches[2].Trim() -split ' '
- if ($addr -eq $visibleFlag) { $visibleLog.Add([Convert]::ToInt32($hexBytes[0], 16)); continue }
- if ($addr -eq $people) { $current.Clear() }
- foreach ($hex in $hexBytes) {
- if ($current.Count -lt 5 * $count) { $current.Add([Convert]::ToByte($hex, 16)) }
- }
- if ($current.Count -eq 5 * $count) { $frames.Add($current.ToArray()); $current.Clear() }
- }
- if ($frames.Count -lt 60) { Write-Host "[FAIL] Only $($frames.Count) frames logged." -ForegroundColor Red; exit 1 }
-
- $tileData = Get-TileData
- $bridgeFirst = $tileData.patterns.Count - 3 # BRIDGE_H..BRIDGE_X are the last tiles
- $onBridge = 0
- $first = $frames[0]
- $moves = [int[]]::new($count)
- $previous = $null
- foreach ($f in $frames) {
- for ($i = 0; $i -lt $count; $i++) {
- $cx = $f[$i]; $sx = $f[16 + $i]; $cy = $f[32 + $i]; $sy = $f[48 + $i]; $heading = $f[64 + $i]
- if ($cx -ge $width -or $cy -ge $height -or $sx -gt 3 -or $sy -gt 7 -or $heading -gt 3) {
- $failures.Add("person $i out of range: cell ($cx,$cy) pixel ($sx,$sy) dir $heading"); continue
- }
- $tile = $tiles[$cy * $width + $cx]
- if ($tile -in $blocking) { $failures.Add("person $i on blocking tile $tile at cell ($cx,$cy)") }
- # The pixel under the dot: %01 takes the high colour nibble, %10 the low one.
- $pixel = ($tileData.patterns[$tile][$sy] -shr (6 - 2 * $sx)) -band 3
- $color = switch ($pixel) { 1 { $tileData.colors[$tile] -shr 4 } 2 { $tileData.colors[$tile] -band 15 } default { -1 } }
- if ($color -in $waterColors) { $failures.Add("person $i on a colour $color pixel of tile $tile at cell ($cx,$cy) pixel ($sx,$sy)") }
- if ($tile -ge $bridgeFirst) { $onBridge++ }
- if ($previous) {
- $x0 = $previous[$i] * 4 + $previous[16 + $i]; $y0 = $previous[32 + $i] * 8 + $previous[48 + $i]
- $dx = [Math]::Abs($cx * 4 + $sx - $x0); $dy = [Math]::Abs($cy * 8 + $sy - $y0)
- if ($dx + $dy -gt 1) { $failures.Add("person $i jumped $dx,$dy pixels") }
- if ($dx + $dy -eq 1) { $moves[$i]++ }
- }
- }
- $previous = $f
- if ($failures.Count -gt 10) { break }
- }
- for ($i = 0; $i -lt $count; $i++) {
- if ($moves[$i] -eq 0) { $failures.Add("person $i never moved") }
- }
- # Main loop timing: one hook per NTSC frame (65 cycles x 263 lines on x64sc).
- # The hook comes after a variable amount of work, so gaps jitter around 17095.
- $frameCycles = 17095
- $late = 0
- for ($k = 1; $k -lt $clocks.Count; $k++) { if ($clocks[$k] - $clocks[$k - 1] -gt 1.5 * $frameCycles) { $late++ } }
- if ($clocks.Count -lt $frames.Count) { $failures.Add("only $($clocks.Count) hook clocks for $($frames.Count) frames") }
- if ($late -gt 0) { $failures.Add("the main loop missed $late frame(s)") }
- # The flip happens from line 251; the update must end before the visible area (line 51).
- $lastLine = ($lines | Where-Object { $_ -lt 200 } | Measure-Object -Maximum).Maximum
- if (@($lines | Where-Object { $_ -ge 51 }).Count -gt 0) { $failures.Add('a people update ran into the visible area (hook at raster line 51 or later)') }
-
- # Final frame: hidden buffer = clean + current dots, visible buffer = clean + previous dots.
- function Get-ExpectedBitmap([byte[]]$Clean, [byte[]]$Table) {
- $bitmap = [byte[]]$Clean.Clone()
- $masks = 0xC0, 0x30, 0x0C, 0x03
- for ($i = 0; $i -lt $count; $i++) {
- $cx = $Table[$i]; $cy = $Table[32 + $i]
- if ($cx -ge 39 -or $cy -ge 20) { continue } # 39 drawn columns; the dumps hold the 20 map rows
- $offset = $cy * 320 + $cx * 8 + $Table[48 + $i]
- $bitmap[$offset] = $bitmap[$offset] -bor $masks[$Table[16 + $i]]
- }
- , $bitmap
- }
- $clean = 'clean-a.bin', 'clean-b.bin' | ForEach-Object { , [IO.File]::ReadAllBytes((Join-Path $dir $_)) }
- $final = 'final-a.bin', 'final-b.bin' | ForEach-Object { , [IO.File]::ReadAllBytes((Join-Path $dir $_)) }
- # Column 39 is never drawn (never visible in 38-column mode): skip its bytes.
- $drawnByte = { param($b) ($b % 320) -lt 312 }
- for ($b = 0; $b -lt $bitmapSize; $b++) { if ((& $drawnByte $b) -and $clean[0][$b] -ne $clean[1][$b]) { $failures.Add('buffers A and B differ before people are drawn'); break } }
- $shown = $visibleLog[$visibleLog.Count - 1]
- $hiddenIndex = 1 - $shown
- $expectHidden = Get-ExpectedBitmap $clean[$hiddenIndex] $frames[$frames.Count - 1]
- $expectShown = Get-ExpectedBitmap $clean[$shown] $frames[$frames.Count - 2]
- $dots = 0
- foreach ($pair in @(@($final[$hiddenIndex], $expectHidden, 'hidden'), @($final[$shown], $expectShown, 'visible'))) {
- $bad = 0
- for ($b = 0; $b -lt $bitmapSize; $b++) {
- if ($pair[0][$b] -ne $pair[1][$b] -and ($b % 320) -lt 312) { $bad++ }
- }
- if ($bad -gt 0) { $failures.Add("final $($pair[2]) bitmap has $bad bytes that differ from clean map plus people") }
- }
- for ($b = 0; $b -lt $bitmapSize; $b++) { if ($final[$hiddenIndex][$b] -ne $clean[$hiddenIndex][$b] -and ($b % 320) -lt 312) { $dots++ } }
-
- $sectors = @(0..($count - 1) | ForEach-Object { [int]($first[$_] -ge 40) + 2 * [int]($first[32 + $_] -ge 25) } | Sort-Object -Unique)
- if ($sectors.Count -ne 4) { $failures.Add("people start in only $($sectors.Count) of 4 screens") }
-
- $seconds = [Math]::Round($frames.Count / 60.0, 1)
- Write-Host ("Buffer flips: {0} frames, {1} missed; people update done by raster line {2} at the latest. Final hidden buffer: {3}, {4} bytes carry dots." -f $clocks.Count, $late, $lastLine, ('A', 'B')[$hiddenIndex], $dots)
- Write-Host ("Logged {0} movement frames (~{1} s NTSC). Steps per person: {2}. Person-frames on bridges: {3}." -f $frames.Count, $seconds, ($moves -join ' '), $onBridge)
- if ($failures.Count -gt 0) {
- $failures | Select-Object -First 10 | ForEach-Object { Write-Host " $_" }
- Write-Host '[FAIL] people check' -ForegroundColor Red
- exit 1
- }
- Write-Host '[PASS] 16 people spread over 4 screens move one pixel at a time, never stand on water or stone (tiles or blue/grey bridge pixels); both buffers hold only the clean map plus the right dots; one flip per frame.' -ForegroundColor Green
- exit 0
|