		.ORG $F000


;SOURCE LISTING OF THE VIDEO HANDLER FOR
;ELEKTOR'S DOS JUNIOR COMPUTER

;WRITTEN BY A. NACHTMANN

;©1983

;COPYRIGHT BY ELEKTOR/ELEKTUUR


;PROCESSOR: 6502 OR 65CO2
;CRT CONTROLLER: 6845 MOTOROLA, ROCKWELL, SYNERTEC


;SCREEN COMMANDS OF THE DOS JUNIOR COMPUTER

;OPERATING SYSTEM: OS-65D V3.3 (OHIO SCIENTIFIC)


;	CARRIAGE RETURN
;	LINE FEED
;	<ESC><1> CLEAR SCREEN & HOME
;	<ESC><2> CLEAR SCREEN & HOME
;	<ESC><3> HOME CURSOR
;	<ESC><4> ERASE TO EOS
;	<ESC><5> CURSOR UP
;	<ESC><6> CURSOR DOWN
;	<ESC><8> ERASE TO EOL
;	<CTL-H> CURSOR LEFT
;	<CTL-P> CURSOR RIGHT
;	<CTL-F> CURSOR TO FRONT OF LINE (EMULATED)
;	<CTL-R> CURSOR TO REAR OF LINE (EMULATED)
;	<CTL-I> TAB 8 COLUMS	(EMULATED)
;	<CTL-T> RETYPE THE CURRENT LINE (EMULATED)
;		*POINTERS AND TEMPS*

RAMPTR	= 	$006C 			;RAMPOINTER
AHOLD	=	$2363 			;SAVE THE CHARACTER HERE
FLN		=	$EFC0 			;FIRST LINE POINTER
CLN		=	FLN + 02 		;CURRENT LINE POINTER
LLN		=	CLN	+ 02		;LAST LINE POINTER
SCRPTR	=	LLN	+ 02		;SLAVE SCREEN POINTER
CURSOR	=	SCRPTR + 02		;CURRENT CURSOR
INLINE	=	CURSOR + 02		;IN LINE OF WINDOW
COL		=	INLINE + 01		;CURRENT COLUMN
ESCFLG	=	COL	+ 01		;ESC FLAG
TEMCOL	=	ESCFLG + 01		;SLAVE COLUMN
RAMBEG	=	TEMCOL + 01		;THE REFRESH RAM STARTS HEF
CHAPLN	=	RAMBEG + 02		;CHARACTER/LINE
LPSCR	=	CHAPLN + 01		;LINES/SCREEN
FORMAT	=	LPSCR + 01		;SCREEN FORMAT
INDEX	=	FORMAT + 01		;SOFTWARE STACK FOR CHAR. INPUT
BUFFER	=	INDEX + 01
TABLE	=	BUFFER + 08		;CRT FORMAT TABLE
JMPVEC	=	TABLE + $12		;COMMAND ADDRESS POINTER
CENFLG	=	JMPVEC + 02		;CENTRONICS FLAG
DUPLEX	=	CENFLG + 01		;0=FULL, 1=HALF DUPLEX
AUTOLF	=	DUPLEX + 01		;0=NO, 1=AUTO CRLF TO CENTRONICS
NMIVEC	=	AUTOLF + 01		;(NMI VECTOR)
IRQVEC	=	NMIVEC + 02		;(IRQ VECTOR)
TRAPRO	=	IRQVEC + 02		;PROCESSOR FLAG (NOT USED)


;	*SPECIAL CHARACTERS*

SPACE	=	$0020
ESC		=	$001B


;		*JUNIOR'S SERIAL I/0*

SPAD	=	$FA80			;6532 IC ON THE STANDART JC



;	*VIA ON THE EXPANSION BOARD OF THE JUNIOR COMP.*

