; DMTDisplay.asm
; Copyright Xerox Corporation 1979
; Last modified on March 3, 1979  1:02 AM by Boggs

;OUTGOING
.ENT	RESTORE, UNRESTORE
.ENT	PMSG, PMSG1, POCT, PDEC
.BEXTZ	SOM

;INCOMING
.BEXT	STARTETHEROUT, INITETHEROUT, PACKCHAR
.BEXTZ	pACT, pWW, pDISPLAY, VINT
.BEXT	sysFont

.DALC	SKL = ADCZ# 0,0,SNC
.DALC	SKLE = SUBZ# 0,0,SNC
.DALC	SKE = SUB# 0,0,SZR
.DALC	SKNE = SUB# 0,0,SNR
.DALC	SKGE =ADCZ# 0,0,SZC
.DALC	SKG = SUBZ# 0,0,SZC

.DUSR	BREAK = 77400


PFONT = 0		;DISPLACEMENT IN STREAM TABLE OF POINTER TO FONT
ERTN = 7		;ERROR ROUTINE POINTER
SSA = 15.		;START OF STORAGE AREA
ESA = 16.		;END OF STORAGE AREA
FDCB = 17.		;FIRST DISPLAY CONTROL BLOCK
LDCB = 18.		;LAST DISPLAY CONTROL BLOCK
BLKSZ = 19.		;BLOCK SIZE FOR ONE TEXT LINE
NWRDS = 20.		;WORDS PER SCAN LINE
DBA = 21.		;DESTINATION BIT ADDRESS
WAD = 24.		;DESTINATION WORD ADDRESS
BSOFAR = 23.		;BITS USED SO FAR IN THIS LINE

;THE REST OF THESE LOCATIONS ARE TEMPORARIES

LINK1 = 25.
LINK0 = 26.
LINK2 = 27.
CHAR = 28.
STKP = 29.
TEMP1 = 30.
TEMP2 = 31.


	.SREL

RESTORE:	.RESTORE
UNRESTORE: 	.UNRESTORE

CLRBLK:		.CLRBLK
SCROLL:		.SCROLL
PRINTCHAR:	.PRINTCHAR

	.ZREL

PMSG:		.PMSG
PMSG1:		.PMSG1
POCT:		.POCT
PDEC:		.PDEC

pSTREAM:	STREAM
SDL:		0

	.NREL

STREAM:	.BLK 20.
	36.		;NUMBER OF WORDS PER DISPLAYED SCAN LINE
	.BLK 11.


;-----------------------------------------------------------------------------
.RESTORE:			;INITIALIZE THE STREAM
;-----------------------------------------------------------------------------
	LDA 2 pSTREAM
	STA 3 LINK0,2

	LDA 0 @pDISPLAY		;SAVE OLD DISPLAY POINTER
	STA 0 SDL
	MKZERO 0 0		;TURN OFF DISPLAY
	STA 0 @pDISPLAY
	STA 0 VINT		;WAIT FOR IT TO STOP
	LDA 0 VINT
	SNZ 0 0
	 JMP .-2

	JSRII ppINITETHEROUT	;INITIALIZE ETHERNET OUTPUT PACKET

	LDA 0 @pACT		;IGNORE PARITY INTERRUPTS
	MOVR 0 0
	MOVZL 0 0
	STA 0 @pACT

	LDA 2 pSTREAM
	LDA 0 SOM
	SKEVEN 0 0		;FORCE IT TO BE EVEN
	 INC 0 0
	STA 0 FDCB,2		;IT IS THE FIRST DCB
	STA 0 SSA,2		;ALSO START OF SCREEN AREA
	LDA 1 DSIZE
	ADD 0 1
	STA 1 ESA,2		;END OF SCREEN AREA

	LDA 3 @pSysFont		;FONT
	STA 3 PFONT,2		;INSTALL IT IN THE STREAM
	LDA 1 NWRDS,2		;WORDS/SCAN LINE
	LDA 2 -2,3		;HEIGHT OF THE FONT IN SCAN LINES
	SKEVEN 2 2		;FORCE IT TO BE EVEN
	 INC 2 2
	MKZERO 0 0
	MUL			;HEIGHT * NWRDS = BITMAP FOR ONE TEXT LINE
	LDA 2 pSTREAM
	STA 1 BLKSZ,2		;PUT RESULT IN STREAM STRUCTURE

	LDA 3 C4		;4 WORDS ARE REQUIRED FOR THE DCB
	ADD 1 3
	STA 3 TEMP1,2		;STORAGE FOR BITMAP + DCB
	LDA 0 FDCB,2		;POINTER TO FIRST DCB
	ADD 0 3			;POTENTIAL END OF FIRST TEXT BLOCK.
	LDA 1 ESA,2		;END OF STORAGE
	SKG 1 3			;SKIPS IF AT LEAST ONE LINE OF TEXT WILL FIT
	 BREAK			;PUNT IF NOT.  (NEVER HAPPEN)
	JMP GENBLK

