From 051bf12ec7907e8f88f31bfb92418888a1a3505f Mon Sep 17 00:00:00 2001
From: Aurelien Jarno <aurelien@aurel32.net>
Date: Sat, 1 Apr 2023 21:44:23 +0200
Subject: Import flexloader-0.01.tar.gz

---
 src/Makefile.am   |  10 ++
 src/Makefile.in   | 401 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/byteblaster.c | 219 +++++++++++++++++++++++++++++
 src/byteblaster.h |  38 ++++++
 src/flexloader.c  | 233 +++++++++++++++++++++++++++++++
 src/flexloader.h  |  29 ++++
 src/main.c        | 141 +++++++++++++++++++
 src/main.h        |  30 ++++
 8 files changed, 1101 insertions(+)
 create mode 100644 src/Makefile.am
 create mode 100644 src/Makefile.in
 create mode 100644 src/byteblaster.c
 create mode 100644 src/byteblaster.h
 create mode 100644 src/flexloader.c
 create mode 100644 src/flexloader.h
 create mode 100644 src/main.c
 create mode 100644 src/main.h

(limited to 'src')

diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000..01677d3
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,10 @@
+CLEANFILES = *~ *.bb *.bbg
+
+bin_PROGRAMS = flexloader
+flexloader_SOURCES = byteblaster.h \
+                     byteblaster.c \
+                     flexloader.h \
+                     flexloader.c \
+                     main.h \
+                     main.c
+
diff --git a/src/Makefile.in b/src/Makefile.in
new file mode 100644
index 0000000..0f0b360
--- /dev/null
+++ b/src/Makefile.in
@@ -0,0 +1,401 @@
+# Makefile.in generated by automake 1.7.9 from Makefile.am.
+# @configure_input@
+
+# Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
+# Free Software Foundation, Inc.
+# This Makefile.in is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
+# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE.
+
+@SET_MAKE@
+
+srcdir = @srcdir@
+top_srcdir = @top_srcdir@
+VPATH = @srcdir@
+pkgdatadir = $(datadir)/@PACKAGE@
+pkglibdir = $(libdir)/@PACKAGE@
+pkgincludedir = $(includedir)/@PACKAGE@
+top_builddir = ..
+
+am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd
+INSTALL = @INSTALL@
+install_sh_DATA = $(install_sh) -c -m 644
+install_sh_PROGRAM = $(install_sh) -c
+install_sh_SCRIPT = $(install_sh) -c
+INSTALL_HEADER = $(INSTALL_DATA)
+transform = $(program_transform_name)
+NORMAL_INSTALL = :
+PRE_INSTALL = :
+POST_INSTALL = :
+NORMAL_UNINSTALL = :
+PRE_UNINSTALL = :
+POST_UNINSTALL = :
+host_triplet = @host@
+ACLOCAL = @ACLOCAL@
+AMDEP_FALSE = @AMDEP_FALSE@
+AMDEP_TRUE = @AMDEP_TRUE@
+AMTAR = @AMTAR@
+AUTOCONF = @AUTOCONF@
+AUTOHEADER = @AUTOHEADER@
+AUTOMAKE = @AUTOMAKE@
+AWK = @AWK@
+CC = @CC@
+CCDEPMODE = @CCDEPMODE@
+CFLAGS = @CFLAGS@
+CPP = @CPP@
+CPPFLAGS = @CPPFLAGS@
+CYGPATH_W = @CYGPATH_W@
+DEFS = @DEFS@
+DEPDIR = @DEPDIR@
+ECHO_C = @ECHO_C@
+ECHO_N = @ECHO_N@
+ECHO_T = @ECHO_T@
+EGREP = @EGREP@
+EXEEXT = @EXEEXT@
+INSTALL_DATA = @INSTALL_DATA@
+INSTALL_PROGRAM = @INSTALL_PROGRAM@
+INSTALL_SCRIPT = @INSTALL_SCRIPT@
+INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@
+LDFLAGS = @LDFLAGS@
+LIBOBJS = @LIBOBJS@
+LIBS = @LIBS@
+LTLIBOBJS = @LTLIBOBJS@
+MAKEINFO = @MAKEINFO@
+OBJEXT = @OBJEXT@
+PACKAGE = @PACKAGE@
+PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@
+PACKAGE_NAME = @PACKAGE_NAME@
+PACKAGE_STRING = @PACKAGE_STRING@
+PACKAGE_TARNAME = @PACKAGE_TARNAME@
+PACKAGE_VERSION = @PACKAGE_VERSION@
+PATH_SEPARATOR = @PATH_SEPARATOR@
+RANLIB = @RANLIB@
+SET_MAKE = @SET_MAKE@
+SHELL = @SHELL@
+STRIP = @STRIP@
+VERSION = @VERSION@
+ac_ct_CC = @ac_ct_CC@
+ac_ct_RANLIB = @ac_ct_RANLIB@
+ac_ct_STRIP = @ac_ct_STRIP@
+am__fastdepCC_FALSE = @am__fastdepCC_FALSE@
+am__fastdepCC_TRUE = @am__fastdepCC_TRUE@
+am__include = @am__include@
+am__leading_dot = @am__leading_dot@
+am__quote = @am__quote@
+bindir = @bindir@
+build = @build@
+build_alias = @build_alias@
+build_cpu = @build_cpu@
+build_os = @build_os@
+build_vendor = @build_vendor@
+datadir = @datadir@
+exec_prefix = @exec_prefix@
+host = @host@
+host_alias = @host_alias@
+host_cpu = @host_cpu@
+host_os = @host_os@
+host_vendor = @host_vendor@
+includedir = @includedir@
+infodir = @infodir@
+install_sh = @install_sh@
+libdir = @libdir@
+libexecdir = @libexecdir@
+localstatedir = @localstatedir@
+mandir = @mandir@
+oldincludedir = @oldincludedir@
+prefix = @prefix@
+program_transform_name = @program_transform_name@
+sbindir = @sbindir@
+sharedstatedir = @sharedstatedir@
+sysconfdir = @sysconfdir@
+target_alias = @target_alias@
+CLEANFILES = *~ *.bb *.bbg
+
+bin_PROGRAMS = flexloader
+flexloader_SOURCES = byteblaster.h \
+                     byteblaster.c \
+                     flexloader.h \
+                     flexloader.c \
+                     main.h \
+                     main.c
+
+subdir = src
+ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
+mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
+CONFIG_HEADER = $(top_builddir)/config.h
+CONFIG_CLEAN_FILES =
+bin_PROGRAMS = flexloader$(EXEEXT)
+PROGRAMS = $(bin_PROGRAMS)
+
+am_flexloader_OBJECTS = byteblaster.$(OBJEXT) flexloader.$(OBJEXT) \
+	main.$(OBJEXT)
+flexloader_OBJECTS = $(am_flexloader_OBJECTS)
+flexloader_LDADD = $(LDADD)
+flexloader_DEPENDENCIES =
+flexloader_LDFLAGS =
+
+DEFAULT_INCLUDES =  -I. -I$(srcdir) -I$(top_builddir)
+depcomp = $(SHELL) $(top_srcdir)/depcomp
+am__depfiles_maybe = depfiles
+@AMDEP_TRUE@DEP_FILES = ./$(DEPDIR)/byteblaster.Po \
+@AMDEP_TRUE@	./$(DEPDIR)/flexloader.Po ./$(DEPDIR)/main.Po
+COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
+	$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
+CCLD = $(CC)
+LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@
+DIST_SOURCES = $(flexloader_SOURCES)
+DIST_COMMON = $(srcdir)/Makefile.in Makefile.am
+SOURCES = $(flexloader_SOURCES)
+
+all: all-am
+
+.SUFFIXES:
+.SUFFIXES: .c .o .obj
+$(srcdir)/Makefile.in:  Makefile.am  $(top_srcdir)/configure.ac $(ACLOCAL_M4)
+	cd $(top_srcdir) && \
+	  $(AUTOMAKE) --foreign  src/Makefile
+Makefile:  $(srcdir)/Makefile.in  $(top_builddir)/config.status
+	cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)
+binPROGRAMS_INSTALL = $(INSTALL_PROGRAM)
+install-binPROGRAMS: $(bin_PROGRAMS)
+	@$(NORMAL_INSTALL)
+	$(mkinstalldirs) $(DESTDIR)$(bindir)
+	@list='$(bin_PROGRAMS)'; for p in $$list; do \
+	  p1=`echo $$p|sed 's/$(EXEEXT)$$//'`; \
+	  if test -f $$p \
+	  ; then \
+	    f=`echo "$$p1" | sed 's,^.*/,,;$(transform);s/$$/$(EXEEXT)/'`; \
+	   echo " $(INSTALL_PROGRAM_ENV) $(binPROGRAMS_INSTALL) $$p $(DESTDIR)$(bindir)/$$f"; \
+	   $(INSTALL_PROGRAM_ENV) $(binPROGRAMS_INSTALL) $$p $(DESTDIR)$(bindir)/$$f || exit 1; \
+	  else :; fi; \
+	done
+
+uninstall-binPROGRAMS:
+	@$(NORMAL_UNINSTALL)
+	@list='$(bin_PROGRAMS)'; for p in $$list; do \
+	  f=`echo "$$p" | sed 's,^.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/'`; \
+	  echo " rm -f $(DESTDIR)$(bindir)/$$f"; \
+	  rm -f $(DESTDIR)$(bindir)/$$f; \
+	done
+
+clean-binPROGRAMS:
+	-test -z "$(bin_PROGRAMS)" || rm -f $(bin_PROGRAMS)
+flexloader$(EXEEXT): $(flexloader_OBJECTS) $(flexloader_DEPENDENCIES) 
+	@rm -f flexloader$(EXEEXT)
+	$(LINK) $(flexloader_LDFLAGS) $(flexloader_OBJECTS) $(flexloader_LDADD) $(LIBS)
+
+mostlyclean-compile:
+	-rm -f *.$(OBJEXT) core *.core
+
+distclean-compile:
+	-rm -f *.tab.c
+
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/byteblaster.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/flexloader.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@
+
+.c.o:
+@am__fastdepCC_TRUE@	if $(COMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" \
+@am__fastdepCC_TRUE@	  -c -o $@ `test -f '$<' || echo '$(srcdir)/'`$<; \
+@am__fastdepCC_TRUE@	then mv -f "$(DEPDIR)/$*.Tpo" "$(DEPDIR)/$*.Po"; \
+@am__fastdepCC_TRUE@	else rm -f "$(DEPDIR)/$*.Tpo"; exit 1; \
+@am__fastdepCC_TRUE@	fi
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	depfile='$(DEPDIR)/$*.Po' tmpdepfile='$(DEPDIR)/$*.TPo' @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@	$(COMPILE) -c `test -f '$<' || echo '$(srcdir)/'`$<
+
+.c.obj:
+@am__fastdepCC_TRUE@	if $(COMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" \
+@am__fastdepCC_TRUE@	  -c -o $@ `if test -f '$<'; then $(CYGPATH_W) '$<'; else $(CYGPATH_W) '$(srcdir)/$<'; fi`; \
+@am__fastdepCC_TRUE@	then mv -f "$(DEPDIR)/$*.Tpo" "$(DEPDIR)/$*.Po"; \
+@am__fastdepCC_TRUE@	else rm -f "$(DEPDIR)/$*.Tpo"; exit 1; \
+@am__fastdepCC_TRUE@	fi
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	depfile='$(DEPDIR)/$*.Po' tmpdepfile='$(DEPDIR)/$*.TPo' @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@	$(COMPILE) -c `if test -f '$<'; then $(CYGPATH_W) '$<'; else $(CYGPATH_W) '$(srcdir)/$<'; fi`
+uninstall-info-am:
+
+ETAGS = etags
+ETAGSFLAGS =
+
+CTAGS = ctags
+CTAGSFLAGS =
+
+tags: TAGS
+
+ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES)
+	list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
+	unique=`for i in $$list; do \
+	    if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
+	  done | \
+	  $(AWK) '    { files[$$0] = 1; } \
+	       END { for (i in files) print i; }'`; \
+	mkid -fID $$unique
+
+TAGS:  $(HEADERS) $(SOURCES)  $(TAGS_DEPENDENCIES) \
+		$(TAGS_FILES) $(LISP)
+	tags=; \
+	here=`pwd`; \
+	list='$(SOURCES) $(HEADERS)  $(LISP) $(TAGS_FILES)'; \
+	unique=`for i in $$list; do \
+	    if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
+	  done | \
+	  $(AWK) '    { files[$$0] = 1; } \
+	       END { for (i in files) print i; }'`; \
+	test -z "$(ETAGS_ARGS)$$tags$$unique" \
+	  || $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \
+	     $$tags $$unique
+
+ctags: CTAGS
+CTAGS:  $(HEADERS) $(SOURCES)  $(TAGS_DEPENDENCIES) \
+		$(TAGS_FILES) $(LISP)
+	tags=; \
+	here=`pwd`; \
+	list='$(SOURCES) $(HEADERS)  $(LISP) $(TAGS_FILES)'; \
+	unique=`for i in $$list; do \
+	    if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
+	  done | \
+	  $(AWK) '    { files[$$0] = 1; } \
+	       END { for (i in files) print i; }'`; \
+	test -z "$(CTAGS_ARGS)$$tags$$unique" \
+	  || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \
+	     $$tags $$unique
+
+GTAGS:
+	here=`$(am__cd) $(top_builddir) && pwd` \
+	  && cd $(top_srcdir) \
+	  && gtags -i $(GTAGS_ARGS) $$here
+
+distclean-tags:
+	-rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags
+DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
+
+top_distdir = ..
+distdir = $(top_distdir)/$(PACKAGE)-$(VERSION)
+
+distdir: $(DISTFILES)
+	@srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; \
+	topsrcdirstrip=`echo "$(top_srcdir)" | sed 's|.|.|g'`; \
+	list='$(DISTFILES)'; for file in $$list; do \
+	  case $$file in \
+	    $(srcdir)/*) file=`echo "$$file" | sed "s|^$$srcdirstrip/||"`;; \
+	    $(top_srcdir)/*) file=`echo "$$file" | sed "s|^$$topsrcdirstrip/|$(top_builddir)/|"`;; \
+	  esac; \
+	  if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \
+	  dir=`echo "$$file" | sed -e 's,/[^/]*$$,,'`; \
+	  if test "$$dir" != "$$file" && test "$$dir" != "."; then \
+	    dir="/$$dir"; \
+	    $(mkinstalldirs) "$(distdir)$$dir"; \
+	  else \
+	    dir=''; \
+	  fi; \
+	  if test -d $$d/$$file; then \
+	    if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \
+	      cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \
+	    fi; \
+	    cp -pR $$d/$$file $(distdir)$$dir || exit 1; \
+	  else \
+	    test -f $(distdir)/$$file \
+	    || cp -p $$d/$$file $(distdir)/$$file \
+	    || exit 1; \
+	  fi; \
+	done
+check-am: all-am
+check: check-am
+all-am: Makefile $(PROGRAMS)
+
+installdirs:
+	$(mkinstalldirs) $(DESTDIR)$(bindir)
+install: install-am
+install-exec: install-exec-am
+install-data: install-data-am
+uninstall: uninstall-am
+
+install-am: all-am
+	@$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am
+
+installcheck: installcheck-am
+install-strip:
+	$(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \
+	  install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \
+	  `test -z '$(STRIP)' || \
+	    echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install
+mostlyclean-generic:
+
+clean-generic:
+	-test -z "$(CLEANFILES)" || rm -f $(CLEANFILES)
+
+distclean-generic:
+	-rm -f $(CONFIG_CLEAN_FILES)
+
+maintainer-clean-generic:
+	@echo "This command is intended for maintainers to use"
+	@echo "it deletes files that may require special tools to rebuild."
+clean: clean-am
+
+clean-am: clean-binPROGRAMS clean-generic mostlyclean-am
+
+distclean: distclean-am
+	-rm -rf ./$(DEPDIR)
+	-rm -f Makefile
+distclean-am: clean-am distclean-compile distclean-generic \
+	distclean-tags
+
+dvi: dvi-am
+
+dvi-am:
+
+info: info-am
+
+info-am:
+
+install-data-am:
+
+install-exec-am: install-binPROGRAMS
+
+install-info: install-info-am
+
+install-man:
+
+installcheck-am:
+
+maintainer-clean: maintainer-clean-am
+	-rm -rf ./$(DEPDIR)
+	-rm -f Makefile
+maintainer-clean-am: distclean-am maintainer-clean-generic
+
+mostlyclean: mostlyclean-am
+
+mostlyclean-am: mostlyclean-compile mostlyclean-generic
+
+pdf: pdf-am
+
+pdf-am:
+
+ps: ps-am
+
+ps-am:
+
+uninstall-am: uninstall-binPROGRAMS uninstall-info-am
+
+.PHONY: CTAGS GTAGS all all-am check check-am clean clean-binPROGRAMS \
+	clean-generic ctags distclean distclean-compile \
+	distclean-generic distclean-tags distdir dvi dvi-am info \
+	info-am install install-am install-binPROGRAMS install-data \
+	install-data-am install-exec install-exec-am install-info \
+	install-info-am install-man install-strip installcheck \
+	installcheck-am installdirs maintainer-clean \
+	maintainer-clean-generic mostlyclean mostlyclean-compile \
+	mostlyclean-generic pdf pdf-am ps ps-am tags uninstall \
+	uninstall-am uninstall-binPROGRAMS uninstall-info-am
+
+# Tell versions [3.59,3.63) of GNU make to not export all variables.
+# Otherwise a system limit (for SysV at least) may be exceeded.
+.NOEXPORT:
diff --git a/src/byteblaster.c b/src/byteblaster.c
new file mode 100644
index 0000000..838c828
--- /dev/null
+++ b/src/byteblaster.c
@@ -0,0 +1,219 @@
+/***************************************************************************
+         byteblaster.c - Low-level functions to access the byteblaster
+         -------------------------------------------------------------
+			    
+    begin                : Sun Jan 09 2005
+    copyright            : (C) 2005 by Aurelien Jarno
+    email                : aurelien@aurel32.net
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+/* Application specific includes */
+#include "byteblaster.h"
+#include "main.h"
+
+/* Standard includes */
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <linux/ppdev.h>
+#include <linux/parport.h>
+#include <sys/ioctl.h>
+
+/***************************************************************************
+ *                                                                         *
+ *   Private functions                                                     *
+ *                                                                         *
+ ***************************************************************************/
+
+static void out_data(int device, unsigned char mask, unsigned char val)
+{
+	static unsigned char data = 0;
+
+	data = (data & ~mask) | val;
+	ioctl(device, PPWDATA, &data);
+}
+
+static void set_AUTOFEED(int device, int value)
+{
+	struct ppdev_frob_struct frob;
+
+	frob.mask = PARPORT_CONTROL_AUTOFD;
+	frob.val = (value != 0) ? PARPORT_CONTROL_AUTOFD : 0;
+	ioctl(device, PPFCONTROL, &frob);
+	usleep(100000);
+}
+
+static void set_D5(int device, int value)
+{
+	/* D5 */
+	out_data(device, 0x20, (value != 0)? 0x20 : 0x00);
+}
+
+static int get_ACK(int device)
+{
+        unsigned char status;
+	
+	ioctl(device, PPRSTATUS, &status);
+	return (status & PARPORT_STATUS_ACK) == PARPORT_STATUS_ACK;
+}
+
+static int open_parport(char *device_name)
+{
+	int dev;
+
+	if (debug)
+		printf("Opening %s\n", device_name);
+
+	dev = open(device_name, O_RDWR);
+	if (dev < 0)
+	{
+		if (!quiet)
+			perror("open");
+		return -1;
+	}
+	if (ioctl(dev, PPCLAIM) < 0)
+	{
+		if (!quiet)
+			perror("open");
+		perror("ioctl");
+		close(dev);
+		return -1;
+	}
+	return dev;
+}
+
+static void close_parport(int device)
+{
+	if (device)
+	{
+		ioctl(device, PPRELEASE);
+		close(device);
+	}
+}
+
+static int detect_byteblaster(int device)
+{
+	if (debug)
+		printf("Detecting byteblaster\n");
+
+	/* ACK and D5 are connected together */
+	if (debug)
+		printf("Driving D5 low\n");
+	set_D5(device, 0);
+	usleep(10000);
+	if (get_ACK(device) == 1)
+	{
+		if (debug)
+			fprintf(stderr, "ACK stuck high\n");
+		return -1;
+	}
+	
+	if (debug)
+		printf("Driving D5 high\n");
+	set_D5(device, 1);
+	usleep(10000);
+	if (get_ACK(device) == 0)
+	{
+		if (debug)
+			fprintf(stderr, "ACK stuck low\n");
+		return -1;
+	}
+	
+	if (debug)
+		printf("Driving D5 low\n");
+	set_D5(device, 0);
+	usleep(10000);
+	if (get_ACK(device) == 1)
+	{
+		if (debug)
+			fprintf(stderr, "ACK stuck high\n");
+		return -1;
+	}
+
+	return 0;
+}
+
+
+/***************************************************************************
+ *                                                                         *
+ *   Public functions                                                      *
+ *                                                                         *
+ ***************************************************************************/
+
+int get_nSTATUS(int device)
+{
+        unsigned char status;
+	
+	ioctl(device, PPRSTATUS, &status);
+	return (status & PARPORT_STATUS_SELECT) == PARPORT_STATUS_SELECT;
+}
+
+int get_CONF_DONE(int device)
+{
+	unsigned char status;
+
+	ioctl(device, PPRSTATUS, &status);
+	return (status & PARPORT_STATUS_BUSY) != PARPORT_STATUS_BUSY;
+}
+
+void set_DCLK(int device, int value)
+{
+	/* D0 */
+	out_data(device, 0x01, (value != 0)? 0x01 : 0x00);
+}
+
+void set_nCONFIG(int device, int value)
+{
+	/* D1 */
+	out_data(device, 0x02, (value != 0)? 0x02 : 0x00);
+}
+
+void set_DATA0(int device, int value)
+{
+	/* D6 */
+	out_data(device, 0x40, (value != 0)? 0x40 : 0x00);
+}
+
+int open_byteblaster(char *devicename)
+{
+	int dev;
+	
+	dev = open_parport(devicename);
+	if (dev < 0 )
+	{
+		if (!quiet)
+			fprintf(stderr, "Can't open %s!\n", devicename);
+		return -1;
+	}
+	
+	if (detect_byteblaster(dev) < 0)
+	{
+		if (!quiet)
+			fprintf(stderr, "Byteblaster not found on %s!\n", devicename);
+		close_byteblaster(dev);
+		return -1;	
+	}
+	
+	return dev;
+}
+
+void close_byteblaster(int device)
+{
+	enable_byteblaster(device, 0);
+	close_parport(device);
+}
+
+void enable_byteblaster(int device, int value)
+{
+	set_AUTOFEED(device, value);
+}
+
diff --git a/src/byteblaster.h b/src/byteblaster.h
new file mode 100644
index 0000000..5b84389
--- /dev/null
+++ b/src/byteblaster.h
@@ -0,0 +1,38 @@
+/***************************************************************************
+         byteblaster.c - Low-level functions to access the byteblaster
+         -------------------------------------------------------------
+			    
+    begin                : Sun Jan 09 2005
+    copyright            : (C) 2005 by Aurelien Jarno
+    email                : aurelien@aurel32.net
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef BYTEBLASTER_H
+#define BYTEBLASTER_H
+
+#ifdef HAVE_CONFIG_H
+  #include <config.h>
+#endif
+
+/* Subroutines */
+int open_byteblaster(char *devicename);
+void close_byteblaster(int device);
+void enable_byteblaster(int device, int value);
+
+void set_DCLK(int device, int value);
+void set_nCONFIG(int device, int value);
+void set_DATA0(int device, int value);
+
+int get_nSTATUS(int device);
+int get_CONF_DONE(int device);
+
+#endif
diff --git a/src/flexloader.c b/src/flexloader.c
new file mode 100644
index 0000000..324fa48
--- /dev/null
+++ b/src/flexloader.c
@@ -0,0 +1,233 @@
+/***************************************************************************
+       flexloader.c - Routines to download code to an Altera FLEX FPGA
+       ---------------------------------------------------------------
+			    
+    begin                : Sun Jan 09 2004
+    copyright            : (C) 2005 by Aurelien Jarno
+    email                : aurelien@aurel32.net
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+/* Application specific includes */
+#include "flexloader.h"
+#include "byteblaster.h"
+#include "main.h"
+
+/* Standard includes */
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+
+/***************************************************************************
+ *                                                                         *
+ *   Declarations                                                          *
+ *                                                                         *
+ ***************************************************************************/
+
+#define TIMEOUT 	500	/* ms */
+
+
+/***************************************************************************
+ *                                                                         *
+ *   Private functions                                                     *
+ *                                                                         *
+ ***************************************************************************/
+
+static int flex_config(int device)
+{
+	int t; 
+	
+	set_DCLK(device, 0);
+	set_DATA0(device, 1);
+	set_nCONFIG(device, 1);
+	enable_byteblaster(device, 1);
+	usleep(100000);
+
+	if (debug)
+		printf("Configuring FLEX\n");
+	
+	for (t = TIMEOUT / 10 ; t > 0 ; t--)
+	{
+		if (get_nSTATUS(device) == 1)
+			break;
+		usleep(10000);
+	}
+	if (t == 0) 
+	{
+		if (!quiet)
+			fprintf(stderr, "nSTATUS != 1\n");
+		return -1;
+	}
+
+	set_nCONFIG(device, 0);
+	
+	for (t = TIMEOUT / 10 ; t > 0 ; t--)
+	{
+		if (get_nSTATUS(device) == 0)
+			break;
+		usleep(10000);
+	}
+	if (t == 0)
+	{
+		if (!quiet)
+			fprintf(stderr, "nSTATUS != 0\n");
+		return -1;
+	}
+	
+	for (t = TIMEOUT / 10 ; t > 0 ; t--)
+	{
+		if (get_CONF_DONE(device) == 0)
+			break;
+		usleep(10000);
+	}
+	if (t == 0)
+	{
+		if (!quiet)
+			fprintf(stderr, "CONF_DONE != 0\n");
+		return -1;
+	}
+
+	set_nCONFIG(device, 1);
+	
+	for (t = TIMEOUT / 10 ; t > 0 ; t--)
+	{
+		if (get_nSTATUS(device) == 1)
+			break;
+		usleep(10000);
+	}
+	if (t == 0)
+	{
+		if (!quiet)
+			fprintf(stderr, "nSTATUS != 1\n");
+		return -1;
+	}
+
+	usleep(40000);
+	return 0;
+}
+
+static int flex_download(int device, int file)
+{
+	int byte, bit;
+	unsigned char data;
+
+	if (debug)
+		printf("Downloading code\n");
+
+	for(byte = 0 ; ; byte++)
+	{
+		if (debug)
+			printf("Downloading byte %i\n", byte);
+
+		if (read(file, &data, 1) != 1)
+		{
+			if (!quiet)
+				fprintf(stderr, "Unexpected end of file\n");
+			return -1;
+		}
+		
+		for (bit = 0 ; bit < 8 ; bit++)
+		{
+			set_DATA0(device, data & 0x01);
+			set_DCLK(device, 1);
+			set_DCLK(device, 0);
+			data >>= 1;
+			
+			if (get_nSTATUS(device) == 0)
+			{
+				if (!quiet)
+					fprintf(stderr, "Checksum error\n");
+				return -1;
+			}
+
+			if (get_CONF_DONE(device) == 1)
+			{
+				if (!quiet)
+					printf("%i bytes downloaded\n", byte);
+				return 0;
+			}
+		}
+	}
+}
+	
+static void flex_init(int device)
+{
+	int i;
+	
+	if (debug)
+		printf("Initializing FLEX\n");
+
+	/* more clocks required to initialize the device */
+	for (i = 0 ; i < 10 ; i++)
+	{
+		set_DCLK(device, 1);
+		set_DCLK(device, 0);
+	}
+}
+
+
+/***************************************************************************
+ *                                                                         *
+ *   Public functions                                                      *
+ *                                                                         *
+ ***************************************************************************/
+
+int flex_download_program(char *device, char *rbf_file)
+{
+	int fd;
+	int byteblaster;
+
+	if (debug)
+		printf("Opening %s\n", rbf_file);
+
+	fd = open(rbf_file, O_RDONLY);
+
+	if (fd < 0)
+	{
+		if (!quiet)
+		{
+			perror("open");
+			fprintf(stderr, "Can't open %s!\n", rbf_file);
+		}
+		return -1;
+	}
+
+	byteblaster = open_byteblaster(device);
+
+	if (byteblaster < 0)
+		return -1;
+		
+	if (flex_config(byteblaster) < 0)
+	{
+		if (!quiet)
+			fprintf(stderr, "Altera FLEX not found!\n");
+		return -1;
+	}
+	
+	if (flex_download(byteblaster, fd) < 0)
+	{
+		if (!quiet)
+			fprintf(stderr, "Download failed\n");
+		return -1;
+	}
+	
+	flex_init(byteblaster);
+
+	close_byteblaster(byteblaster);
+
+	close(fd);
+	return 0;
+}
+
diff --git a/src/flexloader.h b/src/flexloader.h
new file mode 100644
index 0000000..3031c1c
--- /dev/null
+++ b/src/flexloader.h
@@ -0,0 +1,29 @@
+/***************************************************************************
+       flexloader.h - Routines to download code to an Altera FLEX FPGA
+       ---------------------------------------------------------------
+			    
+    begin                : Sun Jan 09 2005
+    copyright            : (C) 2005 by Aurelien Jarno
+    email                : aurelien@aurel32.net
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef FLEXLOADER_H
+#define FLEXLOADER_H
+
+#ifdef HAVE_CONFIG_H
+  #include <config.h>
+#endif
+
+/* Subroutines */
+int flex_download_program(char *device, char *rbf_file);
+
+#endif
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..8310c89
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,141 @@
+/***************************************************************************
+                             main.c - Main program
+                             ---------------------
+			    
+    begin                : Sun Jan 09 2005
+    copyright            : (C) 2005 by Aurelien Jarno
+    email                : aurelien@aurel32.net
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+/* Application specific includes */
+#include "main.h"
+#include "flexloader.h"
+
+/* Standard includes */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <sys/shm.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+/***************************************************************************
+ *                                                                         *
+ *   Declarations                                                          *
+ *                                                                         *
+ ***************************************************************************/
+
+/* Variables */
+int debug = 0;
+int quiet = 0;
+
+
+/***************************************************************************
+ *                                                                         *
+ *   Functions                                                             *
+ *                                                                         *
+ ***************************************************************************/
+
+static void print_usage(void)
+{
+	printf("Usage: "PACKAGE" parport_device rbf_file\n"
+	       "\n"
+	       "Valid options are:\n"
+	       "  -d,       --debug             Print debug messages.\n"
+	       "  -h,       --help              Print this message.\n"
+	       "  -q,       --quiet             Don't print any message.\n"
+	       "  -v,       --version           Display "PACKAGE" version number.\n"
+	       "\n");
+}
+
+static void print_banner(void)
+{
+	if (!quiet)
+	{
+		printf(PACKAGE" version "VERSION"\n");
+		printf("(C) 2005 Aurelien Jarno\n\n");
+	}
+}
+
+int main(int argc, char* argv[])
+{
+	int c;
+	int lindex = 0;
+	
+	static struct option long_options[] = 
+	{
+		{"debug",           no_argument,       NULL, 'd'},
+		{"help",            no_argument,       NULL, 'h'},
+		{"quiet",           no_argument,       NULL, 'q'},
+		{"version",         no_argument,       NULL, 'v'},
+		{0, 0, 0, 0}
+	};      
+
+	while ((c = getopt_long (argc, argv, "dhqv", long_options, &lindex)) != -1)
+	{
+		printf("%c\n",c);
+		switch (c)
+		{
+			case 'd':
+				debug = 1;
+				break;
+				
+			case 'h':
+				print_banner();
+				print_usage();
+				exit(EXIT_SUCCESS);
+				break;
+				
+			case 'q':
+				quiet = 1;
+				break;
+			
+			case 'v':
+				print_banner();
+				exit(EXIT_SUCCESS);	       
+				break;
+
+			default:
+				print_banner();
+				print_usage();
+				exit(EXIT_FAILURE);
+				break;
+		}
+	}    
+	
+	print_banner();
+	if (argc - optind != 2)
+	{
+		print_usage();
+		exit(EXIT_FAILURE);
+	}    
+
+
+	if (flex_download_program(argv[optind], argv[optind + 1]) != 0)
+	{
+		if (!quiet)
+			printf("Code NOT downloaded to the FPGA\n\n");
+	}
+	else
+	{
+		if (!quiet)
+			printf("Code successfully downloaded to the FPGA\n\n");
+	}
+	
+	return EXIT_SUCCESS;
+}
+
+
+
diff --git a/src/main.h b/src/main.h
new file mode 100644
index 0000000..a5e83ed
--- /dev/null
+++ b/src/main.h
@@ -0,0 +1,30 @@
+/***************************************************************************
+                             main.h - Main program
+                             ---------------------
+			    
+    begin                : Mon Nov 24 2003
+    copyright            : (C) 2003 by Aurelien Jarno
+    emails               : aurelien@aurel32.net
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef MAIN_H
+#define MAIN_H
+
+#ifdef HAVE_CONFIG_H
+  #include <config.h>
+#endif
+
+/* Variables */
+extern int debug;
+extern int quiet;
+
+#endif
-- 
cgit v1.2.3