;
;          bios_s5x_ip.txt
;      
; KOTUINO mode 2     aspgm1
;
;----------------------------------------------
      .cseg at 0x8000
 startus:
  LLPRINT reinfo
  WAITT 100

;-------------------------
; * 1:
;    
 point1:
  mov murport,#init_port ; EXT_S =1,OE_L =1,MOSI_L =0,P1.3=1,
	                   ; LED_L =1,RST_L =1,SCK_L =0,MISO_L
  LLPRINT str8 ;   "    "
               ;        "   "
  acall wait_s ;       murus
;-------------------------
  WAITT 50 ; 0,5S ; "  reset"
  anl murport,#~(1<<RST_L) ; RST_L =0
  WAITT 50 ; 0,5S
  orl murport,#(1<<RST_L) ; RST_L =1
	WAITT 50 ; 0,5S
;    64    
  anl murport,#~(1<<OE_L || 1<<LED_L) ;  OE_L =0 
                  ;   LED_L =0
                  ;    Z  
                  ; MOSI_L =0,SCK_L =0,LED_L =0
;-------------------------
;    "programming enable"
  mov a,#0b10101100
  acall tr_spi
  mov a,#0b01010011
  acall tr_spi
  clr a
  acall tr_spi
  clr a
  acall tr_spi
  xrl a,#0b01101001 ;    
  jz ess_prog ;    ess_prog
;     (ERROR)
  orl murport,#(1<<OE_L || 1<<LED_L) ;  OE_L =1 
                  ;   LED_L =1
  LLPRINT str9 ;  "   ISP"
  WAITT 200 ;  2 
  sjmp point1 ;    point1
 ess_prog:
  LLPRINT str10 ;  "  "
;-------------------------
; * 2:    
 point2:
  mov a,#0b00101000
  acall tr_spi
  clr a
  acall tr_spi
  clr a
  acall tr_spi
  clr a
  acall tr_spi ;     00000 = 01E  
  ;     " 00000 = nnnn"
  acall binascii ;     r5:r4
  mov dptr,#optrs0_b
  mov a,r5 ;
  movx @dptr,a
  inc dptr
  mov a,r4 ;
  movx @dptr,a
  LLPRINT optrs0_a
  ;-------------------------
  mov a,#0b00101000;
  acall tr_spi
  mov a,#1
  acall tr_spi
  clr a
  acall tr_spi
  clr a
  acall tr_spi ;     00100 = 051  89S51
   ;  052  89S52
   ;      mk_type
  mov mk_type,a
   ;     " 00100 = nnnn"
   ;        
  acall binascii ;     r5:r4
  mov dptr,#optrs0_b
  mov a,r5
  movx @dptr,a
  inc dptr
  mov a,r4
  movx @dptr,a
  mov dptr,#(optrs0_a+12)
  mov a,#'1'
  movx @dptr,a
  LLPRINT optrs0_a
  ;-------------------------
;       
  mov a,#0b00101000
  acall tr_spi
  mov a,#2
  acall tr_spi
  clr a
  acall tr_spi
  clr a
  acall tr_spi ;     00200 = 006
          ;     " 00200 = nnnn"
  acall binascii ;     r5:r4
  mov dptr,#optrs0_b
  mov a,r5
  movx @dptr,a
  inc dptr
  mov a,r4
  movx @dptr,a
  mov dptr,#(optrs0_a+12)
  mov a,#'2'
  movx @dptr,a
  LLPRINT optrs0_a
  ;-------------------------
;         
  mov a,mk_type
  cjne a,#type51,tstp1
 ;     flash_size+1:flash_size
  mov flash_size,#low flash_size_51
  mov flash_size+1,#high flash_size_51
  mov dptr,#(optrs1_a+8)
  mov a,#'1'
  movx @dptr,a
  LLPRINT optrs1_a ; AT89S51 MC type will installed
  sjmp point3
 tstp1:
  cjne a,#type52,tstp2
 ;     flash_size+1:flash_size
  mov flash_size,#low flash_size_52
  mov flash_size+1,#high flash_size_52
  mov dptr,#(optrs1_a+8)
  mov a,#'2'
  movx @dptr,a
  LLPRINT optrs1_a ; AT89S52 MC type will installed
  sjmp point3
 tstp2:
  orl murport,#(1<<OE_L || 1<<LED_L) ;  OE_L =1 
                  ;   LED_L =1
  LLPRINT error_type ;     89s51  89s52
  WAITT 200 ;  2 
  ajmp point1 ;    point1
 error_type:
 ; "      "
  .db "\r\nstated MC does not correspond to parameter given device",0
;-------------------------
;* 3:
; -
 point3:
  mov a,#0b00100100
  acall tr_spi
  clr a
  acall tr_spi
  clr a
  acall tr_spi
  clr a
  acall tr_spi ;   ...lb3.lb2.lb1.x.x
          ;    "LB3=n, LB2=n, LB1=n"
  mov tmp_lbf,a ;    tmp_lbf
  jnb ACC.lb3_bit,tlb2
   mov dptr,#(optrs2_a+7)
   mov a,#'1'
   movx @dptr,a
 tlb2:
  jnb ACC.lb2_bit,tlb1
   mov dptr,#(optrs2_a+14)
   mov a,#'1'
   movx @dptr,a
 tlb1:
  jnb ACC.lb1_bit,tlb0
   mov dptr,#(optrs2_a+21)
   mov a,#'1'
   movx @dptr,a
 tlb0:
  LLPRINT optrs2_a
  orl murport,#(1<<LED_L) ;   LED_L =1
;-------------------------
;       0xFF
  acall blanc
;-------------------------
;      
; "      "
; "will enter hex file of the MC with terminal of the computer"
  LLPRINT str11
  PJMPR point4
;-------------------------
;* 4:
;   
;    00000 - flash_size  
;    
;   
;     00000 - dump_size
 point4:
  anl murport,#~(1<<LED_L) ;   LED_L =0
  acall dumper
  mov dump_size,#0 ; 
  inc r1 ;     256 
  mov dump_size+1,r1
;-------------------------
;* 5:
; " "
 point5:
  LLPRINT str12 ; "Chip Erase is executed"
  mov a,#0b10101100
  acall tr_spi
  mov a,#0b10000000
  acall tr_spi
  clr a
  acall tr_spi
  clr a
  acall tr_spi
  WAITT 100
  LLPRINT str13 ; "checking the execution of Chip Erase"
  mov dptr,#0 ;     
 f_test:
  acall page_hed
  acall fftsr
  jnb ver_er,go_clertest
;-------------------------
;          
  clr ver_er
  mov a,errd
  acall binascii ;  
  mov dptr,#optrs4_a+2
  mov a,r5
  movx @dptr,a
  inc dptr
  mov a,r4
  movx @dptr,a
 pt_error:
  mov a,erradr
  acall binascii ;    
  mov dptr,#optrs3_a+7
  mov a,r5
  movx @dptr,a
  inc dptr
  mov a,r4
  movx @dptr,a
  LLPRINT str16
  LLPRINT optrs3_a
  LLPRINT optrs4_a
  orl murport,#(1<<LED_L) ;   LED_L =1
  WAITT 200 ;  2 
  ajmp point1 ;    point1
;-------------------------
 go_clertest:
  LLPRINT optrs3_b ;      "*"
  inc DP0H ;   (DP0H)
  mov a,DP0H ;     (dptr=dump_size)
  cjne a,dump_size+1,f_test
  LLPRINT str14 ; "Chip Erase is it successfully executed"
;-------------------------
;* 6:
;   
;    
;  
;    " "
;  " :"
;    OE_L =1    "* 1"
;
 point6:
  LLPRINT str15 ; "page writing the dump with simultaneous verification"
  mov dptr,#0 ;     
 wr_rt:
  acall page_hed
  acall page_wr
  WAITT 150
  acall wr_tsr
   jnb ver_er,go_wr_rt
;-------------------------
;          
  clr ver_er
  mov a,errd
  acall binascii ;  
  mov dptr,#optrs4_a+2
  mov a,r5
  movx @dptr,a
  inc dptr
  mov a,r4
  movx @dptr,a
  mov a,errk
  acall binascii ;   
  mov dptr,#optrs4_a+10
  mov a,r5
  movx @dptr,a
  inc dptr
  mov a,r4
  movx @dptr,a
  ajmp pt_error ;      
;-------------------------
 go_wr_rt:
  LLPRINT optrs3_b ;      "*"
  inc DP0H ;   (DP0H)
  mov a,DP0H ;     (dptr=dump_size)
  cjne a,dump_size+1,wr_rt
  orl murport,#(1<<LED_L) ;   LED_L =1
  LLPRINT str15
  LLPRINT str3
  WAITT 250 ;  2,5  (max 255)
  ajmp point1 ;    point1
;-------------------------
