summaryrefslogtreecommitdiffhomepage
path: root/Private/HALKit/AMD64/DebugOutput.cxx
blob: e23d9dae98d0c904af0af100964b937f78f1412b (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
/*
 *	========================================================
 *
 *	HCore
 * 	Copyright Mahrouss Logic, all rights reserved.
 *
 * 	========================================================
 */

#include <ArchKit/Arch.hpp>
#include <KernelKit/DebugOutput.hpp>
#include <NewKit/Utils.hpp>

namespace HCore {
enum CommStatus {
  kStateReady = 0xCF,
  kStateTransmit = 0xFC,
  kStateLimbo,
  kStateCnt = 3
};

namespace Detail {
constexpr short PORT = 0x3F8;

static int kState = kStateLimbo;

bool serial_init() {
#ifdef __DEBUG__
  HAL::out8(PORT + 1, 0x00);  // Disable all interrupts
  HAL::out8(PORT + 3, 0x80);  // Enable DLAB (set baud rate divisor)
  HAL::out8(PORT + 0, 0x03);  // Set divisor to 3 (lo byte) 38400 baud
  HAL::out8(PORT + 1, 0x00);  //                  (hi byte)
  HAL::out8(PORT + 3, 0x03);  // 8 bits, no parity, one stop bit
  HAL::out8(PORT + 2, 0xC7);  // Enable FIFO, clear them, with 14-byte threshold
  HAL::out8(PORT + 4, 0x0B);  // IRQs enabled, RTS/DSR set
  HAL::out8(PORT + 4, 0x1E);  // Set in loopback mode, test the serial chip
  HAL::out8(PORT + 0, 0xAE);  // Test serial chip (send byte 0xAE and check if
                              // serial returns same byte)

  // Check if serial is faulty (i.e: not same byte as sent)
  if (HAL::in8(PORT) != 0xAE) {
    ke_stop(RUNTIME_CHECK_HANDSHAKE);
  }

  kReady = kStateReady;

  // If serial is not faulty set it in normal operation mode
  // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
  HAL::out8(Detail::PORT + 4, 0x0F);
#endif

  return true;
}
}  // namespace Detail

void system_io_print(const char *bytes) {
  if (!bytes || Detail::kState != kStateReady) return;

  Detail::kState = kStateTransmit;

  SizeT index = 0;
  SizeT len = string_length(bytes, 256);

  while (index < len) {
    HAL::out8(Detail::PORT, bytes[index]);
    ++index;
  }

  Detail::kState = kStateReady;
}

TerminalDevice kcout(HCore::system_io_print, nullptr);
}  // namespace HCore