62
MEMORY ARCHITECTURE EEN 417 Fall 2013

MEMORY ARCHITECTURE

Embed Size (px)

DESCRIPTION

MEMORY ARCHITECTURE. EEN 417 Fall 2013. MEMORY ARCHITECTURES. Thinking about memory. Imagine we have an 8-bit processor Has byte addressable memory 128B of RAM Imagine this processor has an instruction: LD, R, ADR Loads data from to named register. Thinking about memory. - PowerPoint PPT Presentation

Citation preview

Page 1: MEMORY ARCHITECTURE

MEMORY ARCHITECTURE

EEN 417Fall 2013

Page 2: MEMORY ARCHITECTURE

MEMORY ARCHITECTURES

Page 3: MEMORY ARCHITECTURE

Thinking about memory

• Imagine we have an 8-bit processor– Has byte addressable memory– 128B of RAM

• Imagine this processor has an instruction:– LD, R, ADR

Loads data from <ADR> to named register

Page 4: MEMORY ARCHITECTURE

Thinking about memory

• What would the following instruction do?– LD, R0, 0xFF

• What will the result in R0 be?

Page 5: MEMORY ARCHITECTURE

Thinking about memory

• What will the result in R0 be?• Undefined -- Three possibilities:

– Throw some sort of exception– Return the contents of 0x7F– Return a byte of 0x00

• Could be designed for any of these three

Page 6: MEMORY ARCHITECTURE

An Exception?

• Would be returned by a memory management unit (if one exists)

• Sees the illegal address, and does… what?

Page 7: MEMORY ARCHITECTURE

An Exception?

• Would be returned by a memory management unit (if one exists)

• Sees the illegal address, and does… what?– Set some bit in a status register?

Page 8: MEMORY ARCHITECTURE

An Exception?

• Would be returned by a memory management unit (if one exists)

• Sees the illegal address, and does… what?– Set some bit in a status register?– Request an interrupt?

Page 9: MEMORY ARCHITECTURE

An Exception?

• Would be returned by a memory management unit (if one exists)

• Sees the illegal address, and does… what?– Set some bit in a status register?– Request an interrupt?

• What is an interrupt?

Page 10: MEMORY ARCHITECTURE

Memory Architecture: Issues Types of memory Stack Caches Scratchpad memories Absolute and relative addresses Virtual memory Heaps

– allocation/deallocation– fragmentation– garbage collection

Segmented memory spaces …

These issues loom larger in embedded systems than in general-purpose computing.

Page 11: MEMORY ARCHITECTURE

What do you think the difference is?

• Register vs. DRAM?

• Register vs. disk?

Page 12: MEMORY ARCHITECTURE

What do you think the difference is?

• Register vs. DRAM?– 100x slower

• Register vs. disk?– 10,000x slower!!!

Page 13: MEMORY ARCHITECTURE

What do you think the difference is?

• Register vs. DRAM?– 100x slower

• Register vs. disk?– 10,000x slower!!!

• These issues matter, timing can be critical in embedded systems.

Page 14: MEMORY ARCHITECTURE

ATMega328P• 8-bit microcontroller• Same principles of other

systems

Page 15: MEMORY ARCHITECTURE

Harvard Architecture• Flash• RAM• EEPROM

Page 16: MEMORY ARCHITECTURE

Three busesEEPROM shares a bus with other I/O devices

Page 17: MEMORY ARCHITECTURE

Memory Hierarchy• 32K Flash• 2K SRAM

• 32 GP Registers (8 bits)• 1K EEPROM

• Memory locations have 8-bit data values

• Can use higher precision, but over 8-bit and adds are not atomic.

Page 18: MEMORY ARCHITECTURE

Memory Hierarchy• 32K Flash• 2K SRAM

• 32 GP Registers (8 bits)• 1K EEPROM

• Memory locations have 8-bit data values

• Can use higher precision, but over 8-bit and adds are not atomic.

• Why is this important to understand?

Page 19: MEMORY ARCHITECTURE

Memory Hierarchy• 32K Flash• 2K SRAM

• 32 GP Registers (8 bits)• 1K EEPROM

• Memory locations have 8-bit data values