VAPBD	=	$F800			;PORT B DATA
VAPAD	=	VAPBD + $01		;PORT A DATA
VAPBDD	=	VAPBD + $02		;PORT B DATA DIRECTION
VAPADD	=	VAPBD + $03		;PORT A DATA DIRECTION
VATACL	=	VAPBD + $04		;T1, LATCH-LOW, COUNTER-LOW
VATACH	=	VAPBD + $05		;T1, COUNTER-HIGH
VATALL	=	VAPBD + $06		;T1, LATCH-LOW
VATALH	=	VAPBD + $07		;T1, LATCH-HIGH
VATBCL	=	VAPBD + $08		;T2, LATCH-LOW,COUNTER-LOW
VATBCH	=	VAPBD + $09		;T2, COUNTER-HIGH
VASR	=	VAPBD + $0A		;SHIFT REGISTER
VAACR	=	VAPBD + $0B		;AUXILARY CONTROL REGISTER
VAPCR	=	VAPBD + $0C		;PERIPHERAL CONTROL REGISTER
VAIFR	=	VAPBD + $0D		;INTERUPT FLAG REGISTER
VAIER	=	VAPBD + $0E		;INTERUPT ENABLE REGISTER
VAPADN	=	VAPBD + $0F		;PORT A DATA, NO HANDSHAKE


;		*CRT ADDRESSES*

AR		=	$D800			;ADDRESS REGISTER OF THE CRT
RFILE	=	AR	+ 01		;REGISTER FILE OF CRT




;		*****************************
;		* VT 52 COMPATIBLE TERMINAL *
;		*****************************


;		*INTERNAL 6845 REGISTER FILE*

HORTOT	=	$0000			;HORIZONTAL TOTAL-1
HORDIS	=	$0001			;HORIZONTAL DISPLAYED
HSYPOS	=	$0002			;HORIZONTAL SYNC POSSTION
VHSYWI	=	$0003			;VERTICAL, HORIZONTAL SYNC WIDTH
VERTOT	=	$0004			;VERTICAL TOTAL-1
VTOTAJ	=	$0005			;VERTICAL TOTAL ADJUST
VERDIS	=	$0006			;VERTICAL DISPLAYED
VSYPOS	=	$0007			;VERTICAL SYNC POSITION
MODE	=	$0008			;INTERLACE MODE REGISTER
SCANLN	=	$0009			;CHARACTER SCAN LINES-1
CURSTA	=	$000A			;CURSOR START
CUREND	=	$000B			;CURSOR END
DSPSTH	=	$000C			;DISPLAY START HIGH
DSPSTL	=	$000D			;DISPLAY START LOW
CURPOH	=	$000E			;CURSOR POSITION HIGH
CURPOL	=	$000F			;CURSOR POSITION LOW
LIPENH	=	$0010			;LIGHT PEN HIGH
LIPENL	=	$0011			;LIGHT PEN LOW
UPDATH	=	$0012			;UPDATE HIGH (ROCKWELL, SYNERTEK)
UPDATL	=	$0013			;UPDATE LOW
DUMMY	=	$001F			;DUMMY REGISTER





;		***VIDEO HANDLER***

VIDEO 	PHA					;SAVE THE REGISTERS
		TXA
		PHA
		TYA
		PHA
		LDA AHOLD			;GET THE CHARACTER
		CMP #ESC			;IS IT THE ESC CHAR.?
		BNE CHECK
		STA ESCFLG			;SET THE ESC FLAG

VIDEND	PLA					;RESTORE THE REGISTERS
		TAY
		PLA
		TAX
		PLA
		BIT SPAD			;BIT SPAD CHECK FOR BREAK DURING OUTPUT
		BPL BRKTST
		RTS

BRKTST 	BIT SPAD			;BIT SPAD WAIT TILL KEY IS RELEASED
		BPL BRKTST
		JMP ($FA7C)			;GOTO BREAK HANDLER

