Electronic – Atmega16 flash memory address space

atmegaembeddedflash

The atmega16 data sheet says that it has 16KB on-chip flash program memory, but the memory map for flash memory ranges from 0x0000 to 0x1FFF(8192, i.e, 8KB) only, which gives a range of only 8KB flash memory. Also the program counter is only 13 bits wide, which can access only 8KB of memory.

In my understanding the address space should have been 0x0000 to 0x3FFF, which would address the whole 16KB of flash, and the program counter should have been 14 bits to access the whole 16KB flash memory. I am confused with this clash between the total flash memory and the memory map of flash memory.

Where am I going wrong? Please help. Thanks in advance.

Best Answer

ATmega16 (and all AVR CPU) instructions are 16bit words.
So the program counter doesn't need to address bytes, but only 16bit words.

Flash is defined on the datasheet, and in marketing material as 16kiBytes, though it is actually organised and used as 8192 x 16bit words.

Hence 13 bits, 0 to 8191, is enough to address all of 16kiBytes of flash program memory.

Edit:
It is sensible to use word addressing for AVR instructions, rather than byte addressing because:

  • The bottom bit of a byte address into program code can never be used, so it is always wasted.
  • Program offsets within instructions are instruction offsets, not byte offsets, so (depending on your point of view) offsets have twice the range of instructions, or don't waste any bits.

Note:
Data memory is byte addressed. This is straightforward on the AVR because it has separate address spaces for programs and data.