summaryrefslogtreecommitdiff
path: root/src/kernel/vga.c
blob: f83a2cfb158cd89618b08ef165d4c31289a13853 (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright 2019 Benji Dial

Permission to use, copy, modify, and/or distribute this
software for any purpose with or without fee is hereby
granted, provided that the above copyright notice and this
permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS
ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE.
*/

#include "vga.h"
#include "serial.h"
#include <stdbool.h>

#define VGA_BUFFER ((uint8_t *)0x000b8000)
#define cols 80
#define rows 25
uint16_t cursor_pos = 0;
uint8_t color = 0x70;

void clear(void) {
  uint32_t fill = 0x00200020 | (color << 24) | (color << 8);
  for (uint32_t *i = (uint32_t *)VGA_BUFFER; i < (uint32_t *)(VGA_BUFFER + rows * cols * 2); ++i)
    *i = fill;
  cursor_pos = 0;
}

void scroll(void) {
  cursor_pos -= cols * 2;
  for (uint32_t *i = (uint32_t *)VGA_BUFFER; i < (uint32_t *)(VGA_BUFFER + (rows - 1) * cols * 2); ++i)
    *i = *(i + cols / 2);
  uint32_t fill = (color << 8) | (color << 24) | 0x00200020;
  for (uint32_t *i = (uint32_t *)(VGA_BUFFER + (rows - 1) * cols * 2); i < (uint32_t *)(VGA_BUFFER + rows * cols * 2); ++i)
    *i = fill;
}

void put_char(uint8_t ch) {
  send_byte(ch, COM2);
  switch (ch) {
  case '\b':
    if (cursor_pos)
      VGA_BUFFER[cursor_pos -= 2] = (uint8_t)' ';
    break;
  case '\t':
    while ((cursor_pos % (cols * 2)) % 20)
      put_char(' ');
    break;
  case '\n':
    if ((cursor_pos = (cursor_pos / (cols * 2) + 1) * (cols * 2)) >= cols * rows * 2)
      scroll();
    break;
  default:
    VGA_BUFFER[cursor_pos++] = ch;
    VGA_BUFFER[cursor_pos++] = color;
    if (cursor_pos == (cols * rows * 2))
      scroll();
  }
}

void put_sz(uint8_t *sz) {
  while (*sz)
    put_char(*(sz++));
}

void put_32_hex(uint32_t n) {
  for (uint8_t i = 0; i < 4; ++i) {
    put_char("0123456789abcdef"[n >> 28]);
    n <<= 4;
  }
  put_char('.');
  for (uint8_t i = 0; i < 4; ++i) {
    put_char("0123456789abcdef"[n >> 28]);
    n <<= 4;
  }
}

void put_16_hex(uint16_t n) {
  for (uint8_t i = 0; i < 4; ++i) {
    put_char("0123456789abcdef"[n >> 12]);
    n <<= 4;
  }
}

void put_8_hex(uint8_t n) {
  put_char("0123456789abcdef"[n >> 4]);
  put_char("0123456789abcdef"[n & 0x0f]);
}

void put_32_dec(uint32_t n) {
  if (n) {
    bool sig = false;
    for (uint32_t m = 1000000000; m; m /= 10) {
      if (((n / m) % 10) || sig) {
        sig = true;
        put_char((uint8_t)'0' + (n / m) % 10);
      }
    }
  }
  else
    put_char('0');
}

void put_16_dec(uint16_t n) {
  if (n) {
    bool sig = false;
    for (uint16_t m = 10000; m; m /= 10) {
      if (((n / m) % 10) || sig) {
        sig = true;
        put_char((uint8_t)'0' + (n / m) % 10);
      }
    }
  }
  else
    put_char('0');
}

void put_8_dec(uint8_t n) {
  if (n) {
    bool sig = false;
    for (uint8_t m = 100; m; m /= 10) {
      if (((n / m) % 10) || sig) {
        sig = true;
        put_char((uint8_t)'0' + (n / m) % 10);
      }
    }
  }
  else
    put_char('0');
}

void move_cursor(uint8_t col, uint8_t row) {
  cursor_pos = (col + row * cols) * 2;
}

void set_color(uint8_t c) {
  color = c;
}