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 pathrom.js
293 lines (277 loc) · 9.4 KB
/
rom.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
/**
* @fileoverview Simulates ROM
* @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";
/**
* @typedef {Config} ROMConfig
* @property {string} class
* @property {Object} [bindings]
* @property {number} [version]
* @property {Array.<string>} [overrides]
* @property {number} wordSize
* @property {number} valueSize
* @property {number} valueTotal
* @property {boolean} littleEndian
* @property {string} file
* @property {string} reference
* @property {string} chipID
* @property {number} [revision]
* @property {string} [colorROM]
* @property {string} [backgroundColorROM]
* @property {Array.<number>} values
*/
/**
* @class {ROM}
* @unrestricted
* @property {ROMConfig} config
* @property {Array.<number>} data
* @property {number} addrMask
*/
class ROM extends Device {
/**
* ROM(idMachine, idDevice, config)
*
* Sample config:
*
* "rom": {
* "class": "ROM",
* "wordSize": 13,
* "valueSize": 16,
* "valueTotal": 2048,
* "littleEndian": true,
* "file": "ti57le.bin",
* "reference": "",
* "chipID": "TMC1501NC DI 7741",
* "revision": "0",
* "bindings": {
* "array": "romArrayTI57",
* "cellDesc": "romCellTI57"
* },
* "overrides": ["colorROM","backgroundColorROM"],
* "values": [
* ...
* ]
* }
*
* @this {ROM}
* @param {string} idMachine
* @param {string} idDevice
* @param {ROMConfig} [config]
*/
constructor(idMachine, idDevice, config)
{
super(idMachine, idDevice, ROM.VERSION, config);
this.data = config['values'];
/*
* This addrMask calculation assumes that the data array length is a power-of-two (which we assert).
*/
this.addrMask = this.data.length - 1;
this.assert(!((this.addrMask + 1) & this.addrMask));
/*
* If an "array" binding has been supplied, then create an LED array sufficiently large to represent the
* entire ROM. If data.length is an odd power-of-two, then we will favor a slightly wider array over a taller
* one, by virtue of using Math.ceil() instead of Math.floor() for the columns calculation.
*/
if (this.bindings[ROM.BINDING.ARRAY]) {
let rom = this;
let addrLines = Math.log2(this.data.length) / 2;
this.cols = Math.pow(2, Math.ceil(addrLines));
this.rows = (this.data.length / this.cols)|0;
let configLEDs = {
"class": "LED",
"bindings": {"container": this.getBindingID(ROM.BINDING.ARRAY)},
"type": LED.TYPE.ROUND,
"cols": this.cols,
"rows": this.rows,
"color": this.getDefaultString('colorROM', "green"),
"backgroundColor": this.getDefaultString('backgroundColorROM', "black"),
"persistent": true
};
this.ledArray = new LED(idMachine, idDevice + "LEDs", configLEDs);
this.clearArray();
let configInput = {
"class": "Input",
"location": [0, 0, this.ledArray.widthView, this.ledArray.heightView, this.cols, this.rows],
"bindings": {"surface": this.getBindingID(ROM.BINDING.ARRAY)}
};
this.ledInput = new Input(idMachine, idDevice + "Input", configInput);
this.sCellDesc = this.getBindingText(ROM.BINDING.CELLDESC);
this.ledInput.addHover(function onROMHover(col, row) {
if (rom.chip) {
let sDesc = rom.sCellDesc;
if (col >= 0 && row >= 0) {
let addr = row * rom.cols + col;
this.assert(addr >= 0 && addr < rom.data.length);
let opCode = rom.data[addr];
sDesc = rom.chip.disassemble(opCode, addr);
}
rom.setBindingText(ROM.BINDING.CELLDESC, sDesc);
}
});
}
}
/**
* clearArray()
*
* clearBuffer(true) performs a combination of clearBuffer() and drawBuffer().
*
* @this {ROM}
*/
clearArray()
{
if (this.ledArray) this.ledArray.clearBuffer(true);
}
/**
* drawArray()
*
* This performs a simple drawBuffer(); intended for synchronous updates (eg, step operations);
* otherwise, you should allow the LED object's async animation handler take care of drawing updates.
*
* @this {ROM}
*/
drawArray()
{
if (this.ledArray) this.ledArray.drawBuffer();
}
/**
* getData(addr, fInternal)
*
* Set fInternal to true if an internal caller (eg, the disassembler) is accessing the ROM, to avoid touching
* the ledArray.
*
* @this {ROM}
* @param {number} addr
* @param {boolean} [fInternal]
* @returns {number|undefined}
*/
getData(addr, fInternal)
{
if (this.ledArray && !fInternal) {
this.ledArray.setLEDState(addr % this.cols, (addr / this.cols)|0, LED.STATE.ON, LED.FLAGS.MODIFIED);
}
return this.data[addr];
}
/**
* loadState(state)
*
* If any saved values don't match (presumably overridden), abandon the given state and return false.
*
* @this {ROM}
* @param {Array} state
* @returns {boolean}
*/
loadState(state)
{
let length, success = true;
let buffer = state.shift();
if (buffer && this.ledArray) {
length = buffer.length;
this.assert(this.ledArray.buffer.length == length);
if (this.ledArray.buffer.length == length) {
this.ledArray.buffer = buffer;
this.ledArray.drawBuffer(true);
} else {
this.printf("inconsistent saved LED state (%d), unable to load\n", length);
success = false;
}
}
/*
* Version 1.21 and up also saves the ROM contents, since our "mini-debugger" has been updated
* with an edit command ("e") to enable ROM patching. However, we prefer to detect improvements
* in saved state based on the length of the array, not the version number.
*/
if (state.length) {
let data = state.shift();
let length = data && data.length || -1;
if (this.data.length == length) {
this.data = data;
} else {
this.printf("inconsistent saved ROM state (%d), unable to load\n", length);
success = false;
}
}
return success;
}
/**
* reset()
*
* Called by the Chip (eg, TMS1500) onReset() handler. Originally, there was no need for this
* handler, until we added the min-debugger's ability to edit ROM locations via setData(). So this
* gives the user the ability to revert back to the original ROM if they want to undo any modifications.
*
* @this {ROM}
*/
reset()
{
this.data = this.config['values'];
}
/**
* saveState(state)
*
* @this {ROM}
* @param {Array} state
*/
saveState(state)
{
if (this.ledArray) {
state.push(this.ledArray.buffer);
state.push(this.data);
}
}
/**
* setChip()
*
* @this {ROM}
* @param {Chip} chip
*/
setChip(chip)
{
this.chip = chip;
}
/**
* setData(addr, value)
*
* @this {ROM}
* @param {number} addr
* @param {number} value
* @return {number|undefined} (previous value, if available)
*/
setData(addr, value)
{
let prev;
if (addr >= 0 && addr < this.data.length) {
prev = this.data[addr];
this.data[addr] = value;
}
return prev;
}
}
ROM.BINDING = {
ARRAY: "array",
CELLDESC: "cellDesc"
};
ROM.VERSION = +VERSION || 1.00;