External Memory HOWTO
When your projects get bigger, the internal RAM of the P89V664 is no longer sufficient. External RAM can be used to overcome this limitation. The P89V664 used in the ZKit-51 has 2KB of on-chip external RAM.
The external RAM can be used from SDCC, in two ways
-
By using the storage class
__xdata
. -
By using the large memory model.
Large Memory Model
By default the compiler uses the small memory model, where in the
global variables, local variables are allocated in internal RAM. The
compiler can be instructed to use the large memory model. In the large
memory model all global variables and local variables are allocated in
external RAM. The large memory model is specified by pass
--large-model
to the sdcc
command.
__xdata Storage Class
In the small memory model, specific variables can be put in external
RAM, using the __xdata
storage class. An an array can be placed in
external RAM, using the following code.
__xdata int a[10];
Enabling External RAM
By default the on-chip external RAM is disabled in the P89V664. To
enable it the EXTRAM
bit in the AUX
(0x8E
) register has to be
set to 0
. Please note that there is a bug in the P89V664 datasheet
which indicates that the EXTRAM
bit should be set to 1
to enable
on-chip external RAM.
Enabling of the external RAM should be done even before main()
is
invoked. This is because global variable initialization is done before
invoking main()
. And external RAM has to enabled so that global
variables placed in external RAM are properly intialized.
The SDCC startup code invokes a function named
_sdcc_external_startup()
before it initializes global variables. The
user can implement this function and enable the on-chip external RAM
within it. An implementation of _sdcc_external_startup()
for the
P89V664 processor is given below.
int _sdcc_external_startup()
{
__sfr __at (0x8E) AUXR;
AUXR = 0x0;
return 0;
}
The return value of the function has a specific meaning. If the function returns zero, global variables are initialized next. If the function returns a non-zero value, global variables are not initialized.