diff options
author | Richard Braun <rbraun@sceen.net> | 2012-11-03 17:38:20 +0100 |
---|---|---|
committer | Richard Braun <rbraun@sceen.net> | 2012-11-03 17:38:20 +0100 |
commit | 8b82a8f0a8097accf4f95d9bbfc9b0baaaf6b20c (patch) | |
tree | e4daef1850b30fa4a200e3234b3a22e218d598ed | |
parent | 27484e07cdd0be148743cb8e4acd99e96264eca9 (diff) |
x86/vga: rename module to cga
-rw-r--r-- | arch/x86/Makefrag.am | 6 | ||||
-rw-r--r-- | arch/x86/machine/boot.c | 20 | ||||
-rw-r--r-- | arch/x86/machine/cga.c | 184 | ||||
-rw-r--r-- | arch/x86/machine/cga.h (renamed from arch/x86/machine/vga.h) | 15 | ||||
-rw-r--r-- | arch/x86/machine/vga.c | 184 |
5 files changed, 206 insertions, 203 deletions
diff --git a/arch/x86/Makefrag.am b/arch/x86/Makefrag.am index 6e61160c..b61ab9e9 100644 --- a/arch/x86/Makefrag.am +++ b/arch/x86/Makefrag.am @@ -9,6 +9,8 @@ x86_FILES = \ arch/x86/machine/boot.c \ arch/x86/machine/boot.h \ arch/x86/machine/config.h \ + arch/x86/machine/cga.c \ + arch/x86/machine/cga.h \ arch/x86/machine/cpu_asm.S \ arch/x86/machine/cpu.c \ arch/x86/machine/cpu.h \ @@ -27,9 +29,7 @@ x86_FILES = \ arch/x86/machine/trap_asm.S \ arch/x86/machine/trap.c \ arch/x86/machine/trap.h \ - arch/x86/machine/types.h \ - arch/x86/machine/vga.c \ - arch/x86/machine/vga.h + arch/x86/machine/types.h EXTRA_DIST += arch/x86/x15.lds.S $(x86_FILES) diff --git a/arch/x86/machine/boot.c b/arch/x86/machine/boot.c index 35fabb53..331d44ea 100644 --- a/arch/x86/machine/boot.c +++ b/arch/x86/machine/boot.c @@ -54,13 +54,13 @@ #include <kern/string.h> #include <machine/biosmem.h> #include <machine/boot.h> +#include <machine/cga.h> #include <machine/cpu.h> #include <machine/multiboot.h> #include <machine/pic.h> #include <machine/pit.h> #include <machine/pmap.h> #include <machine/trap.h> -#include <machine/vga.h> #include <vm/vm_kmem.h> #include <vm/vm_page.h> #include <vm/vm_phys.h> @@ -69,9 +69,9 @@ /* * Macros used by the very early panic function. */ -#define INIT_VGAMEM ((uint16_t *)0xb8000) -#define INIT_VGACHARS (80 * 25) -#define INIT_VGACOLOR 0x7 +#define INIT_CGAMEM ((uint16_t *)0xb8000) +#define INIT_CGACHARS (80 * 25) +#define INIT_CGACOLOR 0x7 char boot_stack[BOOT_STACK_SIZE] __aligned(DATA_ALIGN) __initdata; char boot_ap_stack[BOOT_STACK_SIZE] __aligned(DATA_ALIGN) __initdata; @@ -96,21 +96,21 @@ boot_panic(const char *msg) uint16_t *ptr, *end; const char *s; - ptr = INIT_VGAMEM; - end = ptr + INIT_VGACHARS; + ptr = INIT_CGAMEM; + end = ptr + INIT_CGACHARS; s = (void *)BOOT_VTOP((unsigned long)"panic: "); while ((ptr < end) && (*s != '\0')) - *ptr++ = (INIT_VGACOLOR << 8) | *s++; + *ptr++ = (INIT_CGACOLOR << 8) | *s++; s = BOOT_VTOP(msg); while ((ptr < end) && (*s != '\0')) - *ptr++ = (INIT_VGACOLOR << 8) | *s++; + *ptr++ = (INIT_CGACOLOR << 8) | *s++; while (ptr < end) - *ptr++ = (INIT_VGACOLOR << 8) | ' '; + *ptr++ = (INIT_CGACOLOR << 8) | ' '; cpu_halt(); @@ -269,7 +269,7 @@ boot_main(void) trap_setup(); cpu_setup(); pmap_bootstrap(); - vga_setup(); + cga_setup(); boot_show_version(); cpu_check(cpu_current()); cpu_info(cpu_current()); diff --git a/arch/x86/machine/cga.c b/arch/x86/machine/cga.c new file mode 100644 index 00000000..6e4ffc0d --- /dev/null +++ b/arch/x86/machine/cga.c @@ -0,0 +1,184 @@ +/* + * 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/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); + 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"); diff --git a/arch/x86/machine/vga.h b/arch/x86/machine/cga.h index 6e7c6043..0486fd96 100644 --- a/arch/x86/machine/vga.h +++ b/arch/x86/machine/cga.h @@ -13,21 +13,24 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * + * Tiny CGA driver. */ -#ifndef _X86_VGA_H -#define _X86_VGA_H +#ifndef _X86_CGA_H +#define _X86_CGA_H #include <kern/stdint.h> /* - * Initialize the vga module. + * Initialize the cga module. */ -void vga_setup(void); +void cga_setup(void); /* * Write a byte on the screen at current cursor position. */ -void vga_write_byte(uint8_t byte); +void cga_write_byte(uint8_t byte); -#endif /* _X86_VGA_H */ +#endif /* _X86_CGA_H */ diff --git a/arch/x86/machine/vga.c b/arch/x86/machine/vga.c deleted file mode 100644 index 08b72332..00000000 --- a/arch/x86/machine/vga.c +++ /dev/null @@ -1,184 +0,0 @@ -/* - * 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/stdint.h> -#include <kern/string.h> -#include <machine/io.h> -#include <machine/pmap.h> -#include <machine/vga.h> - -/* - * Screen dimensions. - */ -#define VGA_COLUMNS 80 -#define VGA_LINES 25 - -/* - * Text mode mapped memory and size. - */ -#define VGA_MEMORY 0xb8000 -#define VGA_MEMORY_SIZE (VGA_COLUMNS * VGA_LINES * 2) - -/* - * I/O ports. - */ -#define VGA_MISC_OUTPUT_REGISTER_READ 0x3cc -#define VGA_MISC_OUTPUT_REGISTER_WRITE 0x3c2 -#define VGA_CRTC_ADDRESS_REGISTER 0x3d4 -#define VGA_CRTC_DATA_REGISTER 0x3d5 - -/* - * CRTC registers. - */ -#define VGA_CRTC_CURSOR_LOCATION_HIGH_REGISTER 0xe -#define VGA_CRTC_CURSOR_LOCATION_LOW_REGISTER 0xf - -/* - * Foreground screen color. - */ -#define VGA_FOREGROUND_COLOR 0x7 - -/* - * Blank space 16 bits word. - */ -#define VGA_BLANK ((VGA_FOREGROUND_COLOR << 8) | ' ') - -/* - * Number of spaces to display for a tabulation. - */ -#define VGA_TABULATION_SPACES 8 - -static uint8_t *vga_memory; -static uint16_t vga_cursor; - -static uint16_t -vga_get_cursor_position(void) -{ - uint16_t tmp; - - io_write_byte(VGA_CRTC_ADDRESS_REGISTER, - VGA_CRTC_CURSOR_LOCATION_HIGH_REGISTER); - tmp = io_read_byte(VGA_CRTC_DATA_REGISTER) << 8; - io_write_byte(VGA_CRTC_ADDRESS_REGISTER, - VGA_CRTC_CURSOR_LOCATION_LOW_REGISTER); - tmp |= io_read_byte(VGA_CRTC_DATA_REGISTER); - - return tmp; -} - -static void -vga_set_cursor_position(uint16_t position) -{ - io_write_byte(VGA_CRTC_ADDRESS_REGISTER, - VGA_CRTC_CURSOR_LOCATION_HIGH_REGISTER); - io_write_byte(VGA_CRTC_DATA_REGISTER, position >> 8); - io_write_byte(VGA_CRTC_ADDRESS_REGISTER, - VGA_CRTC_CURSOR_LOCATION_LOW_REGISTER); - io_write_byte(VGA_CRTC_DATA_REGISTER, position & 0xff); -} - -static uint8_t -vga_get_cursor_column(void) -{ - return vga_cursor % VGA_COLUMNS; -} - -void __init -vga_setup(void) -{ - uint8_t misc_output_register; - unsigned long va; - - va = pmap_bootalloc(1); - pmap_kenter(va, VGA_MEMORY); - vga_memory = (uint8_t *)va; - - /* - * Check if the Input/Output Address Select bit is set. - */ - misc_output_register = io_read_byte(VGA_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(VGA_MISC_OUTPUT_REGISTER_WRITE, misc_output_register); - } - - vga_cursor = vga_get_cursor_position(); -} - -static void -vga_scroll_lines(void) -{ - uint16_t *last_line; - int i; - - memmove(vga_memory, (uint16_t *)vga_memory + VGA_COLUMNS, - VGA_MEMORY_SIZE - (VGA_COLUMNS * 2)); - last_line = (uint16_t *)vga_memory + (VGA_COLUMNS * (VGA_LINES - 1)); - - for(i = 0; i < VGA_COLUMNS; i++) - last_line[i] = VGA_BLANK; -} - -void -vga_write_byte(uint8_t byte) -{ - if (byte == '\r') - return; - else if (byte == '\n') { - vga_cursor += VGA_COLUMNS - vga_get_cursor_column(); - - if (vga_cursor >= (VGA_LINES * VGA_COLUMNS)) { - vga_scroll_lines(); - vga_cursor -= VGA_COLUMNS; - } - - vga_set_cursor_position(vga_cursor); - } else if (byte == '\b') { - if (vga_cursor > 0) { - vga_cursor--; - ((uint16_t *)vga_memory)[vga_cursor] = VGA_BLANK; - vga_set_cursor_position(vga_cursor); - } - } else if (byte == '\t') { - int i; - - for(i = 0; i < VGA_TABULATION_SPACES; i++) - vga_write_byte(' '); - } else { - if ((vga_cursor + 1) >= VGA_COLUMNS * VGA_LINES) { - vga_scroll_lines(); - vga_cursor -= VGA_COLUMNS; - } - - ((uint16_t *)vga_memory)[vga_cursor] = ((VGA_FOREGROUND_COLOR << 8) - | byte); - vga_cursor++; - vga_set_cursor_position(vga_cursor); - } -} - -void console_write_byte(char c) __alias("vga_write_byte"); |