⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test2040.cpp

📁 pci2040VxD驱动非常详细的源程序
💻 CPP
字号:
/// MDAPP32.CPP - example app for use with MAPDEV example

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <winioctl.h>
#include "test2040.h"
#include "pci.h"

#define   INTSET			0;
#define   INTCLEAR			0x04;
#define   INTMASK			0x08;
#define   INTUNMASK			0x0C;
#define   HPISTART          0x14;
#define   HPIDATAWIDTH      0x18;


HANDLE		hDevice;
void main(int ac, char* av[])
{
	DWORD Hpiaddress,Conaddress;       //the physical address
	BOOL  BoolMap0,BoolMap1[4];			 //to decide if lock the linear address
	int   Irq;			 //the virtual irq
	DWORD Ioport;        //the ioport address
	PVOID	inBuf[1];		 // buffer for struct pointer to VxD
	DWORD	cbBytesReturned; // count of bytes returned from VxD
	MAPDEVREQUEST req;		 // map device request structure
	DWORD Testid;			 //device id and vendor
	unsigned char  a[256];
	for(int k=0;k<256;k++)
		a[k]=11;

	DWORD CONFIGUEREG;
	DWORD Addhpic,Addhpia,Addhpidinc,Addhpid;
	WORD *Addhpic1,*Addhpia1,*Addhpidinc1,*Addhpid1;
	WORD Addhpic2,Addhpia2,Addhpidinc2,Addhpid2;
	WORD command;

/////////////////////////////get the device id and vendor
	for(int i=0x80000000;i<0x80ffff00+1;i=i+0x100)
	{
				_outpd(0x0cf8,i);
				Testid=_inpd(0x0cfc);
				if(Testid==0xac60104c)
				{	
					printf("The bus line ID and device ID is  %08lx\n", i);
					CONFIGUEREG=i;
///////////////////////////////command register
					_outpd(0x0cf8,CONFIGUEREG+0x04);
					command=_inpw(0x0cfc);
					printf("    Testcommmand is %04x",command); 
					command=command|0x0002;
					_outpd(0x0cf8,CONFIGUEREG+0x04); 
					_outpw(0x0cfc,command);
					_outpd(0x0cf8,CONFIGUEREG+0x04); 
					command=_inpw(0x0cfc);
					printf("     is %04x\n",command); 
					_outpd(0x0cf8,CONFIGUEREG);
					Testid=_inpd(0x0cfc);
					printf("Device ID and Vendor is %08lx\n",Testid); 
					break;
				}
	}


//////////////////////////////open   the    vxd
	hDevice = CreateFile("\\\\.\\pci2040.VXD", 0,0,0,
                        CREATE_NEW, FILE_FLAG_DELETE_ON_CLOSE, 0);
	if (hDevice == INVALID_HANDLE_VALUE)
	{
        	fprintf(stderr, "Cannot open driver, error=%08lx\n",GetLastError());
			exit(1);
	}


//////////////////////////////////get the hpi csr irq 
		if (!DeviceIoControl(hDevice,GETIRQ,0,0,&Irq,0,0,0))
	{
	    	fprintf(stderr, "Failed to IRQ\n");
			exit(1);
	}
	printf("The IRQ is %08lx\n", Irq);


////////////////////////////////get the hpi csr memory base physical and linear address;
////1.get the physical address
	if ( ! DeviceIoControl(hDevice,GETMEMBASE0,0,0,&Hpiaddress,0,0,0)
	   )
	{
	    	fprintf(stderr, "Failed to getmembase0\n");
			exit(1);
	}
	printf("The HPI CSR Memory Base Phycical Address %08lx", Hpiaddress);
////2.map the address
	req.mdr_PhysicalAddress = (PVOID)Hpiaddress;
	req.mdr_SizeInBytes = 0x1000;
	inBuf[0] = &req;
	if ( ! DeviceIoControl(hDevice, MDR_SERVICE_MAP,
               inBuf, sizeof(PVOID), NULL, 0,
               &cbBytesReturned, NULL)
	   )
	    	fprintf(stderr, "Failed to map device\n");
	else
	{
		printf(" linear is %08lx\n", req.mdr_LinearAddress);
		BoolMap0=TRUE;
	}
///////////////////////////////////read the items of hpi reg
	DWORD *Dhpicsr;
	DWORD dhpi,dlasthpi;
	WORD  *Whpicsr,*Whpicsreset;
	WORD  whpi,wlasthpi;

	Whpicsr=(WORD*)((BYTE*)req.mdr_LinearAddress+0x18);////////0x18   data width
	whpi=*Whpicsr;  
  	printf("        datawidth    is %04x",whpi);
	*Whpicsr=whpi&0xfff0;
	whpi=*Whpicsr;
	printf("     is %04x\n",whpi);


	Whpicsr=(WORD*)((BYTE*)req.mdr_LinearAddress+0x16);////////0x16     implement reg 
	wlasthpi=*Whpicsr;
  	printf("        implement    is %04x",wlasthpi);
	*Whpicsr=wlasthpi|0x000f;
	whpi=*Whpicsr;
	printf("     is %04x\n",whpi);

///////////////////////////////////get the device id and vendor
	for(int i=0x80000000;i<0x80ffff00+1;i=i+0x100)
	{
				_outpd(0x0cf8,i);
				Testid=_inpd(0x0cfc);
				if(Testid==0xac60104c)
				{	
					printf("The bus line ID and device ID is  %08lx\n", i);
					CONFIGUEREG=i;
///////////////////////////////command register
					_outpd(0x0cf8,CONFIGUEREG+0x04);
					command=_inpw(0x0cfc);
					printf("        commmand     is %04lx",command); 
					command=command|0x0003;
					_outpd(0x0cf8,CONFIGUEREG+0x04); 
					_outpw(0x0cfc,command);
					_outpd(0x0cf8,CONFIGUEREG+0x04); 
					command=_inpw(0x0cfc);
					printf("     is %04lx\n",command); 
				//	_outpd(0x0cf8,CONFIGUEREG);
				//	Testid=_inpd(0x0cfc);
				//	printf("Device ID and Vendor is %08lx\n",Testid); 
					break;
				}
	}


	Whpicsreset=(WORD*)((BYTE*)req.mdr_LinearAddress+0x14);////////0x14    reset reg
	wlasthpi=*Whpicsreset;
  	printf("        reset        is %04x",wlasthpi);
	*Whpicsreset=wlasthpi&0xfff0;
	whpi=*Whpicsreset;
	printf("     is %04x\n",whpi);


	Whpicsr=(WORD*)((BYTE*)req.mdr_LinearAddress+0x10);////////0x10    error reg
	wlasthpi=*Whpicsr;
  	printf("        error        is %04x",wlasthpi);
	*Whpicsr=wlasthpi&0xfff0;
	whpi=*Whpicsr;
	printf("     is %04x\n",whpi);

	Dhpicsr=(DWORD*)((BYTE*)req.mdr_LinearAddress+0x4);////////0x04   clear interrupt
	dlasthpi=*Dhpicsr;								   ////////0x00   set interrupt
  	printf("        interrupt    is %08x\n",dlasthpi);

//	Dhpicsr=(DWORD*)((BYTE*)req.mdr_LinearAddress+0x08);////////0x0c   clear mask
//	dlasthpi=*Dhpicsr;								   ////////0x08   set mask
// 	printf("        mask        is %08x\n",dlasthpi);
//	*Dhpicsr=dlasthpi&0xfff0;
//	dhpi=*Dhpicsr;
//	printf("    is %08x\n",dhpi);





/////////////////////////////////////get the control space base physical and linear address;
////1.get the physical address
	if ( ! DeviceIoControl(hDevice,GETMEMBASE1,0,0,&Conaddress,0,0,0))
	{
	    	fprintf(stderr, "Failed to getmembase0\n");
			exit(1);
	}
	printf("The Control Space Base Phycical Address %08lx\n", Conaddress);

	Addhpic=Conaddress&0xffff8000;
	Addhpia=Addhpic|0x00001000;
	Addhpid=Addhpic|0x00001800;
	Addhpidinc=Addhpic|0x0000800;

/////map the addhpic address
	req.mdr_PhysicalAddress = (PVOID)Addhpic;
	req.mdr_SizeInBytes = 0x10000;
	inBuf[0] = &req;
	if ( ! DeviceIoControl(hDevice, MDR_SERVICE_MAP,
               inBuf, sizeof(PVOID), NULL, 0,
               &cbBytesReturned, NULL)
	   )
	    	fprintf(stderr, "Failed to map device\n");
	else
	{
		printf("	The addhpic  is %08lx",Addhpic);
		printf(" is %08lx\n",req.mdr_LinearAddress);
		BoolMap1[0]=TRUE;
	}
	Addhpic1=(WORD*)req.mdr_LinearAddress;
//	Addhpia1=Addhpic1+2048;
//	Addhpid1=Addhpia1+1024;
//	Addhpidinc1=Addhpic1+1024;
/////map the addhpia  address
	req.mdr_PhysicalAddress = (PVOID)Addhpia;
	req.mdr_SizeInBytes = 0x1000;
	inBuf[0] = &req;
	if ( ! DeviceIoControl(hDevice, MDR_SERVICE_MAP,
               inBuf, sizeof(PVOID), NULL, 0,
               &cbBytesReturned, NULL)
	   )
	    	fprintf(stderr, "Failed to map device\n");
	else
	{
		printf("  The addhpia    is %08lx",Addhpia);
		printf("  is %08lx\n",   req.mdr_LinearAddress);
		BoolMap1[1]=TRUE;
	}
	Addhpia1=(WORD*)req.mdr_LinearAddress;
//////map the addhpid address
	req.mdr_PhysicalAddress = (PVOID)Addhpid;
	req.mdr_SizeInBytes = 0x1000;
	inBuf[0] = &req;
	if ( ! DeviceIoControl(hDevice, MDR_SERVICE_MAP,
               inBuf, sizeof(PVOID), NULL, 0,
               &cbBytesReturned, NULL)
	   )
	    	fprintf(stderr, "Failed to map device\n");
	else
	{
		printf("  The addhpid    is %08lx",Addhpid);
		printf("  is %08lx\n",req.mdr_LinearAddress);
		BoolMap1[2]=TRUE;
	}
	Addhpid1=(WORD*)req.mdr_LinearAddress;
//////map the addhpidinc address
	req.mdr_PhysicalAddress = (PVOID)Addhpidinc;
	req.mdr_SizeInBytes = 0x1000;
	inBuf[0] = &req;
	if ( ! DeviceIoControl(hDevice, MDR_SERVICE_MAP,
               inBuf, sizeof(PVOID), NULL, 0,
               &cbBytesReturned, NULL)
	   )
	    	fprintf(stderr, "Failed to map device\n");
	else
	{
		printf("  The addhpidinc is %08lx",Addhpidinc);
		printf("  is %08lx\n",    req.mdr_LinearAddress);
		BoolMap1[3]=TRUE;
	}
	Addhpidinc1=(WORD*)req.mdr_LinearAddress;

///////test the dsp and pci2040
	*Addhpic1=0x1010;
	*Addhpia1=0x0000;
	*Addhpic1=0x0000;
	*Addhpia1=0x007f;
	
	*Addhpidinc1=0x0;
	*Addhpidinc1=0x1;
	*Addhpidinc1=0x2;
	*Addhpidinc1=0x3;
	*Addhpidinc1=0x4;
	*Addhpidinc1=0x5;
	*Addhpidinc1=0x6;
	*Addhpidinc1=0x7;


	*Addhpic1=0x1010;
	*Addhpia1=0x0000;
	*Addhpic1=0x0101;
	*Addhpia1=0x007f;
	Addhpid2=*Addhpidinc1;
	printf("\nAddhpid2 is %x\n", Addhpid2);
	for(int data=0;data<0x8;data++)
	{
		Addhpid2=*Addhpidinc1;
		printf("%04x ", Addhpid2);
	}



//////////////////////////test the communication between vxd and win32

	if ( ! DeviceIoControl(hDevice,TEST,a,0,0,0,0,NULL))
	{
	    	fprintf(stderr, "Failed to communication\n");
			exit(1);
	}
		fprintf(stderr,"\n");
		for( i=0;i<2;i++)
			{
				for(int j=0;j<16;j++)
				{
					printf("%3d ",a[i*16+j]);
				}
				fprintf(stderr,"\n");
			}

//////////////////////////test the communication between dsp and vxd
	if ( ! DeviceIoControl(hDevice,GETIOBASE,0,0,&Ioport,0,0,0)
	   )
	{
	    	fprintf(stderr, "Failed to get ioport base\n");
			exit(1);
	}
	printf("The Ioport base is  %08lx\n",Ioport);

////////////////////////to unlock all the locked linear address
////////unmap the hpi csr address
	if(BoolMap0==TRUE)
	{
		req.mdr_PhysicalAddress=(PVOID)Hpiaddress;
		req.mdr_SizeInBytes=0x1000;
		inBuf[0] = &req;
		DeviceIoControl(hDevice,MDR_SERVICE_UNMAP,
			inBuf,	sizeof(PVOID),NULL,0,
			&cbBytesReturned,NULL);
		BoolMap0=FALSE;
	}

///////unmap the control regist address
///////ummap hpic
	if(BoolMap1[0]==TRUE)
	{
		req.mdr_PhysicalAddress=(PVOID)Addhpic;
		req.mdr_SizeInBytes=0x1000;
		inBuf[0] = &req;
		DeviceIoControl(hDevice,MDR_SERVICE_UNMAP,
			inBuf,	sizeof(PVOID),NULL,0,
			&cbBytesReturned,NULL);
		BoolMap1[0]=FALSE;
	}
///////ummap hpia
	if(BoolMap1[1]==TRUE)
	{
		req.mdr_PhysicalAddress=(PVOID)Addhpia;
		req.mdr_SizeInBytes=0x1000;
		inBuf[0] = &req;
		DeviceIoControl(hDevice,MDR_SERVICE_UNMAP,
			inBuf,	sizeof(PVOID),NULL,0,
			&cbBytesReturned,NULL);
		BoolMap1[1]=FALSE;
	}
///////ummap hpid
	if(BoolMap1[2]==TRUE)
	{
		req.mdr_PhysicalAddress=(PVOID)Addhpid;
		req.mdr_SizeInBytes=0x1000;
		inBuf[0] = &req;
		DeviceIoControl(hDevice,MDR_SERVICE_UNMAP,
			inBuf,	sizeof(PVOID),NULL,0,
			&cbBytesReturned,NULL);
		BoolMap1[2]=FALSE;
	}
///////ummap hpidinc
	if(BoolMap1[3]==TRUE)
	{
		req.mdr_PhysicalAddress=(PVOID)Addhpidinc;
		req.mdr_SizeInBytes=0x1000;
		inBuf[0] = &req;
		DeviceIoControl(hDevice,MDR_SERVICE_UNMAP,
			inBuf,	sizeof(PVOID),NULL,0,
			&cbBytesReturned,NULL);
		BoolMap1[3]=FALSE;
	}

	wlasthpi=*Whpicsreset;
  	printf("reset  is %04x",wlasthpi);
	*Whpicsreset=wlasthpi|0x000f;
	whpi=*Whpicsreset;
	printf("     is %04x\n",whpi);

//////////////////////To terminate the thread
	CloseHandle(hDevice);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -