forked from tenstorrent/whisper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUart8250.cpp
171 lines (147 loc) · 3.06 KB
/
Uart8250.cpp
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
#include <cassert>
#include <iostream>
#include <unistd.h>
#include <poll.h>
#include <termios.h>
#include "Uart8250.hpp"
using namespace WdRiscv;
Uart8250::Uart8250(uint64_t addr, uint64_t size)
: IoDevice(addr, size)
{
auto func = [this]() { this->monitorStdin(); };
stdinThread_ = std::thread(func);
struct termios term;
tcgetattr(fileno(stdin), &term);
cfmakeraw(&term);
tcsetattr(fileno(stdin), 0, &term);
}
Uart8250::~Uart8250()
{
terminate_ = true;
stdinThread_.join();
}
uint32_t
Uart8250::read(uint64_t addr)
{
uint64_t offset = (addr - address()) / 4;
bool dlab = lcr_ & 0x80;
if (dlab == 0)
{
switch (offset)
{
case 0:
{
std::lock_guard<std::mutex> lock(mutex_);
uint32_t res = byte_;
byte_ = 0;
lsr_ &= ~1; // Clear least sig bit
iir_ |= 1; // Set least sig bit indicating no interrupt.
setInterruptPending(false);
return res;
}
case 1: return ier_;
case 2: return iir_;
case 3: return lcr_;
case 4: return mcr_;
case 5: return lsr_;
case 6: return msr_;
case 7: return scr_;
}
}
else
{
switch (offset)
{
case 0: return dll_;
case 1: return dlm_;
}
}
assert(0);
return 0;
}
void
Uart8250::write(uint64_t addr, uint32_t value)
{
uint64_t offset = (addr - address()) / 4;
bool dlab = lcr_ & 0x80;
if (dlab == 0)
{
switch (offset)
{
case 0:
{
int c = static_cast<int>(value & 0xff);
if (c)
{
putchar(c);
fflush(stdout);
}
}
break;
case 1: ier_ = value; break;
case 2: fcr_ = value; break;
case 3: lcr_ = value; break;
case 4: mcr_ = value; break;
case 5:
case 6: break;
case 7: scr_ = value; break;
default:
std::cerr << "Uart writing addr 0x" << std::hex << addr << std::dec << '\n';
assert(0);
}
}
else
{
switch (offset)
{
case 0: dll_ = value; break;
case 1: dlm_ = value; break;
case 3: lcr_ = value; break;
case 5: psd_ = value; break;
default:
std::cerr << "Uart writing addr 0x" << std::hex << addr << std::dec << '\n';
assert(0);
}
}
}
void
Uart8250::monitorStdin()
{
struct pollfd inPollfd;
int fd = fileno(stdin); // stdin file descriptor
inPollfd.fd = fd;
inPollfd.events = POLLIN;
while (true)
{
if (terminate_)
return;
int code = poll(&inPollfd, 1, 20);
if (code == 0)
continue; // Timed out.
if (code == 1)
{
if ((inPollfd.revents & POLLIN) != 0)
{
std::lock_guard<std::mutex> lock(mutex_);
char c;
if (::read(fd, &c, sizeof(c)) != 1)
std::cerr << "Uart8250::monitorStdin: unexpected fail on read\n";
putchar(c);
fflush(stdout);
if (isatty(fd))
{
static char prev = 0;
// Force a stop if control-a x is seen.
if (prev == 1 and c == 'x')
throw std::runtime_error("Keyboard stop");
prev = c;
}
byte_ = c;
lsr_ |= 1; // Set least sig bit of line status.
iir_ &= ~1; // Clear bit 0 indicating interrupt is pending.
// setInterruptPending(true);
}
}
// TODO: handle error return codes from poll.
}
}