Spaces:
Runtime error
Runtime error
File size: 11,794 Bytes
8df6da4 |
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 |
import { LOG_SERIAL } from "./const.js";
import { h } from "./lib.js";
import { dbg_log } from "./log.js";
// For Types Only
import { CPU } from "./cpu.js";
import { BusConnector } from "./bus.js";
/*
* Serial ports
* http://wiki.osdev.org/UART
* https://github.com/s-macke/jor1k/blob/master/js/worker/dev/uart.js
* https://www.freebsd.org/doc/en/articles/serial-uart/
*/
const DLAB = 0x80;
const UART_IER_MSI = 0x08; /* Modem Status Changed int. */
const UART_IER_THRI = 0x02; /* Enable Transmitter holding register int. */
const UART_IER_RDI = 0x01; /* Enable receiver data interrupt */
const UART_IIR_MSI = 0x00; /* Modem status interrupt (Low priority) */
const UART_IIR_NO_INT = 0x01;
const UART_IIR_THRI = 0x02; /* Transmitter holding register empty */
const UART_IIR_RDI = 0x04; /* Receiver data interrupt */
const UART_IIR_RLSI = 0x06; /* Receiver line status interrupt (High p.) */
const UART_IIR_CTI = 0x0c; /* Character timeout */
const UART_LSR_DATA_READY = 0x1; // data available
const UART_LSR_TX_EMPTY = 0x20; // TX (THR) buffer is empty
const UART_LSR_TRANSMITTER_EMPTY = 0x40; // TX empty and line is idle
// Modem status register
const UART_MSR_DCD = 0x7; // Data Carrier Detect
const UART_MSR_RI = 0x6; // Ring Indicator
const UART_MSR_DSR = 0x5; // Data Set Ready
const UART_MSR_CTS = 0x4; // Clear To Send
// Delta bits
const UART_MSR_DDCD = 0x3; // Delta DCD
const UART_MSR_TERI = 0x2; // Trailing Edge RI
const UART_MSR_DDSR = 0x1; // Delta DSR
const UART_MSR_DCTS = 0x0; // Delta CTS
/**
* @constructor
* @param {CPU} cpu
* @param {number} port
* @param {BusConnector} bus
*/
export function UART(cpu, port, bus)
{
/** @const @type {BusConnector} */
this.bus = bus;
/** @const @type {CPU} */
this.cpu = cpu;
this.ints = 1 << UART_IIR_THRI;
this.baud_rate = 0;
this.line_control = 0;
// line status register
this.lsr = UART_LSR_TRANSMITTER_EMPTY | UART_LSR_TX_EMPTY;
this.fifo_control = 0;
// interrupts enable
this.ier = 0;
// interrupt identification register
this.iir = UART_IIR_NO_INT;
this.modem_control = 0;
this.modem_status = 0;
this.scratch_register = 0;
this.irq = 0;
this.input = [];
this.current_line = "";
switch(port)
{
case 0x3F8:
this.com = 0;
this.irq = 4;
break;
case 0x2F8:
this.com = 1;
this.irq = 3;
break;
case 0x3E8:
this.com = 2;
this.irq = 4;
break;
case 0x2E8:
this.com = 3;
this.irq = 3;
break;
default:
dbg_log("Invalid serial port: " + h(port), LOG_SERIAL);
this.com = 0;
this.irq = 4;
}
this.bus.register("serial" + this.com + "-input", function(data)
{
this.data_received(data);
}, this);
this.bus.register("serial" + this.com + "-modem-status-input", function(data)
{
this.set_modem_status(data);
}, this);
// Set individual modem status bits
this.bus.register("serial" + this.com + "-carrier-detect-input", function(data)
{
const status = data ?
this.modem_status | (1 << UART_MSR_DCD) | (1 << UART_MSR_DDCD) :
this.modem_status & ~(1 << UART_MSR_DCD) & ~(1 << UART_MSR_DDCD);
this.set_modem_status(status);
}, this);
this.bus.register("serial" + this.com + "-ring-indicator-input", function(data)
{
const status = data ?
this.modem_status | (1 << UART_MSR_RI) | (1 << UART_MSR_TERI) :
this.modem_status & ~(1 << UART_MSR_RI) & ~(1 << UART_MSR_TERI);
this.set_modem_status(status);
}, this);
this.bus.register("serial" + this.com + "-data-set-ready-input", function(data)
{
const status = data ?
this.modem_status | (1 << UART_MSR_DSR) | (1 << UART_MSR_DDSR) :
this.modem_status & ~(1 << UART_MSR_DSR) & ~(1 << UART_MSR_DDSR);
this.set_modem_status(status);
}, this);
this.bus.register("serial" + this.com + "-clear-to-send-input", function(data)
{
const status = data ?
this.modem_status | (1 << UART_MSR_CTS) | (1 << UART_MSR_DCTS) :
this.modem_status & ~(1 << UART_MSR_CTS) & ~(1 << UART_MSR_DCTS);
this.set_modem_status(status);
}, this);
var io = cpu.io;
io.register_write(port, this, function(out_byte)
{
this.write_data(out_byte);
}, function(out_word)
{
this.write_data(out_word & 0xFF);
this.write_data(out_word >> 8);
});
io.register_write(port | 1, this, function(out_byte)
{
if(this.line_control & DLAB)
{
this.baud_rate = this.baud_rate & 0xFF | out_byte << 8;
dbg_log("baud rate: " + h(this.baud_rate), LOG_SERIAL);
}
else
{
if((this.ier & UART_IIR_THRI) === 0 && (out_byte & UART_IIR_THRI))
{
// re-throw THRI if it was masked
this.ThrowInterrupt(UART_IIR_THRI);
}
this.ier = out_byte & 0xF;
dbg_log("interrupt enable: " + h(out_byte), LOG_SERIAL);
this.CheckInterrupt();
}
});
io.register_read(port, this, function()
{
if(this.line_control & DLAB)
{
return this.baud_rate & 0xFF;
}
else
{
let data = 0;
if(this.input.length === 0)
{
dbg_log("Read input empty", LOG_SERIAL);
}
else
{
data = this.input.shift();
dbg_log("Read input: " + h(data), LOG_SERIAL);
}
if(this.input.length === 0)
{
this.lsr &= ~UART_LSR_DATA_READY;
this.ClearInterrupt(UART_IIR_CTI);
this.ClearInterrupt(UART_IIR_RDI);
}
return data;
}
});
io.register_read(port | 1, this, function()
{
if(this.line_control & DLAB)
{
return this.baud_rate >> 8;
}
else
{
return this.ier & 0xF;
}
});
io.register_read(port | 2, this, function()
{
var ret = this.iir & 0xF;
dbg_log("read interrupt identification: " + h(this.iir), LOG_SERIAL);
if(this.iir === UART_IIR_THRI) {
this.ClearInterrupt(UART_IIR_THRI);
}
if(this.fifo_control & 1) ret |= 0xC0;
return ret;
});
io.register_write(port | 2, this, function(out_byte)
{
dbg_log("fifo control: " + h(out_byte), LOG_SERIAL);
this.fifo_control = out_byte;
});
io.register_read(port | 3, this, function()
{
dbg_log("read line control: " + h(this.line_control), LOG_SERIAL);
return this.line_control;
});
io.register_write(port | 3, this, function(out_byte)
{
dbg_log("line control: " + h(out_byte), LOG_SERIAL);
this.line_control = out_byte;
});
io.register_read(port | 4, this, function()
{
return this.modem_control;
});
io.register_write(port | 4, this, function(out_byte)
{
dbg_log("modem control: " + h(out_byte), LOG_SERIAL);
this.modem_control = out_byte;
});
io.register_read(port | 5, this, function()
{
dbg_log("read line status: " + h(this.lsr), LOG_SERIAL);
return this.lsr;
});
io.register_write(port | 5, this, function(out_byte)
{
dbg_log("Factory test write", LOG_SERIAL);
});
io.register_read(port | 6, this, function()
{
dbg_log("read modem status: " + h(this.modem_status), LOG_SERIAL);
// Clear delta bits
this.modem_status &= 0xF0;
return this.modem_status;
});
io.register_write(port | 6, this, function(out_byte)
{
dbg_log("write modem status: " + h(out_byte), LOG_SERIAL);
this.set_modem_status(out_byte);
});
io.register_read(port | 7, this, function()
{
return this.scratch_register;
});
io.register_write(port | 7, this, function(out_byte)
{
this.scratch_register = out_byte;
});
}
UART.prototype.get_state = function()
{
var state = [];
state[0] = this.ints;
state[1] = this.baud_rate;
state[2] = this.line_control;
state[3] = this.lsr;
state[4] = this.fifo_control;
state[5] = this.ier;
state[6] = this.iir;
state[7] = this.modem_control;
state[8] = this.modem_status;
state[9] = this.scratch_register;
state[10] = this.irq;
return state;
};
UART.prototype.set_state = function(state)
{
this.ints = state[0];
this.baud_rate = state[1];
this.line_control = state[2];
this.lsr = state[3];
this.fifo_control = state[4];
this.ier = state[5];
this.iir = state[6];
this.modem_control = state[7];
this.modem_status = state[8];
this.scratch_register = state[9];
this.irq = state[10];
};
UART.prototype.CheckInterrupt = function() {
if((this.ints & (1 << UART_IIR_CTI)) && (this.ier & UART_IER_RDI)) {
this.iir = UART_IIR_CTI;
this.cpu.device_raise_irq(this.irq);
} else
if((this.ints & (1 << UART_IIR_RDI)) && (this.ier & UART_IER_RDI)) {
this.iir = UART_IIR_RDI;
this.cpu.device_raise_irq(this.irq);
} else
if((this.ints & (1 << UART_IIR_THRI)) && (this.ier & UART_IER_THRI)) {
this.iir = UART_IIR_THRI;
this.cpu.device_raise_irq(this.irq);
} else
if((this.ints & (1 << UART_IIR_MSI)) && (this.ier & UART_IER_MSI)) {
this.iir = UART_IIR_MSI;
this.cpu.device_raise_irq(this.irq);
} else {
this.iir = UART_IIR_NO_INT;
this.cpu.device_lower_irq(this.irq);
}
};
UART.prototype.ThrowInterrupt = function(line) {
this.ints |= (1 << line);
this.CheckInterrupt();
};
UART.prototype.ClearInterrupt = function(line) {
this.ints &= ~(1 << line);
this.CheckInterrupt();
};
/**
* @param {number} data
*/
UART.prototype.data_received = function(data)
{
dbg_log("input: " + h(data), LOG_SERIAL);
this.input.push(data);
this.lsr |= UART_LSR_DATA_READY;
if(this.fifo_control & 1)
{
this.ThrowInterrupt(UART_IIR_CTI);
}
else
{
this.ThrowInterrupt(UART_IIR_RDI);
}
};
UART.prototype.write_data = function(out_byte)
{
if(this.line_control & DLAB)
{
this.baud_rate = this.baud_rate & ~0xFF | out_byte;
return;
}
dbg_log("data: " + h(out_byte), LOG_SERIAL);
this.ThrowInterrupt(UART_IIR_THRI);
this.bus.send("serial" + this.com + "-output-byte", out_byte);
if(DEBUG)
{
var char = String.fromCharCode(out_byte);
this.current_line += char;
if(char === "\n")
{
const line = this.current_line.trimRight().replace(/[\x00-\x08\x0b-\x1f\x7f\x80-\xff]/g, "");
dbg_log("SERIAL: " + line);
this.current_line = "";
}
}
};
UART.prototype.set_modem_status = function(status)
{
dbg_log("modem status: " + h(status), LOG_SERIAL);
const prev_delta_bits = this.modem_status & 0x0F;
// compare the bits that have changed and shift them into the delta bits
let delta = (this.modem_status ^ status) >> 4;
// The delta should stay set if they were previously set
delta |= prev_delta_bits;
// update the current modem status
this.modem_status = status;
// update the delta bits based on the changes and previous
// values, but also leave the delta bits set if they were
// passed in as part of the status
this.modem_status |= delta;
};
|