Overhead associated with Reading from Program Memory

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is there a difference in overhead reading from RAM vs. reading from program memory?

Thanks,
Andy

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You may need to use a search function it work very well or read this tutorial.

https://www.avrfreaks.net/index.p...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am very familiar with that tutorial, and have refered to it many times. I have experience with PROGMEM, but just never in time sensitive situations. That tutorial does not address my question.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Avr Instruction Set datasheet lists cycles for each instruction.

ldi r31,0 //1cycle
ldi r30,0 //1cycle
lpm r24,Z //3cycles

lds r24,0x0100 /2cycles

so reading flash will be at the very least least 1 cycle slower per byte.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to refer to the AVR instruction set (not just the summary that is included in every AVR data sheet, but the long version).

This is what I understand from that document:

Each read takes 3 clock cycles as opposed to the 2 clock cycles while reading from SRAM.

The other overhead - managing the pointer registers - is the same.

Drat! curtvm beat me to it. :D

If you think education is expensive, try ignorance.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

so reading flash will be at the very least least 1 cycle slower per byte.

Often more because of the need to set up the index register. Consider this:

int main(void) 
{ 
	DDRB = x;
  94:	80 91 60 00 	lds	r24, 0x0060
  98:	87 bb       	out	0x17, r24	; 23
	DDRC = pgm_read_byte(&y);
  9a:	e4 e5       	ldi	r30, 0x54	; 84
  9c:	f0 e0       	ldi	r31, 0x00	; 0
  9e:	e4 91       	lpm	r30, Z+
  a0:	e4 bb       	out	0x14, r30	; 20