-
Notifications
You must be signed in to change notification settings - Fork 5
/
serial.js
237 lines (223 loc) · 6.68 KB
/
serial.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
"use strict";
// http://www.lammertbies.nl/comm/info/serial-uart.html
// TODO: fixes from jsmodem Xscale => standard PC UART (16550A?)
function SerialPort(pc_emulator, address, set_irq_func, emulname) {
this.divider = 0;
this.rbr = 0;
this.ier = 0;
this.iir = 0x01;
// DLAB=0
this.lcr = 0;
this.mcr = 0;
// Line status register
// '0b1100000'
/*
0 Data available = 1
1 Overrun error = 2
2 Parity error = 4
3 Framing error = 8
4 Break signal received = 0x10
5 THR is empty = 0x20
6 THR is empty, and line is idle = 0x40
7 Errornous data in FIFO = 0x80
*/
// "THR is empty" and "THR is empty, and line is idle"
this.lsr = 0x40 | 0x20;
this.name = emulname;
/*
0 change in Clear to send
1 change in Data set ready
2 trailing edge Ring indicator
3 change in Carrier detect
4 Clear to send = 0x10
5 Data set ready = 0x20
6 Ring indicator = 0x40
7 Carrier detect = 0x80
*/
// set Carrier-detect
this.msr = 0x80 | 0x10;
this.scr = 0;
this.fcr = 0;
this.set_irq_func = set_irq_func;
var me = this;
this.write_func = function (data) {
me.log(emulname + 'writes to unbound COM port: ' + data);
};
this.tx_fifo = ""; // guest -> wire
this.rx_fifo = ""; // wire -> guest
this.baseaddr = address;
pc_emulator.register_ioport_write(address, 8, 1, this.ioport_write.bind(this));
pc_emulator.register_ioport_read(address, 8, 1, this.ioport_read.bind(this));
}
SerialPort.prototype.log = function () {
};
SerialPort.prototype.update_irq = function () {
if ((this.lsr & 0x01) && (this.ier & 0x01)) {
this.iir = 0x04;
} else if ((this.lsr & 0x20) && (this.ier & 0x02)) {
this.iir = 0x02;
} else {
this.iir = 0x01;
}
if (this.iir != 0x01) {
this.set_irq_func(1);
} else {
this.set_irq_func(0);
}
};
SerialPort.prototype.write_tx_fifo = function () {
if (this.tx_fifo != "") {
this.write_func(this.tx_fifo);
this.tx_fifo = "";
this.lsr |= 0x20;
this.lsr |= 0x40;
this.update_irq();
}
};
SerialPort.prototype.ioport_write = function (address, byte_value) {
var DLAB = 0x80;
switch (address & 7) {
//default:
case 0:
if (this.lcr & DLAB) {
// DLL divisor latch LSB
this.divider = (this.divider & 0xff00) | byte_value;
break;
}
//write ti BASE+0, DLAB=0
// THR transmitter holding
if (this.fcr & 0x01) {
// if FIFO enabled
this.tx_fifo += String.fromCharCode(byte_value);
this.lsr &= ~0x20;
this.update_irq();
// real buffering affect visual sppeed. just tun it off via HACK :)
if (true || this.tx_fifo.length >= 16) {
this.write_tx_fifo();
}
} else {
// if FIFO disabled
this.lsr &= ~0x20;
this.update_irq();
this.write_func(String.fromCharCode(byte_value));
this.lsr |= 0x20;
this.lsr |= 0x40;
this.update_irq();
}
break;
case 1:
if (this.lcr & DLAB) {
// DLM divisor latch MSB
this.divider = (this.divider & 0x00ff) | (byte_value << 8);
break;
}
// IER interrupt enable
this.ier = byte_value;
this.update_irq();
break;
case 2:
// FCR FIFO control
if ((this.fcr ^ byte_value) & 0x01) {
// if fifo state changed, set "Clear receive FIFO" and "Clear transmit FIFO" also.
byte_value |= 0x04 | 0x02;
}
if (byte_value & 0x04) {
this.tx_fifo = "";
}
if (byte_value & 0x02) {
this.rx_fifo = "";
}
this.fcr = byte_value & 0x01;
break;
case 3:
// LCR line control
this.lcr = byte_value;
break;
case 4:
// MCR modem control
this.mcr = byte_value;
break;
case 5:
this.log('Factory UART test is not implemented');
break;
case 6:
this.log('Unknown UART write operation BASE+6');
break;
case 7:
// SCR scratch
this.scr = byte_value;
break;
}
};
SerialPort.prototype.ioport_read = function (port_offset) {
var returned_byte_value, DLAB = 0x80;
port_offset &= 7;
switch (port_offset) {
//default:
case 0:
if (this.lcr & DLAB) {
returned_byte_value = this.divider & 0xff;
} else {
returned_byte_value = this.rbr;
this.lsr &= ~(0x01 | 0x10);
this.update_irq();
this.send_char_from_fifo();
}
break;
case 1:
if (this.lcr & DLAB) {
// DLM divisor latch MSB
returned_byte_value = (this.divider >> 8) & 0xff;
} else {
// IER interrupt enable
returned_byte_value = this.ier;
}
break;
case 2:
// IIR interrupt identification
returned_byte_value = this.iir;
if (this.fcr & 0x01) {
// if FIFO enabled, mark in iir that FIFO enabled
returned_byte_value |= 0xc0;
}
break;
case 3:
returned_byte_value = this.lcr;
break;
case 4:
returned_byte_value = this.mcr;
break;
case 5:
returned_byte_value = this.lsr;
break;
case 6:
returned_byte_value = this.msr;
break;
case 7:
returned_byte_value = this.scr;
break;
}
return returned_byte_value;
};
SerialPort.prototype.send_break = function () {
this.rbr = 0;
this.lsr |= 0x10 | 0x01;
this.update_irq();
};
SerialPort.prototype.send_char = function (byte_value) {
this.rbr = byte_value;
this.lsr |= 0x01;
this.update_irq();
};
SerialPort.prototype.send_char_from_fifo = function () {
var rx_fifo = this.rx_fifo;
if (rx_fifo != "" && !(this.lsr & 0x01)) {
this.send_char(rx_fifo.charCodeAt(0));
this.rx_fifo = rx_fifo.substr(1, rx_fifo.length - 1);
}
};
SerialPort.prototype.send_chars = function (str) {
this.rx_fifo += str;
this.send_char_from_fifo();
};
self.SerialPort = SerialPort;