CHECK	JSR	COMCOM			;COMPUTE THE COMMAND INDEX
		BCS	VALVEC			;THERE WAS NO COMMAND
		LDA COMADR,X		;SET THE COMMAND VECTOR
		STA JMPVEC
		INX
		LDA COMADR,X
		STA JMPVEC+01
		JMP (JMPVEC)

VALVEC	JMP VALID


;		***EXECUTE A VIDEO COMMAND***


;		*CARRIAGE RETURN*

RETURN	LDX #$00
		STX COL				;COL-0
		JSR	ADJUST			;ADJUST THE CURSOR
		JMP VIDEND

;		*LINE FEED*

FEED	JSR CURDN
		JMP VIDEND

;		*CLEAR SCREEN & HOME CURSOR*

CLRHOM	JSR HOME
		JSR ERTEOS
		JMP VIDEND

;		*CURSOR LEFT*

LEFT	JSR CURLFT
		JMP VIDEND

;		*HOME CURSOR*



HOCU	JSR HOME
		JMP VIDEND

;		*CURSOR UP*

UP		JSR CURUP
		JMP VIDEND

;		*CURSOR DOWN*

DOWN	JSR CURDN
		JMP VIDEND

;		*CURSOR RIGHT*

RIGHT	JSR CURRGT
		JMP VIDEND

;		*ERASE TO END OF LINE*

ERLNX	JSR ERTEOL
		JMP VIDEND

;		*ERASE TO END OF SCREEN*

ERSCRX	JSR ERTEOS
		JMP VIDEND




;		*DELETE THE CURRENT LINE*

DELLIN	LDX #$00
		STX COL
		JSR ADJCUR			;MOVE CURSOR TO COL
		JSR ERTEOL
		JMP VIDEND




;		*FILTER < SPACE*

VALID	LDA AHOLD
		CMP #' '
		BCS TOSCR
		JMP VIDEND



;		*TRANSFER TO SCREEN*
TOSCR	JSR TVPUT
		JMP VIDEND


;		***SUBROUTINES***


;		*FIRST LINE UP*

FLNUP	SEC
		LDA FLN
		SBC CHAPLN			;FLN=FLN-CHAPLN
		STA FLN
		LDA FLN+01
		SBC #$00
		AND #$07			;MAX IS $7FF
		STA FLN+01
		RTS

;		*CURRENT LINE UP*

CLNUP	SEC
		LDA CLN
		SBC	CHAPLN
		STA	CLN
		LDA	CLN+01
		SBC #$00
		AND #$07			;MAX IS $7FF
		STA CLN+01
		RTS

;		*LAST LINE UP*

LLNUP	SEC
		LDA LLN
		SBC CHAPLN
		STA LLN
		LDA LLN+01
		SBC #$00
		AND #$07			;MAX IS $7FF
		STA LLN+01
		RTS

;		*FIRST LINE DOWN*

FLNDN	CLC
		LDA FLN
		ADC CHAPLN
		STA FLN				;FLN=FLN+CHAPLN
		LDA FLN+01
		ADC #$00
		AND #$07			;MAX IS $7FF
		STA FLN+01
		RTS

;		*CURRENT LINE DOWN*

CLNDN	CLC
		LDA	CLN
		ADC CHAPLN
		STA CLN				;CLN=CLN+CHAPLN
		LDA CLN+01
		ADC #$00
		AND #$07			;MAX IS $7FF
		STA CLN+01
		RTS

;		*LAST LINE DOWN*

LLNDN	CLC
		LDA LLN
		ADC CHAPLN
		STA LLN				;LLN=LLN+CHAPLN
		LDA LLN+01
		ADC #$00
		AND #$07			;MAX IS $7FF
		STA LLN+01
		RTS

;		*ADJUST THE LINE POINTERS*

