diff --git a/sysdata/programs/ps2_driver/README.md b/sysdata/programs/ps2_driver/README.md new file mode 100644 index 0000000..283b6ae --- /dev/null +++ b/sysdata/programs/ps2_driver/README.md @@ -0,0 +1,23 @@ +# Unified PS/2 Driver + +Te entire thing is held together inspite + +## !!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. +- These DeviceIDs never exist: + - 0xFFFD + - 0xFFFE + - 0xFFFF + - 0x01xx + - 0x03xx + - 0x04xx +- Literally all PS/2 keyboards can be handeled the exact same way. We have the capability for detecting different keyboard types, I just don't bother with it because that would litreally take months to get working. +- The device doesn't send any data while we're waiting for an `ACK`. + +Supporting mice in the keyboard port and vice versa was a ***bad*** idea, but I do not regret it because it means we're "superior" to real world operating systems. \ No newline at end of file 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/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..029fdd2 --- /dev/null +++ b/sysdata/programs/ps2_driver/src/controller.hb @@ -0,0 +1,95 @@ +.{memory, log} := @use("../../../libraries/stn/src/lib.hb"); +.{bit0, bit1, bit5, bit6, bit7} := @use("bits.hb"); +.{Port, PORT_AT_STARTUP} := @use("port.hb") + +port1 := @as(Port, PORT_AT_STARTUP) +port2 := @as(Port, PORT_AT_STARTUP) + +$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) + +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) + loop if has_input(get_info()) break + return get_input() +} + +Info := struct {d: u8} + +$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 + } else { + return &port1 + } +} + +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) + +flush_input := fn(): void { + loop if has_input(get_info()) == false break else get_info() +} + +init := fn(): void { + disable_port1() + disable_port2() + //Disables ports to make sure that they won't interfere with the setup process. + + flush_input() + + enable_port2() + port2.exists = bit5(@inline(get_config_byte)) == false + disable_port2() + + flush_input() + + port1.exists = test_port1() + + if port2.exists { + port2.exists = test_port2() + } + + if (port1.exists | port2.exists) == false { + log.error("No ports detected! No input will be processed! Cannot handle this!\0") + } + + if port1.exists { + log.info("Port 1 exists.\0") + enable_port1() + } + if port2.exists { + log.info("Port 2 exists.\0") + enable_port2() + } +} \ No newline at end of file diff --git a/sysdata/programs/ps2_driver/src/devices.hb b/sysdata/programs/ps2_driver/src/devices.hb new file mode 100644 index 0000000..7cfbdb0 --- /dev/null +++ b/sysdata/programs/ps2_driver/src/devices.hb @@ -0,0 +1,15 @@ +DeviceID := struct {value: u16} + +$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 new file mode 100644 index 0000000..d2289c5 --- /dev/null +++ b/sysdata/programs/ps2_driver/src/main.hb @@ -0,0 +1,144 @@ +.{memory, log, buffer, string} := @use("../../../libraries/stn/src/lib.hb"); +.{MouseEvent} := @use("../../../libraries/intouch/src/lib.hb").events; +.{bit0, bit1, bit2, bit3, bit4} := @use("bits.hb") +devices := @use("devices.hb") +controller := @use("controller.hb") +mouse := @use("mouse.hb") +format_page := memory.dangling(u8) + +mouse_buffer := 0 +keyboard_buffer := 0 +info := controller.Info.(0) + +send_command := fn(port: ^controller.Port, byte: u8): void { + tries := 3 + loop if tries == 0 break else { + controller.send_byte(port, byte) + loop { + info = controller.get_info() + if controller.has_input(info) == false { + continue + } + input := controller.get_input() + if controller.get_port(info) != port { + if check_complete(port) == false { + port.packet[port.packet_length] = input + port.packet_length += 1 + } + continue + } + if input == 0xFA { + return + } else { + break + } + } + tries -= 1 + } +} + +process := fn(port: ^controller.Port): void { + if port.device.value < devices.MOUSE_5_BUTTON.value { + event := MouseEvent.(0, 0, false, false, false) + + event.left = bit0(port.packet[0]) + event.right = bit1(port.packet[0]) + event.middle = bit2(port.packet[0]) + + event.x_change = @intcast(port.packet[1]) + event.y_change = @intcast(port.packet[2]) + + buffer.write(MouseEvent, mouse_buffer, &event) + } else if port.device == devices.MOUSE_INIT_1 { + port.device.value = port.packet[0] + if port.device != devices.MOUSE_SCROLLWHEEL { + controller.send_byte(port, 0xF4) + return + } + port.device = devices.MOUSE_INIT_2 + } else if port.device == devices.MOUSE_INIT_2 { + port.device.value = port.packet[0] + } 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 + send_command(port, 0xF4) + } + log.info("Identified device!\0") + log.info(string.display_int(port.device.value, format_page, 16)) + } else { + log.info("KEY PRESSED\0") + } +} + +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 + } + } else if port.device == devices.MOUSE_3_BUTTON { + if port.packet_length == 3 return true + } else if port.device == devices.MOUSE_SCROLLWHEEL | port.device == devices.MOUSE_5_BUTTON { + if port.packet_length == 4 return true + } else { + if port.packet[0] == 0xE1 { + if port.packet_length == 6 { + return true + } + } else if port.packet[0] != 0xE0 { + return true + } else if port.packet_length == 2 & port.packet[1] != 0x2A & port.packet[1] != 0xB7 { + return true + } else if port.packet_length == 4 { + return true + } + } + return false +} + +main := fn(): void { + mouse_buffer = buffer.create("PS/2 Mouse\0") + format_page = memory.alloc(u8, 1024) + + controller.init() + + if controller.port1.exists { + controller.send_byte(&controller.port1, 0xF4) + } + if controller.port2.exists { + controller.send_byte(&controller.port2, 0xF4) + } + + 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) + if port.packet_length > 0 & check_complete(port) { + process(port) + } + input := controller.get_input() + /*if input == 0xAA & port.can_hot_plug { + port.device = devices.NO_DEVICE + controller.send_byte(port, 0xF4) + }*/ + port.packet[port.packet_length] = input + port.packet_length += 1 + if check_complete(port) { + process(port) + port.packet_length = 0 + } + } + } +} \ No newline at end of file diff --git a/sysdata/programs/ps2_driver/src/mouse.hb b/sysdata/programs/ps2_driver/src/mouse.hb new file mode 100644 index 0000000..a3dd51b --- /dev/null +++ b/sysdata/programs/ps2_driver/src/mouse.hb @@ -0,0 +1,21 @@ +Button := struct {id: u8} +$LEFT_BUTTON := Button.(1) +$RIGHT_BUTTON := Button.(2) +$MIDDLE_BUTTON := Button.(4) +$BUTTON4 := Button.(8) +$BUTTON5 := Button.(16) + +SampleRate := struct {value: u8} +$SR10 := SampleRate.(10) +$SR20 := SampleRate.(20) +$SR40 := SampleRate.(40) +$SR60 := SampleRate.(60) +$SR80 := SampleRate.(80) +$SR100 := SampleRate.(100) +$SR200 := SampleRate.(200) + +Resolution := struct {value: u8} +$RES_1COUNT_PER_MM := Resolution.(0) +$RES_2COUNT_PER_MM := Resolution.(1) +$RES_4COUNT_PER_MM := Resolution.(2) +$RES_8COUNT_PER_MM := Resolution.(3) \ 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..76563d1 --- /dev/null +++ b/sysdata/programs/ps2_driver/src/port.hb @@ -0,0 +1,21 @@ +.{DeviceID, NO_DEVICE} := @use("devices.hb") + +State := struct {s: u8} +$Recive := State.(0) +$Reboot := State.(1) + +Port := packed struct { + exists: bool, + device: DeviceID, + packet: [u8; 8], + packet_length: u8, + can_hot_plug: bool, +} + +$PORT_AT_STARTUP := Port.( + true, + NO_DEVICE, + .(0, 0, 0, 0, 0, 0, 0, 0), + 0, + true, +) \ No newline at end of file diff --git a/sysdata/system_config.toml b/sysdata/system_config.toml index 530c98a..89830c9 100644 --- a/sysdata/system_config.toml +++ b/sysdata/system_config.toml @@ -28,20 +28,17 @@ 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" -[boot.limine.ableos.modules.sunset_client] -path = "boot:///sunset_client.hbf" +[boot.limine.ableos.modules.ps2_driver] +path = "boot:///ps2_driver.hbf" -[boot.limine.ableos.modules.sunset_client_2] -path = "boot:///sunset_client_2.hbf" +# [boot.limine.ableos.modules.sunset_client] +# path = "boot:///sunset_client.hbf" -[boot.limine.ableos.modules.sunset_server] -path = "boot:///sunset_server.hbf" - -# [boot.limine.ableos.modules.processes] -# path = "boot:///processes.hbf" +# [boot.limine.ableos.modules.sunset_server] +# path = "boot:///sunset_server.hbf"