akern-gkgoat-fork/sysdata/programs/ps2_driver/src/main.hb

152 lines
3.9 KiB
Plaintext
Raw Normal View History

2024-11-17 15:38:07 -06:00
.{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")
2024-11-17 14:30:58 -06:00
devices := @use("devices.hb")
2024-11-24 06:31:57 -06:00
controller := @use("controller.hb");
.{Info, Port} := controller
2024-11-17 15:38:07 -06:00
mouse := @use("mouse.hb")
2024-11-15 13:47:11 -06:00
format_page := memory.dangling(u8)
2024-11-10 14:24:19 -06:00
2024-11-17 15:38:07 -06:00
mouse_buffer := 0
keyboard_buffer := 0
2024-11-24 06:31:57 -06:00
info := Info.(0)
2024-11-17 10:57:06 -06:00
2024-11-24 06:31:57 -06:00
send_command := fn(port: ^Port, byte: u8): void {
2024-11-18 13:29:51 -06:00
tries := 3
loop if tries == 0 break else {
2024-11-17 15:59:05 -06:00
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
2024-11-17 15:38:07 -06:00
}
}
2024-11-18 13:29:51 -06:00
tries -= 1
2024-11-17 15:38:07 -06:00
}
}
2024-11-24 06:31:57 -06:00
enable_streaming := fn(port: ^Port): void {
@inline(send_command, port, 0xF4)
}
2024-11-17 12:11:13 -06:00
process := fn(port: ^controller.Port): void {
2024-11-17 15:38:07 -06:00
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)
2024-11-17 14:30:58 -06:00
} else if port.device == devices.MOUSE_INIT_1 {
2024-11-17 15:38:07 -06:00
port.device.value = port.packet[0]
if port.device != devices.MOUSE_SCROLLWHEEL {
2024-11-24 06:31:57 -06:00
enable_streaming(port)
2024-11-17 15:38:07 -06:00
return
2024-11-17 14:30:58 -06:00
}
2024-11-17 15:38:07 -06:00
port.device = devices.MOUSE_INIT_2
2024-11-17 14:30:58 -06:00
} else if port.device == devices.MOUSE_INIT_2 {
2024-11-17 15:38:07 -06:00
port.device.value = port.packet[0]
2024-11-17 14:30:58 -06:00
} else if port.device == devices.NO_DEVICE {
if port.packet_length == 1 {
port.device.value = port.packet[0]
2024-11-24 06:31:57 -06:00
enable_streaming(port)
//TODO: Upgrade mouse.
2024-11-17 14:30:58 -06:00
} else {
port.device.value = port.packet[1] | port.packet[0] << 8
2024-11-24 06:31:57 -06:00
enable_streaming(port)
2024-11-17 14:30:58 -06:00
}
log.info("Identified device!\0")
log.info(string.display_int(port.device.value, format_page, 16))
2024-11-17 15:59:05 -06:00
} else {
log.info("KEY PRESSED\0")
2024-11-17 14:30:58 -06:00
}
2024-11-17 10:57:06 -06:00
}
check_complete := fn(port: ^controller.Port): bool {
2024-11-17 14:30:58 -06:00
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
}
2024-11-17 15:38:07 -06:00
} else if port.device == devices.MOUSE_3_BUTTON {
2024-11-17 14:30:58 -06:00
if port.packet_length == 3 return true
2024-11-17 15:38:07 -06:00
} else if port.device == devices.MOUSE_SCROLLWHEEL | port.device == devices.MOUSE_5_BUTTON {
2024-11-17 14:30:58 -06:00
if port.packet_length == 4 return true
} else {
2024-11-17 15:59:05 -06:00
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
}
2024-11-17 14:30:58 -06:00
}
return false
2024-11-17 10:57:06 -06:00
}
2024-11-10 14:24:19 -06:00
main := fn(): void {
2024-11-17 15:38:07 -06:00
mouse_buffer = buffer.create("PS/2 Mouse\0")
2024-11-15 13:47:11 -06:00
format_page = memory.alloc(u8, 1024)
2024-11-17 04:17:32 -06:00
controller.init()
2024-11-17 10:57:06 -06:00
2024-11-17 15:59:05 -06:00
if controller.port1.exists {
2024-11-24 06:31:57 -06:00
//log.info("Port 1 exists.\0")
controller.send_byte(@bitcast(0), 0xF4)
2024-11-17 15:59:05 -06:00
}
if controller.port2.exists {
2024-11-24 06:31:57 -06:00
//controller.send_byte(&controller.port2, 0xF4)
2024-11-17 15:59:05 -06:00
}
2024-11-17 10:57:06 -06:00
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")
}
2024-11-24 06:31:57 -06:00
/*
2024-11-17 10:57:06 -06:00
if controller.has_input(info) {
port := controller.get_port(info)
2024-11-17 15:38:07 -06:00
if port.packet_length > 0 & check_complete(port) {
process(port)
}
2024-11-17 14:30:58 -06:00
input := controller.get_input()
2024-11-17 15:59:05 -06:00
/*if input == 0xAA & port.can_hot_plug {
2024-11-17 14:30:58 -06:00
port.device = devices.NO_DEVICE
2024-11-17 15:59:05 -06:00
controller.send_byte(port, 0xF4)
}*/
2024-11-17 14:30:58 -06:00
port.packet[port.packet_length] = input
2024-11-17 10:57:06 -06:00
port.packet_length += 1
2024-11-17 15:38:07 -06:00
if check_complete(port) {
2024-11-17 10:57:06 -06:00
process(port)
2024-11-17 15:38:07 -06:00
port.packet_length = 0
2024-11-17 10:57:06 -06:00
}
2024-11-24 06:31:57 -06:00
}*/
2024-11-17 10:57:06 -06:00
}
2024-11-10 14:24:19 -06:00
}