aboutsummaryrefslogtreecommitdiff
path: root/drivers/cga.h
blob: f4dfb3c10b2f16873444225a0a7d0318d1631fbe (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
#pragma once
#include <stdlib.h>
#include "ports.h"

/*
 * CGA (Colour Graphics Adapter)
 *
 * useful links:
 * https://www.seasip.info/VintagePC/cga.html
 * https://www.lowlevel.eu/wiki/Color_Graphics_Adapter
 *
 * TODO: switching between modes
 * TODO: cursor styling
 */

class CGA : public Console {
public:
  /* Hardware text mode colour constants. */
  enum Colour : uint8_t {
    CGA_COLOR_BLACK = 0,
    CGA_COLOR_BLUE = 1,
    CGA_COLOR_GREEN = 2,
    CGA_COLOR_CYAN = 3,
    CGA_COLOR_RED = 4,
    CGA_COLOR_MAGENTA = 5,
    CGA_COLOR_BROWN = 6,
    CGA_COLOR_LIGHT_GREY = 7,
    CGA_COLOR_DARK_GREY = 8,
    CGA_COLOR_LIGHT_BLUE = 9,
    CGA_COLOR_LIGHT_GREEN = 10,
    CGA_COLOR_LIGHT_CYAN = 11,
    CGA_COLOR_LIGHT_RED = 12,
    CGA_COLOR_LIGHT_MAGENTA = 13,
    CGA_COLOR_LIGHT_BROWN = 14,
    CGA_COLOR_WHITE = 15,
  };

  CGA();
  ~CGA() = default;

  void set_colour(Colour fg, Colour bg);

  void enable_cursor(uint8_t start, uint8_t end);
  void disable_cursor();
  void update_cursor() override;

  void write(char c) override;
  void write(ViewIterator& iter) override;


  struct Entry {
    char c;
    Colour fg : 4;
    Colour bg : 4;
  } __attribute((packed));

private:
  const size_t max_columns = 80, max_rows = 25;
  size_t column = 0, row = 0;

  Colour colour_fg = CGA_COLOR_BLACK;
  Colour colour_bg = CGA_COLOR_LIGHT_GREY;

  Entry* buffer;

  // ports
  cga_idx_port p_idx;
  cga_dat_port p_dat;
};