c语言指针访问PCI设备,C语言:编写访问PCI的小工具 -电脑资料

PCI的读写原理我就不罗嗦了,PCI的spec上面写的很清楚,仔细多看几遍就OK了,

最好的办法就是把读写两个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

}

其他的使用注意事项,你可以参考此页中的描述:

http://msdn.microsoft.com/en-us/library/1b80826t.aspx

好了,如果你把这两个小程序搞好之后,那么访问PCI就很简单了,C语言:编写访问PCI的小工具》(https://www.unjs.com)。

下面是我写的一段小程序事例,读取bus 0, device 0, function 0上面的所有64个寄存器的内容。

[cpp]

int main()

{

void iowrite(short int port1,unsigned long value);

unsigned long int ioread(short int port);

short int Config_Add=0xcf8;

short int Config_Dat=0xcfc;

int bus=0x00,dev=0x00,fun=0x00,reg=0x00;

unsigned long dat;

for(reg=0;reg<0x40;reg++){

iowrite(Config_Add,(0x80000000 |(bus<<16) |(dev<<11) |(fun<<8) |(reg<<2)));

dat=ioread(Config_Dat);

printf("%8.8lx",dat);

printf(" ");

if((reg+1)%4==0){printf("\n");}

}

return 0;

}

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;

}

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;

}

运行的结果和下图类似,我是在我的windows环境下运行的,所以数据内容肯定不对,大致的看一下就ok了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值