-
Notifications
You must be signed in to change notification settings - Fork 3
/
uef_file.js
462 lines (365 loc) · 13.6 KB
/
uef_file.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
// uef file handling emulation routines for ElkJs
// written by Darren Coles
ElkJs.UEFFile = function (opts) {
var self = {};
var tableName = "";
var snapshotLoaded = false;
var tapeLoaded = false;
var tapeData = new Array();
var tapePos = 0;
var currentTime = 0;
var dataOffset = 0;
var dataBit = -1;
var processor = opts.processor;
var memory = opts.memory;
var sheila = opts.sheila;
var highTone = false;
var autoLoad = true;
var fastLoad = true;
self.filename = "";
function makeChunk(code, data) {
var bytes = new Array();
bytes.push(code & 0xff);
bytes.push(code >> 8);
bytes.push(data.length & 0xff);
bytes.push((data.length >> 8) & 0xff);
bytes.push((data.length >> 16) & 0xff);
bytes.push((data.length >> 24) & 0xff);
bytes = bytes.concat(data);
return bytes;
}
function load_filereader(file, callback, callback2) {
var reader = new FileReader();
reader.onload = function (e) {
callback(e.target.result, callback2);
};
reader.readAsArrayBuffer(file);
}
function load_binary_resource(url, callback, callback2) {
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response; // Note: not oReq.responseText
if (arrayBuffer) {
callback(arrayBuffer, callback2);
}
};
oReq.send(null);
}
function processUEF(arrayBuffer, autoLoadcallback) {
tapeData = new Array();
tapePos = 0;
var buffer = null;
buffer = new Uint8Array(arrayBuffer);
var unzipper = new JSUnzip(buffer);
if (unzipper.isZipFile()) {
unzipper.readEntries();
for (var i = 0; i < unzipper.entries.length; i++) {
var entry = unzipper.entries[i];
var fname = entry.fileName.toUpperCase();
if (fname.length > 4 & fname.substr(entry.fileName.length - 4) == '.UEF' & (entry.compressionMethod == 8 | entry.compressionMethod == 0)) {
if (entry.compressionMethod == 8) {
var deflated = JSInflate.inflate(entry.data);
buffer = new Uint8Array(deflated);
} else {
buffer = Uint8Array(entry.data);
}
break;
}
}
}
if (buffer[0] == 0x1f & buffer[1] == 0x8b & buffer[2] == 0x08) {
var deflated = JSInflate.inflate(buffer);
buffer = new Uint8Array(deflated);
}
snapshotLoaded = false;
tapeLoaded = false;
if ((String.fromCharCode(buffer[0]) != 'U') | (String.fromCharCode(buffer[1]) != 'E') | (String.fromCharCode(buffer[2]) != 'F') |
(String.fromCharCode(buffer[3]) != ' ') | (String.fromCharCode(buffer[4]) != 'F') | (String.fromCharCode(buffer[5]) != 'i') |
(String.fromCharCode(buffer[6]) != 'l') | (String.fromCharCode(buffer[7]) != 'e') | (String.fromCharCode(buffer[8]) != '!') |
(buffer[9] != 0)) {
return;
}
var offset = 12;
var chunktype = 0;
var chunklen = 0;
while (offset < buffer.length) {
chunktype = (buffer[offset]) + (buffer[offset + 1] << 8);
chunklen = (buffer[offset + 2]) + (buffer[offset + 3] << 8) + (buffer[offset + 4] << 16) + (buffer[offset + 5] << 24);
offset += 6;
switch (chunktype) {
//snapshot chunks
case 0x0400: //6502 state
// update,a, p (status), x, y, s, pc
processor.loadSnapshot(buffer, offset + 1);
snapshotLoaded = true;
break;
case 0x0401: //ula chunk type
sheila.loadSnapshot(buffer, offset + 1);
snapshotLoaded = true;
break
case 0x0410: //memory chunk type
memory.loadSnapshot(buffer, offset + 1, chunklen - 1);
snapshotLoaded = true;
break;
//tape data chunks
case 0x100:
tapeLoaded = true;
var data = new Array();
for (i = 0; i < chunklen; i++) { data.push(buffer[offset + i]); }
tapeData.push({
type: "implicitDataBlock",
desc: "data block",
data: data
});
break;
case 0x101:
tapeData.push({
desc: "multiplexed data block"
});
break;
case 0x102:
tapeData.push({
desc: "explicit tape data block"
});
break;
case 0x104:
tapeData.push({
desc: "defined tape format data block"
});
break;
case 0x110:
cycleCount = buffer[offset] + (buffer[offset + 1] << 8)
tapeData.push({
type: "carrier",
desc: "carrier",
cycleCount: cycleCount
});
break;
case 0x111:
tapeLoaded = true;
cycleCount1 = buffer[offset] + (buffer[offset + 1] << 8)
cycleCount2 = buffer[offset + 2] + (buffer[offset + 3] << 8)
tapeData.push({
type: "carrierWithDummy",
desc: "carrier with dummy byte",
preCycleCount: cycleCount1,
postCycleCount: cycleCount2
});
break;
case 0x112:
tapeLoaded = true;
gap = buffer[offset] + (buffer[offset + 1] << 8)
tapeData.push({
type: "integerGap",
desc: "integer tap",
gapTime: gap
});
break;
case 0x116:
tapeData.push({
desc: "floating point gap"
});
break;
case 0x113:
tapeData.push({
desc: "change of base frequency"
});
break;
case 0x114:
tapeData.push({
desc: "security cycles"
});
break;
case 0x115:
tapeData.push({
desc: "phase change"
});
break;
case 0x117:
tapeData.push({
desc: "data encoding format change"
});
break;
case 0x120:
tapeData.push({
desc: "position marker"
});
break;
case 0x130:
tapeData.push({
desc: "tape set info"
});
break;
case 0x131:
tapeData.push({
desc: "start of tape side"
});
break;
}
offset += chunklen;
}
if (tapeLoaded & autoLoad) {
autoLoadcallback();
}
}
self.loadUEF = function (file, autoLoadCallback) {
if (Object.prototype.toString.call(file) == '[object File]') {
self.filename = file.filename;
load_filereader(file, processUEF, autoLoadCallback);
}
else {
load_binary_resource(file, processUEF, autoLoadCallback)
self.filename = file;
}
self.createUEFSnapshot();
}
self.createUEFSnapshot = function () {
var bytes = new Array();
bytes.push(0x55); bytes.push(0x45); bytes.push(0x46); bytes.push(0x20); bytes.push(0x46); bytes.push(0x69); bytes.push(0x6c); bytes.push(0x65); bytes.push(0x21); bytes.push(0x0); bytes.push(0xa); bytes.push(0x0);
var temp = new Array();
temp.push(0x10);
//target machine chunk
bytes = bytes.concat(makeChunk(0x5, temp));
bytes = bytes.concat(makeChunk(0x400, processor.makeSnapshotData()));
bytes = bytes.concat(makeChunk(0x401, sheila.makeSnapshotData()));
bytes = bytes.concat(makeChunk(0x410, memory.makeSnapshotData()));
return bytes;
}
self.snapshotLoaded = function () {
return snapshotLoaded;
}
self.tapeLoaded = function () {
return tapeLoaded;
}
self.process = function () {
var processNext = false;
if (!tapeData.length) return;
switch (tapeData[tapePos].type) {
case "carrier":
if (!highTone) {
sheila.trigger_hightone();
highTone = true;
}
currentTime++;
if (currentTime >= tapeData[tapePos].cycleCount * 13 / (fastLoad ? 4 : 1)) processNext = true;
break;
case "implicitDataBlock":
highTone = false;
currentTime++;
if (fastLoad & sheila.casetteDataRead & currentTime < 11) currentTime = 11;
if (currentTime >= 13) {
currentTime = 0;
if ((dataBit++) == 8) {
dataBit = 0;
sheila.clear_receive();
if (++dataOffset >= tapeData[tapePos].data.length) {
processNext = true;
}
else {
//sheila.addTapeBit(tapeData[tapePos].data[dataOffset] & (1 << dataBit));
}
}
else {
//sheila.addTapeBit(tapeData[tapePos].data[dataOffset] & (1 << dataBit));
if (dataBit == 7) {
sheila.setTapeData(tapeData[tapePos].data[dataOffset]);
sheila.casetteDataRead = false;
sheila.trigger_receive();
}
}
}
break;
case "integerGap":
currentTime++;
if (currentTime >= tapeData[tapePos].gapTime * 13) {
processNext = true;
}
break;
default:
processNext = true;
}
if (processNext) {
if (tableName != "") self.populateTapeDataTable(tableName);
if (++tapePos > tapeData.length) {
tapePos = 0;
}
currentTime = 0;
dataOffset = 0;
dataBit = -1;
}
}
self.stopTape = function () {
highTone = false;
currentTime = 0;
dataOffset = 0;
dataBit = -1;
}
self.startTape = function () {
}
self.setAutoLoad = function (auto) {
autoLoad = auto;
}
self.setLoadSpeed = function (speed) {
fastLoad = speed > 1;
}
self.populateTapeDataTable = function (tablename) {
tableName = tablename;
$("#" + tablename + " tr").remove();
$("#" + tablename).append('<tr style="height: 20px;background-color: blue"><th> Chunk</th><th> Description</th></tr>');
if (!tapeData) return;
for (var i = 0; i < tapeData.length; i++) {
if (i == tapePos) {
$("#" + tablename).append('<tr style="height: 20px;background-color: white"><td>' + i.toString() + '</td><td>' + tapeData[i].desc + '</td></tr>');
}
else {
$("#" + tablename).append('<tr style="height: 20px;background-color: gray"><td>' + i.toString() + '</td><td>' + tapeData[i].desc + '</td></tr>');
}
}
}
self.tapeFwd = function () {
if (!tapeData) return;
if (tapePos < tapeData.length - 1) tapePos++;
highTone = false;
currentTime = 0;
dataOffset = 0;
dataBit = -1;
if (tableName != "") self.populateTapeDataTable(tableName);
}
self.tapeRew = function () {
if (!tapeData) return;
if (tapePos > 0) tapePos--;
highTone = false;
currentTime = 0;
dataOffset = 0;
dataBit = -1;
if (tableName != "") self.populateTapeDataTable(tableName);
}
self.tapeFirst = function () {
if (!tapeData) return;
tapePos = 0;
highTone = false;
currentTime = 0;
dataOffset = 0;
dataBit = -1;
if (tableName != "") self.populateTapeDataTable(tableName);
}
self.tapeLast = function () {
if (!tapeData) return;
tapePos = tapeData.length - 1;
highTone = false;
currentTime = 0;
dataOffset = 0;
dataBit = -1;
if (tableName != "") self.populateTapeDataTable(tableName);
}
self.tapeEject = function () {
if (!tapeData) return;
tapeLoaded = false;
tapeData = new Array();
tapePos = 0;
if (tableName != "") self.populateTapeDataTable(tableName);
}
return self;
}