.{memory, log} := @use("../../../libraries/stn/src/lib.hb") 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) Port := struct {exists: bool, device: DeviceID, command_queued: bool, command_queue: u8, 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, false, 0xFF, Reboot, false, 0x0), .(true, NoDevice, false, 0xFF, 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 { memory.outb(0x64, 0xAE) //Enables port 1. ports[0].command_queued = true } if ports[1].exists { memory.outb(0x64, 0xA8) //Enables port 2. ports[1].command_queued = true } } handle_input := fn(port: uint, input: u8): void { if ports[port].state.value == Recive.value { } else if ports[port].state.value == Reboot.value { if input == 0xAA { log.info("Device rebooted!\0") ports[port].state = Recive } else if (ports[port].device.value & 15) == 0 { ports[port].device.value |= input } else { ports[port].device.value |= input << 4 } } } main := fn(): void { @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].command_queued { memory.outb(0x60, ports[0].command_queue) ports[0].command_queued = false } if ports[1].exists & ports[1].command_queued { memory.outb(0x64, 0xD4) memory.outb(0x60, ports[1].command_queue) ports[1].command_queued = false } } port := 0 if ports[1].exists { port = port_info >> 5 & 1 } if ports[port].exists { @inline(handle_input, port, memory.inb(0x60)) } } }