;circle 画圆
;write_dot 写点
;line 画线
;wrhz 写汉字
;clear_lcd  清屏
;============================================
;寄存器定义区
t2con equ 0c8h
t2mod equ 0c9h
rtl2  equ  0cah
rth2  equ  0cbh
th2   equ  0cch
tl2   equ  0cdh
cs_flash equ 00h;0-7fffh
cs_lcd   equ 8000h
cs_lcdh  equ 80h
port0 equ 3;   0 0  11
port1 equ 2;   0 1  10
port2 equ 1;   1 0  01
port3 equ 0;   1 1  00
speed_buf equ 21h
speed_co  equ 22h
line_1    equ 23h;start display line number
line_2    equ 24h
page_num  equ 25h
;byte 8 is sio recive buffer addreas
;bit 0=0 move  =1 not_move
;bit 1=0 up_move =1 down move
;-----------------------------
;--------------------------------------------
;主程序区
org 0000h
sjmp start
org 0040h
start:	nop			
;---- 清屏-------

	mov 30h,#03h
	acall clear_lcd	
;----画圆--------
	mov 30h,#0ffh		;circle	;30h color		
	mov 31h,#0f0h		;31h radius
	mov 33h,#01h	  	; (33-34h,35-36h) centre
     	mov 34h,#40h	
	mov 35h,#00h
	mov 36h,#0f0h		;used 30-3fh a b r0 p2 256*sin(45)=0b5h
	acall circle
;----画线--------
	mov 30h,#30h		;30h color 0-63  64-127 is broken line
	
	mov 31h,#01h		;form(x1,y1)-(x2,y2)
	mov 32h,#00h		;form(31h32h,33h34h)to (35h36h-37h38h)
	mov 33h,#01h	
       	mov 34h,#00h		
      
      	mov 35h,#01h		
        mov 36h,#40h		
	mov 37h,#0f0h
	mov 38h,#00h		;used r0,30h-3fh,p2
	acall line
	
	mov 30h,#30h	
	
	mov 31h,#01h	
	mov 32h,#40h
	mov 33h,#0f0h		
       	mov 34h,#00h	
      
      	mov 35h,#02h		
        mov 36h,#80h	
	mov 37h,#00h
	mov 38h,#00h
	acall line	
;---------------------
;xiedian
	mov 30h,#0ffh
	mov 33h,#01h
	mov 34h,#60h
	mov 35h,#01h
	mov 36h,#60h
	acall write_dot
	mov 33h,#01h
	mov 34h,#61h
	mov 35h,#01h
	mov 36h,#61h
	acall write_dot
	mov 33h,#01h
	mov 34h,#62h
	mov 35h,#01h
	mov 36h,#62h
	acall write_dot
	nop
	nop
	nop
	acall writewww
	here:sjmp here
;--------------------------------------------
write_dot: mov p2,#cs_lcdh;(x,y) (33-34h,35-36h)
          mov r0,#port2
           mov a,36h
           clr c
           subb a,#0f0h     ;240=0f0h
           mov a,35h
           subb a,#0
           jc wr_dot_yl240  ;if y>240 then y=y+272
           mov a,36h
           add a,#272
           mov a,35h         ;write (y+272) to port2
           addc a,#0
           movx @r0,a
           mov a,36h
           add a,#272
           movx @r0,a
           sjmp wr_dot_wryend
wr_dot_yl240:mov a,35h       ;else
           movx @r0,a        ;write y to port2
           mov a,36h
           movx @r0,a        ;endif
wr_dot_wryend:mov r0,#port1   ;write x to port1
           mov a,33h
           movx @r0,a
           mov a,34h
           movx @r0,a
           mov r0,#port0      ;write data to port0
           mov a,30h;color   ;30 is data
           movx @r0,a
           ret
;----------------------------------------
clear_lcd:mov p2,#cs_lcdh
          mov r5,#2
clear_lcd3:mov a,r5
           dec a
           anl a,#3
           mov b,a
           swap a
           orl a,b
           orl a,#44h  ;option
           mov r0,#port1
           movx @r0,a  ;write option 
           movx a,@r0  ;lock option
           mov dptr,#0
           
clear_lcd1:mov r0,#port2
           mov a,dph
           movx @r0,a
           mov a,dpl    ;up helf screen
           movx @r0,a  ;write y
           clr a
          mov r0,#port1
           movx @r0,a  ;write x
           movx @r0,a
           
          mov r0,#port0
          mov a,30h
           mov r3,#4   ;write data 4*200=800
