77 lines
2.3 KiB
C++

#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)