ADJUST	LDY COL				;IS COLUMN NEGATIVE?
		BPL ADSA			;BRANCH ON NO
		DEC INLINE			;GO BACK ONE LINE
		BPL ADJU			;BRANCH IF WE ARE STILL ON THE SCREEN
		JSR FLNUP			;FIRST LINE UP
		JSR FLNCRT			;ADJUST DISPLAY START
		JSR ERAFLN			;ERASE THE FIRST LINE
		JSR LLNUP			;LAST LINE UP
		LDY #$00			;RESET INLINE
		STY INLINE

ADJU	JSR CLNUP			;CURRENT LINE UP
		LDY CHAPLN
		DEY					;COL=CHAPLN-1
		STY COL
		JMP ADJCUR


ADSA	CPY CHAPLN 			;IS COL>=CHAPLN?
		BCC ADJCUR			;BRANCH ON NO
		LDY #$00
		STY COL				;COL=0
		INC INLINE 			;MOVE DOWN FOR 1 LINE
		LDY INLINE
		CPY LPSCR			;ARE WE STILL ON THE SCREEN?
		BCC ADJV			;IS INLINE>=LPSCR?
		JSR FLNDN			;FIRST LINE DOWN
		JSR FLNCRT 			;ADJUST DISPLAY START
		JSR LLNDN			;LAST LINE DOWN
		LDY LPSCR
		DEY
		STY INLINE 			;INLINE=LPSCR-1
		JSR ERALLN 			;ERASE LAST LINE

ADJV	JSR CLNDN			;CURRENT LINE DOWN

ADJCUR	LDX #$00
		STX CURSOR			;RESET CURSOR
		STX CURSOR+01
		LDX INLINE 			;IS INLINE=O?
		BEQ ACURX			;BRANCH ON YES

ACURA	CLC
		LDA CHAPLN
		ADC CURSOR
		STA CURSOR
		BCC ACURB
		INC CURSOR+01		;CURSOR=INLINE*CHAPLN

ACURB	DEX
		BNE ACURA

ACURX CLC
		LDA FLN
		ADC CURSOR
		STA CURSOR			;CURSOR=CURSOR+FLN
		LDA FLN+01
		ADC CURSOR+01
		STA CURSOR+01

		CLC
		LDA COL
		ADC CURSOR			;CURSOR=CURSOR+COL
		STA CURSOR
		BCC ACURC
		INC CURSOR+01

ACURC	LDX #CURPOH
		LDA CURSOR+01
		STX AR
		STA RFILE			;CURSOR--->CRT CONTROLLER
		INX
		LDA CURSOR
		STX AR
		STA RFILE
		RTS

;		*FIRST LINE TO CRT*

FLNCRT 	LDX #DSPSTH
		LDA FLN+01
		STX AR
		STA RFILE
		INX
		LDA FLN
		STX AR
		STA RFILE
		RTS


;		*COMPUTE THE RAM POINTER*

CRAMPT	CLC
		LDA SCRPTR
		ADC RAMBEG			;KAMPTR=SCRPTR+RAMBEG
		STA RAMPTR
		LDA SCRPTR+01
		ADC RAMBEG+01
		AND #$D7			;MAX IS $D7FF
		STA RAMPTR+01
		CLC
		LDA RAMPTR			;RAMPTR=RAMBEG+SCRPTR+TEMCOL
		ADC TEMCOL
		STA RAMPTR
		BCC CRAMP
		INC RAMPTR+01
		LDA RAMPTR+01
		AND #$D7			;MAX IS $D7FF
		STA RAMPTR+01

CRAMP	RTS

;		*ERASE TO END OF LINE*

ERTEOL	LDX CLN
		LDY CLN+01
		STX SCRPTR			;SCRPTR=CLN
		STY SCRPTR+01
		LDY COL				;GET THE CURR. COLUMN
		STY TEMCOL			;TEMCOL=COL
		LDX #' '			;SPACE TO X
		LDY #$00			;INDEX=0

EREOL	JSR CRAMPT			;RAMPTR=RAMBEG+SCRPTR+TEMCOL

