forked from jeffpar/pcjs.v1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcx86.js
408 lines (379 loc) · 14 KB
/
pcx86.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env node
/**
* @fileoverview Implements the PCx86 command-line interface
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2020 Jeff Parsons
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*
* PCjs is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* PCjs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCjs. If not,
* see <http://www.gnu.org/licenses/gpl.html>.
*
* You are required to include the above copyright notice in every modified copy of this work
* and to display that copyright notice when the software starts running; see COPYRIGHT in
* <https://www.pcjs.org/modules/shared/lib/defines.js>.
*
* Some PCjs files also attempt to load external resource files, such as character-image files,
* ROM files, and disk image files. Those external resource files are not considered part of PCjs
* for purposes of the GNU General Public License, and the author does not claim any copyright
* as to their contents.
*/
"use strict";
var path = require("path");
var fs = require("fs");
var repl = require("repl");
var Defines = require("../../shared/lib/defines");
var Str = require("../../shared/lib/strlib");
var Proc = require("../../shared/lib/proclib");
var fConsole = false;
var fDebug = false;
var fGlobalsSet = false;
var args = Proc.getArgs();
var argv = args.argv;
var sCmdPrev = "";
if (argv['console'] !== undefined) fConsole = argv['console'];
if (argv['debug'] !== undefined) fDebug = argv['debug'];
var lib = path.join(path.dirname(fs.realpathSync(__filename)), "../lib/");
try {
var machines = require(lib + "../../../_data/machines.json");
var scriptsPCx86 = /** @type {Array.<string>} */ (machines['pcx86'].scripts);
} catch(err) {
console.log(err.message);
}
/*
* We will build an array of components whose names will match the component names
* used in a JSON machine definition file; eg:
*
* [
* {name: "chipset":
* Create: ChipSet,
* objects: []
* },
* ...
* ]
*
* Every component name comes from the component filename, minus the ".js" extension;
* Create is the constructor returned by require().
*
* TODO: Update the list of ignored (ie, ignorable) components.
*/
var Component;
var dbg;
var aComponents = [];
var asComponentsIgnore = ["panel", "embed", "save"];
/*
* A few of the components are subclasses of other classes (eg, "cpux86" is a subclass
* of "cpu"). In those situations, we "hoist" the subclass constructor into the
* corresponding superclass, because it is the name of the superclass that we rely on during
* machine initialization.
*/
var aSubClasses = {
"pcx86/lib/cpux86": "pcx86/lib/cpu",
"pcx86/lib/debugger": "shared/lib/debugger"
};
/**
* loadComponents(asFiles)
*
* @param {Array.<string>} asFiles
*/
function loadComponents(asFiles)
{
for (let i = 0; i < asFiles.length; i++) {
let sFile = asFiles[i];
if (Str.getExtension(sFile) != "js") continue;
let sName = Str.getBaseName(sFile, true);
if (asComponentsIgnore.indexOf(sName) >= 0) continue;
if (fDebug) console.log(sFile);
try {
/*
* We COULD load ("require") all the files on-demand, because it's only the browser initialization
* sequence we want to mimic in loadMachine(), but this is simpler, and it also gives us direct references
* to certain components we'll want to access later (eg, "component" in getComponentByType()).
*/
let props = 0;
let exports = require(lib + "../../../" + sFile);
let afn = (typeof exports == "function"? [exports] : exports);
for (let f in afn) {
props++;
let fn = afn[f];
if (typeof fn != "function") continue;
let sSuperClass = null;
for (let s in aSubClasses) {
if (sFile.indexOf(s) >= 0) {
sSuperClass = aSubClasses[s];
break;
}
}
if (sSuperClass) {
for (let j = 0; j < aComponents.length; j++) {
if (aComponents[j].path.indexOf(sSuperClass) >= 0) {
if (fDebug) console.log("updating superclass " + aComponents[j].path + " with subclass " + sFile);
aComponents[j].Create = fn;
sName = null;
break;
}
}
}
if (sName == "component") {
fn.log = fn.println = function(s, type) {
console.log((type !== undefined? (type + ": ") : "") + (s || ""));
}; // jshint ignore:line
}
if (sName) {
aComponents.push({name: sName, path: sFile, Create: fn, objects: []});
}
}
/*
* The "defines.js" module that defines all PCjs globals (as opposed to machine-specific globals)
* doesn't export anything, so exports is an empty object, hence props is zero. However, that isn't
* the ONLY module that doesn't export anything, so we also check for whether DEBUG has been set yet.
*/
if (!props && !fGlobalsSet) {
if (global.DEBUG !== undefined) {
global.DEBUG = fDebug;
global.APPVERSION = machines['shared']['version'];
fGlobalsSet = true;
}
}
} catch(err) {
console.log(err.message);
}
}
}
/**
* getComponentByName(sName)
*
* @param sName
* @return {*}
*/
function getComponentByName(sName)
{
for (let i = 0; i < aComponents.length; i++) {
if (aComponents[i].name == sName) {
return aComponents[i].Create;
}
}
return null;
}
/**
* getComponentByType(sType)
*
* @param sType
* @return {*}
*/
function getComponentByType(sType)
{
let component = null;
if (!Component) {
Component = getComponentByName("component");
}
if (Component) {
component = Component.getComponentByType(sType);
}
return component;
}
/**
* loadMachine(sFile)
*
* @param {string} sFile
* @return {Object} representing the machine whose component objects have been loaded into aComponents
*/
function loadMachine(sFile)
{
if (fDebug) console.log('loadMachine("' + sFile + '")');
/*
* Clear any/all saved objects from any previous machine
*/
let i, j;
Component = dbg = null;
for (i = 0; i < aComponents.length; i++) {
aComponents[i].objects = [];
}
let machine;
try {
/*
* Since our JSON files may contain comments, hex values, and/or other tokens deemed unacceptable
* by the JSON Overlords, we can't use require() to load it, as we're able to do with "package.json".
* Also note that require() assumes the same path as that of the requiring file, whereas fs.readFileSync()
* assumes the path reported by process.cwd().
*
* TODO: I've since removed the comments from my sample "ibm5150.json" file, so we could try to reinstate
* this code; however, there are still hex constants, which I find *much* preferable to the decimal equivalents.
* JSON's restrictions continue to irritate me.
*
* let machine = require(lib + "../bin/" +sFile);
*/
let sMachine = /** @type {string} */ (fs.readFileSync(sFile, {encoding: "utf8"}));
sMachine = '(' + sMachine + ')';
if (fDebug) console.log(sMachine);
machine = eval(sMachine); // jshint ignore:line
if (machine) {
/*
* Since we have a machine object, we now mimic the initialization sequence that occurs in
* the browser, by walking the list of PCx86 components we loaded above and looking for matches.
*/
let idMachine = "";
/*
* 'machine' is a pseudo-component that is only used to define an ID for the entire machine;
* if it exists, then that ID is prepended to every component ID, just as our XSLT code would
* do for a machine XML file. This relieves the JSON file from having to manually prepend
* a machine ID to every component ID itself.
*
* This doesn't mean I anticipate a Node environment running multiple machines, as we do in
* a browser; it only means that I'm trying to make both environments operate similarly.
*/
if (machine['machine']) {
idMachine = machine['machine']['id'];
}
for (i = 0; i < aComponents.length; i++) {
let component = aComponents[i];
let parms = machine[component.name];
/*
* If parms is undefined, it means there is no component with that name defined in the
* machine object (NOT that the component has no parms), and therefore we should skip it.
*/
if (parms === undefined) continue;
/*
* If parms is an Array, then we must create an object for each parms element; and yes,
* I'm relying on the fact that none of my parm objects use a "length" property, as a quick
* and dirty way of differentiating objects from arrays.
*/
let aParms = parms.length !== undefined? parms : [parms];
for (j = 0; j < aParms.length; j++) {
let obj;
let parmsObj = aParms[j];
if (idMachine) parmsObj['id'] = idMachine + '.' + parmsObj['id'];
if (fDebug) {
console.log("creating " + component.name + "...");
console.log(parmsObj);
}
if (component.name == "cpu") {
parmsObj['autoStart'] = false;
}
try {
obj = new component.Create(parmsObj);
} catch(err) {
console.log("error creating " + component.name + ": " + err.message);
continue;
}
console.log(obj['id'] + " object created");
component.objects.push(obj);
if (obj.type == "Debugger") dbg = obj;
}
}
/*
* Return the original machine object only in DEBUG mode
*/
if (!fDebug) machine = true;
}
} catch(err) {
console.log(err.message);
}
return machine;
}
/**
* doCommand(sCmd)
*
* @param {string} sCmd
* @return {*}
*/
function doCommand(sCmd)
{
if (!sCmd) {
sCmd = sCmdPrev;
} else {
sCmdPrev = sCmd;
}
let result = false;
let aTokens = sCmd.split(' ');
switch(aTokens[0]) {
case "cwd":
result = process.cwd();
break;
case "load":
result = loadMachine(aTokens[1]);
break;
case "quit":
process.exit();
result = true;
break;
default:
if (sCmd) {
try {
console.log("doCommand(" + sCmd + "): " + dbg);
if (dbg && !dbg.doCommands(sCmd, true)) {
sCmd = '(' + sCmd + ')';
result = eval(sCmd); // jshint ignore:line
}
} catch(err) {
console.log(err.message);
}
}
break;
}
return result;
}
/**
* onCommand(cmd, context, filename, callback)
*
* The Node docs (http://nodejs.org/api/repl.html) say that repl.start's "eval" option is:
*
* a function that will be used to eval each given line; defaults to an async wrapper for eval()
*
* and it gives this example of such a function:
*
* function eval(cmd, context, filename, callback) {
* callback(null, result);
* }
*
* but it defines NEITHER the parameters for the function NOR the parameters for the callback().
*
* It's pretty clear that "result" is expected to return whatever "eval()" would return for the expression
* in "cmd" (which is always parenthesized in preparation for a call to "eval()"), but it's not clear what
* the first callback() parameter (represented by null) is supposed to be. Should we assume it's an Error
* object, in case we want to report an error?
*
* @param {string} cmd
* @param {Object} context
* @param {string} filename
* @param {function(Object|null, Object)} callback
*/
var onCommand = function (cmd, context, filename, callback)
{
let result = false;
/*
* WARNING: After updating from Node v0.10.x to v0.11.x, the incoming expression in "cmd" is no longer
* parenthesized, so I had to tweak the RegExp below. But... WTF. Do we not care what we break, folks?
*/
let match = cmd.match(/^\(?\s*(.*?)\s*\)?$/);
if (match) result = doCommand(match[1]);
callback(null, result);
};
if (scriptsPCx86) {
loadComponents(scriptsPCx86);
}
/*
* Before falling into the REPL, process any command-line (--cmd) commands -- which should eventually include batch files.
*/
if (argv['cmd'] !== undefined) {
var cmds = argv['cmd'];
var aCmds = (typeof cmds == "string"? [cmds] : cmds);
for (let i = 0; i < aCmds.length; i++) {
doCommand(aCmds[i]);
}
sCmdPrev = "";
}
repl.start({
prompt: "PCx86> ",
input: process.stdin,
output: process.stdout,
eval: onCommand
});