For picoboot I need to calculate the start of the application code from the address 0 rjmp instruction, and do the calculations to generate an rjmp to the start of the application from the virtual reset vector.
I know what I have to do, but I'm getting a headache trying to figure out the code to do it.
From avrdude's structures I know the size of the flash (always <= 8KB which is the addressing limit of rjmp). I know the size of the bootloader, and therefore the address of the virtual reset vector that comes just before the bootloader at the end of memory.
I save the original reset vector as follows:
appstart = *((uint8_t *)m->buf) | *((uint8_t *)m->buf +1 ) << 8;
If I have a variable flashsize (bytes), a constant BOOTLOADER_SIZE (bytes), how do I create the opcode to go in the virtual reset vector? The rjmp opcode is 0xCXXX where XXX is a 12-bit signed value representing a forward or reverse word offset. I've started the calculation as follows:
appstart = 0xc000 | (appstart & 0x0fff) ...
The way I code is I calculate/visualize things in my head, and then write the steps down in code. The rjmp address calculation is too complicated to do all in my head, which means I'm drawing a bit of a blank for the code.