
/*
 *	main.c
 *
 *	(C) Copyright 2000 by Robert Krten, all rights reserved.
 *	Please see the LICENSE file for more information.
 *
 *	This module represents the main module for the RIM/BIN dumper/disassembler.
 *
 *	This program will dump a RIM/BIN-formatted image to stdout, or
 *	convert a tape from one format to another, or clean up a tape.
 *
 *	2001 01 07	R. Krten		created
 *	2003 12 16	R. Krten		added disassembler
 *	2003 12 17	R. Krten		made it RIM/BIN aware.
*/

#ifdef __USAGE

%C [options] papertapefile [papertapefile...]

where [options] are optional parameters chosen from:
    -b             generate a BIN-formatted output (adds ".bin")
    -d             suppress disassembly (useful for conversion-only mode)
    -r             generate a RIM-formatted output (adds ".rim")
    -v             verbose operation

Dumps the RIM- or BIN-formatted input file(s) specified on
the command line.  If the "-r" and/or "-b" options are
present, also creates a RIM and/or BIN version of the output
by adding ".rim" and/or ".bin" (respectively) to the input
filename (can be used to "clean up" BIN and RIM images by
deleting excess data before and after the leader/trailer).

#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>

#include <sys/types.h>
#include <sys/stat.h>

#define	CORE_SIZE		4096				// size of core memory

extern  void dasm8 (int addr, unsigned short int buf);

static	void	optproc (int, char **);
static	void	dumptape (unsigned char *t, int n);
static	void	dumprim (unsigned char *t, int n);
static	void	dumpbin (unsigned char *t, int n);
static	int		checkrim (unsigned char *t, int n);
static	int		checkbin (unsigned char *t, int n);
static	void	writebin (void);
static	void	writerim (void);
static	int		blank (short int *core, int size);

const	char	*progname = "d8tape";
const	char	*blankname= "      ";
extern	char	*version;					// version.c

int				optb;
int				optd;
int				optr;
int				optv;
unsigned char	*tape;						// paper tape image
short int		core [CORE_SIZE];			// in-core image (-1 means location never used)
char			*tapename;

/*
 *	main
 *
 *	Main simply calls the option processor, from which everything happens.
*/

int
main (int argc, char **argv)
{
	optproc (argc, argv);
	exit (EXIT_SUCCESS);
}

/*
 *	usageError
 *
 *	This is the usage message
*/

static void
usageError ()
{
	fprintf (stderr, "\nUsage:  %s [options] papertapefile [papertapefile...]\n\n", progname);
	fprintf (stderr, "where [options] are optional parameters chosen from:\n");
	fprintf (stderr, "    -b             generate a BIN-formatted output (adds \".bin\")\n");
	fprintf (stderr, "    -d             suppress disassembly (useful for conversion-only mode)\n");
	fprintf (stderr, "    -r             generate a RIM-formatted output (adds \".rim\")\n");
	fprintf (stderr, "    -v             verbose operation\n");
	fprintf (stderr, "\n");
	fprintf (stderr, "Dumps the RIM- or BIN-formatted input file(s) specified on\n");
	fprintf (stderr, "the command line.  If the \"-r\" and/or \"-b\" options are\n");
	fprintf (stderr, "present, also creates a RIM and/or BIN version of the output\n");
	fprintf (stderr, "by adding \".rim\" and/or \".bin\" (respectively) to the input\n");
	fprintf (stderr, "filename (can be used to \"clean up\" BIN and RIM images by\n");
	fprintf (stderr, "deleting excess data before and after the leader/trailer).\n");
	fprintf (stderr, "\n");
	exit (EXIT_FAILURE);
}

/*
 *	optproc
 *
 *	This is the option processor.  It detects the command line options, and
 *	then processes the individual files.
*/

