Files
NESLite/tools/generate-rc-roms.ps1

104 lines
6.0 KiB
PowerShell

[CmdletBinding()]
param(
[string]$OutputDirectory = ''
)
$ErrorActionPreference = 'Stop'
$root = Split-Path -Parent $PSScriptRoot
if (-not $OutputDirectory) { $OutputDirectory = Join-Path $root 'build\rc-roms' }
$resolvedOutput = [IO.Path]::GetFullPath($OutputDirectory)
$resolvedBuildRoot = [IO.Path]::GetFullPath((Join-Path $root 'build')) + [IO.Path]::DirectorySeparatorChar
if (-not $resolvedOutput.StartsWith($resolvedBuildRoot, [StringComparison]::OrdinalIgnoreCase)) {
throw 'Generated ROM output must remain inside the repository build directory.'
}
if (Test-Path $resolvedOutput) { Remove-Item -LiteralPath $resolvedOutput -Recurse -Force }
$null = New-Item -ItemType Directory -Path $resolvedOutput
$specifications = @(
@{ id = 'RC01'; mapper = 0; region = 'NTSC'; header = 'iNES'; battery = $false; prgBanks = 1 },
@{ id = 'RC02'; mapper = 0; region = 'PAL'; header = 'NES2'; battery = $false; prgBanks = 2 },
@{ id = 'RC03'; mapper = 1; region = 'NTSC'; header = 'iNES'; battery = $true; prgBanks = 2 },
@{ id = 'RC04'; mapper = 1; region = 'PAL'; header = 'NES2'; battery = $true; prgBanks = 2 },
@{ id = 'RC05'; mapper = 2; region = 'NTSC'; header = 'iNES'; battery = $false; prgBanks = 2 },
@{ id = 'RC06'; mapper = 2; region = 'PAL'; header = 'NES2'; battery = $false; prgBanks = 2 },
@{ id = 'RC07'; mapper = 3; region = 'NTSC'; header = 'iNES'; battery = $false; prgBanks = 2 },
@{ id = 'RC08'; mapper = 3; region = 'PAL'; header = 'NES2'; battery = $false; prgBanks = 2 },
@{ id = 'RC09'; mapper = 4; region = 'NTSC'; header = 'iNES'; battery = $true; prgBanks = 2 },
@{ id = 'RC10'; mapper = 4; region = 'PAL'; header = 'NES2'; battery = $true; prgBanks = 2 },
@{ id = 'RC11'; mapper = 0; region = 'NTSC'; header = 'NES2'; battery = $true; prgBanks = 1 },
@{ id = 'RC12'; mapper = 4; region = 'NTSC'; header = 'NES2'; battery = $false; prgBanks = 2 }
)
$manifestEntries = foreach ($specification in $specifications) {
$header = [byte[]]::new(16)
$header[0] = 0x4e; $header[1] = 0x45; $header[2] = 0x53; $header[3] = 0x1a
$header[4] = [byte]$specification.prgBanks
$header[5] = 1
$header[6] = [byte]((($specification.mapper -band 0x0f) -shl 4) -bor $(if ($specification.battery) { 0x02 } else { 0 }))
$header[7] = [byte]($specification.mapper -band 0xf0)
if ($specification.header -eq 'NES2') { $header[7] = [byte]($header[7] -bor 0x08) }
if ($specification.region -eq 'PAL') {
if ($specification.header -eq 'NES2') { $header[12] = 1 } else { $header[9] = 1 }
}
$prg = [byte[]]::new([int]$specification.prgBanks * 16384)
[Array]::Fill($prg, [byte]0xea)
$programOffset = $prg.Length - 16384
$program = [byte[]](0x78, 0xd8, 0xa2, 0xff, 0x9a, 0xe8, 0x8e, 0x00, 0x20, 0x8e, 0x01, 0x20, 0x4c, 0x0c, 0xc0)
[Array]::Copy($program, 0, $prg, $programOffset, $program.Length)
$marker = [Text.Encoding]::ASCII.GetBytes("NESLITE-$($specification.id)-M$($specification.mapper)-$($specification.region)-$($specification.header)")
[Array]::Copy($marker, 0, $prg, $programOffset + 64, $marker.Length)
$vectorOffset = $prg.Length - 6
for ($index = 0; $index -lt 3; ++$index) {
$prg[$vectorOffset + ($index * 2)] = 0x00
$prg[$vectorOffset + ($index * 2) + 1] = 0xc0
}
$chr = [byte[]]::new(8192)
$rom = [byte[]]::new($header.Length + $prg.Length + $chr.Length)
[Array]::Copy($header, 0, $rom, 0, $header.Length)
[Array]::Copy($prg, 0, $rom, $header.Length, $prg.Length)
[Array]::Copy($chr, 0, $rom, $header.Length + $prg.Length, $chr.Length)
$fileName = "$($specification.id)-m$($specification.mapper)-$($specification.region.ToLowerInvariant())-$($specification.header.ToLowerInvariant()).nes"
$path = Join-Path $resolvedOutput $fileName
[IO.File]::WriteAllBytes($path, $rom)
[ordered]@{
id = $specification.id
fileName = $fileName
sha256 = (Get-FileHash $path -Algorithm SHA256).Hash
bytes = $rom.Length
region = $specification.region
mapper = $specification.mapper
header = $specification.header
battery = [bool]$specification.battery
license = 'CC0-1.0 generated test fixture'
purpose = 'Deterministic loader/header/mapper-declaration smoke fixture; not gameplay evidence.'
}
}
$manifest = [ordered]@{
formatVersion = 1
generator = 'tools/generate-rc-roms.ps1'
rights = 'Generated by NESLite for this test set and dedicated under CC0-1.0; contains no third-party game data.'
limitations = 'Minimal looping programs validate import/load declarations only; they do not replace action, scrolling, audio, or battery-progress gameplay samples.'
samples = @($manifestEntries)
}
$frozenPath = Join-Path $root 'tests\rc-rom-manifest.json'
$frozen = Get-Content $frozenPath -Raw | ConvertFrom-Json
if ($frozen.samples.Count -ne $manifestEntries.Count) { throw 'Frozen RC ROM sample count changed.' }
foreach ($expected in $frozen.samples) {
$matches = @($manifestEntries | Where-Object id -eq $expected.id)
$actual = if ($matches.Count -eq 1) { $matches[0] } else { $null }
if (-not $actual -or $actual.sha256 -ne $expected.sha256 -or $actual.region -ne $expected.region -or
$actual.mapper -ne $expected.mapper -or $actual.header -ne $expected.header -or
$actual.battery -ne $expected.battery) {
throw "Generated RC ROM no longer matches frozen manifest: $($expected.id)"
}
}
$manifest | ConvertTo-Json -Depth 6 | Set-Content -LiteralPath (Join-Path $resolvedOutput 'manifest.json') -Encoding utf8NoBOM
@'
To the extent possible under law, the author has waived all copyright and related or neighboring rights to these generated test fixtures under CC0 1.0 Universal.
These fixtures contain only a minimal original loop and deterministic metadata; they contain no third-party game code, graphics, audio, or other ROM content.
'@ | Set-Content -LiteralPath (Join-Path $resolvedOutput 'LICENSE.txt') -Encoding utf8NoBOM
Write-Output "Generated 12 deterministic RC ROM fixtures: $resolvedOutput"
$manifest | ConvertTo-Json -Depth 6