Game port:并行通信端口、历史、编程与寄存器说明
文章目录
Game port 是一种古老且已淘汰的并行通信端口。主要用于模拟摇杆,但也用于其他设备(如 MIDI 设备或拨轮)。Game port 使用 DB-15 端口。它有八个模拟连接用于四个按钮,四个用于轴。
历史
该端口最初出现在 IBM Game Control Adapter 中,这是原始 IBM PC 的一块可选附加板。Creative Labs 将 game port 包含在其声卡(SoundBlaster)中,为游戏玩家提供“一体化”解决方案,因为声卡主要用于游戏。从那时起它在玩家群体中非常流行。摇杆并不像你可能认为的那样仅限于四个按钮。一些摇杆(如带有九个按钮的 Microsoft Sidewinder)使用复用技术来获得多达 15 个按钮,甚至 20 个。
然而,有一个严重的缺点;旧的模拟摇杆(使用 game port)使用一个电位器(可变电阻),其电阻通常为 100kΩ,以及一个电容。控制板计算电容的充电阈值,然后将其转换为数字(轴)。问题是摇杆电阻器会随时间发热(10-15 分钟),电阻发生变化,导致数值不正确。模拟摇杆需要不断校准。
Game port 编程
Game port 使用端口 201h。它是一个读写端口。没什么可说的,非常简单。任何轴读取之前必须先进行一次写入,可以是任何数据,这无关紧要。
| IO 端口 | 访问类型 | 用途 |
|---|---|---|
| 0x201 | 读/写 | 摇杆状态 |
| 位 | 用途 |
|---|---|
| 0 | X 摇杆轴 (A)。如果该位被置位,计时未激活。所有位同理。 |
| 1 | Y 摇杆轴 (A) |
| 2 | X 摇杆轴 (B) |
| 3 | Y 摇杆轴 (B) |
| 4 | (A) 按钮 1 的状态。如果该位未被置位,按钮已被按下。所有按钮同理。 |
| 5 | (A) 按钮 2 的状态 |
| 6 | (B) 按钮 1 的状态 |
| 7 | (B) 按钮 2 的状态 |
还有一个用于摇杆的 BIOS 调用;Int 0x15,AH 0x84。但它的支持很差,大多数 BIOS 的实现都有 bug。
示例代码
示例代码,用于获取按钮状态。使用轮询来检测状态。你也可以使用 IRQ 9-11,它们对 peripherals 是空闲的,但对于如此简单的设备来说是一种浪费。
警告! 此代码尚未经过测试!
/* Simple joystick port controller. hextakatt 2019, public domain. Improvements are welcome, send me an PM (in the forum) if you have any. */ #include <stdint.h> #include <stdbool.h> /* Joystick 200h - 201h. */ #define JOYSTICK_PORT 0x201 #define GARBAGE_DATA 0xAA55 #define MAX_TIME_ATTEMPTS 1000 /* Joystick values: axis and buttons */ enum JoystickValues { JoystickButtonA = 0x10, JoystickButtonB = 0x20, JoystickButtonC = 0x40, JoystickButtonD = 0x80, JoystickAxisX = 0x01, JoystickAxisY = 0x02, JoystickDeltaX = 0x04, JoystickDeltaY = 0x08, }; /* A nice struct which contains the joystick status */ struct JoystickStatus { int16_t AxisX; int16_t AxisY; int16_t DeltaX; int16_t DeltaY; bool ButtonA; bool ButtonB; bool ButtonC; bool ButtonD; bool JoystickFlag; }; struct JoystickStatus *jst; /* If joystick is present, flag is true. */ jst->JoystickFlag = false; uint8_t joystick_values[] = { 0x01, 0x02, 0x04, 0x08, /* Axis */ 0x10, 0x20, 0x40, 0x80, /* Buttons */ } bool joystick_button(uint8_t buttonnum) { for (int i = 4; i <= 8; ++i) { if (joystick_values[i] == buttonnum) { return ((inb(JOYSTICK_PORT) & buttonnum) == 0); } } kputs("Invalid value: %x; Out of bounds.\n", buttonnum); return 0; } /* Dump all joystick values to an structure */ struct JoystickStatus joystick_dump(void) { struct JoystickStatus jst; jst.AxisX = joystick_status(JoystickAxisX); jst.AxisY = joystick_status(JoystickAxisY); jst.DeltaX = joystick_status(JoystickDeltaX); jst.DeltaY = joystick_status(JoystickDeltaY); jst.ButtonA = joystick_button(joystick_status(JoystickButtonA)); jst.ButtonB = joystick_button(joystick_status(JoystickButtonB)); jst.ButtonC = joystick_button(joystick_status(JoystickButtonC)); jst.ButtonD = joystick_button(joystick_status(JoystickButtonD)); return jst; } /* * There's no IRQ for joystick port, so we poll instead. * However isn't a problem, because very few data is transmitted * through game port. */ uint16_t joystick_status(uint8_t byte) { int timeout = 0; /* Disable interrupts, so they don't affect timing */ asm volatile("cli"); uint16_t stat = inb(JOYSTICK_PORT); /* Any read needs to be with a write. The byte that we send * can be garbage, it doesn't matters. */ outb(JOYSTICK_PORT, GARBAGE_DATA); /* Poll until we get our requested value */ while (1) { if (stat & byte) { jst->JoystickFlag = true; break; } else if (timeout == MAX_TIME_ATTEMPTS) { jst->JoystickFlag = false; return 0; } ++timeout; } /* Now we can enable interrupts again */ asm volatile("sti"); return (stat & byte); }
另请参阅
外部链接
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论