static void
optproc (int argc, char **argv)
{
	int		opt;
	int		got_any;
	int		fd;
	struct	stat statbuf;

	if (!argc) {
		usageError ();
	}

	// clear out option values to defaults
	optb = optr = 0;

	// handle command line options
	got_any = 0;
	while ((opt = getopt (argc, argv, "bdrv")) != -1) {
		switch (opt) {
		case	'b':
			optb++;
			break;
		case	'd':
			optd++;
			break;
		case	'r':
			optr++;
			break;
		case	'v':
			optv++;
			if (optv > 1) {
				printf ("Verbosity is %d\n", optv);
			}
			break;
		default:
			usageError ();
			break;
		}
	}

	// handle command line arguments
	for (; optind < argc; optind++) {
		got_any++;
		tapename = argv [optind];				// snap tapename to global

		// open the tape
		fd = open (tapename, O_RDONLY);
		if (fd == -1) {
			fprintf (stderr, "%s:  couldn't open %s for O_RDONLY, errno %d\n", progname, tapename, errno);
			perror (NULL);
			exit (EXIT_FAILURE);
		}
		fstat (fd, &statbuf);					// get the size, so we can read it into memory

		tape = calloc (1, statbuf.st_size);
		if (tape == NULL) {
			fprintf (stderr, "%s:  can't allocate %d bytes during processing of %s, errno %d (%s)\n", progname, statbuf.st_size, tapename, errno, strerror (errno));
			exit (EXIT_FAILURE);
		}
		memset (core, 0xff, sizeof (core));		// set to -1 (assumption; all 0xff's in an int is -1; pretty safe)
		read (fd, tape, statbuf.st_size);
		close (fd);

		// dump the tape (this also reads the tape into "core"
		dumptape (tape, statbuf.st_size);

		free (tape);

		// convert to RIM/BIN if required (-b and/or -r)
		if (optb || optr) {
			// see if there is any data there at all..
			if (blank (core, CORE_SIZE)) {
				printf ("%s:  tape image from %s is empty, not creating a BIN version\n", progname, tapename);
				return;
			}
			if (optb) {
				writebin ();
			}
			if (optr) {
				writerim ();
			}
		}
	}

	// if no arguments given, dump usage message
	if (!got_any) {
		usageError ();
	}
}

/*
 *	dumptape
 *
 *	This function does some basic processing (detecting and skipping the
 *	header, killing off data past the trailer) and then determines via
 *	checkrim() and ckeckbin() the format of the tape.  Depending on the
 *	type of tape, dumprim() or dumpbin() is called to read the tape into
 *	the "core" array and disassemble it.
*/

static void
dumptape (unsigned char *t, int n)
{
	int		i;

	// basic preprocessing; find header
	for (i = 0; i < n; i++) {
		if (t [i] == 0x80) {		// got a header
			break;
		}
	}
	if (i == n) {
		fprintf (stderr, "%s:  couldn't find a 0x80 leader on tape %s; tape ignored\n", progname, tapename);
		return;
	}

	// skip header
	for (; i < n; i++) {
		if (t [i] != 0x80) {
			break;
		}
	}
	if (i == n) {
		fprintf (stderr, "%s:  no data content found after 0x80 leader on tape %s; tape ignored\n", progname, tapename);
		return;
	}

	// at this point, we're positioned on the first-non-leader byte of the tape
	if (n - i < 4) {
		fprintf (stderr, "%s:  tape %s is too short; tape ignored\n", progname, tapename);
		return;
	}

	// skip leader (t now points to start of tape; n indicates remaining # bytes)
	if (optv) {
		printf ("%s:  tape %s skipped %d (0x%0X, 0%0o) bytes of header, original size %d new size %d\n", progname, tapename, i, i, i, n, n - i);
	}
	t += i;
	n -= i;

	// foreshorten tape to squeeze out trailer
	for (i = 0; i < n; i++) {
		if (t [i] == 0x80) {
			break;
		}
	}
	if (i == n) {
		fprintf (stderr, "%s:  tape %s does not have any data; tape skipped\n", progname, tapename);
		return;
	}

	// reset end-of-tape to last character
	if (optv) {
		printf ("%s:  tape %s skipped %d bytes of trailer, new size is %d bytes\n", progname, tapename, n - i, i);
	}
	n = i;

	// determine type of tape and dump it
	if (checkrim (t, n)) {
		dumprim (t, n);
	} else if (checkbin (t, n)) {
		dumpbin (t, n);
	} else {
		fprintf (stderr, "%s:  tape %s is neither RIM nor BIN (first four bytes are 0%03o 0%03o 0%03o 0%03o)\n", progname, tapename, t [0], t [1], t [2], t [3]);
		return;
	}
}

/*
 *	checkrim
 *	checkbin
 *
 *	These two functions try to determine what format the tape is in.
 *	A zero return indicates the tape is not in the given format; a one
 *	indicates it is.  The heuristics used here are fairly simple.
*/

static int
checkrim (unsigned char *t, int n)
{
	int		i;

	if (n % 4) {
		if (optv) {
			printf ("%s:  tape %s size (%d bytes) is not divisible by four; not a RIM tape\n", progname, tapename, n);
		}
		return (0);
	}

	// see if it's a RIM-formatted tape; we're looking for 01xxxxxx 00xxxxxx 00xxxxxx 00xxxxxx
	for (i = 0; i < n; i += 4) {
		if ((t [i] & 0xC0) != 0x40 || (t [i + 1] & 0xC0) || (t [i + 2] & 0xC0) || (t [i + 3] & 0xC0)) {
			if (optv) {
				printf ("%s:  tape %s does not have the RIM signature at offset 0%04o; expected 01xxxxxx 00xxxxxx 00xxxxxx 00xxxxxx, got 0%04o 0%04o 0%04o 0%04o\n", progname, tapename, i, t [i], t [i + 1], t [i + 2], t [i + 3]);
			}
			return (0);
		}
	}
	return (1);
}

