<# .SYNOPSIS Build each target, run it headless in VICE, and check that the expected screen appears. .DESCRIPTION For every target in project.json (resolved.targets) that has a "smoke" section: 1. builds the artifact (and disk image, if any); 2. starts VICE minimized, in warp mode, with sound off and a private config file, so your own VICE settings are never read or changed; 3. sets non-stopping monitor tracepoints on the program's "ready" point. When the program reaches it, VICE saves the listed memory ranges (screen RAM, a VDC probe, mode flags) to build/smoke//*.bin; 4. lets VICE quit after a fixed number of cycles (-limitcycles) and saves a screenshot; 5. compares the saved memory with the expected text and bytes. Exit codes: 0 = all automated checks passed, 1 = a check failed, 2 = a tool is missing, 3 = nothing automated to run (manual verification only). .EXAMPLE pwsh -File tools/smoke.ps1 pwsh -File tools/smoke.ps1 -Target c64 #> [CmdletBinding()] param( [string]$Target, [switch]$NoBuild, [int]$TimeoutSeconds = 180 ) . (Join-Path $PSScriptRoot 'lib.ps1') function Resolve-SmokeAddress([hashtable]$Target, [string]$Symbol, [string]$Address) { if ($Address) { return [Convert]::ToInt32($Address, 16) } $labels = Get-RepoPath $Target.build.labels if (-not (Test-Path -LiteralPath $labels)) { throw "Label file $($Target.build.labels) not found; cannot resolve symbol '$Symbol'." } foreach ($line in Get-Content -LiteralPath $labels) { $parts = $line -split '\s+' if ($parts.Count -ge 3 -and $parts[2].TrimStart('.') -eq $Symbol.TrimStart('.')) { return [Convert]::ToInt32($parts[1], 16) } } throw "Symbol '$Symbol' not found in $($Target.build.labels). Keep the smoke hook in the source (see AGENTS.md)." } function ConvertFrom-ScreenCodes([byte[]]$Codes, [string]$Encoding) { $chars = foreach ($b in $Codes) { $c = $b -band 0x7F if ($c -eq 0) { '@' } elseif ($c -ge 1 -and $c -le 26) { if ($Encoding -eq 'screen-mixed') { [char](0x60 + $c) } else { [char](0x40 + $c) } } elseif ($c -ge 0x1B -and $c -le 0x1F) { [char](0x40 + $c) } elseif ($c -ge 0x20 -and $c -le 0x3F) { [char]$c } elseif ($c -ge 0x41 -and $c -le 0x5A -and $Encoding -eq 'screen-mixed') { [char]$c } else { '.' } } -join $chars } function Invoke-TargetSmoke([hashtable]$Project, [hashtable]$Target) { $smoke = $Target.smoke $dir = Get-RepoPath "build/smoke/$($Target.id)" if (Test-Path -LiteralPath $dir) { Remove-Item -LiteralPath $dir -Recurse -Force } New-Item -ItemType Directory -Force -Path $dir | Out-Null $readyAddr = Resolve-SmokeAddress $Target $smoke.ready.symbol $smoke.ready.address $mon = [System.Collections.Generic.List[string]]::new() $n = 0 foreach ($d in @($smoke.dumps)) { $n++ $start = Resolve-SmokeAddress $Target $d.symbol $d.address $end = $start + [int]$d.length - 1 $mon.Add(('trace {0} ${1:x4}' -f $smoke.ready.kind, $readyAddr)) $mon.Add(('command {0} "bsave \"{1}.bin\" 0 {2:x4} {3:x4}"' -f $n, $d.name, $start, $end)) } Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $mon Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value '' $emu = Get-ToolPath $Project $Target.emulator.binary $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $Target) + @($Target.emulator.args) $vargs += @('-limitcycles', [string]$smoke.limitCycles, '-exitscreenshot', 'screen.png') if ($Target.emulator.binary -eq 'x128') { $vargs += @('-exitscreenshotvicii', 'vicii.png') } $vargs += @('-moncommands', 'monitor.txt') if ($Target.emulator.autostart -eq 'disk') { $vargs += @('-autostart', (Get-AutostartFile $Target)) } else { if ($Target.disk) { $vargs += @('-8', (Get-RepoPath $Target.disk.image)) } $vargs += @('-autostartprgmode', '1', '-autostart', (Get-AutostartFile $Target)) } $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + ($_ -replace '"', '\"') + '"' } else { $_ } } Set-Content -LiteralPath (Join-Path $dir 'command.txt') -Value ("`"$emu`" " + ($quoted -join ' ')) Write-Host "[$($Target.id)] running $($Target.emulator.binary) headless (limit $($smoke.limitCycles) cycles)" $sw = [Diagnostics.Stopwatch]::StartNew() $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) return @{ target = $Target.id; status = 'FAIL'; seconds = [int]$sw.Elapsed.TotalSeconds; checks = @(); message = "VICE did not exit within $TimeoutSeconds s (killed)." } } $checks = @() $reached = @($smoke.dumps | Where-Object { Test-Path -LiteralPath (Join-Path $dir "$($_.name).bin") }).Count -gt 0 if (-not $reached) { $where = if ($smoke.ready.symbol) { "symbol $($smoke.ready.symbol)" } else { "store to `$$($smoke.ready.address)" } return @{ target = $Target.id; status = 'FAIL'; seconds = [int]$sw.Elapsed.TotalSeconds; checks = @(); message = "Program never reached its ready point ($where) within $($smoke.limitCycles) cycles. See build/smoke/$($Target.id)/vice.log and screen.png." } } foreach ($c in @($smoke.checks)) { $file = Join-Path $dir "$($c.dump).bin" $result = @{ description = $c.description; pass = $false; detail = '' } if (-not (Test-Path -LiteralPath $file)) { $result.detail = "dump '$($c.dump)' missing"; $checks += $result; continue } $data = [IO.File]::ReadAllBytes($file) $expected = if ($null -ne $c.text) { ConvertTo-ScreenCodes $c.text $c.encoding } else { [byte[]]($c.bytes -split '\s+' | Where-Object { $_ } | ForEach-Object { [Convert]::ToByte($_, 16) }) } $offset = [int]$c.offset if ($offset + $expected.Length -gt $data.Length) { $result.detail = "dump '$($c.dump)' too short"; $checks += $result; continue } $actual = [byte[]]$data[$offset..($offset + $expected.Length - 1)] $result.pass = ((Format-Hex $actual) -eq (Format-Hex $expected)) if ($null -ne $c.text) { $result.detail = "expected '$($c.text)', found '$(ConvertFrom-ScreenCodes $actual $c.encoding)'" } else { $result.detail = "expected $(Format-Hex $expected), found $(Format-Hex $actual)" } $checks += $result } $status = if (@($checks | Where-Object { -not $_.pass }).Count -eq 0) { 'PASS' } else { 'FAIL' } $shots = @('screen.png', 'vicii.png') | Where-Object { Test-Path -LiteralPath (Join-Path $dir $_) } | ForEach-Object { "build/smoke/$($Target.id)/$_" } @{ target = $Target.id; status = $status; seconds = [int]$sw.Elapsed.TotalSeconds; checks = $checks; screenshots = @($shots); message = $smoke.expectedScreen } } $exitCode = 0 try { $project = Get-ProjectConfig $targets = Get-ProjectTargets $project $Target $done = @{} $results = @() foreach ($t in $targets) { if (-not $NoBuild) { Invoke-TargetBuild $project $t $done } if (-not $t.smoke) { $results += @{ target = $t.id; status = 'MANUAL'; checks = @(); message = 'No automated smoke check for this target; follow docs/manual-tests.md (M0).' } continue } $results += Invoke-TargetSmoke $project $t } } catch [MissingToolException] { Write-Host "" Write-Host $_.Exception.Message -ForegroundColor Yellow exit 2 } Write-Host "" Write-Host "Smoke results" foreach ($r in $results) { $color = switch ($r.status) { 'PASS' { 'Green' } 'FAIL' { 'Red' } default { 'Yellow' } } Write-Host (" [{0}] {1}" -f $r.status, $r.target) -ForegroundColor $color foreach ($c in @($r.checks)) { Write-Host (" {0} {1}: {2}" -f ($(if ($c.pass) { 'ok ' } else { 'FAIL' })), $c.description, $c.detail) } if ($r.ContainsKey('screenshots') -and $r.screenshots) { Write-Host (" screenshot: {0}" -f ($r.screenshots -join ', ')) } if ($r.message) { Write-Host " $($r.message)" } } New-Item -ItemType Directory -Force -Path (Get-RepoPath 'build/smoke') | Out-Null $results | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath (Get-RepoPath 'build/smoke/results.json') if (@($results | Where-Object { $_.status -eq 'FAIL' }).Count -gt 0) { $exitCode = 1 } elseif (@($results | Where-Object { $_.status -eq 'PASS' }).Count -eq 0) { $exitCode = 3 } exit $exitCode