ppINITETHEROUT:	INITETHEROUT
pSysFont:	sysFont
DSIZE:		20000.		;BIG DISPLAY AREA

GENBLK:	MOV 0 3			;BLOCK POINTER (DCB IN FIRST 4 WORDS)
	LDA 1 NWRDS,2		;WORDS/SCAN LINE
	MOVL 1 1
	MOVL 1 1
	MOVOR 1 1		;WHITE ON BLACK BACKGROUND
	MOVZR 1 1		;HIGH RESOLUTION
	STA 1 1 3		;DCB+1
	LDA 3 PFONT,2
	LDA 1 -2 3		;FONT HEIGHT
	MOVZR 1 1 SZC		;DIVIDE BY TWO AND ROUND UP EVEN
	 INC 1 1
	MOV 0 3			;BLOCK POINTER
	STA 1 3 3		;SLC (SCAN LINES PER FIELD)
	LDA 1 C4
	ADD 0 1
	STA 1 2 3		;BITMAP POINTER
	STA 0 TEMP2,2		;SAVE BLOCK POINTER
	JSRII ppCLRBLK		;CLEAR THE BLOCK
	LDA 0 TEMP2,2		;RESTORE BLOCK POINTER
	MOV 0 3
	LDA 1 TEMP1,2		;STORAGE REQUIRED FOR BITMAP + DCB
	ADD 1 0
	STA 0 0 3		;STORE IT INTO THE DCB LINK FIELD
	ADD 0 1			;ADDRESS OF THE NEXT BLOCK'S END
	LDA 0 ESA,2		;CHECK FOR FIT
	SKGE 0 1
	 JMP NOFIT
	LDA 0 0 3		;POINTER TO NEXT BLOCK
	JMP GENBLK		;DO ANOTHER BLOCK

NOFIT:	MKZERO 0 0
	STA 0 0 3		;ZERO THE LINK FIELD OF THE LAST DCB
	STA 3 LDCB,2		;POINTER TO THE LAST BLOCK
	JSRII ppSCROLL		;SCROLL JUST TO SET UP THE LINE STATE

	JSR MTSET		;SET UP THE MASK TABLE IN PAGE 1
MTSET:	LDA 0 MTOF
	ADD 3 0
	LDA 1 MTEND
	LDA 3 MKCNT
	BLT

	LDA 0 FDCB,2		;INSTALL POINTER TO OUR DISPLAY
	STA 0 @pDISPLAY
	JMP @LINK0,2

ppSCROLL:	SCROLL
ppCLRBLK:	CLRBLK

C4:		4
MTOF:		MASKS-MTSET-1
MTEND:		477		;MASK TABLE IS FROM 460-477
MKCNT:		-16.
MASKS:	1
	3
	7
	17
	37
	77
	177
	377
	777
	1777
	3777
	7777
	17777
	37777
	77777
	177777

;-----------------------------------------------------------------------------
.UNRESTORE:
;-----------------------------------------------------------------------------
	STA 3 USRET
	LDA 0 SDL		;PUT BACK THE ORIGINAL DCB
	STA 0 @pDISPLAY
	MKZERO 0 0
	STA 0 VINT
	LDA 1 VINT
	SNZ 1 1			;WAIT FOR THE CONTROLLER TO NOTICE
	 JMP .-2

	LDA 2 pSTREAM		;ZERO THE SCREEN AREA
	LDA 1 ESA,2
	MOV 1 3
	LDA 2 SSA,2
	SUB 2 3			;LENGTH-1
	COM 3 3			;-LENGTH
	BLKS

	LDA 0 @pWW		;FLUSH ANY PARITY INTERRUPTS
	MOVR 0 0
	MOVZL 0 0
	STA 0 @pWW
	LDA 0 @pACT		;ENABLE PARITY INTERRUPTS
	MOVR 0 0
	MOVOL 0 0
	STA 0 @pACT

	JMP @USRET

USRET:	0


;-----------------------------------------------------------------------------
.PMSG1:				; AC3 -> BCPL STRING TO PRINT
;-----------------------------------------------------------------------------
	LDA 0 0 3
	LDA 1 MSG00
	ANDS 0 1
	MOVZR 1 1
	MOV 3 0
	ADD 1 3 SKP		; FALL THROUGH INTO PMSG

