summaryrefslogtreecommitdiff
path: root/src/drivers/uart.cc
blob: 4064b16d6f275d9deb15d044323d33cc8f8523c2 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "drivers/uart.h"
#include "kernel/riscv.h"
#include "kernel/spinlock.h"
#include "kernel/proc.h"
#include "kernel/mm.h"
#include "lib/string.h"

namespace Drivers
{
    // Circular buffer structure
    struct
    {
        Spinlock lock;
        char buf[UART_RX_BUF_SIZE];
        uint32 r; // read index
        uint32 w; // write index
    } uart_rx;

    Spinlock uart_tx_lock;

    void uart_init()
    {
        uart_rx.lock.init("uart_rx");
        uart_tx_lock.init("uart_tx");
        uart_rx.r = 0;
        uart_rx.w = 0;

        // Disable interrupts and configure
        *(volatile uint8 *)(UART0 + IER) = 0x00;

        // Enter baud rate setting mode (LCR_DLAB) = 0x80;
        *(volatile uint8 *)(UART0 + LCR) = 0x80;
        *(volatile uint8 *)(UART0 + 0) = 0x03; // DLL: 38.4K baud
        *(volatile uint8 *)(UART0 + 1) = 0x00; // DLM

        // Exit baud rate setting, set 8-bit word length, no parity
        *(volatile uint8 *)(UART0 + LCR) = 0x03;

        // Reset and enable FIFO
        *(volatile uint8 *)(UART0 + FCR) = 0x07;

        // Enable Receive Data Available Interrupt
        *(volatile uint8 *)(UART0 + IER) = 0x01;
    }

    void uart_putc(char c)
    {
        uart_tx_lock.acquire();
        while ((*(volatile uint8 *)(UART0 + LSR) & LSR_TX_IDLE) == 0)
            ;
        *(volatile uint8 *)(UART0 + THR) = c;
        uart_tx_lock.release();
    }

    void uart_puts(const char *s)
    {
        while (*s)
            uart_putc(*s++);
    }

    void uart_put_int(int val)
    {
        char buf[16];
        int i = 0;
        if (val == 0)
        {
            uart_puts("0");
            return;
        }
        if (val < 0)
        {
            uart_puts("-");
            val = -val;
        }
        while (val > 0)
        {
            buf[i++] = (val % 10) + '0';
            val /= 10;
        }
        while (i > 0)
        {
            uart_putc(buf[--i]);
        }
    }

    void print_hex(uint64 val)
    {
        char hex[] = "0123456789ABCDEF";
        uart_puts("0x");
        for (int i = 60; i >= 0; i -= 4)
        {
            uart_putc(hex[(val >> i) & 0xF]);
        }
    }

    void uart_intr()
    {
        uart_rx.lock.acquire();

        while (1)
        {
            if ((*(volatile uint8 *)(UART0 + LSR) & LSR_RX_READY) == 0)
                break;

            char c = *(volatile uint8 *)(UART0 + RHR);

            uart_rx.buf[uart_rx.w % UART_RX_BUF_SIZE] = c;
            uart_rx.w++;

            ProcManager::wakeup(&uart_rx);
        }

        uart_rx.lock.release();
    }

    // Modern console read: Line buffering + Editing Keys + Echo
    int console_read(uint64 target, int len)
    {
        struct Proc *p = myproc();
        int target_idx = 0;

        uart_rx.lock.acquire();

        while (target_idx < len)
        {
            while (uart_rx.r == uart_rx.w)
            {
                if (p->xstate != 0)
                {
                    uart_rx.lock.release();
                    return -1;
                }
                ProcManager::sleep(&uart_rx, &uart_rx.lock);
            }

            char c = uart_rx.buf[uart_rx.r % UART_RX_BUF_SIZE];
            uart_rx.r++;

            uart_rx.lock.release();

            // 1. Handle Backspace (127 or \b)
            if (c == 127 || c == '\b')
            {
                if (target_idx > 0)
                {
                    // Erase character from screen: Backspace, Space, Backspace
                    uart_putc('\b');
                    uart_putc(' ');
                    uart_putc('\b');
                    target_idx--;
                }
                uart_rx.lock.acquire();
                continue;
            }

            // 2. Handle Ctrl+U (Kill Line)
            if (c == 0x15)
            {
                while (target_idx > 0)
                {
                    uart_putc('\b');
                    uart_putc(' ');
                    uart_putc('\b');
                    target_idx--;
                }
                uart_rx.lock.acquire();
                continue;
            }

            // 3. Handle Ctrl+L (Clear Screen)
            if (c == 0x0C)
            {
                uart_puts(ANSI_CLEAR);
                uart_puts("$ ");
                uart_rx.lock.acquire();
                continue;
            }

            // 4. Handle Return (CR -> LF)
            if (c == '\r')
            {
                c = '\n';
            }

            // Echo back to user
            // uart_putc(c);

            if (VM::copyout(p->pagetable, target + target_idx, &c, 1) < 0)
            {
                return -1;
            }

            target_idx++;

            if (c == '\n')
            {
                return target_idx;
            }

            uart_rx.lock.acquire();
        }

        uart_rx.lock.release();
        return target_idx;
    }
}