EROLX	TXA					;SPACE--->RAM
		STA (RAMPTR),Y
		INC TEMCOL			;TEMCOL=TEMCOL+1
		INC RAMPTR
		BNE EROXX
		INC RAMPTR+01 		;RAMPTR=RAMPTR+1
		LDA	RAMPTR+01
		AND #$D7			;MAX IS $D7FF
		STA RAMPTR+01

EROXX	LDA TEMCOL			;IS TEMCOL>=CHAPLN?
		CMP CHAPLN
		BCC EROLX			;BRANCH ON NO
		RTS

;		*ERASE TO END OF SCREEN*

ERTEOS	LDX INLINE			;ARE WE ALREADY IN THE
		INX					;LAST LINE?
		CPX LPSCR
		BEQ ERTEOL			;IF YES DO ONLY "EOL"
		JSR ERTEOL			;DO "EOL" FOR THIS LINE

EREOS	CLC
		LDA SCRPTR
		ADC CHAPLN			;SCRPTR=SCRPTR+CHAPLN
		STA SCRPTR			;OR SCREEN POINTER DOWN
		LDA SCRPTR+01
		ADC #$00
		AND #$07			;MAX IS $7FF
		STA SCRPTR+01
		CMP LLN+01 			;IS SCRPTR=LLN?
		BNE EEOL
		LDA SCRPTR
		CMP LLN
		BEQ EEOLB			;ERASE LAST LINE AND STOP

EEOL	STY TEMCOL			;TEMCOL=0
		JSR EREOL
		BCS EREOS


EEOLB	STY	TEMCOL			;TEMCOL=0
		BEQ	EREOL

;		*ERASE THE FIRST LINE*

ERAFLN	LDY #$00
		STY TEMCOL
		LDX #' '
		LDA FLN
		STA SCRPTR			;SCRPTR=FLN
		LDA FLN+01
		STA SCRPTR+01
		JMP EREOL			;NOW ERASE THE FIRST LINE

;		*ERASE THE LAST LINE*

ERALLN	LDY #$00
		STY TEMCOL			;TEMCOL=0
		LDX #' '
		LDA LLN
		STA SCRPTR			;SCRPTR=LLN
		LDA LLN+01
		STA SCRPTR+01
		JMP EREOL			;NOW ERASE THE LAST LINE

;		*PUT A CHARACTER ON THE SCREEN*

TVPUT	CLC
		LDA CLN
		ADC RAMBEG
		STA RAMPTR			;RAMPTR=CLN+RAMBEG
		LDA CLN+01
		ADC RAMBEG+01
		STA RAMPTR+01
		LDY #$00
		CLC
		LDA COL				;RAMPTR=CLN+RAMBEG+COL
		ADC RAMPTR
		STA RAMPTR
		BCC TPX
		INC RAMPTR+01
		LDA RAMPTR+01
		AND #$D7			;MAX IS $D7FF
		STA RAMPTR+01

TPX		LDA AHOLD
		STA (RAMPTR),Y
		INC COL				;COL=COL+01
		JMP ADJUST

;		*CURSOR DOWN*

CURDN	INC	INLINE 			;INLINE=INLINE+1
		JSR CLNDN			;CUREENT LINE DOWN
		LDY INLINE
		CPY LPSCR			;IS INLINE>=LPSCR?
		BCC CURDNX			;BRANCH ON NO
		JSR FLNDN			;FIRST LINE DOWN
		JSR FLNCRT 			;FIRST LINE--->CRT
		JSR LLNDN			;LAST LINE DOWN
		JSR ERALLN			;ERASE LAST LINE
		LDY LPSCR
		DEY
		STY INLINE			;INLINE=LPSCR-1

CURDNX JMP ADJCUR 			;ADJUST THE CURSOR AND RETURN

;		*CURSOR UP*

