From 2fdede7199363f1adf021a26bb17e9ace9a138d4 Mon Sep 17 00:00:00 2001
From: peony <peony@email.com>
Date: Sun, 17 Nov 2024 21:30:58 +0100
Subject: [PATCH] PS/2 workkkk

---
 Cargo.lock                                    |   6 +-
 sysdata/programs/ps2_driver/README.md         |   8 +-
 sysdata/programs/ps2_driver/src/controller.hb |   6 +-
 sysdata/programs/ps2_driver/src/devices.hb    |  23 +--
 sysdata/programs/ps2_driver/src/main.hb       |  46 +++++-
 .../programs/ps2_driver/src/main_legacy.hb    | 144 ------------------
 sysdata/programs/ps2_driver/src/port.hb       |  10 +-
 7 files changed, 74 insertions(+), 169 deletions(-)
 delete mode 100644 sysdata/programs/ps2_driver/src/main_legacy.hb

diff --git a/Cargo.lock b/Cargo.lock
index 1eaf3cc5..c82d4622 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#fe5a8631f66e7acde8707a68ad25074ab5b1f408"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#542c69fd6042a4b48462199c4391761bfe1b7c2e"
 
 [[package]]
 name = "hblang"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#fe5a8631f66e7acde8707a68ad25074ab5b1f408"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#542c69fd6042a4b48462199c4391761bfe1b7c2e"
 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#fe5a8631f66e7acde8707a68ad25074ab5b1f408"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#542c69fd6042a4b48462199c4391761bfe1b7c2e"
 dependencies = [
  "hbbytecode",
 ]
diff --git a/sysdata/programs/ps2_driver/README.md b/sysdata/programs/ps2_driver/README.md
index c3116537..61936064 100644
--- a/sysdata/programs/ps2_driver/README.md
+++ b/sysdata/programs/ps2_driver/README.md
@@ -10,4 +10,10 @@ 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
+- These DeviceIDs never exist:
+    - 0xFFFD
+    - 0xFFFE
+    - 0xFFFF
+    - 0x01xx
+    - 0x03xx
+    - 0x04xx
\ 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 38863b15..35fb4fed 100644
--- a/sysdata/programs/ps2_driver/src/controller.hb
+++ b/sysdata/programs/ps2_driver/src/controller.hb
@@ -1,9 +1,9 @@
 .{memory, log} := @use("../../../libraries/stn/src/lib.hb");
 .{bit0, bit1, bit5, bit6, bit7} := @use("bits.hb");
-.{Port, port_at_startup} := @use("port.hb")
+.{Port, PORT_AT_STARTUP} := @use("port.hb")
 
-port1 := port_at_startup
-port2 := port_at_startup
+port1 := PORT_AT_STARTUP
+port2 := PORT_AT_STARTUP
 
 $disable_port1 := fn(): void memory.outb(0x64, 0xAD)
 $enable_port1 := fn(): void memory.outb(0x64, 0xAE)
diff --git a/sysdata/programs/ps2_driver/src/devices.hb b/sysdata/programs/ps2_driver/src/devices.hb
index db92593a..7cfbdb01 100644
--- a/sysdata/programs/ps2_driver/src/devices.hb
+++ b/sysdata/programs/ps2_driver/src/devices.hb
@@ -1,12 +1,15 @@
 DeviceID := struct {value: u16}
 
