New code: (needs V6 or later)
// If this is not a seperate file, you must end the main() function first
// Just a quick test to ensure it's working
CREATEBANK(0, 128)
POKELONG(0, 0, 0x01234567)
POKEBYTE(0, 4, 0x70)
POKELONG(0, 5, 0xffff00ff)
LOCAL a%, b%, c%
a = PEEKLONG(0, 0)
b = PEEKBYTE(0, 4)
c = PEEKLONG(0, 5)
IF a=0x01234567 AND b=0x70 AND c=0xffff00ff
DEBUG "ok, works!"
ENDIF
FUNCTION _foo:
ENDFUNCTION
// Global variable for storing the bank pointers
INLINE
DGArray<unsigned char*> gMemBanks;
class MemBank_cleaner
{
public:
~MemBank_cleaner()
{
for(int i=0; i<(int)LEN(gMemBanks); ++i)
FREEBANK(i);
}
} gMemBankClean;
ENDINLINE
FUNCTION CREATEBANK: index%, memsz%
INLINE
if(LEN(gMemBanks) <= index)
REDIM(gMemBanks, index+1);
gMemBanks(index) = new unsigned char[memsz];
ENDINLINE
ENDFUNCTION
FUNCTION POKEBYTE: ibank%, position%, value%
INLINE
gMemBanks(ibank)[position] = (unsigned char)value;
ENDINLINE
ENDFUNCTION
FUNCTION PEEKBYTE%: ibank%, position%
INLINE
return (int)gMemBanks(ibank)[position];
ENDINLINE
ENDFUNCTION
FUNCTION POKELONG: ibank%, position%, value%
INLINE
unsigned char* pBytes = &gMemBanks(ibank)[position];
*((int*)pBytes) = value;
ENDINLINE
ENDFUNCTION
FUNCTION PEEKLONG%: ibank%, position%
INLINE
unsigned char* pBytes = &gMemBanks(ibank)[position];
return *((int*)pBytes);
ENDINLINE
ENDFUNCTION
FUNCTION FREEBANK: ibank%
INLINE
if(gMemBanks(ibank))
{
delete[] gMemBanks(ibank);
gMemBanks(ibank) = 0L;
}
ENDINLINE
ENDFUNCTION
FUNCTION WRITEBANK: ibank%, ifile%, ifrom%, ilen%
FOR i% = ifrom TO ifrom+ilen-1
WRITEBYTE ifile%, PEEKBYTE(ibank%, i%)
NEXT
ENDFUNCTION
FUNCTION READBANK: ibank%, ifile%, ifrom%, ilen%
LOCAL val%
FOR i% = ifrom TO ifrom+ilen-1
READBYTE ifile%, val%
POKEBYTE(ibank%, i%, val%)
NEXT
ENDFUNCTION
Old code:
// If this is not a seperate file, you must end the main() function first
FUNCTION _foo:
ENDFUNCTION
// Global variable for storing the bank pointers
INLINE
DGArray<unsigned char*> gMemBanks;
class MemBank_cleaner
{
public:
~MemBank_cleaner()
{
for(int i=0; i<(int)LEN(gMemBanks); ++i)
FREEBANK(i);
}
} gMemBankClean;
ENDINLINE
FUNCTION CREATEBANK: index, memsz
INLINE
if(LEN(gMemBanks) <= index)
REDIM(gMemBanks, index+1);
gMemBanks(index) = new unsigned char[(int)memsz];
ENDINLINE
ENDFUNCTION
FUNCTION POKEBYTE: ibank, position, value
INLINE
gMemBanks(ibank)[(int)position] = (unsigned char)value;
ENDINLINE
ENDFUNCTION
FUNCTION PEEKBYTE: ibank, position
INLINE
return (DGInt)gMemBanks(ibank)[(int)position];
ENDINLINE
ENDFUNCTION
FUNCTION FREEBANK: ibank
INLINE
if(gMemBanks(ibank))
{
delete[] gMemBanks(ibank);
gMemBanks(ibank) = 0L;
}
ENDINLINE
ENDFUNCTION
(untested)