summaryrefslogtreecommitdiff
path: root/arch/x86/machine/cga.c
blob: 0cc857f0bc2ab762093580b8f393e5e187da6fab (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
/*
 * Copyright (c) 2010, 2012 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <kern/init.h>
#include <kern/macros.h>
#include <kern/param.h>
#include <kern/stdint.h>
#include <kern/string.h>
#include <machine/io.h>
#include <machine/pmap.h>
#include <machine/cga.h>

/*
 * Screen dimensions.
 */
#define CGA_COLUMNS 80
#define CGA_LINES   25

/*
 * Text mode mapped memory and size.
 */
#define CGA_MEMORY      0xb8000
#define CGA_MEMORY_SIZE (CGA_COLUMNS * CGA_LINES * 2)

/*
 * I/O ports.
 */
#define CGA_MISC_OUTPUT_REGISTER_READ   0x3cc
#define CGA_MISC_OUTPUT_REGISTER_WRITE  0x3c2
#define CGA_CRTC_ADDRESS_REGISTER       0x3d4
#define CGA_CRTC_DATA_REGISTER          0x3d5

/*
 * CRTC registers.
 */
#define CGA_CRTC_CURSOR_LOCATION_HIGH_REGISTER  0xe
#define CGA_CRTC_CURSOR_LOCATION_LOW_REGISTER   0xf

/*
 * Foreground screen color.
 */
#define CGA_FOREGROUND_COLOR 0x7

/*
 * Blank space 16 bits word.
 */
#define CGA_BLANK ((CGA_FOREGROUND_COLOR << 8) | ' ')

/*
 * Number of spaces to display for a tabulation.
 */
#define CGA_TABULATION_SPACES 8

static uint8_t *cga_memory;
static uint16_t cga_cursor;

static uint16_t
cga_get_cursor_position(void)
{
    uint16_t tmp;

    io_write_byte(CGA_CRTC_ADDRESS_REGISTER,
                  CGA_CRTC_CURSOR_LOCATION_HIGH_REGISTER);
    tmp = io_read_byte(CGA_CRTC_DATA_REGISTER) << 8;
    io_write_byte(CGA_CRTC_ADDRESS_REGISTER,
                  CGA_CRTC_CURSOR_LOCATION_LOW_REGISTER);
    tmp |= io_read_byte(CGA_CRTC_DATA_REGISTER);

    return tmp;
}

static void
cga_set_cursor_position(uint16_t position)
{
    io_write_byte(CGA_CRTC_ADDRESS_REGISTER,
                  CGA_CRTC_CURSOR_LOCATION_HIGH_REGISTER);
    io_write_byte(CGA_CRTC_DATA_REGISTER, position >> 8);
    io_write_byte(CGA_CRTC_ADDRESS_REGISTER,
                  CGA_CRTC_CURSOR_LOCATION_LOW_REGISTER);
    io_write_byte(CGA_CRTC_DATA_REGISTER, position & 0xff);
}

static uint8_t
cga_get_cursor_column(void)
{
    return cga_cursor % CGA_COLUMNS;
}

void __init
cga_setup(void)
{
    uint8_t misc_output_register;
    unsigned long va;

    va = pmap_bootalloc(1);
    pmap_kenter(va, CGA_MEMORY);
    pmap_update(kernel_pmap, va, va + PAGE_SIZE);
    cga_memory = (uint8_t *)va;

    /*
     * Check if the Input/Output Address Select bit is set.
     */
    misc_output_register = io_read_byte(CGA_MISC_OUTPUT_REGISTER_READ);

    if (!(misc_output_register & 0x1)) {
        /*
         * Set the I/O AS bit.
         */
        misc_output_register |= 0x1;

        /*
         * Update the misc output register.
         */
        io_write_byte(CGA_MISC_OUTPUT_REGISTER_WRITE, misc_output_register);
    }

    cga_cursor = cga_get_cursor_position();
}

static void
cga_scroll_lines(void)
{
    uint16_t *last_line;
    int i;

    memmove(cga_memory, (uint16_t *)cga_memory + CGA_COLUMNS,
            CGA_MEMORY_SIZE - (CGA_COLUMNS * 2));
    last_line = (uint16_t *)cga_memory + (CGA_COLUMNS * (CGA_LINES - 1));

    for(i = 0; i < CGA_COLUMNS; i++)
        last_line[i] = CGA_BLANK;
}

void
cga_write_byte(uint8_t byte)
{
    if (byte == '\r')
        return;
    else if (byte == '\n') {
        cga_cursor += CGA_COLUMNS - cga_get_cursor_column();

        if (cga_cursor >= (CGA_LINES * CGA_COLUMNS)) {
            cga_scroll_lines();
            cga_cursor -= CGA_COLUMNS;
        }

        cga_set_cursor_position(cga_cursor);
    } else if (byte == '\b') {
        if (cga_cursor > 0) {
            cga_cursor--;
            ((uint16_t *)cga_memory)[cga_cursor] = CGA_BLANK;
            cga_set_cursor_position(cga_cursor);
        }
    } else if (byte == '\t') {
        int i;

        for(i = 0; i < CGA_TABULATION_SPACES; i++)
            cga_write_byte(' ');
    } else {
        if ((cga_cursor + 1) >= CGA_COLUMNS * CGA_LINES) {
            cga_scroll_lines();
            cga_cursor -= CGA_COLUMNS;
        }

        ((uint16_t *)cga_memory)[cga_cursor] = ((CGA_FOREGROUND_COLOR << 8)
                                                | byte);
        cga_cursor++;
        cga_set_cursor_position(cga_cursor);
    }
}

void console_write_byte(char c) __alias("cga_write_byte");