static int
checkbin (unsigned char *t, int n)
{
	if (n % 2) {
		if (optv){ 
			printf ("%s:  tape %s size (%d bytes) is not divisible by two; not a BIN tape\n", progname, tapename, n);
		}
		return (0);
	}

	// see if it's a BIN-formatted tape; 01xxxxxx 00xxxxxx (i.e., we at least expect an origin)
	if ((t [0] & 0xC0) != 0x40 || (t [1] & 0xC0)) {
		if (optv) {
			printf ("%s:  tape %s does not have the BIN origin signature; expected header of 01xxxxxx 00xxxxxx, got 0%04o 0%04o\n", progname, tapename, t [0], t [1]);
		}
		return (0);
	}

	return (1);
}

/*
 *	From the PDP-8/I & PDP-8/L Small Computer Handbook (099-00272-A1983 / J-09-5)
 *	Appendix D, "Perforated-Tape Loader Sequences", page 383
 *
 *	READIN MODE LOADER
 *	The readin mode (RIM) loader is a minimum length, basic perforated-tape
 *	reader program for the ASR33, it is initially stored in memory by manual use
 *	of the operator console keys and switches.  The loader is permantenly stored in
 *	18 locations of page 37.
 *	
 *	A perforated tape to be read by the RIM loader must be in RIM format:
 *	
 *	   Tape Channel
 *	 8 7 6 5 4 3 2 1        Format
 *	 ---------------        ------------------------
 *	 1 0 0 0 0 0 0 0        Leader-trailer code
 *	 0 1 -A1-  -A2-         Absolute address to
 *	 0 0 -A3-  -A4-         contain next 4 digits
 *	 0 0 -X1-  -X2-         Content of previous
 *	 0 0 -X3-  -X4-         4-digit address
 *	 0 1 -A1-  -A2-         Address
 *	 0 0 -A3-  -A4-         
 *	 0 0 -X1-  -X2-         Content
 *	 0 0 -X3-  -X4-         
 *	     (etc)              (etc)
 *	 1 0 0 0 0 0 0 0        Leader-trailer code
 *	
 *	The RIM loader can only be used in conjunction with the ASR33 reader (not
 *	the high-speed perforated-tape reader).  Because a tape in RIM format is, in
 *	effect, twice as long as it need be, it is suggested that the RIM loader be used
 *	only to read the binary loader when using the ASR33. (Note that PDP-8 diag-
 *	nostic program tapes are in RIM format.)
 *	
 *	The complete PDP-8/I RIM loader (SA=7756) is as follows:
 *	
 *	Absolute	Octal						
 *	Address		Content		Tag		Instruction I Z		Comments
 *	7756,		6032		BEG,	KCC					/CLEAR AC AND FLAG
 *	7757,		6031				KSF					/SKIP IF FLAG = 1
 *	7760,		5357				JMP .-1				/LOOKING FOR CHARACTER
 *	7761,		6036				KRB					/READ BUFFER
 *	7762,		7106				CLL RTL				
 *	7763,		7006				RTL					/CHANNEL 8 IN AC0
 *	7764,		7510				SPA					/CHECKING FOR LEADER
 *	7765,		5357				JMP BEG+1			/FOUND LEADER
 *	7766,		7006				RTL					/OK, CHANNEL 7 IN LINK
 *	7767,		6031				KSF					
 *	7770,		5367				JMP .-1				
 *	7771,		6034				KRS					/READ, DO NOT CLEAR
 *	7772,		7420				SNL					/CHECKING FOR ADDRESS
 *	7773,		3776				DCA I TEMP			/STORE CONTENT
 *	7774,		3376				DCA TEMP			/STORE ADDRESS
 *	7775,		5356				JMP BEG				/NEXT WORD
 *	7776,		0			TEMP,	0					/TEMP STORAGE
 *	7777,		5XXX				JMP X				/JMP START OF BIN LOADER
*/

/*
 *	dumprim
 *
 *	This is a finite-state-machine that runs through the tape reading the address
 *	and data records, stuffs them into core[], and disassembles the opcodes unless
 *	"-d" is specified.
*/

