summaryrefslogtreecommitdiffhomepage
path: root/dev/Kernel/HALKit/AMD64/HalDebugOutput.cc
blob: d984e087ab68dbdad0ed6d08e23187daa44bf25a (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* -------------------------------------------

	Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved.

------------------------------------------- */

#include <ArchKit/ArchKit.h>
#include <KernelKit/DebugOutput.h>
#include <NewKit/Utils.h>
#include <NewKit/New.h>
#include <Mod/CoreGfx/FBMgr.h>
#include <Mod/CoreGfx/TextMgr.h>

namespace Kernel
{
	enum CommStatus
	{
		kStateInvalid,
		kStateReady	   = 0xCF,
		kStateTransmit = 0xFC,
		kStateCnt	   = 3
	};

	namespace Detail
	{
		constexpr Int16 kPort  = 0x3F8;
		static Int32	kState = kStateInvalid;

		/// @brief Init COM1.
		/// @return
		template <Int16 PORT>
		bool hal_serial_init() noexcept
		{
			if (kState == kStateReady || kState == kStateTransmit)
				return true;

			HAL::rt_out8(PORT + 1, 0x00); // Disable all interrupts
			HAL::rt_out8(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
			HAL::rt_out8(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
			HAL::rt_out8(PORT + 1, 0x00); //                  (hi byte)
			HAL::rt_out8(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
			HAL::rt_out8(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
			HAL::rt_out8(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
			HAL::rt_out8(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
			HAL::rt_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::rt_in8(PORT) != 0xAE)
			{
				ke_panic(RUNTIME_CHECK_HANDSHAKE);
			}

			kState = 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::rt_out8(PORT + 4, 0x0F);

			return true;
		}
	} // namespace Detail

	TerminalDevice::~TerminalDevice() = default;

	EXTERN_C void ke_io_write(const Char* bytes)
	{
#ifdef __DEBUG__
		Detail::hal_serial_init<Detail::kPort>();

		if (!bytes || Detail::kState != kStateReady)
			return;

		if (*bytes == 0)
			return;

		Detail::kState = kStateTransmit;

		SizeT index = 0;
		SizeT len	= 0;

		index = 0;
		len	  = rt_string_len(bytes, 255);

		static int x = kFontSizeX, y = kFontSizeY;
		auto	   log_txt = RGB(0xff, 0xff, 0xff);

		while (index < len)
		{
			if (bytes[index] == '\r')
				HAL::rt_out8(Detail::kPort, '\r');

			HAL::rt_out8(Detail::kPort, bytes[index] == '\r' ? '\n' : bytes[index]);

			char tmp_str[2];
			tmp_str[0] = bytes[index];
			tmp_str[1] = 0;

			fb_render_string(tmp_str, y, x, log_txt);

			if (bytes[index] == '\r')
			{
				y += kFontSizeY;
				x = kFontSizeX;
			}

			x += kFontSizeX;

			if (y > kHandoverHeader->f_GOP.f_Height)
			{
				y = kFontSizeY;
				FB::fb_clear_video();
			}

			++index;
		}

		Detail::kState = kStateReady;
#endif // __DEBUG__
	}

	EXTERN_C void ke_io_read(const Char* bytes)
	{
#ifdef __DEBUG__
		Detail::hal_serial_init<Detail::kPort>();

		if (!bytes || Detail::kState != kStateReady)
			return;

		Detail::kState = kStateTransmit;

		SizeT index = 0;

		///! TODO: Look on how to wait for the UART to complete.
		while (true)
		{
			auto in = HAL::rt_in8(Detail::kPort);

			///! If enter pressed then break.
			if (in == 0xD)
			{
				break;
			}

			if (in < '0' || in < 'A' || in < 'a')
			{
				if (in != '@' || in != '!' || in != '?' || in != '.' || in != '/' ||
					in != ':')
				{
					continue;
				}
			}

			((char*)bytes)[index] = in;

			++index;
		}

		((char*)bytes)[index] = 0;

		Detail::kState = kStateReady;
#endif // __DEBUG__
	}

	TerminalDevice TerminalDevice::The() noexcept
	{
		TerminalDevice out(Kernel::ke_io_write, Kernel::ke_io_read);
		return out;
	}

} // namespace Kernel