;lv160 flash' 
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 0
ljmp start
org 3
   reti;ajmp int0
org 0bh
 reti;  ljmp intt0;
org 13h
;ajmp recivedata;reti;ajmp trdata;
org 1bh
reti
org 23h
;-------------------------------------
;1-DISPLAY RAM 2-DISPLAY FLASH 3-DISPLAY EX-RAM
;4-DISPLAY LCD-BUFFER
;5-RECIVE DATA TO LCD-BUFFER
;6-RECIVE DATA AND WRITE TO FLASH
;7-RESET CPU
;8-GOTO FLASH
;9-erase flash secttor
;--------------------------------------
prsio:jb ri,have_recive
     clr ti
      reti
have_recive: clr ri
         push acc
       mov a,sbuf
       cjne a,#24h,prsio1
       mov 8,#30h
      jb 0,siointxx
      mov p2,cs_lcdh
      mov r0,port1;#1
      mov a,#44h
      movx @r0,a
      movx a,@r0
       setb 0           ;1-DISPLAY RAM
       mov sp,#60h
       mov dptr,#mon
       push dpl
       push dph
       reti
siointxx:pop acc
       reti
prsio1:push 0
       mov 0,8
       mov @r0,a
       mov a,8
       cjne a,#5fh,prsio2
prsio2:jnc noinc_8
       inc 8
noinc_8:mov a,@r0
        cjne a,#0ah,prsio3
        sjmp prsio4
prsio3:       pop 0
       pop acc
       reti
prsio4ret:pop b
 pop dpl
 pop dph
 pop 0
 pop acc
 reti
prsio4:mov 8,#30h
        mov r0,#30h
       lcall crc_buffer
       jc prsio3
       push dph
       push dpl
       push b
       mov r0,#30h
       mov a,@r0
      cjne a,#3ah,cmpf3ah
cmpf3ah:jc nsub7f
        clr c
        subb a,#7
nsub7f: dec a
       anl a,#07h
       mov b,#3
       mul ab
       mov dptr,#com_tab
       jmp @a+dptr
com_tab:ljmp stop_move   ;1-DISPLAY RAM 
ljmp up_move             ;2-DISPLAY FLASH
ljmp down_move           ;3-DISPLAY EX-RAM
ljmp display_image          ;4-DISPLAY LCD-BUFFER
ljmp down_image          ;5-RECIVE DATA TO LCD-BUFFER
ljmp erase_image           ;6-RECIVE DATA AND WRITE TO FLASH
ljmp reset_cpu         ;7
ljmp set_mod          ;8
prsio4retx:   mov sbuf,#'X'
   clr ti
   jnb ti,$
   mov sbuf,#'X'
   clr ti
   jnb ti,$
ljmp prsio4ret
;==================================
stop_move:setb 0           ;1-DISPLAY RAM 
       reti
ljmp prsio4ret
set_mod:
up_move:clr 0             ;2-DISPLAY FLASH
        mov a,31h
        anl a,#0fh
        mov speed_buf,a
        clr 1
ljmp prsio4ret
;==================================
down_move:clr 0           ;3-DISPLAY EX-RAM
          setb 1
        mov a,31h
        anl a,#0fh
        mov speed_buf,a
ljmp prsio4ret
;==================================
display_image:mov a,31h          ;4-DISPLAY LCD-BUFFER
          anl a,#3
          lcall load_image
ljmp prsio4ret
;==================================
reset_cpu:mov ie,#0           ;6-RECIVE DATA AND WRITE TO FLASH
  clr a
  mov sp,#60h
  push acc
  push acc
  reti
;--------------------------------
erase_image:mov a,31H          ;5-RECIVE DATA TO LCD-BUFFER
            mov b,#8
            anl a,#3
            mul ab
            mov 30h,a
         mov r3,#8
erase_4_sector:lcall erase_sect_flash
       jc lcd_to_flash_er
       inc 30h
       djnz r3,erase_4_sector
       mov dptr,#eras_ok
     lcall transmit_line_rom
 ljmp prsio4ret