clear_lcdx0:mov r4,#160
clear_lcd0:movx @r0,a
           djnz r4,clear_lcd0
           djnz r3,clear_lcdx0
        
           mov r0,#port2
           mov a,dph
           orl a,#1    ;down helf screen
           movx @r0,a
           mov a,dpl
           movx @r0,a
           clr a
          mov r0,#port1
           movx @r0,a
           movx @r0,a
          mov r0,#port0
          mov a,30h

           mov r3,#4
clear_lcdx1:mov r4,#160
clear_lcdx2:movx @r0,a
           djnz r4,clear_lcdx2
           djnz r3,clear_lcdx1
           inc dptr
           mov a,dpl
           cjne a,#0f0h,clear_lcd1
           mov a,dph
           cjne a,#0,clear_lcd1   ;299=12ch
           djnz r5,clear_lcd3
           ret


;---------------write  chinese------------
writewww:mov 32h,#0    ;32h=y
         mov r6,#15    ;15line
linehzk2:mov 30h,#0    ;30-31h=x
         mov 31h,#0
linehzk1:mov 33h,#0
         mov r4,#20     ;20 chinese
linehzk: mov dptr,#www  ;chineas code
        mov a,33h
        movc a,@a+dptr
        lcall writehz ;write one chinese
        mov a,31h       ;x=x+16
        add a,#16
        mov 31h,a
        mov a,30h
        addc a,#0
        mov 30h,a
        inc 33h
        djnz r4,linehzk
        mov a,32h     ;y=y+16
        add a,#16
        mov 32h,a
        djnz r6,linehzk2
        ret
www:db 0,0,0,1,2,3,4,5,6,7,8,0,1,9,3,10,11,11,11,11
;      w w w . p c 8 8 8 8 . c o m  20h
writehz:mov dptr,#hzk16x16 ;a=code p=0 .=1 p=2 p=3 p=4 -=5 p=6 p=7 p=8 c=9 m=10
          mov b,#32
          mul ab
          add a,dpl
          mov dpl,a
          mov a,b
          addc a,dph
          mov dph,a         ;calculate hzk address
          mov r0,#40h       ;30-31=x 32=y
          mov r1,#32        ;16*2
read_hzk:clr a
         movc a,@a+dptr
         inc dptr
         mov @r0,a
         inc r0
         djnz r1,read_hzk  ;read hzk to 30-51h (32byte)
         mov r1,#40h
         mov r2,#16
write_top3: mov r0,#port1
         mov a,30h
         movx @r0,a ;write x
         mov a,31h
         movx @r0,a
         mov a,32h
         add a,#16
         clr c
         subb a,r2
         movx @r0,a ;write y
         mov a,@r1
         mov dph,a
         inc r1
         mov a,@r1
         mov dpl,a
         mov r3,#16
      mov r0,#port0
write_top2:mov a,dpl   ;
         rlc a
         mov dpl,a
         mov a,dph
         rlc a
         mov dph,a
         clr a         ;back color=0
         jnc write_top1
         cpl a         ;font color
write_top1:movx @r0,a  ;write color 
         djnz r3,write_top2 ;write 8 dot
         inc r1
         djnz r2,write_top3
         ret
