From 42bda3b69c4ee510c63fffed362a6a07a50e9be8 Mon Sep 17 00:00:00 2001
From: Bensong Liu <bensl@microsoft.com>
Date: Mon, 2 Nov 2020 16:37:37 +0800
Subject: [PATCH] add bus io

---
 bootloader/boot.asm       |  6 +++---
 kernel/Makefile           |  2 +-
 kernel/include/bus_io.hpp | 35 +++++++++++++++++++++++++++++++++++
 kernel/include/stdint.hpp | 24 ++++++++++++++++++++++++
 kernel/include/vga.hpp    | 17 +++++++++++++++++
 kernel/kernel.cc          | 23 ++++++++++++-----------
 6 files changed, 92 insertions(+), 15 deletions(-)
 create mode 100644 kernel/include/bus_io.hpp
 create mode 100644 kernel/include/stdint.hpp
 create mode 100644 kernel/include/vga.hpp

diff --git a/bootloader/boot.asm b/bootloader/boot.asm
index ff513b2..41043c6 100644
--- a/bootloader/boot.asm
+++ b/bootloader/boot.asm
@@ -76,11 +76,11 @@ disk_io_error:
 
 
 _init_prot_mode:
-    cli
-    lgdt [gdt_desc]
+    cli ; turn off interrupt, until we set interrupt vector for 32b protected mode.
+    lgdt [gdt_desc] ; set GDT,
     mov eax, cr0
     or eax, 0x1
-    mov cr0, eax
+    mov cr0, eax ; and set CR0.
     ; We're almost in 32bit protected mode now. Flush the pipeline by far jump. 
     jmp CODE_SEG_OFFSET:_prot_begin
 
diff --git a/kernel/Makefile b/kernel/Makefile
index da48893..a766e15 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -10,7 +10,7 @@ head:
 	nasm -f elf image_head.asm -o image_head.o
 
 kernel:
-	g++ -ffreestanding -fno-pie -c kernel.cc -o kernel.o -m32
+	g++ -ffreestanding -fno-pie -c kernel.cc -o kernel.o -m32 -std=c++17
 
 clean:
 	rm *.o *.img
diff --git a/kernel/include/bus_io.hpp b/kernel/include/bus_io.hpp
new file mode 100644
index 0000000..8ac4d3f
--- /dev/null
+++ b/kernel/include/bus_io.hpp
@@ -0,0 +1,35 @@
+#ifndef ROS_KERN_BUS_IO_HPP
+#define ROS_KERN_BUS_IO_HPP
+#include "stdint.hpp"
+
+inline auto read_byte_from_bus(uint16_t port) {
+    uint8_t result;
+    asm ("in %%dx, %%al" : "=a"(result) : "d"(port));
+    return result;
+}
+inline auto read_word_from_bus(uint16_t port) {
+    uint16_t result;
+    asm ("in %%dx, %%ax" : "=a"(result) : "d"(port));
+    return result;
+}
+inline auto read_dword_from_bus(uint16_t port) {
+    uint32_t result;
+    asm ("in %%dx, %%eax" : "=a"(result) : "d"(port));
+    return result;
+}
+
+inline void write_byte_to_bus(uint16_t port, uint8_t data) {
+    asm ("out %%al, %%dx": : "a"(data), "d"(port));
+}
+inline void write_word_to_bus(uint16_t port, uint16_t data) {
+    asm ("out %%ax, %%dx": : "a"(data), "d"(port));
+}
+inline void write_dword_to_bus(uint16_t port, uint32_t data) {
+    asm ("out %%eax, %%dx": : "a"(data), "d"(port));
+}
+
+
+
+
+
+#endif
diff --git a/kernel/include/stdint.hpp b/kernel/include/stdint.hpp
new file mode 100644
index 0000000..a3e534d
--- /dev/null
+++ b/kernel/include/stdint.hpp
@@ -0,0 +1,24 @@
+#ifndef ROS_KERN_STDINT_HPP
+#define ROS_KERN_STDINT_HPP
+
+using uint8_t = unsigned char;
+using uint16_t = unsigned short;
+using uint32_t = unsigned int;
+using uint64_t = unsigned long long;
+static_assert(sizeof(uint8_t) == 1);
+static_assert(sizeof(uint16_t) == 2);
+static_assert(sizeof(uint32_t) == 4);
+static_assert(sizeof(uint64_t) == 8);
+
+using sint8_t  = signed char;
+using sint16_t = signed short;
+using sint32_t = signed int;
+using sint64_t = signed long long;
+static_assert(sizeof(sint8_t) == 1);
+static_assert(sizeof(sint16_t) == 2);
+static_assert(sizeof(sint32_t) == 4);
+static_assert(sizeof(sint64_t) == 8);
+
+using size_t = uint32_t;
+
+#endif
diff --git a/kernel/include/vga.hpp b/kernel/include/vga.hpp
new file mode 100644
index 0000000..b6925ed
--- /dev/null
+++ b/kernel/include/vga.hpp
@@ -0,0 +1,17 @@
+#ifndef ROS_KERN_VGA_HPP
+#define ROS_KERN_VGA_HPP
+
+inline char *vga_begin = (char *)0xb8000;
+void push_char(char c, char color) {
+    static int pos = 0;
+    vga_begin[pos++] = c;
+    vga_begin[pos++] = color;
+}
+
+void set_char(int x, int y, char c, char color) {
+    auto pos = x + 80 * y;
+    vga_begin[pos*2] = c;
+    vga_begin[pos*2+1] = color;
+}
+
+#endif
\ No newline at end of file
diff --git a/kernel/kernel.cc b/kernel/kernel.cc
index fc5f776..d1c4e40 100644
--- a/kernel/kernel.cc
+++ b/kernel/kernel.cc
@@ -1,20 +1,21 @@
 
 
-char *vga_begin = (char *)0xb8000;
-void push_char(char c, char color) {
-    static int pos = 0;
-    vga_begin[pos++] = c;
-    vga_begin[pos++] = color;
+#include "include/vga.hpp"
+#include "include/bus_io.hpp"
+
+void test_dummy_function() {
+    auto tmp = read_byte_from_bus(0x3f2);
+    tmp |= 0x08;
+    write_byte_to_bus(0x3f2, tmp);
 }
 
 void main() {
-    int pos = 0;
-    for(int cter = 0; cter < 256; ++cter) {
-        push_char(cter % 10 + '0', cter);
-        for(int i = 1; i < 10; ++i) {
-            push_char(i + '0', cter);
+    for(auto y = 0; y < 25; ++y) {
+        for(auto x = 0; x < 80; ++x) {
+            char c = x%10 + '0';
+            char color = x + y*80;
+            set_char(x, y, c, color);
         }
     }
-
 }
 
-- 
GitLab