-$Mouse3Button := DeviceID.(0x0)
-$MouseScrollwheel := DeviceID.(0x3)
-$Mouse5Button := DeviceID.(0x4)
-$Spacesaver := DeviceID.(0x84AB)
-$Keyboard122Key := DeviceID.(0x86AB)
-$KeyboardJapaneseG := DeviceID.(0x90AB)
-$KeyboardJapanesep := DeviceID.(0x91AB)
-$KeyboardJapaneseA := DeviceID.(0x92AB)
-$KeyboardNCDSun := DeviceID.(0xA1AC)
-$NoDevice := DeviceID.(0xFFFF)
\ No newline at end of file
+$MOUSE_3_BUTTON := DeviceID.(0x0)
+$MOUSE_SCROLLWHEEL := DeviceID.(0x3)
+$MOUSE_5_BUTTON := DeviceID.(0x4)
+$KEYBOARD_SPACESAVER := DeviceID.(0x84AB)
+$KEYBOARD_122_KEY := DeviceID.(0x86AB)
+$KEYBOARD_JAPANESE_G := DeviceID.(0x90AB)
+$KEYBOARD_JAPANESE_P := DeviceID.(0x91AB)
+$KEYBOARD_JAPANESE_A := DeviceID.(0x92AB)
+$KEYBOARD_NCD_SUN := DeviceID.(0xA1AC)
+
+$MOUSE_INIT_1 := DeviceID.(0xFFFD)
+$MOUSE_INIT_2 := DeviceID.(0xFFFE)
+$NO_DEVICE := DeviceID.(0xFFFF)
\ 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 362c1016..32b786a7 100644
--- a/sysdata/programs/ps2_driver/src/main.hb
+++ b/sysdata/programs/ps2_driver/src/main.hb
@@ -1,15 +1,49 @@
-.{memory, log, string} := @use("../../../libraries/stn/src/lib.hb");
-.{Mouse3Button} := @use("devices.hb")
+.{memory, log, string} := @use("../../../libraries/stn/src/lib.hb")
+devices := @use("devices.hb")
 controller := @use("controller.hb")
 format_page := memory.dangling(u8)
 
 info := controller.Info.(0)
 
 process := fn(port: ^controller.Port): void {
-
+	if port.device == devices.MOUSE_3_BUTTON {
+	} else if port.device == devices.MOUSE_INIT_1 {
+		port.device.value = port.packet[0] | port.packet[1] << 8
+		if port.device == devices.MOUSE_SCROLLWHEEL {
+			port.device = devices.MOUSE_INIT_2
+		}
+	} else if port.device == devices.MOUSE_INIT_2 {
+		port.device.value = port.packet[0] | port.packet[1] << 8
+	} else if port.device == devices.NO_DEVICE {
+		if port.packet_length == 1 {
+			port.device.value = port.packet[0]
+		} else {
+			port.device.value = port.packet[1] | port.packet[0] << 8
+		}
+		log.info("Identified device!\0")
+		log.info(string.display_int(port.device.value, format_page, 16))
+	}
 }
 
 check_complete := fn(port: ^controller.Port): bool {
+	last_value := port.packet[port.packet_length - 1]
+	if port.device == devices.NO_DEVICE {
+		if last_value == 0 | last_value == 3 | last_value == 4 {
+			return true
+		} else if port.packet_length == 2 {
+			return true
+		}
+	}
+	if port.device == devices.MOUSE_3_BUTTON {
+		if port.packet_length == 3 return true
+	}
+	if port.device == devices.MOUSE_SCROLLWHEEL | port.device == devices.MOUSE_5_BUTTON {
+		if port.packet_length == 4 return true
+	} else {
+		log.error("Very unexpected error. Cannot handle this!\0")
+		return true
+	}
+	return false
 }
 
 main := fn(): void {
@@ -29,7 +63,11 @@ main := fn(): void {
 
 		if controller.has_input(info) {
 			port := controller.get_port(info)
-			port.packet[port.packet_length] = controller.get_input()
+			input := controller.get_input()
+			if input == 0xAA & port.can_hot_plug {
+				port.device = devices.NO_DEVICE
+			}
+			port.packet[port.packet_length] = input
 			port.packet_length += 1
 			if @inline(check_complete, port) {
 				process(port)
diff --git a/sysdata/programs/ps2_driver/src/main_legacy.hb b/sysdata/programs/ps2_driver/src/main_legacy.hb
deleted file mode 100644
index 5f335593..00000000
--- a/sysdata/programs/ps2_driver/src/main_legacy.hb
+++ /dev/null
@@ -1,144 +0,0 @@
-.{memory, log, string} := @use("../../../libraries/stn/src/lib.hb")
-format_page := memory.dangling(u8)
-
-DeviceID := struct {value: u16}
-
-$Mouse3Button := DeviceID.(0x0)
-$MouseScrollwheel := DeviceID.(0x3)
-$Mouse5Button := DeviceID.(0x4)
-$Spacesaver := DeviceID.(0x84AB)
-$Keyboard122Key := DeviceID.(0x86AB)
-$KeyboardJapaneseG := DeviceID.(0x90AB)
-$KeyboardJapanesep := DeviceID.(0x91AB)
-$KeyboardJapaneseA := DeviceID.(0x92AB)
-$KeyboardNCDSun := DeviceID.(0xA1AC)
-$NoDevice := DeviceID.(0xFFFF)
-
-State := struct {value: u8}
-$Recive := State.(0)
-$Reboot := State.(1)
-$GetID := State.(2)
-$TurnOnStreaming := State.(3)
-
-CommandQueue := struct {queue: [u8; 8]}
-
-Port := struct {exists: bool, device: DeviceID, commands_queued: u8, command_queue: [u8; 8], command_index: u8, awaiting_acks: u8, state: State, expecting: bool}
-
-$check_bit := fn(value: u8, bit: u8, state: u8): bool {
-	return (value >> bit & 1) == state
-}
-
-ports := [Port].(.(true, NoDevice, 1, .(0xFF, 0, 0, 0, 0, 0, 0, 0), 0, 0, Reboot, false), .(true, NoDevice, 1, .(0xFF, 0, 0, 0, 0, 0, 0, 0), 0, 0, Reboot, false))
-
-$send_byte := fn(port: uint, value: u8): void {
-	//Sending over 8 bytes will cause and overflow, don't do pwease?
-	ports[port].command_queue[(ports[port].command_index + 1) % 8] = value
-	ports[port].awaiting_acks += 1
-}
-
-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 {
-		log.info("Port 1 exists.\0")
-		memory.outb(0x64, 0xAE)
-		//Enables port 1.
-		ports[0].commands_queued = 1
-	}
-	if ports[1].exists {
-		log.info("Port 2 exists.\0")
-		memory.outb(0x64, 0xA8)
-		//Enables port 2.
-		ports[1].commands_queued = 1
-	}
-}
-
-handle_input := fn(port: uint, input: u8): void {
-	if input == 0xAA {
-		log.info("Device rebooted!\0")
-		log.info(string.display_int(@intcast(port + 1), format_page, 16))
-		ports[port].state = GetID
-		send_byte(port, 0xF2)
-		//Get ID
-	} else if ports[port].state.value == Recive.value {
-	}
-}
-
-main := fn(): void {
-	format_page = memory.alloc(u8, 1024)
-
-	@inline(initialize_controller)
-
-	loop {
-		port_info := memory.inb(0x64)
-
-		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].commands_queued > 0 {
-				memory.outb(0x60, ports[0].command_queue[ports[0].command_index])
-				ports[0].commands_queued -= 1
-				ports[0].command_index = (ports[0].command_index + 1) % 8
-			}
-			if ports[1].exists & ports[1].commands_queued > 0 {
-				memory.outb(0x64, 0xD4)
-				memory.outb(0x60, ports[1].command_queue[ports[1].command_index])
-				ports[1].commands_queued -= 1
-				ports[1].command_index = (ports[1].command_index + 1) % 8
-			}
-		}
-
-		port := 0
-		if ports[1].exists {
-			port = port_info >> 5 & 1
-		}
-
-		if ports[port].exists {
-			input := memory.inb(0x60)
-			if ports[port].awaiting_acks > 0 & input == 0xFA {
-				ports[port].awaiting_acks -= 1
-			} else {
-				@inline(handle_input, port, input)
-			}
-		}
-	}
-}
\ 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 852d0d6d..108c9aaf 100644
--- a/sysdata/programs/ps2_driver/src/port.hb
+++ b/sysdata/programs/ps2_driver/src/port.hb
@@ -1,4 +1,4 @@
-.{DeviceID, NoDevice} := @use("devices.hb")
+.{DeviceID, NO_DEVICE} := @use("devices.hb")
 
 State := struct {s: u8}
 $Recive := State.(0)
@@ -7,13 +7,15 @@ $Reboot := State.(1)
 Port := packed struct {
 	exists: bool,
 	device: DeviceID,
-	packet: [u8; 4],
+	packet: [u8; 8],
 	packet_length: u8,
+	can_hot_plug: bool,
 }
 
-$port_at_startup := Port.(
+$PORT_AT_STARTUP := Port.(
 	true,
-	NoDevice,
+	NO_DEVICE,
 	.(0, 0, 0, 0),
 	0,
+	true,
 )
\ No newline at end of file