#define	RIM_Initial			1				// waiting for address
#define	RIM_Addr			2				// got top part of address, process bottom part
#define	RIM_Data1			3				// got address, process top part of data
#define	RIM_Data2			4				// got top part of data, process bottom part

static void
dumprim (unsigned char *t, int n)
{
	int		state;
	int		i;
	unsigned short int	addr, data;

	state = RIM_Initial;

	for (i = 0; i < n; i++) {
		if (optv > 1) {
			printf ("[%03o] ", t [i]); fflush (stdout);
		}
		switch (state) {
		case	RIM_Initial:
			if (t [i] & 0100) {				// indicates 1st part of address
				addr = (t [i] & 0077) << 6;	// store top part
				state = RIM_Addr;
			}
			break;
		case	RIM_Addr:
			addr |= (t [i] & 0077);
			state = RIM_Data1;
			break;
		case	RIM_Data1:
			data = (t [i] & 0077) << 6;		// store top part
			state = RIM_Data2;
			break;
		case	RIM_Data2:
			data |= (t [i] & 0077);
			if (!optd) {
				dasm8 (addr, data);
			}
			core [addr] = data;				// stash data into core image
			state = RIM_Initial;
			break;
		}
	}
}

/*
 *	BIN format, from the same doc as above, page 384:
 *	
 *	BINARY LOADER
 *	The binary loader (BIN) is used to read machine language tapes (in binary
 *	format) produced by the program assembly language (PAL).  A tape in binary
 *	format is about one-half the length of the comparable RIM format tape.  It can,
 *	therefore, be read about twice as fast as a RIM tape and is, for this reason,
 *	the more desirable format to use with the 10 cps ASR33 reader or the Type
 *	PR8/I High-Speed Perforated-Tape Reader.
 *	
 *	The format of a binary tape is as follows:
 *	
 *		LEADER: about 2 feet of leader-trailer codes.
 *	
 *		BODY: characters representing the absolute, machine language program
 *		in easy-to-read binary (or octal) form.  The section of tape may contain
 *		characters representing instructions (channel 8 and 7 not punched) or
 *		origin resettings (channel 8 not punched, channel 7 punched) and is
 *		concluded by 2 characters (channel 8 and 7 not punched) that represent
 *		a check sum for the entire section.
 *	
 *		TRAILER: same as leader.
*/

/*
 *	dumpbin
 *
 *	This is a finite-state-machine that runs through the tape looking for the
 *	origin and subsequent data fields, stuffs them into the core[] array, and
 *	optionally disassembles the opcodes.
*/

#define	BIN_Initial				1			// initial state; we require an origin to get out of it
#define	BIN_Origin				2			// we got the top part of the origin, now need to get the bottom part
#define	BIN_DataHW				3			// we have an address, so we are looking for another origin or the top part of the data
#define	BIN_DataLW				4			// we have the top part of the data, now fetching the low part

static void
dumpbin (unsigned char *t, int n)
{
	int		tape_checksum;					// checksum stored on tape
	int		calc_checksum;					// calculated checksum
  	int		i;
  	int		state;
  	unsigned short int addr, data;

	if (n < 4) {
		fprintf (stderr, "%s:  tape %s is too short; tape skipped\n", progname, tapename);
		return;
	}

	tape_checksum = ((t [n - 2] & 0x3f) << 6) + (t [n - 1] & 0x3f);
	if (optv) {
		printf ("%s:  tape %s expected checksum 0%04o\n", progname, tapename, tape_checksum);
	}
	n -= 2;									// tape is now shorter by the two bytes

	// now calculate checksum
	calc_checksum = 0;
	for (i = 0; i < n; i++) {
		calc_checksum += t [i];
	}
	calc_checksum &= 07777;					// mask to 12 bits
	if (optv) {
		printf ("%s:  tape %s calculated checksum 0%04o\n", progname, tapename, calc_checksum);
	}

	if (tape_checksum != calc_checksum) {
		fprintf (stderr, "%s:  tape %s calculated checksum [0%04o] != stored checksum [0%04o]; tape skipped\n", progname, tapename, calc_checksum, tape_checksum);
		return;
	}

	// now we can dump the binary data via the state machine
	state = BIN_Initial;
	for (i = 0; i < n; i++) {
		if (optv > 1) {
			printf ("[%03o] ", t [i]); fflush (stdout);
		}
		switch (state) {
		case	BIN_Initial:
			if (t [i] & 0100) {				// indicates origin setting code
				addr = (t [i] & 0077) << 6;	// store top part
				state = BIN_Origin;
			}
			break;
		case	BIN_Origin:
			addr += (t [i] & 0077);			// store bottom part
			state = BIN_DataHW;
			if (!optd) {
				printf ("/ ORIGIN %04o\n", addr);
			}
			break;
		case	BIN_DataHW:
			if (t [i] & 0100) {				// another origin; skip loading data and load address instead
				addr = (t [i] & 0077) << 6;
				state = BIN_Origin;
			} else {
				data = (t [i] & 0077) << 6;	// store top part of data
				state = BIN_DataLW;
			}
			break;
		case	BIN_DataLW:
			data += (t [i] & 0077);
			if (!optd) {
				dasm8 (addr, data);
			}
			core [addr] = data;
			addr++;							// the magic of BIN-format is the autoincrement of the address
			state = BIN_DataHW;
		}
	}
}

