95 of 208
ZBasic Language Reference 87 ZX Microcontroller Family routine. This works because integral-byte aliases are required to be byte aligned. Note that when passed by value as a parameter, a sub-byte type parameter occupies an entire byte on the stack. Special type conversion functions, CBit() and CNibble() are provided to facilitate the use of these types in combination with other types. See the ZBasic System Library Reference Manual for more information on the conversion functions. In Section 3.20 Aliases, it was mentioned that there may an issue with aliases of a fundamental type overlaying sub-byte types. The issue arises because fundamental data types must be byte-aligned and a particular Bit or Nibble variable may or may not be byte-aligned. In most cases it will be simpler to define a sub-byte alias to overlay variables of fundamental type. Doing the converse may require some experimentation to achieve the required byte-alignment. The alias defined below may or may not be accepted by the compiler depending on what other sub-byte types are defined at the same scope level and the order in which the are defined. Dim ba(1 to 20) as Bit Dim bval as Byte Alias ba(3) Internally, the compiler collects together all of the sub-byte types at a given scope level (module, procedure, block) and allocates space for them collectively. The Nibble variables are allocated first in the order that they are defined followed by the Bit variables in the order that they are defined. The map file will show a special variable with a name like @BitNib00. This is the host variable that contains the individual Bit and Nibble values for a particular scope level. BasicX Compatibility Note Bit and Nibble data types are not available in BasicX compatibility mode. 3.24.1 Forcing Byte Alignment As described earlier, sub-byte type variables are normally sub-allocated within a host variable. One consequence of this space-saving strategy is that a sub-byte type variable is often not aligned on a byte boundary, a circumstance that imposes limitations on the possible uses of the variable. One solution to this problem is to instruct the compiler to align a particular variable on a byte boundary. This is done by adding the ByteAlign attribute to the variable definition as shown below. Dim ByteAlign ba(1 to 20) as Bit Each variable that is so defined will occupy an integral number of bytes of memory. For the example above, three bytes will be allocated even though only 20 of the 24 available bits will actually be used. Note that the ByteAlign attribute may be used with any variable type but it has no effect for those types that are inherently byte-aligned. Also, the ByteAlign attribute may be used in a Structure to force sub- byte types to be byte-aligned. 3.25 Structures It is often useful to define a data type that is a collection of data elements. For example, if you write a program to manipulate dates it may be useful to define a data type that contains “year”, “month” and “day” elements. This is convenient because it allows you to think about or refer to the group of data elements as a single entity rather than as the individual constituent elements. In ZBasic, as in many other programming languages, such a collection of data items is referred to as a “structure”. A structure is a user-defined data type similar in some respects to an enumeration. As with an enumeration, you define a structure by specifying the consituent elements. The syntax for defining a structure at the module level is: