ISP_IAP_BYTE_PROGRAM 	equ 0x80;
ISP_IAP_SECTOR_ERASE 	equ 0x82;
sfr 	ONE_DATA	=0x20;
sfr	BYTE_ADDR_HIGH	=0x21;
sfr	BYTE_ADDR_LOW	=0x22;
sfr	ISP_DATA	=0xF6;
sfr	ISP_ADDRH	=0xF4;
sfr	ISP_ADDRL	=0xF5;
sfr	ISP_CMD		=0xF7;
sfr	RCON 		=0xB5;
sfr	sconf		=0xBF;
sfr	SECTOR_FIRST_BYTE_ADDR_HIGH	=0x23;
sfr	SECTOR_FIRST_BYTE_ADDR_LOW	=0x24;

org	00h
jmp	start

/*;================================================================
//
//                  S Y N C M O S   T E C H N O L O G Y
//
//           COPYRIGHT    2005 BY SyncMOS TECHNOLOGY
//                     --  ALL RIGHTS RESERVED  --
//
// File Name    ::      main.asm
// Created      ::      Oct,05,2007
// Editor       ::      chia-hua
// Revision     ::      
// Description  ::      EEPROM +768 Byte    
/;=================================================================*/
;====================================================================
; (1) page erase 2600h ~27ffh ,
; (2) 2600h ~27ffh 512 byte byte program   
; (3) 2600h ~27ffh byte program 
; (4) read data ->change data ->page erase -> read byte and byte program =>p2.7(75ms)
;====================================================================
	org	100h
start:  
	mov	p2,#0ffh
	mov	p3,#0ffh
	call	enable_expand_RAM	;enable expanded RAM for buffer used
;================================================
; first page erase 2600~27ff
;================================================
page_erase_2600:
        mov	SECTOR_FIRST_BYTE_ADDR_HIGH,#026h
	call	ISP_Sector_erase
;================================================
; first Byte program,
; address:0x2600~~0x26ff , data:0~~255
; address:0x2700~~0x27ff , data:0~~255
;================================================
	mov	dptr,#02600h
	mov	r1,#00h  ;data
Byte_program:
	mov	BYTE_ADDR_HIGH,dph
	mov	BYTE_ADDR_LOW,dpl		
	mov	ONE_DATA,r1
	call	ISP_Byte_Program
	inc	dptr
	mov	a,dph
	inc     r1
        cjne	a,#028h,Byte_program	

;==================================================
; read data,address:0x2600~0x26ff , data:0x00~0xff
; save data to expanded RAM 0x0100~0x01ff,
; OME=1,expanded 768 byte RAM,
; one page of data ram = 256 bytes,
; Expanded RAM have three pages .RCON,#01H , page one
;================================================
read_data:
	mov	dptr,#02600h
	mov	RCON,#01H	;change expanded RAM to page 1,
	mov	r0,#00h
read_data1:
	mov	BYTE_ADDR_HIGH,dph
	mov	BYTE_ADDR_LOW,dpl
	call	ISP_Byte_read
	movx	@r0,a 
	inc	r0
	inc	dptr
	mov	a,dph
	cjne	a,#027h,read_data1
;==================================================
; read data,address:0x2700~0x27ff , data:0x00~0xff
; save data to expanded RAM 0x0200~0x02ff,
; OME=1,expanded 768 byte RAM,
; one page of data ram = 256 bytes, 
; Expanded RAM have three pages .RCON,#02H , page two
;==================================================
read_data2:
	mov	dptr,#02700h
	mov	RCON,#02H	;change expanded RAM to page 2,
	mov	r0,#00h
read_data3:
	mov	BYTE_ADDR_HIGH,dph
	mov	BYTE_ADDR_LOW,dpl
	call	ISP_Byte_read
	movx	@r0,a 
        inc	r0
	inc	dptr
	mov	a,dph
	cjne	a,#028h,read_data3
;================================================
;change data;change expanded RAM 0x0105 from 0x05 to 0x55
;same as chenge 0x2605 from 0x05 to 0x55 
;================================================
	mov	r6,#055h
	mov	RCON,#01H	;change eRAM to page 1,0x0100~0x01ff 
	mov	r0,#05h
	mov	a,r6
	movx	@r0,a
;================================================
;change data;change expanded RAM 0x0210 from 0x10 to 0x66
;same as chenge 0x2710 from 0x10 to 0x66 
;================================================
	mov	r6,#066h
	mov	RCON,#02H	;change eRAM to page 2,0x0200~0x02ff
	mov	r0,#10h
	mov	a,r6
	movx	@r0,a