/*
 *	writebin
 *	writerim
 *
 *	These two functions write the BIN and RIM format tapes to a file.
 *	The filename is constructed by appending ".bin" or ".rim" to the
 *	input filename.
 *
 *	The header and trailer written are short, LEADER_LENGTH bytes.
 *
 *	The writebin() uses a finit-state-machine to generate the origin.
*/

#define	LEADER_LENGTH	16					// 16 chars of leader/trailer should be plenty

#define	WBIN_Initial	1					// looking for first/next in-use core[] element
#define	WBIN_Writing	2					// origin written, dumping consecutive words

static	void
writebin (void)
{
	char	fname [PATH_MAX];
	char	leader [LEADER_LENGTH];
	FILE	*fp;
	int		i;
	int		cksum;
	int		state;

	// create filename and open it
	sprintf (fname, "%s.bin", tapename);
	if ((fp = fopen (fname, "w")) == NULL) {
		fprintf (stderr, "%s:  unable to open BIN output file %s for w, errno %d (%s); creation of output file skipped\n", progname, fname, errno, strerror (errno));
		return;
	}

	// write leader
	memset (leader, 0x80, sizeof (leader));
	fwrite (leader, 1, sizeof (leader), fp);

	// now scan through "core" and write the data out...
	cksum = 0;
	state = WBIN_Initial;
	for (i = 0; i < CORE_SIZE; i++) {
		switch (state) {
		case	WBIN_Initial:				// transit out of WBIN_Initial on a "used" core position
			if (core [i] != -1) {
				state = WBIN_Writing;
				fprintf (fp, "%c%c", 0x40 | ((i & 07700) >> 6), i & 00077);		// write origin directive
				fprintf (fp, "%c%c", (core [i] & 07700) >> 6, core [i] & 00077);	// write data
				cksum += (0x40 | ((i & 07700) >> 6)) + (i & 00077) + ((core [i] & 07700) >> 6) + (core [i] & 00077);
			}
			break;
		case	WBIN_Writing:
			if (core [i] == -1) {
				state = WBIN_Initial;		// waiting again for a used core position
			} else {
				fprintf (fp, "%c%c", (core [i] & 07700) >> 6, core [i] & 00077);
				cksum += ((core [i] & 07700) >> 6) + (core [i] & 00077);
			}
			break;
		}
	}

	// now write the checksum
	fprintf (fp, "%c%c", (cksum & 07700) >> 6, cksum & 00077);

	// write trailer
	fwrite (leader, 1, sizeof (leader), fp);

	fclose (fp);
}

static	void
writerim (void)
{
	char	fname [PATH_MAX];
	char	leader [LEADER_LENGTH];
	FILE	*fp;
	int		i;

	// create the filename and open it
	sprintf (fname, "%s.rim", tapename);
	if ((fp = fopen (fname, "w")) == NULL) {
		fprintf (stderr, "%s:  unable to open RIM output file %s for w, errno %d (%s); creation of output file skipped\n", progname, fname, errno, strerror (errno));
		return;
	}

	// write leader
	memset (leader, 0x80, sizeof (leader));
	fwrite (leader, 1, sizeof (leader), fp);

	for (i = 0; i < CORE_SIZE; i++) {
		if (core [i] != -1) {
			fprintf (fp, "%c%c%c%c", 0x40 + ((i & 07700) >> 6), i & 00077, (core [i] & 07700) >> 6, core [i] & 00077);
		}
	}

	// write trailer
	fwrite (leader, 1, sizeof (leader), fp);

	fclose (fp);
}

/*
 *	blank
 *
 *	A utility routine to see if core[] is blank (returns 1).
 *	Used to avoid writing an empty tape.
*/

static int
blank (short int *c, int size)
{
	int		i;

	for (i = 0; i < size; i++) {
		if (c [i] != -1) {
			return (0);
		}
	}
	return (1);
}