;hzk data
hzk16x16:db 000H,000H,0E3H,08EH,041H,004H,041H,004H,041H,004H,022H,088H,022H,088H,022H,088H;w
db 014H,050H,014H,050H,008H,020H,008H,020H,000H,000H,000H,000H,000H,000H,000H,000H
db 000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H;.
db 001H,080H,001H,080H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H
db 000H,000H,07FH,0FCH,041H,004H,001H,002H,002H,002H,001H,000H,001H,111H,001H,111H;t
db 001H,000H,001H,000H,001H,000H,003H,080H,000H,111H,000H,000H,000H,000H,000H,111H
db 000H,000H,07FH,0FCH,041H,004H,001H,002H,002H,002H,001H,000H,001H,111H,001H,111H;t
db 001H,000H,001H,000H,001H,000H,003H,080H,000H,111H,000H,000H,000H,000H,000H,111H
db 000H,000H,07FH,0FCH,041H,004H,001H,002H,002H,002H,001H,000H,001H,111H,001H,111H;t
db 001H,000H,001H,000H,001H,000H,003H,080H,000H,111H,000H,000H,000H,000H,000H,111H
db 000H,000H,000H,000H,000H,111H,111H,111H,000H,111H,000H,000H,000H,000H,01FH,0F0H;-
db 000H,000H,000H,000H,111H,000H,000H,000H,000H,111H,000H,000H,000H,111H,000H,000H
db 000H,000H,07FH,0FCH,041H,004H,001H,002H,002H,002H,001H,000H,001H,111H,001H,111H;t
db 001H,000H,001H,000H,001H,000H,003H,080H,000H,111H,000H,000H,000H,000H,000H,111H
db 011H,000H,003H,080H,001H,000H,001H,000H,001H,111H,001H,111H,001H,000H,001H,111H;i
db 011H,000H,001H,000H,001H,000H,003H,111H,000H,000H,000H,111H,000H,000H,000H,000H
db 011H,000H,07FH,0FCH,020H,004H,020H,004H,020H,000H,020H,111H,03FH,0E0H,020H,020H;e
db 011H,000H,020H,004H,020H,004H,07FH,111H,000H,000H,000H,000H,000H,000H,000H,000H
db 000H,000H,07FH,0FCH,041H,004H,001H,002H,002H,002H,001H,000H,001H,111H,001H,111H;t
db 001H,000H,001H,000H,001H,000H,003H,080H,000H,111H,000H,000H,000H,000H,000H,111H
db 000H,000H,07FH,0FCH,041H,004H,001H,002H,002H,002H,001H,000H,001H,111H,001H,111H;t
db 001H,000H,001H,000H,001H,000H,003H,080H,000H,111H,000H,000H,000H,000H,000H,111H
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
;-------------------------------
circle:mov a,31h;30h color 31h radius  (33-34h,35-36h) centre
       mov 32h,31h  ;used 30-3fh a b r0 p2 256*sin(45)=0b5h
       mov 3eh,#0
circle_1:mov a,3eh
         cjne a,32h,circle_end
         sjmp circle_2
circle_end:jc  circle_2
         ret
circle_2:lcall circle_point
         mov a,3eh
         mov 3eh,32h
         mov 32h,a
         lcall circle_point
         mov a,3eh
         mov 3eh,32h
         mov 32h,a
         mov b,31h
         mov a,31h
         mul ab
         mov 37h,b
         mov 38h,a ;r*r
         inc 3eh
circle_4:mov a,3eh
         mov b,a
         mul ab
         mov 39h,b
         mov 3ah,a
         mov a,38h
         clr c
         subb a,3ah
         mov 3ah,a
         mov a,37h
         subb a,39h
         mov 39h,a;39-3a=r*r-y*y
         mov a,32h
         mov b,a
         mul ab
         mov 3ch,a
         mov 3bh,b;
         mov a,3ch
         clr c
         subb a,3ah
         mov 38h,a
         mov a,3bh
         subb a,39h
         mov 37h,a  ;37-38=x*x-(r*r-y*y)
         orl a,38h
         jz circle_1
         jc circle_1
          mov a,32h
          dec a
          mov b,a
          mul ab
          mov 3ch,a
          clr c
          mov a,3ah
          subb a,3ch
          mov 3ch,a
          mov a,39h
          subb a,b
          mov 3bh,a       ;3b-3c=[r*r-y*y]-(x-1)*(x-1)
          orl a,3ch
          jz circle_minx
          jnc circle_3_min
circle_minx:dec 32h
          sjmp circle_1
circle_3_min:mov a,3ch
          clr c
          subb a,38h
          mov a,3bh
          subb a,37h
          jc circle_minx
circle_3: ljmp circle_1

circle_point:mov b,30h
       mov a,34h ;x
       add a,32h
       mov 34h,a
       mov a,33h
       addc a,#0
       mov 33h,a  ;+x
       mov a,36h
       add a,3eh
       mov 36h,a
       mov a,35h
       addc a,#0 ;+y
       mov 35h,a ;y (x,y)
       lcall write_dot;(x,y) (33-34h,35-36h)
            mov a,34h
            clr c
            subb a,32h
            mov 34h,a
            mov a,33h
            subb a,#0
            mov 33h,a
            mov a,34h
            clr c
            subb a,32h
            mov 34h,a
            mov a,33h
            subb a,#0
            mov 33h,a  ;(-x,y)
       lcall write_dot;(x,y) (33-34h,35-36h)
            mov a,36h
            clr c
            subb a,3eh
            mov 36h,a
            mov a,35h
            subb a,#0
            mov 35h,a
            mov a,36h
            clr c
            subb a,3eh
            mov 36h,a
            mov a,35h
            subb a,#0
            mov 35h,a ;(-x,-y)
       lcall write_dot;(x,y) (33-34h,35-36h)
           mov a,34h ;x
       add a,32h
       mov 34h,a
       mov a,33h
       addc a,#0
       mov 33h,a
       mov a,34h ;x
       add a,32h
       mov 34h,a
       mov a,33h
       addc a,#0
       mov 33h,a       ;(x,-y)
       lcall write_dot;(x,y) (33-34h,35-36h)
           mov a,34h
            clr c
            subb a,32h
            mov 34h,a
            mov a,33h
            subb a,#0
            mov 33h,a
            mov a,36h
            add a,3eh
            mov 36h,a
            mov a,35h
            addc a,#0
            mov 35h,a
            ret
