-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbuild.sh
133 lines (115 loc) · 3.84 KB
/
build.sh
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
# Assembles and links the source assemblies into a .nes ROM.
# Run this script from a bash terminal if on linux or mac.
# If you are on windows, use either build.ps1, or build.bat
GAME="$1"
GAME_HASH="1c747c78c678f14a68d4e5fcae065298a103f833638775860f0e5c5ffaa061f62d45fd8942148b1507c1fd57fde950a5d83f9f84a9782ec048a56067740c48e9"
ROM_NAME="contra.nes"
DBG_NAME="contra.dbg"
ASSETS_NAME="assets.txt"
ASSET_GAME_TYPE="src/assets/asset-game-type.txt"
if [[ "$GAME" == "Probotector" ]]
then
ROM_NAME="probotector.nes"
DBG_NAME="probotector.dbg"
GAME_HASH="a4bda4572ec8a3f520deb4bf483510f6e41ed7665505850d22ec07ca1b25abff40b3368a27ece982ea6e9c71a1b698eb2add16c26a7ad67dba3c0a98c4e2ba43"
ASSETS_NAME="probotector-assets.txt"
else
GAME="Contra"
fi
# function to check between different available hash functions
# mac doesn't come with sha512sum by default, but includes shasum
romHasher() {
if command -v sha512sum &> /dev/null
then
sha512sum $1
else
shasum -a 512 $1
fi
}
setBytes(){
if test -f $3
then
return
fi
echo " Writing file $3."
dd bs=1 skip=$1 count=$2 if=baserom.nes of=$3 status=none
}
if ! ld65 --version &> /dev/null
then
echo "cc65 compiler suite could not be found. Please install cc65 and add it to your path."
exit
fi
mkdir -p obj
if test -f ROM_NAME
then
echo "Deleting ${ROM_NAME}."
rm ROM_NAME
fi
if test -f "obj/*.o"
then
echo "Deleting object files."
rm obj/*.o
fi
if ! test -f "baserom.nes"
then
echo "No baserom.nes file found. If assets are missing, then the build will fail."
else
ROM_HASH=$(romHasher baserom.nes | awk '{print $1}')
if [[ "$ROM_HASH" != "$GAME_HASH" ]]
then
echo "baserom.nes file integrity does NOT match expected result."
fi
fi
# used to know which assets were last build
LAST_BUILD_TYPE="Contra"
if test -f $ASSET_GAME_TYPE
then
LAST_BUILD_TYPE=`cat $ASSET_GAME_TYPE`
fi
# If the assets are from a different game, then delete them
# For example, if the assets were extracted from Contra and currently building
# Probotector, then delete the assets and extract them from the Probotector baserom.nes
if [[ "$LAST_BUILD_TYPE" != "$GAME" ]]
then
echo "Removing graphic asset files"
rm src/assets/graphic_data/*.bin
fi
# loop through assets defined in assets.txt (or probotector-assets.txt) and extract bytes from baserom.nes
echo "Extracting binary data from baserom.nes"
while read -r line || [ -n "$p" ]
do
set $line
file=$1
start=$2
length=$3
length=$(echo $length | tr -d '\r')
file=$(echo "$file" | tr '\\' '/')
setBytes $start $length $file
done < $ASSETS_NAME
echo "$GAME" > $ASSET_GAME_TYPE
echo "Assembling PRG Rom Banks"
ca65 -D $GAME --debug-info -o obj/ram.o src/ram.asm
ca65 -D $GAME --debug-info -o obj/constants.o src/constants.asm
ca65 -D $GAME --debug-info -o obj/ines_header.o src/ines_header.asm
ca65 -D $GAME --debug-info -o obj/bank0.o src/bank0.asm
ca65 -D $GAME --debug-info -o obj/bank1.o src/bank1.asm
ca65 -D $GAME --debug-info -o obj/bank2.o src/bank2.asm
ca65 -D $GAME --debug-info -o obj/bank3.o src/bank3.asm
ca65 -D $GAME --debug-info -o obj/bank4.o src/bank4.asm
ca65 -D $GAME --debug-info -o obj/bank5.o src/bank5.asm
ca65 -D $GAME --debug-info -o obj/bank6.o src/bank6.asm
ca65 -D $GAME --debug-info -o obj/bank7.o src/bank7.asm
echo "Creating .nes ROM"
ld65 -C contra.cfg --dbgfile $DBG_NAME ./obj/ram.o ./obj/constants.o ./obj/ines_header.o ./obj/bank0.o ./obj/bank1.o ./obj/bank2.o ./obj/bank3.o ./obj/bank4.o ./obj/bank5.o ./obj/bank6.o ./obj/bank7.o -o $ROM_NAME
if test -f $ROM_NAME
then
# compare assembled ROM hash to expected hash
ROM_HASH=$(romHasher $ROM_NAME | awk '{print $1}')
if [[ "$ROM_HASH" == "$GAME_HASH" ]]
then
echo "File integrity matches."
else
echo "File integrity does NOT match."
fi
fi