From cc4a32afaa5541f392b6874bc4465170fb5231d9 Mon Sep 17 00:00:00 2001
From: peony <peony@email.com>
Date: Sun, 10 Nov 2024 21:24:19 +0100
Subject: [PATCH] PS/2 work

---
 Cargo.lock                              |   6 +-
 sysdata/programs/ps2_driver/README.md   |  12 +++
 sysdata/programs/ps2_driver/meta.toml   |  11 +++
 sysdata/programs/ps2_driver/src/main.hb | 108 ++++++++++++++++++++++++
 sysdata/system_config.toml              |   7 +-
 5 files changed, 139 insertions(+), 5 deletions(-)
 create mode 100644 sysdata/programs/ps2_driver/README.md
 create mode 100644 sysdata/programs/ps2_driver/meta.toml
 create mode 100644 sysdata/programs/ps2_driver/src/main.hb

diff --git a/Cargo.lock b/Cargo.lock
index dfc9b2e..1e01574 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -228,12 +228,12 @@ dependencies = [
 [[package]]
 name = "hbbytecode"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#654005eea293b1a2e7d81a08d5d4cb542d4cc8a2"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#c61efc393361089f5217db9f4a9011ed0248b8db"
 
 [[package]]
 name = "hblang"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#654005eea293b1a2e7d81a08d5d4cb542d4cc8a2"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#c61efc393361089f5217db9f4a9011ed0248b8db"
 dependencies = [
  "hashbrown 0.15.1",
  "hbbytecode",
@@ -245,7 +245,7 @@ dependencies = [
 [[package]]
 name = "hbvm"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#654005eea293b1a2e7d81a08d5d4cb542d4cc8a2"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#c61efc393361089f5217db9f4a9011ed0248b8db"
 dependencies = [
  "hbbytecode",
 ]
diff --git a/sysdata/programs/ps2_driver/README.md b/sysdata/programs/ps2_driver/README.md
new file mode 100644
index 0000000..c0a3d83
--- /dev/null
+++ b/sysdata/programs/ps2_driver/README.md
@@ -0,0 +1,12 @@
+# Unified PS/2 Driver
+
+Te entire thing is heavily documented with comments because I'm not sure how else to make this understandable.
+
+## !!Assumptions!!
+Anyone who works on this should work to keep this list as small as possible/remove as many of these as possible.
+- Bit 5 of the response form 0x64 indicates which port the data is coming from. (Not true on all systems)
+- A parity or timeout error never occurs.
+- PS/2 controller exists.
+- Both PS/2 ports being broken doesn't need handling.
+- One PS/2 port being broken doesn't need special attention.
+- PS/2 controller doesn't need to perform a self-check.
diff --git a/sysdata/programs/ps2_driver/meta.toml b/sysdata/programs/ps2_driver/meta.toml
new file mode 100644
index 0000000..005cb92
--- /dev/null
+++ b/sysdata/programs/ps2_driver/meta.toml
@@ -0,0 +1,11 @@
+[package]
+name = "ps2_driver"
+authors = ["Peony"]
+
+[dependants.libraries]
+
+[dependants.binaries]
+hblang.version = "1.0.0"
+
+[build]
+command = "hblang src/main.hb"
diff --git a/sysdata/programs/ps2_driver/src/main.hb b/sysdata/programs/ps2_driver/src/main.hb
new file mode 100644
index 0000000..351187b
--- /dev/null
+++ b/sysdata/programs/ps2_driver/src/main.hb
@@ -0,0 +1,108 @@
+.{memory, log} := @use("../../../libraries/stn/src/lib.hb")
+
+DeviceID := struct {value: u16}
+
+$Mouse3Button := DeviceID.(0x0)
+$MouseScrollwheel := DeviceID.(0x3)
+$Mouse5Button := DeviceID.(0x4)
+$Spacesaver := DeviceID.(0xAB84)
+$Keyboard122Key := DeviceID.(0xAB86)
+$KeyboardJapaneseG := DeviceID.(0xAB90)
+$KeyboardJapanesep := DeviceID.(0xAB91)
+$KeyboardJapaneseA := DeviceID.(0xAB92)
+$KeyboardNCDSun := DeviceID.(0xACA1)
+$NoDevice := DeviceID.(0xFFFF)
+
+Port := struct {exists: bool, device: DeviceID, command_queued: bool, command_queue: u8}
+
+$check_bit := fn(value: u8, bit: u8, state: u8): bool {
+	return value >> bit & 1 == state
+}
+
+ports := [Port].(.(true, NoDevice, false, 0xFF), .(true, NoDevice, false, 0xFF))
+
+$initialize_controller := fn(): void {
+	memory.outb(0x64, 0xAD)
+	memory.outb(0x64, 0xA7)
+	//Disables ports to make sure that they won't interfere with the setup process.
+
+	loop if (memory.inb(0x64) & 1) == 0 break else memory.inb(0x60)
+	//Flushes any output because apperantly that might interfere with stuff.
+
+	memory.outb(0x64, 0xA8)
+	//Enables port 2.
+	memory.outb(0x64, 0x20)
+	//Gimme configuration byte.
+	loop if (memory.inb(0x64) & 1) == 1 break
+	ports[1].exists = check_bit(memory.inb(0x60), 5, 0)
+	if ports[1].exists {
+		memory.outb(0x64, 0xA7)
+	}
+
+	loop if (memory.inb(0x64) & 1) == 0 break else memory.inb(0x60)
+	//Flushes any output because apperantly that might interfere with stuff.
+
+	memory.outb(0x64, 0xAB)
+	loop if (memory.inb(0x64) & 1) == 1 break
+	ports[0].exists = memory.inb(0x60) == 0x0
+	//Test port 1.
+
+	if ports[1].exists {
+		memory.outb(0x64, 0xA9)
+		loop if (memory.inb(0x64) & 1) == 1 break
+		ports[1].exists = memory.inb(0x60) == 0x0
+	}
+	//Test port 2.
+
+	if (ports[0].exists | ports[1].exists) == false {
+		log.error("No ports detected! No input will be processed! Cannot handle this!\0")
+	}
+
+	if ports[0].exists {
+		memory.outb(0x64, 0xAE)
+		//Enables port 1.
+		ports[0].command_queued = true
+	}
+	if ports[1].exists {
+		memory.outb(0x64, 0xA8)
+		//Enables port 2.
+		ports[1].command_queued = true
+	}
+}
+
+handle_input := fn(port: uint, input: u8): void {
+}
+
+main := fn(): void {
+	loop {
+		port_info := memory.inb(0x64)
+		//Enables port 1.
+		if (port_info & 0x40) > 0 {
+			log.error("Timeout error! Cannot handle these!\0")
+		}
+		if (port_info & 0x80) > 0 {
+			log.error("Parity error! Cannot handle these!\0")
+		}
+
+		if (port_info & 1) == 0 {
+			if ports[0].exists & ports[0].command_queued {
+				memory.outb(0x60, ports[0].command_queue)
+				ports[0].command_queued = false
+			}
+			if ports[1].exists & ports[1].command_queued {
+				memory.outb(0x64, 0xD4)
+				memory.outb(0x60, ports[1].command_queue)
+				ports[1].command_queued = false
+			}
+		}
+
+		port := 0
+		if ports[1].exists {
+			port = port_info >> 5 & 1
+		}
+
+		if ports[port].exists {
+			@inline(handle_input, port, memory.inb(0x60))
+		}
+	}
+}
\ No newline at end of file
diff --git a/sysdata/system_config.toml b/sysdata/system_config.toml
index 93bd739..688c762 100644
--- a/sysdata/system_config.toml
+++ b/sysdata/system_config.toml
@@ -22,8 +22,8 @@ resolution = "1024x768x24"
 
 [boot.limine.ableos.modules]
 
-[boot.limine.ableos.modules.render_example]
-path = "boot:///render_example.hbf"
+# [boot.limine.ableos.modules.render_example]
+# path = "boot:///render_example.hbf"
 
 # [boot.limine.ableos.modules.horizon]
 # path = "boot:///horizon.hbf"
@@ -33,3 +33,6 @@ path = "boot:///render_example.hbf"
 
 # [boot.limine.ableos.modules.ps2_keyboard_driver]
 # path = "boot:///ps2_keyboard_driver.hbf"
+
+[boot.limine.ableos.modules.ps2_driver]
+path = "boot:///ps2_driver.hbf"