;********************************************************************
;====================================================================
; Program:graphic1.asm
; programmer(s): Dincer Aydin 
; function:Displays preformatted image data on LCD
;====================================================================
;********************************************************************
; Example presented here require that the KS0108 cotrolled 128*64 LCD is connected to an
; 8255 with base address of 00h.
; This program was tested on a Powertip PG12864-D 128*64 Graphic LCD
; The Z80 was clocked at 2 MHz
;
; Connections:
; LCD data bus(pins #14-#7) connected to Port A of an 8255 with 00h base address
; LCD Enable pin(#6) connected to Port C bit #7 of the 8255
; LCD R/W pin(#5) connected to Port C bit #6 of the 8255
; LCD RS pin(#4) connected to Port C bit #5 of the 8255
; CSA Chip selection driver 1 connected to Port C bit #4 of the 8255
; CSB Chip selection driver 2 connected to Port C bit #3 of the 8255

; 8255 port address(base 00h):	
	paadr equ 00h
	pbadr equ 01h
	pcadr equ 02h
	cwadr equ 03h
; stuff to be written into the control word of the 8255:
; Some of the change the state of the ports and some manipulate
; bits on port C
	allpsin equ 9bh
	paincout equ 90h
	pandcout equ 80h
	pacoutbin equ 82h
	enable equ 0fh
	disable	equ 0eh
	read equ 0dh
	write equ 0ch
	command equ 0ah
	data equ 0bh	
	
; Define number of commands / strings 
	numofc 	equ 4h
	
; initialization:		
	ld 	sp,500h 	; Set stack pointer
	ld 	c,cwadr 
	ld 	a,pacoutbin 	; Ports A&C out,B in
	out 	(c),a
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;*********it all begins here**********:
	call 	choose_right	
	call 	prepare_lcd	; initialize the right controller
	call 	choose_left	
	call 	prepare_lcd	; initialize the left controller

	ld 	d,0b8h		; set page command is in reg D 
	ld 	b,8h		; number of pages
	ld	hl,image	; make HL point the image data
down:	call	send_64		; send 64 bytes of the image data to fill 
				; one page of the left controller
	call 	choose_right	; enable the right controller
	ld	a,d		; command byte to set the right page goes to A
	call    sendcomA	; page is set
	call	send_64		; send 64 bytes of the image data to fill 
				; one page of the right controller
	call 	choose_left	; enable the left controller
	inc 	d		; change the set page command to choose another page
	ld	a,d		; command byte to set the left page goes to A
	call    sendcomA	; page is set
	djnz	down		; loop till all pages of both halves are done
	
	halt			

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;***********************************************
; commands to prepare KS0108 for graphic loading
; these will set page address,display start address
; and y adress
;***********************************************
prepare_lcd:		
	ld 	hl,combegadr
	ld 	b,numofc
nextcom:
	call 	sendcom
	inc 	hl
	djnz	nextcom
	ret

;***********************************************
; routine to send 64 bytes (one page)
; to the LCD statring from (HL) 
;  input:(HL)
;  output:
;  Registers altered: HL
;***********************************************
send_64:
	push 	bc
	ld 	b,64
nxt_byte: 
	call 	sendchar
	inc 	hl
	djnz    nxt_byte
	pop 	bc
	ret
 	
;***********************************************
; routine to select the left part of the display
;***********************************************
choose_left:
	push 	bc
	ld 	c,pcadr	
	ld 	b,08h		; bit pattern to select the left part
	out 	(c),b
	pop	bc
	ret
	    
;************************************************
; routine to select the right part of the display
;************************************************
choose_right:
	push 	bc
	ld 	c,pcadr	
	ld 	b,10h		; bit pattern to select the right part
	out 	(c),b
	pop	bc
	ret

;====================================================================
;  Subroutine name:sendcomA & sendcom & sendcharA & sendchar 
;  programmer:Caner Buyuktuna & Dincer Aydin
;  input:A or (HL)
;  output:
;  Registers altered:A 
;  function:	sendcharA sends the data in A to the LCD
;   	  	sendchar sends the data in (HL) to the LCD
;  		sendcomA sends the command in A to the LCD
;   		sendcom sends the command in (HL) to the LCD
; !!!!!!!!!!- The busy flag is not checked -!!!!!!!!!!	
;====================================================================
sendchar:
	ld 	a,(hl)		; put the data to be sent to the LCD in A
sendcharA:	
	push 	bc		; save BC
	push 	de		; save DE
	ld 	e,data		  
	jp 	common

sendcom:
	ld 	a,(hl)
sendcomA:	
	push 	bc		; save BC
	push 	de		; save DE   
	ld 	e,command	   

common:	
	ld  	c,cwadr
	out 	(c),e		; Set/reset RS accoring to the content of register E
	ld 	d,write		
	out	(c),d		; reset RW pin for writing to LCD
	out	(paadr),a	; place data/instrucrtion to be written into portA
	ld 	d,enable	
	out	(c),d		; enable the LCD
	ld 	d,disable	
	out 	(c),d		; disable the LCD 
	pop 	de		; restore DE
	pop 	bc		; restore BC
	ret			; return


image:
; Page0
	db	0FFh,0FFh,0FFh,0FFh,07Fh,07Fh,07Fh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh
	db	0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,07Fh,07Fh,01Fh
	db	01Fh,0CFh,0F7h,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh
	db	0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,07Fh,03Fh,03Fh,0FFh,0FFh,0FFh,0FFh,0FFh
	db	0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,07Fh,07Fh,0FFh,0FFh
	db	0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0BFh,0BFh,0FFh,0FFh
	db	0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,07Fh,01Fh,05Fh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh
	db	0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh
; Page1
	db	0FFh,0FFh,083h,001h,000h,005h,00Fh,00Dh,00Fh,00Fh,03Fh,07Fh,0FFh,0FFh,0FFh,0FFh
	db	0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0E7h,0E3h,0E1h,0F0h,0F8h,0BCh,03Eh
	db	07Fh,0FFh,0FFh,0FFh,0EFh,007h,003h,00Bh,003h,00Bh,09Bh,05Fh,0DFh,0FFh,0FFh,0FFh
	db	0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,000h,000h,020h,081h,0FFh,0FFh,0FFh,0FFh,0FFh
	db	0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,07Fh,07Fh,01Fh,001h,001h,041h,0D4h,0F6h,0FCh
	db	0FFh,0FFh,0FFh,0FFh,0FFh,0BFh,03Fh,043h,003h,081h,0D4h,0F5h,0FCh,0FFh,0FFh,0FFh
	db	0FFh,0FFh,0FFh,0FFh,0FBh,0F1h,000h,000h,080h,009h,01Fh,0A3h,00Fh,0FFh,01Fh,09Fh
	db	07Fh,07Fh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh
; Pageh,02
	db	0FFh,0FFh,0FFh,008h,012h,000h,080h,0E0h,0F8h,0F0h,0E0h,0C0h,084h,081h,001h,03Dh
	db	00Fh,02Fh,03Fh,07Fh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0F9h,0C0h,086h,000h
	db	0D2h,0FFh,0FFh,0FFh,0FFh,0FCh,00Ch,004h,000h,000h,0E0h,070h,083h,009h,07Fh,07Fh
	db	0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,003h,000h,000h,0D8h,0FDh,0FFh,0FFh,0FFh,0FFh,07Fh
	db	07Fh,03Fh,037h,017h,089h,086h,0C1h,0F1h,0F8h,0FCh,0FEh,0FFh,0FFh,07Fh,03Fh,01Fh
	db	00Fh,047h,003h,063h,0D1h,031h,090h,05Ch,01Eh,0BFh,07Fh,03Fh,07Fh,07Fh,07Fh,03Fh
	db	07Fh,07Fh,0FFh,0FFh,0FFh,0FFh,0FFh,0F8h,0C0h,022h,000h,03Ah,0FEh,0FCh,0F8h,0FAh
	db	0F5h,0E3h,0D6h,0C1h,087h,003h,023h,007h,00Fh,00Fh,00Fh,08Fh,0FFh,0FFh,0FFh,0FFh
; Page3
	db	0FFh,0FFh,00Fh,040h,002h,00Ch,0FEh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FEh,0F4h
	db	0FCh,0F8h,0C4h,022h,000h,009h,007h,087h,0E7h,0FFh,0FFh,0FFh,0FFh,0F0h,000h,002h
	db	000h,0A7h,0FFh,0FFh,0FFh,07Fh,080h,008h,084h,0DAh,0FFh,0FFh,0F0h,0B4h,062h,0E6h
	db	06Dh,03Fh,0FFh,0FFh,0FFh,0FFh,004h,080h,004h,0D5h,0FFh,0FFh,0FFh,0E1h,080h,0B0h
	db	018h,006h,03Eh,0BFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0F9h,0F8h,0F8h,0F1h,0E0h
	db	0C0h,081h,021h,044h,00Dh,029h,0B9h,079h,0FCh,0F9h,0FAh,0FAh,0F8h,0F8h,0F8h,0F8h
	db	0FCh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,004h,022h,000h,001h,003h,047h,083h,043h
	db	043h,0E1h,0F3h,0E1h,0E1h,0E1h,0F1h,0F0h,0F8h,0FCh,0FEh,0FFh,0FFh,0FFh,0FFh,0FFh
; Page4
	db	0FFh,0FFh,009h,012h,008h,009h,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,01Fh
	db	00Fh,007h,041h,070h,0DCh,0FEh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,07Fh,007h,000h,000h
	db	0E8h,0FFh,0FFh,0FFh,03Fh,000h,000h,000h,0F8h,0FFh,0FFh,0FFh,0FFh,0FFh,0FCh,0E3h
	db	000h,0C1h,008h,003h,07Fh,0FFh,010h,0A4h,044h,0A6h,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh
	db	0FCh,0FCh,0F1h,0E4h,0EBh,09Dh,003h,047h,0CFh,04Fh,05Fh,0FFh,0FFh,0FFh,0FFh,0FFh
	db	0FFh,0FFh,0FFh,0FEh,0F8h,0F8h,0E0h,018h,002h,083h,00Fh,0FFh,07Fh,0FFh,0FFh,0FFh
	db	0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,03Fh,042h,004h,080h,0FAh,0FEh,0F9h,0F0h,0E0h
	db	00Bh,000h,001h,0B7h,03Fh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh
; Page5
	db	0FFh,07Fh,0C0h,080h,0E0h,0F8h,0FFh,0FFh,0FFh,0FFh,07Fh,01Fh,00Fh,083h,0C0h,0E0h
	db	0F8h,0FEh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,03Fh,000h,004h,000h,0D1h
	db	0FFh,0FFh,0FFh,00Fh,080h,000h,008h,0FDh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh
	db	0FFh,0F8h,000h,000h,000h,003h,003h,014h,0E8h,0F9h,0FFh,0FFh,0FFh,0FFh,0E3h,0C5h
	db	000h,003h,003h,00Fh,08Fh,0DFh,0FEh,0FEh,0F8h,0E9h,0C0h,0A7h,003h,00Bh,00Bh,007h
	db	01Fh,09Fh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0F0h,082h,000h,000h,005h,01Fh,07Fh
	db	0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,007h,000h,000h,0C0h,0FCh,0FFh,0FFh,0FFh,0FFh,0FFh
	db	0FFh,0FCh,0FAh,0FAh,000h,011h,001h,061h,0BFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh
; Page6
	db	0FFh,0FFh,0FFh,09Fh,0BFh,0A7h,0A7h,00Fh,001h,080h,0C0h,0FCh,0FEh,0FEh,0FFh,0FFh
	db	0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,000h,000h,080h,0C0h,0FFh
	db	0FFh,0FFh,0FFh,070h,0A8h,060h,0DCh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh
	db	0FFh,0FFh,0E8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