From f5c6d7d822a49588b59fbced23a5ca9f8e9b81ea Mon Sep 17 00:00:00 2001
From: peony <peony@email.com>
Date: Sat, 16 Nov 2024 21:51:55 +0100
Subject: [PATCH] PS/2 driver refactoring and poassibly completion process

---
 Cargo.lock                                    |  55 ++-----
 sysdata/programs/ps2_driver/src/bits.hb       |  26 ++++
 sysdata/programs/ps2_driver/src/controller.hb |  53 +++++++
 sysdata/programs/ps2_driver/src/main.hb       | 126 ---------------
 .../programs/ps2_driver/src/main_legacy.hb    | 144 ++++++++++++++++++
 sysdata/programs/ps2_driver/src/port.hb       |  10 ++
 6 files changed, 243 insertions(+), 171 deletions(-)
 create mode 100644 sysdata/programs/ps2_driver/src/bits.hb
 create mode 100644 sysdata/programs/ps2_driver/src/controller.hb
 create mode 100644 sysdata/programs/ps2_driver/src/main_legacy.hb
 create mode 100644 sysdata/programs/ps2_driver/src/port.hb

diff --git a/Cargo.lock b/Cargo.lock
index 17a86d6..a0152a0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -65,15 +65,6 @@ version = "2.6.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
 
-[[package]]
-name = "bumpalo"
-version = "3.16.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
-dependencies = [
- "allocator-api2",
-]
-
 [[package]]
 name = "byteorder"
 version = "1.5.0"
@@ -208,12 +199,6 @@ dependencies = [
  "wasi",
 ]
 
-[[package]]
-name = "hashbrown"
-version = "0.14.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
-
 [[package]]
 name = "hashbrown"
 version = "0.15.1"
@@ -228,24 +213,23 @@ dependencies = [
 [[package]]
 name = "hbbytecode"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#7058efe75c7ad245db80986e77a97d426b9be8a4"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#a64383e72b0920ef1c03773aac197404c78b330f"
 
 [[package]]
 name = "hblang"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#7058efe75c7ad245db80986e77a97d426b9be8a4"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#a64383e72b0920ef1c03773aac197404c78b330f"
 dependencies = [
- "hashbrown 0.15.1",
+ "hashbrown",
  "hbbytecode",
  "hbvm",
  "log",
- "regalloc2",
 ]
 
 [[package]]
 name = "hbvm"
 version = "0.1.0"
-source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#7058efe75c7ad245db80986e77a97d426b9be8a4"
+source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#a64383e72b0920ef1c03773aac197404c78b330f"
 dependencies = [
  "hbbytecode",
 ]
@@ -396,7 +380,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da"
 dependencies = [
  "equivalent",
- "hashbrown 0.15.1",
+ "hashbrown",
 ]
 
 [[package]]
@@ -406,7 +390,7 @@ dependencies = [
  "aarch64-cpu",
  "crossbeam-queue",
  "derive_more",
- "hashbrown 0.15.1",
+ "hashbrown",
  "hbvm",
  "limine",
  "log",
@@ -428,9 +412,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
 
 [[package]]
 name = "libc"
-version = "0.2.162"
+version = "0.2.164"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398"
+checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f"
 
 [[package]]
 name = "limine"
@@ -553,19 +537,6 @@ dependencies = [
  "bitflags 2.6.0",
 ]
 
-[[package]]
-name = "regalloc2"
-version = "0.10.2"
-source = "git+https://github.com/jakubDoka/regalloc2?branch=reuse-allocations#21c43e3ee182824e92e2b25f1d3c03ed47f9c02b"
-dependencies = [
- "allocator-api2",
- "bumpalo",
- "hashbrown 0.14.5",
- "log",
- "rustc-hash",
- "smallvec",
-]
-
 [[package]]
 name = "regex-syntax"
 version = "0.8.5"
@@ -602,12 +573,6 @@ dependencies = [
  "windows-sys",
 ]
 
-[[package]]
-name = "rustc-hash"
-version = "2.0.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152"
-
 [[package]]
 name = "rustc_version"
 version = "0.4.1"
@@ -619,9 +584,9 @@ dependencies = [
 
 [[package]]
 name = "rustls"
-version = "0.23.16"
+version = "0.23.17"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "eee87ff5d9b36712a58574e12e9f0ea80f915a5b0ac518d322b24a465617925e"
+checksum = "7f1a745511c54ba6d4465e8d5dfbd81b45791756de28d4981af70d6dca128f1e"
 dependencies = [
  "log",
  "once_cell",
diff --git a/sysdata/programs/ps2_driver/src/bits.hb b/sysdata/programs/ps2_driver/src/bits.hb
new file mode 100644
index 0000000..3a8afeb
--- /dev/null
+++ b/sysdata/programs/ps2_driver/src/bits.hb
@@ -0,0 +1,26 @@
+//Do not question.
+
+$bit0 := fn(value: u8): bool {
+	return (value & 0x1) > 0
+}
+$bit1 := fn(value: u8): bool {
+	return (value & 0x2) > 0
+}
+$bit2 := fn(value: u8): bool {
+	return (value & 0x4) > 0
+}
+$bit3 := fn(value: u8): bool {
+	return (value & 0x8) > 0
+}
+$bit4 := fn(value: u8): bool {
+	return (value & 0x10) > 0
+}
+$bit5 := fn(value: u8): bool {
+	return (value & 0x20) > 0
+}
+$bit6 := fn(value: u8): bool {
+	return (value & 0x40) > 0
+}
+$bit7 := fn(value: u8): bool {
+	return (value & 0x80) > 0
+}
\ No newline at end of file
diff --git a/sysdata/programs/ps2_driver/src/controller.hb b/sysdata/programs/ps2_driver/src/controller.hb
new file mode 100644
index 0000000..00a83f6
--- /dev/null
+++ b/sysdata/programs/ps2_driver/src/controller.hb
@@ -0,0 +1,53 @@
+.{memory, log} := @use("../../../libraries/stn/src/lib.hb");
+.{bit5} := @use("bits.hb")
+
+init := 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 = bit5(memory.inb(0x60)) == false
+	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
+	}
+}
\ 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 21946cf..a62e9e1 100644
--- a/sysdata/programs/ps2_driver/src/main.hb
+++ b/sysdata/programs/ps2_driver/src/main.hb
@@ -1,132 +1,6 @@
 .{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)
-
-Port := struct {exists: bool, device: DeviceID, commands_queued: u8, command_queue: [u8; 8], awaiting_ack: bool, state: State, expecting: bool, expecting_data: u8}
-
-$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), false, Reboot, false, 0x0), .(true, NoDevice, 1, .(0xFF, 0, 0, 0, 0, 0, 0, 0), false, Reboot, false, 0x0))
-
-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 = Recive
-	} 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)
-		//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].commands_queued > 0 {
-				memory.outb(0x60, ports[0].command_queue)
-				ports[0].commands_queued -= 1
-			}
-			if ports[1].exists & ports[1].commands_queued > 0 {
-				memory.outb(0x64, 0xD4)
-				memory.outb(0x60, ports[1].command_queue)
-				ports[1].commands_queued -= 1
-			}
-		}
-
-		port := 0
-		if ports[1].exists {
-			port = port_info >> 5 & 1
-		}
-
-		if ports[port].exists {
-			input := memory.inb(0x60)
-			if ports[port].awaiting_ack & input == 0xFA {
-				ports[port].awaiting_ack = false
-			} else {
-				@inline(handle_input, port, input)
-			}
-		}
-	}
 }
\ 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
new file mode 100644
index 0000000..04b8228
--- /dev/null
+++ b/sysdata/programs/ps2_driver/src/main_legacy.hb
@@ -0,0 +1,144 @@
+.{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)
+		//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].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
new file mode 100644
index 0000000..6591840
--- /dev/null
+++ b/sysdata/programs/ps2_driver/src/port.hb
@@ -0,0 +1,10 @@
+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