59 lines
1.9 KiB
C++

#pragma once
#if !defined(BAD_APPLE_OS_SERIAL_HPP_INCLUDED)
#define BAD_APPLE_OS_SERIAL_HPP_INCLUDED
#include <cstdint>
#include "./port.hpp"
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;
[[nodiscard]] inline bool initSerialPort(std::uint16_t port) noexcept
{
writePort8(port + 1, 0x00); // Disable all interrupts
writePort8(port + 3, 0x80); // Enable DLAB (set baud rate divisor)
writePort8(port + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
writePort8(port + 1, 0x00); // (hi byte)
writePort8(port + 3, 0x03); // 8 bits, no parity, one stop bit
writePort8(port + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
writePort8(port + 4, 0x0B); // IRQs enabled, RTS/DSR set
writePort8(port + 4, 0x1E); // Set in loopback mode, test the serial chip
writePort8(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(readPort8(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)
writePort8(port + 4, 0x0F);
return true;
}
inline void serialWrite(std::uint16_t port, std::uint8_t data) noexcept
{
while ((readPort8(port + 5) & 0x20) == 0);
writePort8(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)