aboutsummaryrefslogtreecommitdiff
path: root/drivers/ports.h
blob: 393fd4df69b5ca3c41e0963fb5efedc188216b54 (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
#pragma once

template <typename T>
concept port_data_t = (is_same<T, uint8_t>::value || is_same<T, uint16_t>::value || is_same<T, uint32_t>::value);

template <uint16_t port_num, port_data_t T>
class Port {
public:
  [[nodiscard]] T read(uint16_t offset = 0) {
    const uint16_t port = port_num + offset;
    T result;

    switch (sizeof(T)) {
      case 1:
        asm volatile("inb %1, %0" : "=a"(result) : "Nd"(port));
        break;
      case 2:
        asm volatile("inw %1, %0" : "=a"(result) : "Nd"(port));
        break;
      case 4:
        asm volatile("inl %1, %0" : "=a"(result) : "Nd"(port));
        break;
    }

    return result;
  }

  void write(T data, uint16_t offset = 0) {
    const uint16_t port = port_num + offset;
    switch (sizeof(T)) {
      case 1:
        asm volatile("outb %0, %1" : : "a"(data), "Nd"(port));
        break;
      case 2:
        asm volatile("outw %0, %1" : : "a"(data), "Nd"(port));
        break;
      case 4:
        asm volatile("outl %0, %1" : : "a"(data), "Nd"(port));
        break;
    }
  }
};

/* Ports 0x3d0 to 0x3df are reserved for CGA; each port is 1 byte wide */
typedef Port<0x3d4, uint8_t> cga_idx_port;
typedef Port<0x3d5, uint8_t> cga_dat_port;
// 0x3d8 : mode select register
// 0x3d8 : color control register
// 0x3da : status register

/* Serial ports */
typedef Port<0x3f8, uint8_t> com1_port_t;
typedef Port<0x2f8, uint8_t> com2_port_t;
typedef Port<0x3e8, uint8_t> com3_port_t;
typedef Port<0x2e8, uint8_t> com4_port_t;

/* 8259 PIC */
typedef Port<0x20, uint8_t> pic1_t;
typedef Port<0xa0, uint8_t> pic2_t;