• Can use higher precision, but over 8-bit and adds are not atomic.

• Why is this important to understand?

• Interrupts!

Page 20: MEMORY ARCHITECTURE

Memory Hierarchy• 32K Flash• 2K SRAM

• 32 GP Registers (8 bits)• 1K EEPROM

Page 21: MEMORY ARCHITECTURE

Memory Hierarchy• 32K Flash• 2K SRAM

• 32 GP Registers (8 bits)• 1K EEPROM

• Why only 32 registers?

Page 22: MEMORY ARCHITECTURE

Memory Hierarchy• 32K Flash• 2K SRAM

• 32 GP Registers (8 bits)• 1K EEPROM

• Why only 32 registers?• How many bits do we

need to address registers?

Page 23: MEMORY ARCHITECTURE

Why only 32 registers?

• Need 5-bits to address 32 registers• Address lives in an 8-bit instruction

– Like to have instructions only take up 8-bits to fetch in one cycle.

– 16-bit instructions take two cycles to fetch.• Some instructions are only 8-bits

– Five bits will be specifying registers• How many instructions of this sort can we

have?

Page 24: MEMORY ARCHITECTURE

How many 8-bit instructions?

7 6 5 4 3 2 1 0

? ? Register Address

Register Address

Register Address

Register Address

Register Address

Register Address

Page 25: MEMORY ARCHITECTURE

How many 8-bit instructions?

7 6 5 4 3 2 1 0

? ? Register Address

Register Address

Register Address

Register Address

Register Address

Register Address

• Need on 2-bit pattern to indicate a 16 bit instructions• Only 7 instructions left.• If we had 64 registers, we’d only have 3 fast instructions

• Register files are expensive• Not because of the hardware• Due to instruction-space

Page 26: MEMORY ARCHITECTURE

I/O on Systems

• I/O registers– How do you interact with an accelerometer?

Page 27: MEMORY ARCHITECTURE

I/O on Systems

• I/O registers– How do you interact with an accelerometer?– Instruction set doesn’t tell us. Board will.– We can set memory locations from a device on the

board.• How do we know when the data is available?• Poll a register• Raise an interrupt

Page 28: MEMORY ARCHITECTURE

What does C do with a program?

• Put static variables in the low addresses– Needs to know about memory!

• Dynamic variables in the high addresses (stack).

Page 29: MEMORY ARCHITECTURE

Understanding the memory architecture

char x;x = 0x20;

• Where does x live?– What part of memory?

Page 30: MEMORY ARCHITECTURE

Understanding the memory architecture

char x;x = 0x20;

• Where does x live?– What part of memory?

• If declared outside of a procedure it’s a static variable.

• Put it at the lowest address for which there is memory available.

Page 31: MEMORY ARCHITECTURE

Understanding the memory architecture

char x;x = 0x20;

• Where does x live?– What part of memory?

• If declared inside a procedure?

Page 32: MEMORY ARCHITECTURE

Understanding the memory architecture

char *x;x = 0x20;

• Where does x live?– What part of memory?

• How much space will it take?

Page 33: MEMORY ARCHITECTURE

Understanding the memory architecture

char *x;x = 0x20;

• Where does x live?– What part of memory?

• Same as before

• How much space will it take?– 2 bytes

• 8-bit data, 16-bit addresses

Page 34: MEMORY ARCHITECTURE

Understanding the memory architecture

char *x, y;x = 0x20;y = *x;

• What does this program mean?

Page 35: MEMORY ARCHITECTURE

Understanding the memory architecture

char *x, y;x = 0x20;y = *x;

• What does this program mean?

• y will be whatever is in memory location 0x20– I/O register– Read the manual to find

out what on this hardware y will have.

Page 36: MEMORY ARCHITECTURE

Understanding the memory architecture

char *x, y;x = 0x20;y = *x;

• What does this program mean?

• y will be whatever is in memory location 0x20– I/O register– Read the manual to find

out what on this hardware y will have.

• For ATMega328P– EEPROM Data register

Page 37: MEMORY ARCHITECTURE

Understanding the memory architecture

char *x, y;x = y&;*x = 0x20;

• What does this program mean?

Page 38: MEMORY ARCHITECTURE

