Storing data in Program Memory Special-purpose Load and Store Instructions CS-280 Dr. Mark L. Hornick 1 Atmega32 Memory Address bus (16-bit in Atmega32) Volatile – RAM (fast RW) SRAM (temp data store) DRAM CS-280 Dr. Mark L. Hornick A unique 16-bit address references each memory byte. Data bus (8-bit) Nonvolatile – ROM (fast R – slow W) ROM PROM EPROM EEPROM (permanent data store) 2 Flash ROM (program store) SRAM is volatile memory Power must be supplied to maintain values stored in SRAM SRAM values disappear when power is shut off SRAM is cleared when power is first applied CS-280 Dr. Mark L. Hornick 3 Review: Program vs. Data Memory Addressing Program Memory is organized and accessed in words A word is 2 bytes Each word has a unique address Data Memory is organized and accessed in bytes Each byte has a unique address CS-280 Dr. Mark L. Hornick 0x0000 Byte 0 Byte 1 0x0001 Byte 2 Byte 3 0x0002 Byte 4 Byte 5 0x0003 Byte 6 Byte 7 0x0004 Byte 8 Byte 9 0x0060 Byte 0 0x0061 Byte 1 0x0062 Byte 2 0x0063 Byte 3 0x0064 Byte 4 4 Review: Allocating SRAM Data memory .DSEG ; subsequent directives refer to data segment .ORG SRAM_START ; address where Data Memory starts (0x60) x1: .byte 1 ; reserve 1 byte of SRAM, assign label x1=0x60 x2: .byte 2 ; reserve 2 bytes of SRAM, assign label x2=0x61 x3: .byte 1 ; reserve 1 byte of SRAM, assign label x3=0x63 .CSEG ; switch further directives to code segment .ORG 0x2A ; set addr for start of program instructions LDS R20, x1 ; load value at data addr specified by x1 STS x2, R20 ; store value in R20 to data addr x2 The addresses are assigned automatically by the Assembler The .byte n directive tells the assembler to allocation n bytes Be very careful about using .ORG for addresses < 0x60 CS-280 Dr. Mark L. Hornick 5 Review: The X, Y, and Z Registers The X, Y, and Z 16-bit registers overlap the last six 8-bit registers R26 through R31 CS-280 Dr. Mark L. Hornick 6 Review: Indirect Addressing Accesses a 8-bit (byte) value from SRAM data memory at the address specified indirectly via the 16-bit X, Y, or Z index-registers Example: LD R20, X ; load value at data addr held in X to R20 ST Y, R20 ; store value in R20 to data addr held in Y Note X, Y, Z hold data address values that are 16-bits CS-280 Dr. Mark L. Hornick 7 Data values can be stored permanently by allocating them to non-volatile Program Memory .DSEG ; subsequent directives refer to data segment .ORG SRAM_START ; address where Data Memory starts (0x60) x1: .byte 5 ; reserve 5 bytes of (uninitialized) volatile SRAM .CSEG ; switch further directives to code segment .ORG 0x2A ; set addr for start of program instructions y1: .db 1,5 ; alloc 2 bytes in Prog Memory with initial values of 1 and 5 title: .db ‘c’,’s’,’2’,’8’,’0’,0 ; allocate 6 bytes in Program Memory course: .db “CS-280”, 0 ; allocate 7 bytes in Program Memory Note assembler does NOT automatically insert a NULL char at the end of the string The .byte n directive tells the assembler to allocate n bytes in Data Memory No initial value can be specified to SRAM data memory values The .db n,m,… (“define byte”) directive tells the assembler to allocate and store the bytes n,m… in Program Memory The initial values of the memory are specified The assembler always starts data on a word boundary in memory CS-280 Dr. Mark L. Hornick 8 Review: ASCII Encoding 9 O 70 CS-280 Dr. Mark L. Hornick For example, character 'O' is 79 (row value 70 + col value 9 = 79). 9 Accessing (reading) data from Program Memory There are three instructions to load (read) data from Program Memory to a register LPM loads from byte address in Z register to R0, where Z and R0 are implied arguments LPM Rn, Z loads from byte address in Z to Rn (R0-R31); note that X and Y are not allowed LPM Rn, Z+ Same as above, but increments Z after loading CS-280 Dr. Mark L. Hornick 10 Big Red Flag When using LPM, the Z register must contain the data’s byte address Recall: Program Memory is organized by words – each word has an address Byte address is 2*word address CS-280 Dr. Mark L. Hornick 11 How do you load a byte address value into Z? .CSEG .ORG LDI LDI LPM ; switch further directives to code segment 0x2A ; set addr for start of program instructions ZL, LOW(2*values) ; load ZL with the low 8 bits of the address ZH, HIGH(2*values) ; load ZH with the high 8 bits of the address R20, Z ; load Program Memory byte addr contained in Z values: .db 5, 10, 11, 12, 13 ; define 5 bytes of Program Memory Use LOW() and HIGH() are Assembler functions “values” label contains the word address of the location of the first reserved byte The assembler expression “2*values” correctly computes the associated byte address CS-280 Dr. Mark L. Hornick 12 There are certain conventions for defining Program Memory The .db directives are placed after program instructions, so that the data itself is placed in memory after the instruction opcodes When defining numeric data values, the first value specifies the number of values to follow: y1: .db 1,5 ; alloc 2 bytes in Prog Memory with initial values of 1 and 5 When defining character or string data values, the last value speicified should always be zero, indicating that the character data is terminated with a null-character: title: course: .db .db ‘c’,’s’,’2’,’8’,’0’,0 “CS-280”, 0 ; allocate 6 bytes in Program Memory ; allocate 7 bytes in Program Memory CS-280 Dr. Mark L. Hornick 13 Note: The Z register can also be incremented explicitly ADIW ZH:ZL, 5 ; add immediate value (to Z) The value may be 0-63 (decimal) So you can use ADIW in conjuction with LPM and LPM Rn, Z instructions to post-increment the Z register to point to the address of the next character in program memory CS-280 Dr. Mark L. Hornick 14 There is also an SPM instruction…. But due to the nature of Flash memory, writing to it is more complex Before writing, an entire page of memory must be erased Page size is device-specific On some devices, only entire pages can be written In CE2800, use the .DB directive to write to Flash memory Only writes once, when the program is downloaded to the device CS-280 Dr. Mark L. Hornick 15