Last active
August 31, 2024 19:31
-
-
Save juj/84f6c680939d1da7686db890b69954a0 to your computer and use it in GitHub Desktop.
Borland Turbo C++ 3.0 32-bit I/O port read and write functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <dos.h> | |
#include <stdio.h> | |
// outport32(): Performs a 32-bit OUT I/O instruction in Borland Turbo C++. | |
// This corresponds to "out dx, eax" command in 386 protected mode assembly. | |
// port: 16-bit I/O port address to write to. | |
// value: a 32-bit value to write. | |
void outport32(unsigned int port, unsigned long value) { | |
// Problem: Borland Turbo C++ is a 16-bit real-mode compiler, so does | |
// not have a 32-bit outport() C API, and its assembler does not | |
// recognize 32-bit EAX register or 32-bit instructions. | |
// However x86 assembly allows a single-byte instruction prefix 66h | |
// to be prepended to many instructions to make these instructions | |
// perform a wider 32-bit operation instead. | |
asm { | |
db 0x66 | |
mov ax, word ptr value // Actually 'mov eax, dword ptr value' | |
mov dx, port // Load dx with the 16-bit port to write to | |
db 0x66 | |
out dx, ax // Actually 'out dx, eax' | |
} | |
} | |
// inport32(): Performs a 32-bit IN I/O instruction in Borland Turbo C++. | |
// This corresponds to "in eax, dx" command in 386 protected mode assembly. | |
// port: the 16-bit port address to read from. | |
// return: the 32-bit value read from the port. | |
unsigned long inport32(unsigned int port) { | |
unsigned long result; | |
asm { | |
mov dx, port // Load dx with the 16-bit port to read from | |
db 0x66 | |
in ax, dx // Actually 'in eax, dx' | |
db 0x66 | |
mov word ptr result, ax // Actually 'mov dword ptr result, eax' | |
} | |
return result; | |
} | |
int main() | |
{ | |
// Example code to enable a bit in Rendition Verite V2200 PCI I/O space. | |
printf("Before: 0x%08lX\n", inport32(0x60C0)); | |
outport32(0x60C0, inport32(0x60C0) | (1ul << 19)); | |
printf("After: 0x%08lX\n", inport32(0x60C0)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment