Close

Public Domain Bootloader PDBLv1

A project log for nedoPC-16

Computer family on PIC16 microcontrollers

shaosSHAOS 06/03/2018 at 21:171 Comment

A little more about my "Public Domain Boot Loader" PDBLv1 which source code could be found right here in the file section of this project:

https://cdn.hackaday.io/files/1584186777755712/PDBLv1.c


It was written in C programming language in November 2011 to accelerate software development for PIC simulator of #3niti alpha ternary computer that was built around PIC16F870 (that has ability to re-write its own program programmatically from the code onboard). Source code was compiled into HEX by PICC 9.82.9453 lite (free version) under MPLABX IDE beta 7.12 that put all code at the end of available program space of PIC16F870:

https://cdn.hackaday.io/files/1584186777755712/PDBLv1.hex

So as you can see it takes space from address 0x2A2 and up (67% of program space - I know, it's a little bit too much) with GOTO 0x2A2 instruction at address 0x000. Bootloader takes control when you press RESET (or power-on your PIC16F870) and immediately after that send multiple ENTER keypresses to serial port with speed 9600 (8 bits, 1 stop bit and no parity) in less than 1 second after that (otherwise it will jump to address 0x001) then it will go in dialog mode with PDBLv1 prompt in your terminal:

PDBLv1
>

Available PDBL commands (all digits A and B below are hexadecimal):

!AAAA=BBBB - write word BBBB to program memory in address AAAA
!AAA=BB - write byte BB to data memory in address AAA
!AA=BB - write byte BB to EEPROM in address AA
?AAAA - read one word from program memory with address AAAA
?AAA - read one byte from data memory with address AAA
?AA - read one byte from EEPROM in address AA
. - exit from bootloader and jump to address 0x0001

Possible errors:

ERR01 - illegal hexadecimal digit was entered
ERR02 - character '=' is expected
ERR03 - enter is expected
ERR04 - attempt to write in protected region (bootloader code)

User can write words into program memory and then jump to the code by sending dot to the terminal - bootloader will perform GOTO 1 where user should have entry point of user program. Because bootloader has a number of useful subroutines already as writing and reading of EEPROM, sending and receiving bytes from terminal and also initialization of peripherals user program my utilize it by direct calling those subroutines - for this particular built (that I called v1-2A2) actual addresses may be taken from this include file (that also includes useful macros):

https://cdn.hackaday.io/files/1584186777755712/PDBLv1-2A2.inc

For example this is interface to EEPROM reading subroutine of PDBLv1:

eeprom_read             equ     0x0385
; ARG:W RET:W USE:#70,#71

_eeprom_read MACRO A,B
        movf    A,w
        call    eeprom_read
        bcf STATUS,RP0
        bcf STATUS,RP1
        movwf   B
        ENDM

_eeprom_read_ MACRO A,B
        movlw   A
        call    eeprom_read
        bcf STATUS,RP0
        bcf STATUS,RP1
        movwf   B
        ENDM

1st macro (_eeprom_read) if argument A is a data register and 2nd macro (_eeprom_read_) if argument A is an actual number (argument B is always data register to store read byte). Also this INC-file lists data registers that PDBLv1 is using and also specify free data registers that user program can use:

; COMMON #70..#7D(14) free #7E,#7F
; BANK0 #20..#3B(27) free #3C..#6F
; BANK1 (0) free #A0..#BF

This knowledge is relevant only if user program uses PDBL subroutines. If not then it could be ignored because PDBL will give full control to user program if nobody invokes bootloader from terminal in the boot time and user program then can use all available data memory.


In order to simplify process of re-flashing the PIC I wrote a program for Windows/MacOS/Linux that uses PDBL commands and sends HEX program to the PIC word by word (with verification):

https://cdn.hackaday.io/files/1584186777755712/pdbl1hex.c

PDBL makes sure that written program occupies only free space between 0x001 and 0x2A1 (and 1st GOTO is moved from address 0x000 to address 0x001 in order to co-exist with bootloader that takes control on every reset). Process of filling all available program space takes about 1m25s and from PC side it looks like this:

> pdbl1hex t1_test3.hex

pdbl1hex v1.1 sends HEX-file to the PDBLv1 device

HEX 't1_test3.hex'...
HEX Ok
Opening /dev/ttyS0
PDBLv1 device detected
Programming...
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooo
Verification...
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooo
Program OK (669 words)

 
Any questions? ;)

Discussions

SHAOS wrote 06/03/2018 at 21:48 point

I want eventually re-write PDBL to pure Assembler to decrease used space and also add more commands in future versions as:

 !AAAA=BBBB... - write words to program memory starting with AAAA
 !AAA=BB... - write bytes to data memory starting with AAA
 ?AAAA=BB - read BB words from program memory starting with AAAA
 ?AAA=BB - read BB bytes from data memory starting with AAA
 $A - call subprogram from address A
 ^A - jump to address A

and may be even extend it to whole PIC26F87X family (higher PICs from this family has more program memory and more data memory for user to use)...

  Are you sure? yes | no