What kind of function is the boot swap function? And when is it used?

Question:

What kind of function is the boot swap function? And when is it used?

Answer:

In the 78K0, in the case of startup by reset, the address (vector) of the program to be executed is read from the memory area (boot area) from address 0 of the internal flash memory, and instruction execution is started (boot) from that address. Normally, the contents of the flash memory of that area are rarely rewritten once they have been written with a flash programmer. However, during write to the internal flash memory by the program (self programming), there may be cases when rewrite including that boot area is desired. In this case, it is necessary to write the new program after first erasing the contents of the target blocks. However, if before the program is written following erasure, microcontroller reset occurs due to a power flicker or some other reason, the contents of the boot area are erased, so normal startup is not possible. (The reset vector becomes FFFFH, and the FFFFH address is in the internal peripheral function (SFR) area, and instruction fetch from that address is not possible, so that undefined code is read as the instructions and the subsequent operation becomes undefined. In other words, startup through reset becomes impossible.) The boot swap function is provided to deal with this issue.

The boot swap function is a function for performing physical memory area swaps. There are boot cluster 0 and boot cluster 1 as a physical memory area, and when the internal flash memory has been erased, boot cluster 0 is allocated to the first 4 KB (0000 to 0FFFH: blocks 0 to 3) of the flash memory, and boot cluster 1 is allocated to the following 4 KB (1000H to 1FFFH: blocks 4 to 7).

Then, when the boot flag is set, the area used as the boot area at the next reset is swapped into boot cluster 1 (indicated with a red triangle in the figure). At this stage (when the boot flag has been set and before reset has occurred), the memory map is still unchanged ("Boot flag setting" in the figure). Next, when reset occurs with the boot flag set, boot cluster 1 is placed to the area from 0 to 0FFFH, and boot cluster 0 to the area from 1000H to 1FFFH ("Further after reset" in the figure). In other words, address 0 as seen from the CPU becomes boot cluster 1, and following reset release, startup is done from boot cluster 1 (the fact that startup is from address 0 as seen from the CPU remains unchanged). Startup can be done normally even if boot cluster 0 has been erased, as long as the program required for bo ot has been copied to boot cluster 1. In this way, the boot swap function can be used to switch the cluster for startup at reset.

Thus, this function is used only for rewriting of the boot area from addresses 0 to 0FFFH through self-programming. Therefore, this function need not be used if not rewriting the boot area.

Moreover, by simply manipulating the boot flag, only the next boot location is changed, and the actual memory allocation is not switched. Since the memory allocation is switched only after reset has been applied, caution must be paid to the following points when performing rewrite with the writer.
When all area of the flash memory including the boot flag set is erased with the flash programmer, the boot flag is also cleared. However, in this state, the memory allocation remains unchanged, so that if write is performed as is, the contents of address 0 are written to boot cluster 1. Then, when startup is attempted, the boot flag is cleared, so that boot cluster 0 is used and boot cluster 1 written targeting address 0 becomes address 1000H.
To avoid such a problem, after the flash memory has been erased, apply a reset to match the memory allocation with the boot flag, and then execute writing. (Concretely, execute the erase command on its own rather than the EPV command, and then execute the write command, because a reset is automatically executed at the completion of the erase command.)
On the other hand, when using the boot swap function, writing the same contents to the two boot clusters in advance makes it possible to execute boot from either one, and write is possible with just one EPV command.