/* port_data holds the current values of signals for every port. By default, they hold the values in */
/* reset mode (PM_RESET_). */
/* port_data[Z], where Z - port number, holds the value of the port. */
int cur_data = 0x42;/* Initial value for Port 0, 1 and 2 */
int bb_open( void )
{
int init_ok = 0; /* Initialization OK */
int status = 0;
status = DeviceIoControl(
nt_device_handle, /* handle to device */
PGDC_IOCTL_PROCESS_LIST_PP, /* IO control code */
(LPVOID)port_io_buffer, /* IN buffer (list buffer) */
port_io_buffer_count * sizeof(struct PORT_IO_LIST_STRUCT),/* length of IN buffer in bytes */
(LPVOID)port_io_buffer, /* OUT buffer (list buffer) */
port_io_buffer_count * sizeof(struct PORT_IO_LIST_STRUCT),/* length of OUT buffer in bytes */
&n_writes, /* number of writes performed */
0); /* wait for operation to complete */
int bb_lptread( int port, int *data )
{
int temp = 0;
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 *)&temp, /* Buffer from driver. */
sizeof(int), /* Length of buffer in bytes. */
(ULONG *)&returned_length, /* Bytes placed in data_buffer. */
NULL); /* Wait for operation to complete */
int bb_lptwrite( int port, int data, int nbuffering )
{
int status = 0;
int returned_length = 0;
int buffer[2];
/* Collect up to [PORT_IO_BUFFER_SIZE] data for Port0, then flush them */
/* if nbuffering = 1 or Port = 1 or Port = 2, writing to the ports are done immediately */
if (port == 0 && nbuffering == 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;
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 */
if ( !status )
{
fprintf(stderr, "I/O error: Cannot write to ByteBlaster hardware!\n");
return CB_BB_LPTWRITE_ERROR;
}
}
return CB_OK;
}
int bb_read( int signal, int *data )
{
int temp = 0;
int status = 0;
status = bb_lptread( LPT_STATUS, &temp );
if ( status == CB_OK )
*data = (temp ^ 0x80) & signal;
return status;
}
int bb_write( int signal, int data )
{
int status = 0;
/* AND signal bit with '0', then OR with [data] */
int mask = ~signal;
cur_data = ( cur_data & mask ) | ( data * signal );
status = bb_lptwrite( LPT_DATA, cur_data, 0 );
return status;
}
int bb_reset( int mode )
{
int status = 0;
/* write to Port 0 and Port 2 with predefined values */
int control = mode ? 0x0C : 0x0E;
cur_data = 0x42;
status = bb_lptwrite( LPT_DATA, cur_data, 1 );
if ( status == CB_OK )
status = bb_lptwrite( LPT_CONTROL, control, 1 );
return status;
}