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
use core::fmt::{Display, Write, Error, Formatter};
use crate::sync::{Once, SpinLockIRQ};
use crate::io::Io;
use crate::i386::pio::Pio;
#[derive(Debug, Copy, Clone)]
pub struct ComPort(u16);
#[cfg(any(all(target_arch="x86", not(test)), doc))]
const COM1: ComPort = ComPort(0x3F8);
#[cfg(any(all(target_arch="x86", not(test)), doc))]
const COM2: ComPort = ComPort(0x2F8);
#[cfg(any(all(target_arch="x86", not(test)), doc))]
const COM3: ComPort = ComPort(0x3E8);
#[cfg(any(all(target_arch="x86", not(test)), doc))]
const COM4: ComPort = ComPort(0x2E8);
#[cfg(test)]
const COM1: ComPort = ComPort(0x7777);
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
#[repr(u8)]
#[derive(Debug, Copy, Clone)]
pub enum SerialColor {
Black = 0,
Red = 1,
Green = 2,
Yellow = 3,
Blue = 4,
Magenta = 5,
Cyan = 6,
LightGray = 7,
Default = 9,
DarkGray = 60,
LightRed = 61,
LightGreen = 62,
LightYellow = 63,
LightBlue = 64,
LightMagenta = 65,
LightCyan = 66,
White = 67,
}
#[derive(Debug, Copy, Clone)]
pub struct SerialAttributes {
fg: SerialColor,
bg: SerialColor,
}
impl SerialAttributes {
pub fn fg(fg: SerialColor) -> SerialAttributes {
SerialAttributes { fg, bg: SerialColor::Default }
}
pub fn fg_bg(fg: SerialColor, bg: SerialColor) -> SerialAttributes {
SerialAttributes { fg, bg }
}
pub fn default() -> SerialAttributes {
SerialAttributes { fg: SerialColor::Default, bg: SerialColor::Default }
}
}
impl Display for SerialAttributes {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
write!(f, "\x1B[{};{}m", self.fg as u8 + 30, self.bg as u8 + 40)
}
}
static G_SERIAL: Once<SpinLockIRQ<SerialInternal<Pio<u8>>>> = Once::new();
struct SerialInternal<T> {
data_port: T,
status_port: T
}
impl SerialInternal<Pio<u8>> {
#[cfg(any(all(target_arch="x86", not(test)), doc))]
#[allow(unused)]
pub fn new(com_port: ComPort) -> SerialInternal<Pio<u8>> {
let mut data_port = Pio::<u8>::new(com_port.0 + 0);
let mut interrupt_port = Pio::<u8>::new(com_port.0 + 1);
let mut baud_diviser_lo = Pio::<u8>::new(com_port.0 + 0);
let mut baud_diviser_hi = Pio::<u8>::new(com_port.0 + 1);
let mut fifo_port = Pio::<u8>::new(com_port.0 + 2);
let mut lcr_port = Pio::<u8>::new(com_port.0 + 3);
let mut mcr_port = Pio::<u8>::new(com_port.0 + 4);
let mut status_port = Pio::<u8>::new(com_port.0 + 5);
interrupt_port .write(0x00);
lcr_port .write(0x80);
baud_diviser_lo.write(0x03);
baud_diviser_hi.write(0x00);
lcr_port .write(0x03);
fifo_port .write(0xC7);
SerialInternal { data_port, status_port }
}
#[cfg(test)]
pub fn new(_com_port: ComPort) -> SerialInternal<Pio<u8>> { panic!("mock implementation !") }
fn send_string(&mut self, string: &str) {
for byte in string.bytes() {
while self.status_port.read() & 0x20 == 0 {}
self.data_port.write(byte);
}
}
}
#[derive(Debug)]
pub struct SerialLogger;
impl SerialLogger {
pub unsafe fn force_unlock(&mut self) {
G_SERIAL.call_once(|| SpinLockIRQ::new(SerialInternal::<Pio<u8>>::new(COM1))).force_unlock();
}
}
impl Write for SerialLogger {
#[cfg(not(test))]
fn write_str(&mut self, s: &str) -> Result<(), ::core::fmt::Error> {
let mut internal = G_SERIAL.call_once(|| SpinLockIRQ::new(SerialInternal::<Pio<u8>>::new(COM1))).lock();
internal.send_string(s);
Ok(())
}
#[cfg(test)]
fn write_str(&mut self, s: &str) -> Result<(), ::core::fmt::Error> {
use std::println;
println!("{}", s);
Ok(())
}
}