Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

281 wiersze
14KB

  1. # E07-S05: drive build/n128c.prg through scripts/launch-compiler.ps1 on a fresh
  2. # development image and check the PRG, MAP, ERR and summary it produces.
  3. #
  4. # success COUNTER.N8 -> COUNTER.PRG + COUNTER.MAP, no ERR, summary on screen
  5. # failure BAD.N8 -> BAD.ERR, no BAD.PRG/BAD.MAP, earlier outputs intact
  6. # collision COUNTER.N8 -> OUTPUT EXISTS, COUNTER.PRG byte-identical
  7. # run the generated COUNTER.PRG reports MB_ST_PASS in a second session
  8. [CmdletBinding()]
  9. param([int]$Port = 6552, [int]$TimeoutSeconds = 240)
  10. $ErrorActionPreference = "Stop"
  11. $project = Split-Path $PSScriptRoot -Parent
  12. Import-Module (Join-Path $PSScriptRoot "lib\Nova128Test.psm1") -Force
  13. & (Join-Path $PSScriptRoot "build-driver.ps1") | Out-Host
  14. $root = Join-Path $project "build\driver-test"
  15. if (Test-Path -LiteralPath $root) { Remove-Item -LiteralPath $root -Recurse -Force }
  16. New-Item -ItemType Directory -Force -Path $root | Out-Null
  17. $driver = Join-Path $project "build\n128c.prg"
  18. $work = Join-Path $root "work.d81"
  19. $c1541 = Get-ViceTool "c1541"
  20. $failures = @()
  21. # 0: prepare-disk.ps1 injects exactly the build the driver was made from.
  22. $prepared = Join-Path $root "prepared.d81"
  23. & (Join-Path $PSScriptRoot "prepare-disk.ps1") -OutputImage $prepared `
  24. -CompilerPrg $driver -RequireCompiler | Out-Host
  25. $injectedPath = Join-Path $root "prepared-n128c"
  26. & $c1541 $prepared -read "n128c" $injectedPath 2>&1 | Out-Null
  27. if (-not (Test-Path -LiteralPath $injectedPath)) {
  28. $failures += "prepare-disk.ps1 did not inject n128c"
  29. } elseif ([Convert]::ToBase64String([IO.File]::ReadAllBytes($injectedPath)) -ne
  30. [Convert]::ToBase64String([IO.File]::ReadAllBytes($driver))) {
  31. $failures += "prepare-disk.ps1 injected a different n128c"
  32. }
  33. $counterPet = Join-Path $root "counter.pet"
  34. [IO.File]::WriteAllBytes($counterPet, (ConvertTo-Nova128Petscii `
  35. (Get-Content -Raw -LiteralPath (Join-Path $project "tests\compiler\counter.n8"))))
  36. $badPet = Join-Path $root "bad.pet"
  37. [IO.File]::WriteAllBytes($badPet, (ConvertTo-Nova128Petscii `
  38. (Get-Content -Raw -LiteralPath (Join-Path $project "tests\grammar\rejected\miss_end.n8"))))
  39. $helloPet = Join-Path $root "hello.pet"
  40. [IO.File]::WriteAllBytes($helloPet, (ConvertTo-Nova128Petscii `
  41. (Get-Content -Raw -LiteralPath (Join-Path $project "tests\compiler\hello.n8"))))
  42. New-Nova128Disk -Path $work -Files @(
  43. @{ Name = "n128c"; Path = $driver; Type = "prg" },
  44. @{ Name = "counter.n8"; Path = $counterPet; Type = "seq" },
  45. @{ Name = "bad.n8"; Path = $badPet; Type = "seq" },
  46. @{ Name = "hello.n8"; Path = $helloPet; Type = "seq" })
  47. function Invoke-Compile {
  48. param([string]$Keys, [string]$Screenshot)
  49. if (Test-Path -LiteralPath $Screenshot) { Remove-Item -LiteralPath $Screenshot -Force }
  50. & (Join-Path $PSScriptRoot "launch-compiler.ps1") -CompilerPrg $driver -DiskImage $work `
  51. -SkipPrepare -Keys $Keys -KeyDelay 4000 -LimitCycles 200000000 `
  52. -Screenshot $Screenshot -Warp | Out-Host
  53. if (-not (Test-Path -LiteralPath $Screenshot)) {
  54. throw "the compiler session produced no exit screenshot"
  55. }
  56. }
  57. function Get-Listing {
  58. return ((& $c1541 -attach $work -list 2>&1) -join "`n")
  59. }
  60. function Read-DiskFile {
  61. param([string]$Name, [string]$Type, [string]$Dest)
  62. if (Test-Path -LiteralPath $Dest) { Remove-Item -LiteralPath $Dest -Force }
  63. & $c1541 $work -read "$Name,$Type" $Dest 2>&1 | Out-Null
  64. if (-not (Test-Path -LiteralPath $Dest)) { throw "could not read $Name,$Type from the disk" }
  65. return ,([IO.File]::ReadAllBytes($Dest))
  66. }
  67. function Get-ScreenText {
  68. param($Session)
  69. $ram = Read-Nova128Memory $Session 0x0400 1000
  70. $rows = for ($r = 0; $r -lt 25; $r++) {
  71. $chars = for ($c = 0; $c -lt 40; $c++) {
  72. # The C128 40-column display stores the printed bytes with bit 7
  73. # set ($88 for H, $A0 for space), so mask it before decoding.
  74. $code = $ram[$r * 40 + $c] -band 0x7F
  75. if ($code -le 0x1F) { [char]($code + 0x40) } elseif ($code -le 0x3F) { [char]$code } else { '?' }
  76. }
  77. -join $chars
  78. }
  79. return ($rows -join "`n")
  80. }
  81. # 1: the on-screen summary, read from a copy so the three launcher runs below
  82. # still start from an image without outputs.
  83. $summaryDisk = Join-Path $root "summary.d81"
  84. Copy-Item -LiteralPath $work -Destination $summaryDisk -Force
  85. $session = Start-Nova128X128 -Autostart $summaryDisk -Drive8Image $summaryDisk -Port $Port `
  86. -Keys "counter.n8\n" -KeyDelay 4000 -Screenshot (Join-Path $root "summary-screen.png")
  87. try {
  88. $screenText = ""
  89. $deadline = [DateTime]::UtcNow.AddSeconds(120)
  90. while ([DateTime]::UtcNow -lt $deadline) {
  91. Invoke-Nova128Monitor $session "x" | Out-Null
  92. Start-Sleep -Seconds 1
  93. $screenText = Get-ScreenText $session
  94. if ($screenText -match "OUTPUT COUNTER\.PRG" -and $screenText -match "0 ERRORS" -and
  95. $screenText -match "SOURCE COUNTER\.N8") { break }
  96. }
  97. } finally {
  98. Stop-Nova128X128 $session
  99. }
  100. foreach ($needle in @("NOVA128 COMPILER", "SOURCE COUNTER.N8", "OUTPUT COUNTER.PRG", "0 ERRORS")) {
  101. if ($screenText -notmatch [regex]::Escape($needle)) {
  102. $failures += "the summary did not show '$needle'"
  103. }
  104. }
  105. # 2: success through the launcher.
  106. Invoke-Compile -Keys "counter.n8\n" -Screenshot (Join-Path $root "counter-summary.png")
  107. $listing = Get-Listing
  108. if ($listing -notmatch '"counter\.prg"\s+prg') { $failures += "COUNTER.PRG is missing" }
  109. if ($listing -notmatch '"counter\.map"\s+seq') { $failures += "COUNTER.MAP is missing" }
  110. if ($listing -match '"counter\.err"') { $failures += "a clean source left COUNTER.ERR" }
  111. $prgPath = Join-Path $root "counter.prg"
  112. $prg = Read-DiskFile -Name "counter.prg" -Type "p" -Dest $prgPath
  113. $stub = [byte[]](0x0B, 0x1C, 0x0A, 0x00, 0x9E, 0x37, 0x31, 0x38, 0x31, 0x00, 0x00, 0x00)
  114. if ($prg.Length -lt 14 -or $prg[0] -ne 0x01 -or $prg[1] -ne 0x1C) {
  115. $failures += "COUNTER.PRG does not load at `$1C01"
  116. } else {
  117. for ($i = 0; $i -lt $stub.Length; $i++) {
  118. if ($prg[2 + $i] -ne $stub[$i]) {
  119. $failures += ("COUNTER.PRG stub byte {0} is {1:X2}, expected {2:X2}" -f $i, $prg[2 + $i], $stub[$i])
  120. break
  121. }
  122. }
  123. }
  124. # c1541 translates PETSCII CR to CRLF on read, so normalize before matching.
  125. $map = ([Text.Encoding]::ASCII.GetString((Read-DiskFile -Name "counter.map" -Type "s" `
  126. -Dest (Join-Path $root "counter.map"))) -replace "`r", "`n")
  127. $mapLines = $map -split "`n"
  128. if ($mapLines[0] -ne "NOVA128 MAP 1") { $failures += "MAP header is '$($mapLines[0])'" }
  129. if ($map -notmatch '(?im)^ENTRY 1c0d') { $failures += "MAP ENTRY is not 1C0D" }
  130. $sections = @{}
  131. foreach ($m in [regex]::Matches($map, '(?im)^SECTION (\w+) ([0-9a-f]{4}) ([0-9a-f]{4})')) {
  132. $sections[$m.Groups[1].Value.ToUpper()] = @{
  133. Start = [Convert]::ToInt32($m.Groups[2].Value, 16)
  134. Size = [Convert]::ToInt32($m.Groups[3].Value, 16)
  135. }
  136. }
  137. foreach ($name in @("STUB", "ENTRY", "START", "PRINT", "CODE")) {
  138. if (-not $sections.ContainsKey($name)) { $failures += "MAP has no $name section" }
  139. }
  140. $ordered = @("ENTRY", "START", "PRINT", "CODE")
  141. if ($sections.ContainsKey("DATA")) { $ordered += "DATA" }
  142. if ($sections.ContainsKey("STUB") -and
  143. ($sections.STUB.Start -ne 0x1C01 -or $sections.STUB.Size -ne 0x0C)) {
  144. $failures += "MAP STUB section is wrong"
  145. }
  146. $expectedStart = 0x1C0D
  147. foreach ($name in $ordered) {
  148. if (-not $sections.ContainsKey($name)) { continue }
  149. if ($sections[$name].Start -ne $expectedStart) {
  150. $failures += ("{0} starts at {1:X4}, expected {2:X4}" -f $name, $sections[$name].Start, $expectedStart)
  151. }
  152. if ($sections[$name].Size -le 0) { $failures += "$name section is empty" }
  153. $expectedStart += $sections[$name].Size
  154. }
  155. if ($sections.ContainsKey("CODE")) {
  156. $expectedLen = 2 + $sections.STUB.Size
  157. foreach ($name in $ordered) { $expectedLen += $sections[$name].Size }
  158. if ($prg.Length -ne $expectedLen) {
  159. $failures += ("PRG is {0} bytes, MAP accounts for {1}" -f $prg.Length, $expectedLen)
  160. }
  161. }
  162. # 3: a syntax error writes the ERR file and no PRG, and leaves COUNTER.PRG alone.
  163. Invoke-Compile -Keys "bad.n8\n" -Screenshot (Join-Path $root "bad-summary.png")
  164. $listing = Get-Listing
  165. if ($listing -notmatch '"bad\.err"\s+seq') { $failures += "BAD.ERR is missing" }
  166. if ($listing -match '"bad\.prg"') { $failures += "a failing source produced BAD.PRG" }
  167. if ($listing -match '"bad\.map"') { $failures += "a failing source produced BAD.MAP" }
  168. $errText = [Text.Encoding]::ASCII.GetString((Read-DiskFile -Name "bad.err" -Type "s" `
  169. -Dest (Join-Path $root "bad.err")))
  170. if ($errText -notmatch '^E\d{3} BAD\.N8 \d+ ') { $failures += "BAD.ERR has no catalogued diagnostic" }
  171. if ($listing -notmatch '"counter\.prg"\s+prg') { $failures += "the failure removed COUNTER.PRG" }
  172. $afterFailure = Read-DiskFile -Name "counter.prg" -Type "p" -Dest (Join-Path $root "counter-after.prg")
  173. if ([Convert]::ToBase64String($afterFailure) -ne [Convert]::ToBase64String($prg)) {
  174. $failures += "the failing run changed COUNTER.PRG"
  175. }
  176. # 4: an existing output is refused and the file stays byte-identical.
  177. Invoke-Compile -Keys "counter.n8\n" -Screenshot (Join-Path $root "collision-summary.png")
  178. $listing = Get-Listing
  179. if (([regex]::Matches($listing, '"counter\.prg"')).Count -ne 1) {
  180. $failures += "the collision run changed the COUNTER.PRG directory entry"
  181. }
  182. $afterCollision = Read-DiskFile -Name "counter.prg" -Type "p" -Dest (Join-Path $root "counter-again.prg")
  183. if ([Convert]::ToBase64String($afterCollision) -ne [Convert]::ToBase64String($prg)) {
  184. $failures += "a refused output changed COUNTER.PRG"
  185. }
  186. # 5: the generated program itself runs and reaches its started/pass contract.
  187. $runSession = Start-Nova128X128 -Autostart $prgPath -Port ($Port + 1) `
  188. -Screenshot (Join-Path $root "counter-run.png")
  189. try {
  190. $mb = Wait-Nova128Mailbox $runSession -TimeoutSeconds 120
  191. } finally {
  192. Stop-Nova128X128 $runSession
  193. }
  194. if ($mb[0] -ne 0x4E -or $mb[1] -ne 0x38) { $failures += "the generated program did not initialise the mailbox" }
  195. if ($mb[2] -ne 0x03) { $failures += ("producer {0:X2}, expected 03 (generated program)" -f $mb[2]) }
  196. if ($mb[3] -ne 0xA5) { $failures += ("generated program status {0:X2}, expected A5" -f $mb[3]) }
  197. # 6: a print source compiles with the copied runtime and prints on screen.
  198. Invoke-Compile -Keys "hello.n8\n" -Screenshot (Join-Path $root "hello-summary.png")
  199. $listing = Get-Listing
  200. if ($listing -notmatch '"hello\.prg"\s+prg') { $failures += "HELLO.PRG is missing" }
  201. if ($listing -match '"hello\.err"') { $failures += "HELLO.N8 left HELLO.ERR" }
  202. $helloMap = ([Text.Encoding]::ASCII.GetString((Read-DiskFile -Name "hello.map" -Type "s" `
  203. -Dest (Join-Path $root "hello.map"))) -replace "`r", "`n")
  204. if ($helloMap -notmatch '(?im)^SECTION PRINT ') { $failures += "HELLO.MAP has no PRINT section" }
  205. $helloPrgPath = Join-Path $root "hello.prg"
  206. $helloBytes = Read-DiskFile -Name "hello.prg" -Type "p" -Dest $helloPrgPath
  207. $helloSections = @{}
  208. foreach ($m in [regex]::Matches($helloMap, '(?im)^SECTION (\w+) ([0-9a-f]{4}) ([0-9a-f]{4})')) {
  209. $helloSections[$m.Groups[1].Value.ToUpper()] = @{
  210. Start = [Convert]::ToInt32($m.Groups[2].Value, 16)
  211. Size = [Convert]::ToInt32($m.Groups[3].Value, 16)
  212. }
  213. }
  214. if (-not $helloSections.ContainsKey("DATA")) {
  215. $failures += "HELLO.MAP has no DATA section"
  216. } else {
  217. $literal = [Text.Encoding]::ASCII.GetBytes("HELLO FROM NOVA128")
  218. $dataOffset = 2 + $helloSections.STUB.Size + $helloSections.ENTRY.Size +
  219. $helloSections.START.Size + $helloSections.PRINT.Size + $helloSections.CODE.Size
  220. if ($helloBytes.Length -lt $dataOffset + $literal.Length) {
  221. $failures += "HELLO.PRG is shorter than its DATA section"
  222. } else {
  223. for ($i = 0; $i -lt $literal.Length; $i++) {
  224. if ($helloBytes[$dataOffset + $i] -ne $literal[$i]) {
  225. $failures += "HELLO.PRG DATA does not hold the print literal"
  226. break
  227. }
  228. }
  229. }
  230. $codeOffset = 2 + $helloSections.STUB.Size + $helloSections.ENTRY.Size +
  231. $helloSections.START.Size + $helloSections.PRINT.Size
  232. $calledPrint = $false
  233. for ($i = 0; $i -lt $helloSections.CODE.Size - 2; $i++) {
  234. if ($helloBytes[$codeOffset + $i] -eq 0x20) {
  235. $target = $helloBytes[$codeOffset + $i + 1] + 256 * $helloBytes[$codeOffset + $i + 2]
  236. if ($target -ge $helloSections.PRINT.Start -and
  237. $target -lt ($helloSections.PRINT.Start + $helloSections.PRINT.Size)) {
  238. $calledPrint = $true
  239. break
  240. }
  241. }
  242. }
  243. if (-not $calledPrint) { $failures += "HELLO.PRG CODE never calls the PRINT runtime" }
  244. }
  245. $helloSession = Start-Nova128X128 -Autostart $helloPrgPath -Port ($Port + 2) `
  246. -Screenshot (Join-Path $root "hello-run.png")
  247. try {
  248. $helloMb = Wait-Nova128Mailbox $helloSession -TimeoutSeconds 60
  249. $helloText = Get-ScreenText $helloSession
  250. } finally {
  251. Stop-Nova128X128 $helloSession
  252. }
  253. if ($helloMb[3] -ne 0xA5) { $failures += "HELLO.PRG did not return from its body" }
  254. if ($helloText -notmatch "HELLO FROM NOVA128") { $failures += "HELLO.PRG did not print its string" }
  255. if ($failures) { throw ("n128c verification failed:`n " + ($failures -join "`n ")) }
  256. Write-Host ("summary: SOURCE COUNTER.N8 / OUTPUT COUNTER.PRG / 0 ERRORS")
  257. Write-Host ("success: COUNTER.PRG {0} bytes, COUNTER.MAP {1} bytes, no COUNTER.ERR" -f
  258. $prg.Length, $map.Length)
  259. Write-Host ("failure: BAD.ERR {0}, no BAD.PRG, COUNTER.PRG unchanged" -f $errText.Trim())
  260. Write-Host "injection: prepare-disk.ps1 wrote build/n128c.prg byte-identically as n128c"
  261. Write-Host "collision: OUTPUT EXISTS and COUNTER.PRG unchanged"
  262. Write-Host "run: generated COUNTER.PRG reported MB_ST_PASS in x128"
  263. Write-Host "print: HELLO.PRG carries PRINT+DATA, calls the runtime, and shows 'HELLO FROM NOVA128' on screen"

Powered by TurnKey Linux.