;-----------------------------------------------------------------------------
.PMSG:	LDA 0 0 3		; @AC3 -> BCPL STRING TO PRINT
;-----------------------------------------------------------------------------
	STA 0 pSTRING
	INC 3 3
	STA 3 MRET
	LDA 0 @pSTRING
	LDA 1 MSG00
	ANDS 1 0		; BYTE COUNT
	INC 0 0 
	STA 0 BYTECOUNT
	JMP LOOPEND

MSGL:	LDA 0 @pSTRING		; DO THE RIGHT BYTE
	LDA 1 MSG01
	AND 1 0
	JSRII ppPRINTCHAR
	ISZ pSTRING
	 NOP
	DSZ BYTECOUNT
	 JMP .+2
	JMP@ MRET
	LDA 0 @pSTRING
	LDA 1 MSG00
	ANDS 1 0
	JSRII ppPRINTCHAR
LOOPEND:DSZ BYTECOUNT
	 JMP MSGL
	JMP@ MRET

BYTECOUNT:	0
pSTRING:	0
MRET:		0
MSG00:		77400
MSG01:		177
ppPRINTCHAR:	PRINTCHAR

;-----------------------------------------------------------------------------
.POCT:			;PRINT A NUMBER IN OCTAL (POCT) OR DECIMAL (PDEC)
;-----------------------------------------------------------------------------
	STA 3 RLINK		;POCT ENTRY POINT
	JSR PNO
	100000
	10000
	1000
	100
	10
	1
	0

.PDEC:	STA 3 RLINK		;PDEC ENTRY POINT
	JSR PNO
	10000.
	1000.
	100.
	10.
	1
	0

PNO:	MOV 1 2
	LDA 0 PNO05
	STA 0 DPF		;DIGIT PRINTED FLAG
PNO01:	LDA 0 PNO00		;ASCII '0'
	LDA 1 0 3		;CURRENT POWER OF THE RADIX
	SNZ 1 1
	 JMP @RLINK		;0 MEANS DONE
PNO03:	SKG 1 2
	 JMP PNO04
	LDA 1 PNO00		;CHARACTER ZERO?
	SKNE 0 1
	 JMP PNO07		;YES
	MKMINUSONE 1 1
	STA 1 DPF		;NO, PRINT THE CHARACTER AND SET DPF TO -1
	JMP PNO06
PNO07:	ISZ DPF			;CHARACTER IS 0. DPF=-1?
	 JMP PNO09		;NO.  PRINT SPACE
	DSZ DPF
PNO06:	STA 3 S3
	STA 2 S2
	JSRII ppPRINTCHAR
	LDA 2 S2
	LDA 3 S3
	INC 3 3			;ADVANCE RADIX POINTER
	JMP PNO01

PNO04:	SUB 1 2
	INC 0 0
	JMP PNO03


PNO09:	LDA 0 PNO10
	JMP PNO06

DPF:		0
PNO05:		-5
PNO10:		40		;ASCII SPACE
PNO00:		60
S2:		0
S3:		0
RLINK:		0

;-----------------------------------------------------------------------------
.PRINTCHAR:
;-----------------------------------------------------------------------------
	LDA 2 pSTREAM
	STA 3 LINK0,2
	LDA 1 C177		;MASK CHARACTER TO 8 BITS
	AND 0 1
	LDA 0 C40		;CHECK FOR CONTROL CHARACTERS
	SKL 1 0
	 JMP PUT0		;NOT ONE
	LDA 0 CCR
	SKE 0 1
	 JMP DRET
	JSRII ppSTARTETHEROUT	;SEND THE ETHERNET PACKET
	LDA 2 pSTREAM
	JSR .SCROLL		;CARRIAGE RETURN SCROLLS
DRET:	LDA 3 LINK0,2
	LDA 2 STKP,2
	JMP 0,3			;RETURN

ppSTARTETHEROUT:	STARTETHEROUT
ppPACKCHAR:		PACKCHAR

PUT0:	STA 1 CHAR,2
	JSRII ppPACKCHAR	;PUT CHARACTER IN ETHER OUTPUT BUFFER
	LDA 2 pSTREAM


;THE CHARACTER IS GUARANTEED TO FIT ON THE CURRENT LINE IF THE MAX.
;WIDTH OF ANY CHARACTER IN THE FONT + BITS USED SO FAR < NWRDS*16.

	LDA 3 PFONT, 2		;POINTER TO FONT
	LDA 1 -1 3		;MAXIMUM WIDTH OF ANY CHARACTER IN THE FONT
	LDA 0 C377		;MASK OFF PROPORTIONAL BIT AND BASELINE
	AND 0 1			;LEAVING MAX WIDTH IN AC0
	LDA 0 BSOFAR , 2
	ADD 1 0
	LDA 1 NWRDS,2
	ADDZL 1 1		;MULTIPLY BY 16
	ADDZL 1 1
	SKG 1 0
	 JSR .SCROLL		;THE LINE WOULD (MIGHT) OVERFLOW
	LDA 0 CHAR,2

