aboutsummaryrefslogtreecommitdiff
path: root/drivers/cga.cc
blob: 961d7e46b4377f00a7f396c59d3b86495f193647 (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
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
#include "cga.h"
#include <string.h>

static_assert(sizeof(CGA::Entry) == 2);

/* cga registers, see https://www.lowlevel.eu/wiki/Color_Graphics_Adapter */
constexpr uint8_t cursor_start = 10;   // 0xa
constexpr uint8_t cursor_end = 11;     // 0xb
constexpr uint8_t cursor_addr_h = 14;  // 0xe
constexpr uint8_t cursor_addr_l = 15;  // 0xf

/* cursor format
 * register | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
 *   start  |   | e | t |     scanline      |
 *   end    |           |     scanline      |
 * e = enable; t = timing
 *  e t | effect
 *  0 0 | no blinking
 *  0 1 | hide cursor
 *  1 0 | blink normally
 *  1 1 | blink fast
 */
constexpr uint8_t cursor_hide = 0b00100000;

CGA::CGA(Colour fg, Colour bg, uint32_t address) : colour_fg(fg), colour_bg(bg) {
  buffer = reinterpret_cast<Entry*>(address);

  // clear buffer
  for (size_t y = 0; y < max_rows; y++) {
    for (size_t x = 0; x < max_columns; x++) {
      const size_t index = y * max_columns + x;
      buffer[index].c = ' ';
      buffer[index].fg = colour_fg;
      buffer[index].bg = colour_bg;
    }
  }

  enable_cursor(14, 15);
  update_cursor();
}

void CGA::set_colour(Colour fg, Colour bg) {
  colour_fg = fg;
  colour_bg = bg;
  for (size_t y = 0; y < max_rows; y++) {
    for (size_t x = 0; x < max_columns; x++) {
      const size_t index = y * max_columns + x;
      buffer[index].fg = colour_fg;
      buffer[index].bg = colour_bg;
    }
  }
}

void CGA::enable_cursor(uint8_t start, uint8_t end) {
  p_idx.write(cursor_start);
  p_dat.write((p_dat.read() & 0xc0) | start);

  p_idx.write(cursor_end);
  p_dat.write((p_dat.read() & 0xe0) | end);
}
void CGA::disable_cursor() {
  p_idx.write(cursor_start);
  p_dat.write(cursor_hide);
}
void CGA::update_cursor() {
  const uint16_t pos = row * max_columns + column;

  p_idx.write(cursor_addr_l);
  p_dat.write(static_cast<uint8_t>(pos & 0xff));

  p_idx.write(cursor_addr_h);
  p_dat.write(static_cast<uint8_t>((pos >> 8) & 0xff));
}

void CGA::write(char c) {
  switch (c) {
    case '\n':
      column = 0;
      ++row;
      break;
    default: {
      const size_t index = row * max_columns + column;
      buffer[index].c = c;
      ++column;
    }
  }

  if (column == max_columns) {
    column = 0;
    ++row;
  }

  if (row == max_rows) {
    // scroll up - move rows 1~25 up by one
    for (size_t y = 1; y < max_rows; y++) {
      const auto prev_y = y - 1;
      for (size_t x = 0; x < max_columns; ++x) {
        const auto prev = prev_y * max_columns + x;
        const auto idx = y * max_columns + x;
        buffer[prev] = buffer[idx];
      }
    }

    // blank out last row
    for (size_t idx = (max_rows - 1) * max_columns; idx < max_rows * max_columns; ++idx) {
      buffer[idx].c = ' ';
    }
    --row;
  }
}

void CGA::write(ViewIterator& iter) {
  while (iter) {
    write(iter.next());
  }
}