From 284aa5a5e647848358250a278d27656779c7ff52 Mon Sep 17 00:00:00 2001
From: peony <peony@email.com>
Date: Sun, 17 Nov 2024 11:17:32 +0100
Subject: [PATCH] PS/2 is so close

---
 sysdata/programs/ps2_driver/src/controller.hb | 63 ++++++++++++-------
 sysdata/programs/ps2_driver/src/main.hb       |  3 +
 .../programs/ps2_driver/src/main_legacy.hb    |  2 +-
 sysdata/programs/ps2_driver/src/port.hb       | 30 +++++----
 4 files changed, 64 insertions(+), 34 deletions(-)

diff --git a/sysdata/programs/ps2_driver/src/controller.hb b/sysdata/programs/ps2_driver/src/controller.hb
index a6826da..6478587 100644
--- a/sysdata/programs/ps2_driver/src/controller.hb
+++ b/sysdata/programs/ps2_driver/src/controller.hb
@@ -1,31 +1,56 @@
 .{memory, log} := @use("../../../libraries/stn/src/lib.hb");
-.{bit0, bit5} := @use("bits.hb")
+.{bit0, bit5, bit6, bit7} := @use("bits.hb");
+.{Port, port_at_startup} := @use("port.hb")
 
 $disable_port1 := fn(): void memory.outb(0x64, 0xAD)
 $enable_port1 := fn(): void memory.outb(0x64, 0xAE)
 $disable_port2 := fn(): void memory.outb(0x64, 0xA7)
 $enable_port2 := fn(): void memory.outb(0x64, 0xA8)
 
-//TODO test functions
-/*test_port1 := fn(): bool {
+test_port1 := fn(): bool {
+	memory.outb(0x64, 0xAB)
+	loop if has_input(get_info()) break
+	input := get_input()
+	return input == 0x0
+}
 
-}*/
+test_port2 := fn(): bool {
+	memory.outb(0x64, 0xA9)
+	loop if has_input(get_info()) break
+	input := get_input()
+	return input == 0x0
+}
 
 get_config_byte := fn(): u8 {
-    memory.outb(0x64, 0x20)
+	memory.outb(0x64, 0x20)
 	loop if has_input(get_info()) break
-    return memory.inb(0x60)
+	return get_input()
 }
 
 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_port := fn(info: Info): ^Port {
+	if bit5(info.d) {
+		return &port2
+	} else {
+		return &port1
+	}
+}
+
+$get_input := fn(): u8 return memory.inb(0x60)
+$write_out := fn(data: u8): void memory.outb(0x60, data)
 
 flush_input := fn(): void {
-	loop if has_input(get_info()) == false break else memory.inb(0x60)
+	loop if has_input(get_info()) == false break else get_info()
 }
 
+port1 := port_at_startup
+port2 := port_at_startup
+
 init := fn(): void {
 	disable_port1()
 	disable_port2()
@@ -34,35 +59,29 @@ init := fn(): void {
 	flush_input()
 
 	enable_port2()
-	ports[1].exists = bit5(@inline(get_config_byte)) == false
+	port2.exists = bit5(@inline(get_config_byte)) == false
 	disable_port2()
 
 	flush_input()
 
-	memory.outb(0x64, 0xAB)
-	loop if has_input(get_info()) break
-	ports[0].exists = memory.inb(0x60) == 0x0
-	//Test port 1.
+	port1.exists = test_port1()
 
-	if ports[1].exists {
-		memory.outb(0x64, 0xA9)
-		loop if has_input(get_info()) break
-		ports[1].exists = memory.inb(0x60) == 0x0
+	if port2.exists {
+		port2.exists = test_port2()
 	}
-	//Test port 2.
 
-	if (ports[0].exists | ports[1].exists) == false {
+	if (port1.exists | port2.exists) == false {
 		log.error("No ports detected! No input will be processed! Cannot handle this!\0")
 	}
 
-	if ports[0].exists {
+	if port1.exists {
 		log.info("Port 1 exists.\0")
 		enable_port1()
-		ports[0].commands_queued = 1
+		port1.commands.length = 1
 	}
-	if ports[1].exists {
+	if port2.exists {
 		log.info("Port 2 exists.\0")
 		enable_port2()
-		ports[1].commands_queued = 1
+		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 a62e9e1..8362c4f 100644
--- a/sysdata/programs/ps2_driver/src/main.hb
+++ b/sysdata/programs/ps2_driver/src/main.hb
@@ -1,6 +1,9 @@
 .{memory, log, string} := @use("../../../libraries/stn/src/lib.hb")
+controller := @use("controller.hb")
 format_page := memory.dangling(u8)
 
 main := fn(): void {
 	format_page = memory.alloc(u8, 1024)
+
+	controller.init()
 }
\ No newline at end of file
diff --git a/sysdata/programs/ps2_driver/src/main_legacy.hb b/sysdata/programs/ps2_driver/src/main_legacy.hb
index 04b8228..5f33559 100644
--- a/sysdata/programs/ps2_driver/src/main_legacy.hb
+++ b/sysdata/programs/ps2_driver/src/main_legacy.hb
@@ -105,7 +105,7 @@ 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")
 		}
diff --git a/sysdata/programs/ps2_driver/src/port.hb b/sysdata/programs/ps2_driver/src/port.hb
index 1bcc674..5f270c1 100644
--- a/sysdata/programs/ps2_driver/src/port.hb
+++ b/sysdata/programs/ps2_driver/src/port.hb
@@ -1,12 +1,20 @@
-.{DeviceID} := @use("devices.hb")
+.{DeviceID, NoDevice} := @use("devices.hb")
 
-Port := struct {
-    exists: bool
-    device: DeviceID
-    commands_queued: u8
-    command_queue: [u8; 8]
-    command_index: u8
-    awaiting_acks: u8
-    state: State
-    expecting: bool
-}
\ No newline at end of file
+State := struct {s: u8}
+$Reboot := State.(0)
+
+CommandQueue := packed struct {queue: [u8; 8], length: u8, current_index: u8}
+
+Port := packed struct {
+	exists: bool,
+	device: DeviceID,
+	commands: CommandQueue,
+	state: State,
+}
+
+$port_at_startup := Port.(
+	true,
+	NoDevice,
+	.(.(0xFF, 0, 0, 0, 0, 0, 0, 0), 1, 0),
+	Reboot,
+)
\ No newline at end of file