	.TITLE	DECODE                   BY J.A. SMITH
/                                        ENGINEERING COMPUTER CENTRE
/                                        UNIVERSITY OF WATERLOO
/                                        WATERLOO, ONTARIO.
/ THIS PROGRAM READS A BINARY FILE OFF DAT SLOT -11 AND DECODES
/ IT INTO ASCII AND WRITES IT OUT ONTO DAT SLOT -12.
/
AUTO10=10
AUTO11=11
AUTO12=12
AUTO15=15
/ 
SPACE=40;CAR=15
CHAR;COUNT;CNTR;TEMP;CODES;NGROUP;EXITSW
IND1;IND2;IND3;IND
/
/ PROGRAM COMEON.
MESAGE	.ASCII '     DECODE V1A'<215>
STRING	.ASCII '     >'<175>
/
/
/
/ THIS BUFFER IS USED FOR OUTPUTTING THE ASCII CODE FOR THE
/ THE BINARY WORDS.
/
HEADER	003000
	000000
ASCII	.BLOCK 4
/
/THIS SUBROUTINE INSERTS A CARRIAGE RETURN AT THE END OF THE
/ OUTPUT LINE AND WRITES THE BUFFER OUT.
OUTPUT	XX
	LAC	(CAR
	JMS	G.PACK
	.WRITE	-12,2,HEADER,0
	.WAIT	-12
	JMP*	OUTPUT
/
/ FILE NAME EXTENSIONS
EXTEN1	.SIXBT	'BIN'
EXTEN2	.SIXBT	'LDR'
/
/ THIS BUFFER IS USED TO CONTAIN THE FILE NAME FOR OPENING THE
/ BINARY FILE AND OPENING THE OUTPUT FILE.
/
BINAME	.BLOCK	2
	.SIXBT	'BIN'
/
/ THIS BUFFER IS USED FOR THE BINARY INPUT RECORDS (AND THE
/ COMMAND STRING).
BINBUF	.BLOCK	50
/
/ THE MAIN SECTION OF CODE STARTS HERE. IT PRINTS OUT 'DECODE V1A'
/ AND '>' AND WAITS FOR THE COMMAND STRING.
	.IODEV	-2,-3,-11,-12
START	.INIT	-3,1,START
	.WAIT	-3
	.WRITE	-3,2,MESAGE,4
	.WRITE -3,2,STRING,0
/
/ INITIALIZE NAME BUFFER AND READ COMMAND STRING INTO BUFFER
	DZM	BINAME
	DZM	BINAME+1
/
	.WAIT	-2
	.READ	-2,2,BINBUF,34
	.WAIT	-2
	LAC	(BINBUF+2-1
	JMS	G.STPK
	LAC	(BINAME
	JMS	G.STSF
ROTE	JMS	G.UNPK
	SAD	(15
	JMP	ROUTC
	SAD	(175
	JMP	ROUTA
	JMS	G.STUF
	JMP	ROTE
/
/ THE NAME HAS BEEN ENTERED IN THE BUFFER. SET THE
/ EXIT SWITCH ACCORDING TO WHETHER A CARRIAGE RETURN OR
/ ALTMODE WAS FOUND.
ROUTC	CLA!SKP		/ A CARRIAGE RETURN WAS FOUND
ROUTA	LAW	-1	/ AN ALTMODE WAS FOUND
	DAC	EXITSW
/
/ ENTER EXTENSION 'BIN' AND OPEN BINARY INPUT FILE
	LAC	EXTEN1
	DAC	BINAME+2
	.INIT	-11,0,START
	.SEEK	-11,BINAME
/
/ ENTER EXTENSION 'LDR' AND OPEN OUTPUT FILE FOR ASCII
	LAC	EXTEN2
	DAC	BINAME+2
	.INIT	-12,1,START
	.ENTER	-12,BINAME
	JMP	BX1
/
/ EACH RECORD OF THE BINARY FILE CONTAINS UP TO 6 GOUPS OF 4 WORDS EACH.
/ EACH GROUP CONSISTS OF 1. 3 CODES DESCRIBING NEXT 3 WORDS
/			     2. THE WORD DESCRIBED BY 1ST CODE
/			     3. THE WORD DESCRIBED BY 2ND CODE
/			     4. THE WORD DESCRIBED BY THE 3RD CODE
/
BX	ISZ	NGROUP
	JMP	B0
/ ENTER HERE WHEN A NEW RECORD IS NEEDED
BX1	LAC	(BINBUF+1
	DAC*	(AUTO15
	.READ	-11,0,BINBUF,26
	.WAIT	-11
	LAC	BINBUF	/ GET 1ST HEADER WORD
	CLL
	LRS	10	/ GIVES TOTAL NUMBER OF WORDS
	TAD	(-2	/ REMOVE HEADER WORDS FROM COUNT
	CLL
	IDIV
		4
	LACQ		/ GIVES NUMBER OF GROUPS
	CMA
	TAD	(1
	DAC	NGROUP
	LAC	BINBUF
	AND	(7
	SAD	(5	/ CHECK FOR END OF FILE
	JMP	EOFEOM	/ YES.
	SAD	(6
	JMP	EOFEOM	/ YES.
	LAC	NGROUP
	SNA		/ DOES THE RECORD CONTAIN ANY DATA
	JMP	BX1	/ NO. WAS JUST TWO HEADER WORDS
	JMP	B0	/ YES. GO PROCESS IT.
EOFEOM	.CLOSE	-11
	.CLOSE	-12
/ CHECK IF RETURN TO MONITOR OR NOT
	ISZ	EXITSW
	JMP	START
	.EXIT
/
/ NOW DECIPHER THE CODES AND LEAVE INDICATORS IN IND1,IND2, AND
/ IND3 RESPECTIVELY, DISTINGUISHING BETWEEN RADX50 AND OCTAL WORDS.
B0	LAC	(ASCII
	JMS	G.STPC
	LAC	(6-2
	DAC	NUMS	/ CONVERT ONLY TWO DIGITS
/ PICK UP CODE WORD, DECIPHER 1ST CODE, CONVERT TO ASCII AND ENTER IN
/ THE OUTPUT BUFFER
B1	LAC*	AUTO15
	DAC	CODES
	LRS	14	/ GET 1ST CHAR
	JMS	TEST	/ TEST IF RADX50 OR OCTAL
	DAC	IND1
	LAC	CHAR
	JMS	G.CVRT	/ CONVERT AND INSERT IN BUFFER
	LAC	(SPACE
	JMS	G.PACK
/ GET 2ND CODE
B2	LAC	CODES
	LRS	6
	JMS	TEST	/ TEST IF RADX50 OR OCTAL
	DAC	IND2
	LAC	CHAR
	JMS	G.CVRT	/ INSERT AND ENTER IN BUFFER
	LAC	(SPACE
	JMS	G.PACK
/ GET 3RD CODE
B3	LAC	CODES
	JMS	TEST	/ TEST IF RADX50 OR OCTAL
	DAC	IND3
	LAC	CHAR
	JMS	G.CVRT	/ CONVERT AND INSERT IN BUFFER
	JMS	OUTPUT	/ WRITE OUT THE LINE OF CODES
/
/
/ NOW PICK UP EACH OF THE THREE WORDS IN TURN. IF THE VALUE
/ OF IND* = 1 THEN THE WORD SHOULD BE INTERPRETED AS RADX50 AND
/ IF IND* = 0 THEN THE WORD IS IN OCTAL.
B4	LAC	(IND1
	DAC	IND	/ SET TO PICK UP INDICATORS
	DZM	NUMS	/ CONVERT 6 DIGITS
	LAW	-3
	DAC	COUNT
AG1	LAW	-2
	DAC	CNTR
	LAC	(ASCII
	JMS	G.STPC	/ RESET TO FIRST OF BUFFER
	LAC*	AUTO15
	ISZ*	IND
	JMP	ITSOCT
/ WORD IS RADX50
ITSRAD	JMS	UNRADX
	SKP
/ WORD IS OCTAL
ITSOCT	JMS	G.OCTL	/ CONVERT TO OCTAL AND INSERT IN BUFFER
/ 
/ PRINT OUT THE WORD AND TRY FOR MORE
	LAC	(CAR
	JMS	G.PACK	/ INSERT CARRIAGE RETURN
	JMS	OUTPUT
	ISZ	IND
	ISZ	COUNT
	JMP	AG1
	JMP	BX
/
/
/ PICK APART THE  RADX50 CHARACTERS TO BE CONVERTED INTO
/ ASCII BY THE ROUTINE SORT.
UNRADX	XX
	AND	(377777
	DAC	TEMP
/
	LAC	(50*50
	SKP
AG2	LAC	(50
	DAC	DIV
	CLL
	LAC	TEMP
	IDIV
DIV	XX
	DAC	TEMP
	LACQ
	JMS	SORT
	ISZ	CNTR
	JMP	AG2
	LAC	TEMP
	JMS	SORT
	JMP*	UNRADX
/
/
/ CONVERT TO ASCII
SORT	XX
	TAD	(-35
	SPA
	JMP	LETTER
DIGIT	TAD	(60	/ YES
	SKP
LETTER	TAD	(101+34
	JMS	G.PACK
	JMP*	SORT
/
/
/
/ TEST CODE, IF CODE= 7 OR 10, THEN THE CORRESPONDING WORD IS IN
/ RADX50 AND THE AC IS SET TO 0. IF THE CODE IS OTHERWISE
/ THEN AC IS SET TO -1.
TEST	XX
	AND	(77
	DAC	CHAR
	SAD	(7
	JMP	SET
	SAD	(10
	JMP	SET
	CLA
	JMP*	TEST
SET	LAW	-1
	JMP*	TEST
/
/
/ THIS SUBROUTINE PERFORMS A SETUP FOR THE G.UNPK SUBROUTINE
/
G.STPK	XX
	DAC*	(AUTO10
	LAC	(UNFRT
	DAC	HINGE
	JMP*	G.STPK
/
/ THIS SUBROUTINE UNPACKS 5/7 ASCII FROM ANY BUFFER AND LEAVES
/ EACH CHARACTER RIGHT JUSTIFIED IN THE AC. ***THE MQ MUST NOT BE
/ DISTURBED****  IT USES AUTO-INDEX 10.
/	LAC	(BUFFER
/	JMS	G.STPK
/	JMS	G.UNPK
/
G.UNPK	XX
	JMP*	HINGE
HINGE	.DSA	UNFRT
	AND	(177	/ TRIM TO 7 BITS
	JMP*	G.UNPK
/
UNFRT	LAC*	AUTO10
	DAC	OBJECT
	LMQ
	LLS	7
	JMS	HINGE
/
	LLS	7
	JMS	HINGE
/
	LAC*	AUTO10
	LMQ
	LAC	OBJECT
	LLS	3
	JMS	HINGE
/
	LLS	7
	JMS	HINGE
/
	LLS	7
	JMS	HINGE
	JMP	UNFRT
/
OBJECT	XX
/
/
/ THIS SUBROUTINE PERFORMS A SET UP FOR THE G.STUF SUBROUTINE
/
G.STSF	XX
	DAC	POINT
	LAC	(FIRST
	DAC	PIVOT
	JMP*	G.STSF
POINT	XX
/ THIS SUBROUTINE IS USED TO PACK SIXBIT ASCII CHARACTERS THREE PER
/ WORD. ENTER WITH 6,7 OR 8 BIT ACII RIGHT JUSTIFIED IN AC.
/	LAC	(STORAGE ADDRESS
/	JMS	G.STSF
/	LAC	CHARACTER
/	JMS	G.STUF
/
G.STUF	XX
	AND	(77	/ TRIM TO 6 BITS
	JMP*	PIVOT
PIVOT	.DSA	FIRST
	TAD*	POINT
	DAC*	POINT
	JMP*	G.STUF
/
FIRST	RCR
	RTR;	RTR;	RTR	/ ROTATE 12 BITS LEFT
	DZM*	POINT
	JMS	PIVOT
/
SECOND	CLL
	RTL;	RTL;	RTL	/ ROTATE 6 BITS LEFT
	JMS	PIVOT
/
THIRD	JMS	PIVOT
	ISZ	POINT
	JMP	FIRST
/
/
/
/
/ THIS SUBROUTINE PERFORMS A SET UP FOR THE G.PACK SUBROUTINE
/
G.STPC	XX
	DAC	POINT
	DZM	PAIRCT
	DZM	CHARCT
	LAC	(LEFT
	DAC	SWING
	JMP*	G.STPC
/
/ THIS SUBROUTINE PACKS CHARACTERS INTO THE 5/7 ASCII FORMAT
/ TAKING THE CHAR FROM THE AC AND PLACING THEM IN A SPECIFIED BUFFER.
/ **** IT CLEARS THE MQ **** BUT DOES NOT USE THE AUTO-INDEX REGISTERS!
/	LAC	(STORAGE ADDRESS
/	JMS	G.STPC
/	LAC	CHARACTER
/	JMS	G.PACK
/
G.PACK	XX
	ISZ	CHARCT	/ COUNT THE CHARACTERS
	CLQ!020000	/ MICRO-PROGRAMMED CLQ AND CLEAR LINK
	JMP*	SWING
SWING	.DSA	LEFT
	TAD*	POINT
	DAC*	POINT
	JMP*	G.PACK
/
LEFT	ISZ	PAIRCT
	RTR;	RTR;	RTR;	RTR	/ SHIFT 11 LEFT
	DZM*	POINT
	JMS	SWING
/
	RTL;	RTL	/ SHIFT 4 LEFT
	JMS	SWING
/
	LRS	3
	TAD*	POINT
	DAC*	POINT
	ISZ	POINT
	DZM*	POINT
	LACQ		/ GET REMAINING 4 BITS
	JMS	SWING
/
	RTL;	RTL;	RTL;	RTL	/ SHIFT 8 LEFT
	JMS	SWING
/
	RAL		/ SHIFT 1 LEFT
	JMS	SWING
	ISZ	POINT
	JMP	LEFT
PAIRCT	XX
CHARCT	XX
/
/
/ THIS SUBROUTINE CONVERTS AN OCTAL NUMBER COUNT FRON THE AC, INTO
/ A DECIMAL 5/7 ASCII AND STORES THE RESULT IN THE BUFFER SET UP
/ FOR THE G.PACK SUBROUTINE.
/ IF THE LINK IS SET, THE ROUTINE WILL TOSS AWAY LEADING ZEROS!
/	LAC	(BUFFER ADDRESS
/	JMS	G.STPC
/	LAC	NUMBER
/	JMS	G.CVRT
/
G.CVRT	XX
	DAC	REMAIN
	LAC	(SKP
	SZL
	LAC	(SNA
	DAC	SWITCH
	LAC	DIVEND
	TAD	NUMS	/ ADJUSTMENT FOR NUMBER OF CHARACTERS
	DAC*	(AUTO10
	CLL
NXTDIV	LAC*	AUTO10
	SAD	DIVEND
	JMP*	G.CVRT
/
	DAC	DIVIDE
	LAC	REMAIN
	IDIV
DIVIDE	XX
	DAC	REMAIN
	LACQ
SWITCH	NOP		/ CHANGED ON STATUS OF LINK, WHEN ENTERING
	JMP	NXTDIV
	TAD	(60
	JMS	G.PACK
	LAC	(SKP
	DAC	SWITCH	/ RESET SWITCH JUST IN CASE
	JMP	NXTDIV
/
DIVISR	303240	/ DECIMAL 100,000
	23420	/ DECIMAL 10,000
	1750	/ DECIMAL 1000
	144	/ DECIMAL 100
	12	/ DECIMAL 10
	1	/ DECIMAL 1
DIVEND	DIVISR-1
/
REMAIN	XX
NUMS	XX
/
/
/ THIS SUBROUTINE CONVERTS AN OCTAL NUMBER COUNT FROM THE AC, INTO
/ AN OCTAL 5/7 ASCII AND STORES THE RESULT IN THE BUFFER SET UP
/ FOR THE G.PACK SUBROUTINE.
G.OCTL	XX
	DAC	REMAIN
	LAW	-6
	DAC	CNT
AGAIN	LAC	REMAIN
	RCL;	RTL	/ ROTATE THREE LEFT
	DAC	REMAIN	/ SAVE FOR NEXT ROUND
	RAL		/ ROTATE LAST BIT FROM LINK TO AC17
	AND	(000007	/ TRIM TO THREE BITS
	TAD	(60
	JMS	G.PACK
	ISZ	CNT
	JMP	AGAIN
	JMP*	G.OCTL
/
CNT	XX
/
	.END	START
