aboutsummaryrefslogtreecommitdiff
path: root/platform/pc/console.c
blob: cfc4a774cff86a9539cba78614c90b2660309775 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (c) 2009 Corey Tabaka
 * Copyright (c) 2016 Travis Geiselbrecht
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include <arch/x86.h>
#include <platform/pc.h>
#include <platform/console.h>
#include <string.h>
#include <lib/io.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

/* memory mapped framebuffer */
#define FB (0xB8000U + KERNEL_ASPACE_BASE)

/* CGA values */
#define CURSOR_START        0x0A
#define CURSOR_END          0x0B
#define VIDEO_ADDRESS_MSB   0x0C
#define VIDEO_ADDRESS_LSB   0x0D
#define CURSOR_POS_MSB      0x0E
#define CURSOR_POS_LSB      0x0F

/* curr settings */
static unsigned char curr_x;
static unsigned char curr_y;
static unsigned char curr_start;
static unsigned char curr_end;
static unsigned char curr_attr = 0x7;

/* video page buffer */
#define VPAGE_SIZE      2048
#define PAGE_MAX        8

static int active_page = 0;
static int visual_page = 0;

static int curs_x[PAGE_MAX];
static int curs_y[PAGE_MAX];

static struct {
    int x1, y1, x2, y2;
} view_window = {
    0, 0, 79, 24
};

void platform_init_console(void)
{
    curr_save();
    window(0, 0, 79, 24);
    clear();
    place(0, 0);
}

void set_visual_page(int page)
{
    unsigned short page_offset = page*VPAGE_SIZE;
    visual_page = page;

    outp(CGA_INDEX_REG, VIDEO_ADDRESS_LSB);
    outp(CGA_DATA_REG, page_offset & 0xFF);
    outp(CGA_INDEX_REG, VIDEO_ADDRESS_MSB);
    outp(CGA_DATA_REG, (page_offset >> 8) & 0xFF);
}

void set_active_page(int page)
{
    curs_x[active_page] = curr_x;
    curs_y[active_page] = curr_y;
    curr_x = curs_x[page];
    curr_y = curs_y[page];
    active_page = page;
}

int get_visual_page(void)
{
    return visual_page;
}

int get_active_page(void)
{
    return active_page;
}

void place(int x,int y)
{
    unsigned short cursor_word = x + y*80 + active_page*VPAGE_SIZE;

    /*
     * program CGA using index reg, then data reg
     */
    outp(CGA_INDEX_REG, CURSOR_POS_LSB);
    outp(CGA_DATA_REG, cursor_word & 0xFF);
    outp(CGA_INDEX_REG, CURSOR_POS_MSB);
    outp(CGA_DATA_REG, (cursor_word >> 8) & 0xFF);

    curr_x = x;
    curr_y = y;
}

void cursor(int start,int end)
{
    outp(CGA_INDEX_REG, CURSOR_START);
    outp(CGA_DATA_REG, start);
    outp(CGA_INDEX_REG, CURSOR_END);
    outp(CGA_DATA_REG, end);
}

void curr_save(void)
{
#if 0
    /* grab some info from the bios data area (these should be defined in memmap.h */
    curr_attr = *((unsigned char *)FB + 159);
    curr_x = *((unsigned char *)0x00450);
    curr_y = *((unsigned char *)0x00451);
    curr_end = *((unsigned char *)0x00460);
    curr_start = *((unsigned char *)0x00461);
#endif
    active_page = visual_page = 0;
}

void curr_restore(void)
{
#if 0
    *((unsigned char *)0x00450) = curr_x;
    *((unsigned char *)0x00451) = curr_y;
#endif

    place(curr_x, curr_y);
    cursor(curr_start, curr_end);
}

void window(int x1, int y1, int x2, int y2)
{
    view_window.x1 = x1;
    view_window.y1 = y1;
    view_window.x2 = x2;
    view_window.y2 = y2;

    //place(x1, y1);
}

void _clear(char c,char attr,int x1,int y1,int x2,int y2)
{
    register int i,j;
    unsigned short w = attr;

    w <<= 8;
    w |= c;
    for (i = x1; i <= x2; i++) {
        for (j = y1; j <= y2; j++) {
            *((unsigned short *)(uintptr_t)(FB + 2*i+160*j + 2 * active_page * VPAGE_SIZE)) = w;
        }
    }

    place(x1,y1);
    curr_y = y1;
    curr_x = x1;
}

void clear()
{
    _clear(' ', curr_attr, view_window.x1, view_window.y1, view_window.x2,
           view_window.y2);
}

void _scroll(char attr, int x1, int y1, int x2, int y2)
{
    register int x,y;
    unsigned short xattr = attr << 8,w;
    unsigned char *v = (unsigned char *)(uintptr_t)(FB + active_page*(2*VPAGE_SIZE));

    for (y = y1+1; y <= y2; y++) {
        for (x = x1; x <= x2; x++) {
            w = *((unsigned short *) (v + 2*(y*80+x)));
            *((unsigned short *)(v + 2*((y-1)*80+x))) = w;
        }
    }

    for (x = x1; x <= x2; x++) {
        *((unsigned short *)(v + 2*((y-1)*80+x))) = xattr;
    }
}

void scroll(void)
{
    _scroll(curr_attr, view_window.x1, view_window.y1, view_window.x2,
            view_window.y2);
}

void cputc(char c)
{
    static unsigned short scan_x, x, y;
    unsigned char *v = (unsigned char *)(uintptr_t)(FB + active_page*(2*VPAGE_SIZE));
    x = curr_x;
    y = curr_y;

    switch (c) {
        case '\t':
            x += 8;
            if (x >= view_window.x2+1) {
                x = view_window.x1;
                if (y == view_window.y2) {
                    scroll();
                } else {
                    y++;
                }
            } else {
                scan_x = 0;

                while ((scan_x+8) < x) {
                    scan_x += 8;
                }

                x = scan_x;
            }
            break;

        case '\r':
            x = view_window.x1;
            break;

        case '\n':
            if (y == view_window.y2) {
                scroll();
            } else {
                y++;
            }
            break;

        case '\b':
            x--;
            *(v + 2*(x + y*80)) = ' ';
            break;

        default:
            *(v + 2*(x + y*80)) = c;
            x++;

            if (x >= view_window.x2+1) {
                x = view_window.x1;
                if (y == view_window.y2) {
                    scroll();
                } else {
                    y++;
                }
            }
    }

    place(x, y);
}

void cputs(char *s)
{
    char c;
    while (*s != '\0') {
        c = *s++;
        cputc(c);
    }
}

void puts_xy(int x,int y,char attr,char *s)
{
    unsigned char *v = (unsigned char *)(uintptr_t)(FB + (80*y+x)*2 + active_page*(2*VPAGE_SIZE));
    while (*s != 0) {
        *v = *s;
        s++;
        v++;
        *v = attr;
        v++;
    }
}

void putc_xy(int x, int y, char attr, char c)
{
    unsigned char *v = (unsigned char *)(uintptr_t)(FB + (80*y+x)*2 + active_page*(2*VPAGE_SIZE));
    *v = c;
    v++;
    *v = attr;
}

int printf_xy(int x, int y, char attr, char *fmt, ...)
{
    char cbuf[200];
    va_list parms;
    int result;

    va_start(parms, fmt);
    result = vsprintf(cbuf, fmt, parms);
    va_end(parms);

    puts_xy(x, y, attr, cbuf);

    return result;
}