dkinzer Site Admin
Joined: 03 Sep 2005
Location: Portland, OR
|
|
Posted: 05 July 2008, 15:31 PM Post subject: Re: Writing an Array to Program Memory??? |
|
|
| edkelly wrote: | | I'm wanting to write an array and some variables to program (persistent) memory [...]. | Before addressing the specifics, it is important to understand that Persistent Memory and Program Memory are different memory areas and they have different characteristics. Unlike RAM, both Persistent Memory and Program Memory have write cycle limits. When you exceed the stated number of writes, data retention may become unreliable.
Persistent Memory is implemented using the internal EEPROM on the CPU chip. Persistent Memory is divided into two regions: the system area and the user area. The system area is the first 32 or 64 bytes of Persistent Memory (beginning at address zero). The user area begins at address 32 or 64 and, depending on the ZX device, contains between 996 bytes and 4064 bytes. The write cycle limit of Persistent Memory is 100,000 writes.
Program Memory is the memory area where your program's code is stored along with Program Memory data items defined by your program. Depending on the ZX model, Program Memory is implemented either in the CPU chip's internal Flash Memory (ZX-1281, ZX-1280, all native mode devices) or in an external serial EEPROM (all VM mode devices except the ZX-1281 and ZX-1280). Program Memory begins at address zero and the size varies from 32K (ZX-24a, ZX-24p) up to 124K (ZX-1281n, ZX-1280n). The write cycle limit for external EEPROM Program Memory is 1,000,000 cycles and it is 10,000 for internal Flash Program Memory.
Whether using Persistent Memory or Program Memory for data storage, you have two choices with regard to address allocation. The simplest method is to define a variable in the desired memory space and let the compiler assign the address. The more complicated alternative is to use the left over (unassigned) space and manage the address allocation yourself.
If you define the Persistent Memory or Program Memory variables, the compiler will automatically assign an address to the defined variable and you can read/write it the same way that you do RAM-based variables (with the exception that you can't pass them by reference to a subroutine/function). Here is a code snippet that defines both a Persistent Memory variable and a Program Memory variable. | Code: |
Dim myPersVar as Persistent Integer
Dim myProgVar as ProgMem Single
myPersVar = 23
myProgVar = 6.02e23
Debug.Print Fmt(myProgVar, 2)
Debug.Print myPersVar |
You can define arrays, structures, arrays of structures, etc. in either Persistent Memory or Program Memory using the same syntax as for RAM-based data items. The key is to add the attribute Persistent or ProgMem before the data type.
If you need the address of either of these types of variables (or any variable, for that matter) you can use the DataAddress property. | Code: | | addr = myProgVar.DataAddress |
If you wish, you can define a large Persistent or ProgMem variable (array) and then read/write parts of it manually using GetPersistent()/PutPersistent() or GetProgMem()/PutProgMem(). You would get the variable's address using the .DataAddress property and then add some offset to that address to read/write the portion of interest.
If you decide to manage the address assignment yourself and use GetPersistent()/PutPersistent() or GetProgMem()/PutProgMem() to read/write it, you can use the space beyond that which is automatically assigned by the compiler. To do so, you need to know the address of the first available byte. The code snippet below shows how to get these addresses. | Code: |
Dim persAddr as UnsignedInteger
persAddr = Register.PersistentStart + Register.PersistentUsed
Debug.Print "persAddr = ";persAddr
Dim progAddr as Long
progAddr = Register.CodeSize
Debug.Print "progAddr = ";progAddr |
|
|