forked from mikeryan/ems-flasher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateheader.sh
More file actions
executable file
·76 lines (63 loc) · 1.56 KB
/
Copy pathupdateheader.sh
File metadata and controls
executable file
·76 lines (63 loc) · 1.56 KB
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
#!/bin/sh
# Validate the header of a ROM passed through stdin (update the logo and the
# header checksum) and enable the GBC and/or the SGB enhancements.
# Output the ROM with the updated header on stdout.
#
# Used in the Makefile when generating the menu files.
unset color super
while getopts "cs" opt; do
case $opt in
c) color=1;;
s) super=1;;
esac
done
# need to do some magic to make this portable to OS X and Linux
AWK="awk"
if which gawk >/dev/null 2>&1; then
AWK="gawk"
fi
OD="od"
if which god >/dev/null 2>&1; then
OD="god"
fi
$OD -v -Ad -tu1 |
$AWK '{ o=0; for (i = 2; i <= NF; i++) print $1 + o++, $i }' |
LC_ALL=C $AWK -vcolor=$color -vsuper=$super '
BEGIN {
split( \
" 206 237 102 102 204 13 0 11 3 115 0 131 0 12 0 13" \
" 0 8 17 31 136 137 0 14 220 204 110 230 221 221 217 153" \
" 187 187 103 99 110 14 236 204 221 220 153 159 187 185 51 62",
logo)
}
{ out = $2 }
# 0x104 to 0x133 - Update the logo
$1 >= 260 && $1 <= 307 {
out = logo[$1-259]
}
# 0x144: Update the GBC flag
color && $1 == 323 {
if (out < 128)
out += 128
}
# 0x146: Update the SGB field
super && $1 == 326 {
out = 3
}
# 0x14b: Update Old license code field for SGB
super && $1 == 331 {
out = 51
}
# 0x14d - Update the checksum
$1 == 333 {
out = chksum
}
# 0x134 to 0x14c - Compute the checksum of the new header (must be after all
# rules modifying "out")
$1 >= 308 && $1 <= 332 {
chksum = chksum - out -1
if (chksum < 0)
chksum += 256
}
{ printf "%c", out }
'