DIM
Contents |
Action
Dimension a variable.
Syntax
DIM var AS [XRAM/SRAM/ERAM]type [AT location/variable] [OVERLAY]
Remarks
Var |
Any valid variable name such as b1, i or longname. var can also be an array : ar(10) for example.
|
Type |
Bit, Byte, Word, Integer, Long, Dword, Single, Double or String |
XRAM |
Specify XRAM to store variable into external memory |
SRAM |
Specify SRAM to store variable into internal memory (default) |
ERAM |
Specify ERAM to store the variable into EEPROM |
OVERLAY |
Specify that the variable is overlaid in memory. |
location |
The address of name of the variable when OVERLAY is used. |
A string variable needs an additional length parameter:
Dim s As XRAM String * 10
In this case, the string can have a maximum length of 10 characters. Internally one additional byte is needed to store the end of string marker. Thus in the example above, 11 bytes will be used to store the string.
Note that BITS can only be stored in internal memory.
You may also specify IRAM. IRAM is the place in memory where the registers are located : absolute address 0 - 31. BASCOM uses most of these addresses, depending on the instructions/options you use. For a $TINY chip it makes sense to use IRAM since there is NO SRAM in most tiny AVR chips (TINY15 for example). You may also use to IRAM to overlay registers in memory.
See also Memory usage
SCOPE
The scope for DIM is global. So no matter where you use the DIM statements, the variable will end up as a global visible variable that is visible in all modules, procedures and functions.
When you need a LOCAL variable that is local to the procedure or function, you can use LOCAL.
Since LOCAL variables are stored on the frame, it takes more code to dynamic generate and clean up these variables.
AT
The optional AT parameter lets you specify where in memory the variable must be stored. When the memory location already is occupied, the first free memory location will be used. You need to look in the report file to see where the variable is located in memory.
OVERLAY
The OVERLAY option will not use any variable space. It will create a sort of phantom variable.
Dim x as Long at $60 'long uses 60,61,62 and 63 hex of SRAM
Dim B1 As Byte At $60 Overlay '$60 is the same as &H60
Dim B2 As Byte At $61 Overlay
B1 and B2 are no real variables! They refer to a place in memory. In this case to &H60 and &H61. By assigning the phantom variable B1, you will write to memory location &H60 that is used by variable X.
So to define it better, OVERLAY does create a normal usable variable, but it will be stored at the specified memory location which could be already be occupied by another OVERLAY variable, or by a normal variable.
Take care with the OVERLAY option. Use it only when you understand it.
You can also read the content of B1:
Print B1
This will print the content of memory location &H60.
By using a phantom variable you can manipulate the individual bytes of real variables.
Overlay example 2
Dim L as Long at &H60
Dim W as Word at &H62 OVERLAY
W will now point to the upper two bytes of the long.
Overlay example 3
Following you find the Bascom-AVR Simulator Memory status when you run the following example in Bascom-AVR Simulator. This example is intended to be used with the simulator. You need to uncomment the $sim when you want to test it on an real AVR.
Strings need an additional byte (Null termination). So you need an overlay of 8 bytes when you overlay a string with 7 bytes.
Using variable name instead of address
As variables can be moved though the program during development it is not always convenient to specify an address. You can also use the name of the variable :
DIM W as WORD
Dim B as BYTE AT W OVERLAY
Now B is located at the same address as variable W.
For XRAM variables, you need additional hardware : an external RAM and address decoder chip.
ERAM
For ERAM variables, it is important to understand that these are not normal variables. ERAM variables serve as a way to simple read and write the EEPROM memory. You can use READEEPROM and WRITEEEPROM for that purpose too.
To write to an ERAM variable you have to use an SRAM variable as the source : eramVAR= sramVAR
To read from an ERAM variable you have to use an SRAM variable as the targer : sramVAR=eramVAR
Both variables need to be of the same data type. So when writing to an ERAM double, the source variable need to be of the double type too.
ERAM can be assigned with a numeric value too : eramVAR= 123
You can not use an ERAM variable as you would use a normal variable.
Also keep in mind that when you write to ERAM, you write to EEPROM, and that after 100.000 times, the EEPROM will not erase properly.
Dim b as byte, bx as ERAM byte
B= 1
Bx=b ' write to EEPROM
B=bx ' read from EEPROM
Xmega
The XMEGA need an additional configuration command : CONFIG EEPROM = MAPPED, in order to use ERAM.
Size
The maximum size of an array depends on the available memory and the data type. The XMEGA supports up to 8 MB of external memory. BASCOM supports this but the implementation is still considered BETA. It should not be used for production. The only thing you need to do to activate the big memory is to specify the size with $XRAMSIZE.
For example : $XRAMSIZE=8000000 will tell the compiler that you use 8 MB of external memory.
Additional registers must be set to pass the 24 bit address. This will create more code.
There is only one restriction : you can/may not pass variables located in the external memory to a sub or function.
The compiler will always pass a word address and does not support to pass the additional byte.
See Also
CONST , LOCAL , Memory usage
Example