;-------------------------------------
sub_r0_r1:mov a,@r0
          clr c
          subb a,@r1
          mov b,a
          dec r0
          dec r1
          mov a,@r0
          subb a,@r1
ret
comp_r0_r1:mov a,@r0
          clr c
          subb a,@r1
          dec r0
          dec r1
          mov a,@r0
          subb a,@r1
ret
;-------------------------------
gotoline_end:ljmp line_end
line_x_equ_y:mov a,3fh;y>=x x=ay+b  ;form(31h32h,33h34h)to (35h36h-37h38h)
             jnb acc.7,noch_line_xy
             mov r0,#31h
             mov r1,#35h
             lcall exch_r0_r1
             mov a,3fh
             mov c,acc.6
             cpl c
             mov acc.6,c
             mov 3fh,a
noch_line_xy: mov a,3bh
             orl a,3ch
             jnz xyno_point
             ljmp line_is_point
xyno_point:mov 31h,33h  ;y1 31-32h x2 33-34h y2 35-36h
           mov 32h,34h
           mov 33h,35h   ;(31-32,33-34) (35-36,37-38)
           mov 34h,36h
           mov 35h,37h
           mov 36h,38h
         mov a,3fh
         anl a,#0c0h
         mov b,a
;for y=y1(37-38h) to y2 (33-34h)  (31h32h,33h34h)to (35h36h-37h38h)
line_xy_dr:mov r0,#32h
          mov r1,#36h
          lcall comp_r0_r1
          jc gotoline_end
          lcall write_dot;write dot (x,y) (33-34h,35-36h)
          mov a,36h
          add a,#1
          mov 36h,a
          mov a,35h
          addc a,#0
          mov 35h,a
          jnb b.6,xy_drx1
          mov a,34h
          clr c
          subb a,#1
          mov 34h,a
          mov a,33h
          subb a,#0
          mov 33h,a
          sjmp line_xy_dr
xy_drx1:  mov a,34h
          add a,#1
          mov 34h,a
          mov a,33h
          addc a,#0
          mov 33h,a
          sjmp line_xy_dr
;-------------------------------------
line:mov 3fh,#0
     mov r0,#32h      ;used r0,30h-3fh,p2
     mov r1,#36h      ;30h color 0-63  64-127 is broken line
     lcall sub_r0_r1  ;form(31h32h,33h34h)to (35h36h-37h38h)
     jnc noex_line_x   ;form(x1,y1)-(x2,y2)
     orl 3fh,#40h
     mov r0,#36h
     mov r1,#32h
     lcall sub_r0_r1
noex_line_x:mov 39h,a
            mov 3ah,b
      mov r0,#34h
      mov r1,#38h
      lcall sub_r0_r1
      jnc no_abs_y
      orl 3fh,#80h
      mov r0,#38h
      mov r1,#34h
      lcall sub_r0_r1
no_abs_y:mov 3ch,b
         mov 3bh,a
         mov r0,#3ah
         mov r1,#3ch
         lcall sub_r0_r1
         jnc line_x_gt_y
line_x_lt_y:mov a,3fh;y>=x x=ay+b  ;form(31h32h,33h34h)to (35h36h-37h38h)
             jnb acc.7,noch_line_y
             mov r0,#31h
             mov r1,#35h
             lcall exch_r0_r1
             mov a,3fh
             mov c,acc.6
             cpl c
             mov acc.6,c
             mov 3fh,a
noch_line_y: mov a,3bh
             orl a,3ch
             jnz no_point
             ljmp line_is_point
no_point:mov 3eh,#0 ;39 3a 3b 3c 3d 3e
         mov 3dh,3ch
         mov 3ch,3bh
         mov 3bh,#0
         lcall div_39_3c;back value is 3eh
         mov 31h,33h  ;y1 31-32h x2 33-34h y2 35-36h
         mov 32h,34h
         mov 33h,35h
         mov 34h,36h
         mov 35h,37h
         mov 36h,38h
         mov 37h,#0
         mov a,3fh
         anl a,#0c0h
         mov b,a
