148 lines
3.6 KiB
C
148 lines
3.6 KiB
C
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <stdarg.h>
|
|
#include <limits.h>
|
|
#include <stdbool.h>
|
|
|
|
#define PORT 0x3f8 // COM1
|
|
|
|
static int init_serial() {
|
|
outb(PORT + 1, 0x00); // Disable all interrupts
|
|
outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
|
|
outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
|
|
outb(PORT + 1, 0x00); // (hi byte)
|
|
outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
|
|
outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
|
|
outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
|
|
outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
|
|
outb(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(inb(PORT + 0) != 0xAE) {
|
|
return 1;
|
|
}
|
|
|
|
// If serial is not faulty set it in normal operation mode
|
|
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
|
|
outb(PORT + 4, 0x0F);
|
|
return 0;
|
|
}
|
|
|
|
int serial_received() {
|
|
return inb(PORT + 5) & 1;
|
|
}
|
|
|
|
char read_serial() {
|
|
while (serial_received() == 0);
|
|
|
|
return inb(PORT);
|
|
}
|
|
|
|
int is_transmit_empty() {
|
|
return inb(PORT + 5) & 0x20;
|
|
}
|
|
|
|
void write_serial(char a) {
|
|
while (is_transmit_empty() == 0);
|
|
|
|
outb(PORT, a);
|
|
}
|
|
|
|
static bool print(const char* data, size_t length) {
|
|
const unsigned char* bytes = (const unsigned char*) data;
|
|
for (size_t i = 0; i < length; i++) {
|
|
if (bytes[i] == '\n') {
|
|
write_serial('\r');
|
|
write_serial('\n');
|
|
} else {
|
|
write_serial(bytes[i]);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int serial_printf(const char* restrict format, ...) {
|
|
va_list parameters;
|
|
va_start(parameters, format);
|
|
|
|
int written = 0;
|
|
|
|
while (*format != '\0') {
|
|
size_t maxrem = INT_MAX - written;
|
|
|
|
if (format[0] != '%' || format[1] == '%') {
|
|
if (format[0] == '%')
|
|
format++;
|
|
size_t amount = 1;
|
|
while (format[amount] && format[amount] != '%')
|
|
amount++;
|
|
if (maxrem < amount) {
|
|
// TODO: Set errno to EOVERFLOW.
|
|
return -1;
|
|
}
|
|
if (!print(format, amount))
|
|
return -1;
|
|
format += amount;
|
|
written += amount;
|
|
continue;
|
|
}
|
|
|
|
const char* format_begun_at = format++;
|
|
|
|
if (*format == 'c') {
|
|
format++;
|
|
char c = (char) va_arg(parameters, int /* char promotes to int */);
|
|
if (!maxrem) {
|
|
// TODO: Set errno to EOVERFLOW.
|
|
return -1;
|
|
}
|
|
if (!print(&c, sizeof(c)))
|
|
return -1;
|
|
written++;
|
|
} else if (*format == 's') {
|
|
format++;
|
|
const char* str = va_arg(parameters, const char*);
|
|
size_t len = strlen(str);
|
|
if (maxrem < len) {
|
|
// TODO: Set errno to EOVERFLOW.
|
|
return -1;
|
|
}
|
|
if (!print(str, len))
|
|
return -1;
|
|
written += len;
|
|
} else {
|
|
format = format_begun_at;
|
|
size_t len = strlen(format);
|
|
if (maxrem < len) {
|
|
// TODO: Set errno to EOVERFLOW.
|
|
return -1;
|
|
}
|
|
if (!print(format, len))
|
|
return -1;
|
|
written += len;
|
|
format += len;
|
|
}
|
|
}
|
|
|
|
va_end(parameters);
|
|
return written;
|
|
}
|
|
|
|
inline void outb(uint16_t port, uint8_t val)
|
|
{
|
|
asm volatile ( "outb %0, %1" : : "a"(val), "Nd"(port) );
|
|
/* There's an outb %al, $imm8 encoding, for compile-time constant port numbers that fit in 8b. (N constraint).
|
|
* Wider immediate constants would be truncated at assemble-time (e.g. "i" constraint).
|
|
* The outb %al, %dx encoding is the only option for all other cases.
|
|
* %1 expands to %dx because port is a uint16_t. %w1 could be used if we had the port number a wider C type */
|
|
}
|
|
|
|
inline int inb(int port)
|
|
{
|
|
uint8_t ret;
|
|
asm volatile ( "inb %1, %0"
|
|
: "=a"(ret)
|
|
: "Nd"(port) );
|
|
return ret;
|
|
}
|