	DEVICE PIC16f877,WDT_OFF


; Writing and reading to code space in the 16F87x series
; If using with the Mathias emulator you need the following:
; CVM87xM Member Module
; CVM03F  Advanced Family Module
; Version 2.6 or higher firmware
; TDE 4.1 or higher software
; Make sure that the CODE RD/WT is selected as emulated
; in you Project - Emulator section

; by James Vroman - TechTools - 6/8/00

	ORG 70H        ;memory area common between banks
ADRLOW     DS 1
ADRHIGH    DS 1
DATALOW    DS 1
DATAHIGH   DS 1

  ORG	00h
	jmp INIT

  ORG 05h

INIT     nop
   setb RP1               ;Switch to 
	clrb RP0               ;bank 2
	mov ADRLOW,#02h        ; lower address 
   mov ADRHIGH,#00h       ; upper address 
   mov DATALOW,#055h      ; lower data 
	mov DATAHIGH,#00Ah     ; upper data

START    nop

CODEWRITE nop             ;Write to the code space
   setb RP1               ;Switch to
	clrb RP0               ;bank 2
	mov  EEADRH,ADRHIGH    ;set eeadr high
	mov  EEADR,ADRLOW      ;set eeadr low
   mov  EEDATA,DATALOW    ;set low data 
	mov  EEDATH,DATAHIGH   ;set high data 
	setb RP0               ;switch to bank 3
	setb EEPGD             ;set to PROGRAM memory
	setb WREN              ;enable writes
	clrb GIE               ;disable interrupts
	mov  EECON2,#055h      ;
	mov  EECON2,#0AAh      ;
	setb EEWR              ;set write bit
	nop                    ;these are ignored by the actual PIC
	nop                    ;but do need to be here
   setb GIE               ;enable interrupts
		

CLEARALL nop              ;Clear data between write and read cycle
                          ;Only needed for this demo
	setb RP1               ;Switch to
   clrb RP0               ;bank 2
	clr DATALOW            ;clear DATALOW
   clr DATAHIGH           ;clear DATAHIGH
	

CODEREAD nop              ;read from the code space
   setb  RP1              ;Switch to 
	clrb	RP0			     ;bank 2 
	mov  EEADRH,ADRHIGH    ;set eeadr high
	mov  EEADR,ADRLOW      ;set eeadr low
	setb RP0               ;switch to bank 3
	setb EEPGD             ;set to PROGRAM memory
	setb EERD              ; set to read
	nop                    ; these are ignored by the actual PIC
	nop                    ; but do need to be here
	clrb RP0               ;switch back to bank 2
	mov DATALOW,EEDATA     ;move low data to ram
	mov DATAHIGH,EEDATH    ;move high data to ram
	nop ; set breakpoint here - look at DATALOW and DATAHIGH
	goto INIT
