forked from mattgodbolt/jsbeeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tapes.js
262 lines (236 loc) · 9.37 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
define(['utils'], function (utils) {
"use strict";
function UefTape(stream) {
var self = this;
var dummyData, state, curByte, numDataBits, parity;
var numParityBits, numStopBits, carrierBefore, carrierAfter;
self.rewind = function () {
dummyData = [false, false, true, false, true, false, true, false, true, true];
state = -1;
curByte = 0;
numDataBits = 8;
parity = 'N';
numParityBits = 0;
numStopBits = 1;
carrierBefore = 0;
carrierAfter = 0;
stream.seek(10);
var minor = stream.readByte();
var major = stream.readByte();
if (major !== 0x00) throw "Unsupported UEF version " + major + "." + minor;
};
self.rewind();
function readChunk() {
var chunkId = stream.readInt16();
var length = stream.readInt32();
return {
id: chunkId,
stream: stream.substream(length)
};
}
var curChunk = readChunk();
var baseFrequency = 1200;
function secsToClocks(secs) {
return (2 * 1000 * 1000 * secs) | 0;
}
function cycles(count) {
return secsToClocks(count / baseFrequency);
}
function parityOf(curByte) {
var 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();
}
acia.setDCD(false);
var gap;
switch (curChunk.id) {
case 0x0000:
console.log("Origin: " + curChunk.stream.readNulString());
break;
case 0x0100:
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
if (state === -1) {
numDataBits = curChunk.stream.readByte();
parity = curChunk.stream.readByte();
numStopBits = curChunk.stream.readByte();
numParityBits = parity !== 'N' ? 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)) {
var bit = parityOf(curByte);
if (parity === 'N') 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
acia.setDCD(true);
if (state === -1) {
carrierBefore = curChunk.stream.readInt16();
carrierAfter = curChunk.stream.readInt16();
console.log("Carrier with", carrierBefore, carrierAfter);
state = 0;
acia.tone(2 * baseFrequency);
return cycles(carrierBefore);
} else if (state < 10) {
acia.tone(dummyData[state] ? baseFrequency : (2 * baseFrequency));
state++;
} else if (state === 10) {
acia.receive(0xaa);
acia.tone(2 * baseFrequency);
state++;
return cycles(carrierAfter);
} else {
state = -1;
}
return cycles(1);
case 0x0114:
console.log("Ignoring security cycles");
break;
case 0x0115:
console.log("Ignoring polarity change");
break;
case 0x0110:
var count = curChunk.stream.readInt16();
acia.setDCD(true);
acia.tone(2 * baseFrequency);
return cycles(count);
case 0x0113:
baseFrequency = curChunk.stream.readFloat32();
console.log("Frequency change ", baseFrequency);
break;
case 0x0112:
gap = 1 / (2 * curChunk.stream.readInt16() * baseFrequency);
console.log("Tape gap of " + gap + "s");
acia.tone(0);
return secsToClocks(gap);
case 0x0116:
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) {
var self = this;
self.count = 0;
self.stream = stream;
var dividerTable = [1, 16, 64, -1];
function rate(acia) {
var bitsPerByte = 9;
if (!(acia.cr & 0x80)) {
bitsPerByte++; // Not totally correct if the AUG is to be believed.
}
var divider = dividerTable[acia.cr & 0x03];
// http://beebwiki.mdfs.net/index.php/Serial_ULA says the serial rate is ignored
// for cassette mode.
var 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;
var byte = stream.readByte();
if (byte === 0xff) {
byte = stream.readByte();
if (byte === 0) {
acia.setDCD(false);
return 0;
} else if (byte === 0x04) {
acia.setDCD(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);
};
}
function loadTapeFromData(name, data) {
var 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;
}
function loadTape(name) {
console.log("Loading tape from " + name);
return utils.loadData(name).then(function (data) {
return loadTapeFromData(name, data);
});
}
return {
loadTape: loadTape,
loadTapeFromData: loadTapeFromData
};
});