4 * Copyright (c) 2008 Joshua Wise. All rights reserved.
5 * This program is free software; you can redistribute and/or modify it under
6 * the terms found in the file LICENSE in the root of this source tree.
11 #include "console-ext.h"
15 #define POS_IS_VALID(row, col) ((row) >= 0 && (row) < CONSOLE_HEIGHT && (col) >= 0 && (col) < CONSOLE_WIDTH)
17 /** @brief A structure describing a console that is backed by memory.
19 * In the mode of designing for extensibility, all of the console driver's
20 * state is encapsulated in a 'struct console', which would ostensibly make
21 * it easier to create virtual consoles later. All of the console driver's
22 * workings touch this struct; ideally, there are no references to
23 * CONSOLE_MEM_BASE or other hardware outside of this. In practice, this
24 * isn't quite the case (see update_cursor()).... but we're close.
27 int row; /**< The current row. */
28 int col; /**< The current column. */
29 unsigned char attr; /**< The current color/attribute. */
30 unsigned char *base; /**< The current base to write data to.
31 * Might not be equal to physbase if we're
32 * in a different virtual console right
33 * now or we have requested a backbuffer!
35 unsigned char *physbase; /**< The physical memory base for
38 int showcursor; /**< Whether the cursor should be shown
41 int initialized; /**< Whether the console has been
42 * initialized. Functions should check
43 * this and call clear_console() before
44 * running if this is zero.
46 int backbuffer; /**< Whether we're currently writing to a
47 * backbuffer instead of the physical
52 /** @brief The structure describing the one console on the system. Should
53 * not be touched by anyone but the initializer for curcons!
55 static struct console cons = {
59 .base = (unsigned char *)CONSOLE_MEM_BASE,
60 .physbase = (unsigned char *)CONSOLE_MEM_BASE,
66 /** @brief The current console that all console.c operations work on. */
67 static struct console *curcons = &cons;
69 /** @brief Makes sure that the VGA cursor matches with the console.c's idea
70 * of where the cursor should be.
72 * update_cursor would be trivial, but for a few important checks. In
73 * particular, it won't touch the cursor at all if we're backbuffered
74 * (since that would make the cursor fly around a screen that isn't
75 * actually being updated), and if the cursor is hidden, it sets it to an
76 * out-of-bounds segment to make sure that it's actually hidden.
78 static void update_cursor()
80 if (curcons->backbuffer)
82 if (curcons->showcursor)
84 unsigned short addr = (curcons->row * CONSOLE_WIDTH + curcons->col);
85 outb(CRTC_IDX_REG, CRTC_CURSOR_MSB_IDX);
86 outb(CRTC_DATA_REG, (addr >> 8));
87 outb(CRTC_IDX_REG, CRTC_CURSOR_LSB_IDX);
88 outb(CRTC_DATA_REG, addr & 0xFF);
90 outb(CRTC_IDX_REG, CRTC_CURSOR_MSB_IDX);
91 outb(CRTC_DATA_REG, 255 /* invalid */);
92 outb(CRTC_IDX_REG, CRTC_CURSOR_LSB_IDX);
93 outb(CRTC_DATA_REG, 255 /* invalid */);
97 /** @brief Redirects console writes to a backbuffer.
99 * Verifies that the console is not already backbuffered. If it's not, it
100 * allocates a backbuffer, copies the current console into the backbuffer,
101 * and sets the backbuffered flag.
103 * This isn't just theoretical, by the way. The game screen's timer causes
104 * it to repaint every frame (for lack of a better way to do it), which is
105 * fine in qemu (which is fast), but causes severe flicker in simics. This
106 * backbuffering logic seems to have alleviated the flicker.
108 * @see cons_debackbuffer
110 /*void cons_backbuffer()
112 if (!curcons->initialized)
114 if (curcons->backbuffer)
116 curcons->base = malloc(CONSOLE_WIDTH * CONSOLE_HEIGHT * 2);
117 memcpy(curcons->base, curcons->physbase, CONSOLE_WIDTH * CONSOLE_HEIGHT * 2);
118 curcons->backbuffer = 1;
121 /** @brief Turns off the backbuffer.
123 * Verifies that we are currently backbuffered. If so, copies the
124 * backbuffer into video memory, frees the backbuffer, sets the pointer
125 * back to video memory, clears the backbuffered flag, and updates the
128 * @see cons_backbuffer
130 /*void cons_debackbuffer()
132 if (!curcons->initialized)
134 if (!curcons->backbuffer)
136 memcpy(curcons->physbase, curcons->base, CONSOLE_WIDTH * CONSOLE_HEIGHT * 2);
138 curcons->base = curcons->physbase;
139 curcons->backbuffer = 0;
145 if (!curcons->initialized)
148 /* Make sure to handle special cases nicely.*/
153 if (curcons->row >= CONSOLE_HEIGHT) /* Moving off the end? Scroll. */
156 memmove(curcons->base, curcons->base + 2*CONSOLE_WIDTH, 2*CONSOLE_WIDTH*(CONSOLE_HEIGHT-1));
158 for (c=0; c<CONSOLE_WIDTH; c++) /* Clear the newly blank bottom line. */
160 curcons->base[(curcons->row * CONSOLE_WIDTH + c) * 2] = ' ';
161 curcons->base[(curcons->row * CONSOLE_WIDTH + c) * 2 + 1] = curcons->attr;
173 curcons->base[(curcons->row*CONSOLE_WIDTH + curcons->col) * 2] = ' ';
178 curcons->base[(curcons->row*CONSOLE_WIDTH + curcons->col) * 2] = ch;
179 curcons->base[(curcons->row*CONSOLE_WIDTH + curcons->col) * 2 + 1] = curcons->attr;
181 if (curcons->col >= CONSOLE_WIDTH)
188 void putbytes(const char *s, int len)
190 if (!curcons->initialized)
197 int set_term_color(int color)
199 if (!curcons->initialized)
202 curcons->attr = (unsigned char)color;
206 void get_term_color(int *color)
208 if (!curcons->initialized)
211 *color = (int)curcons->attr;
214 int set_cursor(int row, int col)
216 if (!curcons->initialized)
218 if (!POS_IS_VALID(row, col))
226 void get_cursor(int *row, int *col)
228 if (!curcons->initialized)
236 if (!curcons->initialized)
238 curcons->showcursor = 0;
244 if (!curcons->initialized)
246 curcons->showcursor = 1;
253 curcons->initialized = 1;
256 for (i = 0; i < CONSOLE_WIDTH * CONSOLE_HEIGHT; i++)
258 curcons->base[i*2] = ' ';
259 curcons->base[i*2+1] = FGND_LGRAY;
264 void draw_char(int row, int col, int ch, int color)
266 if (!POS_IS_VALID(row, col))
268 curcons->base[2 * (CONSOLE_WIDTH * row + col)] = (unsigned char)ch;
269 curcons->base[2 * (CONSOLE_WIDTH * row + col)+1] = (unsigned char)color;
272 char get_char(int row, int col)
274 if (!POS_IS_VALID(row, col))
276 return curcons->base[2 * (CONSOLE_WIDTH * row + col)];