25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
11KB

  1. <#
  2. .SYNOPSIS
  3. Run the C64 build headless in VICE and check that the 16 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. 16 people inside the 80x50 world, every one on a walkable tile (not water or stone),
  13. every step at most one pixel, and every person moving during the run. It checks
  14. that the main loop never misses a frame (x64sc NTSC frame = 263 lines x 65 =
  15. 17,095 cycles; a gap over 1.5 frames fails), and that
  16. in the final frame the hidden bitmap equals the clean map plus the people's current
  17. dots and the visible bitmap equals the clean map plus the previous frame's dots:
  18. no trails, no damaged terrain.
  19. Exit codes: 0 = pass, 1 = fail, 2 = a tool is missing.
  20. .EXAMPLE
  21. pwsh -File tools/check-people.ps1
  22. #>
  23. [CmdletBinding()]
  24. param(
  25. [int]$Cycles = 40000000,
  26. [switch]$NoBuild,
  27. [int]$TimeoutSeconds = 180
  28. )
  29. . (Join-Path $PSScriptRoot 'lib.ps1')
  30. $count = 16
  31. $width = 80
  32. $height = 50
  33. $blocking = 3..12 # STONE..WATER_SE in src/tiles.inc
  34. function Get-Label([hashtable]$Target, [string]$Symbol) {
  35. foreach ($line in Get-Content -LiteralPath (Get-RepoPath $Target.build.labels)) {
  36. $parts = $line -split '\s+'
  37. if ($parts.Count -ge 3 -and $parts[2].TrimStart('.') -eq $Symbol) { return [Convert]::ToInt32($parts[1], 16) }
  38. }
  39. throw "Symbol '$Symbol' not found in $($Target.build.labels)."
  40. }
  41. try {
  42. $project = Get-ProjectConfig
  43. $target = Get-ProjectTargets $project 'c64'
  44. if (-not $NoBuild) { Invoke-TargetBuild $project $target @{} }
  45. $emu = Get-ToolPath $project $target.emulator.binary
  46. } catch [MissingToolException] {
  47. Write-Host $_.Exception.Message -ForegroundColor Yellow
  48. exit 2
  49. }
  50. $dir = Get-RepoPath 'build/check-people'
  51. if (Test-Path -LiteralPath $dir) { Remove-Item -LiteralPath $dir -Recurse -Force }
  52. New-Item -ItemType Directory -Force -Path $dir | Out-Null
  53. $ready = Get-Label $target 'smoke_ready'
  54. $moved = Get-Label $target 'people_moved'
  55. $world = Get-Label $target 'world_tiles'
  56. $people = Get-Label $target 'people_cx'
  57. $visibleFlag = Get-Label $target 'visible'
  58. $tableEnd = $people + 5 * $count - 1
  59. $bitmapSize = 20 * 320 # the 20 map rows; rows 20-24 lie under the status text
  60. $buffers = @(0x6000, 0xA000) # bitmaps of buffers A and B (src/main.s)
  61. $monitor = [System.Collections.Generic.List[string]]::new()
  62. $monitor.Add('logname "monitor.log"')
  63. $monitor.Add('log on')
  64. $n = 0
  65. function Add-Trace([int]$Address, [string]$Command) {
  66. $script:n++
  67. $monitor.Add(('trace exec ${0:x4}' -f $Address))
  68. $monitor.Add(('command {0} "{1}"' -f $script:n, $Command))
  69. }
  70. Add-Trace $ready ('bsave \"world.bin\" 0 {0:x4} {1:x4}' -f $world, ($world + $width * $height - 1))
  71. Add-Trace $ready ('bsave \"clean-a.bin\" 0 {0:x4} {1:x4}' -f $buffers[0], ($buffers[0] + $bitmapSize - 1))
  72. Add-Trace $ready ('bsave \"clean-b.bin\" 0 {0:x4} {1:x4}' -f $buffers[1], ($buffers[1] + $bitmapSize - 1))
  73. Add-Trace $moved ('m {0:x4} {1:x4}' -f $people, $tableEnd)
  74. $peopleCheckpoint = $n
  75. Add-Trace $moved ('m {0:x4} {0:x4}' -f $visibleFlag)
  76. Add-Trace $moved ('bsave \"final-a.bin\" 0 {0:x4} {1:x4}' -f $buffers[0], ($buffers[0] + $bitmapSize - 1))
  77. Add-Trace $moved ('bsave \"final-b.bin\" 0 {0:x4} {1:x4}' -f $buffers[1], ($buffers[1] + $bitmapSize - 1))
  78. Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $monitor
  79. Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
  80. $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target)
  81. $vargs += @('-limitcycles', [string]$Cycles, '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target))
  82. $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } }
  83. Write-Host "[c64] running x64sc headless for $Cycles cycles"
  84. $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru `
  85. -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err')
  86. if (-not $p.WaitForExit($TimeoutSeconds * 1000)) { $p.Kill($true); Write-Host '[FAIL] VICE did not exit in time.' -ForegroundColor Red; exit 1 }
  87. $failures = [System.Collections.Generic.List[string]]::new()
  88. $worldFile = Join-Path $dir 'world.bin'
  89. if (-not (Test-Path -LiteralPath $worldFile)) { Write-Host '[FAIL] Program never reached smoke_ready.' -ForegroundColor Red; exit 1 }
  90. $tiles = [IO.File]::ReadAllBytes($worldFile)
  91. # Monitor memory dumps look like ">C:1e4c 00 01 02 ... text". Collect bytes per dump.
  92. $logFile = @('monitor.log', 'vice.log') | ForEach-Object { Join-Path $dir $_ } |
  93. Where-Object { (Test-Path -LiteralPath $_) -and (Select-String -LiteralPath $_ -Pattern '^>C:' -Quiet) } | Select-Object -First 1
  94. if (-not $logFile) { Write-Host '[FAIL] No people table was logged (people_moved never reached?).' -ForegroundColor Red; exit 1 }
  95. $frames = [System.Collections.Generic.List[byte[]]]::new()
  96. $visibleLog = [System.Collections.Generic.List[int]]::new()
  97. $clocks = [System.Collections.Generic.List[long]]::new()
  98. $current = [System.Collections.Generic.List[byte]]::new()
  99. $wantClock = $false
  100. $lines = [System.Collections.Generic.List[int]]::new()
  101. foreach ($line in Get-Content -LiteralPath $logFile) {
  102. if ($line -match "^#$peopleCheckpoint \(Trace\s+exec [0-9a-fA-F]+\)\s+(\d+)/") { $lines.Add([int]$Matches[1]); $wantClock = $true; continue }
  103. if ($wantClock -and $line -match '^\.C:.*\s(\d+)\s*$') { $clocks.Add([long]$Matches[1]); $wantClock = $false; continue }
  104. # Hex column only: each byte is two digits and one space; the ASCII column
  105. # after it can itself look like hex (for example bytes $34 $42 print as "4B").
  106. if ($line -notmatch '^>C:([0-9a-fA-F]{4}) ((?:[0-9a-fA-F]{2} )+)') { continue }
  107. $addr = [Convert]::ToInt32($Matches[1], 16)
  108. $hexBytes = $Matches[2].Trim() -split ' '
  109. if ($addr -eq $visibleFlag) { $visibleLog.Add([Convert]::ToInt32($hexBytes[0], 16)); continue }
  110. if ($addr -eq $people) { $current.Clear() }
  111. foreach ($hex in $hexBytes) {
  112. if ($current.Count -lt 5 * $count) { $current.Add([Convert]::ToByte($hex, 16)) }
  113. }
  114. if ($current.Count -eq 5 * $count) { $frames.Add($current.ToArray()); $current.Clear() }
  115. }
  116. if ($frames.Count -lt 60) { Write-Host "[FAIL] Only $($frames.Count) frames logged." -ForegroundColor Red; exit 1 }
  117. $first = $frames[0]
  118. $moves = [int[]]::new($count)
  119. $previous = $null
  120. foreach ($f in $frames) {
  121. for ($i = 0; $i -lt $count; $i++) {
  122. $cx = $f[$i]; $sx = $f[16 + $i]; $cy = $f[32 + $i]; $sy = $f[48 + $i]; $heading = $f[64 + $i]
  123. if ($cx -ge $width -or $cy -ge $height -or $sx -gt 3 -or $sy -gt 7 -or $heading -gt 3) {
  124. $failures.Add("person $i out of range: cell ($cx,$cy) pixel ($sx,$sy) dir $heading"); continue
  125. }
  126. $tile = $tiles[$cy * $width + $cx]
  127. if ($tile -in $blocking) { $failures.Add("person $i on blocking tile $tile at cell ($cx,$cy)") }
  128. if ($previous) {
  129. $x0 = $previous[$i] * 4 + $previous[16 + $i]; $y0 = $previous[32 + $i] * 8 + $previous[48 + $i]
  130. $dx = [Math]::Abs($cx * 4 + $sx - $x0); $dy = [Math]::Abs($cy * 8 + $sy - $y0)
  131. if ($dx + $dy -gt 1) { $failures.Add("person $i jumped $dx,$dy pixels") }
  132. if ($dx + $dy -eq 1) { $moves[$i]++ }
  133. }
  134. }
  135. $previous = $f
  136. if ($failures.Count -gt 10) { break }
  137. }
  138. for ($i = 0; $i -lt $count; $i++) {
  139. if ($moves[$i] -eq 0) { $failures.Add("person $i never moved") }
  140. }
  141. # Main loop timing: one hook per NTSC frame (65 cycles x 263 lines on x64sc).
  142. # The hook comes after a variable amount of work, so gaps jitter around 17095.
  143. $frameCycles = 17095
  144. $late = 0
  145. for ($k = 1; $k -lt $clocks.Count; $k++) { if ($clocks[$k] - $clocks[$k - 1] -gt 1.5 * $frameCycles) { $late++ } }
  146. if ($clocks.Count -lt $frames.Count) { $failures.Add("only $($clocks.Count) hook clocks for $($frames.Count) frames") }
  147. if ($late -gt 0) { $failures.Add("the main loop missed $late frame(s)") }
  148. # The flip happens from line 251; the update must end before the visible area (line 51).
  149. $lastLine = ($lines | Where-Object { $_ -lt 200 } | Measure-Object -Maximum).Maximum
  150. 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)') }
  151. # Final frame: hidden buffer = clean + current dots, visible buffer = clean + previous dots.
  152. function Get-ExpectedBitmap([byte[]]$Clean, [byte[]]$Table) {
  153. $bitmap = [byte[]]$Clean.Clone()
  154. $masks = 0xC0, 0x30, 0x0C, 0x03
  155. for ($i = 0; $i -lt $count; $i++) {
  156. $cx = $Table[$i]; $cy = $Table[32 + $i]
  157. if ($cx -ge 40 -or $cy -ge 20) { continue }
  158. $offset = $cy * 320 + $cx * 8 + $Table[48 + $i]
  159. $bitmap[$offset] = $bitmap[$offset] -bor $masks[$Table[16 + $i]]
  160. }
  161. , $bitmap
  162. }
  163. $clean = 'clean-a.bin', 'clean-b.bin' | ForEach-Object { , [IO.File]::ReadAllBytes((Join-Path $dir $_)) }
  164. $final = 'final-a.bin', 'final-b.bin' | ForEach-Object { , [IO.File]::ReadAllBytes((Join-Path $dir $_)) }
  165. if ((Format-Hex $clean[0]) -ne (Format-Hex $clean[1])) { $failures.Add('buffers A and B differ before people are drawn') }
  166. $shown = $visibleLog[$visibleLog.Count - 1]
  167. $hiddenIndex = 1 - $shown
  168. $expectHidden = Get-ExpectedBitmap $clean[$hiddenIndex] $frames[$frames.Count - 1]
  169. $expectShown = Get-ExpectedBitmap $clean[$shown] $frames[$frames.Count - 2]
  170. $dots = 0
  171. foreach ($pair in @(@($final[$hiddenIndex], $expectHidden, 'hidden'), @($final[$shown], $expectShown, 'visible'))) {
  172. $bad = 0
  173. for ($b = 0; $b -lt $bitmapSize; $b++) {
  174. if ($pair[0][$b] -ne $pair[1][$b]) { $bad++ }
  175. }
  176. if ($bad -gt 0) { $failures.Add("final $($pair[2]) bitmap has $bad bytes that differ from clean map plus people") }
  177. }
  178. for ($b = 0; $b -lt $bitmapSize; $b++) { if ($final[$hiddenIndex][$b] -ne $clean[$hiddenIndex][$b]) { $dots++ } }
  179. $sectors = @(0..($count - 1) | ForEach-Object { [int]($first[$_] -ge 40) + 2 * [int]($first[32 + $_] -ge 25) } | Sort-Object -Unique)
  180. if ($sectors.Count -ne 4) { $failures.Add("people start in only $($sectors.Count) of 4 screens") }
  181. $seconds = [Math]::Round($frames.Count / 60.0, 1)
  182. 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)
  183. Write-Host ("Logged {0} movement frames (~{1} s NTSC). Steps per person: {2}" -f $frames.Count, $seconds, ($moves -join ' '))
  184. if ($failures.Count -gt 0) {
  185. $failures | Select-Object -First 10 | ForEach-Object { Write-Host " $_" }
  186. Write-Host '[FAIL] people check' -ForegroundColor Red
  187. exit 1
  188. }
  189. Write-Host '[PASS] 16 people spread over 4 screens move one pixel at a time, never stand on water or stone; both buffers hold only the clean map plus the right dots; one flip per frame.' -ForegroundColor Green
  190. exit 0

Powered by TurnKey Linux.