;================================================
; second page erase 0x2600~0x27ff
;================================================
page_erase1:
        mov	SECTOR_FIRST_BYTE_ADDR_HIGH,#026h
	call	ISP_Sector_erase
;================================================*
; second Byte program,
; read data from expanded RAM 0x0100~0x01ff and 
; byte program to address:0x2600~0x26ff,
; the 0x2605 data will change from "05h" to "55h"
;================================================
change_data1:	
	mov	RCON,#01H	;change eRAM to page 1,0x0100~0x01ff 
	mov	r0,#00h
	mov	dptr,#02600h
Byte_program11:
	movx	a,@r0		;mov eRAM 0x01xx to Acc
	mov     r1,a
	mov	BYTE_ADDR_HIGH,dph
	mov	BYTE_ADDR_LOW,dpl		
	mov	ONE_DATA,r1
	call	ISP_Byte_Program
	inc	r0
	inc     dptr
	mov	a,dph
        cjne	a,#027h,Byte_program11	
;================================================*
; second Byte program,
; read data from expanded RAM 0x0200~0x02ff and
; byte program to address:0x2700~0x27ff,
; the 0x2710 data will change from "10h" to "66h"
;================================================
change_data2:  	
	mov	dptr,#02700h
	mov	RCON,#02H	;change eRAM to page 2,0x0200~0x02ff 
	mov	r0,#00h	
Byte_program21:
	movx	a,@r0		;mov eRAM 0x02xx to Acc
	mov     r1,a
	mov	BYTE_ADDR_HIGH,dph
	mov	BYTE_ADDR_LOW,dpl		
	mov	ONE_DATA,r1
	call	ISP_Byte_Program
	inc	r0
	inc     dptr
	mov	a,dph
        cjne	a,#028h,Byte_program21
	clr	p2.7
	call	disable_expand_RAM	;disable expand RAM
	jmp     $
;========================================================
;	Expand RAM enable .
;========================================================
enable_expand_RAM:
	orl	sconf,#02h;// xxxx xx1x
        ret
;========================================================
;	Expand RAM disable .
;========================================================
disable_expand_RAM:
	anl	sconf,#0FDh;//  xxxx xx0x
        ret
;========================================================
;	ISP enable .
;========================================================
ISP_enable:
	CLR	EA
	MOV	ISP_DATA,#055H	 ;// Enable ISP function key sequence 1
	MOV	ISP_DATA,#0AAH	 ;// Enable ISP function key sequence 2
	MOV	ISP_DATA,#055H	 ;// Enable ISP function key sequence 3
	ORL	SCONF,#04H	 ;// Set ISP Enable SFR xxxx x1xx
	RET	
;========================================================
;       ISP Disable  . 
;========================================================
ISP_disable:
	ANL	SCONF,#0FBH	 ;// Disable ISP Enable SFR xxxx x0xx
	SETB	EA
	RET
;========================================================
;       ISP Byte Program  . 
;========================================================
ISP_Byte_Program:
	ACALL	ISP_enable
	MOV	ISP_DATA,ONE_DATA
	MOV	ISP_ADDRH,BYTE_ADDR_HIGH
	MOV	ISP_ADDRL,BYTE_ADDR_LOW
	MOV	ISP_CMD,#ISP_IAP_BYTE_PROGRAM
	ACALL	ISP_disable
	RET
;========================================================
;       ISP Byte Read  . 
;========================================================
ISP_Byte_read:
	MOV	ISP_ADDRH,BYTE_ADDR_HIGH
	MOV	ISP_ADDRL,BYTE_ADDR_LOW
	CLR	A
	MOV	DPH, ISP_ADDRH
	MOV	DPL, ISP_ADDRL
	MOVC	A,@A+DPTR
	RET
;========================================================
;       ISP Sector erase  . 
;========================================================
ISP_Sector_erase:
	ACALL	ISP_enable
	MOV	ISP_ADDRH,SECTOR_FIRST_BYTE_ADDR_HIGH
	MOV	ISP_ADDRL,SECTOR_FIRST_BYTE_ADDR_LOW
	MOV	ISP_CMD,#ISP_IAP_SECTOR_ERASE
	MOV	ISP_CMD,#ISP_IAP_SECTOR_ERASE
	ACALL	ISP_disable
	RET
        end

