From 23b45b1887b5a661112b7102f5172a2e4d0ecd38 Mon Sep 17 00:00:00 2001
From: peony <peony@email.com>
Date: Sun, 17 Nov 2024 17:57:06 +0100
Subject: [PATCH] Driver workkkk

---
 Cargo.lock                                    |  6 ++--
 sysdata/programs/ps2_driver/README.md         |  1 +
 sysdata/programs/ps2_driver/src/controller.hb | 22 ++++++++-----
 sysdata/programs/ps2_driver/src/main.hb       | 31 ++++++++++++++++++-
 sysdata/programs/ps2_driver/src/port.hb       | 13 ++++----
 sysdata/system_config.toml                    |  4 +--
 6 files changed, 57 insertions(+), 20 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 7d4cd9e..a336be8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -213,12 +213,12 @@ dependencies = [
 [[package]]
 name = "hbbytecode"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#12bb7029b4bafd1edff77ed9a12888374cc7f8be"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#397b2a4b1b7c088f379f32d846e235c1286e17e0"
 
 [[package]]
 name = "hblang"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#12bb7029b4bafd1edff77ed9a12888374cc7f8be"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#397b2a4b1b7c088f379f32d846e235c1286e17e0"
 dependencies = [
  "hashbrown",
  "hbbytecode",
@@ -229,7 +229,7 @@ dependencies = [
 [[package]]
 name = "hbvm"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#12bb7029b4bafd1edff77ed9a12888374cc7f8be"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#397b2a4b1b7c088f379f32d846e235c1286e17e0"
 dependencies = [
  "hbbytecode",
 ]
diff --git a/sysdata/programs/ps2_driver/README.md b/sysdata/programs/ps2_driver/README.md
index c0a3d83..c311653 100644
--- a/sysdata/programs/ps2_driver/README.md
+++ b/sysdata/programs/ps2_driver/README.md
@@ -10,3 +10,4 @@ Anyone who works on this should work to keep this list as small as possible/remo
 - 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.
+- Stack overflows aren't a thing
\ No newline at end of file
diff --git a/sysdata/programs/ps2_driver/src/controller.hb b/sysdata/programs/ps2_driver/src/controller.hb
index 6478587..2c6af89 100644
--- a/sysdata/programs/ps2_driver/src/controller.hb
+++ b/sysdata/programs/ps2_driver/src/controller.hb
@@ -1,5 +1,5 @@
 .{memory, log} := @use("../../../libraries/stn/src/lib.hb");
-.{bit0, bit5, bit6, bit7} := @use("bits.hb");
+.{bit0, bit1, bit5, bit6, bit7} := @use("bits.hb");
 .{Port, port_at_startup} := @use("port.hb")
 
 $disable_port1 := fn(): void memory.outb(0x64, 0xAD)
@@ -29,10 +29,12 @@ get_config_byte := fn(): u8 {
 
 Info := struct {d: u8}
 
-$get_info := fn(): u8 return .(memory.inb(0x64))
-$has_input := fn(info: Info): bool return bit0(info.d)
-$timed_out := fn(info: Info): bool return bit6(info.d)
-$check_parity := fn(info: Info): bool return bit7(info.d)
+$get_info := fn(): Info return .(memory.inb(0x64))
+//inline when can
+has_input := fn(info: Info): bool return bit0(info.d)
+can_send := fn(info: Info): bool return bit1(info.d) == false
+timed_out := fn(info: Info): bool return bit6(info.d)
+check_parity := fn(info: Info): bool return bit7(info.d)
 get_port := fn(info: Info): ^Port {
 	if bit5(info.d) {
 		return &port2
@@ -41,6 +43,14 @@ get_port := fn(info: Info): ^Port {
 	}
 }
 
+send_byte := fn(port: ^Port, byte: u8): void {
+	if port == port2 {
+		memory.outb(0x64, 0xD4)
+	}
+	loop if can_send(get_info()) break
+	memory.outb(0x60, byte)
+}
+
 $get_input := fn(): u8 return memory.inb(0x60)
 $write_out := fn(data: u8): void memory.outb(0x60, data)
 
@@ -77,11 +87,9 @@ init := fn(): void {
 	if port1.exists {
 		log.info("Port 1 exists.\0")
 		enable_port1()
-		port1.commands.length = 1
 	}
 	if port2.exists {
 		log.info("Port 2 exists.\0")
 		enable_port2()
-		port2.commands.length = 1
 	}
 }
\ No newline at end of file
diff --git a/sysdata/programs/ps2_driver/src/main.hb b/sysdata/programs/ps2_driver/src/main.hb
index 8362c4f..ce6bd6b 100644
--- a/sysdata/programs/ps2_driver/src/main.hb
+++ b/sysdata/programs/ps2_driver/src/main.hb
@@ -1,9 +1,38 @@
-.{memory, log, string} := @use("../../../libraries/stn/src/lib.hb")
+.{memory, log, string} := @use("../../../libraries/stn/src/lib.hb");
+.{Mouse3Button} := @use("devices.hb")
 controller := @use("controller.hb")
 format_page := memory.dangling(u8)
 
+info := controller.Info.(0)
+
+process := fn(port: ^controller.Port): bool {
+}
+
+check_complete := fn(port: ^controller.Port): bool {
+}
+
 main := fn(): void {
 	format_page = memory.alloc(u8, 1024)
 
 	controller.init()
+
+	loop {
+		info = controller.get_info()
+
+		if controller.timed_out(info) {
+			log.error("Timeout error! Cannot handle these!\0")
+		}
+		if controller.check_parity(info) {
+			log.error("Parity error! Cannot handle these!\0")
+		}
+
+		if controller.has_input(info) {
+			port := controller.get_port(info)
+			port.packet[port.packet_length] = controller.get_input()
+			port.packet_length += 1
+			if @inline(check_complete, port) {
+				process(port)
+			}
+		}
+	}
 }
\ No newline at end of file
diff --git a/sysdata/programs/ps2_driver/src/port.hb b/sysdata/programs/ps2_driver/src/port.hb
index 5f270c1..852d0d6 100644
--- a/sysdata/programs/ps2_driver/src/port.hb
+++ b/sysdata/programs/ps2_driver/src/port.hb
@@ -1,20 +1,19 @@
 .{DeviceID, NoDevice} := @use("devices.hb")
 
 State := struct {s: u8}
-$Reboot := State.(0)
-
-CommandQueue := packed struct {queue: [u8; 8], length: u8, current_index: u8}
+$Recive := State.(0)
+$Reboot := State.(1)
 
 Port := packed struct {
 	exists: bool,
 	device: DeviceID,
-	commands: CommandQueue,
-	state: State,
+	packet: [u8; 4],
+	packet_length: u8,
 }
 
 $port_at_startup := Port.(
 	true,
 	NoDevice,
-	.(.(0xFF, 0, 0, 0, 0, 0, 0, 0), 1, 0),
-	Reboot,
+	.(0, 0, 0, 0),
+	0,
 )
\ No newline at end of file
diff --git a/sysdata/system_config.toml b/sysdata/system_config.toml
index 6bb34b7..89830c9 100644
--- a/sysdata/system_config.toml
+++ b/sysdata/system_config.toml
@@ -28,8 +28,8 @@ resolution = "1024x768x24"
 # [boot.limine.ableos.modules.horizon]
 # path = "boot:///horizon.hbf"
 
-[boot.limine.ableos.modules.ps2_mouse_driver]
-path = "boot:///ps2_mouse_driver.hbf"
+# [boot.limine.ableos.modules.ps2_mouse_driver]
+# path = "boot:///ps2_mouse_driver.hbf"
 
 # [boot.limine.ableos.modules.ps2_keyboard_driver]
 # path = "boot:///ps2_keyboard_driver.hbf"