Lecture 7 - Memory Architecture - PERFORM Alumni

hungryhorsecabinSoftware and s/w Development

Dec 14, 2013 (3 years and 7 months ago)

81 views

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 <ADR> to named register

Thinking about memory


What would the following instruction do?


LD, R0, 0xFF



What will the result in R0 be?

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

An Exception?


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


Sees the illegal address, and does… what?

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?

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?

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?

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.

What do you think the difference
is?



Register vs. DRAM?



Register vs. disk?

What do you think the difference
is?



Register vs. DRAM?


100x slower


Register vs. disk?


10,000x slower!!!

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.

ATMega328P


8
-
bit microcontroller


Same principles of other
systems


Harvard Architecture


Flash


RAM


EEPROM

Three buses

EEPROM shares a bus with other I/O devices

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.

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?

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!

Memory Hierarchy


32K Flash


2K SRAM


32 GP Registers (8 bits)


1K EEPROM

Memory Hierarchy


32K Flash


2K SRAM


32 GP Registers (8 bits)


1K EEPROM



Why only 32 registers?

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?

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?

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

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


I/O on Systems


I/O registers


How do you interact with an accelerometer?

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

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).

Understanding the memory
architecture

c
har x;

x = 0x20;


Where does x live?


What part of memory?

Understanding the memory
architecture

c
har 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.

Understanding the memory
architecture

c
har x;

x = 0x20;


Where does x live?


What part of memory?


If declared inside a
procedure?

Understanding the memory
architecture

c
har *x;

x = 0x20;


Where does x live?


What part of memory?


How much space will it
take?

Understanding the memory
architecture

c
har *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

Understanding the memory
architecture

c
har *x, y;

x = 0x20;

y = *x;


What does this program
mean?

Understanding the memory
architecture

c
har *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.

Understanding the memory
architecture

c
har *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

Understanding the memory
architecture

c
har *x, y;

x = y&;

*x = 0x20;


What does this program
mean?

Understanding the memory
architecture

c
har *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

Understanding the memory
architecture

c
har foo() {


char *x, y;


x = 0x20;


y = *x;


return y;

}

char z;

i
nt

main(void){


z = foo();




}


What does this program
mean?


Where are x, y, and z in
memory?

Understanding the memory
architecture

c
har foo() {


char *x, y;


x = 0x20;


y = *x;


return y;

}

char z;

i
nt

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?

Understanding the memory
architecture

c
har foo() {


char *x, y;


x = 0x20;


y = *x;


return y;

}

char z;

i
nt

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

Understanding the memory
architecture

c
har foo() {


char *x, y;


x = 0x20;


y = *x;


return y;

}

char z;

i
nt

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?

Understanding the memory
architecture

c
har foo() {


char *x, y;


x = 0x20;


y = *x;


return y;

}

char z;

i
nt

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

Understanding the memory
architecture

c
har foo() {


char y;


uint16_t x;


x = 0x20;


y = *x;


return y;

}

char z;

i
nt

main(void){


z = foo();




}


What does this program
mean?

Understanding the memory
architecture

c
har foo() {


char y;


uint16_t x;


x = 0x20;


y = *x;


return y;

}

char z;

i
nt

main(void){


z = foo();




}


What does this program
mean?


uint16_t


unsigned
16bit integer data type

Understanding the memory
architecture

c
har foo() {


char y;


uint16_t x;


x = 0x20;


y = *x;


return y;

}

char z;

i
nt

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.


r
eturn whatever is
stored at 0x20 again

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?

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?

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?

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?

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.

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?

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…

Memory usage: Understanding the stack.

Find the flaw in this program

v
oid foo(uint16_t x) {


char y;


y = *x;


if (x > 0x100) {



foo(x


1);


}

}


c
har z;

void main(…) {


z = 0x10;


foo(0x04FF);

}


What is the value of z?

Memory usage: Understanding the stack.

Find the flaw in this program

v
oid foo(uint16_t x) {


char y;


y = *x;


if (x > 0x100) {



foo(x


1);


}

}


c
har z;

void main(…) {


z = 0x10;


foo(0x04FF);

}


What is the value of z?


Should just be 0x10? foo() doesn’t
affect z…

Memory usage: Understanding the stack.

Find the flaw in this program

v
oid foo(uint16_t x) {


char y;


y = *x;


if (x > 0x100) {



foo(x


1);


}

}


c
har 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

Memory usage: Understanding the stack.

Find the flaw in this program

v
oid foo(uint16_t x) {


char y;


y = *x;


if (x > 0x100) {



foo(x


1);


}

}


c
har 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!

Memory usage: Understanding the stack.

Find the flaw in this program

v
oid foo(uint16_t x) {


char y;


y = *x;


if (x > 0x100) {



foo(x


1);


}

}


c
har 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!

Embedded Limitations


PC OS usually allow things like garbage
collection.


Why don’t we usually have this in embedded
systems?

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.

WRAP UP

Assignments


Homework


Problem
1 from
Chapter 8



Read Chapter 9


Input and Output