PUT1:	LDA 3 PFONT, 2		;POINTER TO FONT
	ADD 0 3			;POINTS TO CHARACTER SLOT
	LDA 0 WAD,2		;WORD ADDRESS IN THIS LINE
	CONVERT	NWRDS		;BINGO!
	JMP PUT2		;CHARACTER HAS AN EXTENSION

	LDA 0 BSOFAR,2		;NO EXTENSION, AC3=WIDTH, AC1=DBA AND 17B
	ADD 3 0
	STA 0 BSOFAR,2

	SUBZ 3 1 SZC
	 JMP PUT3		;DIDN'T OVERFLOW A WORD BOUNDARY
	ISZ WAD,2		;INCREMENT WORD ADDRESS
	LDA 3 C20
	ADD 3 1			;UPDATE DBA, WHICH IS NOW NEGATIVE

PUT3:	STA 1 DBA,2
	JMP DRET

PUT2:	ISZ WAD,2		;HANDLE EXTENSION.
	LDA 0 BSOFAR,2		;AC3 HAS PSEUDO-CHARACTER CODE
	LDA 1 C20
	ADD 1 0
	STA 0 BSOFAR,2
	MOV 3 0
	JMP PUT1


C20:	20
TEMP:	0
C40:	40
CCR:	15
C377:	377

;-----------------------------------------------------------------------------
.SCROLL:			;SCROLL UP
;-----------------------------------------------------------------------------
	STA 3 LINK1,2		;AC2 CONTAINS THE STREAM POINTER ON CALL
	LDA 0 FDCB,2
	JSR .CLRBLK		;CLEAR THE BLOCK OF STORAGE POINTED TO BY
				;THE DCB POINTED TO BY AC0.  AC2 IS PRESERVED

	LDA 3 FDCB,2
	LDA 0 2 3		;POINTER TO THE FIRST DCB'S STORAGE
	STA 0 TEMP2,2		;SAVE IT

;AT THIS TIME, AC3 CONTAINS A POINTER TO A DCB.  IS IT THE LAST ONE?
SCRLOOP:LDA 0 LDCB,2
	SKNE 3,0
	 JMP SCRDONE		;YES.
	MOV 3 1			;IT ISN'T. GET THE STORAGE FROM THE DCB LINKED
				;TO THIS ONE, AND STORE IT INTO THIS DCB.
	LDA 3 0 3		;LINK FIELD OF THIS DCB
	LDA 0 2 3		;STORAGE POINTER FOR THE LINKED DCB
	MOV 1 3			;THE CURRENT DCB POINTER
	STA 0 2 3
	LDA 3 0 3		;POINTER TO THE LINKED DCB
	JMP SCRLOOP

SCRDONE:LDA 0 TEMP2,2		;POINTER TO SCROLLED STORAGE
	STA 0 2 3		;PUT IT INTO THE LAST DCB
	LDA 1 NWRDS,2
	SUB 1 0			;FORM DWA-NWRDS FOR THE ACTIVE LINE
	STA 0 WAD,2
	LDA 0 C10		;DBA=10 AT START OF LINE
	STA 0 DBA,2
	STA 0 BSOFAR,2
	JMP@ LINK1,2

C10:	10
C17:	17
C177:	177


;-----------------------------------------------------------------------------
.CLRBLK: ;CLEAR THE BLOCK OF STORAGE POINTED TO BY THE DCB POINTED TO BY AC0
;-----------------------------------------------------------------------------
	STA 3 LINK2,2
	MOV 0 3
	LDA 1 2 3		;STARTING ADDRESS OF THE BLOCK'S STORAGE
	LDA 3 BLKSZ,2		;BLOCK SIZE
	ADD 3 1
	ADC 0 0
	ADD 0 1 		;LAST ADDRESS IN THE BLOCK
	NEG 3 3			;WORD COUNT
	SUB 0 0			;DATA=0
	BLKS			;ZAP!
	JMP@ LINK2,2


;----------------------------------------------------------------------------
; DO NOT LOAD ANYTHING AFTER THIS.  IT WILL GET ZEROED DURING INITIALIZATION
;----------------------------------------------------------------------------

CodeEnd = .

	.ZREL

SOM:	CodeEnd		;START OF MEMORY (TEST AREA), END OF CODE +1

	.END