-
Notifications
You must be signed in to change notification settings - Fork 49
/
uart.c
225 lines (204 loc) · 5.57 KB
/
uart.c
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
#include <errno.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "device.h"
#include "riscv.h"
#include "riscv_private.h"
/*
* The control mode flag for keyboard.
*
* ICANON: Enable canonical mode.
* ECHO: Echo input characters.
* ISIG: When any of the characters INTR, QUIT,
* SUSP, or DSUSP are received, generate the
* corresponding signal.
*
* It is essential to re-enable ISIG upon exit.
* Otherwise, the default signal handler will
* not catch the signal. E.g., SIGINT generated by
* CTRL + c.
*
*/
#define TERMIOS_C_CFLAG (ICANON | ECHO | ISIG)
/* Emulate 8250 (plain, without loopback mode support) */
#define U8250_INT_THRE 1
static void reset_keyboard_input()
{
/* Re-enable echo, etc. on keyboard. */
struct termios term;
tcgetattr(0, &term);
term.c_lflag |= TERMIOS_C_CFLAG;
tcsetattr(0, TCSANOW, &term);
}
/* Asynchronous communication to capture all keyboard input for the VM. */
void capture_keyboard_input()
{
/* Hook exit, because we want to re-enable keyboard. */
atexit(reset_keyboard_input);
struct termios term;
tcgetattr(0, &term);
term.c_lflag &= ~TERMIOS_C_CFLAG; /* Disable echo as well */
tcsetattr(0, TCSANOW, &term);
}
void u8250_update_interrupts(u8250_state_t *uart)
{
/* Some interrupts are level-generated. */
/* TODO: does it also generate an LSR change interrupt? */
if (uart->in_ready)
uart->pending_ints |= 1;
else
uart->pending_ints &= ~1;
/* Prevent generating any disabled interrupts in the first place */
uart->pending_ints &= uart->ier;
/* Update current interrupt (higher bits -> more priority) */
if (uart->pending_ints)
uart->current_int = ilog2(uart->pending_ints);
}
void u8250_check_ready(u8250_state_t *uart)
{
if (uart->in_ready)
return;
struct pollfd pfd = {uart->in_fd, POLLIN, 0};
poll(&pfd, 1, 0);
if (pfd.revents & POLLIN)
uart->in_ready = true;
}
static void u8250_handle_out(u8250_state_t *uart, uint8_t value)
{
if (write(uart->out_fd, &value, 1) < 1)
fprintf(stderr, "failed to write UART output: %s\n", strerror(errno));
}
static uint8_t u8250_handle_in(u8250_state_t *uart)
{
uint8_t value = 0;
u8250_check_ready(uart);
if (!uart->in_ready)
return value;
if (read(uart->in_fd, &value, 1) < 0)
fprintf(stderr, "failed to read UART input: %s\n", strerror(errno));
uart->in_ready = false;
u8250_check_ready(uart);
if (value == 1) { /* start of heading (Ctrl-a) */
if (getchar() == 120) { /* keyboard x */
printf("\n"); /* end emulator with newline */
exit(0);
}
}
return value;
}
static void u8250_reg_read(u8250_state_t *uart, uint32_t addr, uint8_t *value)
{
switch (addr) {
case 0:
if (uart->lcr & (1 << 7)) { /* DLAB */
*value = uart->dll;
break;
}
*value = u8250_handle_in(uart);
break;
case 1:
if (uart->lcr & (1 << 7)) { /* DLAB */
*value = uart->dlh;
break;
}
*value = uart->ier;
break;
case 2:
*value = (uart->current_int << 1) | (uart->pending_ints ? 0 : 1);
if (uart->current_int == U8250_INT_THRE)
uart->pending_ints &= ~(1 << uart->current_int);
break;
case 3:
*value = uart->lcr;
break;
case 4:
*value = uart->mcr;
break;
case 5:
/* LSR = no error, TX done & ready */
*value = 0x60 | (uint8_t) uart->in_ready;
break;
case 6:
/* MSR = carrier detect, no ring, data ready, clear to send. */
*value = 0xb0;
break;
/* no scratch register, so we should be detected as a plain 8250. */
default:
*value = 0;
}
}
static void u8250_reg_write(u8250_state_t *uart, uint32_t addr, uint8_t value)
{
switch (addr) {
case 0:
if (uart->lcr & (1 << 7)) { /* DLAB */
uart->dll = value;
break;
}
u8250_handle_out(uart, value);
uart->pending_ints |= 1 << U8250_INT_THRE;
break;
case 1:
if (uart->lcr & (1 << 7)) { /* DLAB */
uart->dlh = value;
break;
}
uart->ier = value;
break;
case 3:
uart->lcr = value;
break;
case 4:
uart->mcr = value;
break;
}
}
void u8250_read(hart_t *vm,
u8250_state_t *uart,
uint32_t addr,
uint8_t width,
uint32_t *value)
{
uint8_t u8value;
switch (width) {
case RV_MEM_LBU:
u8250_reg_read(uart, addr, &u8value);
*value = (uint32_t) u8value;
break;
case RV_MEM_LB:
u8250_reg_read(uart, addr, &u8value);
*value = (uint32_t) (int8_t) u8value;
break;
case RV_MEM_LW:
case RV_MEM_LHU:
case RV_MEM_LH:
vm_set_exception(vm, RV_EXC_LOAD_MISALIGN, vm->exc_val);
return;
default:
vm_set_exception(vm, RV_EXC_ILLEGAL_INSN, 0);
return;
}
}
void u8250_write(hart_t *vm,
u8250_state_t *uart,
uint32_t addr,
uint8_t width,
uint32_t value)
{
switch (width) {
case RV_MEM_SB:
u8250_reg_write(uart, addr, value);
break;
case RV_MEM_SW:
case RV_MEM_SH:
vm_set_exception(vm, RV_EXC_STORE_MISALIGN, vm->exc_val);
return;
default:
vm_set_exception(vm, RV_EXC_ILLEGAL_INSN, 0);
return;
}
}