Understanding the memory architecture

char *x, y;x = y&;*x = 0x20;

• What does this program mean?

• x will be the address of y

• the value of y gets set to 0x20

Page 39: MEMORY ARCHITECTURE

Understanding the memory architecture

char foo() {char *x, y;x = 0x20;y = *x;return y;

}char z;int main(void){

z = foo();…

}

• What does this program mean?

• Where are x, y, and z in memory?

Page 40: MEMORY ARCHITECTURE

Understanding the memory architecture

char foo() {char *x, y;x = 0x20;y = *x;return y;

}char z;int main(void){

z = foo();…

}

• What does this program mean?

• Where are x, y, and z in memory?– Static: z– Stack: x, y

• How much space do x and y take on the stack?

Page 41: MEMORY ARCHITECTURE

Understanding the memory architecture

char foo() {char *x, y;x = 0x20;y = *x;return y;

}char z;int main(void){

z = foo();…

}

• What does this program mean?

• Where are x, y, and z in memory?– Static: z– Stack: x, y

• How much space do x and y take on the stack?– 2 bytes and 1 byte

Page 42: MEMORY ARCHITECTURE

Understanding the memory architecture

char foo() {char *x, y;x = 0x20;y = *x;return y;

}char z;int main(void){

z = foo();…

}

• What does this program mean?– Assigns to y the value of

the I/O register (EEPROM Data) 0x20 and returns that value

– What does it mean to return a value?

Page 43: MEMORY ARCHITECTURE

Understanding the memory architecture

char foo() {char *x, y;x = 0x20;y = *x;return y;

}char z;int main(void){

z = foo();…

}

• What does this program mean?– Assigns to y the value of

the I/O register (EEPROM Data) 0x20 and returns that value

– What does it mean to return a value?

• Return something on the stack… but when it returns stack is invalid memory…

• Store it in a register

Page 44: MEMORY ARCHITECTURE

Understanding the memory architecture

char foo() {char y;uint16_t x;x = 0x20;y = *x;return y;

}char z;int main(void){

z = foo();…

}

• What does this program mean?

Page 45: MEMORY ARCHITECTURE

Understanding the memory architecture

char foo() {char y;uint16_t x;x = 0x20;y = *x;return y;

}char z;int main(void){

z = foo();…

}

• What does this program mean?

• uint16_t – unsigned 16bit integer data type

Page 46: MEMORY ARCHITECTURE

Understanding the memory architecture

char foo() {char y;uint16_t x;x = 0x20;y = *x;return y;

}char z;int main(void){

z = foo();…

}

• What does this program mean?

• uint16_t – unsigned 16bit integer data type

• x - 2 bytes, y - 1 byte, both stored on the stack.

• return whatever is stored at 0x20 again

Page 47: MEMORY ARCHITECTURE

Memory usage: Understanding the stack.Find the flaw in this program

int x = 2;

int* foo(int y) { int z; z = y * x; return &z;}

int main(void) { int* result = foo(10); ...}

statically allocated: compiler assigns a memory location.

arguments on the stack

automatic variables on the stack

program counter and copies of all registers on the stack

This program returns a pointer to a variable on the stack. What if another procedure call occurs before the returned pointer is de-referenced?

Page 48: MEMORY ARCHITECTURE

Memory usage: Understanding the stack.Find the flaw in this program

int x = 2;

int* foo(int y) { int z; z = y * x; return &z;}

int main(void) { int* result = foo(10); ...}

• What if the very next instruction reads from the stack?

• Will it work?

Page 49: MEMORY ARCHITECTURE

Memory usage: Understanding the stack.Find the flaw in this program

int x = 2;

int* foo(int y) { int z; z = y * x; return &z;}

int main(void) { int* result = foo(10); ...}

• What if the very next instruction reads from the stack?

• Will it work?• All the time?

Page 50: MEMORY ARCHITECTURE

Memory usage: Understanding the stack.Find the flaw in this program

int x = 2;

int* foo(int y) { int z; z = y * x; return &z;}

int main(void) { int* result = foo(10); ...}

• What if the very next instruction reads from the stack?

• Will it work?• All the time?• What about interrupts?

Page 51: MEMORY ARCHITECTURE

Memory usage: Understanding the stack.Find the flaw in this program

int x = 2;

int* foo(int y) { int z; z = y * x; return &z;}

int main(void) { int* result = foo(10); ...}

• What if the very next instruction reads from the stack?

• Will it work?• All the time?• What about interrupts?

• The first thing an interrupt occurs it pushes a bunch of data on the stack.

• Odds of an interrupt are low.• In testing, we never find the

error.• Now the system goes live on

10 million cars…

Testing is not a reliable technique.

Page 52: MEMORY ARCHITECTURE

Memory usage: Understanding the stack.Find the flaw in this program

int x = 2;

int* foo(int y) { int z; z = y * x; return &z;}

int main(void) { int* result = foo(10); ...}

• How many bytes does x take?

Page 53: MEMORY ARCHITECTURE

Memory usage: Understanding the stack.Find the flaw in this program

int x = 2;

int* foo(int y) { int z; z = y * x; return &z;}

int main(void) { int* result = foo(10); ...}

• How many bytes does x take?

• Ambiguous, have to read the documentation for C and the architecture target.

• Advantage of uint16_t…

Page 54: MEMORY ARCHITECTURE

Memory usage: Understanding the stack.Find the flaw in this program

void foo(uint16_t x) {char y;y = *x;if (x > 0x100) {

foo(x – 1);}

}

char z;void main(…) {

z = 0x10;foo(0x04FF);

}

• What is the value of z?

Page 55: MEMORY ARCHITECTURE

Memory usage: Understanding the stack.Find the flaw in this program

void foo(uint16_t x) {char y;y = *x;if (x > 0x100) {

foo(x – 1);}

}

char z;void main(…) {

z = 0x10;foo(0x04FF);

}

• What is the value of z?• Should just be 0x10? foo() doesn’t

affect z…

Page 56: MEMORY ARCHITECTURE

Memory usage: Understanding the stack.Find the flaw in this program

void foo(uint16_t x) {char y;y = *x;if (x > 0x100) {

foo(x – 1);}

}

char z;void main(…) {

z = 0x10;foo(0x04FF);

}

• What is the value of z?• Should just be 0x10? foo() doesn’t

affect z…• No idea what z will be. How much

of what gets pushed on the stack for each procedure call?

• Some number of recursive calls (0x03FF times)

• Each time more is pushed on the stack.

• If only 1 byte is pushed each time (PC is at least 2 bytes)

• SP will move into the static memory

Page 57: MEMORY ARCHITECTURE

Memory usage: Understanding the stack.Find the flaw in this program

void foo(uint16_t x) {char y;y = *x;if (x > 0x100) {

foo(x – 1);}

}

char z;void main(…) {

z = 0x10;foo(0x04FF);

}

• Will also write all over the I/O registers

• Eventually overwrite PC!

• Will begin executing random code somewhere in the memory!

• Very bad behavior!

Page 58: MEMORY ARCHITECTURE

Memory usage: Understanding the stack.Find the flaw in this program

void foo(uint16_t x) {char y;y = *x;if (x > 0x100) {

foo(x – 1);}

}

char z;void main(…) {

z = 0x10;foo(0x04FF);

}

• Stack overflows into static memory

• Memory hierarchy has consequences for our system design.

• Understand our assumptions, and make correct ones!

Page 59: MEMORY ARCHITECTURE

Embedded Limitations

• PC OS usually allow things like garbage collection.– Why don’t we usually have this in embedded

systems?

Page 60: MEMORY ARCHITECTURE

Heaps•An operating system typically offers a way to dynamically allocate memory on a “heap”.

•Memory management (malloc() and free()) can lead to many problems with embedded systems: Memory leaks (allocated memory is never freed) Memory fragmentation (allocatable pieces get smaller)

•Automatic techniques (“garbage collection”) typically require stopping everything and reorganizing the allocated memory. This is deadly for real-time programs.

Page 61: MEMORY ARCHITECTURE

WRAP UP

Page 62: MEMORY ARCHITECTURE

Assignments

HomeworkProblem 1 from Chapter 8

Read Chapter 9 – Input and Output