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
|
#include <stdint.h>
#include <kernel/lib/lib.h>
#include <kernel/io/io.h>
#include <kernel/system.h>
#include <kernel/lib/string.h>
#include <kernel/lib/stdlib.h>
uint8_t inb(uint16_t port)
{
uint8_t value;
asm ("inb %1, %0" : "=a"(value) : "Nd"(port));
return value;
}
uint16_t inw(uint16_t port)
{
uint16_t value;
asm ("inw %1, %0" : "=a"(value) : "Nd"(port));
return value;
}
uint32_t inl(uint16_t port)
{
uint32_t value;
asm ("inl %1, %0" : "=a"(value) : "Nd"(port));
return value;
}
void outb(uint16_t port, uint8_t data)
{
asm ("outb %0, %1"::"a" (data), "Nd"(port));
}
void outw(uint16_t port, uint16_t data)
{
asm ("outw %0, %1"::"a" (data), "Nd"(port));
}
void outl(uint16_t port, uint32_t data)
{
asm ("outl %0, %1"::"a" (data), "Nd"(port));
}
void init_serial()
{
outb(0x3f8 + 1, 0x00);
outb(0x3f8 + 3, 0x80);
outb(0x3f8 + 0, 0x03);
outb(0x3f8 + 1, 0x00);
outb(0x3f8 + 3, 0x03);
outb(0x3f8 + 2, 0xC7);
outb(0x3f8 + 4, 0x0B);
vga_log("Installed serial connection", 2);
}
int is_transmit_empty()
{
return inb(0x3f8 + 5) & 0x20;
}
void serial_put(char ch)
{
while (is_transmit_empty() == 0);
outb(0x3f8, ch);
}
void serial_write(const char *data)
{
for (size_t i = 0; i < strlen(data); i++) {
serial_put(data[i]);
}
}
void serial_write_hex(int n)
{
int tmp;
serial_write("0x");
char noZeroes = 1;
for (int i = 28; i > 0; i -= 4) {
tmp = (n >> i) & 0xF;
if (tmp == 0 && noZeroes != 0) continue;
if (tmp >= 0xA) {
noZeroes = 0;
serial_put(tmp - 0xA + 'a');
} else {
noZeroes = 0;
serial_put(tmp + '0');
}
}
tmp = n & 0xF;
if (tmp >= 0xA) {
serial_put(tmp - 0xA + 'a');
} else {
serial_put(tmp + '0');
}
}
void serial_write_dec(int n)
{
serial_write(itoa(n));
}
|