jp (Jam STAPL Player) Version 2.5
quick port to Linux ppdev - ByteBlaster support.

by: Michał Mirosław <mirq-linux@rere.qmqm.pl>

diff -urN jp_25/source.orig/jamport.h jp_25/source/jamport.h
--- jp_25/source.orig/jamport.h	2002-06-26 20:07:44.000000000 +0200
+++ jp_25/source/jamport.h	2007-04-06 02:29:10.000000000 +0200
@@ -27,14 +27,17 @@
 *	PORT = EMBEDDED means all DOS, WINDOWS, and UNIX code is excluded. Remaining
 *			code supports 16 and 32-bit compilers. Additional porting
 *			steps may be necessary. See readme file for more details.
+*
+*	PORT = LINUX    means Linux PPDEV (/dev/parportX)
 */
 
 #define DOS      2
 #define WINDOWS  3
 #define UNIX     4
 #define EMBEDDED 5
+#define LINUX    6
 
 /* change this line to build a different port */
-#define PORT WINDOWS
+#define PORT LINUX
 
 #endif /* INC_JAMPORT_H */
diff -urN jp_25/source.orig/jamstub.c jp_25/source/jamstub.c
--- jp_25/source.orig/jamstub.c	2004-05-26 15:36:12.000000000 +0200
+++ jp_25/source/jamstub.c	2007-04-06 02:45:34.000000000 +0200
@@ -52,9 +52,13 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#if PORT == DOS
 #include <io.h>
+#endif
 #include <fcntl.h>
+#if PORT == WINDOWS
 #include <process.h>
+#endif
 #if defined(USE_STATIC_MEMORY)
 	#define N_STATIC_MEMORY_KBYTES ((unsigned int) USE_STATIC_MEMORY)
 	#define N_STATIC_MEMORY_BYTES (N_STATIC_MEMORY_KBYTES * 1024)
@@ -64,7 +68,9 @@
 	#define POINTER_ALIGNMENT sizeof(BYTE)
 #endif /* USE_STATIC_MEMORY */
 #include <time.h>
+#if PORT == DOS
 #include <conio.h>
+#endif
 #include <ctype.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -73,6 +79,12 @@
 #include <bios.h>
 #endif
 
+#if PORT == LINUX
+#include <sys/ioctl.h>
+#include <linux/ppdev.h>
+#include <errno.h>
+#endif
+
 #include "jamexprt.h"
 
 #if PORT == WINDOWS
@@ -154,14 +166,18 @@
 	const DWORD END_GUARD = 0x76543210;
 #endif /* MEM_TRACKER */
 
-#if PORT == WINDOWS || PORT == DOS
+#if PORT == WINDOWS || PORT == DOS || PORT == LINUX
 /* parallel port interface available on PC only */
 BOOL specified_lpt_port = FALSE;
+#if PORT != LINUX
 BOOL specified_lpt_addr = FALSE;
+#endif
 int lpt_port = 1;
 int initial_lpt_ctrl = 0;
+#if PORT != LINUX
 WORD lpt_addr = 0x3bc;
 WORD lpt_addr_table[3] = { 0x3bc, 0x378, 0x278 };
+#endif
 BOOL alternative_cable_l = FALSE;
 BOOL alternative_cable_x = FALSE;
 void write_byteblaster(int port, int data);
@@ -319,7 +335,7 @@
 	}
 	else
 	{
-#if PORT == WINDOWS || PORT == DOS
+#if PORT == WINDOWS || PORT == DOS || PORT == LINUX
 		data = (alternative_cable_l ? ((tdi ? 0x01 : 0) | (tms ? 0x04 : 0)) :
 		       (alternative_cable_x ? ((tdi ? 0x01 : 0) | (tms ? 0x04 : 0) | 0x10) :
 		       ((tdi ? 0x40 : 0) | (tms ? 0x02 : 0))));
@@ -343,7 +359,11 @@
 #endif
 	}
 
+#if PORT != LINUX
 	if (tck_delay != 0) delay_loop(tck_delay);
+#else
+	usleep(tck_delay);
+#endif
 
 	return (tdo);
 }
@@ -465,8 +485,12 @@
 	if (windows_nt && (port_io_count > 0)) flush_ports();
 #endif
 
+#if PORT == UNIX || PORT == LINUX
+	usleep(microseconds);
+#else
 	delay_loop(microseconds *
 		((one_ms_delay / 1000L) + ((one_ms_delay % 1000L) ? 1 : 0)));
+#endif
 }
 
 int jam_vector_map
@@ -570,7 +594,7 @@
 		}
 		else
 		{
-#if PORT == WINDOWS || PORT == DOS
+#if PORT == WINDOWS || PORT == DOS || PORT == LINUX
 
 			write_byteblaster(0, data);
 
@@ -602,7 +626,7 @@
 		}
 		else
 		{
-#if PORT == WINDOWS || PORT == DOS
+#if PORT == WINDOWS || PORT == DOS || PORT == LINUX
 
 			data = read_byteblaster(1) ^ 0x80; /* parallel port inverts bit 7 */
 
@@ -962,7 +986,7 @@
 
 	for (arg = 1; arg < argc; arg++)
 	{
-#if PORT == UNIX
+#if PORT == UNIX || PORT == LINUX
 		if (argv[arg][0] == '-')
 #else
 		if ((argv[arg][0] == '-') || (argv[arg][0] == '/'))
@@ -975,7 +999,7 @@
 				if (action[0] == '"') ++action;
 				break;
 
-#if PORT == WINDOWS || PORT == DOS
+#if PORT == WINDOWS || PORT == DOS || PORT == LINUX
 			case 'C':				/* Use alternative ISP download cable */
 				if(toupper(argv[arg][2]) == 'L')
 					alternative_cable_l = TRUE;
@@ -996,11 +1020,12 @@
 				init_list[++init_count] = NULL;
 				break;
 
-#if PORT == WINDOWS || PORT == DOS
+#if PORT == WINDOWS || PORT == DOS || PORT == LINUX
 			case 'P':				/* set LPT port address */
 				specified_lpt_port = TRUE;
 				if (sscanf(&argv[arg][2], "%d", &lpt_port) != 1) error = TRUE;
 				if ((lpt_port < 1) || (lpt_port > 3)) error = TRUE;
+#if PORT != LINUX
 				if (error)
 				{
 					if (sscanf(&argv[arg][2], "%x", &lpt_port) == 1)
@@ -1019,6 +1044,7 @@
 						}
 					}
 				}
