This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathdevice.js
409 lines (391 loc) · 13.7 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
/**
* @fileoverview Base class for devices, with assorted handy services
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2019 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/devices/machine.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";
/**
* 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.PORT = 0x000000000004;
MESSAGE.MEMORY = 0x000000000008;
MESSAGE.CPU = 0x000000000010;
MESSAGE.VIDEO = 0x000000000020; // used with video hardware messages (see video.js)
MESSAGE.MONITOR = 0x000000000040; // used with video monitor messages (see monitor.js)
MESSAGE.SCREEN = 0x000000000080; // used with screen-related messages (also monitor.js)
MESSAGE.TIMER = 0x000000000100;
MESSAGE.EVENT = 0x000000000200;
MESSAGE.KEY = 0x000000000400;
MESSAGE.WARN = 0x000000000800;
MESSAGE.HALT = 0x000000001000;
MessageNames["addr"] = MESSAGE.ADDR;
MessageNames["bus"] = MESSAGE.BUS;
MessageNames["port"] = MESSAGE.PORT;
MessageNames["memory"] = MESSAGE.MEMORY;
MessageNames["cpu"] = MESSAGE.CPU;
MessageNames["video"] = MESSAGE.VIDEO;
MessageNames["monitor"] = MESSAGE.MONITOR;
MessageNames["screen"] = MESSAGE.SCREEN;
MessageNames["timer"] = MESSAGE.TIMER;
MessageNames["event"] = MESSAGE.EVENT;
MessageNames["key"] = MESSAGE.KEY;
MessageNames["warn"] = MESSAGE.WARN;
MessageNames["halt"] = MESSAGE.HALT;
MessageNames["buffer"] = MESSAGE.BUFFER;
/**
* 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()
* 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} status
* @property {Object} registers
* @property {Device|undefined|null} cpu
*/
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. 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 {number} [version]
*/
constructor(idMachine, idDevice, config, version)
{
super();
this.idMachine = idMachine;
this.idDevice = idDevice;
this.checkConfig(config);
this.checkVersion(version);
this.addDevice();
this.registers = {};
this.cpu = undefined;
}
/**
* addDevice()
*
* Adds this Device to the global set of Devices, so that findDevice(), findBinding(), etc, will work.
*
* @this {Device}
*/
addDevice()
{
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;
}
/**
* checkConfig(config)
*
* @this {Device}
* @param {Config} [config]
*/
checkConfig(config = {})
{
/*
* 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.
*/
if (config['overrides']) {
let parms = this.getURLParms();
for (let prop in parms) {
if (config['overrides'].indexOf(prop) >= 0) {
let value;
let s = parms[prop];
/*
* You might think we could simply call parseInt() and check isNaN(), but parseInt() has
* some annoying quirks, like stopping at the first non-numeric character. If the ENTIRE
* string isn't a number, then we don't want to treat ANY part of it as a number.
*/
if (s.match(/^[+-]?[0-9.]+$/)) {
value = Number.parseInt(s, 10);
} else 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.checkMachine(config);
}
/**
* checkMachine(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
*/
checkMachine(config)
{
if (this.version) {
let sVersion = "", version;
let machine = this.findDevice(this.idMachine);
if (machine.version != this.version) {
sVersion = "Machine";
version = machine.version;
}
else if (config.version && config.version > this.version) {
sVersion = "Config";
version = config.version;
}
if (sVersion) {
let sError = this.sprintf("%s Device version (%3.2f) incompatible with %s version (%3.2f)", config.class, this.version, sVersion, version);
this.alert("Error: " + sError + '\n\n' + "Clearing your browser's cache may resolve the issue.", Device.Alerts.Version);
}
}
}
/**
* checkVersion(version)
*
* @this {Device}
* @param {number} [version]
*/
checkVersion(version)
{
this.version = version || +VERSION;
}
/**
* 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.bind(this)};
}
/**
* enumDevices(func)
*
* @this {Device}
* @param {function(Device)} func
*/
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.CLASS.MACHINE) {
func(device);
}
}
}
} catch(err) {
this.printf("error while enumerating device '%s': %s\n", id, err.message);
}
}
/**
* 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]
* @return {Element|null|undefined}
*/
findBinding(name, all = false)
{
let 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)
*
* @this {Device}
* @param {string} idDevice
* @return {Device|null}
*/
findDevice(idDevice)
{
let devices = Device.Machines[this.idMachine];
return devices && devices[idDevice] || null;
}
/**
* findDeviceByClass(idClass)
*
* 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
* @return {Device|null}
*/
findDeviceByClass(idClass)
{
let device = null;
let devices = Device.Machines[this.idMachine];
if (devices) {
for (let id in devices) {
if (devices[id].config['class'] == idClass) {
device = devices[id];
break;
}
}
}
return device;
}
/**
* getRegister(name)
*
* @this {Device}
* @param {string} name
* @return {number|undefined}
*/
getRegister(name)
{
let reg = this.registers[name];
return reg && reg.get();
}
/**
* 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
*/
printf(format, ...args)
{
if (typeof format == "number" && (Messages & MESSAGE.ADDR) && this.isMessageOn(format)) {
/*
* The following will execute at most once, because findDeviceByClass() returns either a Device or null,
* neither of which is undefined. 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(Machine.CLASS.CPU));
}
if (this.cpu) {
format = args.shift();
let s = this.sprintf(format, ...args).trim();
super.printf("%s at %#0x\n", s, this.cpu.regPCLast);
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
*/
setRegister(name, value)
{
let reg = this.registers[name];
if (reg) reg.set(value);
}
}
/**
* Machines is a global object whose properties are machine IDs and whose values are arrays of Devices.
*
* @type {Object}
*/
Device.Machines = {};