📄 jb_io.c
字号:
exit(1);
}
port_io_buffer_count = 0;
#endif /* PARPORT*/
}
int VerifyHardware()
{
int verify_ok = 0;
int test_count = 0;
int read_data = 0;
for ( test_count = 0; test_count < 2; test_count++ )
{
/* Write '0' to Pin 7 and Pin 9 (Data5,7) for the first test and '1' for the second test */
int vector = (test_count) ? 0xA0 : 0x0;/* 1010 0000:0000 0000... drive to Port0 */
int expect = (test_count) ? 0x60 : 0x0;/* X11X XXXX:X00X XXXX... expect from Port1 */
WritePort( 0, vector, 1 );
/* Expect '0' at Pin 10 and Pin 12 (Ack and Paper End) for the first test and '1' for the second test */
read_data = ReadPort( 1 ) & 0x60;
verify_ok = verify_ok && ( read_data == expect ? 1:0 );
}
if ( !verify_ok )
fprintf( stdout, "Info: Verifying hardware: Hardware found...\n" );
else
fprintf( stderr, "Error: Verifying hardware: Hardware not found or not installed properly...\n" );
return verify_ok;
}
#else if PORT == EMBEDDED
/* put your code for driver initializing, closing and flushing (optional) code here */
#endif /*PORT */
/******************************************************************/
/* Name: ReadPort */
/* */
/* Parameters: port */
/* -the index of port from the parallel port base */
/* address. */
/* */
/* Return Value: Value of the port. */
/* */
/* Descriptions: Read the value of the port registers. */
/* */
/******************************************************************/
int ReadPort(int port)
{
int data = 0;
#if PORT==WINDOWS_NT
int status = 0;
int returned_length = 0;
status = DeviceIoControl(
nt_device_handle, /* Handle to device */
PGDC_IOCTL_READ_PORT_PP, /* IO Control code for Read */
(ULONG *)&port, /* Buffer to driver. */
sizeof(int), /* Length of buffer in bytes. */
(ULONG *)&data, /* Buffer from driver. */
sizeof(int), /* Length of buffer in bytes. */
(ULONG *)&returned_length, /* Bytes placed in data_buffer. */
NULL); /* Wait for operation to complete */
if ((!status) || (returned_length != sizeof(int)))
{
fprintf(stderr, "I/O error: Cannot access ByteBlaster hardware\n");
CloseHandle(nt_device_handle);
CloseNtDriver();
exit(1);
}
#elif PORT == LINUX
#ifndef LINUX_PARPORT
/* printf("Attempting to read port 0x%03x\n",port+lpt_addr); */ /*DEBUG*/
data = inb(port + lpt_addr);
/* printf("Read data == 0x%02x\n",data); */ /*DEBUG*/
#else
int ret = -1;
if (port == 0) ret = ioctl (ppfp, PPRDATA, &data);
else if (port == 1) ret = ioctl (ppfp, PPRSTATUS, &data);
else if (port == 2) ret = ioctl (ppfp, PPRCONTROL, &data);
// fprintf(stderr,"Debug: Device read access [p:%i|d:%i] (err: %i).\n",port,data,ret);
if (ret != 0) {
fprintf(stderr,"Warning: Device read access failed (err: %i).\n",ret);
} else totalrd++;
#endif /* PARPORT */
#else if PORT == EMBEDDED
/* Put your I/O routines here */
#endif
return (data & 0xff);
}
/******************************************************************/
/* Name: WritePort */
/* */
/* Parameters: port,data,test */
/* -port is the index from the parallel port base */
/* address. */
/* -data is the value to dump to the port. */
/* -purpose of write. */
/* */
/* Return Value: None. */
/* */
/* Descriptions: Write "data" to "port" registers. When dump to */
/* port 0,if "test"=0,processes in "port_io_buffer" */
/* are flushed when "PORT_IO_BUFFER_SIZE" is */
/* reached. if "test"=1,"data" is dumped to port 0 */
/* at once. */
/* */
/******************************************************************/
void WritePort(int port,int data,int test)
{
#if PORT==WINDOWS_NT
int returned_length = 0;
int buffer[2];
/* Collect up to [PORT_IO_BUFFER_SIZE] data for Port0, then flush them */
/* if test = 1 or Port = 1 or Port = 2, writing to the ports are done immediately */
if (port == 0 && test == 0)
{
port_io_buffer[port_io_buffer_count].data = (USHORT) data;
port_io_buffer[port_io_buffer_count].command = PGDC_WRITE_PORT;
++port_io_buffer_count;
if (port_io_buffer_count >= PORT_IO_BUFFER_SIZE) flush_ports();
}
else
{
buffer[0] = port;
buffer[1] = data;
status = DeviceIoControl(
nt_device_handle, /* Handle to device */
PGDC_IOCTL_WRITE_PORT_PP, /* IO Control code for write */
(ULONG *)&buffer, /* Buffer to driver. */
2 * sizeof(int), /* Length of buffer in bytes. */
(ULONG *)NULL, /* Buffer from driver. Not used. */
0, /* Length of buffer in bytes. */
(ULONG *)&returned_length, /* Bytes returned. Should be zero. */
NULL); /* Wait for operation to complete */
}
#elif PORT == LINUX
#ifndef LINUX_PARPORT
/* printf("writing 0x%02x to address 0x%03x\n", data, port + lpt_addr); *//* DEBUG */
outb(data, port + lpt_addr);
#else
int ret = -1;
/*
if (port == 0 && test == 0)
{
port_io_buffer[port_io_buffer_count] = data;
++port_io_buffer_count;
if (port_io_buffer_count >= PORT_IO_BUFFER_SIZE) flush_ports();
ret=0;
}
*/
if (port == 0) ret = ioctl (ppfp, PPWDATA, &data);
else if (port == 2) ret = ioctl (ppfp, PPWCONTROL, &data);
// fprintf(stderr,"Debug: Device write access [p:%i|d:%i|t:%i] (err: %i).\n",port,data,test,ret);
if (ret != 0) {
fprintf(stderr,"Warning: Device write access failed (err: %i).\n",ret);
} else totalwr++;
#endif /* PARPORT */
#else if PORT==EMBEDDED
/* Put your I/O rountines here */
#endif
}
/*****************************/
/* */
/* File processing functions */
/* */
/*****************************/
int jb_fopen(char* argv,char* mode)
{
FILE* file_id;
file_id = fopen( argv, mode );
return (int) file_id;
}
int jb_fclose(int file_id)
{
fclose( (FILE*) file_id);
return 0;
}
int jb_fseek(int finputid,int start,int end)
{
int seek_position;
seek_position = fseek( (FILE*) finputid, start, end );
return seek_position;
}
int jb_ftell(int finputid)
{
int file_size;
file_size = ftell( (FILE*) finputid );
return file_size;
}
int jb_fgetc(int finputid)
{
int one_byte;
one_byte = fgetc( (FILE*) finputid );
return one_byte;
}
char* jb_fgets(char* buffer, int finputid)
{
char* test;
test=fgets(buffer,MAX_FILE_LINE_LENGTH,(FILE*) finputid);
return test;
}
/*******************************/
/* */
/* String processing functions */
/* */
/*******************************/
void jb_strcpy(char* a,char* b)
{
strcpy(a,b);
}
int jb_str_cmp(char* charset,char* buffer)
{
char* pc;
char temp[MAX_FILE_LINE_LENGTH+1],store;
unsigned int i;
for(i=0;i<strlen(buffer);i++)
{
strcpy(temp,buffer);
pc = &temp[i];
if(*pc==*charset)
{
store = temp[i+strlen(charset)];
temp[i+strlen(charset)]='\0';
if(!strncmp(pc,charset,strlen(charset)))
{
temp[i+strlen(charset)]=store;
return i+1;
}
temp[i+strlen(charset)]=store;
}
pc++;
}
return 0;
}
int jb_strlen(const char* str)
{
return strlen(str);
}
int jb_strcmp(char* a,char* b)
{
return strcasecmp(a,b);
}
void jb_strcat(char* dst,char* src)
{
strcat(dst,src);
}
int jb_atoi(char* number)
{
return atoi(number);
}
void jb_toupper(char* str)
{
char* pstr;
pstr = str;
while(*pstr)
{
if(*pstr>='a' && *pstr<='z')
*pstr -= 0x20;
pstr++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -