forked from jeffpar/pcjs.v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.js
621 lines (592 loc) · 19.9 KB
/
device.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/**
* @fileoverview Base class for devices, with assorted handy services
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2020 Jeff Parsons
* @license MIT
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*/
"use strict";
/**
* Every device has a 'registers' property that's a hash of register names to Register objects.
*
* @typedef {Object} Register
* @property {function()} get
* @property {function(number)|null} set
*/
/**
* In addition to basic Device services, such as:
*
* addDevice()
* enumDevices()
* findDevice()
* findDeviceByClass()
*
* this class also supports register "registration" services, to allow a Device to make any registers
* it supports available by name to other devices (notably the Debugger):
*
* defineRegister()
* defineRegisterAlias()
* getRegister()
* setRegister()
*
* Besides CPUs, other devices may have internal registers or ports that are useful to access by name, too.
*
* @class {Device}
* @unrestricted
* @property {string} idMachine
* @property {string} idDevice
* @property {Config} config
* @property {string} id
* @property {Object.<Register>} registers
* @property {CPU|undefined|null} cpu
* @property {Debugger|undefined|null} dbg
*/
class Device extends WebIO {
/**
* Device()
*
* Supported config properties:
*
* "bindings": object containing name/value pairs, where name is the generic name
* of a element, and value is the ID of the DOM element that should be mapped to it
*
* The properties in the "bindings" object are copied to our own bindings object in addBindings(),
* but only for DOM elements that actually exist, and it is the elements themselves (rather than
* their IDs) that we store.
*
* Also, URL parameters can be used to override config properties, as long as those properties
* have been listed in the device's "overrides" array. For example, the URL:
*
* http://localhost:4000/?cyclesPerSecond=100000
*
* will set the Time device's cyclesPerSecond config property to 100000. In general, the values
* will be treated as strings, unless they contain all digits (number), or equal "true" or "false"
* (boolean).
*
* @this {Device}
* @param {string} idMachine
* @param {string} idDevice
* @param {Config} [config]
* @param {Array} [overrides] (default overrides, if any, which in turn can be overridden by config['overrides'])
*/
constructor(idMachine, idDevice, config, overrides)
{
super(idMachine == idDevice);
this.addDevice(idMachine, idDevice);
this.checkConfig(config, overrides);
this.registers = {};
this.aReadyCallbacks = [];
}
/**
* addDevice(idMachine, idDevice)
*
* Adds this Device to the global set of Devices, so that findDevice(), findBinding(), etc, will work.
*
* @this {Device}
* @param {string} idMachine
* @param {string} idDevice
*/
addDevice(idMachine, idDevice)
{
this.idMachine = idMachine;
this.idDevice = idDevice;
if (!Device.Machines[this.idMachine]) {
Device.Machines[this.idMachine] = {};
}
if (Device.Machines[this.idMachine][this.idDevice]) {
this.printf("warning: machine configuration contains multiple '%s' devices\n", this.idDevice);
}
Device.Machines[this.idMachine][this.idDevice] = this;
/*
* The new Device classes don't use the Components array or machine+device IDs, but we need to continue
* updating both of those for backward compatibility with older PCjs machines.
*/
this['id'] = this.idMachine + '.' + this.idDevice;
Device.Components.push(this);
/*
* The WebIO constructor set this.machine tentatively, so that it could define any per-machine variables
* it needed; we now set it definitively.
*/
this.machine = this.findDevice(this.idMachine);
this.ready = true;
}
/**
* addDumper(device, name, desc, func)
*
* Interface definition only; implemented by the Debugger.
*
* @this {Device}
* @param {Device} device
* @param {string} name
* @param {string} desc
* @param {function(Array.<number>)} func
*/
addDumper(device, name, desc, func)
{
}
/**
* addSymbols(aSymbols)
*
* Interface definition only; implemented by the Debugger.
*
* @this {Device}
* @param {Array|undefined} aSymbols
*/
addSymbols(aSymbols)
{
}
/**
* checkConfig(config, overrides)
*
* @this {Device}
* @param {Config} [config]
* @param {Array} [overrides]
*/
checkConfig(config = {}, overrides = [])
{
/*
* If this device's config contains an "overrides" array, then any of the properties listed in
* that array may be overridden with a URL parameter. We don't impose any checks on the overriding
* value, so it is the responsibility of the component with overridable properties to validate them.
*/
overrides = config['overrides'] || overrides;
if (overrides.length) {
let parms = this.getURLParms();
for (let prop in parms) {
if (overrides.indexOf(prop) >= 0) {
let s = parms[prop];
let value = this.parseInt(s, 10);
if (value == undefined) {
if (s == "true") {
value = true;
} else if (s == "false") {
value = false;
} else {
value = s;
s = '"' + s + '"';
}
}
config[prop] = value;
this.println("overriding " + this.idDevice + " property '" + prop + "' with " + s);
}
}
}
this.config = config;
this.addBindings(config['bindings']);
this.checkVersion(config);
}
/**
* checkVersion(config)
*
* Verify that device's version matches the machine's version, and also that the config version stored in
* the JSON (if any) matches the device's version.
*
* This is normally performed by the constructor, but the Machine device cannot be fully initialized in the
* constructor, so it calls this separately.
*
* @this {Device}
* @param {Config} [config]
*/
checkVersion(config = {})
{
this.version = +VERSION;
if (this.version) {
let sVersion = "", version;
if (this.idMachine != this.idDevice) {
let machine = this.findDevice(this.idMachine);
version = machine.version;
if (version && version != this.version) {
sVersion = "Machine";
}
}
if (!sVersion) {
version = config['version'];
if (version && version > this.version) {
sVersion = "Config";
}
}
if (sVersion) {
let sError = this.sprintf("%s Device version (%3.2f) incompatible with %s version (%3.2f)", config.class, this.version, sVersion, version);
this.error("%s\n\nClearing your browser's cache may resolve the issue.", sError);
}
}
}
/**
* defineRegister(name, get, set)
*
* @this {Device}
* @param {string} name
* @param {function()} get
* @param {function(number)} [set]
*/
defineRegister(name, get, set)
{
this.registers[name] = {get: get.bind(this), set: set? set.bind(this) : null};
}
/**
* defineRegisterAlias(name, alias)
*
* @this {Device}
* @param {string} name
* @param {string} alias
*/
defineRegisterAlias(name, alias)
{
this.assert(this.registers[name]);
if (this.registers[name]) {
this.registers[alias] = this.registers[name];
}
}
/**
* enumDevices(func)
*
* @this {Device}
* @param {function(Device)} func
* @returns {boolean} (true if all devices successfully enumerated, false otherwise)
*/
enumDevices(func)
{
let id;
try {
let devices = Device.Machines[this.idMachine];
if (devices) {
for (id in devices) {
let device = devices[id];
if (device.config['class'] != "Machine") {
if (!func(device)) return false;
}
}
}
return true;
} catch(err) {
this.printf("error while enumerating device '%s': %s\n", id, err.message);
}
return false;
}
/**
* findBinding(name, all)
*
* This will search the current device's bindings, and optionally all the device bindings within the
* machine. If the binding is found in another device, that binding is recorded in this device as well.
*
* @this {Device}
* @param {string} [name]
* @param {boolean} [all]
* @returns {Element|null|undefined}
*/
findBinding(name, all = false)
{
let element;
if (name) {
element = super.findBinding(name, all);
if (element === undefined && all) {
let devices = Device.Machines[this.idMachine];
for (let id in devices) {
element = devices[id].bindings[name];
if (element) break;
}
if (!element) element = null;
this.bindings[name] = element;
}
}
return element;
}
/**
* findDevice(idDevice, fRequired)
*
* @this {Device}
* @param {string} idDevice
* @param {boolean} [fRequired] (default is true, so if the device is not found, an Error is thrown)
* @returns {Device|null}
*/
findDevice(idDevice, fRequired=true)
{
let id = idDevice;
let idMachine = this.idMachine;
let i = idMachine.indexOf('.');
if (i > 0) {
idMachine = idMachine.substr(0, i);
idDevice = idDevice.substr(i + 1);
}
let devices = Device.Machines[idMachine];
let device = devices && devices[idDevice] || null;
if (!device) {
/*
* Also check the old list of PCjs machine component IDs, to maintain backward compatibility.
*/
for (i = 0; i < Device.Components.length; i++) {
if (Device.Components[i]['id'] === id) {
device = Device.Components[i];
break;
}
}
if (!device && fRequired) {
throw new Error(this.sprintf('no "%s" device', id));
}
}
return device;
}
/**
* findDeviceByClass(idClass, fRequired)
*
* This is only appropriate for device classes where no more than one instance of the device is allowed;
* for example, it is NOT appropriate for the Bus class, because machines can have multiple buses (eg, an
* I/O bus and a Memory bus).
*
* @this {Device}
* @param {string} idClass
* @param {boolean} [fRequired] (default is true, so if the device is not found, an Error is thrown)
* @returns {Device|null}
*/
findDeviceByClass(idClass, fRequired=true)
{
let device = null;
let devices = Device.Machines[this.idMachine];
if (devices) {
for (let id in devices) {
if (devices[id].config['class'] == idClass) {
if (device) {
device = null; // multiple devices with the same class, so return an error
break;
}
device = devices[id];
}
}
}
if (!device && fRequired) {
throw new Error(this.sprintf('no %s device', idClass));
}
return device;
}
/**
* getMachineConfig(prop)
*
* @this {Device}
* @param {string} prop
* @returns {*}
*/
getMachineConfig(prop)
{
let machine = this.findDevice(this.idMachine);
return machine && machine.config && machine.config[prop] || this.config[prop];
}
/**
* getRegister(name)
*
* @this {Device}
* @param {string} name
* @returns {number|undefined}
*/
getRegister(name)
{
let reg = this.registers[name];
return reg && reg.get();
}
/**
* isReady()
*
* @this {Device}
* @returns {boolean}
*/
isReady()
{
if (this != this.machine || !this.ready) {
return this.ready;
}
/*
* Machine readiness is more complicated: check the readiness of all devices. This is easily
* checked with an enumDevices() function that returns false if a device isn't ready yet, which
* in turn terminates the enumeration and returns false.
*/
return this.enumDevices((device) => device.isReady());
}
/**
* setReady(ready)
*
* @this {Device}
* @param {boolean} [ready]
*/
setReady(ready = this.ready)
{
this.ready = ready;
if (this.isReady()) {
let callback;
while ((callback = this.aReadyCallbacks.pop())) {
callback();
}
if (this != this.machine) this.machine.setReady();
}
}
/**
* whenReady(callback)
*
* @this {Device}
* @param {function()} callback
* @returns {boolean} (true if ready now, false if not ready yet)
*/
whenReady(callback)
{
if (this.isReady()) {
callback();
return true;
}
this.aReadyCallbacks.push(callback);
return false;
}
/**
* notifyMessage(messages)
*
* Overidden by other devices (eg, Debugger) to receive notifications of messages, along with the messages bits.
*
* @this {Device}
* @param {number} messages
*/
notifyMessage(messages)
{
}
/**
* printf(format, ...args)
*
* Just as WebIO.printf() overrides StdIO.printf() to add support for Messages, we override WebIO.printf()
* to add support for MESSAGE.ADDR: if that message bit is set, we want to append the current execution address
* (PC) to any message-driven printf() call.
*
* @this {Device}
* @param {string|number} format
* @param {...} args
* @returns {number}
*/
printf(format, ...args)
{
if (typeof format == "number" && this.isMessageOn(format)) {
/*
* The following will execute at most once, because findDeviceByClass() returns either a Device or null,
* neither of which is undefined.
*/
if (this.dbg === undefined) {
this.dbg = /** @type {Device} */ (this.findDeviceByClass("Debugger", false));
}
if (this.dbg) {
this.dbg.notifyMessage(format);
}
if (this.machine.messages & MESSAGE.ADDR) {
/*
* Same rules as above apply here. Hopefully no message-based printf() calls will arrive with MESSAGE.ADDR
* set *before* the CPU device has been initialized.
*/
if (this.cpu === undefined) {
this.cpu = /** @type {CPU} */ (this.findDeviceByClass("CPU"));
}
if (this.cpu) {
format = args.shift();
return super.printf("%#06x: %s.%s\n", this.cpu.regPCLast, this.idDevice, this.sprintf(format, ...args).trim());
}
}
}
return super.printf(format, ...args);
}
/**
* removeDevice(idDevice)
*
* @this {Device}
* @param {string} idDevice
*/
removeDevice(idDevice)
{
let device;
let devices = Device.Machines[this.idMachine];
if (devices) delete devices[idDevice];
}
/**
* setRegister(name, value)
*
* @this {Device}
* @param {string} name
* @param {number} value
* @returns {boolean} (true if register exists and successfully set, false otherwise)
*/
setRegister(name, value)
{
let reg = this.registers[name];
if (reg && reg.set) {
reg.set(value);
return true;
}
return false;
}
}
/**
* Machines is a global object whose properties are machine IDs and whose values are arrays of Devices.
*
* @type {Object}
*/
Device.Machines = {};
/**
* Components is maintained for backward-compatibility with older PCjs machines, to facilitate machine connections.
*
* @type {Array}
*/
Device.Components = [];
/*
* List of additional message groups, extending the base set defined in lib/webio.js.
*
* NOTE: To support more than 32 message groups, be sure to use "+", not "|", when concatenating.
*/
MESSAGE.ADDR = 0x000000000001; // this is a special bit (bit 0) used to append address info to messages
MESSAGE.BUS = 0x000000000002;
MESSAGE.FAULT = 0x000000000004;
MESSAGE.MEMORY = 0x000000000008;
MESSAGE.PORTS = 0x000000000010;
MESSAGE.CHIPS = 0x000000000020;
MESSAGE.KBD = 0x000000000040;
MESSAGE.SERIAL = 0x000000000080;
MESSAGE.MISC = 0x000000000100;
MESSAGE.CPU = 0x000000000200;
MESSAGE.MMU = 0x000000000400;
MESSAGE.INT = 0x000000000800;
MESSAGE.TRAP = 0x000000001000;
MESSAGE.VIDEO = 0x000000002000; // used with video hardware messages (see video.js)
MESSAGE.MONITOR = 0x000000004000; // used with video monitor messages (see monitor.js)
MESSAGE.SCREEN = 0x000000008000; // used with screen-related messages (also monitor.js)
MESSAGE.TIME = 0x000000010000;
MESSAGE.TIMER = 0x000000020000;
MESSAGE.EVENT = 0x000000040000;
MESSAGE.INPUT = 0x000000080000;
MESSAGE.KEY = 0x000000100000;
MESSAGE.MOUSE = 0x000000200000;
MESSAGE.TOUCH = 0x000000400000;
MESSAGE.WARN = 0x000000800000;
MESSAGE.HALT = 0x000001000000;
MESSAGE.CUSTOM = 0x000100000000; // all custom device messages must start here
WebIO.MESSAGE_NAMES["addr"] = MESSAGE.ADDR;
WebIO.MESSAGE_NAMES["bus"] = MESSAGE.BUS;
WebIO.MESSAGE_NAMES["fault"] = MESSAGE.FAULT;
WebIO.MESSAGE_NAMES["memory"] = MESSAGE.MEMORY;
WebIO.MESSAGE_NAMES["ports"] = MESSAGE.PORTS;
WebIO.MESSAGE_NAMES["chips"] = MESSAGE.CHIPS;
WebIO.MESSAGE_NAMES["kbd"] = MESSAGE.KBD;
WebIO.MESSAGE_NAMES["serial"] = MESSAGE.SERIAL;
WebIO.MESSAGE_NAMES["misc"] = MESSAGE.MISC;
WebIO.MESSAGE_NAMES["cpu"] = MESSAGE.CPU;
WebIO.MESSAGE_NAMES["mmu"] = MESSAGE.MMU;
WebIO.MESSAGE_NAMES["int"] = MESSAGE.INT;
WebIO.MESSAGE_NAMES["trap"] = MESSAGE.TRAP;
WebIO.MESSAGE_NAMES["video"] = MESSAGE.VIDEO;
WebIO.MESSAGE_NAMES["monitor"] = MESSAGE.MONITOR;
WebIO.MESSAGE_NAMES["screen"] = MESSAGE.SCREEN;
WebIO.MESSAGE_NAMES["time"] = MESSAGE.TIME;
WebIO.MESSAGE_NAMES["timer"] = MESSAGE.TIMER;
WebIO.MESSAGE_NAMES["event"] = MESSAGE.EVENT;
WebIO.MESSAGE_NAMES["input"] = MESSAGE.INPUT;
WebIO.MESSAGE_NAMES["key"] = MESSAGE.KEY;
WebIO.MESSAGE_NAMES["mouse"] = MESSAGE.MOUSE;
WebIO.MESSAGE_NAMES["touch"] = MESSAGE.TOUCH;
WebIO.MESSAGE_NAMES["warn"] = MESSAGE.WARN;
WebIO.MESSAGE_NAMES["halt"] = MESSAGE.HALT;
if (window) {
if (!window['PCjs']) window['PCjs'] = {};
if (!window['PCjs']['Machines']) window['PCjs']['Machines'] = Device.Machines;
if (!window['PCjs']['Components']) window['PCjs']['Components'] = Device.Components;
}
Defs.CLASSES["Device"] = Device;