-
Notifications
You must be signed in to change notification settings - Fork 22
/
build.ps1
119 lines (89 loc) · 2.35 KB
/
build.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
param ( [switch] $NoVerify, [switch] $NoExtract )
# Escalate any statement-terminating error to a script-terminating one.
trap { break }
. (join-path $PSScriptRoot util.ps1)
function Assemble( $srcPath, $objPath )
{
write-host "Assembling $srcPath"
.\ext\ca65 $srcPath -o $objPath --bin-include-dir .\bin
$passed = $LastExitCode -eq 0
$script:assemblyPassed = $script:assemblyPassed -and $passed
if ( !$passed ) { write-host "" }
}
function CompareBinaryFiles( $left, $right )
{
write-host "Comparing $left and $right"
return CompareFiles $left $right
}
function CheckRequirements()
{
$message = "Missing external. Ensure ext directory contains: "
get-command ext\ca65, ext\ld65 > $null 2>&1
if ( !$? )
{
throw ($message + "ca65.exe, ld65.exe")
}
if ( !$NoVerify -and !(test-path ext\Original.nes) )
{
throw ($message + "Original.nes")
}
}
function ExtractBins()
{
$romPath = resolve-path .\ext\Original.nes
$binXmlPath = resolve-path .\src\bins.xml
$binRootPath = resolve-path .\bin
$xml = new-object Xml
$xml.Load( $binXmlPath )
$image = [IO.File]::ReadAllBytes( $romPath )
foreach ( $bin in $xml.Binaries.Binary )
{
$binPath = join-path $binRootPath $bin.FileName
$binDir = split-path $binPath
mkdir $binDir -ErrorAction ignore > $null
$offset = [int] $bin.Offset + 16
$buf = new-object byte[] $bin.Length
[Array]::Copy( $image, $offset, $buf, 0, $bin.Length )
[IO.File]::WriteAllBytes( $binPath, $buf )
}
}
CheckRequirements
mkdir obj -ErrorAction ignore > $null
mkdir bin -ErrorAction ignore > $null
if ( !$NoExtract )
{
ExtractBins
}
$srcPaths = @()
$objPaths = @()
foreach ( $file in (dir src\*.asm) )
{
$base = [IO.Path]::GetFileNameWithoutExtension( $file )
$srcPaths += "src\$base.asm"
$objPaths += "obj\$base.o"
}
$assemblyPassed = $true
foreach ( $i in 0..($srcPaths.length-1) )
{
Assemble $srcPaths[$i] $objPaths[$i]
}
if ( !$assemblyPassed )
{
exit
}
echo "Linking"
.\ext\ld65 -o bin\Z.bin -C src\Z.cfg $objPaths
if ( $LastExitCode -ne 0 ) { exit }
echo "Combining raw ROM with NES header"
JoinFiles bin\Z.nes -in OriginalNesHeader.bin, bin\Z.bin
if ( !$NoVerify )
{
if ( CompareBinaryFiles bin\Z.nes ext\Original.nes )
{
echo "ROM image is OK"
}
else
{
echo "ROM image mismatch"
}
}