From f9452e6a380c33f22cd89e8a349449743a3be4a6 Mon Sep 17 00:00:00 2001
From: Amelia Coutard <eliottulio.coutard@gmail.com>
Date: Thu, 12 May 2022 14:23:23 +0200
Subject: [PATCH] Restructured code a bit, to move all snippets of asm to
 utils.{c,h}pp

---
 src/serial.cpp |  8 --------
 src/utils.cpp  | 28 +++++++++++++++++++++++-----
 src/utils.hpp  |  7 +++++++
 3 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/src/serial.cpp b/src/serial.cpp
index 384ba9d..d353fc8 100644
--- a/src/serial.cpp
+++ b/src/serial.cpp
@@ -2,14 +2,6 @@
 #include "serial.hpp"
 
 namespace {
-	void outb(std::uint16_t port, std::uint8_t data) {
-		asm volatile ("outb %1,%0" : : "dN"(port), "a"(data));
-	}
-	std::uint8_t inb(std::uint16_t port) {
-		std::uint8_t data;
-		asm volatile ("inb %1,%0" : "=a"(data) : "dN"(port));
-		return data;
-	}
 }
 
 bool os::init_serial_port() {
diff --git a/src/utils.cpp b/src/utils.cpp
index 2c53ef6..5806cb6 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -1,9 +1,27 @@
 #include "utils.hpp"
 
+void os::cli() {
+	asm volatile("cli");
+}
+void os::sti() {
+	asm volatile("sti");
+}
+void os::hlt() {
+	asm volatile("hlt");
+}
+
 void os::halt() {
-	asm volatile (
-		"cli\n"
-		"\t1:\thlt\n"
-		"\tjmp 1b"
-	);
+	cli();
+	while (true) {
+		hlt();
+	}
+}
+
+void os::outb(std::uint16_t port, std::uint8_t data) {
+	asm volatile ("outb %1,%0" : : "dN"(port), "a"(data));
+}
+std::uint8_t os::inb(std::uint16_t port) {
+	std::uint8_t data;
+	asm volatile ("inb %1,%0" : "=a"(data) : "dN"(port));
+	return data;
 }
diff --git a/src/utils.hpp b/src/utils.hpp
index e2e242f..baa4b05 100644
--- a/src/utils.hpp
+++ b/src/utils.hpp
@@ -1,7 +1,14 @@
 #pragma once
 
+#include <cstdint>
+
 namespace os {
 
+void cli();
+void sti();
+void hlt();
 void halt();
+void outb(std::uint16_t port, std::uint8_t data);
+std::uint8_t inb(std::uint16_t port);
 
 } // namespace os
-- 
2.46.0