2014-12-14 16:26:09 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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/
|
2014-01-08 03:14:33 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** @const */
|
|
|
|
var DLAB = 0x80;
|
|
|
|
|
2014-12-14 16:26:09 +01:00
|
|
|
/** @const */ var UART_IER_THRI = 0x02; /* Enable Transmitter holding register int. */
|
|
|
|
/** @const */ var UART_IER_RDI = 0x01; /* Enable receiver data interrupt */
|
|
|
|
|
|
|
|
/** @const */var UART_IIR_MSI = 0x00; /* Modem status interrupt (Low priority) */
|
|
|
|
/** @const */var UART_IIR_NO_INT = 0x01;
|
|
|
|
/** @const */var UART_IIR_THRI = 0x02; /* Transmitter holding register empty */
|
|
|
|
/** @const */var UART_IIR_RDI = 0x04; /* Receiver data interrupt */
|
|
|
|
/** @const */var UART_IIR_RLSI = 0x06; /* Receiver line status interrupt (High p.) */
|
|
|
|
/** @const */var UART_IIR_CTI = 0x0c; /* Character timeout */
|
|
|
|
|
|
|
|
|
2014-01-08 03:14:33 +01:00
|
|
|
/**
|
2013-11-06 01:12:55 +01:00
|
|
|
* @constructor
|
|
|
|
*/
|
2014-12-25 02:32:18 +01:00
|
|
|
function UART(cpu, port, bus)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
2014-12-25 02:32:18 +01:00
|
|
|
this.bus = bus;
|
|
|
|
|
2014-10-04 20:50:31 +02:00
|
|
|
this.pic = cpu.devices.pic;
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-12-14 16:26:09 +01:00
|
|
|
this.interrupts = 0;
|
|
|
|
|
2014-10-04 20:50:31 +02:00
|
|
|
this.baud_rate = 0;
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-10-04 20:50:31 +02:00
|
|
|
this.line_control = 0;
|
|
|
|
this.line_status = 0;
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-10-04 20:50:31 +02:00
|
|
|
this.fifo_control = 0;
|
2014-12-14 16:26:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
// interrupts enable
|
|
|
|
this.ier = 0;
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-10-04 20:50:31 +02:00
|
|
|
// interrupt identification register
|
|
|
|
this.iir = 1;
|
2014-06-02 22:31:25 +02:00
|
|
|
|
2014-10-04 20:50:31 +02:00
|
|
|
this.modem_control = 0;
|
|
|
|
this.modem_status = 0;
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-10-04 20:50:31 +02:00
|
|
|
this.scratch_register = 0;
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-10-04 20:50:31 +02:00
|
|
|
this.irq = 0;
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-10-04 20:50:31 +02:00
|
|
|
this.input = new ByteQueue(4096);
|
2014-01-08 03:14:33 +01:00
|
|
|
|
|
|
|
if(port === 0x3E8 || port === 0x3F8)
|
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
this.irq = 4;
|
2014-01-08 03:14:33 +01:00
|
|
|
}
|
|
|
|
else if(port === 0x3E8 || port === 0x3E8)
|
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
this.irq = 3;
|
2014-01-08 03:14:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dbg_log("Invalid port: " + h(port), LOG_SERIAL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-25 02:32:18 +01:00
|
|
|
this.bus.register("serial0-input", function(data)
|
|
|
|
{
|
|
|
|
this.data_received(data);
|
|
|
|
}, this);
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-10-04 20:50:31 +02:00
|
|
|
var io = cpu.io;
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-10-21 21:51:42 +02:00
|
|
|
io.register_write(port, this, function(out_byte)
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
if(this.line_control & DLAB)
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
this.baud_rate = this.baud_rate & ~0xFF | out_byte;
|
2014-01-08 03:14:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-14 16:26:09 +01:00
|
|
|
dbg_log("data: " + h(out_byte), LOG_SERIAL);
|
|
|
|
|
|
|
|
if(this.ier & UART_IER_THRI)
|
|
|
|
{
|
|
|
|
this.push_irq();
|
|
|
|
}
|
2014-01-08 03:14:33 +01:00
|
|
|
|
|
|
|
if(out_byte === 0xFF)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-25 02:32:18 +01:00
|
|
|
this.bus.send("serial0-output", String.fromCharCode(out_byte));
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-10-21 21:51:42 +02:00
|
|
|
io.register_write(port | 1, this, function(out_byte)
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
if(this.line_control & DLAB)
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
this.baud_rate = this.baud_rate & 0xFF | out_byte << 8;
|
|
|
|
dbg_log("baud rate: " + h(this.baud_rate), LOG_SERIAL);
|
2014-01-08 03:14:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-14 16:26:09 +01:00
|
|
|
this.ier = out_byte;
|
2014-01-08 03:14:33 +01:00
|
|
|
dbg_log("interrupt enable: " + h(out_byte), LOG_SERIAL);
|
|
|
|
}
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
2013-11-06 01:12:55 +01:00
|
|
|
|
2014-10-21 21:51:42 +02:00
|
|
|
io.register_read(port, this, function()
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
if(this.line_control & DLAB)
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
return this.baud_rate & 0xFF;
|
2014-01-08 03:14:33 +01:00
|
|
|
}
|
|
|
|
else
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
var data = this.input.shift();
|
2014-01-05 03:19:09 +01:00
|
|
|
|
2014-01-08 03:14:33 +01:00
|
|
|
if(data === -1)
|
|
|
|
{
|
2014-12-14 16:26:09 +01:00
|
|
|
dbg_log("Read input empty", LOG_SERIAL);
|
2014-01-08 03:14:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-14 16:26:09 +01:00
|
|
|
dbg_log("Read input: " + h(data), LOG_SERIAL);
|
2014-01-08 03:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-10-21 21:51:42 +02:00
|
|
|
io.register_read(port | 1, this, function()
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
if(this.line_control & DLAB)
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
return this.baud_rate >> 8;
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-14 16:26:09 +01:00
|
|
|
return this.ier;
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
2013-12-20 22:05:49 +01:00
|
|
|
|
2014-10-21 21:51:42 +02:00
|
|
|
io.register_read(port | 2, this, function()
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
2014-12-14 16:26:09 +01:00
|
|
|
var ret = this.iir & 0xF | 0xC0;
|
2014-10-04 20:50:31 +02:00
|
|
|
dbg_log("read interrupt identification: " + h(this.iir), LOG_SERIAL);
|
2014-12-14 16:26:09 +01:00
|
|
|
|
|
|
|
if(this.iir === UART_IIR_THRI)
|
|
|
|
{
|
|
|
|
this.clear_interrupt(UART_IIR_THRI);
|
|
|
|
}
|
|
|
|
else if(this.iir === UART_IIR_CTI)
|
|
|
|
{
|
|
|
|
this.clear_interrupt(UART_IIR_CTI);
|
|
|
|
}
|
2014-06-02 22:31:25 +02:00
|
|
|
|
|
|
|
return ret;
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
|
|
|
io.register_write(port | 2, this, function(out_byte)
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
|
|
|
dbg_log("fifo control: " + h(out_byte), LOG_SERIAL);
|
2014-10-04 20:50:31 +02:00
|
|
|
this.fifo_control = out_byte;
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-10-21 21:51:42 +02:00
|
|
|
io.register_read(port | 3, this, function()
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
dbg_log("read line control: " + h(this.line_control), LOG_SERIAL);
|
|
|
|
return this.line_control;
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
|
|
|
io.register_write(port | 3, this, function(out_byte)
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
|
|
|
dbg_log("line control: " + h(out_byte), LOG_SERIAL);
|
2014-10-04 20:50:31 +02:00
|
|
|
this.line_control = out_byte;
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
2014-01-08 03:14:33 +01:00
|
|
|
|
|
|
|
|
2014-10-21 21:51:42 +02:00
|
|
|
io.register_read(port | 4, this, function()
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
return this.modem_control;
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
|
|
|
io.register_write(port | 4, this, function(out_byte)
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
|
|
|
dbg_log("modem control: " + h(out_byte), LOG_SERIAL);
|
2014-10-04 20:50:31 +02:00
|
|
|
this.modem_control = out_byte;
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-10-21 21:51:42 +02:00
|
|
|
io.register_read(port | 5, this, function()
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
|
|
|
var line_status = 0;
|
|
|
|
|
2014-10-04 20:50:31 +02:00
|
|
|
if(this.input.length)
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
|
|
|
line_status |= 1;
|
|
|
|
}
|
|
|
|
|
2014-06-02 22:31:25 +02:00
|
|
|
line_status |= 0x20 | 0x40;
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-06-02 22:31:25 +02:00
|
|
|
dbg_log("read line status: " + h(line_status), LOG_SERIAL);
|
2014-01-08 03:14:33 +01:00
|
|
|
return line_status;
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
|
|
|
io.register_write(port | 5, this, function(out_byte)
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
2014-06-02 22:31:25 +02:00
|
|
|
dbg_log("Factory test write", LOG_SERIAL);
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-10-21 21:51:42 +02:00
|
|
|
io.register_read(port | 6, this, function()
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
dbg_log("read modem status: " + h(this.modem_status), LOG_SERIAL);
|
|
|
|
return this.modem_status;
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
|
|
|
io.register_write(port | 6, this, function(out_byte)
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
2014-06-02 22:31:25 +02:00
|
|
|
dbg_log("Unkown register write (base+6)", LOG_SERIAL);
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
2014-01-08 03:14:33 +01:00
|
|
|
|
2014-10-21 21:51:42 +02:00
|
|
|
io.register_read(port | 7, this, function()
|
2014-01-08 03:14:33 +01:00
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
return this.scratch_register;
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
|
|
|
io.register_write(port | 7, this, function(out_byte)
|
2013-12-20 22:05:49 +01:00
|
|
|
{
|
2014-10-04 20:50:31 +02:00
|
|
|
this.scratch_register = out_byte;
|
2014-10-21 21:51:42 +02:00
|
|
|
});
|
2014-12-25 02:32:18 +01:00
|
|
|
|
|
|
|
this._state_skip = [
|
|
|
|
"bus",
|
|
|
|
];
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
2014-10-04 20:50:31 +02:00
|
|
|
|
|
|
|
UART.prototype.push_irq = function()
|
|
|
|
{
|
2014-12-14 16:26:09 +01:00
|
|
|
dbg_log("Push irq", LOG_SERIAL);
|
2014-10-04 20:50:31 +02:00
|
|
|
this.pic.push_irq(this.irq);
|
|
|
|
};
|
|
|
|
|
2014-12-14 16:26:09 +01:00
|
|
|
UART.prototype.clear_interrupt = function(line)
|
|
|
|
{
|
|
|
|
this.interrupts &= ~(1 << line);
|
|
|
|
this.iir = UART_IIR_NO_INT;
|
|
|
|
|
|
|
|
if(line === this.iir)
|
|
|
|
{
|
|
|
|
this.next_interrupt();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
UART.prototype.next_interrupt = function()
|
|
|
|
{
|
|
|
|
if ((this.interrupts & (1 << UART_IIR_CTI)) && (this.ier & UART_IER_RDI)) {
|
|
|
|
//this.ThrowCTI();
|
|
|
|
}
|
|
|
|
else if ((this.interrupts & (1 << UART_IIR_THRI)) && (this.ier & UART_IER_THRI)) {
|
|
|
|
//this.ThrowTHRI();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.iir = UART_IIR_NO_INT;
|
|
|
|
//this.intdev.ClearInterrupt(0x2);
|
|
|
|
}
|
|
|
|
};
|
2014-12-21 22:57:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} data
|
|
|
|
*/
|
|
|
|
UART.prototype.data_received = function(data)
|
|
|
|
{
|
|
|
|
dbg_log("input: " + h(data), LOG_SERIAL);
|
|
|
|
this.input.push(data);
|
|
|
|
this.interrupts |= 1 << UART_IIR_CTI;
|
|
|
|
|
|
|
|
if(this.ier & UART_IER_RDI)
|
|
|
|
{
|
|
|
|
this.iir = UART_IIR_CTI;
|
|
|
|
this.push_irq();
|
|
|
|
}
|
|
|
|
};
|