Page 1 of 1

File load as const byte array

Posted: 29 Mar 2010 09:16
by peterverkaik
Hi,

I'd like to load a file as const byte array.

eg.
const myArray as byte[] = "drive:\path\filename.ext"

The compiler would get the size for the array from the filesize
and would load the file contents in the asm file.

As an alternative
const myArray as byte[nn] = "drive:\path\filename.ext"
where I set nn. If nn is smaller than the filesize than only nn bytes are
read from the file. If nn > filesize than the array is padded with zeroes.

This is a fast way to include large data arrays.


regards peter

Re: File load as const byte array

Posted: 30 Mar 2010 14:35
by anikolic
Hi,
Very unorthodox approach, I must admit, but worth of consideration. Probably will need some additional directives that will tell the compiler how to treat your path to the file. I will pass your suggestions to our developers for further consideration, and I will inform you of their decision on this forum topic.

Thank you.

Best regards,
Aleksandar

Re: File load as const byte array

Posted: 30 Mar 2010 16:27
by peterverkaik
Thanks for considering it.

I noticed it is not possible (yet) to fill a const byte array from a string.
So a few more thoughts.

const myArray as byte[nn] = "This text is written as char array\r\n\0"
This would write myArray as if it were specified as
const myArray as byte[nn] = ("T","h","i", .... 13,10,0)

Maybe using specifiers to allow compact storage as well

const myArray as compact byte[nn] = "This text is written as char array\r\n\0"
This will store the array using all 3 bytes of romaddresses.

For a file you could then use

const myArray as file byte[nn] = "drive:\path\filename.ext"
This stores the filecontents using 2 bytes of every romaddress

const myArray as compact file byte[nn] = "drive:\path\filename.ext"
This stores the filecontents using 3 bytes of every romaddress

It would be nice if the nn is optional.

regards peter

Re: File load as const byte array

Posted: 31 Mar 2010 10:09
by anikolic
Hello,
Regarding your suggestions for introducing the initialization of constants using external files, I have generated and enhancement report, and once again wish to thank you for your ideas. Personally, I find them very convenient, with great practical benefits. It is however, all up to our developers to determine if they are able to implement this within the existing framework. I will inform you when more information comes along.

Regarding the other problem, I must say that we have finally implemented by-the-book definitions of Basic strong data types, so your assignment of a string to a byte array, simply cannot be done within these set of rules. In order to do so, you may implement array of char, and then use some typecasting to byte, if necessary. So:

Code: Select all

'const myArray as byte[40] = "This text is written as char array\r\n\0"  'This is invalid expression
const myArray as char[40] = "This text is written as char array\r\n\0" ' This is the only possible alternative
As you may know, byte arrays are not needed to be ended with a NULL characters, but char arrays are treated as strings, so compiler requires that they contain an ending NULL character. Using this information, you may deduce that you are allowed to assign a byte array to char array, but not vice-verse. This applies also to pointers to those arrays. Pointer to byte array can be assigned to pointer to char array, but not the other way around.

Best regards,
Aleksandar

Re: File load as const byte array

Posted: 31 Mar 2010 10:33
by peterverkaik
Thanks for the tip on using char arrays!
Saves me alot of typing.

Since a string can only be applied to char arrays,
the file modifier is not necessary because a string
applied to a byte array would indicate a path/filename
Nice.

regards peter

Re: File load as const byte array

Posted: 04 Apr 2010 13:45
by nervous
peterverkaik wrote:Hi,

I'd like to load a file as const byte array.

eg.
const myArray as byte[] = "drive:\path\filename.ext"

The compiler would get the size for the array from the filesize
and would load the file contents in the asm file.

As an alternative
const myArray as byte[nn] = "drive:\path\filename.ext"
where I set nn. If nn is smaller than the filesize than only nn bytes are
read from the file. If nn > filesize than the array is padded with zeroes.

This is a fast way to include large data arrays.


regards peter

Uhmmm Isn't more clever just include a file with the definition of the array ?

Something like ...

Code: Select all


include "constant.mbas"

that need just the definition of your const inside the file included.... Seems to me the easy way

Re: File load as const byte array

Posted: 04 Apr 2010 14:24
by peterverkaik
Imagine you have a toolchain that creates an imagefile of whatever.
Allowing to simply point to the imagefile saves us the burden to
convert the bytes to hexvalues and place these values in a const array
everytime the image changes. Think having a bootloader that runs a program
that scannes or interpretes the imagefile.

regards peter