<# .SYNOPSIS Run the worn-paths test build headless in VICE and check walk counts, path tiles and both bitmap buffers (R7, M5.2). .DESCRIPTION Builds the PATH_TEST variant (build/-pathtest.prg: a path at 4 steps, back below 2, every count one less each 400 frames; see src/paths.inc) and runs it in x64sc (NTSC, warp, private config) with no keys held. Monitor tracepoints: - at smoke_ready: save the generated world; - at path_changed: log the new tile (register A), once per path that appears or fades; - at people_moved when move_frame is 0 (every 256 frames, each save overwriting the last): save the world, the walk counts, the change queue, both bitmaps and screens, the buffer origins and the people's saved-byte lists. Checks on the last save: - tiles: every cell is its generated tile, or the path tile for it (land, grass, trees only); water and stone have count 0 and never change - counts and paths agree: no land, grass or tree cell has count PATH_ON or more, no path has count below PATH_OFF (unless the change queue was full) - both buffers: every drawn cell (39 columns, 20 rows) has the colour and bitmap of its current tile, except cells still queued for that buffer; the only other pixels are people's dots (%11) in bytes listed in the buffer's saved-byte list, whose oldest entry holds the clean tile byte - paths appeared and faded during the run (path_changed hits with a path tile and with a terrain tile) Exit codes: 0 = pass, 1 = fail, 2 = a tool is missing. .EXAMPLE pwsh -File tools/check-paths.ps1 #> [CmdletBinding()] param( [int]$Cycles = 90000000, [switch]$NoBuild, [int]$TimeoutSeconds = 400 ) . (Join-Path $PSScriptRoot 'lib.ps1') $width = 80; $height = 50 # PATH_TEST thresholds: the first PATH_ON / PATH_OFF in src/paths.inc (the .ifdef PATH_TEST branch) $pathsSrc = Get-Content -LiteralPath (Get-RepoPath 'src/paths.inc') -Raw $PATH_ON = [int]([regex]::Match($pathsSrc, '(?m)^PATH_ON\s*=\s*(\d+)').Groups[1].Value) $PATH_OFF = [int]([regex]::Match($pathsSrc, '(?m)^PATH_OFF\s*=\s*(\d+)').Groups[1].Value) if ($PATH_ON -le 0 -or $PATH_OFF -le 0) { throw 'PATH_ON / PATH_OFF not found in src/paths.inc' } $LAND = 0; $TREE = 2; $STONE = 3; $WATER_SE = 12 $CHANGE_SLOTS = 8 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 = [System.Collections.Generic.List[object]]::new() $repeat = 1 foreach ($line in $block -split "`n") { if ($line -match '^\s*\.repeat\s+(\d+)') { $repeat = [int]$Matches[1]; continue } if ($line -match '^\s*\.endrepeat') { $repeat = 1; continue } if ($line -match '^\s*\.byte\s+([^;\r\n]+)') { $vals = @($Matches[1] -split ',' | ForEach-Object { [Convert]::ToByte($_.Trim().TrimStart('$'), 16) }) for ($r = 0; $r -lt $repeat; $r++) { $patterns.Add($vals) } } } $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) } } } } $names = @([regex]::Matches(($src.Substring($src.IndexOf('.enum'), $src.IndexOf('.endenum') - $src.IndexOf('.enum'))), '(?m)^\s+([A-Z_]+)') | ForEach-Object { $_.Groups[1].Value }) @{ patterns = $patterns.ToArray(); colors = $colors.ToArray(); ids = @{} + (& { $h = @{}; for ($i = 0; $i -lt $names.Count; $i++) { $h[$names[$i]] = $i }; $h }) } } try { $project = Get-ProjectConfig $target = Get-ProjectTargets $project 'c64' Set-TargetVariant $target 'PATH_TEST' 'pathtest' if (-not $NoBuild) { Invoke-TargetBuild $project $target @{} } $emu = Get-ToolPath $project $target.emulator.binary } catch [MissingToolException] { Write-Host $_.Exception.Message -ForegroundColor Yellow exit 2 } $L = @{} foreach ($line in Get-Content -LiteralPath (Get-RepoPath $target.build.labels)) { $parts = $line -split '\s+' if ($parts.Count -ge 3) { $L[$parts[2].TrimStart('.')] = [Convert]::ToInt32($parts[1], 16) } } foreach ($s in 'smoke_ready', 'people_moved', 'path_changed', 'world_tiles', 'walk_counts', 'chg_count', 'chg_done', 'move_frame', 'buf_cx', 'buf_cy', 'buf_ready', 'buf_rows', 'drawn_count', 'drawn_low_a') { if (-not $L.ContainsKey($s)) { Write-Host "[FAIL] label $s not found" -ForegroundColor Red; exit 1 } } $tileData = Get-TileData $ids = $tileData.ids $PATH_LAND = $ids['PATH_LAND']; $PATH_TREE = $ids['PATH_TREE'] $dir = Get-RepoPath 'build/check-paths' if (Test-Path -LiteralPath $dir) { Remove-Item -LiteralPath $dir -Recurse -Force } 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 function Add-Trace([int]$Address, [string]$Command, [string]$Condition) { $script:n++ $line = 'trace exec ${0:x4}' -f $Address if ($Condition) { $line += " if $Condition" } $mon.Add($line) if ($Command) { $mon.Add(('command {0} "{1}"' -f $script:n, $Command)) } } $bsave = { param([string]$name, [int]$from, [int]$len) 'bsave \"{0}\" 0 {1:x4} {2:x4}' -f $name, $from, ($from + $len - 1) } Add-Trace $L.smoke_ready (& $bsave 'start-world.bin' $L.world_tiles 4000) $null Add-Trace $L.path_changed $null $null $changeCheckpoint = $n $every = '(@cpu:${0:x4} == $00)' -f $L.move_frame Add-Trace $L.people_moved (& $bsave 'world.bin' $L.world_tiles 4000) $every Add-Trace $L.people_moved (& $bsave 'counts.bin' $L.walk_counts 4000) $every Add-Trace $L.people_moved (& $bsave 'queue.bin' $L.chg_count ($L.chg_done + $CHANGE_SLOTS - $L.chg_count)) $every Add-Trace $L.people_moved (& $bsave 'bitmap-a.bin' 0x6000 6400) $every Add-Trace $L.people_moved (& $bsave 'bitmap-b.bin' 0xA000 6400) $every Add-Trace $L.people_moved (& $bsave 'screen-a.bin' 0x4000 800) $every Add-Trace $L.people_moved (& $bsave 'screen-b.bin' 0x8000 800) $every $bufVars = $L.buf_rows + 2 - $L.buf_cx # buf_cx .. buf_rows (2 bytes each; read by label below) Add-Trace $L.people_moved (& $bsave 'bufs.bin' $L.buf_cx $bufVars) $every Add-Trace $L.people_moved (& $bsave 'counts2.bin' $L.drawn_count 2) $every # Saved-byte lists (src/people.inc): six pages from drawn_low_a: low, high, byte for buffer A, then B. Add-Trace $L.people_moved (& $bsave 'lists.bin' $L.drawn_low_a (6 * 256)) $every foreach ($row in 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space', 'key_row_p') { Add-Trace $L[$row] 'r a = ff' $null } 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, '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target)) $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } } Write-Host "[c64] running the PATH_TEST build 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 } $read = { param($name) $f = Join-Path $dir $name; if (Test-Path -LiteralPath $f) { , [IO.File]::ReadAllBytes($f) } else { $null } } $start = & $read 'start-world.bin'; $world = & $read 'world.bin'; $counts = & $read 'counts.bin'; $queue = & $read 'queue.bin' if (-not $start -or -not $world -or -not $counts -or -not $queue) { Write-Host '[FAIL] the program did not reach the save points (smoke_ready, people_moved at move_frame 0).' -ForegroundColor Red; exit 1 } $bitmaps = @((& $read 'bitmap-a.bin'), (& $read 'bitmap-b.bin')); $screens = @((& $read 'screen-a.bin'), (& $read 'screen-b.bin')) $bufs = & $read 'bufs.bin'; $drawnCount = & $read 'counts2.bin' $lists = & $read 'lists.bin' $failures = [System.Collections.Generic.List[string]]::new() # Change events: the trace's register line gives A = the new tile. $logFile = @('monitor.log', 'vice.log') | ForEach-Object { Join-Path $dir $_ } | Where-Object { Test-Path -LiteralPath $_ } | Select-Object -First 1 $appeared = 0; $faded = 0; $want = $false foreach ($line in Get-Content -LiteralPath $logFile) { if ($line -match "^#$changeCheckpoint \(Trace\s+exec") { $want = $true; continue } if ($want -and $line -match '^\.C:.*\bA:([0-9A-Fa-f]{2})') { $a = [Convert]::ToInt32($Matches[1], 16) if ($a -ge $PATH_LAND -and $a -le $PATH_TREE) { $appeared++ } else { $faded++ } $want = $false } } if ($appeared -eq 0) { $failures.Add('no path appeared during the run') } if ($faded -eq 0) { $failures.Add('no path faded during the run') } # Queue: chg_count, new_cell, chg_tile, chg_ty, chg_cx[8], chg_cy[8], chg_done[8]. $qCount = $queue[0]; $qFull = $qCount -ge $CHANGE_SLOTS $pending = @(@{}, @{}) for ($i = 0; $i -lt $qCount; $i++) { $cx = $queue[4 + $i]; $cy = $queue[4 + $CHANGE_SLOTS + $i]; $done = $queue[4 + 2 * $CHANGE_SLOTS + $i] for ($b = 0; $b -lt 2; $b++) { if (-not ($done -band (1 -shl $b))) { $pending[$b]["$cx,$cy"] = 1 } } } # Tiles and counts. $paths = 0 for ($i = 0; $i -lt 4000; $i++) { $t = $world[$i]; $o = $start[$i]; $c = $counts[$i]; $x = $i % $width; $y = [math]::Floor($i / $width) if ($o -ge $STONE -and $o -le $WATER_SE) { if ($t -ne $o) { $failures.Add("water or stone cell ($x,$y) changed from $o to $t") } if ($c -ne 0) { $failures.Add("water or stone cell ($x,$y) has walk count $c") } continue } if ($t -ge $PATH_LAND -and $t -le $PATH_TREE) { $paths++ if ($t - $PATH_LAND + $LAND -ne $o) { $failures.Add("path tile $t at ($x,$y) over generated tile $o") } if ($c -lt $PATH_OFF -and -not $qFull) { $failures.Add("path at ($x,$y) has count $c (below $PATH_OFF)") } } elseif ($t -ne $o) { $failures.Add("cell ($x,$y) changed from $o to $t") } elseif ($c -ge $PATH_ON -and -not $qFull) { $failures.Add("cell ($x,$y) has count $c but is not a path") } } # Buffers: origin, ready flag and rows built, each read at its label (buf_cx .. buf_rows). $dotsChecked = 0 for ($b = 0; $b -lt 2; $b++) { $at = { param($name) $bufs[$L[$name] - $L.buf_cx + $b] } $ox = & $at 'buf_cx'; $oy = & $at 'buf_cy'; $ready = & $at 'buf_ready'; $rows = [math]::Min(20, (& $at 'buf_rows')) if (-not $ready) { $failures.Add("buffer $b is not complete at the save"); continue } $base = @(0x6000, 0xA000)[$b] $listed = @{} # address offset -> oldest saved byte for ($k = $drawnCount[$b] - 1; $k -ge 0; $k--) { $page = 3 * 256 * $b + $k $addr = $lists[$page] + 256 * $lists[$page + 256] - $base $listed[$addr] = $lists[$page + 512] # ends with the oldest entry } $badCells = 0; $badDots = 0 for ($r = 0; $r -lt $rows; $r++) { for ($col = 0; $col -lt 39; $col++) { $wx = $ox + $col; $wy = $oy + $r if ($wx -ge $width -or $wy -ge $height) { continue } if ($pending[$b].ContainsKey("$wx,$wy")) { continue } $t = $world[$wy * $width + $wx] if ($screens[$b][$r * 40 + $col] -ne $tileData.colors[$t]) { $badCells++; continue } for ($k = 0; $k -lt 8; $k++) { $off = $r * 320 + $col * 8 + $k $want = $tileData.patterns[$t][$k]; $have = $bitmaps[$b][$off] if ($have -eq $want) { if ($listed.ContainsKey($off) -and $listed[$off] -ne $want) { $badDots++ }; continue } # Pairs that differ must be dots (%11) in a listed byte whose oldest saved byte is clean. for ($q = 0; $q -lt 4; $q++) { $sh = 6 - 2 * $q if ((($have -shr $sh) -band 3) -ne (($want -shr $sh) -band 3) -and (($have -shr $sh) -band 3) -ne 3) { $badCells++ } } if (-not $listed.ContainsKey($off) -or $listed[$off] -ne $want) { $badDots++ } else { $dotsChecked++ } } } } if ($badCells -gt 0) { $failures.Add("buffer $('AB'[$b]): $badCells cells or pixels differ from the current world") } if ($badDots -gt 0) { $failures.Add("buffer $('AB'[$b]): $badDots dot bytes without a matching clean saved byte") } } Write-Host ("Paths appeared {0} times and faded {1} times; {2} path cells at the last save; change queue {3}/{4}; {5} dot bytes checked." -f $appeared, $faded, $paths, $qCount, $CHANGE_SLOTS, $dotsChecked) if ($failures.Count -gt 0) { $failures | Select-Object -First 12 | ForEach-Object { Write-Host " $_" } Write-Host '[FAIL] paths check' -ForegroundColor Red exit 1 } Write-Host '[PASS] paths appear and fade with the walk counts, only on land, grass and trees; both buffers show the current world plus the dots.' -ForegroundColor Green exit 0