DataPort = 378h
ControlPort= DataPort+2 
StatusPort = 379h
print	macro	buffer
	mov	ah,09h
	lea	dx,buffer
	int	21h
	endm
any_key	macro
	mov	ah,06h
	mov	dl,0ffh
	int	21h
	endm

get_string	macro buffer
	mov	ax,@data
	mov	ds,ax
	
	mov	dx,offset buffer
	mov	ah,0ah
	int	21h
	

	endm
;N01 delay	macro	
;N01 	local	retry,_end,ah_retry,_end_ah,bl_retry,bl_end,bh_retry,bh_end
;N01 
;N01 	mov	al,0
;N01 	mov	al,01h
;N01 retry:	
;N01 	cmp	al,0
;N01 	jz	_end
;N01 
;N01 	mov	ah,03h
;N01 ah_retry:
;N01 	cmp	ah,0
;N01 	jz	_end_ah
;N01 
;N01 	mov	bl,0ffh
;N01 bl_retry:
;N01 	cmp	bl,0
;N01 	jz	bl_end
;N01 
;N01 	mov	bh,0ffh
;N01 bh_retry:
;N01 	cmp	bh,0
;N01 	jz	bh_end
;N01 	dec	bh
;N01 	jmp	bh_retry
;N01 
;N01 bh_end:
;N01 	dec	bl
;N01 	jmp	bl_retry
;N01 bl_end:
;N01 
;N01 	dec	ah
;N01 	jmp	ah_retry
;N01 _end_ah:
;N01 	dec	al
;N01 	jmp	retry
;N01 
;N01 _end:
;N01 
;N01 		endm

;N01 add start
delay	macro
	;delay 100 us
	push	cx
	mov	cl, 3				; 100/30us (refresh period)
	call	WAIT_REFRESH
	pop	cx
	endm
;N01 add end

delay2	macro	
	local	retry,_end,ah_retry,_end_ah,bl_retry,bl_end,bh_retry,bh_end

	mov	al,0
	mov	al,01h
retry:	
	cmp	al,0
	jz	_end

	mov	ah,01h
ah_retry:
	cmp	ah,0
	jz	_end_ah

	mov	bl,0ffh
bl_retry:
	cmp	bl,0
	jz	bl_end

	mov	bh,0ffh
bh_retry:
	cmp	bh,0
	jz	bh_end
	dec	bh
	jmp	bh_retry

bh_end:
	dec	bl
	jmp	bl_retry
bl_end:

	dec	ah
	jmp	ah_retry
_end_ah:
	dec	al
	jmp	retry

_end:

		endm


check_status    macro
	mov	dx,StatusPort
	in	al,dx		;al=press key status
	endm

.model	small
.386
.stack	200h
.data
;N01	Message5		db	'LCM(20*2) and Panel test program 3.......By Lanner Inc.(2002/05/15)',0dh,0ah,'$'
	Message5		db	'LCM(20*2) and Panel test program 3.......By Lanner Inc.(2004/10/19)',0dh,0ah,'$';N01
	Message		db	'Press any key to test keypad.............Press any key to exit!!',0dh,0ah,'$'
	Message0		db	'Press Upper Key.........................Press Upper Key................'
	Message1		db	'Press Lower Key.........................Press Lower Key................'
	Message2		db	'Press ESC Key...........................Press ESC Key................'
	Message3		db	'Press Enter Key.........................Press Enter Key................'
	   
.code
main:
	mov	ax,@data
	mov	ds,ax
	print   message5
	print	message
_loop:
	check_status		;al=key pressed 
	push	ax
	delay
	pop	ax

	test	al,40h		;keypad pressed?
	jz	test_key
	test	al,08h
	jz	ESC_Enter	;ESC or Enter key
	test	al,20h
	jz	upper
	jmp	lower
ESC_Enter:
	test	al,20h
	jz	_esc
	jmp	_enter

upper:
	mov	dx,ControlPort	       
	mov	al,0c8h		       ;set command byte
	out	dx,al
	delay
;
	mov	dx,DataPort		;set function
	mov	al,38h			;5*10 dots
	out	dx,al			;2 Line display,8 bits interface 
	delay				
	mov	dx,ControlPort		;enable signal
	mov	al,0cah
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort		;set command and reset enable 
	mov	al,0c8h
	out	dx,al
	delay
;
	mov	dx,DataPort		;display on,cursor off 
	mov	al,0ch
	out	dx,al
	delay
	mov	dx,ControlPort
	mov	al,0cah
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort
	mov	al,0c8h
	out	dx,al
	delay
;
	mov	dx,DataPort	       ;clear display
	mov	al,01h
	out	dx,al
	delay
	mov	dx,ControlPort
	mov	al,0cah
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort
	mov	al,0c8h
	out	dx,al
	delay
	delay
;
	mov	dx,ControlPort		;write data
	mov	al,0c0h
	out	dx,al
;
	mov	cx,0
	delay
	delay
show:	
	mov	bx,cx		       ;display string
	mov	dx,DataPort	       
	mov	al,message0[bx]
	out	dx,al
	delay
	mov	dx,ControlPort
	mov	al,0c2h
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx
	mov	dx,ControlPort
	mov	al,0c0h
	out	dx,al
	delay
	inc	cx
	cmp	cx,79
	jbe	show
;For STB ( backlight)
	mov	dx,ControlPort		;turn off backlight
	mov	al,0c1h
	out	dx,al
	delay

	jmp	test_key
;end of upper

lower:
	mov	dx,ControlPort
;	mov	al,0c8h
	mov	al,0c9h
	out	dx,al
	delay
;
	mov	dx,DataPort
	mov	al,38h			;set function
	out	dx,al	
	delay
	mov	dx,ControlPort
	mov	al,0cah
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort
	mov	al,0c8h
	out	dx,al
	delay
;
	mov	dx,DataPort		;display on
	mov	al,0ch
	out	dx,al
	delay
	mov	dx,ControlPort
	mov	al,0cah
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort
	mov	al,0c8h
	out	dx,al
	delay
;
	mov	dx,DataPort	       ;clear display
	mov	al,01h
	out	dx,al
	delay
	mov	dx,ControlPort
	mov	al,0cah
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort
	mov	al,0c8h
	out	dx,al
	delay
	delay
;
	mov	dx,ControlPort		;write data
	mov	al,0c0h
	out	dx,al
;
	mov	cx,0
	delay
	delay
show1:	
	mov	bx,cx
	mov	dx,DataPort	       
	mov	al,message1[bx]
	out	dx,al
	delay
	mov	dx,ControlPort
	mov	al,0c2h
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort
	mov	al,0c0h
	out	dx,al
	delay
	inc	cx
	cmp	cx,79
	jbe	show1

;For STB ( backlight)
	mov	dx,ControlPort
	mov	al,0c1h
	out	dx,al
	delay

	jmp	test_key
;end of lower

_ESC:
	mov	dx,ControlPort
	mov	al,0c8h
	out	dx,al
;
	mov	dx,DataPort
	mov	al,38h			;set function
	out	dx,al	
	delay
	mov	dx,ControlPort
	mov	al,0cah
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort
	mov	al,0c8h
	out	dx,al
	delay
;
	mov	dx,DataPort		;display on
	mov	al,0ch
	out	dx,al
	delay
	mov	dx,ControlPort
	mov	al,0cah
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort
	mov	al,0c8h
	out	dx,al
	delay
;
	mov	dx,DataPort	       ;clear display
	mov	al,01h
	out	dx,al
	delay
	mov	dx,ControlPort
	mov	al,0cah
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort
	mov	al,0c8h
	out	dx,al
	delay
	delay
;
	mov	dx,ControlPort		;write data
	mov	al,0c0h
	out	dx,al
;
	mov	cx,0
	delay
	delay
show2:	
	mov	bx,cx
	mov	dx,DataPort	       
	mov	al,message2[bx]
	out	dx,al
	delay
	mov	dx,ControlPort
	mov	al,0c2h
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort
	mov	al,0c0h
	out	dx,al
	delay
	inc	cx
	cmp	cx,79
	jbe	show2
;For STB ( backlight)
	mov	dx,ControlPort
	mov	al,0c1h
	out	dx,al
	delay

	jmp	test_key
;end of _ESC	

_Enter:
	mov	dx,ControlPort
	mov	al,0c8h
	out	dx,al
;
	mov	dx,DataPort
	mov	al,38h			;set function
	out	dx,al	
	delay
	mov	dx,ControlPort
	mov	al,0cah
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort
	mov	al,0c8h
	out	dx,al
	delay
;
	mov	dx,DataPort		;display on
	mov	al,0ch
	out	dx,al
	delay
	mov	dx,ControlPort
	mov	al,0cah
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort
	mov	al,0c8h
	out	dx,al
	delay
;
	mov	dx,DataPort	       ;clear display
	mov	al,01h
	out	dx,al
	delay
	mov	dx,ControlPort
	mov	al,0cah
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort
	mov	al,0c8h
	out	dx,al
	delay
	delay
;
	mov	dx,ControlPort		;write data
	mov	al,0c0h
	out	dx,al
;
	mov	cx,0
	delay
	delay
show3:	
	mov	bx,cx
	mov	dx,DataPort	       
	mov	al,message3[bx]
	out	dx,al
	delay
	mov	dx,ControlPort
	mov	al,0c2h
	out	dx,al
;N01	delay
	push	cx			;N01
	mov	cx, 20			;N01 delay more time for command 
@@:					;N01
	delay				;N01
	loop	@B			;N01
	pop	cx			;N01
	mov	dx,ControlPort
	mov	al,0c0h
	out	dx,al
	delay
	inc	cx
	cmp	cx,79
	jbe	show3
;For STB ( backlight)
	mov	dx,ControlPort
	mov	al,0c1h
	out	dx,al
	delay

	jmp	test_key
;end of _Enter

test_key:
;	mov	al,0
;	any_key
	push	ax
	push	dx
	mov	ah,06h		 ;any key pressed ?
	mov	al,00h
	mov	dl,0ffh
	int	21h

	cmp	al,0
	pop	dx
	pop	ax
	jne	_exit
	jmp	_loop

_exit:
	mov	ah,4ch
	int	21h
;N01 add start
wait_refresh::	

		PUSH	AX			; save ax.

WR_INNER_LP:
WR_STATE_0:	IN	AL,61h	;SYS1
		TEST	AL,010H
		JZ	SHORT WR_STATE_0

WR_STATE_1:	IN	AL,61h ;SYS1
		TEST	AL,010H
		JNZ	SHORT WR_STATE_1
		LOOP	SHORT WR_INNER_LP

WR_EXIT:	POP	AX			; restore ax
		ret
;N01 add end

end	main

	

