From 08ab6eb8e5be78d6362fccb95cdf825cd977d934 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Mon, 1 May 2017 19:07:34 +0200 Subject: write vga strings and some cursor handling --- src/Makefile | 2 +- src/kernel.c | 22 ++++++++++---------- src/stage1_functions.asm | 2 +- src/string.c | 11 +++++++++- src/string.h | 1 + src/vga.c | 53 +++++++++++++++++++++++++++++++++++++++++------- src/vga.h | 7 ++++--- 7 files changed, 74 insertions(+), 24 deletions(-) diff --git a/src/Makefile b/src/Makefile index 0d9cc1e..ec9e466 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,7 +8,7 @@ image.bin: boot.bin kernel.bin cat boot.bin kernel.bin > image.bin # truncate to correct number of sectors, we have # 512 (boot, first stage) + N * 512 (N currenty is 3) + M * 512 (M is currently 3) - truncate -s 3584 image.bin + truncate -s 4096 image.bin boot.bin: boot.asm gdt.asm stage1_functions.asm stage2_functions.asm switch_mode.asm nasm boot.asm -f bin -o boot.bin diff --git a/src/kernel.c b/src/kernel.c index bdca5e7..1692259 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -9,26 +9,26 @@ void entry( void ) vga_set_color( &vga, VGA_COLOR_LIGHT_GREY ); vga_set_background_color( &vga, VGA_COLOR_BLACK ); + // again volatile? const results in garbage on clang + volatile char msg[] = "ABAOS"; + vga_put_string_at( &vga, 0, 0, (const char *)msg ); + //~ vga_put_string_at( &vga, 0, 0, "ABAOS" ); - vga_put_char( &vga, 0, 0, 'A' ); - vga_put_char( &vga, 1, 0, 'B' ); - vga_put_char( &vga, 2, 0, 'A' ); - vga_put_char( &vga, 3, 0, 'O' ); - vga_put_char( &vga, 4, 0, 'S' ); - - volatile uint8_t bar[] = "\\|/-"; + // clang 4.0.0 needs volatile otherwise it takes a random value + // from the register in the second vga_put_char_at below??! + volatile char bar[] = "\\|/-"; int pos = 5; int i = 0; for( i = 0; i < 10000; i++ ) { if( i % 1000 == 0 ) { - vga_put_char( &vga, pos, 0, '.' ); + vga_put_char_at( &vga, pos, 0, '.' ); pos++; } - vga_put_char( &vga, pos, 0, bar[i%4] ); - for( int j = 0; j < 10000; j++ ) { + vga_put_char_at( &vga, pos, 0, bar[i%4] ); + for( int j = 0; j < 3000; j++ ) { } } - vga_put_char( &vga, pos, 0, '.' ); + vga_put_char_at( &vga, pos, 0, '.' ); vga_set_color( &vga, VGA_COLOR_WHITE ); vga_set_background_color( &vga, VGA_COLOR_RED ); diff --git a/src/stage1_functions.asm b/src/stage1_functions.asm index 575a465..b32d23d 100644 --- a/src/stage1_functions.asm +++ b/src/stage1_functions.asm @@ -3,7 +3,7 @@ ; 3 * 512 for bootloader stage2 and the kernel code ; (note: the first sector gets loaded by the BIOS, so ; subtract 1 here!) -NOF_LOAD_SECTORS equ 6 +NOF_LOAD_SECTORS equ 7 ; IN bx: begin of memory area to dump ; IN ax: number of words to dump diff --git a/src/string.c b/src/string.c index 30ccba6..07042ce 100644 --- a/src/string.c +++ b/src/string.c @@ -3,9 +3,18 @@ void *memset( void *s, int c, size_t n ) { for( size_t i = 0; i < n; i++ ) { - ((uint8_t *)s)[i] = c; + ((char *)s)[i] = c; } return s; } +size_t strlen( const char *s ) +{ + size_t len; + const char *p = s; + + for( len = 0; *p; len++, p++ ); + + return len; +} diff --git a/src/string.h b/src/string.h index c380155..b82d1aa 100644 --- a/src/string.h +++ b/src/string.h @@ -4,5 +4,6 @@ #include "sys/types.h" void *memset( void *s, int c, size_t n ); +size_t strlen( const char *s ); #endif /* STRING_H */ diff --git a/src/vga.c b/src/vga.c index 767d45f..0133ddf 100644 --- a/src/vga.c +++ b/src/vga.c @@ -1,7 +1,10 @@ #include "vga.h" +#include + #include "string.h" +static bool params_ok( vga_t *vga, const int x, const int y ); static int calculate_offset( vga_t *vga ); static uint8_t calculate_color_cell( vga_t *vga ); @@ -32,6 +35,9 @@ void vga_clear_screen( vga_t *vga ) void vga_set_cursor( vga_t *vga, const int x, const int y ) { + // TODO: PANIC? OUT OF BOUND ACCESS + if( !params_ok( vga, x, y ) ) return; + vga->cursor_x = x; vga->cursor_y = y; } @@ -62,16 +68,49 @@ static uint8_t calculate_color_cell( vga_t *vga ) return cell; } -void vga_put_char( vga_t *vga, const int x, const int y, const uint8_t c ) +static bool params_ok( vga_t *vga, const int x, const int y ) +{ + if( x > vga->res_x || y > vga->res_y ) return false; + + return true; +} + +void vga_put_char_at( vga_t *vga, const int x, const int y, const char c ) { - // TODO: PANIC? OUT OF BOUND ACCESS - if( x > vga->res_x || y > vga->res_y ) return; - vga_set_cursor( vga, x, y ); - + vga_put_char( vga, c ); +} + + +void vga_put_string_at( vga_t *vga, const int x, const int y, const char *s ) +{ + vga_set_cursor( vga, x, y ); + vga_put_string( vga, s ); +} + +void vga_put_char( vga_t *vga, const char c ) +{ int offset = calculate_offset( vga ); volatile uint8_t *VIDEO_MEMORY = (uint8_t *)0xb8000; - *(VIDEO_MEMORY+offset) = c; - *(VIDEO_MEMORY+offset+1) = calculate_color_cell( vga ); + *(VIDEO_MEMORY+offset) = (uint8_t)c; + *(VIDEO_MEMORY+offset+1) = calculate_color_cell( vga ); + + vga->cursor_x++; + if( vga->cursor_x >= vga->res_x ) { + vga->cursor_x = 0; + vga->cursor_y++; + if( vga->cursor_y >= vga->res_y ) { + // TODO: implement scrolling + vga->res_y = 0; + } + } +} + +void vga_put_string( vga_t *vga, const char *s ) +{ + for( size_t i = 0; i < strlen( s ); i++ ) { + vga_put_char( vga, s[i] ); + } } + diff --git a/src/vga.h b/src/vga.h index 8aef4e6..9664e2c 100644 --- a/src/vga.h +++ b/src/vga.h @@ -1,8 +1,6 @@ #ifndef VGA_H #define VGA_H -#include "sys/types.h" - enum { VGA_DEFAULT_RES_X = 80, VGA_DEFAULT_RES_Y = 25 @@ -41,6 +39,9 @@ void vga_clear_screen( vga_t *vga ); void vga_set_cursor( vga_t *vga, const int x, const int y ); void vga_set_color( vga_t *vga, const vga_color_t color ); void vga_set_background_color( vga_t *vga, const vga_color_t color ); -void vga_put_char( vga_t *vga, const int x, const int y, const uint8_t c ); +void vga_put_char_at( vga_t *vga, const int x, const int y, const char c ); +void vga_put_string_at( vga_t *vga, const int x, const int y, const char *s ); +void vga_put_char( vga_t *vga, const char c ); +void vga_put_string( vga_t *vga, const char *s ); #endif /* VGA_H */ -- cgit v1.2.3-54-g00ecf