aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/io/io.c
blob: c4dad52b2445b871d293a907b21add888d8761f6 (plain) (blame)
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
#include <stdint.h>
#include <kernel/system.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 cli()
{
    asm volatile ("cli");
}

void sti()
{
    asm volatile ("sti");
}

void hlt()
{
    asm volatile ("hlt");
}

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");
}

int is_transmit_empty()
{
    return inb(0x3f8 + 5) & 0x20;
}

void serial_put(char ch)
{
    while (is_transmit_empty() == 0);
    outb(0x3f8, (uint8_t) ch);
}