<# .SYNOPSIS Check the keyboard matrix decoding of the C64 build headless in VICE. .DESCRIPTION VICE's monitor cannot press keys, so each scenario fakes held keys instead: tracepoints on the instructions that store the port B row bits (key_row_cursor, key_row_lshift, key_row_rshift, key_row_space in src/keys.inc) run "r a = ", as if those keys were held down. At keys_read the script logs scroll_dx, scroll_dy and the frames that reached it; it counts space_ready hits. (The M3.1 border-colour direction indicator was removed in M3.3, so the border is no longer checked.) Scenarios: no key, right, left (right SHIFT), down, up (left SHIFT), down-right, up-left, and SPACE held from frame 60 on (must regenerate exactly once). Exit codes: 0 = pass, 1 = fail, 2 = a tool is missing. .EXAMPLE pwsh -File tools/check-keys.ps1 #> [CmdletBinding()] param([switch]$NoBuild, [int]$TimeoutSeconds = 180) . (Join-Path $PSScriptRoot 'lib.ps1') function Get-Label([hashtable]$Target, [string]$Symbol) { foreach ($line in Get-Content -LiteralPath (Get-RepoPath $Target.build.labels)) { $parts = $line -split '\s+' if ($parts.Count -ge 3 -and $parts[2].TrimStart('.') -eq $Symbol) { return [Convert]::ToInt32($parts[1], 16) } } throw "Symbol '$Symbol' not found in $($Target.build.labels)." } try { $project = Get-ProjectConfig $target = Get-ProjectTargets $project 'c64' if (-not $NoBuild) { Invoke-TargetBuild $project $target @{} } $emu = Get-ToolPath $project $target.emulator.binary } catch [MissingToolException] { Write-Host $_.Exception.Message -ForegroundColor Yellow exit 2 } $hooks = @{} foreach ($s in 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space', 'keys_read', 'space_ready', 'scroll_dx') { $hooks[$s] = Get-Label $target $s } # Row bits are active low. Cursor column: RIGHT = bit 2, DOWN = bit 7. # Left SHIFT = bit 7 of its column, right SHIFT and SPACE = bit 4 of theirs. $scenarios = @( @{ name = 'no key'; cursor = 'ff'; lshift = 'ff'; rshift = 'ff'; dx = 0; dy = 0 } @{ name = 'CRSR RIGHT'; cursor = 'fb'; lshift = 'ff'; rshift = 'ff'; dx = 1; dy = 0 } @{ name = 'right SHIFT+CRSR RIGHT'; cursor = 'fb'; lshift = 'ff'; rshift = 'ef'; dx = -1; dy = 0 } @{ name = 'CRSR DOWN'; cursor = '7f'; lshift = 'ff'; rshift = 'ff'; dx = 0; dy = 1 } @{ name = 'left SHIFT+CRSR DOWN'; cursor = '7f'; lshift = '7f'; rshift = 'ff'; dx = 0; dy = -1 } @{ name = 'CRSR RIGHT+DOWN'; cursor = '7b'; lshift = 'ff'; rshift = 'ff'; dx = 1; dy = 1 } @{ name = 'left SHIFT+RIGHT+DOWN'; cursor = '7b'; lshift = '7f'; rshift = 'ff'; dx = -1; dy = -1 } @{ name = 'SPACE held'; cursor = 'ff'; lshift = 'ff'; rshift = 'ff'; space = 'ef'; spaceAfter = 60; dx = 0; dy = 0; regenerations = 1 } ) $root = Get-RepoPath 'build/check-keys' if (Test-Path -LiteralPath $root) { Remove-Item -LiteralPath $root -Recurse -Force } $failed = 0 $index = 0 foreach ($sc in $scenarios) { $index++ $dir = Join-Path $root ("{0:d2}" -f $index) New-Item -ItemType Directory -Force -Path $dir | Out-Null $mon = [System.Collections.Generic.List[string]]::new() $mon.Add('logname "monitor.log"'); $mon.Add('log on') $n = 0 $add = { param($addr, $cmd) $script:n++ $mon.Add(('trace exec ${0:x4}' -f $addr)); $mon.Add(('command {0} "{1}"' -f $script:n, $cmd)) } & $add $hooks.key_row_cursor "r a = $($sc.cursor)" & $add $hooks.key_row_lshift "r a = $($sc.lshift)" & $add $hooks.key_row_rshift "r a = $($sc.rshift)" if ($sc.space) { & $add $hooks.key_row_space "r a = $($sc.space)" $mon.Add(('ignore {0} {1}' -f $n, $sc.spaceAfter)) } else { & $add $hooks.key_row_space 'r a = ff' } & $add $hooks.keys_read ('m {0:x4} {1:x4}' -f $hooks.scroll_dx, ($hooks.scroll_dx + 1)) $spaceCheckpoint = $n + 1 & $add $hooks.space_ready 'r' Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $mon Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value '' $cycles = if ($sc.space) { 20000000 } else { 6000000 } $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target) $vargs += @('-limitcycles', [string]$cycles, '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target)) $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } } $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru ` -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err') if (-not $p.WaitForExit($TimeoutSeconds * 1000)) { $p.Kill($true) } $log = Join-Path $dir 'monitor.log' $scroll = $null; $frames = 0; $regenerations = 0 if (Test-Path -LiteralPath $log) { foreach ($line in Get-Content -LiteralPath $log) { if ($line -match ('^#{0} \(Trace' -f $spaceCheckpoint)) { $regenerations++ } elseif ($line -match ('^>C:{0:x4}\s+([0-9a-f]{{2}}) ([0-9a-f]{{2}})' -f $hooks.scroll_dx)) { $scroll = @([Convert]::ToByte($Matches[1], 16), [Convert]::ToByte($Matches[2], 16)) | ForEach-Object { if ($_ -ge 128) { $_ - 256 } else { [int]$_ } }; $frames++ } } } $problems = @() if ($frames -lt 30) { $problems += "only $frames frames reached keys_read" } elseif ($scroll[0] -ne $sc.dx -or $scroll[1] -ne $sc.dy) { $problems += "scroll ($($scroll[0]),$($scroll[1])), expected ($($sc.dx),$($sc.dy))" } $wantRegen = if ($sc.regenerations) { $sc.regenerations } else { 0 } if ($regenerations -ne $wantRegen) { $problems += "$regenerations regenerations, expected $wantRegen" } if ($problems.Count -gt 0) { $failed++ Write-Host (" FAIL {0}: {1}" -f $sc.name, ($problems -join '; ')) -ForegroundColor Red } else { $extra = if ($sc.space) { ", $regenerations regeneration while held" } else { '' } Write-Host (" ok {0}: scroll ({1},{2}), {3} frames{4}" -f $sc.name, $scroll[0], $scroll[1], $frames, $extra) } } if ($failed -gt 0) { Write-Host "[FAIL] $failed of $($scenarios.Count) key scenarios" -ForegroundColor Red; exit 1 } Write-Host "[PASS] all $($scenarios.Count) key scenarios decode correctly" -ForegroundColor Green exit 0