CURUP	DEC INLINE 			;INLINE=INLINE-1
		JSR CLNUP			;CURRENT LINE UP
		LDY INLINE 			;IS INLINE=NEGATIVE?
		BPL CURUPX 			;BRANCH ON NO
		JSR FLNUP			;FIRST LINE UP
		JSR FLNCRT 			;FLN--->CRT
		JSR LLNUP			;LAST LINE UP
		JSR ERAFLN 			;ERASE THE FIRST LINE
		LDY #$00
		STY INLINE			;INLINE=0

CURUPX 	JMP ADJCUR 			;ADJUST THE CURSOR AND RETURN

;		*CURSOR LEFT*

CURLFT 	DEC COL				;COL=COL-1
		JMP ADJUST 			;ADJUST THE CURSOR AND RETURN

;		*CURSOR RIGHT*

CURRGT 	INC COL				;COL=COL+1
		JMP ADJUST			;ADJUST THE CURSOR AND RETURN

;		*HOME CURSOR*

HOME	LDX #$00
		STX INLINE			;INLINE=0
		STX COL				;COL =0
		LDX FLN
		STX CLN				;CLN=FLN
		LDX FLN+01
		STX CLN+01
		JMP ADJUST			;ADJUST THE CURSOR AND RETURN

;		*MASTER RESET OF THE MEMORY MAPPED VDU*

RESET	LDA #$00
		STA FLN				;FLN=$0000
		STA FLN+01
		STA CLN				;CLN=$0000
		STA CLN+01
		STA LLN				;RESET LAST LINE
		STA LLN+01
		STA COL				;COL=0
		STA INLINE			;INLINE=0
		LDX LPSCR
		DEX					;X=LPSCR-1

RSA		CLC
		LDA	CHAPLN
		ADC LLN
		STA LLN				;LLN=(LPSCR-1)*CHAPLN
		BCC RSB
		INC LLN+01

RSB		DEX
		BNE RSA
		JSR CRTINT			;SET THE CRT TIMING REGISTERS
		JSR HOME			;HOME CURSOR
		JSR ERTEOS			;CLEAR THE SCREEN
		JMP FLNCRT			;FLN--->CRT AND RETURN

;		*INITIALIZE THE CRT CONTROLLER*

CRTINT	LDX #$00

CIA 	STX AR				;SET THE FILE INDEX
		LDA TABLE,X
		STA RFILE			;VALUE--->FILE
		INX
		CPX #$10			;SET ONLY THE TIMING REGISTERS
		BNE CIA
		RTS



;		*COMPUTE THE COMMAND ADDRESS INDEX*

COMCOM	LDX #$00
		LDY #$01

COMCOA	LDA COMTAB,X
		CMP ESCFLG			;FIND THE COMMAND
		BNE	COMCOB
		LDA COMTAB,Y
		CMP AHOLD
		BNE COMCOB
		CLC					;C=0 --> X=ADDRESS INDEX
		BCC	COMCOC 			;AND RESET THE ESC-FLAG

COMCOB	INX
		INX
		INY
		INY
		CPX #$16
		BCC COMCOA 			;C=1 --> NO COMMAND

COMCOC	LDA #$00
		STA	ESCFLG 			;RESET THE ESCAPE FLAG
		RTS


;		*COMMAND TABLE*

COMTAB	.BYTE $00
		.BYTE $0D			;<CR>
		.BYTE $00
		.BYTE $0A			;<LF>
		.BYTE $00
		.BYTE $08			;<BS>,<CTL-H> BACK SPACE
		.BYTE $1B
		.BYTE '1'			;<ESC><1> CLEAR SCREEN & HOME
		.BYTE $1B
		.BYTE '2'			;<ESC><2> CLEAR SCREEN &
		.BYTE $1B
		.BYTE '3'			;<ESC><3> HOME CURSOR
		.BYTE $1B
		.BYTE '4'			;<ESC><4> ERASE TO EOS
		.BYTE $1B
		.BYTE '5'			;<ESC><5> CURSOR UP
		.BYTE $1B
		.BYTE '6'			;<ESC><6> CURSOR DOWN
		.BYTE $1B
		.BYTE '8'			;<ESC><8> ERASE TO EOL

