;this program tests:
;			if parallel port is present
;			if joystick hardware is present
;			joystick staus
;hitting any key will terminate the program
;display:
;	message	 if parallel port not found
;		 if joystick hw is not ready
;	two digit number	xx
;				||----- bit value 1    up 
;				|	bit value 2    down
;				|	bit value 4    right
;				|	bit value 8    left
;				|-0 if FIRE not pressed
;				  1 if FIRE button pressed


start:
	MOV 	AH,1
	INT 	016
	jnz	end_it		;is the key pressed?

	call	hw_parallel	;test if parallel port connected
	jc	error1		

	call	joy_ready	;test if joy ready
	jc	error2

	call	get_joy		;joy lines status

	push	ax

	mov 	dl,ah
	add 	dl,030 		;convert FIRE status into digit

	mov 	ah,2
	INT 	021		;out on the screen
	
	pop	ax

	AND 	al,0F
	ADD 	al,030		;convert direction status into digit
	MOV 	dl,aL
	MOV 	AH,2
	INT 	021		;out on the screen

	MOV 	DL,0D
	INT 	021		;CR
	MOV 	DL,0A
	INT 	021		;LF

	jmp short start

end_it:
	MOV 	AX,04C00
	INT 	021		;end if program


error1:

	mov 	dx,offset txt1
err:
	mov 	ah,9
	int 	021		;write text on the screen
	jmp 	start

error2:
	mov 	dx,offset txt2
	jmp 	short err	;write text using error1 routine


txt1:
	db 'Parallel port not found',0d,0a,'$'
txt2:
	db 'Joystick hardware not ready',0d,0a,'$'

;*************************
;*       JOY_READY	 *	
;*************************

joy_ready:
	;tests if the paralel interface and the joy adaptor
	;are plugged and working
	;also clears intial random values on the joy lines
	;
	;INPUT:		none
	;OUTPUT:	CY set	if joy adaptor does not work
	;		CY cleared if joy adaptor seems to work
	;DESTROYS:	flags

	push 	ax
	push	cx
	push	dx

	mov	dx,08078
	mov	al,0
	out	dx,al		;makes 0V on the D7 pin

	call	joy_pause

	mov 	dx,0807a
	in	al,dx		;reads the data (2nd)
	mov	ah,al		;stores in AH

	mov	dx,08078
	mov	al,080
	out	dx,al		;makes +5V on the D7 pin

	call	joy_pause

	mov 	dx,0807a
	in	al,dx		;reads the data (3rd)

	cmp	al,ah
	clc			;clears CY
	if e 	stc		;if 2nd and 3rd reading from joy lines
				;are equal than sets CY
	pop	dx
	pop	cx
	pop	ax
	ret

joy_pause:

	mov	cx,0aff
jp1:	nop
	loop	jp1
	ret


;************************
;*     HW_PARALEL	*
;************************
	
hw_parallel:
	;uses INT 061 to get the info about connected parallel interface
	;INPUT:		none
	;OUTPUT		CY set if no parallel interface detected
	;		CY cleared if parallel detected
	;		   than		AH - 00  if communication card
	;				AH - 02	 if parallel interface
	;DESTROYS:	AX			
	;		flags	
	;
	;note: communication card is parallel and serial interface on one board
	;use INT 061 fn 01B set up the proper function.

	mov 	ah,01a
	int 	061
	cmp 	al,0
	jz	no_hw
	cmp 	ah,00		;communication card
	je	hw_ok
	cmp 	ah,02		;paralel interface
	je	hw_ok

no_hw:	stc
	ret

hw_ok:	clc
	ret


;************************
;*	GET_JOY 	*
;************************

get_joy:
	;reads data from joystick lines
	;joystick should be before inicialized by the joy_ready routine
	;INPUT:		none
	;OUTPUT:	AL - direction
	;		AL - 0000xxxx
	;			 ||||
	;			 |||----- 1 - UP
	;			 ||------ 1 - DOWN 
	;			 |------- 1 - RIGHT
	;			 -------- 1 - LEFT
	;		AH - fire button
	;		  	00 - not pressed
	;			01 - pressed
	

	push 	dx

	mov 	dx,08078
	mov 	al,080
	out	dx,al		;makes +5V on the D7 pin of the parallel port
		
	push	cx
	mov	cx,08ff
jr1:	nop			;there must be some pause 
	loop	jr1
	pop	cx
			
	mov 	dx,0807a
	in	al,DX		;reads data from the joy pins
		
	xor	ah,ah		;ah=00

	test 	al,08		;error - UP
	if z 	or ah,1

	test 	al,02		;select - DOWN
	if z 	or ah,2	

	test 	al,020		;ack - RIGHT
	if z 	or ah,4

	test 	al,01		;paper error - LEFT
	if z 	or ah,8

	test 	al,010    	;busy - FIRE
	mov	al,0		
	if z 	inc al		;if FIRE than al=1 else al=0

	xchg	al,ah		;swaps register for ? convenience

	pop	dx
	ret

