Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

203 linhas
13KB

  1. <#
  2. .SYNOPSIS
  3. Check the map/status raster split of the C64 build headless in VICE.
  4. .DESCRIPTION
  5. Runs x64sc (NTSC, warp, private config) with monitor tracepoints:
  6. - at split_store (src/status.inc): logs the raster line and cycle where the three
  7. mode writes start, the map's y-scroll (map_d011) and the patched split delay,
  8. every frame;
  9. - on the key_row_* instructions (src/keys.inc): fakes "no key" (keys typed on the PC
  10. while a headless VICE has focus would otherwise reach the program), then holds
  11. CRSR DOWN from frame 30 until the view is at cell row 3, then SHIFT+CRSR DOWN
  12. until it is back at row 0, then no key, so every map y-scroll value (0-7) is split;
  13. - at people_moved: logs the view position, the people table with move_frame, and the
  14. people-in-view digits of text screen 1 in both banks.
  15. Then checks:
  16. - every split starts between line 210 cycle 53 and line 210 cycle 64, for every
  17. y-scroll value, and every y-scroll value 0-7 was seen. This window was measured
  18. at y-scroll 3 by sweeping the delay: starts at 210:40-51 damaged the last map
  19. line, 211:0-11 damaged the first text line, 210:50-211:1 were clean;
  20. - a split happens on every frame (no missed interrupt);
  21. - the status is updated every 4th frame (move_frame a multiple of 4, after
  22. people_moved): in the frame after each update, the digits in both banks equal the
  23. number of people whose dot is at least partly inside the visible map window
  24. (24-row, 38-column display; buffer pixel rows fy..fy+155 and hires pixel columns
  25. fx..fx+303), counted here from the people table and view of the updating frame;
  26. - in the exit screenshot, the 40 text lines (raster 211-250) contain only brown
  27. and light red, status rows 20, 21, 22 and 24 are lit and row 23 is empty, and
  28. the last map line (raster 210) uses no colour foreign to the map. This is a
  29. coarse check: in the delay sweep it caught damage from writes at 210:40-46 but
  30. not subtler damage, so the timing window above is the main guard.
  31. Exit codes: 0 = pass, 1 = fail, 2 = a tool is missing.
  32. .EXAMPLE
  33. pwsh -File tools/check-split.ps1
  34. #>
  35. [CmdletBinding()]
  36. param([int]$Cycles = 20000000, [switch]$NoBuild, [int]$TimeoutSeconds = 180)
  37. . (Join-Path $PSScriptRoot 'lib.ps1')
  38. function Get-Label([hashtable]$Target, [string]$Symbol) {
  39. foreach ($line in Get-Content -LiteralPath (Get-RepoPath $Target.build.labels)) {
  40. $parts = $line -split '\s+'
  41. if ($parts.Count -ge 3 -and $parts[2].TrimStart('.') -eq $Symbol) { return [Convert]::ToInt32($parts[1], 16) }
  42. }
  43. throw "Symbol '$Symbol' not found in $($Target.build.labels)."
  44. }
  45. try {
  46. $project = Get-ProjectConfig
  47. $target = Get-ProjectTargets $project 'c64'
  48. if (-not $NoBuild) { Invoke-TargetBuild $project $target @{} }
  49. $emu = Get-ToolPath $project $target.emulator.binary
  50. } catch [MissingToolException] {
  51. Write-Host $_.Exception.Message -ForegroundColor Yellow
  52. exit 2
  53. }
  54. $dir = Get-RepoPath 'build/check-split'
  55. if (Test-Path -LiteralPath $dir) { Remove-Item -LiteralPath $dir -Recurse -Force }
  56. New-Item -ItemType Directory -Force -Path $dir | Out-Null
  57. $L = @{}
  58. foreach ($sym in 'split_store', 'split_delay', 'people_moved', 'smoke_ready', 'people_cx', 'map_d011', 'move_frame', 'view_cx', 'view_cy', 'view_fy',
  59. 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space') { $L[$sym] = Get-Label $target $sym }
  60. $digits = @(0x4400, 0x8400) | ForEach-Object { $_ + 22 * 40 + 15 } # row 22, after "PEOPLE IN VIEW "
  61. $PHASE = 0xCFF0 # free RAM: scroll phase 0 still, 1 down, 2 up, 3 still
  62. $cond = { param($pairs) ($pairs | ForEach-Object { '(@cpu:${0:x4} == ${1:x2})' -f $_[0], $_[1] }) -join ' && ' }
  63. if ($L.move_frame -ne $L.people_cx + 80) { throw 'move_frame must follow the five people tables (src/people.inc)' }
  64. # Checkpoints 1-5 keep their numbers: the parser below relies on them.
  65. $mon = @(
  66. 'logname "monitor.log"', 'log on'
  67. ('trace exec ${0:x4}' -f $L.split_store), ('command 1 "m {0:x4} {0:x4}"' -f $L.map_d011)
  68. ('trace exec ${0:x4}' -f $L.people_moved), ('command 2 "m {0:x4} {1:x4}"' -f $L.view_cx, ($L.view_cx + 3))
  69. ('trace exec ${0:x4}' -f $L.people_moved), ('command 3 "m {0:x4} {1:x4}"' -f $L.people_cx, ($L.people_cx + 80))
  70. ('trace exec ${0:x4}' -f $L.people_moved), ('command 4 "m {0:x4} {1:x4}"' -f $digits[0], ($digits[0] + 1))
  71. ('trace exec ${0:x4}' -f $L.people_moved), ('command 5 "m {0:x4} {1:x4}"' -f $digits[1], ($digits[1] + 1))
  72. ('trace exec ${0:x4}' -f $L.split_store), ('command 6 "m {0:x4} {0:x4}"' -f ($L.split_delay + 1))
  73. ('trace exec ${0:x4}' -f $L.smoke_ready), ('command 7 "> {0:x4} 00"' -f $PHASE)
  74. )
  75. $n = 7
  76. foreach ($row in 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space') {
  77. $n++; $mon += ('trace exec ${0:x4}' -f $L[$row]); $mon += ('command {0} "r a = ff"' -f $n)
  78. }
  79. $n++; $mon += ('trace exec ${0:x4} if {1}' -f $L.key_row_cursor, (& $cond @(, @($PHASE, 1)))); $mon += ('command {0} "r a = 7f"' -f $n)
  80. $n++; $mon += ('trace exec ${0:x4} if {1}' -f $L.key_row_cursor, (& $cond @(, @($PHASE, 2)))); $mon += ('command {0} "r a = 7f"' -f $n)
  81. $n++; $mon += ('trace exec ${0:x4} if {1}' -f $L.key_row_lshift, (& $cond @(, @($PHASE, 2)))); $mon += ('command {0} "r a = 7f"' -f $n)
  82. # Phase changes, latest first so one frame moves on at most one phase.
  83. $n++; $mon += ('trace exec ${0:x4} if {1}' -f $L.people_moved, (& $cond @(@($PHASE, 2), @($L.view_cy, 0), @($L.view_fy, 0)))); $mon += ('command {0} "> {1:x4} 03"' -f $n, $PHASE)
  84. $n++; $mon += ('trace exec ${0:x4} if {1}' -f $L.people_moved, (& $cond @(@($PHASE, 1), @($L.view_cy, 3), @($L.view_fy, 0)))); $mon += ('command {0} "> {1:x4} 02"' -f $n, $PHASE)
  85. $n++; $mon += ('trace exec ${0:x4} if {1}' -f $L.people_moved, (& $cond @(@($PHASE, 0), @($L.move_frame, 30)))); $mon += ('command {0} "> {1:x4} 01"' -f $n, $PHASE)
  86. Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $mon
  87. Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
  88. $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target)
  89. $vargs += @('-limitcycles', [string]$Cycles, '-exitscreenshot', 'screen.png', '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target))
  90. $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } }
  91. Write-Host "[c64] running x64sc headless for $Cycles cycles"
  92. $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru `
  93. -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err')
  94. if (-not $p.WaitForExit($TimeoutSeconds * 1000)) { $p.Kill($true); Write-Host '[FAIL] VICE did not exit in time.' -ForegroundColor Red; exit 1 }
  95. $failures = [System.Collections.Generic.List[string]]::new()
  96. $log = Join-Path $dir 'monitor.log'
  97. if (-not (Test-Path -LiteralPath $log)) { Write-Host '[FAIL] no monitor log' -ForegroundColor Red; exit 1 }
  98. # Split timing.
  99. $starts = [System.Collections.Generic.List[string]]::new()
  100. $splits = [System.Collections.Generic.List[hashtable]]::new()
  101. $split = $null
  102. $splitFrames = 0; $moveFrames = 0; $badCounts = 0; $checkedCounts = 0
  103. $lastCheckpoint = 0
  104. $frame = $null; $prev = $null
  105. # Independent count: a person is in view if any part of their dot is in the visible window.
  106. function Get-InView($f) {
  107. $count = 0
  108. for ($i = 0; $i -lt 16; $i++) {
  109. $y = 8 * ($f.people[32 + $i] - $f.cy) + $f.people[48 + $i] - $f.fy
  110. $x = 8 * ($f.people[$i] - $f.cx) + 2 * $f.people[16 + $i]
  111. if ($y -ge 0 -and $y -le 155 -and $x + 1 -ge $f.fx -and $x -le $f.fx + 303) { $count++ }
  112. }
  113. $count
  114. }
  115. foreach ($line in Get-Content -LiteralPath $log) {
  116. if ($line -match '^#(\d+) \(Trace\s+exec [0-9a-fA-F]+\)\s+(\d+)/\$\w+,\s+(\d+)/') {
  117. $lastCheckpoint = [int]$Matches[1]
  118. if ($lastCheckpoint -eq 1) {
  119. $splitFrames++
  120. $split = @{ line = [int]$Matches[2]; cycle = [int]$Matches[3] }
  121. $splits.Add($split)
  122. }
  123. if ($lastCheckpoint -eq 2) { $prev = $frame; $frame = @{ people = [System.Collections.Generic.List[byte]]::new(); digits = @() } }
  124. continue
  125. }
  126. if ($line -match '^>C:[0-9a-fA-F]{4} ((?:[0-9a-fA-F]{2} )+)') {
  127. $bytes = @($Matches[1].Trim() -split ' ' | ForEach-Object { [Convert]::ToByte($_, 16) })
  128. if ($lastCheckpoint -eq 1 -and $null -ne $split) { $split.y = $bytes[0] -band 7 }
  129. if ($lastCheckpoint -eq 6 -and $null -ne $split) { $split.delay = $bytes[0] }
  130. if ($null -eq $frame) { continue }
  131. if ($lastCheckpoint -eq 2) { $frame.cx = $bytes[0]; $frame.fx = $bytes[1]; $frame.cy = $bytes[2]; $frame.fy = $bytes[3] }
  132. if ($lastCheckpoint -eq 3) { foreach ($b in $bytes) { if ($frame.people.Count -lt 81) { $frame.people.Add($b) } } }
  133. if ($lastCheckpoint -eq 4 -or $lastCheckpoint -eq 5) { $frame.digits += , (($bytes[0] - 0x30) * 10 + ($bytes[1] - 0x30)) }
  134. if ($lastCheckpoint -eq 5) {
  135. $moveFrames++
  136. if ($null -ne $prev -and $prev.people.Count -eq 81 -and ($prev.people[80] -band 3) -eq 0) {
  137. $checkedCounts++
  138. $want = Get-InView $prev
  139. if ($frame.digits[0] -ne $want -or $frame.digits[1] -ne $want) { $badCounts++ }
  140. }
  141. }
  142. }
  143. if ($failures.Count -gt 10) { break }
  144. }
  145. $monErrors = @(Select-String -LiteralPath $log -Pattern '^ERROR|not a valid checkpoint' | Select-Object -First 3)
  146. foreach ($e in $monErrors) { $failures.Add("monitor script error: $($e.Line.Trim())") }
  147. foreach ($sp in $splits) {
  148. $starts.Add("y$($sp.y) d$($sp.delay) $($sp.line):$($sp.cycle)")
  149. if ($sp.line -ne 210 -or $sp.cycle -lt 53 -or $sp.cycle -gt 64) { $failures.Add("split at y-scroll $($sp.y) started at line $($sp.line) cycle $($sp.cycle) (allowed 210:53-210:64)") }
  150. }
  151. $seenY = @($splits | ForEach-Object { $_.y } | Where-Object { $null -ne $_ } | Sort-Object -Unique)
  152. if ($seenY.Count -ne 8) { $failures.Add("splits seen only at y-scroll $($seenY -join ', ') (expected 0-7)") }
  153. if ($splitFrames -lt 100) { $failures.Add("only $splitFrames splits logged") }
  154. if ([Math]::Abs($splitFrames - $moveFrames) -gt 2) { $failures.Add("$splitFrames splits for $moveFrames frames: an interrupt was missed") }
  155. if ($checkedCounts -lt 50) { $failures.Add("only $checkedCounts status updates checked") }
  156. if ($badCounts -gt 0) { $failures.Add("people-in-view digits differed from the people in the visible window after $badCounts of $checkedCounts status updates") }
  157. # Screenshot: raster line r is at y = r - 28 in x64sc's NTSC screenshot.
  158. Add-Type -AssemblyName System.Drawing
  159. $shot = Join-Path $dir 'screen.png'
  160. if (-not (Test-Path -LiteralPath $shot)) { $failures.Add('no exit screenshot') } else {
  161. $bmp = [System.Drawing.Bitmap]::new($shot)
  162. try {
  163. $brown = $bmp.GetPixel(2, 2).ToArgb()
  164. $textColours = @{}
  165. $textLit = @{}
  166. for ($y = 211 - 28; $y -le 250 - 28; $y++) {
  167. for ($x = 0; $x -lt $bmp.Width; $x++) {
  168. $c = $bmp.GetPixel($x, $y).ToArgb()
  169. if ($c -ne $brown) { $textColours[$c] = 1; $textLit[[int][Math]::Floor(($y - 183) / 8)] = 1 }
  170. }
  171. }
  172. if ($textColours.Count -gt 1) { $failures.Add("status area shows $($textColours.Count) colours besides brown (expected light red only)") }
  173. foreach ($row in 0, 1, 2, 4) { if (-not $textLit.ContainsKey($row)) { $failures.Add("status row $($row + 20) is empty in the screenshot") } }
  174. if ($textLit.ContainsKey(3)) { $failures.Add('the spare status row 23 is not empty') }
  175. # Map colours: brown, light red, grey, green, blue, black as rendered in the map area above.
  176. $mapColours = @{}
  177. for ($y = 51 - 28; $y -le 209 - 28; $y++) { for ($x = 0; $x -lt $bmp.Width; $x++) { $mapColours[$bmp.GetPixel($x, $y).ToArgb()] = 1 } }
  178. for ($x = 0; $x -lt $bmp.Width; $x++) {
  179. if (-not $mapColours.ContainsKey($bmp.GetPixel($x, 210 - 28).ToArgb())) { $failures.Add("last map line has a colour not used elsewhere in the map at x=$x"); break }
  180. }
  181. } finally { $bmp.Dispose() }
  182. }
  183. $distinct = ($starts | Group-Object | Sort-Object Name | ForEach-Object { "$($_.Name) x$($_.Count)" }) -join ', '
  184. Write-Host "Split starts (y-scroll, delay, line:cycle): $distinct"
  185. Write-Host "Frames: $splitFrames splits, $moveFrames people updates; people-in-view digits wrong after $badCounts of $checkedCounts status updates."
  186. if ($failures.Count -gt 0) {
  187. $failures | Select-Object -First 10 | ForEach-Object { Write-Host " $_" }
  188. Write-Host '[FAIL] split check' -ForegroundColor Red
  189. exit 1
  190. }
  191. Write-Host '[PASS] split writes land in the measured border window every frame at every y-scroll; status digits match; no foreign colours at the boundary.' -ForegroundColor Green
  192. exit 0

Powered by TurnKey Linux.