Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 275x 5x 5x 275x 275x 693487x 152x 3x 149x 95x 95x 95x 95x 12x 8x 8x 4x 2x 2x 12x 15x 6x 2x 2x 2x 3x 3x 3x 5x 5x 5x 693337x 693337x 693337x 693337x 693337x 693337x 11x 6x 6x 6x 6x 5x 5x 5x 5x 5x 9x 9x 5x 5x 5x 5x 5x 5x 5x 5x 3x 3x 2x 2x 1x 1x 3x 2x 1x 1x 50x 50x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 8x 8x 2x 1x 1x 1x 1x 1x 631x 631x 631x 617x 617x 617x 14x 14x 14x 631x 631x 313x 313x 318x 318x 11x 8x 8x 7x | // Some info:
// http://www.playvectrex.com/designit/lecture/UP12.HTM
// https://books.google.com/books?id=wUecAQAAQBAJ&pg=PA431&lpg=PA431&dq=acia+tdre&source=bl&ots=mp-yF-mK-P&sig=e6aXkFRfiIOb57WZmrvdIGsCooI&hl=en&sa=X&ei=0g2fVdDyFIXT-QG8-JD4BA&ved=0CCwQ6AEwAw#v=onepage&q=acia%20tdre&f=false
// http://www.classiccmp.org/dunfield/r/6850.pdf
export class Acia extends EventTarget {
constructor(cpu, toneGen, scheduler, relayNoise) {
super();
this.cpu = cpu;
this.toneGen = toneGen;
this.rs423Handler = null;
this.relayNoise = relayNoise;
this.sr = 0x00;
this.cr = 0x00;
this.dr = 0x00;
this.rs423Selected = false;
this.motorOn = false;
this.playPressed = true;
this.tapeCarrierCount = 0;
this.tapeDcdLineLevel = false;
this.hadDcdHigh = false;
this.saidOverrun = false;
this.serialReceiveRate = 0;
this.serialReceiveCyclesPerByte = 0;
this.serialTransmitRate = 0;
this.serialTransmitCyclesPerByte = 0;
this.setSerialReceive(19200);
this.setSerialTransmit(19200);
this.txCompleteTask = scheduler.newTask(() => {
this.sr |= 0x02; // set the TDRE
this.updateIrq();
});
this.runTapeTask = scheduler.newTask(() => this.runTape());
this.runRs423Task = scheduler.newTask(() => this.runRs423());
}
// MC6850: the transmit interrupt is selected by CR6:CR5 = 01 and follows
// TDRE, which a high CTS inhibits.
txIrq() {
return (this.cr & 0x60) === 0x20 && (this.sr & 0x0a) === 0x02;
}
updateIrq() {
if (this.sr & this.cr & 0x80 || this.txIrq()) {
this.cpu.interrupt |= 0x04;
} else {
this.cpu.interrupt &= ~0x04;
}
}
reset() {
// TODO(#1064) testing on a real beeb seems to suggest that reset also
// clears CR bits (i.e. things stop working until CR is rewritten
// with sane value). This disagrees with the datasheet.
// CTS and DTD are based on external inputs so leave them alone.
this.sr &= 0x08 | 0x04;
// Reset clears the transmit register so raise the empty bit.
this.sr |= 0x02;
this.hadDcdHigh = false;
this.updateIrq();
}
tone(freq) {
if (!freq) this.toneGen.mute();
else this.toneGen.tone(freq);
}
setMotor(on) {
if (on && !this.motorOn) {
this.runTape();
this.relayNoise.motorOn();
} else if (!on && this.motorOn) {
this.stopRunning();
this.relayNoise.motorOff();
}
this.motorOn = on;
}
/** Whether the machine and the recorder between them ask for the tape to move. */
get shouldRun() {
return this.motorOn && this.playPressed;
}
/** Whether the tape is moving: asked to, and not run out. */
get tapeRunning() {
return this.shouldRun && this.runTapeTask.scheduled();
}
/** PLAY on the recorder: down by default, so the relay alone runs the tape as it always has. */
pressPlay() {
Iif (this.playPressed) return;
this.playPressed = true;
if (this.motorOn) this.runTape();
}
/** STOP on the recorder: the tape stays put however the relay is set, until PLAY is pressed again. */
pressStop() {
Iif (!this.playPressed) return;
this.playPressed = false;
if (this.motorOn) this.stopRunning();
}
stopRunning() {
this.toneGen.mute();
this.runTapeTask.cancel();
this.setTapeCarrier(false);
}
read(addr) {
Iif (addr & 1) {
this.sr &= ~0xa1;
this.hadDcdHigh = false;
this.updateIrq();
return this.dr;
} else {
let result = (this.sr & 0x7f) | (this.sr & this.cr & 0x80);
if (this.txIrq()) result |= 0x80;
// MC6850: "A low CTS indicates that there is a Clear-to-Send
// from the modem. In the high state, the Transmit Data Register
// Empty bit is inhibited".
Iif (result & 0x08) {
result &= ~0x02;
}
// MC6850: "It remains high after the DCD input is returned low
// until cleared by first reading the Status Register and then
// the Data Register".
// Testing on a real machine shows that only the Data Register
// read matters, and clears the "saw DCD high" condition.
Iif (this.hadDcdHigh) {
result |= 0x04;
}
return result;
}
}
write(addr, val) {
if (addr & 1) {
this.sr &= ~0x02;
this.txCompleteTask.reschedule(this.serialTransmitCyclesPerByte);
this.updateIrq();
Iif (this.rs423Selected && this.rs423Handler) this.rs423Handler.onTransmit(val);
} else {
Iif ((val & 0x03) === 0x03) {
// According to the 6850 docs writing 3 here doesn't affect any CR bits, but
// just resets the device.
this.reset();
} else {
this.cr = val;
this.setSerialReceive(this.serialReceiveRate);
this.setSerialTransmit(this.serialTransmitRate);
this.updateIrq();
}
}
}
selectRs423(selected) {
this.rs423Selected = !!selected;
if (this.rs423Selected) {
// When a handler is present it acts as the remote device and is
// always ready to receive, so CTS is low (active = clear to send).
// Without a handler there is nothing on the other end and CTS
// stays high, which inhibits TDRE so the OS output buffer drains
// silently rather than hanging.
if (this.rs423Handler) {
this.sr &= ~0x08; // CTS low — clear to send
} else {
this.sr |= 0x08; // CTS high — not connected
}
} else {
// Cassette selected — CTS is always low (clear to send).
this.sr &= ~0x08;
}
this.dcdLineUpdated();
this.runRs423Task.ensureScheduled(this.rs423Selected, this.serialReceiveCyclesPerByte);
}
/**
* Attach or detach an RS-423 peripheral handler at runtime.
* The handler must implement onTransmit(byte) and tryReceive(rts).
* If RS-423 is already selected, the CTS line is updated immediately.
*/
setRs423Handler(handler) {
this.rs423Handler = handler;
Iif (this.rs423Selected) this.selectRs423(true);
}
dcdLineUpdated() {
// AUG: "It will always be low when the RS423 interface is selected".
const level = this.rs423Selected ? false : this.tapeDcdLineLevel;
Iif (level && !(this.sr & 0x04)) {
// DCD interrupts on low -> high level change.
this.sr |= 0x84;
this.hadDcdHigh = true;
} else Iif (!level && this.sr & 0x04) {
this.sr &= ~0x04;
}
this.updateIrq();
}
setTapeCarrier(level) {
if (!level) {
this.tapeCarrierCount = 0;
this.tapeDcdLineLevel = false;
} else E{
this.tapeCarrierCount++;
// The tape hardware doesn't raise DCD until the carrier tone
// has persisted for a while. The BBC service manual opines,
// "The DCD flag in the 6850 should change 0.1 to 0.4 seconds
// after a continuous tone appears".
// Star Drifter doesn't load without this.
// We use 0.174s, measured on an issue 3 model B.
// Testing on real hardware, DCD is blipped, it lowers about
// 210us after it raises, even though the carrier tone
// may be continuing.
this.tapeDcdLineLevel = this.tapeCarrierCount === 209;
}
this.dcdLineUpdated();
}
receive(byte) {
byte |= 0;
if (this.sr & 0x01) {
// Overrun.
// TODO(#1064) this doesn't match the datasheet:
// "The Overrun does not occur in the Status Register until the
// valid character prior to Overrun has been read."
this.sr |= 0xa0;
this.noteOverrun();
} else {
// If bit 7 contains parity, mask it off.
this.dr = byte & (this.cr & 0x10 ? 0xff : 0x7f);
this.sr |= 0x81;
}
this.updateIrq();
}
noteOverrun() {
if (this.saidOverrun) return;
this.saidOverrun = true;
this.dispatchEvent(
new CustomEvent("notice", {
detail: {
message:
"A serial byte arrived before the previous one was read, so it was lost. " +
"A tape load that stops with Data? is usually this.",
title: "Serial",
quietKey: "quietSerialOverrun",
},
}),
);
}
snapshotState() {
const scheduler = this.txCompleteTask.scheduler;
return {
sr: this.sr,
cr: this.cr,
dr: this.dr,
rs423Selected: this.rs423Selected,
motorOn: this.motorOn,
playPressed: this.playPressed,
tapeCarrierCount: this.tapeCarrierCount,
tapeDcdLineLevel: this.tapeDcdLineLevel,
hadDcdHigh: this.hadDcdHigh,
serialReceiveRate: this.serialReceiveRate,
serialTransmitRate: this.serialTransmitRate,
txCompleteTaskOffset: this.txCompleteTask.scheduled()
? this.txCompleteTask.expireEpoch - scheduler.epoch
: null,
runTapeTaskOffset: this.runTapeTask.scheduled() ? this.runTapeTask.expireEpoch - scheduler.epoch : null,
runRs423TaskOffset: this.runRs423Task.scheduled() ? this.runRs423Task.expireEpoch - scheduler.epoch : null,
};
}
restoreState(state) {
this.sr = state.sr;
this.cr = state.cr;
this.dr = state.dr;
this.rs423Selected = state.rs423Selected;
this.motorOn = state.motorOn;
// Snapshots predating the recorder's PLAY latch were taken with it down.
this.playPressed = state.playPressed ?? true;
this.tapeCarrierCount = state.tapeCarrierCount;
this.tapeDcdLineLevel = state.tapeDcdLineLevel;
this.hadDcdHigh = state.hadDcdHigh;
// The byte times are derived from the rates and the restored word format, not saved.
this.setSerialReceive(state.serialReceiveRate);
// Snapshots predating transmit timing have no saved rate.
this.setSerialTransmit(state.serialTransmitRate ?? 19200);
this.updateIrq();
this.txCompleteTask.cancel();
this.runTapeTask.cancel();
this.runRs423Task.cancel();
if (state.txCompleteTaskOffset !== null) this.txCompleteTask.schedule(state.txCompleteTaskOffset);
Iif (state.runTapeTaskOffset !== null) this.runTapeTask.schedule(state.runTapeTaskOffset);
Iif (state.runRs423TaskOffset !== null) this.runRs423Task.schedule(state.runRs423TaskOffset);
}
/** A tape put in, or taken out, while the tape is meant to be running starts or stops at once. */
setTape(tape) {
this.tape = tape;
if (!this.shouldRun) return;
if (tape) this.runTape();
else this.stopRunning();
}
rewindTape() {
Iif (!this.tape) return;
console.log("rewinding tape");
this.tape.rewind();
// A tape that had run out stopped being polled; rewound, it has something to play again.
Eif (this.shouldRun) this.runTape();
}
// Byte times are held in CPU cycles because that's what the scheduler counts.
secondsToCycles(sec) {
return Math.floor(this.cpu.model.cyclesPerSecond * sec) | 0;
}
numBitsPerByte() {
const wordLength = this.cr & 0x10 ? 8 : 7;
let stopBits, parityBits;
switch ((this.cr >>> 2) & 7) {
case 0:
stopBits = 2;
parityBits = 1;
break;
case 1:
stopBits = 2;
parityBits = 1;
break;
case 2:
stopBits = 1;
parityBits = 1;
break;
case 3:
stopBits = 1;
parityBits = 1;
break;
case 4:
stopBits = 2;
parityBits = 0;
break;
case 5:
stopBits = 1;
parityBits = 0;
break;
case 6:
stopBits = 1;
parityBits = 1;
break;
case 7:
stopBits = 1;
parityBits = 1;
break;
}
const startBits = 1;
return startBits + wordLength + stopBits + parityBits;
}
rts() {
// True iff CR6 = 0 or CR5 and CR6 are both 1.
return (this.cr & 0x40) === 0 || (this.cr & 0x60) === 0x60;
}
setSerialReceive(rate) {
this.serialReceiveRate = rate;
this.serialReceiveCyclesPerByte = this.secondsToCycles(this.numBitsPerByte() / rate);
}
setSerialTransmit(rate) {
this.serialTransmitRate = rate;
this.serialTransmitCyclesPerByte = this.secondsToCycles(this.numBitsPerByte() / rate);
}
/** Polls the tape and books the next poll; a tape that has run out is left alone. */
runTape() {
if (!this.tape || !this.playPressed) return;
const delay = this.tape.poll(this);
if (delay === undefined) this.stopRunning();
else this.runTapeTask.reschedule(delay);
}
runRs423() {
if (!this.rs423Handler) return;
const rcv = this.rs423Handler.tryReceive(this.rts());
if (rcv >= 0) this.receive(rcv);
this.runRs423Task.reschedule(this.serialReceiveCyclesPerByte);
}
}
|