640 likes | 753 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.
E N D
MEMORY ARCHITECTURE EEN 417 Fall 2013
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, ADRLoads 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?
Types of memory Stack Caches Scratchpad memories Absolute and relative addresses Virtual memory Heaps allocation/deallocation fragmentation garbage collection Segmented memory spaces … Memory Architecture: Issues 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? • 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 char x; x = 0x20; • Where does x live? • What part of memory?
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.
Understanding the memory architecture char x; x = 0x20; • Where does x live? • What part of memory? • If declared inside a procedure?
Understanding the memory architecture char *x; x = 0x20; • Where does x live? • What part of memory? • How much space will it take?
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
Understanding the memory architecture char *x, y; x = 0x20; y = *x; • What does this program mean?
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.
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
Understanding the memory architecture char *x, y; x = y&; *x = 0x20; • What does this program mean?
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
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?
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?
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
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?
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
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?
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
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
Memory usage: Understanding the stack.Find the flaw in this program statically allocated: compiler assigns a memory location. int x = 2; int* foo(int y) { int z; z = y * x; return &z; } int main(void) { int* result = foo(10); ... } 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?