-
Notifications
You must be signed in to change notification settings - Fork 0
/
UART8250.cpp
executable file
·181 lines (170 loc) · 3.49 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
172
173
174
175
176
177
178
179
180
181
//
// UART8250.cpp
// MIPS_Emulator
//
// Created by Matt on 1/8/16.
// Copyright © 2016 Matt. All rights reserved.
//
#include "UART8250.h"
// Default Constructor
UART8250::UART8250() {
// UART Addresses
addresses.push_back(UART_BASE);
addresses.push_back(UART_BASE+7);
addresses.push_back(UART_BASEPHYS);
addresses.push_back(UART_BASEPHYS+7);
};
UART8250::~UART8250() {
}
// Device initializer
void UART8250::initDevice() {
THR = 0x0;
RBR = 0x0;
DLL = 0x0;
IER = 0x0;
DLH = 0x0;
IIR = 0x0;
FCR = 0x0;
LCR = 0x0;
MCR = 0x0;
LSR = 0x60;
MSR = 0x0;
SR = 0x0;
}
std::vector<uint32_t> UART8250::getAddresses() {
return addresses;
}
inline bool UART8250::DLABSet() {
return (LCR & 0x40) > 0;
}
uint8_t UART8250::readByte(uint32_t address) {
address &= 0xF;
address -= addrOffset;
switch (address) {
case 0: {
if (DLABSet()) {
return DLL;
}
else {
if (!sendBuffer.isEmpty()) {
RBR = sendBuffer.pop();
}
// Clear Data Ready bit
if (sendBuffer.isEmpty()) {
LSR &= ~0x1;
}
return RBR;
}
}
case 1: {
if (DLABSet()) {
return DLH;
}
else {
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 SR;
}
default: {
throw std::runtime_error("Memory tried to read from invalid UART address");
}
}
}
void UART8250::storeByte(uint32_t address, uint8_t value) {
address &= 0xF;
address -= addrOffset;
switch (address) {
case 0: {
if (DLABSet()) {
DLL = value;
}
else {
THR = value;
receiveBuffer.push(THR);
//LSR &= ~0x40;
}
break;
}
case 1: {
if (DLABSet()) {
DLH = value;
}
else {
IER = value;
}
break;
}
case 2: {
FCR = value;
break;
}
case 3: {
LCR = value;
break;
}
case 4: {
MCR = value;
break;
}
case 5: {
break;
}
case 6: {
break;
}
case 7: {
SR = value;
break;
}
default: {
throw std::runtime_error("Memory tried to write to invalid UART address");
}
}
}
// 01000000
// 01100000
// Method to receive character from console
void UART8250::sendChar(char ch) {
// Save to buffer
sendBuffer.push(ch);
// Signal that there is data available
LSR |= 0x1;
}
// Method to send a char to stdout
bool UART8250::getChar(char* ch) {
if (!receiveBuffer.isEmpty()) {
*ch = receiveBuffer.pop();
LSR |= 0x40;
return true;
}
else {
return false;
}
/*
if ((LSR & 0x40) == 0) {
*ch = THR;
LSR |= 0x40;
return true;
}
else {
return false;
}
*/
}