+#endif
 				break;
 #endif
 
@@ -1072,7 +1098,7 @@
 		}
 	}
 
-#if PORT == WINDOWS || PORT == DOS
+#if PORT == WINDOWS || PORT == DOS || PORT == LINUX
 	if (specified_lpt_port && specified_com_port)
 	{
 		fprintf(stderr, "Error:  -s and -p options may not be used together\n\n");
@@ -1090,7 +1116,7 @@
 		fprintf(stderr, "    -d<var=val> : initialize variable to specified value (Jam 1.1)\n");
 		fprintf(stderr, "    -d<proc=1>  : enable optional procedure (Jam STAPL)\n");
 		fprintf(stderr, "    -d<proc=0>  : disable recommended procedure (Jam STAPL)\n");
-#if PORT == WINDOWS || PORT == DOS
+#if PORT == WINDOWS || PORT == DOS || PORT == LINUX
 		fprintf(stderr, "    -p<port>    : parallel port number or address (for ByteBlaster)\n");
 		fprintf(stderr, "    -c<cable>   : alternative download cable compatibility: -cl or -cx\n");
 #endif
@@ -1695,7 +1721,7 @@
 	}
 	else
 	{
-#if PORT == WINDOWS || PORT == DOS
+#if PORT == WINDOWS || PORT == DOS || PORT == LINUX
 
 #if PORT == WINDOWS
 		if (windows_nt)
@@ -1745,9 +1771,8 @@
 			initial_lpt_ctrl = windows_nt ? 0x0c : read_byteblaster(2);
 #endif
 		}
-#endif
 
-#if PORT == DOS
+#elif PORT == DOS
 		/*
 		*	Read word at specific memory address to get the LPT port address
 		*/
@@ -1768,6 +1793,23 @@
 			}
 		}
 		initial_lpt_ctrl = read_byteblaster(2);
+#elif PORT == LINUX
+		char ppdevname[1000];
+
+		snprintf(ppdevname, sizeof(ppdevname), "/dev/parport%d", lpt_port-1);
+		ppdevname[sizeof(ppdevname)-1] = 0;
+
+		com_port = open( ppdevname, O_RDWR );
+		if (com_port < 0) {
+			fprintf(stderr, "Error: ppdev '%s' open failed: %d\n", ppdevname, errno);
+			exit(1);
+		}
+
+		if ((ioctl( com_port, PPEXCL ) == -1) || (ioctl( com_port, PPCLAIM ) == -1)) {
+			close(com_port);
+			fprintf(stderr, "Error: ppdev '%s' excl+claim failed: %d\n", ppdevname, errno);
+			exit(1);
+		}
 #endif
 
 		/* set AUTO-FEED low to enable ByteBlaster (value to port inverted) */
@@ -1785,7 +1827,7 @@
 	}
 	else
 	{
-#if PORT == WINDOWS || PORT == DOS
+#if PORT == WINDOWS || PORT == DOS || PORT == LINUX
 		/* set AUTO-FEED high to disable ByteBlaster */
 		write_byteblaster(2, initial_lpt_ctrl & 0xfd);
 
@@ -1796,6 +1838,12 @@
 
 			CloseHandle(nt_device_handle);
 		}
+
+#elif PORT == LINUX
+		if ( com_port != -1 ) {
+			ioctl( com_port, PPRELEASE );
+			close( com_port );
+		}
 #endif
 #endif
 	}
@@ -2042,11 +2090,58 @@
 #endif /* PORT == WINDOWS */
 #endif /* PORT == WINDOWS || PORT == DOS */
 
+#if PORT == LINUX
+
+void write_byteblaster ( int port, int data )
+{
+	switch (port) {
+	case 0:	/* data */
+		ioctl( com_port, PPWDATA, &data );
+		break;
+	case 2: /* control */
+		ioctl( com_port, PPWCONTROL, &data );
+		break;
+	case 1: /* status */
+		fprintf(stderr, "I/O error: ppdev STATUS is read-only\n");
+		exit(1);
+	default:
+		fprintf(stderr, "write_byteblaster: assertion failed\n");
+		exit(1);
+	}
+}
+
+
+int read_byteblaster ( int port )
+{
+	int data = 0;
+
+	switch (port) {
+	case 0: /* data */
+		ioctl( com_port, PPRDATA, &data );
+		return data;
+	case 1: /* status */
+		ioctl( com_port, PPRSTATUS, &data );
+		return data;
+	case 2: /* control */
+		ioctl( com_port, PPRCONTROL, &data );
+		return data;
+	default:
+		fprintf(stderr, "write_byteblaster: assertion failed\n");
+		exit(1);
+	}
+}
+
+#endif /* PORT == LINUX */
+
 #if !defined (DEBUG)
 #pragma optimize ("ceglt", off)
 #endif
 
+#if PORT != UNIX && PORT != LINUX
+
 void delay_loop(long count)
 {
 	while (count != 0L) count--;
 }
+
+#endif