lcd_to_flash_er:mov dptr,#eras_er
 lcall transmit_line_rom
ljmp prsio4ret
;--------------------------------
file_end:mov dptr,#down_ok
     lcall transmit_line_rom
 ljmp prsio4ret
down_image:setb 0
           mov sbuf,#0dh
           clr ti
           jnb ti,$
           mov sbuf,#'G'
           clr ti
           clr ri
           mov r0,#80h
           mov r1,#81
rec_80byte:lcall wait_rec
           jc rec_error
           mov @r0,a
           inc r0
           djnz r1,rec_80byte
           cjne a,#'*',file_end
           sjmp file_no_end
cmpend_sig:cjne a,#'X',rec_error
;           sjmp file_end
rec_error:jnb ti,$
          mov sbuf,#'R'
          clr ti
          jnb ti,$
          sjmp down_image
;write_lcd_buffer
;  lcd addreas   37h 38h-39h (x) 3ah 3b-3ch y
file_no_end:mov p2,cs_lcdh
           mov a,37h
           mov r0,port1;#1
           movx @r0,a
           mov a,38h
           mov b,39h
           lcall chastohex
           movx @r0,a   ;write x
           mov r0,port2 ;inc r0
           mov a,3ah
           movx @r0,a
           mov a,3bh
           mov b,3ch
           lcall chastohex
           movx @r0,a ;write y
;write lcd_buffer
           mov r1,#80h
           mov r0,port0; #0
           mov r2,#80
lop_wr_lcdbuf:mov a,@r1
           movx @r0,a
           inc r1
           djnz r2,lop_wr_lcdbuf
;change data for flash save
;write_flash
           mov a,31h ;flash addreas 31h-32h 33h-34h 35-36h  5-RECIVE DATA TO LCD-BUFFER
           mov b,32h ;lcd addreas   37h 38h-39h (x) 3ah 3b-3ch y
           lcall chastohex;lcd display addreas 3dh 3eh 3fh  y
           mov 30h,a
           mov a,33h
           mov b,34h
           lcall chastohex
           mov 31h,a
           mov a,35h
           mov b,36h
           lcall chastohex
           mov 32h,a ;30h 31h 32h flash addreas
;write one byte to amd 19f016  wadl wadh wads wd is address  anl data
 mov b,31h
 mov a,30h
 mov c,b.7
 rlc a
 orl a,#0c0h
 mov 30h,a
 anl 31h,#7fh
 mov r0,#80h
 mov r1,#80
lop_wr_flash:lcall write_f160
;             jc wr_flash_er
             inc r0
             inc 32h
             mov a,32h
             jnz no_inc31h
             inc 31h
             mov a,31h
             cjne a,#080h,no_inc31h
             inc 30h
             mov 31h,#0h
no_inc31h:   djnz r1,lop_wr_flash
             jnb ti,$
             mov b,#'F'
             sjmp wr_flash_end
wr_flash_er:jnb ti,$
             mov b,#'X'
wr_flash_end:clr ti
           mov sbuf,b
ljmp prsio4ret
reti
reti
;-------------------------------------
;1-DISPLAY RAM 2-DISPLAY FLASH 3-DISPLAY EX-RAM
;4-DISPLAY LCD-BUFFER
;5-RECIVE DATA TO LCD-BUFFER
;6-RECIVE DATA AND WRITE TO FLASH
;7-RESET CPU
;8-GOTO FLASH
;9-erase flash secttor
;--------------------------------------
start:mov ie,#0
      mov sp,#60h
      mov tmod ,#21h
      mov th0,#0
      mov tl0,#0
      setb tr0
      mov psw,#0
        mov scon,#01010000b
        mov pcon,#80h
mov t2con,#00110000b;0c8h
mov t2mod,#0
mov rth2,#0ffh
mov rtl2,#0f7h ;baud_rate=115200BPS at 33m
mov t2con,#00110100b;0c8h  setb tr2
;----------set sio work as 1200 baud rate
mov ip,#10h;sio is high
setb tr1
mov th1,#0fdh;57600 at 24m
clr et1
setb tr0
       lcall wait_time
clearit:mov r0,#7fh
       clr a
clear_ram:mov @r0,a
       djnz r0,clear_ram
lcall clear_lcd
          mov dptr,#ready_st
lcall transmit_line_rom
mov 8,#30h
clr 0
setb es
setb ea
mov r7,#0
mov a,#44h
ljmp first_in
mon:jb 0,mon
mov a,r7
jnz no_drawe
lcall drawe
no_drawe:mov a,r7
mov c,acc.0
cpl c
mov acc.4,c
anl a,#11h
orl a,#44h
first_in:mov p2,cs_lcdh
mov r0,port1;#1
movx @r0,a
movx a,@r0
mov a,r7
anl a,#3
jb 0,mon
   lcall load_image
inc r7
mov a,r7
mov c,acc.0
cpl c
mov acc.4,c
anl a,#11h
orl a,#44h
movx @r0,a
movx a,@r0
anl a,#3
lcall wait_time
       ljmp mon
;=======================================
load_image:mov b,#16 ;a is image number (0,1,2)
           mul ab     ;00000 a0000 140000
           orl a,#0c0h
        mov 30h,a
        MOV 31H,#0H
        MOV 32H,#0    ;30-31-32 IS FLASH ADDREAS
xdblock240:mov r2,#1;8*256=2048 line
xdblock256:mov r5,#0dfh;1df=479
xd240line:mov p2,cs_lcdh
         lcall write_loady
         clr a
         mov r0,port1;#1
         movx @r0,a
         movx @r0,a
         mov r0,port0;#0
xdreadbyte80:mov r4,#160 ;12byte
xdreadbyte6:lcall read3byte
            lcall write_2dot
            lcall read3byte
            lcall write_2dot
djnz r4,xdreadbyte6
 mov a,r5
 clr c
 subb a,#1
 mov r5,a
 mov a,r2
 subb a,#0
 mov r2,a
 jnc xd240line
ret
setline_as0:mov line_1,#0
     mov line_2,#0
      ret
wr_img_y: mov p2,cs_lcdh
          mov r0,port2;#2
          clr a
          clr c
          subb a,r5
          mov b,a
          mov a,#8
          subb a,r2
           movx @r0,a
           mov a,b
           movx @r0,a
wr_img_wryend:ret
;-----------------------------------------
get16bit:mov a,31h
         mov b,32h
         lcall chastohex
         mov dph,a
         mov a,33h
         mov b,34h
         lcall chastohex
         mov dpl,a
ret
get20bit:mov a,31h
         mov b,32h
         lcall chastohex
         mov dpl,a
         mov a,33h
         mov b,34h
         lcall chastohex
         mov dph,a
         mov a,35h
         mov b,36h
         lcall chastohex
        xch a,dpl
ret
set_lcd_ad:mov dptr,cs_lcd
          inc dpl
           movx @dptr,a
           inc dpl
           mov a,b
           movx @dptr,a
ret
set_flash_ad:push b
 rl a
 rl a
 setb acc.7
anl a,#0fch
 mov b,a
 mov a,dph
 rl a
 rl a
 anl a,#3
 orl a,b
 mov p1,a
 mov a,dph
 anl a,#3fh
 setb acc.7
 mov dph,a
pop b
ret
crc_buffer:push acc
push b
push 0
push dph
 mov r0,#30h
 mov b,#0
crc_bu1:mov a,@r0
         xrl a,b
         rl a
         mov b,a
         mov a,@r0
        cjne a,#'*',crc_bu2
        inc r0
        mov a,@r0
        mov dph,a
        inc r0
        mov a,@r0
        mov r0,a
        mov a,b
        lcall transmit_byte
        mov a,b
        lcall chhextoas
        cjne a,dph,crc_bu4x
        mov a,0
        cjne a,b,crc_bu4x
        clr c
        sjmp crc_bu4
crc_bu2: inc r0
         mov a,r0
         cjne a,#5fh,crc_bu3
crc_bu3:jc crc_bu1
crc_bu4x: setb c
crc_bu4:pop dph
        pop 0
        pop b
        pop acc
ret
transmit_byte:push b
       lcall chhextoas
       mov sbuf,a
       clr ti
       jnb ti,$
       mov a,b
       mov sbuf,b
       clr ti
       jnb ti,$
pop b
ret
transmit_line_rom:clr a
                  movc a,@a+dptr
                  inc dptr
                  mov sbuf,a
                  clr ti
                  jnb ti,$
                  cjne a,#0ah,tr_l_rom1
ret
tr_l_rom1:jb ti,transmit_line_rom
           sjmp tr_l_rom1
CHHexTOAS:MOV B,A ;CHANGE HEX OF A TO TWO BYTE ASCII IN A(h) and B(l)
  ANL A,#0FH
   ADD A,#30H
CJNE A,#3AH,CHHTAS1
CHHTAS1:JC CHHTAS2
   ADD A,#7
CHHTAS2:XCH A,B
   swap a
    ANL A,#0FH
 ADD A,#30H
CJNE A,#3AH,CHHTAS3
CHHTAS3:JC CHHTAS4
    ADD A,#7
CHHTAS4:RET
CHASTOHex:CJNE A,#3AH,CHASTH1  ;CHANGE ASCII IN A(h) and B  TO HEX IN A
CHASTH1:JC CHASTH2
      CLR C
      SUBB A,#7
CHASTH2:ANL A,#0FH
XCH A,B
CJNE A,#3AH,CHASTH3
CHASTH3:JC CHASTH4
    CLR C
    SUBB A,#7
CHASTH4:ANL A,#0FH
SWAP A
ORL A,B
swap a
RET
transmit_20h:mov sbuf,#20h
          clr ti
          jnb ti,$
          ret
transmit_changeline:mov sbuf,#0dh
          clr ti
          jnb ti,$
          mov sbuf,#0ah
          clr ti
          jnb ti,$
ret
dis_flash_ad:         lcall transmit_changeline
        mov a,p1
        mov b,dph
        anl b,#3fh
        rrc a
        mov b.6,c
        rrc a
        mov b.7,c
        anl a,#1fh
        lcall transmit_byte
        mov a,b
        lcall transmit_byte
        mov a,dpl
        lcall transmit_byte
        lcall transmit_20h
        ret
getlcd_add:  mov p2,cs_lcdh
             mov a,32h  ;31-33H=X(0-639)  34-36H=Y(0-479)
             mov b,33h
             lcall chastohex
             mov 32H,a
             ANL 31H,#1 ;x(8-9)
             mov r0,port1;#1
             mov a,31h  ;write x
             movx @r0,a
             mov a,32h
             movx @r0,a
             mov r0,port2;#2  ;write y
             mov a,34h
             movx @r0,a  ;write y(8)
             mov a,35h
             mov b,36h
             lcall chastohex
             movx @r0,a  ;write y(0-7)
ret
dis_line_lcd:push acc
             push b
             mov a,r2
             lcall chhextoas
             mov sbuf,a
             clr ti
             jnb ti,$
             mov sbuf,b
             clr ti
             jnb ti,$
             mov sbuf,#0dh
             clr ti
             jnb ti,$
             mov sbuf,#0ah
             clr ti
             jnb ti,$
             pop b
             pop acc
             ret
eras_ok:db 'Erase image ok...',0dh,0ah
eras_er:db 'Erase image error...',0dh,0ah
down_ok:db 'X_Down_load image ok...',0dh,0ah
lcd_to_flsh_ok:db 'LCD to flash ok..',0dh,0ah
flasher:db 'Operate flash error',0dh,0ah
ready_st:db '51 is ready',0dh,0ah
write_mod:db 'Set mode ok...',0dh,0ah
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
           mov r0,port1;#1
           movx @r0,a
           movx a,@r0
           mov r2,#240
clear_lcd1:mov r0,port2;#2
           clr a
           movx @r0,a
           mov a,r2
           dec a
           movx @r0,a
           clr a
           mov r0,port1;dec r0
           movx @r0,a
           movx @r0,a
           mov r0,port0;dec r0
           mov r3,#8
clear_lcdx0:mov r4,#160
clear_lcd0:movx @r0,a
           djnz r4,clear_lcd0
           djnz r3,clear_lcdx0
           djnz r2,clear_lcd1
clear_lcd2:mov r0,port2;#2
           mov a,#1
           movx @r0,a
           mov a,r2
           dec a
           movx @r0,a
           clr a
           mov r0,port1;dec r0
           movx @r0,a
           movx @r0,a
           mov r0,port0;dec r0
           mov r3,#8
clear_lcdx1:mov r4,#160
clear_lcdx2:movx @r0,a
           djnz r4,clear_lcdx2
           djnz r3,clear_lcdx1
           djnz r2,clear_lcd2
           djnz r5,clear_lcd3
           ret
write_loady:mov r0,port2;#2
         mov a,r2
         movx @r0,a
         mov a,r5
         movx @r0,a
         ret
read3byte:mov r3,#2
          mov r1,#80h
lop_rd6byte:mov p1,30h
         mov dph,31h
         mov dpl,32h
         movx a,@dptr
         mov  @r1,a
         inc r1
        inc dptr
        mov a,dph
        cjne a,#80h,xdnoinc30h
        inc 30h
        mov dptr,#0h
xdnoinc30h:mov 32h,dpl
        mov 31h,dph
djnz r3,lop_rd6byte
        ret
write_2dot:mov r1,#80h
           mov a,@r1
           movx @r0,a ;80h
           inc r1
           mov a,@r1 ;81
           movx @r0,a ;1dot
          ret
wait_time:mov r0,#5h
mov dptr,#0
wait1:inc dptr
jb 0,wait_end
mov a,dph
orl a,dpl
jnz wait1
djnz r0,wait1
wait_end:ret
drawe:
ret
;write one byte to amd 19f016  wadl wadh wads wd is address  anl data
wait_rec:clr tr0
         mov tl0,#0
         mov th0,#0
         clr tf0
         setb tr0
wait_rec1:jb ri,have_rec
         jnb tf0,wait_rec1
         setb c
         ret
have_rec:mov a,sbuf
         clr ri
         clr c
         ret
transmit_acc:push b
             lcall chhextoas
             jnb ti,$
             mov sbuf,a
             clr ti
             jnb ti,$
             mov sbuf,b
             clr ti
             jnb ti,$
             mov sbuf,#20h
             clr ti
             pop b
             ret
;write one byte to amd 19f016  wadl wadh wads wd is address  anl data
write_f160:mov r2,#5
write_f016x0:mov p1,#1
           mov dptr,#2aaah
           mov a,#0aah
           movx @dptr,a
           mov p1,#0
           mov dptr,#5554h
           mov a,#55h
           movx @dptr,a
           mov p1,#1
           mov dptr,#2aaah
           mov a,#0a0h
           movx @dptr,a
           mov p1,30h
           mov dph,31h
           mov dpl,32h
           mov a,@r0
           movx @dptr,a
           lcall test_f160
           jc wr_f_erx
           ret
wr_f_erx: mov a,#0f0h  ;reset flash
           movx @dptr,a
           djnz r2,write_f016x0
           setb c
           ret
test_f160: movx a,@dptr  
           mov b,a
           movx a,@dptr
           xrl a,b
           jb acc.6,test_f160
flash_ok:  clr c
           ret
;f160t s0-s30 64k s31 32k s32 8k s33 8k s34 16k
;30h is sector 
erase_sect_flash:mov a,30h
     anl a,#1fh
     cjne a,#2,eraseat
eraseat:jnc nosector0
     lcall read_id
     mov a,b
     cjne a,#0c0h,nosect31;if=c0 8sector is 8k
     sjmp erase_00
nosector0:cjne a,#30,cmp30_31
cmp30_31:jc nosect31
     lcall read_id
     mov a,b
     cjne a,#0c2h,nosect31 ;if=c2 31sector is 8k
