diff --git a/paw/stos/arch/amd64/Makefile b/paw/stos/arch/amd64/Makefile index d7fc37f..c597d1c 100644 --- a/paw/stos/arch/amd64/Makefile +++ b/paw/stos/arch/amd64/Makefile @@ -12,23 +12,34 @@ include ../../../mk/stos.mk ASMFILES = $(shell find . -name "*.S") ASMOFILES = $(ASMFILES:.S=.S.o) +CFILES = $(shell find . -name "*.c") +OFILES = $(CFILES:.c=.o) + MISC_OFILES = $(ASMOFILES) +MISC_OFILES += $(OFILES) MISC_OFILES += $(shell find $(ST_PROJECT_ROOT)/paw/spkg/ -name "*.o") MISC_OFILES += $(shell find $(ST_PROJECT_ROOT)/paw/stos/ -name "*.o" | grep -v "arch") KERNEL_PATH = \ $(ST_PROJECT_ROOT)/artifacts/stoskrnl.sys -CFLAGS = \ - $(SYS_CFLAGS) +CFLAGS = \ + $(SYS_CFLAGS) \ + -I../../target \ + -I../../head/ \ + -I$(ST_PROJECT_ROOT)/sdk/head .PHONY: all all: bin .PHONY: bin -bin: $(ASMOFILES) +bin: $(OFILES) $(ASMOFILES) $(PROMPT) "LD" $(KERNEL_PATH) $(SYS_LD) -Tdevel/link.ld $(MISC_OFILES) -o $(KERNEL_PATH) +%.o: %.c + $(PROMPT) "CC" $< + $(SYS_CC) -c $< $(CFLAGS) -o $@ + %.S.o: %.S $(PROMPT) "AS" $< $(SYS_CC) -c $< $(CFLAGS) -o $@ diff --git a/paw/stos/arch/amd64/platform/uart.c b/paw/stos/arch/amd64/platform/uart.c new file mode 100644 index 0000000..31abc46 --- /dev/null +++ b/paw/stos/arch/amd64/platform/uart.c @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: serial interface + * Author: Chloe M. + */ + +#include +#include +#include + +/* Channel port numbers */ +#define UART_COM1 0x3F8 +#define UART_COM2 0x2F8 +#define UART_COM3 0x3E8 +#define UART_COM4 0x2E8 +#define UART_COM5 0x5F8 +#define UART_COM6 0x4F8 +#define UART_COM7 0x5E8 +#define UART_COM8 0x4E8 + +/* Register offsets */ +#define UART_REG(OFFSET) (UART_COM1 + OFFSET) +#define UART_REG_FCR UART_REG(2) /* FIFO Control Register */ +#define UART_REG_LCR UART_REG(3) /* Line Control Register */ +#define UART_REG_MCR UART_REG(4) /* MODEM Control Register */ +#define UART_REG_LSR UART_REG(5) /* Line Status Register */ +#define UART_REG_MSR UART_REG(6) /* MODEM Status Register */ +#define UART_REG_SR UART_REG(7) /* Scratch Register */ + +/* Registers when LCR.DLAB=0 */ +#define UART_REG_RBR UART_REG(0) /* Receiver Buffer Register */ +#define UART_REG_THR UART_REG(0) /* Transmitter Holding Register */ +#define UART_REG_IER UART_REG(1) /* Interrupt Enable Register */ + +/* Registers when LCR.DLAB=1 */ +#define UART_REG_DLL UART_REG(0) /* Divisor Latch Low */ +#define UART_REG_DLH UART_REG(1) /* Divisor Latch High */ + +#define UART_LCR_WLS0 BIT(0) /* Word Length Select Bit 0 */ +#define UART_LCR_WLS1 BIT(1) /* Word Length Select Bit 1 */ +#define UART_LCR_DLAB BIT(7) /* Divisor Latch Access Bit*/ + +#define UART_MCR_DTR BIT(0) /* Data Terminal Ready*/ +#define UART_MCR_LOOP BIT(4) /* Loop */ + +#define UART_LSR_THRE BIT(5) /* Transmitter Holding Register */ + +#define UART_DIVISOR(RATE) (115200 / RATE) + +static inline BOOLEAN +UartTxEmpty(VOID) +{ + return ISSET(UART_REG_LSR, UART_LSR_THRE) != 0; +} + +VOID +HalSerialWrite(const CHAR *Data, USIZE Length) +{ + if (Data == NULL) { + return; + } + + while (!UartTxEmpty()) { + ASMV("pause"); + } + + for (USIZE Index = 0; Index < Length; ++Index) { + MdOutb(UART_REG_THR, Data[Index]); + } +} + +ST_STATUS +HalSerialInit(VOID) +{ + /* Disable interrupts */ + MdOutb(UART_REG_IER, 0x00); + + /* Set DLAB to set baud rate */ + MdOutb(UART_REG_LCR, UART_LCR_DLAB); + + /* Set speed to 57600 baud */ + MdOutb(UART_REG_DLL, UART_DIVISOR(57600)); + MdOutb(UART_REG_IER, 0x00); + + /* Set word size to 8 bits and clear DLAB */ + MdOutb(UART_REG_LCR, UART_LCR_WLS0 | UART_LCR_WLS1); + + /* Disable FIFOs for now... (TODO: Use them) */ + MdOutb(UART_REG_FCR, 0x00); + + /* Test chip in loopback mode */ + MdOutb(UART_REG_MCR, UART_MCR_LOOP); + MdOutb(UART_REG_THR, 0xF0); + if (MdInb(UART_REG_RBR != 0xF0)) + return -1; + + /* + * Mark the data terminal ready and clear + * loopback mode. + */ + MdOutb(UART_REG_MCR, UART_MCR_DTR); + return 0; +} diff --git a/paw/stos/head/ex/trace.h b/paw/stos/head/ex/trace.h new file mode 100644 index 0000000..d23a2ab --- /dev/null +++ b/paw/stos/head/ex/trace.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: Trace facility + * Author: Chloe M. + */ + +#ifndef _EX_TRACE_H_ +#define _EX_TRACE_H_ 1 + +#include + +#define TRACE printf +#define FmtPrintf vsnprintf + +#endif /* !_EX_TRACE_H_ */ diff --git a/paw/stos/head/hal/serial.h b/paw/stos/head/hal/serial.h new file mode 100644 index 0000000..b2bddfc --- /dev/null +++ b/paw/stos/head/hal/serial.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: HAL serial interface + * Author: Chloe M. + */ + +#ifndef _HAL_SERIAL_H_ +#define _HAL_SERIAL_H_ 1 + +#include + +/* + * Initialize the serial driver + */ +ST_STATUS HalSerialInit(VOID); + +/* + * Transmit data over serial + * + * @Data: Data to transmit + * @Length: Length of data to transmit + */ +VOID HalSerialWrite(const CHAR *Data, USIZE Length); + +#endif /* !_HAL_SERIAL_H_ */ diff --git a/paw/stos/init/init.c b/paw/stos/init/init.c index 4b15bf9..0297435 100644 --- a/paw/stos/init/init.c +++ b/paw/stos/init/init.c @@ -6,9 +6,26 @@ * Author: Chloe M. */ +#include +#include #include +/* + * Display a boot banner + */ +static VOID +BootBanner(VOID) +{ + TRACE("Machine has been turned on~\n"); + TRACE("Booting SystemPaw3 !! <3\n"); +} + VOID KiKernelEntry(VOID) { + /* Initialize the serial driver */ + HalSerialInit(); + + /* Write the boot banner */ + BootBanner(); } diff --git a/paw/stos/lib/_printf.c b/paw/stos/lib/_printf.c index 1cbb568..3f05fe6 100644 --- a/paw/stos/lib/_printf.c +++ b/paw/stos/lib/_printf.c @@ -6,7 +6,10 @@ * Author: Chloe M. */ +#include + void _putchar(char c) { + HalSerialWrite(&c, 1); }