-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbuild.js
218 lines (158 loc) · 5.34 KB
/
build.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
console.log('TetrisGYM buildscript');
console.time('build');
const mappers = { // https://www.nesdev.org/wiki/Mapper
0: 'NROM',
1: 'MMC1',
3: 'CNROM',
4: 'MMC3',
5: 'MMC5',
1000: 'Autodetect MMC1/CNROM',
};
// options handling
const args = process.argv.slice(2);
if (args.includes('-h')) {
console.log(`usage: node build.js [-h] [-v] [-m<${Object.keys(mappers).join('|')}>] [-a] [-s] [-k] [-w]
-m mapper
-a faster aeppoz + press select to end game
-s disable highscores/SRAM
-k Famicom Keyboard support
-w force WASM compiler
-c force PNG to CHR conversion
-o override autodetect mmc1 header with cnrom
-t run tests (requires cargo)
-T run single test
-h you are here
`);
process.exit(0);
}
const compileFlags = [];
// compiler options
const nativeCC65 = args.includes('-w')
? false
: process.env.PATH.split(path.delimiter).some((dir) =>
fs
.statSync(path.join(dir, 'cc65'), { throwIfNoEntry: false })
?.isFile(),
);
console.log(`using ${nativeCC65 ? 'system' : 'wasm'} ca65/ld65`);
// mapper options
const mapper = args.find((d) => d.startsWith('-m'))?.slice(2) ?? 1000;
if (!mappers[mapper]) {
console.error(
`invalid INES_MAPPER - options are ${Object.keys(mappers)
.map((d) => `-m${d}`)
.join(', ')}`,
);
process.exit(0);
}
// compileFlags
compileFlags.push('-D', `INES_MAPPER=${mapper}`);
console.log(`using ${mappers[mapper]}`);
if (args.includes('-a')) {
compileFlags.push('-D', 'AUTO_WIN=1');
console.log('using fast aeppoz');
}
if (args.includes('-k')) {
compileFlags.push('-D', 'KEYBOARD=1');
console.log('using Famicom Keyboard support');
}
if (args.includes('-s')) {
compileFlags.push('-D', 'SAVE_HIGHSCORES=0');
console.log('highscore saving disabled');
}
if (args.includes('-o')) {
compileFlags.push('-D', 'CNROM_OVERRIDE=1');
console.log('cnrom override for autodetect');
}
console.log();
// build / compress nametables
console.time('nametables');
require('./src/nametables/build');
console.timeEnd('nametables');
// PNG -> CHR
console.time('CHR');
const png2chr = require('./tools/png2chr/convert');
const dir = path.join(__dirname, 'src', 'chr');
fs.readdirSync(dir)
.filter((name) => name.endsWith('.png'))
.forEach((name) => {
const png = path.join(dir, name);
const chr = path.join(dir, name.replace('.png', '.chr'));
const pngStat = fs.statSync(png, { throwIfNoEntry: false });
const chrStat = fs.statSync(chr, { throwIfNoEntry: false });
const staleCHR = !chrStat || chrStat.mtime < pngStat.mtime;
if (staleCHR || args.includes('-c')) {
console.log(`${name} => ${path.basename(chr)}`);
fs.writeFileSync(chr, png2chr(fs.readFileSync(png)));
}
});
console.timeEnd('CHR');
// build object files
const { spawnSync } = require('child_process');
function execArgs(exe, args) {
const output = spawnSync(exe, args).output.flatMap(
(d) => d?.toString() || [],
);
if (output.length) {
console.log(output.join('\n'));
process.exit(0);
}
}
function exec(cmd) {
const [exe, ...args] = cmd.split(' ');
execArgs(exe, args)
}
const ca65bin = nativeCC65 ? 'ca65' : 'node ./tools/assemble/ca65.js';
const flags = compileFlags.join(' ');
console.time('assemble');
exec(`${ca65bin} ${flags} -g src/header.asm -o header.o`);
exec(`${ca65bin} ${flags} -l tetris.lst -g src/main.asm -o main.o`);
console.timeEnd('assemble');
// link object files
const ld65bin = nativeCC65 ? 'ld65' : 'node ./tools/assemble/ld65.js';
console.time('link');
exec(`${ld65bin} -m tetris.map -Ln tetris.lbl --dbgfile tetris.dbg -o tetris.nes -C src/tetris.nes.cfg main.o header.o`);
console.timeEnd('link');
// create patch
if (!fs.existsSync('clean.nes')) {
console.log('clean.nes not found, skipping patch creation');
} else {
console.time('patch');
const patcher = require('./tools/patch/create');
const pct = patcher('clean.nes', 'tetris.nes', 'tetris.bps');
console.timeEnd('patch');
console.log(`\nusing ${pct}% of original file`);
}
// stats
console.log();
if (fs.existsSync('tetris.map')) {
const memMap = fs.readFileSync('tetris.map', 'utf8');
false && console.log((memMap.match(/PRG_chunk\d+\s+0.+$/gm) || []).join('\n'));
const used = parseInt(memMap.match(/PRG_chunk1\s+\w+\s+\w+\s+(\w+)/)?.[1]??'', 16) + 0x100; // 0x100 for reset chunk
console.log(`${0x8000 - used} PRG bytes free`);
}
function hashFile(filename) {
if (fs.existsSync(filename)) {
const shasum = crypto.createHash('sha1');
shasum.update(fs.readFileSync(filename));
console.log(`\n${filename} => ${shasum.digest('hex')}`);
console.log(`${fs.statSync(filename).size} bytes`);
}
}
hashFile('tetris.nes');
hashFile('tetris.bps');
console.log();
console.timeEnd('build');
// tests
if (args.includes('-t')) {
console.log('\nrunning tests');
exec('cargo run --release --manifest-path tests/Cargo.toml -- -t');
}
if (args.includes('-T')) {
const singleTest = args.slice(1+args.indexOf('-T')).join(' ');
console.log(`\nrunning single test: ${singleTest}`);
execArgs('cargo', [...'run --release --manifest-path tests/Cargo.toml -- -T'.split(' '), singleTest]);
}