Mouse driver for QBasic programs
Table of Contents
1. Overview
QBasic, a popular programming language in the DOS era, lacks native mouse support. This limitation can be a hurdle for developers looking to create interactive applications. To bridge this gap, I developed a workaround that allows QBasic to use mouse input.
2. High-level idea
Workaround to access mouse involves a Terminate and Stay Resident (TSR) program written in x86 assembly. This TSR program must be started before running QBasic program that depends on mouse. This TSR program hooks into the system's interrupt mechanism, specifically the timer interrupt (IRQ 0), allowing it to regularly check for mouse activity several times per second.
When this timer interrupt triggers, the TSR reads the latest mouse's horizontal and vertical movements and button states using mouse interrupts. This data is then stored in a dedicated memory location — a data table within the TSR's memory space. The TSR uses interrupt 79h as a pointer to this data table, making it accessible to other programs, including the QBasic application.
While QBasic originally is not able to read mouse, it is able to read (and write) arbitrary location in system RAM. The QBasic demonstration program begins by retrieving the address of the TSR mouse data table from the interrupt vector table using interrupt 79h. By checking a predefined magic number (1983) in the data table, the program confirms that the mouse driver is loaded. Once verified, the QBasic program continuously reads mouse data from this shared memory location, while TSR keeps updating it with latest mouse state simultaneously.
3. Terminate and Stay Resident module
A DOS TSR program that hooks into the system's interrupt mechanism to regularly read mouse input and store it in a dedicated memory location.
Files:
Here is the detailed technical specification for an in-memory table used to exchange mouse coordinates between a TSR program and a QBasic program.
Offset | Size (bytes) | Description |
---|---|---|
0x00 | 2 | Magic Number (1983) |
0x02 | 2 | Horizontal Movement (X) |
0x04 | 2 | Vertical Movement (Y) |
0x06 | 2 | Button Status |
0x08 | 1 | Update counter |
- Update counter
- Signals to the QBasic program that new mouse data is available in the shared memory table. It ensures that the QBasic program only reads fresh data and avoids processing outdated or repeated data. When the TSR updates the mouse data, it increments this flag by 1 to signal the QBasic program that new data is available. QBasic can compare this number against last retrieved value. If value has been increased, then there had been update meanwhile.
4. QBasic demonstration program
A QBasic program that reads mouse data from the memory location populated by the TSR and demonstrates mouse movement and button clicks.
Here are more practical examples where this mouse driver is being used: Within Space themed 3D graphics, see:
- Galaxy explorer
- Universe explorer