aboutsummaryrefslogtreecommitdiff
path: root/kernel/io/vga/text_mode_display.c
blob: db0e0289b7395b5c0dd9417e85a0510064bc4425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "colours.h"

#define COLS (80 * 2) // 2 bytes per char
#define ROWS 25
#define FRAME_SIZE (ROWS * COLS)

char *video_ram = (char *) 0xB8000;
int cursor_pos = 0;
int char_attribute_byte = 0x07;

void vga_clear_screen()
{
  for (int i = 0; i < FRAME_SIZE; i = i + 2)
  {
    video_ram[i] = ' ';
    video_ram[i + 1] = char_attribute_byte;
  };
  cursor_pos = 0;
}

void vga_print(char *msg)
{
  int j = 0;
  while (msg[j] != '\0')
  {
    video_ram[cursor_pos++] = msg[j];
    video_ram[cursor_pos++] = char_attribute_byte;
    ++j;
  }
}

void vga_print_ln(char *msg)
{
  vga_print(msg);
  int current_line = cursor_pos / COLS;
  cursor_pos = (current_line + 1) * COLS;
}

void vga_set_text_colour(int foreground, int background) 
{
  char_attribute_byte = (background << 4) | foreground;
}

void vga_print_raw(unsigned char byte)
{
  video_ram[cursor_pos++] = byte;
  video_ram[cursor_pos++] = char_attribute_byte;
}

// TODO - jump cursor to prev non 0 text char rather than reversing through whole array
void vga_backspace()
{
  if (cursor_pos != 0) 
  {
    video_ram[--cursor_pos] = char_attribute_byte;
    video_ram[--cursor_pos] = 0;
  }
}

void vga_newline()
{
  int current_line = cursor_pos / COLS;
  cursor_pos = (current_line + 1) * COLS;
}