Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

266 рядки
16KB

  1. <#
  2. .SYNOPSIS
  3. Run the C64 build headless in VICE and check that the people (16, or -People) move, never
  4. stand on water or stone, and are redrawn in both buffers without trails.
  5. .DESCRIPTION
  6. Starts x64sc (NTSC, warp, private config) like tools/smoke.ps1. Monitor tracepoints:
  7. - at smoke_ready: save the world tiles and both clean bitmaps (no people yet);
  8. - at people_moved (once per frame, after drawing into the hidden buffer and
  9. before the flip): print the people table and the visible-buffer flag to the
  10. monitor log, and save both bitmaps (each frame overwrites the last).
  11. VICE quits after -Cycles cycles. The script then checks every logged frame:
  12. every person inside the 80x50 world, every one on a walkable tile (not water or stone)
  13. and on a pixel that is not blue or grey (the water pixels of bridge tiles),
  14. every step at most one pixel, and every person moving during the run. It checks
  15. that the main loop never misses a frame (x64sc NTSC frame = 263 lines x 65 =
  16. 17,095 cycles; a gap over 1.5 frames fails), and that
  17. in the final frame the hidden bitmap equals the clean map plus the people's current
  18. dots and the visible bitmap equals the clean map plus the previous frame's dots:
  19. no trails, no damaged terrain.
  20. -People N builds a variant that starts with N people (PEOPLE_START, 1-255;
  21. build/land-map-64-peopleN.prg). With more than 16 the main loop may take longer
  22. than a frame (M7.3: people keep their speed, frames drop): missed frames are then
  23. reported, not failed, and the number of frames per main-loop pass is shown.
  24. Exit codes: 0 = pass, 1 = fail, 2 = a tool is missing.
  25. .EXAMPLE
  26. pwsh -File tools/check-people.ps1
  27. pwsh -File tools/check-people.ps1 -People 255
  28. #>
  29. [CmdletBinding()]
  30. param(
  31. [int]$Cycles = 40000000,
  32. [switch]$NoBuild,
  33. [int]$TimeoutSeconds = 180,
  34. [ValidateRange(1, 255)][int]$People = 16
  35. )
  36. . (Join-Path $PSScriptRoot 'lib.ps1')
  37. $count = $People
  38. $width = 80
  39. $height = 50
  40. $blocking = 3..12 # STONE..WATER_SE in src/tiles.inc
  41. $waterColors = 6, 12 # blue and grey: no dot may stand on these pixels
  42. function Get-TileData {
  43. $src = Get-Content -LiteralPath (Get-RepoPath 'src/tiles.inc') -Raw
  44. $block = $src.Substring($src.IndexOf('tile_patterns:'))
  45. $block = $block.Substring(0, $block.IndexOf('.assert * - tile_patterns'))
  46. $patterns = @(foreach ($m in [regex]::Matches($block, '(?m)^\s*\.byte\s+([^;\r\n]+)')) {
  47. , @($m.Groups[1].Value -split ',' | ForEach-Object { [Convert]::ToByte($_.Trim().TrimStart('$'), 16) })
  48. })
  49. $colors = [System.Collections.Generic.List[byte]]::new()
  50. $cblock = $src.Substring($src.IndexOf('tile_colors:'))
  51. $cblock = $cblock.Substring(0, $cblock.IndexOf('.assert * - tile_colors'))
  52. $repeat = 1
  53. foreach ($line in $cblock -split "`n") {
  54. if ($line -match '\.repeat\s+(\d+)') { $repeat = [int]$Matches[1]; continue }
  55. if ($line -match '\.endrepeat') { $repeat = 1; continue }
  56. if ($line -match '\.byte\s+([^;\r\n]+)') {
  57. $vals = $Matches[1] -split ',' | ForEach-Object { [Convert]::ToByte($_.Trim().TrimStart('$'), 16) }
  58. for ($r = 0; $r -lt $repeat; $r++) { foreach ($v in $vals) { $colors.Add($v) } }
  59. }
  60. }
  61. @{ patterns = $patterns; colors = $colors.ToArray() }
  62. }
  63. function Get-Label([hashtable]$Target, [string]$Symbol) {
  64. foreach ($line in Get-Content -LiteralPath (Get-RepoPath $Target.build.labels)) {
  65. $parts = $line -split '\s+'
  66. if ($parts.Count -ge 3 -and $parts[2].TrimStart('.') -eq $Symbol) { return [Convert]::ToInt32($parts[1], 16) }
  67. }
  68. throw "Symbol '$Symbol' not found in $($Target.build.labels)."
  69. }
  70. try {
  71. $project = Get-ProjectConfig
  72. $target = Get-ProjectTargets $project 'c64'
  73. if ($People -ne 16) { Set-TargetVariant $target "PEOPLE_START=$People" "people$People" }
  74. if (-not $NoBuild) { Invoke-TargetBuild $project $target @{} }
  75. $emu = Get-ToolPath $project $target.emulator.binary
  76. } catch [MissingToolException] {
  77. Write-Host $_.Exception.Message -ForegroundColor Yellow
  78. exit 2
  79. }
  80. $dir = Get-RepoPath 'build/check-people'
  81. if (Test-Path -LiteralPath $dir) { Remove-Item -LiteralPath $dir -Recurse -Force }
  82. New-Item -ItemType Directory -Force -Path $dir | Out-Null
  83. $ready = Get-Label $target 'smoke_ready'
  84. $moved = Get-Label $target 'people_moved'
  85. $world = Get-Label $target 'world_tiles'
  86. $peopleCx = Get-Label $target 'people_cx'
  87. $visibleFlag = Get-Label $target 'visible'
  88. # Logged per frame, in this order: the first $count entries of people_cx, people_sx, people_cy,
  89. # people_sy and people_dir (one page each, src/people.inc), then move_frame and draw_skip.
  90. $tableSize = 5 * $count + 2
  91. $peopleDumps = @('people_cx', 'people_sx', 'people_cy', 'people_sy', 'people_dir' | ForEach-Object {
  92. $a = Get-Label $target $_; 'm {0:x4} {1:x4}' -f $a, ($a + $count - 1) })
  93. $peopleDumps += @('move_frame', 'draw_skip' | ForEach-Object { 'm {0:x4} {0:x4}' -f (Get-Label $target $_) })
  94. $bitmapSize = 20 * 320 # the 20 map rows; rows 20-24 lie under the status text
  95. $buffers = @(0x6000, 0xA000) # bitmaps of buffers A and B (src/main.s)
  96. $monitor = [System.Collections.Generic.List[string]]::new()
  97. $monitor.Add('logname "monitor.log"')
  98. $monitor.Add('log on')
  99. $n = 0
  100. function Add-Trace([int]$Address, [string]$Command) {
  101. $script:n++
  102. $monitor.Add(('trace exec ${0:x4}' -f $Address))
  103. $monitor.Add(('command {0} "{1}"' -f $script:n, $Command))
  104. }
  105. Add-Trace $ready ('bsave \"world.bin\" 0 {0:x4} {1:x4}' -f $world, ($world + $width * $height - 1))
  106. Add-Trace $ready ('bsave \"clean-a.bin\" 0 {0:x4} {1:x4}' -f $buffers[0], ($buffers[0] + $bitmapSize - 1))
  107. Add-Trace $ready ('bsave \"clean-b.bin\" 0 {0:x4} {1:x4}' -f $buffers[1], ($buffers[1] + $bitmapSize - 1))
  108. Add-Trace $moved $peopleDumps[0]
  109. $peopleCheckpoint = $n
  110. foreach ($d in $peopleDumps[1..($peopleDumps.Count - 1)]) { Add-Trace $moved $d }
  111. Add-Trace $moved ('m {0:x4} {0:x4}' -f $visibleFlag)
  112. Add-Trace $moved ('bsave \"final-a.bin\" 0 {0:x4} {1:x4}' -f $buffers[0], ($buffers[0] + $bitmapSize - 1))
  113. Add-Trace $moved ('bsave \"final-b.bin\" 0 {0:x4} {1:x4}' -f $buffers[1], ($buffers[1] + $bitmapSize - 1))
  114. # No key is held during this check: keys typed on the PC while VICE has focus
  115. # would otherwise reach the program (the arrow keys scroll the view).
  116. foreach ($row in 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space', 'key_row_p') { Add-Trace (Get-Label $target $row) 'r a = ff' }
  117. Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $monitor
  118. Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
  119. $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target)
  120. $vargs += @('-limitcycles', [string]$Cycles, '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target))
  121. $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } }
  122. Write-Host "[c64] running x64sc headless for $Cycles cycles"
  123. $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru `
  124. -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err')
  125. if (-not $p.WaitForExit($TimeoutSeconds * 1000)) { $p.Kill($true); Write-Host '[FAIL] VICE did not exit in time.' -ForegroundColor Red; exit 1 }
  126. $failures = [System.Collections.Generic.List[string]]::new()
  127. $worldFile = Join-Path $dir 'world.bin'
  128. if (-not (Test-Path -LiteralPath $worldFile)) { Write-Host '[FAIL] Program never reached smoke_ready.' -ForegroundColor Red; exit 1 }
  129. $tiles = [IO.File]::ReadAllBytes($worldFile)
  130. # Monitor memory dumps look like ">C:1e4c 00 01 02 ... text". Collect bytes per dump.
  131. $logFile = @('monitor.log', 'vice.log') | ForEach-Object { Join-Path $dir $_ } |
  132. Where-Object { (Test-Path -LiteralPath $_) -and (Select-String -LiteralPath $_ -Pattern '^>C:' -Quiet) } | Select-Object -First 1
  133. if (-not $logFile) { Write-Host '[FAIL] No people table was logged (people_moved never reached?).' -ForegroundColor Red; exit 1 }
  134. $frames = [System.Collections.Generic.List[byte[]]]::new()
  135. $visibleLog = [System.Collections.Generic.List[int]]::new()
  136. $clocks = [System.Collections.Generic.List[long]]::new()
  137. $current = [System.Collections.Generic.List[byte]]::new()
  138. $wantClock = $false
  139. $lines = [System.Collections.Generic.List[int]]::new()
  140. foreach ($line in Get-Content -LiteralPath $logFile) {
  141. if ($line -match "^#$peopleCheckpoint \(Trace\s+exec [0-9a-fA-F]+\)\s+(\d+)/") { $lines.Add([int]$Matches[1]); $wantClock = $true; continue }
  142. if ($wantClock -and $line -match '^\.C:.*\s(\d+)\s*$') { $clocks.Add([long]$Matches[1]); $wantClock = $false; continue }
  143. # Hex column only: each byte is two digits and one space; the ASCII column
  144. # after it can itself look like hex (for example bytes $34 $42 print as "4B").
  145. if ($line -notmatch '^>C:([0-9a-fA-F]{4}) ((?:[0-9a-fA-F]{2} )+)') { continue }
  146. $addr = [Convert]::ToInt32($Matches[1], 16)
  147. $hexBytes = $Matches[2].Trim() -split ' '
  148. if ($addr -eq $visibleFlag) { $visibleLog.Add([Convert]::ToInt32($hexBytes[0], 16)); continue }
  149. if ($addr -eq $peopleCx) { $current.Clear() }
  150. foreach ($hex in $hexBytes) {
  151. if ($current.Count -lt $tableSize) { $current.Add([Convert]::ToByte($hex, 16)) }
  152. }
  153. if ($current.Count -eq $tableSize) { $frames.Add($current.ToArray()); $current.Clear() }
  154. }
  155. if ($frames.Count -lt 60) { Write-Host "[FAIL] Only $($frames.Count) frames logged." -ForegroundColor Red; exit 1 }
  156. $tileData = Get-TileData
  157. $bridgeFirst = $tileData.patterns.Count - 3 # BRIDGE_H..BRIDGE_X are the last tiles
  158. $onBridge = 0
  159. $first = $frames[0]
  160. $moves = [int[]]::new($count)
  161. $previous = $null
  162. foreach ($f in $frames) {
  163. for ($i = 0; $i -lt $count; $i++) {
  164. $cx = $f[$i]; $sx = $f[$count + $i]; $cy = $f[2 * $count + $i]; $sy = $f[3 * $count + $i]; $heading = $f[4 * $count + $i]
  165. if ($cx -ge $width -or $cy -ge $height -or $sx -gt 3 -or $sy -gt 7 -or $heading -gt 3) {
  166. $failures.Add("person $i out of range: cell ($cx,$cy) pixel ($sx,$sy) dir $heading"); continue
  167. }
  168. $tile = $tiles[$cy * $width + $cx]
  169. if ($tile -in $blocking) { $failures.Add("person $i on blocking tile $tile at cell ($cx,$cy)") }
  170. # The pixel under the dot: %01 takes the high colour nibble, %10 the low one.
  171. $pixel = ($tileData.patterns[$tile][$sy] -shr (6 - 2 * $sx)) -band 3
  172. $color = switch ($pixel) { 1 { $tileData.colors[$tile] -shr 4 } 2 { $tileData.colors[$tile] -band 15 } default { -1 } }
  173. if ($color -in $waterColors) { $failures.Add("person $i on a colour $color pixel of tile $tile at cell ($cx,$cy) pixel ($sx,$sy)") }
  174. if ($tile -ge $bridgeFirst) { $onBridge++ }
  175. if ($previous) {
  176. $x0 = $previous[$i] * 4 + $previous[$count + $i]; $y0 = $previous[2 * $count + $i] * 8 + $previous[3 * $count + $i]
  177. $dx = [Math]::Abs($cx * 4 + $sx - $x0); $dy = [Math]::Abs($cy * 8 + $sy - $y0)
  178. if ($dx + $dy -gt 1) { $failures.Add("person $i jumped $dx,$dy pixels") }
  179. if ($dx + $dy -eq 1) { $moves[$i]++ }
  180. }
  181. }
  182. $previous = $f
  183. if ($failures.Count -gt 10) { break }
  184. }
  185. for ($i = 0; $i -lt $count; $i++) {
  186. if ($moves[$i] -eq 0) { $failures.Add("person $i never moved") }
  187. }
  188. # Main loop timing: one hook per NTSC frame (65 cycles x 263 lines on x64sc).
  189. # The hook comes after a variable amount of work, so gaps jitter around 17095.
  190. $frameCycles = 17095
  191. $late = 0
  192. for ($k = 1; $k -lt $clocks.Count; $k++) { if ($clocks[$k] - $clocks[$k - 1] -gt 1.5 * $frameCycles) { $late++ } }
  193. if ($clocks.Count -lt $frames.Count) { $failures.Add("only $($clocks.Count) hook clocks for $($frames.Count) frames") }
  194. if ($late -gt 0 -and $count -le 16) { $failures.Add("the main loop missed $late frame(s)") }
  195. $passFrames = if ($clocks.Count -gt 1) { [Math]::Round(($clocks[$clocks.Count - 1] - $clocks[0]) / ($clocks.Count - 1) / $frameCycles, 2) } else { 0 }
  196. # The flip happens from line 251; the update must end before the visible area (line 51).
  197. # Since M7.2 the vertical blank does no people work, so the hook is usually reached at line
  198. # 251-262, before the raster wraps to 0; those count as early (shown as negative: line - 263).
  199. $vblankLines = @($lines | ForEach-Object { if ($_ -ge 251) { $_ - 263 } else { $_ } })
  200. $lastLine = ($vblankLines | Measure-Object -Maximum).Maximum
  201. if (@($vblankLines | Where-Object { $_ -ge 51 }).Count -gt 0) { $failures.Add('the vertical blank work ran into the visible area (hook at raster line 51 or later)') }
  202. # Final frame, at people_moved (after the vertical blank flip; M7.2): the people were drawn into
  203. # the now visible buffer last frame, after the moves, so it holds clean + the dots of the table
  204. # logged now; the now hidden buffer was drawn the frame before: clean + the previous table's dots.
  205. function Get-ExpectedBitmap([byte[]]$Clean, [byte[]]$Table) {
  206. $bitmap = [byte[]]$Clean.Clone()
  207. $masks = 0xC0, 0x30, 0x0C, 0x03
  208. for ($i = 0; $i -lt $count; $i++) {
  209. if ($i -eq $Table[5 * $count + 1]) { continue } # draw_skip: the shown person blinks (R8)
  210. $cx = $Table[$i]; $cy = $Table[2 * $count + $i]
  211. if ($cx -ge 39 -or $cy -ge 20) { continue } # 39 drawn columns; the dumps hold the 20 map rows
  212. $offset = $cy * 320 + $cx * 8 + $Table[3 * $count + $i]
  213. $bitmap[$offset] = $bitmap[$offset] -bor $masks[$Table[$count + $i]]
  214. }
  215. , $bitmap
  216. }
  217. $clean = 'clean-a.bin', 'clean-b.bin' | ForEach-Object { , [IO.File]::ReadAllBytes((Join-Path $dir $_)) }
  218. $final = 'final-a.bin', 'final-b.bin' | ForEach-Object { , [IO.File]::ReadAllBytes((Join-Path $dir $_)) }
  219. # Column 39 is never drawn (never visible in 38-column mode): skip its bytes.
  220. $drawnByte = { param($b) ($b % 320) -lt 312 }
  221. 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 } }
  222. $shown = $visibleLog[$visibleLog.Count - 1]
  223. $hiddenIndex = 1 - $shown
  224. $expectHidden = Get-ExpectedBitmap $clean[$hiddenIndex] $frames[$frames.Count - 2]
  225. $expectShown = Get-ExpectedBitmap $clean[$shown] $frames[$frames.Count - 1]
  226. $dots = 0
  227. foreach ($pair in @(@($final[$hiddenIndex], $expectHidden, 'hidden'), @($final[$shown], $expectShown, 'visible'))) {
  228. $bad = 0
  229. for ($b = 0; $b -lt $bitmapSize; $b++) {
  230. if ($pair[0][$b] -ne $pair[1][$b] -and ($b % 320) -lt 312) { $bad++ }
  231. }
  232. if ($bad -gt 0) { $failures.Add("final $($pair[2]) bitmap has $bad bytes that differ from clean map plus people") }
  233. }
  234. for ($b = 0; $b -lt $bitmapSize; $b++) { if ($final[$hiddenIndex][$b] -ne $clean[$hiddenIndex][$b] -and ($b % 320) -lt 312) { $dots++ } }
  235. $sectors = @(0..($count - 1) | ForEach-Object { [int]($first[$_] -ge 40) + 2 * [int]($first[2 * $count + $_] -ge 25) } | Sort-Object -Unique)
  236. if ($sectors.Count -ne [Math]::Min(4, $count)) { $failures.Add("people start in only $($sectors.Count) of 4 screens") }
  237. $seconds = [Math]::Round($frames.Count / 60.0, 1)
  238. Write-Host ("Main loop passes: {0}, {1} longer than a frame ({2} frames per pass on average); vertical blank work done by raster line {3} at the latest. Final hidden buffer: {4}, {5} bytes carry dots." -f $clocks.Count, $late, $passFrames, $lastLine, ('A', 'B')[$hiddenIndex], $dots)
  239. if ($clocks.Count -gt 1) { $seconds = [Math]::Round(($clocks[$clocks.Count - 1] - $clocks[0]) / 1022727.0, 1) }
  240. $stepText = if ($count -le 16) { $moves -join ' ' } else { 'min {0}, max {1}' -f ($moves | Measure-Object -Minimum).Minimum, ($moves | Measure-Object -Maximum).Maximum }
  241. Write-Host ("Logged {0} movement frames (~{1} s NTSC). Steps per person: {2}. Person-frames on bridges: {3}." -f $frames.Count, $seconds, $stepText, $onBridge)
  242. if ($failures.Count -gt 0) {
  243. $failures | Select-Object -First 10 | ForEach-Object { Write-Host " $_" }
  244. Write-Host '[FAIL] people check' -ForegroundColor Red
  245. exit 1
  246. }
  247. Write-Host "[PASS] $count people spread over the 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$(if ($count -gt 16) { ' (or per main-loop pass)' })." -ForegroundColor Green
  248. exit 0

Powered by TurnKey Linux.