~cypheon/xos

xos/console.c -rw-r--r-- 1.3 KiB
567db604 — Johann Rudloff Implement scanning and allocation of physical memory. 7 years ago
                                                                                
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
#include <sys/console.h>

void console_init(console_t *con) {
  con->base = (char *)0xb8000;
  con->attr = 0x07;
  con->col = 0;
  con->row = 0;
  con->num_cols = 80;
  con->num_rows = 25;
}

void console_clear(console_t *con) {
  char *p = con->base;
  for (int i=0;i<4000;i++) {
    *p++ = 0x00;
  }
  con->col = 0;
  con->row = 0;
}

void console_print(console_t *con, const char *str) {
  char *dst = con->base + 2*(con->row * con->num_cols + con->col);
  const char *txt = str;
  while (*txt != 0) {
    switch(*txt) {
      case '\n':
        con->col = 0;
        con->row++;
        if (con->row >= con->num_rows) {
          console_scroll(con);
          dst = con->base + 2*(con->row * con->num_cols + con->col);
        } else {
          dst = con->base + 2*(con->row * con->num_cols + con->col);
        }
        txt++;
        break;
      default:
        *dst++ = *txt++;
        *dst++ = con->attr;
        con->col++;
        if (con->col >= con->num_cols) {
          con->col = 0;
          con->row++;
        }
        break;
    }
  }
}

void console_scroll(console_t *con) {
  unsigned int *s = (unsigned int *)(con->base + con->num_cols * 2);
  unsigned int *d = (unsigned int *)(con->base);
  for (int i=0;i<960;i++) {
    *d++ = *s++;
  }
  for (int i=0;i<40;i++) {
    *d++ = 0;
  }
  con->row--;
}