Newest update, includes Walter's last post regarding Array size. I have to stop here to think about what I am doing. I DON'T like the Read Program Memory at the end. I think it will work as is, but it really looks like ASM to me. I think I need to utilize the READCODE command, just not sure yet how to deal with the address and data. I assume maybe the address needs to be a word, comprised of EEADRH,EEADR. Then I guess the data is also a word. then I guess I just add 2 to the ARRAYPOINTER?
IF anyone wants to chime in on this, now is a good time 
As a side note, IF anyone wants to help with this conversion, clearly I am working from the top down. If someone were to work from the bottom up, It would be easy for me to add it to what I have. Then when we meet in the middle, we will be done.
No Biggie either way.
ANY testers out there? Still looking for some. I will be able to test compile, but don't feel comfy trying to test the code as I have never used a bootloader so I won't know if problems are with my setup or the code.
Code:
' *****************************************************************************
' Software License Agreement
'
' The software supplied herewith by PBP forum members
' (the “Company”) for Microchip's PICmicro® Microcontroller's is
' intended and supplied to you, the Company’s customer, for use
' solely and exclusively on Microchip PICmicro Microcontroller
' products. The software is owned by the Company and/or its
' supplier, and is protected under applicable copyright laws. All
' rights are reserved. Any use in violation of the foregoing
' restrictions may subject the user to criminal sanctions under
' applicable laws, as well as to civil liability for the breach of
' the terms and conditions of this license.
'
' THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
' WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
' TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
' PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
' IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
' CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
'
'
' This software has been ported from Microchip's AN851 bootloader
' Changes have been made and will be made as the company sees fit.
' This version is for 16F's, there is another version for 18F's
'
'
' Memory Map
' -----------------
' | 0x0000 | Reset vector
' | |
' | 0x0004 | Interrupt vector
' | |
' | |
' |Boot Block | (this program)
' | |
' | 0x0200 | Re-mapped Reset Vector
' | 0x0204 | Re-mapped High Priority Interrupt Vector
' | |
' | |
' | |
' |Code Space | User program space
' | |
' | |
' | |
' | 0x3FFF |
' -----------------
'
' Incomming data format:
'
' {STX}{STX}{DATA}{CHKSUM}{ETX}
' / \
' ________/ \____________________________
' / \
' {COMMAND}{DLEN}{ADDRL}{ADDRH}{ADDRU}{DATA}...
'
' Definitions:
'
' STX - Start of packet indicator
' ETX - End of packet indicator
' LEN - Length of incomming packet
' DATA - General data up to 255 bytes
' CHKSUM - The 8-bit two's compliment sum of LEN & DATA
' COMMAND - Base command
' DLEN - Length of data associated to the command
' ADDR - Address up to 24 bits
' DATA - Data (if any)
'
'
' Commands:
'
' RD_VER 00 Read Version Information
' RD_MEM 01 Read Program Memory
' WR_MEM 02 Write Program Memory
' ER_MEM 03 Erase Program Memory (NOT supported by PIC16)
' RD_EE 04 Read EEDATA Memory
' WR_EE 05 Write EEDATA Memory
' RD_CONFIG 06 Read Config Memory (NOT supported by PIC16)
' WT_CONFIG 07 Write Config Memory (NOT supported by PIC16)
'
' ****************************************************************************
' *****************************************************************************
CHKSUM VAR BYTE ' Checksum accumulator
COUNTER VAR BYTE ' General counter
ABTIME VAR BYTE
RXDATA VAR BYTE
TXDATA VAR BYTE
TEMP VAR BYTE
ARRAYPOINTER VAR BYTE
FAKEW VAR BYTE
TEMPFLAG VAR BIT
PCLATH_TEMP VAR BYTE ' Interrupt context
STATUS_TEMP VAR BYTE ' save/restore registers
W_TEMP VAR BYTE
' Frame Format
'
' {STX}{STX}[{COMMAND}{DATALEN}{ADDRL}{ADDRH}{ADDRU}{...DATA...}]{CHKSUM}{ETX}
DATA_BUFF VAR BYTE[37] ' Start of receive buffer SAME ADDRESS AS COMMAND
COMMAND VAR DATA_BUFF[0] ' Data mapped in receive buffer SAME ADDRESS AS DATA_BYTE
DATA_COUNT VAR DATA_BUFF[1]
ADDRESS_L VAR DATA_BUFF[2]
ADDRESS_H VAR DATA_BUFF[3]
ADDRESS_U VAR DATA_BUFF[4]
PACKET_DATA VAR DATA_BUFF[5] 'Is this Valid? It is now!
' *****************************************************************************
' This is the remapping section, note the interrupt also saves context
' *****************************************************************************
@ ORG 0x0000 ' Re-map Reset vector **Not sure how to set the address to start at
VReset:
PCLATH = 0
goto Setup
@ ORG 0x0004 'standard interrupt vector **again not sure of the PBP equal for ORG
VInt:
W_TEMP = W 'I am assuming this will be the "W" register
STATUS_TEMP = STATUS 'We may have trouble here. ASM uses SWAP so as not to affect the status
STATUS = 0
PCLATH_TEMP = PCLATH
PCLATH = 0
goto RVInt ' Re-map Interrupt vector
' *****************************************************************************
*****************************************************************************
' Setup the appropriate registers.
Setup:
clrwdt
READ $ff,temp 'get the value of last EEPROM location
IF temp = $ff then goto SRX 'BL startup
goto RVReset ' If not 0xFF then normal reset
SRX:
RCSTA = %10000000 ' Setup rx and tx, CREN disabled
TRISC.6 = 0 ' Setup tx pin
TXSTA = %00100110
STATUS.IRP = 1
' *****************************************************************************
' *****************************************************************************
Autobaud:
'
' ___ __________ ________
' \__/ \__________/
' | |
' |-------- p ----------|
'
' p = The number of instructions between the first and last
' rising edge of the RS232 control sequence 0x0F. Other
' possible control sequences are 0x01, 0x03, 0x07, 0x1F,
' 0x3F, 0x7F.
'
' SPBRG = (p / 32) - 1 BRGH = 1
OPTION_REG = %00000011 'sets tmr0 to 1:16 prescale
STATUS.RP0 = 0
RCSTA.CREN = 0
gosub WaitForRise
TMR0 = 0 ' Start counting
gosub WaitForRise
ABTIME = TMR0 ' Read the timer
TEMPFLAG = ABTIME.0
ABTIME = ABTIME }}1
IF !TEMPFLAG THEN ABTIME.0 = 0 'Rounding
SPBRG = ABTIME
RCSTA.CREN = 1 ' Enable receive
TEMP = RCREG ' To clear the Rx buffer
TEMP = RCREG ' Both of them
OPTION_REG = %11111111 'Return Option Reg to reset state
' *****************************************************************************
' *****************************************************************************
' Read and parse the data.
StartOfLine:
GOSUB RdRS232 ' Look for a start of line
IF RXDATA != STX THEN Autobaud '{STX}{STX}
ARRAYPOINTER = 0 ' Point to the beginning of Array
CHKSUM = 0 ' Reset checksum
GetNextDat:
GOSUB RdRS232 ' Get the data
IF RXDATA = STX THEN StartOfLine ' Check for a STX
' Yes, start over
NoSTX:
IF RXDATA = ETX THEN CheckSum ' Check for a ETX
' Yes, examine checksum
NoETX:
IF RXDATA = DLE THEN GOSUB RdRS232 ' Check for a DLE
' Yes, Get the next byte
NoDLE:
DATA_BUFF[ARRAYPOINTER] = RXDATA ' Store the data
CHKSUM = CHKSUM + RXDATA ' Get sum
ARRAYPOINTER = ARRAYPOINTER + 1
goto GetNextDat
CheckSum:
IF CHKSUM = 0 THEN Autobaud ' Checksum test
' ***********************************************
' ***********************************************
' Pre-setup, common to all commands.
EEADR = ADDRESS_L 'Set all possible pointers
EEADRH = ADDRESS_H
ARRAYPOINTER = 5 'start of data in array
COUNTER = DATA_COUNT 'Setup counter
IF COUNTER = 0 THEN VReset 'Non valid count (Special Command)
' ***********************************************
' ***********************************************
' Test the command field and sub-command.
CheckCommand
IF COMMAND } 7 THEN Autobaud ' Test for a valid command
SELECT CASE COMMAND ' Perform calculated jump
CASE 0
goto ReadVersion ' 0
CASE 1
goto ReadProgMem ' 1
CASE 2
goto WriteProgMem ' 2
CASE 3
goto StartOfLine ' 3
CASE 4
goto ReadEE ' 4
CASE 5
goto WriteEE ' 5
CASE 6
goto StartOfLine ' 6
CASE 7
goto StartOfLine ' 7
'maybe add jump to reset vector in this table
'***********************************************
' ***********************************************
' Commands
' ************* Read Version
' In: {STX}{STX}[{0x00}{0x02}]{0xFF}{ETX}
' OUT: {STX}{STX}[{0x00}{VERL}{VERH}]{CHKSUM}{ETX}
ReadVersion:
DATA_BUFF[2] = MINOR_VERSION
DATA_BUFF[3] = MAJOR_VERSION
FAKEW = 4
goto WritePacket
'*************Read Program Memory
' In: {STX}{STX}[{0x01}{DLEN}{ADDRL}{ADDRH}{ADDRU}]{CHKSUM}{ETX}
' OUT: {STX}{STX}[{0x01}{DLEN}{ADDRL}{ADDRH}{ADDRU}{DATA}...]{CHKSUM}{ETX}
ReadProgMem:
RPM1:
EECON1.EEPGD = 1
EECON1.RD = 1
@nop 'not sure how to convert this to PBP
@nop
DATA_BUFF[ARRAYPOINTER] = EEDATA
ARRAYPOINTER = ARRAYPOINTER + 1
DATA_BUFF[ARRAYPOINTER] = EEDATH
ARRAYPOINTER = ARRAYPOINTER + 1
EEADR = EEADR + 1
IF EEADR = 0 then
EEADRH = EEADRH + 1
ENDIF
COUNTER = COUNTER - 1
IF COUNTER > 0 THEN RPM1 ' Not finished then repeat
DATA_COUNT = (DATA_COUNT << 1) + 5 ' Setup packet length
goto WritePacket
'************* Write Program Memory
Bookmarks