You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
13KB

  1. <#
  2. .SYNOPSIS
  3. Run the worn-paths test build headless in VICE and check walk counts, path tiles and
  4. both bitmap buffers (R7, M5.2).
  5. .DESCRIPTION
  6. Builds the PATH_TEST variant (build/<name>-pathtest.prg: a path at 4 steps, back below 2,
  7. every count one less each 400 frames; see src/paths.inc) and runs it in x64sc (NTSC, warp,
  8. private config) with no keys held. Monitor tracepoints:
  9. - at smoke_ready: save the generated world;
  10. - at path_changed: log the new tile (register A), once per path that appears or fades;
  11. - at people_moved when move_frame is 0 (every 256 frames, each save overwriting the
  12. last): save the world, the walk counts, the change queue, both bitmaps and screens,
  13. the buffer origins and the people's saved-byte lists.
  14. Checks on the last save:
  15. - tiles: every cell is its generated tile, or the path tile for it (land, grass, trees
  16. only); water and stone have count 0 and never change
  17. - counts and paths agree: no land, grass or tree cell has count PATH_ON or more, no path
  18. has count below PATH_OFF (unless the change queue was full)
  19. - both buffers: every drawn cell (39 columns, 20 rows) has the colour and bitmap of its
  20. current tile, except cells still queued for that buffer; the only other pixels are
  21. people's dots (%11) in bytes listed in the buffer's saved-byte list, whose oldest entry
  22. holds the clean tile byte
  23. - paths appeared and faded during the run (path_changed hits with a path tile and with a
  24. terrain tile)
  25. Exit codes: 0 = pass, 1 = fail, 2 = a tool is missing.
  26. .EXAMPLE
  27. pwsh -File tools/check-paths.ps1
  28. #>
  29. [CmdletBinding()]
  30. param(
  31. [int]$Cycles = 90000000,
  32. [switch]$NoBuild,
  33. [int]$TimeoutSeconds = 400
  34. )
  35. . (Join-Path $PSScriptRoot 'lib.ps1')
  36. $width = 80; $height = 50
  37. # PATH_TEST thresholds: the first PATH_ON / PATH_OFF in src/paths.inc (the .ifdef PATH_TEST branch)
  38. $pathsSrc = Get-Content -LiteralPath (Get-RepoPath 'src/paths.inc') -Raw
  39. $PATH_ON = [int]([regex]::Match($pathsSrc, '(?m)^PATH_ON\s*=\s*(\d+)').Groups[1].Value)
  40. $PATH_OFF = [int]([regex]::Match($pathsSrc, '(?m)^PATH_OFF\s*=\s*(\d+)').Groups[1].Value)
  41. if ($PATH_ON -le 0 -or $PATH_OFF -le 0) { throw 'PATH_ON / PATH_OFF not found in src/paths.inc' }
  42. $LAND = 0; $TREE = 2; $STONE = 3; $WATER_SE = 12
  43. $CHANGE_SLOTS = 8
  44. function Get-TileData {
  45. $src = Get-Content -LiteralPath (Get-RepoPath 'src/tiles.inc') -Raw
  46. $block = $src.Substring($src.IndexOf('tile_patterns:'))
  47. $block = $block.Substring(0, $block.IndexOf('.assert * - tile_patterns'))
  48. $patterns = [System.Collections.Generic.List[object]]::new()
  49. $repeat = 1
  50. foreach ($line in $block -split "`n") {
  51. if ($line -match '^\s*\.repeat\s+(\d+)') { $repeat = [int]$Matches[1]; continue }
  52. if ($line -match '^\s*\.endrepeat') { $repeat = 1; continue }
  53. if ($line -match '^\s*\.byte\s+([^;\r\n]+)') {
  54. $vals = @($Matches[1] -split ',' | ForEach-Object { [Convert]::ToByte($_.Trim().TrimStart('$'), 16) })
  55. for ($r = 0; $r -lt $repeat; $r++) { $patterns.Add($vals) }
  56. }
  57. }
  58. $colors = [System.Collections.Generic.List[byte]]::new()
  59. $cblock = $src.Substring($src.IndexOf('tile_colors:'))
  60. $cblock = $cblock.Substring(0, $cblock.IndexOf('.assert * - tile_colors'))
  61. $repeat = 1
  62. foreach ($line in $cblock -split "`n") {
  63. if ($line -match '\.repeat\s+(\d+)') { $repeat = [int]$Matches[1]; continue }
  64. if ($line -match '\.endrepeat') { $repeat = 1; continue }
  65. if ($line -match '\.byte\s+([^;\r\n]+)') {
  66. $vals = $Matches[1] -split ',' | ForEach-Object { [Convert]::ToByte($_.Trim().TrimStart('$'), 16) }
  67. for ($r = 0; $r -lt $repeat; $r++) { foreach ($v in $vals) { $colors.Add($v) } }
  68. }
  69. }
  70. $names = @([regex]::Matches(($src.Substring($src.IndexOf('.enum'), $src.IndexOf('.endenum') - $src.IndexOf('.enum'))), '(?m)^\s+([A-Z_]+)') | ForEach-Object { $_.Groups[1].Value })
  71. @{ patterns = $patterns.ToArray(); colors = $colors.ToArray(); ids = @{} + (& { $h = @{}; for ($i = 0; $i -lt $names.Count; $i++) { $h[$names[$i]] = $i }; $h }) }
  72. }
  73. try {
  74. $project = Get-ProjectConfig
  75. $target = Get-ProjectTargets $project 'c64'
  76. Set-TargetVariant $target 'PATH_TEST' 'pathtest'
  77. if (-not $NoBuild) { Invoke-TargetBuild $project $target @{} }
  78. $emu = Get-ToolPath $project $target.emulator.binary
  79. } catch [MissingToolException] {
  80. Write-Host $_.Exception.Message -ForegroundColor Yellow
  81. exit 2
  82. }
  83. $L = @{}
  84. foreach ($line in Get-Content -LiteralPath (Get-RepoPath $target.build.labels)) {
  85. $parts = $line -split '\s+'
  86. if ($parts.Count -ge 3) { $L[$parts[2].TrimStart('.')] = [Convert]::ToInt32($parts[1], 16) }
  87. }
  88. foreach ($s in 'smoke_ready', 'people_moved', 'path_changed', 'world_tiles', 'walk_counts', 'chg_count', 'chg_done', 'move_frame',
  89. 'buf_cx', 'buf_cy', 'buf_ready', 'buf_rows', 'drawn_count', 'drawn_low_a') {
  90. if (-not $L.ContainsKey($s)) { Write-Host "[FAIL] label $s not found" -ForegroundColor Red; exit 1 }
  91. }
  92. $tileData = Get-TileData
  93. $ids = $tileData.ids
  94. $PATH_LAND = $ids['PATH_LAND']; $PATH_TREE = $ids['PATH_TREE']
  95. $dir = Get-RepoPath 'build/check-paths'
  96. if (Test-Path -LiteralPath $dir) { Remove-Item -LiteralPath $dir -Recurse -Force }
  97. New-Item -ItemType Directory -Force -Path $dir | Out-Null
  98. $mon = [System.Collections.Generic.List[string]]::new()
  99. $mon.Add('logname "monitor.log"'); $mon.Add('log on')
  100. $n = 0
  101. function Add-Trace([int]$Address, [string]$Command, [string]$Condition) {
  102. $script:n++
  103. $line = 'trace exec ${0:x4}' -f $Address
  104. if ($Condition) { $line += " if $Condition" }
  105. $mon.Add($line)
  106. if ($Command) { $mon.Add(('command {0} "{1}"' -f $script:n, $Command)) }
  107. }
  108. $bsave = { param([string]$name, [int]$from, [int]$len) 'bsave \"{0}\" 0 {1:x4} {2:x4}' -f $name, $from, ($from + $len - 1) }
  109. Add-Trace $L.smoke_ready (& $bsave 'start-world.bin' $L.world_tiles 4000) $null
  110. Add-Trace $L.path_changed $null $null
  111. $changeCheckpoint = $n
  112. $every = '(@cpu:${0:x4} == $00)' -f $L.move_frame
  113. Add-Trace $L.people_moved (& $bsave 'world.bin' $L.world_tiles 4000) $every
  114. Add-Trace $L.people_moved (& $bsave 'counts.bin' $L.walk_counts 4000) $every
  115. Add-Trace $L.people_moved (& $bsave 'queue.bin' $L.chg_count ($L.chg_done + $CHANGE_SLOTS - $L.chg_count)) $every
  116. Add-Trace $L.people_moved (& $bsave 'bitmap-a.bin' 0x6000 6400) $every
  117. Add-Trace $L.people_moved (& $bsave 'bitmap-b.bin' 0xA000 6400) $every
  118. Add-Trace $L.people_moved (& $bsave 'screen-a.bin' 0x4000 800) $every
  119. Add-Trace $L.people_moved (& $bsave 'screen-b.bin' 0x8000 800) $every
  120. $bufVars = $L.buf_rows + 2 - $L.buf_cx # buf_cx .. buf_rows (2 bytes each; read by label below)
  121. Add-Trace $L.people_moved (& $bsave 'bufs.bin' $L.buf_cx $bufVars) $every
  122. Add-Trace $L.people_moved (& $bsave 'counts2.bin' $L.drawn_count 2) $every
  123. # Saved-byte lists (src/people.inc): six pages from drawn_low_a: low, high, byte for buffer A, then B.
  124. Add-Trace $L.people_moved (& $bsave 'lists.bin' $L.drawn_low_a (6 * 256)) $every
  125. 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 }
  126. Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $mon
  127. Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
  128. $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target)
  129. $vargs += @('-limitcycles', [string]$Cycles, '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target))
  130. $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } }
  131. Write-Host "[c64] running the PATH_TEST build headless for $Cycles cycles"
  132. $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru `
  133. -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err')
  134. if (-not $p.WaitForExit($TimeoutSeconds * 1000)) { $p.Kill($true); Write-Host '[FAIL] VICE did not exit in time.' -ForegroundColor Red; exit 1 }
  135. $read = { param($name) $f = Join-Path $dir $name; if (Test-Path -LiteralPath $f) { , [IO.File]::ReadAllBytes($f) } else { $null } }
  136. $start = & $read 'start-world.bin'; $world = & $read 'world.bin'; $counts = & $read 'counts.bin'; $queue = & $read 'queue.bin'
  137. 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 }
  138. $bitmaps = @((& $read 'bitmap-a.bin'), (& $read 'bitmap-b.bin')); $screens = @((& $read 'screen-a.bin'), (& $read 'screen-b.bin'))
  139. $bufs = & $read 'bufs.bin'; $drawnCount = & $read 'counts2.bin'
  140. $lists = & $read 'lists.bin'
  141. $failures = [System.Collections.Generic.List[string]]::new()
  142. # Change events: the trace's register line gives A = the new tile.
  143. $logFile = @('monitor.log', 'vice.log') | ForEach-Object { Join-Path $dir $_ } | Where-Object { Test-Path -LiteralPath $_ } | Select-Object -First 1
  144. $appeared = 0; $faded = 0; $want = $false
  145. foreach ($line in Get-Content -LiteralPath $logFile) {
  146. if ($line -match "^#$changeCheckpoint \(Trace\s+exec") { $want = $true; continue }
  147. if ($want -and $line -match '^\.C:.*\bA:([0-9A-Fa-f]{2})') {
  148. $a = [Convert]::ToInt32($Matches[1], 16)
  149. if ($a -ge $PATH_LAND -and $a -le $PATH_TREE) { $appeared++ } else { $faded++ }
  150. $want = $false
  151. }
  152. }
  153. if ($appeared -eq 0) { $failures.Add('no path appeared during the run') }
  154. if ($faded -eq 0) { $failures.Add('no path faded during the run') }
  155. # Queue: chg_count, new_cell, chg_tile, chg_ty, chg_cx[8], chg_cy[8], chg_done[8].
  156. $qCount = $queue[0]; $qFull = $qCount -ge $CHANGE_SLOTS
  157. $pending = @(@{}, @{})
  158. for ($i = 0; $i -lt $qCount; $i++) {
  159. $cx = $queue[4 + $i]; $cy = $queue[4 + $CHANGE_SLOTS + $i]; $done = $queue[4 + 2 * $CHANGE_SLOTS + $i]
  160. for ($b = 0; $b -lt 2; $b++) { if (-not ($done -band (1 -shl $b))) { $pending[$b]["$cx,$cy"] = 1 } }
  161. }
  162. # Tiles and counts.
  163. $paths = 0
  164. for ($i = 0; $i -lt 4000; $i++) {
  165. $t = $world[$i]; $o = $start[$i]; $c = $counts[$i]; $x = $i % $width; $y = [math]::Floor($i / $width)
  166. if ($o -ge $STONE -and $o -le $WATER_SE) {
  167. if ($t -ne $o) { $failures.Add("water or stone cell ($x,$y) changed from $o to $t") }
  168. if ($c -ne 0) { $failures.Add("water or stone cell ($x,$y) has walk count $c") }
  169. continue
  170. }
  171. if ($t -ge $PATH_LAND -and $t -le $PATH_TREE) {
  172. $paths++
  173. if ($t - $PATH_LAND + $LAND -ne $o) { $failures.Add("path tile $t at ($x,$y) over generated tile $o") }
  174. if ($c -lt $PATH_OFF -and -not $qFull) { $failures.Add("path at ($x,$y) has count $c (below $PATH_OFF)") }
  175. } elseif ($t -ne $o) {
  176. $failures.Add("cell ($x,$y) changed from $o to $t")
  177. } elseif ($c -ge $PATH_ON -and -not $qFull) {
  178. $failures.Add("cell ($x,$y) has count $c but is not a path")
  179. }
  180. }
  181. # Buffers: origin, ready flag and rows built, each read at its label (buf_cx .. buf_rows).
  182. $dotsChecked = 0
  183. for ($b = 0; $b -lt 2; $b++) {
  184. $at = { param($name) $bufs[$L[$name] - $L.buf_cx + $b] }
  185. $ox = & $at 'buf_cx'; $oy = & $at 'buf_cy'; $ready = & $at 'buf_ready'; $rows = [math]::Min(20, (& $at 'buf_rows'))
  186. if (-not $ready) { $failures.Add("buffer $b is not complete at the save"); continue }
  187. $base = @(0x6000, 0xA000)[$b]
  188. $listed = @{} # address offset -> oldest saved byte
  189. for ($k = $drawnCount[$b] - 1; $k -ge 0; $k--) {
  190. $page = 3 * 256 * $b + $k
  191. $addr = $lists[$page] + 256 * $lists[$page + 256] - $base
  192. $listed[$addr] = $lists[$page + 512] # ends with the oldest entry
  193. }
  194. $badCells = 0; $badDots = 0
  195. for ($r = 0; $r -lt $rows; $r++) {
  196. for ($col = 0; $col -lt 39; $col++) {
  197. $wx = $ox + $col; $wy = $oy + $r
  198. if ($wx -ge $width -or $wy -ge $height) { continue }
  199. if ($pending[$b].ContainsKey("$wx,$wy")) { continue }
  200. $t = $world[$wy * $width + $wx]
  201. if ($screens[$b][$r * 40 + $col] -ne $tileData.colors[$t]) { $badCells++; continue }
  202. for ($k = 0; $k -lt 8; $k++) {
  203. $off = $r * 320 + $col * 8 + $k
  204. $want = $tileData.patterns[$t][$k]; $have = $bitmaps[$b][$off]
  205. if ($have -eq $want) { if ($listed.ContainsKey($off) -and $listed[$off] -ne $want) { $badDots++ }; continue }
  206. # Pairs that differ must be dots (%11) in a listed byte whose oldest saved byte is clean.
  207. for ($q = 0; $q -lt 4; $q++) {
  208. $sh = 6 - 2 * $q
  209. if ((($have -shr $sh) -band 3) -ne (($want -shr $sh) -band 3) -and (($have -shr $sh) -band 3) -ne 3) { $badCells++ }
  210. }
  211. if (-not $listed.ContainsKey($off) -or $listed[$off] -ne $want) { $badDots++ } else { $dotsChecked++ }
  212. }
  213. }
  214. }
  215. if ($badCells -gt 0) { $failures.Add("buffer $('AB'[$b]): $badCells cells or pixels differ from the current world") }
  216. if ($badDots -gt 0) { $failures.Add("buffer $('AB'[$b]): $badDots dot bytes without a matching clean saved byte") }
  217. }
  218. 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)
  219. if ($failures.Count -gt 0) {
  220. $failures | Select-Object -First 12 | ForEach-Object { Write-Host " $_" }
  221. Write-Host '[FAIL] paths check' -ForegroundColor Red
  222. exit 1
  223. }
  224. 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
  225. exit 0

Powered by TurnKey Linux.