-
Notifications
You must be signed in to change notification settings - Fork 68
/
tapes.js
265 lines (247 loc) · 9.47 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
"use strict";
import * as utils from "./utils.js";
function secsToClocks(secs) {
return (2 * 1000 * 1000 * secs) | 0;
}
function parityOf(curByte) {
let parity = false;
while (curByte) {
parity = !parity;
curByte >>>= 1;
}
return parity;
}
const ParityN = "N".charCodeAt(0);
class UefTape {
constructor(stream) {
this.stream = stream;
this.baseFrequency = 1200;
this.rewind();
this.curChunk = this.readChunk();
}
rewind() {
this.dummyData = [false, false, true, false, true, false, true, false, true, true];
this.state = -1;
this.count = 0;
this.curByte = 0;
this.numDataBits = 8;
this.parity = ParityN;
this.numParityBits = 0;
this.numStopBits = 1;
this.carrierBefore = 0;
this.carrierAfter = 0;
this.stream.seek(10);
const minor = this.stream.readByte();
const major = this.stream.readByte();
if (major !== 0x00) throw "Unsupported UEF version " + major + "." + minor;
}
readChunk() {
const chunkId = this.stream.readInt16();
const length = this.stream.readInt32();
return {
id: chunkId,
stream: this.stream.substream(length),
};
}
poll(acia) {
if (!this.curChunk) return;
if (this.state === -1) {
if (this.stream.eof()) {
this.curChunk = null;
return;
}
this.curChunk = this.readChunk();
}
let gap;
switch (this.curChunk.id) {
case 0x0000:
console.log("Origin: " + this.curChunk.stream.readNulString());
break;
case 0x0100:
acia.setTapeCarrier(false);
if (this.state === -1) {
this.state = 0;
this.curByte = this.curChunk.stream.readByte();
acia.tone(this.baseFrequency); // Start bit
} else if (this.state < 9) {
if (this.state === 0) {
// Start bit
acia.tone(this.baseFrequency);
} else {
acia.tone(this.curByte & (1 << (this.state - 1)) ? 2 * this.baseFrequency : this.baseFrequency);
}
this.state++;
} else {
acia.receive(this.curByte);
acia.tone(2 * this.baseFrequency); // Stop bit
if (this.curChunk.stream.eof()) {
this.state = -1;
} else {
this.state = 0;
this.curByte = this.curChunk.stream.readByte();
}
}
return this.cycles(1);
case 0x0104: // Defined data
acia.setTapeCarrier(false);
if (this.state === -1) {
this.numDataBits = this.curChunk.stream.readByte();
this.parity = this.curChunk.stream.readByte();
this.numStopBits = this.curChunk.stream.readByte();
this.numParityBits = this.parity !== ParityN ? 1 : 0;
console.log(
`Defined data with ${this.numDataBits}${String.fromCharCode(this.parity)}${this.numStopBits}`,
);
this.state = 0;
}
if (this.state === 0) {
if (this.curChunk.stream.eof()) {
this.state = -1;
} else {
this.curByte = this.curChunk.stream.readByte() & ((1 << this.numDataBits) - 1);
acia.tone(this.baseFrequency); // Start bit
this.state++;
}
} else if (this.state < 1 + this.numDataBits) {
acia.tone(this.curByte & (1 << (this.state - 1)) ? 2 * this.baseFrequency : this.baseFrequency);
this.state++;
} else if (this.state < 1 + this.numDataBits + this.numParityBits) {
let bit = parityOf(this.curByte);
if (this.parity === ParityN) bit = !bit;
acia.tone(bit ? 2 * this.baseFrequency : this.baseFrequency);
this.state++;
} else if (this.state < 1 + this.numDataBits + this.numParityBits + this.numStopBits) {
acia.tone(2 * this.baseFrequency); // Stop bits
this.state++;
} else {
acia.receive(this.curByte);
this.state = 0;
return 0;
}
return this.cycles(1);
case 0x0111: // Carrier tone with dummy data
if (this.state === -1) {
this.state = 0;
this.carrierBefore = this.curChunk.stream.readInt16();
this.carrierAfter = this.curChunk.stream.readInt16();
console.log("Carrier with", this.carrierBefore, this.carrierAfter);
}
if (this.state === 0) {
acia.setTapeCarrier(true);
acia.tone(2 * this.baseFrequency);
this.carrierBefore--;
if (this.carrierBefore <= 0) this.state = 1;
} else if (this.state < 11) {
acia.setTapeCarrier(false);
acia.tone(this.dummyData[this.state - 1] ? this.baseFrequency : 2 * this.baseFrequency);
if (this.state === 10) {
acia.receive(0xaa);
}
this.state++;
} else {
acia.setTapeCarrier(true);
acia.tone(2 * this.baseFrequency);
this.carrierAfter--;
if (this.carrierAfter <= 0) this.state = -1;
}
return this.cycles(1);
case 0x0114:
console.log("Ignoring security cycles");
break;
case 0x0115:
console.log("Ignoring polarity change");
break;
case 0x0110: // Carrier tone.
if (this.state === -1) {
this.state = 0;
this.count = this.curChunk.stream.readInt16();
}
acia.setTapeCarrier(true);
acia.tone(2 * this.baseFrequency);
this.count--;
if (this.count <= 0) this.state = -1;
return this.cycles(1);
case 0x0113:
this.baseFrequency = this.curChunk.stream.readFloat32();
console.log("Frequency change ", this.baseFrequency);
break;
case 0x0112:
acia.setTapeCarrier(false);
gap = 1 / (2 * this.curChunk.stream.readInt16() * this.baseFrequency);
console.log("Tape gap of " + gap + "s");
acia.tone(0);
return secsToClocks(gap);
case 0x0116:
acia.setTapeCarrier(false);
gap = this.curChunk.stream.readFloat32();
console.log("Tape gap of " + gap + "s");
acia.tone(0);
return secsToClocks(gap);
default:
console.log("Skipping unknown chunk " + utils.hexword(this.curChunk.id));
this.curChunk = this.readChunk();
break;
}
return this.cycles(1);
}
cycles(count) {
return secsToClocks(count / this.baseFrequency);
}
}
const dividerTable = [1, 16, 64, -1];
class TapefileTape {
constructor(stream) {
this.count = 0;
this.stream = stream;
}
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);
}
rewind() {
this.stream.seek(10);
}
poll(acia) {
if (this.stream.eof()) return 100000;
let byte = this.stream.readByte();
if (byte === 0xff) {
byte = this.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 this.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 async function loadTape(name) {
console.log("Loading tape from " + name);
return loadTapeFromData(name, await utils.loadData(name));
}