2 * @brief A console driver.
4 * @author Joshua Wise (jwise) <joshua@joshuawise.com>
9 #include "console-ext.h"
13 #define POS_IS_VALID(row, col) ((row) >= 0 && (row) < CONSOLE_HEIGHT && (col) >= 0 && (col) < CONSOLE_WIDTH)
15 /** @brief A structure describing a console that is backed by memory.
17 * In the mode of designing for extensibility, all of the console driver's
18 * state is encapsulated in a 'struct console', which would ostensibly make
19 * it easier to create virtual consoles later. All of the console driver's
20 * workings touch this struct; ideally, there are no references to
21 * CONSOLE_MEM_BASE or other hardware outside of this. In practice, this
22 * isn't quite the case (see update_cursor()).... but we're close.
25 int row; /**< The current row. */
26 int col; /**< The current column. */
27 unsigned char attr; /**< The current color/attribute. */
28 unsigned char *base; /**< The current base to write data to.
29 * Might not be equal to physbase if we're
30 * in a different virtual console right
31 * now or we have requested a backbuffer!
33 unsigned char *physbase; /**< The physical memory base for
36 int showcursor; /**< Whether the cursor should be shown
39 int initialized; /**< Whether the console has been
40 * initialized. Functions should check
41 * this and call clear_console() before
42 * running if this is zero.
44 int backbuffer; /**< Whether we're currently writing to a
45 * backbuffer instead of the physical
50 /** @brief The structure describing the one console on the system. Should
51 * not be touched by anyone but the initializer for curcons!
53 static struct console cons = {
57 .base = (unsigned char *)CONSOLE_MEM_BASE,
58 .physbase = (unsigned char *)CONSOLE_MEM_BASE,
64 /** @brief The current console that all console.c operations work on. */
65 static struct console *curcons = &cons;
67 /** @brief Makes sure that the VGA cursor matches with the console.c's idea
68 * of where the cursor should be.
70 * update_cursor would be trivial, but for a few important checks. In
71 * particular, it won't touch the cursor at all if we're backbuffered
72 * (since that would make the cursor fly around a screen that isn't
73 * actually being updated), and if the cursor is hidden, it sets it to an
74 * out-of-bounds segment to make sure that it's actually hidden.
76 static void update_cursor()
78 if (curcons->backbuffer)
80 if (curcons->showcursor)
82 unsigned short addr = (curcons->row * CONSOLE_WIDTH + curcons->col);
83 outb(CRTC_IDX_REG, CRTC_CURSOR_MSB_IDX);
84 outb(CRTC_DATA_REG, (addr >> 8));
85 outb(CRTC_IDX_REG, CRTC_CURSOR_LSB_IDX);
86 outb(CRTC_DATA_REG, addr & 0xFF);
88 outb(CRTC_IDX_REG, CRTC_CURSOR_MSB_IDX);
89 outb(CRTC_DATA_REG, 255 /* invalid */);
90 outb(CRTC_IDX_REG, CRTC_CURSOR_LSB_IDX);
91 outb(CRTC_DATA_REG, 255 /* invalid */);
95 /** @brief Redirects console writes to a backbuffer.
97 * Verifies that the console is not already backbuffered. If it's not, it
98 * allocates a backbuffer, copies the current console into the backbuffer,
99 * and sets the backbuffered flag.
101 * This isn't just theoretical, by the way. The game screen's timer causes
102 * it to repaint every frame (for lack of a better way to do it), which is
103 * fine in qemu (which is fast), but causes severe flicker in simics. This
104 * backbuffering logic seems to have alleviated the flicker.
106 * @see cons_debackbuffer
108 /*void cons_backbuffer()
110 if (!curcons->initialized)
112 if (curcons->backbuffer)
114 curcons->base = malloc(CONSOLE_WIDTH * CONSOLE_HEIGHT * 2);
115 memcpy(curcons->base, curcons->physbase, CONSOLE_WIDTH * CONSOLE_HEIGHT * 2);
116 curcons->backbuffer = 1;
119 /** @brief Turns off the backbuffer.
121 * Verifies that we are currently backbuffered. If so, copies the
122 * backbuffer into video memory, frees the backbuffer, sets the pointer
123 * back to video memory, clears the backbuffered flag, and updates the
126 * @see cons_backbuffer
128 /*void cons_debackbuffer()
130 if (!curcons->initialized)
132 if (!curcons->backbuffer)
134 memcpy(curcons->physbase, curcons->base, CONSOLE_WIDTH * CONSOLE_HEIGHT * 2);
136 curcons->base = curcons->physbase;
137 curcons->backbuffer = 0;
143 if (!curcons->initialized)
146 /* Make sure to handle special cases nicely.*/
151 if (curcons->row >= CONSOLE_HEIGHT) /* Moving off the end? Scroll. */
154 memmove(curcons->base, curcons->base + 2*CONSOLE_WIDTH, 2*CONSOLE_WIDTH*(CONSOLE_HEIGHT-1));
156 for (c=0; c<CONSOLE_WIDTH; c++) /* Clear the newly blank bottom line. */
158 curcons->base[(curcons->row * CONSOLE_WIDTH + c) * 2] = ' ';
159 curcons->base[(curcons->row * CONSOLE_WIDTH + c) * 2 + 1] = curcons->attr;
171 curcons->base[(curcons->row*CONSOLE_WIDTH + curcons->col) * 2] = ' ';
176 curcons->base[(curcons->row*CONSOLE_WIDTH + curcons->col) * 2] = ch;
177 curcons->base[(curcons->row*CONSOLE_WIDTH + curcons->col) * 2 + 1] = curcons->attr;
179 if (curcons->col >= CONSOLE_WIDTH)
186 void putbytes(const char *s, int len)
188 if (!curcons->initialized)
195 int set_term_color(int color)
197 if (!curcons->initialized)
200 curcons->attr = (unsigned char)color;
204 void get_term_color(int *color)
206 if (!curcons->initialized)
209 *color = (int)curcons->attr;
212 int set_cursor(int row, int col)
214 if (!curcons->initialized)
216 if (!POS_IS_VALID(row, col))
224 void get_cursor(int *row, int *col)
226 if (!curcons->initialized)
234 if (!curcons->initialized)
236 curcons->showcursor = 0;
242 if (!curcons->initialized)
244 curcons->showcursor = 1;
251 curcons->initialized = 1;
254 for (i = 0; i < CONSOLE_WIDTH * CONSOLE_HEIGHT; i++)
256 curcons->base[i*2] = ' ';
257 curcons->base[i*2+1] = FGND_LGRAY;
262 void draw_char(int row, int col, int ch, int color)
264 if (!POS_IS_VALID(row, col))
266 curcons->base[2 * (CONSOLE_WIDTH * row + col)] = (unsigned char)ch;
267 curcons->base[2 * (CONSOLE_WIDTH * row + col)+1] = (unsigned char)color;
270 char get_char(int row, int col)
272 if (!POS_IS_VALID(row, col))
274 return curcons->base[2 * (CONSOLE_WIDTH * row + col)];