sjmp erase_31
nosect31:lcall wr_er_h ;write erase hand
     mov a,30h
     rl a
     anl a,#3eh
     orl a,#0c0h
     mov p1,a
     mov dptr,#0000h
     mov a,#30h
     movx @dptr,a
     lcall test_f160
erase_er:ret
;erase s31 32k p1=7eh dptr=0
;erase s32 8k  p1=7fh dptr=0
;erase s33 8k  p1=7fh dptr=2000h
;erase s34 16k p1=7fh dptr=4000h
erase_31:mov a,30h
         anl a,#1
         jnz erase_318k
         mov p1,#0fch
         mov dptr,#0000h
         mov a,#30h
         movx @dptr,a
         lcall test_f160
         jc erase_er
         mov p1,#0fdh
         mov dptr,#0000h
         mov a,#30h
         movx @dptr,a
         lcall test_f160
         jc erase_er
         ret
erase_318k:mov 33h,#0
           mov 32h,#0feh
erase_318k1:lcall wr_er_h
           mov p1,32h
           mov dph,33h
           mov dpl,#0
           mov a,#30h
           movx @dptr,a
           lcall test_f160
           jc erase_er
           mov a,33h
           add a,#20h
           mov 33h,a
           cjne a,#80h,erase_318k1
           mov 33h,#0
           inc 32h
           mov a,32h
           jnz erase_318k1
           ret
erase_00:mov a,30h
         anl a,#1
         jz erase_8k
erase_32k:lcall wr_er_h
         mov p1,#0c2h
         mov a,#30h
         mov dptr,#0
         movx @dptr,a
         lcall test_f160
         jc erase_er
         lcall wr_er_h
         mov p1,#0c3h
         mov dptr,#0
         mov a,#30h
         movx @dptr,a
         lcall test_f160
         jc erase_er
         ret
erase_8k:mov 33h,#0;0,2,4,6
         mov 32h,#0c0h 
erase_8k1:lcall wr_er_h
         mov p1,32h
         mov dpl,#0
         mov dph,33h
         mov a,#30h
         movx @dptr,a
         lcall test_f160
         jnc erase_8k2
         ljmp erase_er
erase_8k2:mov a,33h
         add a,#20h
         mov 33h,a
         cjne a,#80h,erase_8k1
         inc 32h
         mov 33h,#0
         mov a,32h
         cjne a,#0c2h,erase_8k1
         ret
goerase_er:ljmp erase_er
wr_er_h: mov p1,#1
         mov dptr,#2aaah
         mov a,#0aah
          movx @dptr,a
           mov p1,#0
        mov dptr,#5554h
          mov a,#55h
           movx @dptr,a
           mov p1,#1
        mov dptr,#2aaah
          mov a,#080h
           movx @dptr,a
          mov a,#0aah
            movx @dptr,a
           mov p1,#0
        mov dptr,#5554h
            mov a,#055h
              movx @dptr,a
ret
read_id:mov dptr,#2aaah ;1 010 1010 1010 1010
        mov p1,#1
        mov a,#0aah     ;
        movx @dptr,a
        mov p1,#0
        mov dptr,#5554h ;0101 0101 0101 0100
        mov a,#055h
        movx @dptr,a
        mov p1,#1
        mov dptr,#2aaah
        mov a,#090h
        movx @dptr,a
        mov p1,#0h
        mov dptr,#2
        movx a,@dptr
        mov b,a  ;a=id c0 lv16x4a c2 lv16xat at 31sector=8K
        mov dpl,#0
        movx a,@dptr
        mov dpl,a
        mov a,#0f0h
        movx @dptr,a
        mov a,dpl
is_bot: ret
s_f_top:db 'Flash is LV160T',0dh,0ah
s_f_bot:db 'Flash is LV160B',0dh,0ah
er_f_ok:db 'Erase flash ok...',0dh,0ah
er_f_er:db 'Erase flash error...',0dh,0ah
