Added basic GDT and (very) basic IDT. And serial output.
This commit is contained in:
76
targets/_any/include/os/serial.hpp
Normal file
76
targets/_any/include/os/serial.hpp
Normal file
@@ -0,0 +1,76 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(BAD_APPLE_OS_SERIAL_HPP_INCLUDED)
|
||||
#define BAD_APPLE_OS_SERIAL_HPP_INCLUDED
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
inline constexpr std::uint16_t PORT_COM1 = 0x3F8;
|
||||
inline constexpr std::uint16_t PORT_COM2 = 0x2F8;
|
||||
inline constexpr std::uint16_t PORT_COM3 = 0x3E8;
|
||||
inline constexpr std::uint16_t PORT_COM4 = 0x2E8;
|
||||
inline constexpr std::uint16_t PORT_COM5 = 0x5F8;
|
||||
inline constexpr std::uint16_t PORT_COM6 = 0x4F8;
|
||||
inline constexpr std::uint16_t PORT_COM7 = 0x5E8;
|
||||
inline constexpr std::uint16_t PORT_COM8 = 0x4E8;
|
||||
|
||||
inline void writePortByte(std::uint16_t port, std::uint8_t value) noexcept
|
||||
{
|
||||
__asm__ __volatile__(
|
||||
"outb %0, %1"
|
||||
:
|
||||
: "a"(value), "Nd"(port)
|
||||
);
|
||||
}
|
||||
|
||||
inline std::uint8_t readPortByte(std::uint16_t port) noexcept
|
||||
{
|
||||
std::uint8_t value = 0;
|
||||
__asm__ __volatile__(
|
||||
"inb %1, %0"
|
||||
: "=a"(value)
|
||||
: "Nd"(port)
|
||||
);
|
||||
return value;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool initSerialPort(std::uint16_t port) noexcept
|
||||
{
|
||||
writePortByte(port + 1, 0x00); // Disable all interrupts
|
||||
writePortByte(port + 3, 0x80); // Enable DLAB (set baud rate divisor)
|
||||
writePortByte(port + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
|
||||
writePortByte(port + 1, 0x00); // (hi byte)
|
||||
writePortByte(port + 3, 0x03); // 8 bits, no parity, one stop bit
|
||||
writePortByte(port + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
|
||||
writePortByte(port + 4, 0x0B); // IRQs enabled, RTS/DSR set
|
||||
writePortByte(port + 4, 0x1E); // Set in loopback mode, test the serial chip
|
||||
writePortByte(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(readPortByte(port + 0) != 0xAE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If serial is not faulty set it in normal operation mode
|
||||
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
|
||||
writePortByte(port + 4, 0x0F);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void serialWrite(std::uint16_t port, std::uint8_t data) noexcept
|
||||
{
|
||||
while ((readPortByte(port + 5) & 0x20) == 0);
|
||||
writePortByte(port, data);
|
||||
}
|
||||
|
||||
inline void serialWriteString(std::uint16_t port, const char* str) noexcept
|
||||
{
|
||||
while (*str)
|
||||
{
|
||||
serialWrite(port, *str);
|
||||
++str;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // !defined(BAD_APPLE_OS_SERIAL_HPP_INCLUDED)
|
||||
@@ -4,13 +4,15 @@
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <stdarg.h>
|
||||
#include "os/serial.hpp"
|
||||
#include "os/tty.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
namespace
|
||||
{
|
||||
int printInt(int value)
|
||||
|
||||
int printInt(long value)
|
||||
{
|
||||
if (value == 0)
|
||||
{
|
||||
@@ -23,7 +25,7 @@ int printInt(int value)
|
||||
return printInt(-value) + 1;
|
||||
}
|
||||
|
||||
char digits[10]; // 2147483647 has 10 digits
|
||||
char digits[19]; // 9223372036854775807 has 10 digits
|
||||
char* pos = &digits[0];
|
||||
|
||||
while (value > 0)
|
||||
@@ -131,13 +133,26 @@ int printByteSize(size_t bytes)
|
||||
int putchar(int chr) noexcept
|
||||
{
|
||||
tty::putChar(static_cast<char>(chr));
|
||||
if (chr == '\n')
|
||||
{
|
||||
serialWrite(PORT_COM1, '\r');
|
||||
serialWrite(PORT_COM1, '\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
serialWrite(PORT_COM1, static_cast<std::uint8_t>(chr));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int puts(const char* str) noexcept
|
||||
{
|
||||
tty::write(str);
|
||||
tty::putChar('\n');
|
||||
while (*str)
|
||||
{
|
||||
putchar(*str);
|
||||
++str;
|
||||
}
|
||||
putchar('\n');
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -191,6 +206,23 @@ int vprintf(const char* format, va_list vlist) BA_CXX_NOEXCEPT
|
||||
case 'X':
|
||||
result += printHexInt(va_arg(vlist, unsigned));
|
||||
break;
|
||||
case 'l':
|
||||
++pos;
|
||||
switch (*pos)
|
||||
{
|
||||
case '\0':
|
||||
assert(!"Invalid format string, ends with %l.");
|
||||
return -1;
|
||||
case 'd':
|
||||
result += printInt(va_arg(vlist, long));
|
||||
break;
|
||||
case 'X':
|
||||
result += printHexInt(va_arg(vlist, unsigned long));
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
assert(!"Invalid format string, unknown format identifier.");
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user