COTABA	.BYTE $00
		.BYTE $10			;<CTL-P> CURSOR RIGHT
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF

;		*COMMAND ADDRESS TABLE*

COMADR	.BYTE $3A			;CR
		.BYTE $F0
		.BYTE $45			;LF
		.BYTE $F0
		.BYTE $54			;CURSOR LEFT
		.BYTE $F0
		.BYTE $4B			;CLEAR SCREEN & HOME
		.BYTE $F0
		.BYTE $4B			;CLEAR SCREEN & HOME
		.BYTE $F0
		.BYTE $5A			;HOME CURSOR
		.BYTE $F0
		.BYTE $78			;ERASE TO EOS
		.BYTE $F0
		.BYTE $60			;CURSOR UP
		.BYTE $F0
		.BYTE $66			;CURSOR DOWN
		.BYTE $F0
		.BYTE $72			;ERASE TO EOL
		.BYTE $F0


;		*NORMAL VIDEO COMMANDS*

		.BYTE $6C			;CURSOR RIGHT
		.BYTE $F0
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF
		.BYTE $FF



;		*CENTRONICS OUTPUT*


CENOUT	JSR INICEN 			;INIT. PRINTER I/O
		JMP CENTRO 			;AND OUTPUT THE CHARACTER



;		> VIA DEFINITIONS
;		> PA7...PAO = DATA OUTPUT
;			CA1 = /ACKN INPUT
;			CA2 = /STB OUTPUT
;			PB1 = PE INPUT
;			PBO = SEL INPUT


CENTRO	LDA VAPBD
		AND #$01			;IS THE PRINTER SELECTED?
		BEQ CTROB			;PBO = SEL INPUT
		LDA VAPBD
		AND #$02			;PAPER EMPTY?
		BNE CTROB			;PB1 = PE INPUT

CTROA	LDA AHOLD
		STA VAPAD			;OUTPUT THE CHARACTER AND

WAIT	LDA VAPBD
		AND #$01			;IS THE PRINTER SELECTED
		BEQ CTROB
		LDA VAPBD
		AND #$02			;PAPER EMPTY?
		BNE CTROB
		LDA VAIFR			;SAMPLE CA1 FLAG
		AND #$02
		BEQ WAIT			;WAIT FOR ACKNOWLEDGE
		LDA VAIFR
		ORA #$03
		STA VAIFR			;RESET CA1, CA2 FLAGS
		STA VAIFR

CTROB	RTS


;		*INITIALZE CENTRONICS*

INICEN LDA #$FF			;PAO...PA7 = OUTPUT
		STA VAPADD
		LDA #$0A			;CA2 = WRITE HANDSHAKE PULSE OUTPUT
		STA VAPCR			;CA1 = NEG. EDGE SENSITIVE
		LDA VAPBDD
		AND #$FC			;PAO,PA1 = INPUT
		STA VAPBDD
		LDA #$00			;RESET THE PRINTER FLAG
		STA CENFLG
		RTS

;		*MOVE THE CRT FILE FROM ROM TO RAM*

MOVCRT	LDA #$00			;REFRESH RAM STARTS AT $D000
		LDX #$D0
		STA RAMBEG
		STX RAMBEG+01
		LDA #$00
		TAY
		LDX FORMAT 			;GET THE CURR. FORMAT
		BEQ MCRTB

MCRTA 	CLC
		ADC #$12			;COMPUTE THE INDEX
		DEX
		BNE MCRTA

MCRTB	TAX

MCRTC	LDA CRTINA,X
		STA TABLE,Y			;MOVE THE TABLE
		INX
		INY
		CPY #$12
		BNE MCRTC
		DEY
		LDA TABLE,Y			;SET SCREEN PARAMETERS
		STA LPSCR
		DEY
		LDA TABLE,Y
		STA CHAPLN
		RTS



