forked from mattgodbolt/jsbeeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tapes.js
267 lines (246 loc) · 9 KB
/
tapes.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
"use strict";
import * as utils from "./utils.js";
function UefTape(stream) {
const self = this;
let dummyData, state, count, curByte, numDataBits, parity;
let numParityBits, numStopBits, carrierBefore, carrierAfter;
const ParityN = "N".charCodeAt(0);
self.rewind = function () {
dummyData = [false, false, true, false, true, false, true, false, true, true];
state = -1;
count = 0;
curByte = 0;
numDataBits = 8;
parity = ParityN;
numParityBits = 0;
numStopBits = 1;
carrierBefore = 0;
carrierAfter = 0;
stream.seek(10);
const minor = stream.readByte();
const major = stream.readByte();
if (major !== 0x00) throw "Unsupported UEF version " + major + "." + minor;
};
self.rewind();
function readChunk() {
const chunkId = stream.readInt16();
const length = stream.readInt32();
return {
id: chunkId,
stream: stream.substream(length),
};
}
let curChunk = readChunk();
let baseFrequency = 1200;
function secsToClocks(secs) {
return (2 * 1000 * 1000 * secs) | 0;
}
function cycles(count) {
return secsToClocks(count / baseFrequency);
}
function parityOf(curByte) {
let parity = false;
while (curByte) {
parity = !parity;
curByte >>>= 1;
}
return parity;
}
self.poll = function (acia) {
if (!curChunk) return;
if (state === -1) {
if (stream.eof()) {
curChunk = null;
return;
}
curChunk = readChunk();
}
let gap;
switch (curChunk.id) {
case 0x0000:
console.log("Origin: " + curChunk.stream.readNulString());
break;
case 0x0100:
acia.setTapeCarrier(false);
if (state === -1) {
state = 0;
curByte = curChunk.stream.readByte();
acia.tone(baseFrequency); // Start bit
} else if (state < 9) {
if (state === 0) {
// Start bit
acia.tone(baseFrequency);
} else {
acia.tone(curByte & (1 << (state - 1)) ? 2 * baseFrequency : baseFrequency);
}
state++;
} else {
acia.receive(curByte);
acia.tone(2 * baseFrequency); // Stop bit
if (curChunk.stream.eof()) {
state = -1;
} else {
state = 0;
curByte = curChunk.stream.readByte();
}
}
return cycles(1);
case 0x0104: // Defined data
acia.setTapeCarrier(false);
if (state === -1) {
numDataBits = curChunk.stream.readByte();
parity = curChunk.stream.readByte();
numStopBits = curChunk.stream.readByte();
numParityBits = parity !== ParityN ? 1 : 0;
console.log("Defined data with " + numDataBits + String.fromCharCode(parity) + numStopBits);
state = 0;
}
if (state === 0) {
if (curChunk.stream.eof()) {
state = -1;
} else {
curByte = curChunk.stream.readByte() & ((1 << numDataBits) - 1);
acia.tone(baseFrequency); // Start bit
state++;
}
} else if (state < 1 + numDataBits) {
acia.tone(curByte & (1 << (state - 1)) ? 2 * baseFrequency : baseFrequency);
state++;
} else if (state < 1 + numDataBits + numParityBits) {
let bit = parityOf(curByte);
if (parity === ParityN) bit = !bit;
acia.tone(bit ? 2 * baseFrequency : baseFrequency);
state++;
} else if (state < 1 + numDataBits + numParityBits + numStopBits) {
acia.tone(2 * baseFrequency); // Stop bits
state++;
} else {
acia.receive(curByte);
state = 0;
return 0;
}
return cycles(1);
case 0x0111: // Carrier tone with dummy data
if (state === -1) {
state = 0;
carrierBefore = curChunk.stream.readInt16();
carrierAfter = curChunk.stream.readInt16();
console.log("Carrier with", carrierBefore, carrierAfter);
}
if (state === 0) {
acia.setTapeCarrier(true);
acia.tone(2 * baseFrequency);
carrierBefore--;
if (carrierBefore <= 0) state = 1;
} else if (state < 11) {
acia.setTapeCarrier(false);
acia.tone(dummyData[state - 1] ? baseFrequency : 2 * baseFrequency);
if (state === 10) {
acia.receive(0xaa);
}
state++;
} else {
acia.setTapeCarrier(true);
acia.tone(2 * baseFrequency);
carrierAfter--;
if (carrierAfter <= 0) state = -1;
}
return cycles(1);
case 0x0114:
console.log("Ignoring security cycles");
break;
case 0x0115:
console.log("Ignoring polarity change");
break;
case 0x0110: // Carrier tone.
if (state === -1) {
state = 0;
count = curChunk.stream.readInt16();
}
acia.setTapeCarrier(true);
acia.tone(2 * baseFrequency);
count--;
if (count <= 0) state = -1;
return cycles(1);
case 0x0113:
baseFrequency = curChunk.stream.readFloat32();
console.log("Frequency change ", baseFrequency);
break;
case 0x0112:
acia.setTapeCarrier(false);
gap = 1 / (2 * curChunk.stream.readInt16() * baseFrequency);
console.log("Tape gap of " + gap + "s");
acia.tone(0);
return secsToClocks(gap);
case 0x0116:
acia.setTapeCarrier(false);
gap = curChunk.stream.readFloat32();
console.log("Tape gap of " + gap + "s");
acia.tone(0);
return secsToClocks(gap);
default:
console.log("Skipping unknown chunk " + utils.hexword(curChunk.id));
curChunk = readChunk();
break;
}
return cycles(1);
};
}
function TapefileTape(stream) {
const self = this;
self.count = 0;
self.stream = stream;
const dividerTable = [1, 16, 64, -1];
function rate(acia) {
let bitsPerByte = 9;
if (!(acia.cr & 0x80)) {
bitsPerByte++; // Not totally correct if the AUG is to be believed.
}
const divider = dividerTable[acia.cr & 0x03];
// http://beebwiki.mdfs.net/index.php/Serial_ULA says the serial rate is ignored
// for cassette mode.
const cpp = (2 * 1000 * 1000) / (19200 / divider);
return Math.floor(bitsPerByte * cpp);
}
self.rewind = function () {
stream.seek(10);
};
self.poll = function (acia) {
if (stream.eof()) return 100000;
let byte = stream.readByte();
if (byte === 0xff) {
byte = stream.readByte();
if (byte === 0) {
acia.setTapeCarrier(false);
return 0;
} else if (byte === 0x04) {
acia.setTapeCarrier(true);
// Simulate 5 seconds of carrier.
return 5 * 2 * 1000 * 1000;
} else if (byte !== 0xff) {
throw "Got a weird byte in the tape";
}
}
acia.receive(byte);
return rate(acia);
};
}
export function loadTapeFromData(name, data) {
const stream = new utils.DataStream(name, data);
if (stream.readByte(0) === 0xff && stream.readByte(1) === 0x04) {
console.log("Detected a 'tapefile' tape");
return new TapefileTape(stream);
}
if (stream.readNulString(0) === "UEF File!") {
console.log("Detected a UEF tape");
return new UefTape(stream);
}
console.log("Unknown tape format");
return null;
}
export function loadTape(name) {
console.log("Loading tape from " + name);
return utils.loadData(name).then(function (data) {
return loadTapeFromData(name, data);
});
}