Showing posts with label program memory. Show all posts
Showing posts with label program memory. Show all posts

Friday, August 18, 2017

Program memory block size

When writing to program memory from code, it is necessary to be aware of the fact that writes happens in blocks. You cannot write a single word/instruction, instead you write what is called a row or block.

I have not tested this i practice, but this datasheet gives us the row size for the PIC16F18325 when writing through the ICP - presumably it's the same for programmatically written data as well:

"When write and erase operations are done on a row basis, the row size (number of 14-bit words) for erase operation is 32 and the row size (number of 14-bit latches) for the write operation is 32"

As each word takes up 14bits, you have to write two bytes to fill it (the two MSB are ignored). Thus, rows are 32 words but 64bytes long when dealt with from code.

PS: Before doing a write, you have to erase the memory you want to write to. Erasing flips all bits to 1 whereas writing can only flip bits to 0. If bits are not 1 when writing starts, it will not be possible to get a 1 after writing either.

Program memory size on MCUs

Funny thing: In most MCU datasheets, the program memory size is listed in KB. The PIC16F18325 for example is said to have 14KB of program memory.

Don't let this fool you! It does not mean that you can store 14KB of data/constants.

The problem is, each word (instruction) on the PIC16 is 14bits long. Storing an 8bit constant in program memory (flash/ROM) takes up 14bits of space.

So how big is the memory really? That's easy: 14KB = 14 * 1024 * 8 = 114688bits, divided by 14bits per word = 8192. Thus, the "real" memory size is 8KB - you can store 8K instructions or constants.