;		*CRT TIMING TABLES*

;		- 80*24

CRTINA 	.BYTE $80 			;HORIZONTAL TOTAL-1 = 129-1 CHAR.
		.BYTE $50           ;HORIZONTAL DISPLAYED = 80 CHAR.
		.BYTE $60           ;HORIZ. SYNC.POSITION = 96 CHAR.
		.BYTE $08           ;VERT./HORIZ. SYNC WIDTH = 16/8
		.BYTE $22           ;VERTICAL TOTAL-1 = 34 CHAR. LINES
		.BYTE $00           ;VERT. TOTAL ADJ. = 0*64 MICRO SEC.
		.BYTE $18           ;VERTICAL DISPLAYED = 24 LINES
		.BYTE $1C           ;VERT. SYNC. POSITION = 29 CHAR. LINES
		.BYTE $00           ;MODE CONTROL
		.BYTE $08           ;SCAN LINES-1 = 9-1
		.BYTE $00           ;CURSOR START
		.BYTE $09           ;CURSOR END
		.BYTE $00           ;DISPLAY START (NOT NEEDED)
		.BYTE $00
		.BYTE $00           ;CURSOR POSITION (NOT NEEDED)
		.BYTE $00           ;
		.BYTE $50           ;CHARACTERS/LINE
		.BYTE $18           ;LINES/SCREEN


;		- 80*25 -

		.BYTE $80
		.BYTE $50
		.BYTE $60
		.BYTE $08
		.BYTE $22
		.BYTE $00
		.BYTE $19
		.BYTE $1C
		.BYTE $00
		.BYTE $08
		.BYTE $00
		.BYTE $09
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $50
		.BYTE $19

;		- 64*16 -

		.BYTE $64
		.BYTE $40
		.BYTE $49
		.BYTE $05
		.BYTE $16
		.BYTE $0E
		.BYTE $10
		.BYTE $12
		.BYTE $00
		.BYTE $0C
		.BYTE $00
		.BYTE $09
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $40
		.BYTE $10

;		- 64*24 -

		.BYTE $64
		.BYTE $40
		.BYTE $52
		.BYTE $05
		.BYTE $22
		.BYTE $00
		.BYTE $18
		.BYTE $1C
		.BYTE $00
		.BYTE $08
		.BYTE $00
		.BYTE $09
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $40
		.BYTE $18

;		- 90*22 -

		.BYTE $87
		.BYTE $5A
		.BYTE $6A
		.BYTE $08
		.BYTE $22
		.BYTE $00
		.BYTE $16
		.BYTE $1C
		.BYTE $00
		.BYTE $08
		.BYTE $00
		.BYTE $09
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $5A
		.BYTE $16

;		- 48*12 -

		.BYTE $46
		.BYTE $30
		.BYTE $3A
		.BYTE $05
		.BYTE $16
		.BYTE $0E
		.BYTE $0C
		.BYTE $12
		.BYTE $00
		.BYTE $0C
		.BYTE $00
		.BYTE $09
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $30
		.BYTE $0C

;		- 24*24 -

		.BYTE $38
		.BYTE $18
		.BYTE $26
		.BYTE $05
		.BYTE $22
		.BYTE $00
		.BYTE $18
		.BYTE $1C
		.BYTE $00
		.BYTE $08
		.BYTE $00
		.BYTE $09
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $18
		.BYTE $18

;		- 80*24 (USA) -

		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $00
		.BYTE $50
		.BYTE $18



RESTTY = $FF03
RECCHA = $FE1B


CHE		JSR RESTTY
		LDA #$00
		STA FORMAT			;80*24 DISPLAY
		JSR MOVCRT
		JSR RESET

CHECKA	JSR RECCHA
		JSR VIDEO
		JMP CHECKA

		.END
