File load as const byte array

Post your requests and ideas on the future development of mikroBasic PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
peterverkaik
Posts: 174
Joined: 31 Aug 2009 22:44

File load as const byte array

#1 Post by peterverkaik » 29 Mar 2010 09:16

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

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

Re: File load as const byte array

#2 Post by anikolic » 30 Mar 2010 14:35

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
Web Department Manager

peterverkaik
Posts: 174
Joined: 31 Aug 2009 22:44

Re: File load as const byte array

#3 Post by peterverkaik » 30 Mar 2010 16:27

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

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

Re: File load as const byte array

#4 Post by anikolic » 31 Mar 2010 10:09

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
Web Department Manager

peterverkaik
Posts: 174
Joined: 31 Aug 2009 22:44

Re: File load as const byte array

#5 Post by peterverkaik » 31 Mar 2010 10:33

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

nervous
Posts: 80
Joined: 13 Feb 2007 08:35
Location: Italy

Re: File load as const byte array

#6 Post by nervous » 04 Apr 2010 13:45

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
Cheers
Nervous

peterverkaik
Posts: 174
Joined: 31 Aug 2009 22:44

Re: File load as const byte array

#7 Post by peterverkaik » 04 Apr 2010 14:24

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

Post Reply

Return to “mikroBasic PRO for dsPIC30/33 and PIC24 Wish List”