MEMORY ARCHITECTURE
-
Upload
manoush-jasmine -
Category
Documents
-
view
54 -
download
1
description
Transcript of MEMORY ARCHITECTURE
![Page 1: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/1.jpg)
MEMORY ARCHITECTURE
EEN 417Fall 2013
![Page 2: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/2.jpg)
MEMORY ARCHITECTURES
![Page 3: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/4.jpg)
Thinking about memory
• What would the following instruction do?– LD, R0, 0xFF
• What will the result in R0 be?
![Page 5: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/6.jpg)
An Exception?
• Would be returned by a memory management unit (if one exists)
• Sees the illegal address, and does… what?
![Page 7: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/11.jpg)
What do you think the difference is?
• Register vs. DRAM?
• Register vs. disk?
![Page 12: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/12.jpg)
What do you think the difference is?
• Register vs. DRAM?– 100x slower
• Register vs. disk?– 10,000x slower!!!
![Page 13: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/14.jpg)
ATMega328P• 8-bit microcontroller• Same principles of other
systems
![Page 15: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/15.jpg)
Harvard Architecture• Flash• RAM• EEPROM
![Page 16: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/16.jpg)
Three busesEEPROM shares a bus with other I/O devices
![Page 17: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/17.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/18.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/19.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/20.jpg)
Memory Hierarchy• 32K Flash• 2K SRAM
• 32 GP Registers (8 bits)• 1K EEPROM
![Page 21: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/21.jpg)
Memory Hierarchy• 32K Flash• 2K SRAM
• 32 GP Registers (8 bits)• 1K EEPROM
• Why only 32 registers?
![Page 22: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/25.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/26.jpg)
I/O on Systems
• I/O registers– How do you interact with an accelerometer?
![Page 27: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/28.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/29.jpg)
Understanding the memory architecture
char x;x = 0x20;
• Where does x live?– What part of memory?
![Page 30: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/30.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/31.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/32.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/33.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/34.jpg)
Understanding the memory architecture
char *x, y;x = 0x20;y = *x;
• What does this program mean?
![Page 35: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/35.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/36.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/37.jpg)
Understanding the memory architecture
char *x, y;x = y&;*x = 0x20;
• What does this program mean?
![Page 38: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/38.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/39.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/40.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/41.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/42.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/43.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/44.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/45.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/46.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/47.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/48.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/49.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/50.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/51.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/52.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/53.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/54.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/55.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/56.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/57.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/58.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/59.jpg)
Embedded Limitations
• PC OS usually allow things like garbage collection.– Why don’t we usually have this in embedded
systems?
![Page 60: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/60.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/61.jpg)
WRAP UP
![Page 62: MEMORY ARCHITECTURE](https://reader036.fdocuments.net/reader036/viewer/2022062308/56813231550346895d989626/html5/thumbnails/62.jpg)
Assignments
HomeworkProblem 1 from Chapter 8
Read Chapter 9 – Input and Output