Doing programmed I/O in the Windows environment is a somewhat tricky business.
You can download a module that will handle the underlying hardware interface through the Windows HAL (Hardware Abstraction Layer).
A zip file containing this code and a wrapper can be downloaded here.
The source code for the wrapper lib_ioports.c is as follows:
#include <stdio.h>
#include <windows.h>
/* Definitions in the build of inpout32.dll are: */
/* short _stdcall Inp32(short PortAddress); */
/* void _stdcall Out32(short PortAddress, short data); */
/* prototype (function typedef) for DLL function Inp32: */
typedef short _stdcall (*inpfuncPtr)(short portaddr);
typedef void _stdcall (*oupfuncPtr)(short portaddr, short datum);
short inb(short port)
{
HINSTANCE hLib;
inpfuncPtr inp32;
short ret;
/* Load the library */
hLib = LoadLibrary("inpout32.dll");
if (hLib == NULL) {
printf("LoadLibrary Failed.\n");
return -1;
}
/* get the address of the function */
inp32 = (inpfuncPtr) GetProcAddress(hLib, "Inp32");
if (inp32 == NULL) {
printf("GetProcAddress for Inp32 Failed.\n");
return -1;
}
ret = (inp32)(port);
FreeLibrary(hLib);
return(ret);
}
void outb(short port, short value)
{
HINSTANCE hLib;
oupfuncPtr oup32;
/* Load the library */
hLib = LoadLibrary("inpout32.dll");
if (hLib == NULL) {
printf("LoadLibrary Failed.\n");
return;
}
/* get the address of the function */
oup32 = (oupfuncPtr) GetProcAddress(hLib, "Out32");
if (oup32 == NULL) {
printf("GetProcAddress for Oup32 Failed.\n");
return;
}
(oup32)(port, value);
FreeLibrary(hLib);
}
The associated header file (ioports.h) looks like:
#ifndef _IOPORTS_H_ #define _IOPORTS_H_ /* Input an 8 bit byte from 16 bit port */ short inb(short port); /* Output 8 bit byte to 16 bit port */ void outb(short port, short value); #endif