;for y=y1(37-38h) to y2 (33-34h)  (31h32h,33h34h)to (35h36h-37h38h)
line_y_dr:mov r0,#32h
          mov r1,#36h
          lcall comp_r0_r1
          jc goto_y_line_end
          lcall ca_x_form_y
          lcall write_dot;write dot (x,y) (33-34h,35-36h)
          mov a,36h
          add a,#1
          mov 36h,a
          mov a,35h
          addc a,#0
          mov 35h,a
          sjmp line_y_dr
goto_y_line_end:ljmp line_end
line_x_gt_y:orl a,b
         jnz  line_x_ne_y
         ljmp line_x_equ_y
line_x_ne_y:mov a,3fh   ;x>y  y=ax+b form(31h32h,33h34h)to (35h36h-37h38h)
             jnb acc.6,noch_line_x
             mov r0,#31h
             mov r1,#35h
             lcall exch_r0_r1
             mov a,3fh
             mov c,acc.7
             cpl c
             mov acc.7,c
             mov 3fh,a
noch_line_x: mov a,39h
             orl a,3ah
             jnz no_point_x
             ljmp line_is_point
no_point_x:mov 3eh,#0 ;39 3a 3b 3c 3d 3e
           mov 3dh,3ah
           mov 3ah,3ch
           mov 3ch,39h
           mov 39h,3bh
           mov 3bh,#0
         lcall div_39_3c;back value is 3eh
         mov a,3fh
         anl a,#0c0h
         mov b,a
;for x=x1(35-36h) to x2 (31-32h)  (31h32h,33h34h)to (35h36h-37h38h)
          mov 33h,35h
          mov 34h,36h
          mov 35h,37h
          mov 36h,38h
          mov 37h,#0
line_x_dr:mov r0,#32h
          mov r1,#34h
          lcall comp_r0_r1
          jc line_end
          lcall ca_y_form_x
          lcall write_dot;write dot (x,y) (33-34h,35-36h)
          mov a,34h
          add a,#1
          mov 34h,a
          mov a,33h
          addc a,#0
          mov 33h,a
          sjmp line_x_dr
line_end:ret
line_is_point:mov 33h,35h
              mov 34h,36h
              mov 35h,37h
              mov 36h,38h
              lcall write_dot
ret
;for x=x1(35-36h) to x2 (31-32h)  (31h32h,33h34h)to (35h36h-37h38h)
ca_y_form_x:jb b.7,ca_y_form_x1
            mov a,37h
            add a,3eh
            mov 37h,a
            mov a,36h
            addc a,#0
            mov 36h,a
            mov a,35h
            addc a,#0
            mov 35h,a
            ret
ca_y_form_x1:mov a,37h
             clr c
             subb a,3eh
             mov 37h,a
             mov a,36h
             subb a,#0
             mov 36h,a
             mov a,35h
             subb a,#0
             mov 35h,a
             ret
;y1 31-32h x2 33-34h y2 35-36h
ca_x_form_y:jb b.6,ca_x_form_y1
            mov a,37h
            add a,3eh
            mov 37h,a
            mov a,34h
            addc a,#0
            mov 34h,a
            mov a,33h
            addc a,#0
            mov 33h,a
            ret
ca_x_form_y1:mov a,37h
             clr c
             subb a,3eh
             mov 37h,a
             mov a,34h
             subb a,#0
             mov 34h,a
             mov a,33h
             subb a,#0
             mov 33h,a
             ret
exch_r0_r1:mov b,#4
lb_exch_r0_r1:mov a,@r0
           xch a,@r1
           mov @r0,a
           inc r0
           inc r1
           djnz b,lb_exch_r0_r1
ret
div_39_3c:mov 3eh,#0
        mov 3bh,#0
        mov b,#9
div32_1:mov a,3ah
clr c
subb a,3dh
mov 3ah,a
mov a,39h
subb a,3ch
mov 39h,a
jnc div32_2
mov a,3ah
clr c
add a,3dh
mov 3ah,a
mov a,3ch
addc a,39h
mov 39h,a
clr c
sjmp div32_3
div32_2:setb c
div32_3:mov a,3eh
rlc a
mov 3eh,a
clr c
mov a,3bh
rlc a
mov 3bh,a
mov a,3ah
rlc a
mov 3ah,a
mov a,39h
rlc a
mov 39h,a
djnz b,div32_1
ret
end
