aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-11-08 03:53:30 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-11-08 03:53:30 +0800
commit3f0691017ebc861c734a8bb589a11ec4da7d9d95 (patch)
tree8c02cd21cc90f393738677097880fe305e02099d
parent9b141251ebfc6ea4dfd9f6be99bbbaa473c79e2c (diff)
downloadrcc-project-3f0691017ebc861c734a8bb589a11ec4da7d9d95.tar.gz
rcc-project-3f0691017ebc861c734a8bb589a11ec4da7d9d95.tar.xz
[mubios] Implement the COM1 serial port driver and POST output.
Signed-off-by: Richard Qin <richard.qin.x@gmail.com>
-rw-r--r--mubios/core/post.c5
-rw-r--r--mubios/drivers/serial.c99
-rw-r--r--mubios/include/bios.h4
-rw-r--r--mubios/include/io.h43
4 files changed, 151 insertions, 0 deletions
diff --git a/mubios/core/post.c b/mubios/core/post.c
index ec0ddbd..69ad7e7 100644
--- a/mubios/core/post.c
+++ b/mubios/core/post.c
@@ -24,6 +24,11 @@
void
post_main (void)
{
+ serial_init ();
+ serial_write ("mubios: POST start\r\n");
+
memory_init ();
bda_init ();
+
+ serial_write ("mubios: memory initialized\r\n");
}
diff --git a/mubios/drivers/serial.c b/mubios/drivers/serial.c
new file mode 100644
index 0000000..3da2093
--- /dev/null
+++ b/mubios/drivers/serial.c
@@ -0,0 +1,99 @@
+/*
+ RCC-Project
+
+ Copyright (C) 2026 Richard Qin
+
+ This file is part of RCC-Project.
+
+ RCC-Project is free software; you can redistribute it and/or modify it under
+ the terms of the GNU General Public License as published by the Free
+ Software Foundation; either version 3 of the License, or (at your option) any
+ later version.
+
+ RCC-Project is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with
+ RCC-Project; see the file COPYING. If not see
+ <http://www.gnu.org/licenses/>.
+ */
+
+#include "bios.h"
+#include "io.h"
+
+#define COM1_BASE 0x03F8u
+
+#define COM_DATA_OFFSET 0x00u
+#define COM_IER_OFFSET 0x01u
+#define COM_FCR_OFFSET 0x02u
+#define COM_LCR_OFFSET 0x03u
+#define COM_MCR_OFFSET 0x04u
+#define COM_LSR_OFFSET 0x05u
+
+#define COM_LCR_DLAB 0x80u
+#define COM_LCR_8N1 0x03u
+
+#define COM_FCR_ENABLE_FIFO 0x01u
+#define COM_FCR_CLEAR_RX 0x02u
+#define COM_FCR_CLEAR_TX 0x04u
+#define COM_FCR_TRIGGER_14 0xC0u
+
+#define COM_MCR_DTR 0x01u
+#define COM_MCR_RTS 0x02u
+#define COM_MCR_OUT2 0x08u
+
+#define COM_LSR_THRE 0x20u
+
+static u16
+com1_port (u16 offset)
+{
+ return (u16)(COM1_BASE + offset);
+}
+
+void
+serial_init (void)
+{
+ // clang-format off
+ io_out8(com1_port(COM_IER_OFFSET), 0x00u);
+
+ io_out8(com1_port(COM_LCR_OFFSET), COM_LCR_DLAB);
+ io_out8(com1_port(COM_DATA_OFFSET), 0x01u);
+ io_out8(com1_port(COM_IER_OFFSET), 0x00u);
+
+ io_out8(com1_port(COM_LCR_OFFSET), COM_LCR_8N1);
+ io_out8(
+ com1_port(COM_FCR_OFFSET),
+ COM_FCR_ENABLE_FIFO |
+ COM_FCR_CLEAR_RX |
+ COM_FCR_CLEAR_TX |
+ COM_FCR_TRIGGER_14
+ );
+ io_out8(
+ com1_port(COM_MCR_OFFSET),
+ COM_MCR_DTR |
+ COM_MCR_RTS |
+ COM_MCR_OUT2
+ );
+ // clang-format on
+}
+
+void
+serial_putc (u8 value)
+{
+ while ((io_in8 (com1_port (COM_LSR_OFFSET)) & COM_LSR_THRE) == 0u)
+ {
+ }
+
+ io_out8 (com1_port (COM_DATA_OFFSET), value);
+}
+
+void
+serial_write (const char *text)
+{
+ while (*text != '\0')
+ {
+ serial_putc ((u8)*text);
+ ++text;
+ }
+}
diff --git a/mubios/include/bios.h b/mubios/include/bios.h
index 7681bf0..c3b5026 100644
--- a/mubios/include/bios.h
+++ b/mubios/include/bios.h
@@ -40,4 +40,8 @@ void post_main (void);
void memory_init (void);
void bda_init (void);
+void serial_init (void);
+void serial_putc (u8 value);
+void serial_write (const char *text);
+
#endif // MUBIOS_BIOS_H
diff --git a/mubios/include/io.h b/mubios/include/io.h
new file mode 100644
index 0000000..8b50508
--- /dev/null
+++ b/mubios/include/io.h
@@ -0,0 +1,43 @@
+/*
+ RCC-Project
+
+ Copyright (C) 2026 Richard Qin
+
+ This file is part of RCC-Project.
+
+ RCC-Project is free software; you can redistribute it and/or modify it under
+ the terms of the GNU General Public License as published by the Free
+ Software Foundation; either version 3 of the License, or (at your option) any
+ later version.
+
+ RCC-Project is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with
+ RCC-Project; see the file COPYING. If not see
+ <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MUBIOS_IO_H
+#define MUBIOS_IO_H
+
+#include "bios.h"
+
+static inline void
+io_out8 (u16 port, u8 value)
+{
+ __asm__ volatile ("outb %0, %1" : : "a"(value), "Nd"(port));
+}
+
+static inline u8
+io_in8 (u16 port)
+{
+ u8 value;
+
+ __asm__ volatile ("inb %1, %0" : "=a"(value) : "Nd"(port));
+
+ return value;
+}
+
+#endif // MUBIOS_IO_H