PCI的读写原理我就不罗嗦了,PCI的spec上面写的很清楚,仔细多看几遍就OK了。因为最近公司来了一个新人,要练习写PCI的小工具,试了很久没有搞出来,主要是用vc编译器,写出来的根本无法在DOS下运行,windows下运行需要通过驱动访问底层硬件;用TC编译器,因为是32位的,没法对CFC和CF8两个32的端口访问。所以,唯一的办法就是在C语言中内嵌汇编程序。
最好的办法就是把读写两个32位端口的动作封装成子程序,这样以后调用就会便利许多。
1,第一个是读程序:
[cpp]
unsigned long ioread(short int port)
{
unsigned long valueRet;
asm mov dx, port;
asm lea bx, valueRet;
__emit__(
0x66,0x50,
0x66,0xED,
0x66,0x89,0x07,
0x66,0x58);
return valueRet;
}
2,第二个是写程序:
[cpp]
void iowrite(short int port1, unsigned long value)
{
asm mov dx, port1;
asm lea bx, value;
__emit__(
0x66,0x50,
0x66,0x8B,0x07,
0x66,0xEF,
0x66,0x58);
return;
}
注意这两个子程序都用到了_emit这个伪代码,他的具体的用法是这样的:
The _emit pseudoinstruction defines one byte at the current location in the current text segment. The_emit pseudoinstruction resembles theDB directive of MASM.
也就是说,它相当于masm中的DB,定义一个byte.
下面有个例子,来自Microsoft的inline assembler.
The following fragment places the bytes 0x4A, 0x43, and 0x4B into the code:
[cpp]
#define randasm __asm _emit 0x4A __asm _emit 0x43 __asm _emit 0x4B
.
.
.
__asm {
randasm
}
好了,如果你把这两个小程序搞好之后,那么访问PCI就很简单了。