25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

209 satır
13KB

  1. <#
  2. .SYNOPSIS
  3. Check the keyboard matrix decoding of the C64 build headless in VICE.
  4. .DESCRIPTION
  5. VICE's monitor cannot press keys, so each scenario fakes held keys instead:
  6. tracepoints on the instructions that store the port B row bits (key_row_cursor,
  7. key_row_lshift, key_row_rshift, key_row_space in src/keys.inc) run "r a = <bits>",
  8. as if those keys were held down. At keys_read the script logs scroll_dx, scroll_dy and
  9. the frames that reached it; it counts space_ready hits. (The M3.1 border-colour
  10. direction indicator was removed in M3.3, so the border is no longer checked.)
  11. Scenarios: no key, right, left (right SHIFT), down, up (left SHIFT), down-right,
  12. up-left, SPACE held from frame 60 on (must regenerate exactly once), P held from
  13. frame 60 on (R8: must step to the next person exactly once), + held from frame 60 on
  14. (M7.3: repeats, so the people count must climb well past 16; the status count may
  15. lag by up to 3 while it climbs) and - held from frame 60
  16. on (must stop at 1 person). In every scenario the
  17. people's scores must be 3-18, every name 3-10 letters (screen codes A-Z, then spaces)
  18. and every sex 0 or 1; status rows 20-21 (text screen 1 of buffer A) must show the
  19. shown person's number, the people count, name, M or F, and six scores.
  20. Exit codes: 0 = pass, 1 = fail, 2 = a tool is missing.
  21. .EXAMPLE
  22. pwsh -File tools/check-keys.ps1
  23. #>
  24. [CmdletBinding()]
  25. param([switch]$NoBuild, [int]$TimeoutSeconds = 180)
  26. . (Join-Path $PSScriptRoot 'lib.ps1')
  27. function Get-Label([hashtable]$Target, [string]$Symbol) {
  28. foreach ($line in Get-Content -LiteralPath (Get-RepoPath $Target.build.labels)) {
  29. $parts = $line -split '\s+'
  30. if ($parts.Count -ge 3 -and $parts[2].TrimStart('.') -eq $Symbol) { return [Convert]::ToInt32($parts[1], 16) }
  31. }
  32. throw "Symbol '$Symbol' not found in $($Target.build.labels)."
  33. }
  34. try {
  35. $project = Get-ProjectConfig
  36. $target = Get-ProjectTargets $project 'c64'
  37. if (-not $NoBuild) { Invoke-TargetBuild $project $target @{} }
  38. $emu = Get-ToolPath $project $target.emulator.binary
  39. } catch [MissingToolException] {
  40. Write-Host $_.Exception.Message -ForegroundColor Yellow
  41. exit 2
  42. }
  43. $hooks = @{}
  44. foreach ($s in 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space', 'key_row_p', 'keys_read', 'space_ready', 'scroll_dx',
  45. 'people_attr', 'shown_person', 'people_name', 'people_sex', 'people_count') {
  46. $hooks[$s] = Get-Label $target $s
  47. }
  48. # Row bits are active low. Cursor column: RIGHT = bit 2, DOWN = bit 7.
  49. # Left SHIFT = bit 7 of its column, right SHIFT and SPACE = bit 4 of theirs.
  50. $scenarios = @(
  51. @{ name = 'no key'; cursor = 'ff'; lshift = 'ff'; rshift = 'ff'; dx = 0; dy = 0 }
  52. @{ name = 'CRSR RIGHT'; cursor = 'fb'; lshift = 'ff'; rshift = 'ff'; dx = 1; dy = 0 }
  53. @{ name = 'right SHIFT+CRSR RIGHT'; cursor = 'fb'; lshift = 'ff'; rshift = 'ef'; dx = -1; dy = 0 }
  54. @{ name = 'CRSR DOWN'; cursor = '7f'; lshift = 'ff'; rshift = 'ff'; dx = 0; dy = 1 }
  55. @{ name = 'left SHIFT+CRSR DOWN'; cursor = '7f'; lshift = '7f'; rshift = 'ff'; dx = 0; dy = -1 }
  56. @{ name = 'CRSR RIGHT+DOWN'; cursor = '7b'; lshift = 'ff'; rshift = 'ff'; dx = 1; dy = 1 }
  57. @{ name = 'left SHIFT+RIGHT+DOWN'; cursor = '7b'; lshift = '7f'; rshift = 'ff'; dx = -1; dy = -1 }
  58. @{ name = 'SPACE held'; cursor = 'ff'; lshift = 'ff'; rshift = 'ff'; space = 'ef'; spaceAfter = 60; dx = 0; dy = 0; regenerations = 1 }
  59. @{ name = 'P held'; cursor = 'ff'; lshift = 'ff'; rshift = 'ff'; p = 'fd'; pAfter = 60; dx = 0; dy = 0; person = 1 }
  60. @{ name = '+ held'; cursor = 'ff'; lshift = 'ff'; rshift = 'ff'; p = 'fe'; pAfter = 60; dx = 0; dy = 0; countMin = 40; countMax = 255; statusLag = 3 }
  61. @{ name = '- held'; cursor = 'ff'; lshift = 'ff'; rshift = 'ff'; p = 'f7'; pAfter = 60; dx = 0; dy = 0; countMin = 1; countMax = 1 }
  62. )
  63. $STATUS_LINE4 = 0x4000 + 0x0400 + 20 * 40 # buffer A, text screen 1, rows 20-21 (src/status.inc)
  64. # Scores and names: one page per score and per letter, indexed by person (src/people.inc).
  65. $attrPages = @(0..5 | ForEach-Object { $hooks.people_attr + 256 * $_ })
  66. $namePages = @(0..9 | ForEach-Object { $hooks.people_name + 256 * $_ })
  67. $dumpStarts = @($hooks.shown_person, $STATUS_LINE4, $hooks.people_sex, $hooks.people_count) + $attrPages + $namePages
  68. $root = Get-RepoPath 'build/check-keys'
  69. if (Test-Path -LiteralPath $root) { Remove-Item -LiteralPath $root -Recurse -Force }
  70. $failed = 0
  71. $index = 0
  72. foreach ($sc in $scenarios) {
  73. $index++
  74. $dir = Join-Path $root ("{0:d2}" -f $index)
  75. New-Item -ItemType Directory -Force -Path $dir | Out-Null
  76. $mon = [System.Collections.Generic.List[string]]::new()
  77. $mon.Add('logname "monitor.log"'); $mon.Add('log on')
  78. $n = 0
  79. $add = {
  80. param($addr, $cmd)
  81. $script:n++
  82. $mon.Add(('trace exec ${0:x4}' -f $addr)); $mon.Add(('command {0} "{1}"' -f $script:n, $cmd))
  83. }
  84. & $add $hooks.key_row_cursor "r a = $($sc.cursor)"
  85. & $add $hooks.key_row_lshift "r a = $($sc.lshift)"
  86. & $add $hooks.key_row_rshift "r a = $($sc.rshift)"
  87. if ($sc.space) {
  88. & $add $hooks.key_row_space "r a = $($sc.space)"
  89. $mon.Add(('ignore {0} {1}' -f $n, $sc.spaceAfter))
  90. } else {
  91. & $add $hooks.key_row_space 'r a = ff'
  92. }
  93. if ($sc.p) {
  94. & $add $hooks.key_row_p "r a = $($sc.p)"
  95. $mon.Add(('ignore {0} {1}' -f $n, $sc.pAfter))
  96. } else {
  97. & $add $hooks.key_row_p 'r a = ff'
  98. }
  99. & $add $hooks.keys_read ('m {0:x4} {1:x4}' -f $hooks.scroll_dx, ($hooks.scroll_dx + 1))
  100. foreach ($pg in $attrPages) { & $add $hooks.keys_read ('m {0:x4} {1:x4}' -f $pg, ($pg + 15)) }
  101. & $add $hooks.keys_read ('m {0:x4} {0:x4}' -f $hooks.shown_person)
  102. & $add $hooks.keys_read ('m {0:x4} {0:x4}' -f $hooks.people_count)
  103. & $add $hooks.keys_read ('m {0:x4} {1:x4}' -f $STATUS_LINE4, ($STATUS_LINE4 + 79))
  104. foreach ($pg in $namePages) { & $add $hooks.keys_read ('m {0:x4} {1:x4}' -f $pg, ($pg + 15)) }
  105. & $add $hooks.keys_read ('m {0:x4} {1:x4}' -f $hooks.people_sex, ($hooks.people_sex + 15))
  106. $spaceCheckpoint = $n + 1
  107. & $add $hooks.space_ready 'r'
  108. Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $mon
  109. Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
  110. $cycles = if ($sc.space) { 26000000 } else { 12000000 }
  111. $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target)
  112. $vargs += @('-limitcycles', [string]$cycles, '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target))
  113. $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } }
  114. $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru `
  115. -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err')
  116. if (-not $p.WaitForExit($TimeoutSeconds * 1000)) { $p.Kill($true) }
  117. $log = Join-Path $dir 'monitor.log'
  118. $scroll = $null; $frames = 0; $regenerations = 0
  119. $dumps = @{}; $dumpAddr = -1 # last frame's dumps: address -> bytes (a dump spans several lines)
  120. if (Test-Path -LiteralPath $log) {
  121. foreach ($line in Get-Content -LiteralPath $log) {
  122. if ($line -match '^>C:([0-9a-f]{4})\s+((?:[0-9a-f]{2} )+)') {
  123. $a = [Convert]::ToInt32($Matches[1], 16)
  124. $bytes = @($Matches[2].Trim() -split ' ' | ForEach-Object { [Convert]::ToByte($_, 16) })
  125. if ($a -in $dumpStarts) { $dumpAddr = $a; $dumps[$a] = [System.Collections.Generic.List[byte]]::new() }
  126. elseif ($a -ne $hooks.scroll_dx -and $dumpAddr -ge 0 -and $a -eq $dumpAddr + $dumps[$dumpAddr].Count) { }
  127. else { $dumpAddr = -1 }
  128. if ($dumpAddr -ge 0) { $dumps[$dumpAddr].AddRange([byte[]]$bytes) }
  129. }
  130. if ($line -match ('^#{0} \(Trace' -f $spaceCheckpoint)) { $regenerations++ }
  131. elseif ($line -match ('^>C:{0:x4}\s+([0-9a-f]{{2}}) ([0-9a-f]{{2}})' -f $hooks.scroll_dx)) {
  132. $scroll = @([Convert]::ToByte($Matches[1], 16), [Convert]::ToByte($Matches[2], 16)) | ForEach-Object { if ($_ -ge 128) { $_ - 256 } else { [int]$_ } }; $frames++
  133. }
  134. }
  135. }
  136. $problems = @()
  137. if ($frames -lt 30) { $problems += "only $frames frames reached keys_read" }
  138. elseif ($scroll[0] -ne $sc.dx -or $scroll[1] -ne $sc.dy) { $problems += "scroll ($($scroll[0]),$($scroll[1])), expected ($($sc.dx),$($sc.dy))" }
  139. # R8: scores 3-18, names and sexes; rows 20-21 show "Pnn", name, M/F and the six scores.
  140. $wantPerson = if ($sc.person) { $sc.person } else { 0 }
  141. if (@($dumpStarts | Where-Object { -not $dumps.ContainsKey($_) -or $dumps[$_].Count -eq 0 }) ) { $problems += 'scores, names or status lines not logged' }
  142. else {
  143. # Back to the old flat layouts: scores person * 6 + k, names person * 10 + letter.
  144. $attr = [byte[]]::new(96); $names = [byte[]]::new(160)
  145. for ($i = 0; $i -lt 16; $i++) {
  146. for ($k = 0; $k -lt 6; $k++) { $attr[6 * $i + $k] = $dumps[$attrPages[$k]][$i] }
  147. for ($k = 0; $k -lt 10; $k++) { $names[10 * $i + $k] = $dumps[$namePages[$k]][$i] }
  148. }
  149. $shown = $dumps[$hooks.shown_person][0]; $line4 = $dumps[$STATUS_LINE4]
  150. $count = $dumps[$hooks.people_count][0]
  151. $wantMin = if ($sc.countMin) { $sc.countMin } else { 16 }
  152. $wantMax = if ($sc.countMax) { $sc.countMax } else { 16 }
  153. if ($count -lt $wantMin -or $count -gt $wantMax) { $problems += "$count people, expected $wantMin-$wantMax" }
  154. $known = [Math]::Min(16, [int]$count) # people whose tables were logged
  155. $bad = @($attr[0..(6 * $known - 1)] | Where-Object { $_ -lt 3 -or $_ -gt 18 })
  156. if ($bad.Count) { $problems += "$($bad.Count) scores outside 3-18" }
  157. if ($shown -ne $wantPerson) { $problems += "shown person $($shown + 1), expected $($wantPerson + 1)" }
  158. $digits = { param($at) ($line4[$at] - 0x30) * 10 + ($line4[$at + 1] - 0x30) }
  159. $digits3 = { param($at) ($line4[$at] - 0x30) * 100 + (& $digits ($at + 1)) }
  160. if ((& $digits3 1) -ne $shown + 1) { $problems += "status shows person $(& $digits3 1), expected $($shown + 1)" }
  161. if ($line4[4] -ne 0x2f) { $problems += "status has no / after the person number" }
  162. $lag = if ($sc.statusLag) { $sc.statusLag } else { 0 } # the status is refreshed every 4th frame
  163. $shownCount = & $digits3 5
  164. if ($shownCount -gt $count -or $shownCount -lt $count - $lag) { $problems += "status shows $shownCount people, expected $count" }
  165. for ($k = 0; $k -lt 6; $k++) {
  166. if ((& $digits (43 + 6 * $k)) -ne $attr[6 * $shown + $k]) { $problems += "status score $($k + 1) shows $(& $digits (43 + 6 * $k)), expected $($attr[6 * $shown + $k])"; break }
  167. }
  168. $sexes = $dumps[$hooks.people_sex]
  169. $toText = { param($codes) -join ($codes | ForEach-Object { if ($_ -ge 1 -and $_ -le 26) { [char](0x40 + $_) } elseif ($_ -eq 0x20) { ' ' } else { '?' } }) }
  170. for ($i = 0; $i -lt $known; $i++) {
  171. $n = & $toText $names[(10 * $i)..(10 * $i + 9)]
  172. if ($n -cnotmatch '^[A-Z]{3,10} *$') { $problems += "person $($i + 1) name '$n' is not 3-10 letters"; break }
  173. if ($sexes[$i] -gt 1) { $problems += "person $($i + 1) sex $($sexes[$i])"; break }
  174. }
  175. $shownName = & $toText $names[(10 * $shown)..(10 * $shown + 9)]
  176. $statusName = & $toText $line4[9..18]
  177. if ($statusName -ne $shownName) { $problems += "status name '$statusName', expected '$shownName'" }
  178. $sexIndex = [Math]::Min(1, [int]$sexes[$shown])
  179. $wantSex = @(13, 6)[$sexIndex] # screen codes M, F
  180. if ($line4[20] -ne $wantSex) { $problems += "status sex letter $($line4[20]), expected $wantSex" }
  181. $females = @($sexes[0..($known - 1)] | Where-Object { $_ -eq 1 }).Count
  182. $scoresText = '{0} {1}, {2}; {3} people, first {5}: {4} female' -f $shownName.Trim(), 'MF'[$sexIndex], ($attr[(6 * $shown)..(6 * $shown + 5)] -join ' '), $count, $females, $known
  183. }
  184. $wantRegen = if ($sc.regenerations) { $sc.regenerations } else { 0 }
  185. if ($regenerations -ne $wantRegen) { $problems += "$regenerations regenerations, expected $wantRegen" }
  186. if ($problems.Count -gt 0) {
  187. $failed++
  188. Write-Host (" FAIL {0}: {1}" -f $sc.name, ($problems -join '; ')) -ForegroundColor Red
  189. } else {
  190. $extra = if ($sc.space) { ", $regenerations regeneration while held" } else { '' }
  191. $extra += ", person $($wantPerson + 1) shown: $scoresText"
  192. Write-Host (" ok {0}: scroll ({1},{2}), {3} frames{4}" -f $sc.name, $scroll[0], $scroll[1], $frames, $extra)
  193. }
  194. }
  195. if ($failed -gt 0) { Write-Host "[FAIL] $failed of $($scenarios.Count) key scenarios" -ForegroundColor Red; exit 1 }
  196. Write-Host "[PASS] all $($scenarios.Count) key scenarios decode correctly" -ForegroundColor Green
  197. exit 0

Powered by TurnKey Linux.