2011-03-26 06:29:54 +03:00
|
|
|
/*
|
|
|
|
* vim:tabstop=4
|
|
|
|
* vim:noexpandtab
|
|
|
|
*
|
|
|
|
* Bochs VBE / QEMU vga=std Graphics Driver
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <system.h>
|
2011-03-26 21:30:08 +03:00
|
|
|
#include <fs.h>
|
2011-03-26 06:29:54 +03:00
|
|
|
|
2011-03-27 22:41:00 +04:00
|
|
|
#define PREFERRED_X 1024
|
|
|
|
#define PREFERRED_Y 768
|
|
|
|
#define PREFERRED_B 32
|
|
|
|
|
|
|
|
uint16_t bochs_resolution_x = 0;
|
|
|
|
uint16_t bochs_resolution_y = 0;
|
|
|
|
uint16_t bochs_resolution_b = 0;
|
2011-03-26 07:24:22 +03:00
|
|
|
|
2011-03-29 05:34:53 +04:00
|
|
|
uint32_t * BOCHS_VID_MEMORY = (uint32_t *)0xE0000000;
|
2011-03-29 04:34:44 +04:00
|
|
|
|
|
|
|
#define TERM_WIDTH 128
|
|
|
|
#define TERM_HEIGHT 64
|
|
|
|
|
|
|
|
static short csr_x = 0;
|
|
|
|
static short csr_y = 0;
|
|
|
|
static uint8_t * term_buffer;
|
|
|
|
static uint8_t current_fg = 7;
|
|
|
|
static uint8_t current_bg = 0;
|
2011-03-26 07:24:22 +03:00
|
|
|
|
2011-03-26 06:29:54 +03:00
|
|
|
void
|
|
|
|
graphics_install_bochs() {
|
|
|
|
outports(0x1CE, 0x00);
|
2011-03-27 21:43:08 +04:00
|
|
|
uint16_t i = inports(0x1CF);
|
|
|
|
if (i < 0xB0C0 || i > 0xB0C6) {
|
|
|
|
kprintf("[bochs] You are not a Bochs VBE pseudo-card!\n");
|
|
|
|
kprintf("[bochs] 0x%x is totally wrong!\n", (unsigned int)i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
kprintf("[bochs] Successfully detected a Bochs VBE setup!\n");
|
|
|
|
kprintf("[bochs] You are using QEMU or Bochs and I love you.\n");
|
2011-03-26 06:29:54 +03:00
|
|
|
outports(0x1CF, 0xB0C4);
|
2011-03-27 21:43:08 +04:00
|
|
|
i = inports(0x1CF);
|
2011-03-26 07:24:22 +03:00
|
|
|
kprintf("[bochs] Enabling 1024x768x32 graphics mode!\n");
|
2011-03-26 06:29:54 +03:00
|
|
|
/* Disable VBE */
|
|
|
|
outports(0x1CE, 0x04);
|
|
|
|
outports(0x1CF, 0x00);
|
|
|
|
/* Set X resolution to 1024 */
|
|
|
|
outports(0x1CE, 0x01);
|
2011-03-27 22:41:00 +04:00
|
|
|
outports(0x1CF, PREFERRED_X);
|
2011-03-26 06:29:54 +03:00
|
|
|
/* Set Y resolution to 768 */
|
|
|
|
outports(0x1CE, 0x02);
|
2011-03-27 22:41:00 +04:00
|
|
|
outports(0x1CF, PREFERRED_Y);
|
2011-03-26 21:47:58 +03:00
|
|
|
/* Set bpp to 32 */
|
2011-03-26 06:29:54 +03:00
|
|
|
outports(0x1CE, 0x03);
|
2011-03-27 22:41:00 +04:00
|
|
|
outports(0x1CF, PREFERRED_B);
|
2011-03-26 06:29:54 +03:00
|
|
|
/* Re-enable VBE */
|
|
|
|
outports(0x1CE, 0x04);
|
2011-03-29 04:34:44 +04:00
|
|
|
outports(0x1CF, 0x41);
|
|
|
|
/* Herp derp */
|
2011-03-26 07:24:22 +03:00
|
|
|
|
2011-03-29 05:34:53 +04:00
|
|
|
uint32_t * herp = (uint32_t *)0xA0000;
|
|
|
|
herp[0] = 0xA5ADFACE;
|
|
|
|
|
|
|
|
uint16_t lfb_addr = pci_get_lfb_addr(0x1234);
|
|
|
|
if (lfb_addr) {
|
|
|
|
BOCHS_VID_MEMORY = (uint32_t *)((uint32_t)lfb_addr << 16);
|
|
|
|
} else {
|
|
|
|
/* Go find it */
|
2011-03-29 05:41:17 +04:00
|
|
|
for (uintptr_t x = 0xE0000000; x < 0xFFFFF000; x += 0x1000) {
|
2011-03-29 05:34:53 +04:00
|
|
|
if (((uintptr_t *)x)[0] == 0xA5ADFACE) {
|
|
|
|
BOCHS_VID_MEMORY = (uint32_t *)x;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-29 04:34:44 +04:00
|
|
|
bochs_resolution_x = PREFERRED_X;
|
|
|
|
bochs_resolution_y = PREFERRED_Y;
|
|
|
|
bochs_resolution_b = PREFERRED_B;
|
|
|
|
|
|
|
|
/* Buffer contains characters, fg (of 256), bg (same), flags (one byte) */
|
|
|
|
term_buffer = (uint8_t *)malloc(sizeof(uint8_t) * 4 * TERM_WIDTH * TERM_HEIGHT);
|
2011-03-26 07:24:22 +03:00
|
|
|
}
|
|
|
|
|
2011-03-29 00:18:48 +04:00
|
|
|
static void
|
|
|
|
bochs_set_point(
|
|
|
|
uint16_t x,
|
|
|
|
uint16_t y,
|
|
|
|
uint32_t color
|
|
|
|
) {
|
2011-03-29 04:34:44 +04:00
|
|
|
BOCHS_VID_MEMORY[(y * bochs_resolution_x + x)] = color;
|
2011-03-26 06:29:54 +03:00
|
|
|
}
|
2011-03-26 21:30:08 +03:00
|
|
|
|
2011-03-26 22:15:24 +03:00
|
|
|
void
|
|
|
|
bochs_scroll() {
|
|
|
|
__asm__ __volatile__ ("cli");
|
2011-03-29 04:34:44 +04:00
|
|
|
uint32_t size = sizeof(uint32_t) * bochs_resolution_x * (bochs_resolution_y - 12);
|
|
|
|
memmove((void *)BOCHS_VID_MEMORY, (void *)((uintptr_t)BOCHS_VID_MEMORY + bochs_resolution_x * 12 * sizeof(uint32_t)), size);
|
2011-03-26 22:15:24 +03:00
|
|
|
__asm__ __volatile__ ("sti");
|
|
|
|
}
|
|
|
|
|
2011-03-26 21:30:08 +03:00
|
|
|
void
|
2011-03-27 22:41:00 +04:00
|
|
|
bochs_draw_logo(char * filename) {
|
2011-03-27 21:43:08 +04:00
|
|
|
/* This is slow and ineffecient, but it's also dead simple. */
|
2011-03-27 22:41:00 +04:00
|
|
|
if (!bochs_resolution_x) { return; }
|
|
|
|
fs_node_t * file = kopen(filename,0);
|
|
|
|
if (!file) { return; }
|
2011-03-26 21:30:08 +03:00
|
|
|
char *bufferb = malloc(file->length);
|
2011-03-27 21:43:08 +04:00
|
|
|
/* Read the boot logo */
|
2011-03-26 21:30:08 +03:00
|
|
|
size_t bytes_read = read_fs(file, 0, file->length, (uint8_t *)bufferb);
|
2011-03-27 21:43:08 +04:00
|
|
|
uint16_t x = 0; /* -> 212 */
|
|
|
|
uint16_t y = 0; /* -> 68 */
|
|
|
|
/* Get the width / height of the image */
|
|
|
|
signed int *bufferi = (signed int *)((uintptr_t)bufferb + 2);
|
|
|
|
uint32_t width = bufferi[4];
|
|
|
|
uint32_t height = bufferi[5];
|
2011-03-28 03:08:41 +04:00
|
|
|
uint32_t row_width = (24 * width + 31) / 32 * 4;
|
2011-03-27 21:43:08 +04:00
|
|
|
/* Skip right to the important part */
|
2011-03-28 03:08:41 +04:00
|
|
|
size_t i = bufferi[2];
|
|
|
|
for (y = 0; y < height; ++y) {
|
|
|
|
for (x = 0; x < width; ++x) {
|
2011-03-28 04:41:31 +04:00
|
|
|
if (i > bytes_read) return;
|
2011-03-28 03:08:41 +04:00
|
|
|
/* Extract the color */
|
|
|
|
uint32_t color = bufferb[i + 3 * x] +
|
|
|
|
bufferb[i+1 + 3 * x] * 0x100 +
|
|
|
|
bufferb[i+2 + 3 * x] * 0x10000;
|
|
|
|
/* Set our point */
|
2011-03-29 04:34:44 +04:00
|
|
|
bochs_set_point((bochs_resolution_x - width) / 2 + x, (bochs_resolution_y - height) / 2 + (height - y), color);
|
2011-03-26 21:30:08 +03:00
|
|
|
}
|
2011-03-28 03:08:41 +04:00
|
|
|
i += row_width;
|
2011-03-26 21:30:08 +03:00
|
|
|
}
|
2011-03-29 04:34:44 +04:00
|
|
|
free(bufferb);
|
2011-03-26 21:30:08 +03:00
|
|
|
}
|
2011-03-29 00:18:48 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
bochs_fill_rect(
|
|
|
|
uint16_t x,
|
|
|
|
uint16_t y,
|
|
|
|
uint16_t w,
|
|
|
|
uint16_t h,
|
|
|
|
uint32_t color
|
|
|
|
) {
|
|
|
|
for (uint16_t i = y; i < h + y; ++i) {
|
|
|
|
for (uint16_t j = x; j < w + x; ++j) {
|
|
|
|
bochs_set_point(j,i,color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-03-29 01:29:59 +04:00
|
|
|
bochs_write_char(
|
2011-03-29 00:18:48 +04:00
|
|
|
uint8_t val,
|
|
|
|
uint16_t x,
|
|
|
|
uint16_t y,
|
|
|
|
uint32_t fg,
|
|
|
|
uint32_t bg
|
|
|
|
) {
|
2011-03-29 01:33:07 +04:00
|
|
|
uint8_t * c = number_font[val - 0x20];
|
2011-03-29 04:34:44 +04:00
|
|
|
__asm__ __volatile__ ("cli");
|
2011-03-29 00:18:48 +04:00
|
|
|
for (uint8_t i = 0; i < 12; ++i) {
|
|
|
|
if (c[i] & 0x80) { bochs_set_point(x,y+i,fg); } else { bochs_set_point(x,y+i,bg); }
|
|
|
|
if (c[i] & 0x40) { bochs_set_point(x+1,y+i,fg); } else { bochs_set_point(x+1,y+i,bg); }
|
|
|
|
if (c[i] & 0x20) { bochs_set_point(x+2,y+i,fg); } else { bochs_set_point(x+2,y+i,bg); }
|
|
|
|
if (c[i] & 0x10) { bochs_set_point(x+3,y+i,fg); } else { bochs_set_point(x+3,y+i,bg); }
|
|
|
|
if (c[i] & 0x08) { bochs_set_point(x+4,y+i,fg); } else { bochs_set_point(x+4,y+i,bg); }
|
|
|
|
if (c[i] & 0x04) { bochs_set_point(x+5,y+i,fg); } else { bochs_set_point(x+5,y+i,bg); }
|
|
|
|
if (c[i] & 0x02) { bochs_set_point(x+6,y+i,fg); } else { bochs_set_point(x+6,y+i,bg); }
|
|
|
|
if (c[i] & 0x01) { bochs_set_point(x+7,y+i,fg); } else { bochs_set_point(x+7,y+i,bg); }
|
|
|
|
}
|
2011-03-29 04:34:44 +04:00
|
|
|
__asm__ __volatile__ ("sti");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t bochs_colors[16] = {
|
|
|
|
/* black */ 0x000000,
|
|
|
|
/* red */ 0xcc0000,
|
|
|
|
/* green */ 0x3e9a06,
|
|
|
|
/* brown */ 0xc4a000,
|
|
|
|
/* navy */ 0x3465a4,
|
|
|
|
/* purple */ 0x75507b,
|
|
|
|
/* d cyan */ 0x06989a,
|
|
|
|
/* gray */ 0xeeeeec,
|
|
|
|
/* d gray */ 0x555753,
|
|
|
|
/* red */ 0xef2929,
|
|
|
|
/* green */ 0x8ae234,
|
|
|
|
/* yellow */ 0xfce94f,
|
|
|
|
/* blue */ 0x729fcf,
|
|
|
|
/* magenta*/ 0xad7fa8,
|
|
|
|
/* cyan */ 0x34e2e2,
|
|
|
|
/* white */ 0xFFFFFF
|
|
|
|
};
|
|
|
|
|
|
|
|
static void cell_set(uint16_t x, uint16_t y, uint8_t c, uint8_t fg, uint8_t bg) {
|
|
|
|
uint8_t * cell = (uint8_t *)((uintptr_t)term_buffer + (y * TERM_WIDTH + x) * 4);
|
|
|
|
cell[0] = c;
|
|
|
|
cell[1] = fg;
|
|
|
|
cell[2] = bg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t cell_ch(uint16_t x, uint16_t y) {
|
|
|
|
uint8_t * cell = (uint8_t *)((uintptr_t)term_buffer + (y * TERM_WIDTH + x) * 4);
|
|
|
|
return cell[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t cell_fg(uint16_t x, uint16_t y) {
|
|
|
|
uint8_t * cell = (uint8_t *)((uintptr_t)term_buffer + (y * TERM_WIDTH + x) * 4);
|
|
|
|
return cell[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t cell_bg(uint16_t x, uint16_t y) {
|
|
|
|
uint8_t * cell = (uint8_t *)((uintptr_t)term_buffer + (y * TERM_WIDTH + x) * 4);
|
|
|
|
return cell[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cell_redraw(uint16_t x, uint16_t y) {
|
|
|
|
uint8_t * cell = (uint8_t *)((uintptr_t)term_buffer + (y * TERM_WIDTH + x) * 4);
|
|
|
|
bochs_write_char(cell[0], x * 8, y * 12, bochs_colors[cell[1]], bochs_colors[cell[2]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bochs_redraw() {
|
|
|
|
for (uint16_t y = 0; y < TERM_HEIGHT; ++y) {
|
|
|
|
for (uint16_t x = 0; x < TERM_WIDTH; ++x) {
|
|
|
|
cell_redraw(x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void bochs_term_scroll() {
|
|
|
|
/* Oh dear */
|
|
|
|
bochs_scroll();
|
|
|
|
for (uint16_t y = 0; y < TERM_HEIGHT - 1; ++y) {
|
|
|
|
for (uint16_t x = 0; x < TERM_WIDTH; ++x) {
|
|
|
|
cell_set(x,y,cell_ch(x,y+1),cell_fg(x,y+1),cell_bg(x,y+1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (uint16_t x = 0; x < TERM_WIDTH; ++x) {
|
|
|
|
cell_set(x, TERM_HEIGHT-1,' ',current_fg, current_bg);
|
2011-03-29 06:34:02 +04:00
|
|
|
cell_redraw(x, TERM_HEIGHT-1);
|
2011-03-29 04:34:44 +04:00
|
|
|
}
|
|
|
|
//bochs_redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void bochs_term_clear() {
|
|
|
|
/* Oh dear */
|
2011-03-29 06:35:10 +04:00
|
|
|
csr_x = 0;
|
|
|
|
csr_y = 0;
|
2011-03-29 04:34:44 +04:00
|
|
|
for (uint16_t y = 0; y < TERM_HEIGHT; ++y) {
|
|
|
|
for (uint16_t x = 0; x < TERM_WIDTH; ++x) {
|
|
|
|
cell_set(x,y,' ',current_fg, current_bg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bochs_redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void bochs_set_colors(uint8_t fg, uint8_t bg) {
|
|
|
|
current_fg = fg;
|
|
|
|
current_bg = bg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bochs_reset_colors() {
|
|
|
|
current_fg = 7;
|
|
|
|
current_bg = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void draw_cursor() {
|
|
|
|
for (uint32_t x = 0; x < 8; ++x) {
|
|
|
|
bochs_set_point(csr_x * 8 + x, csr_y * 12 + 11, bochs_colors[current_fg]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bochs_write(char c) {
|
|
|
|
__asm__ __volatile__ ("cli");
|
|
|
|
cell_redraw(csr_x, csr_y);
|
|
|
|
if (c == '\n') {
|
|
|
|
for (uint16_t i = csr_x; i < TERM_WIDTH; ++i) {
|
|
|
|
/* I like this behaviour */
|
|
|
|
cell_set(i, csr_y, ' ',current_fg, current_bg);
|
|
|
|
cell_redraw(i, csr_y);
|
|
|
|
}
|
|
|
|
csr_x = 0;
|
|
|
|
++csr_y;
|
|
|
|
} else if (c == '\b') {
|
|
|
|
--csr_x;
|
|
|
|
cell_set(csr_x, csr_y, ' ',current_fg, current_bg);
|
|
|
|
cell_redraw(csr_x, csr_y);
|
|
|
|
} else {
|
|
|
|
cell_set(csr_x,csr_y, c, current_fg, current_bg);
|
|
|
|
cell_redraw(csr_x,csr_y);
|
|
|
|
csr_x++;
|
|
|
|
}
|
|
|
|
if (csr_x == TERM_WIDTH) {
|
|
|
|
csr_x = 0;
|
|
|
|
++csr_y;
|
|
|
|
}
|
|
|
|
if (csr_y == TERM_HEIGHT) {
|
|
|
|
bochs_term_scroll();
|
|
|
|
csr_y = TERM_HEIGHT - 1;
|
|
|
|
}
|
|
|
|
draw_cursor();
|
|
|
|
__asm__ __volatile__ ("sti");
|
2011-03-29 00:18:48 +04:00
|
|
|
}
|