Positron Compiler Documentation

Cdata

Source: Positron16 Compiler User Manual, PDF page 381

Syntax

Cdata { alphanumeric data }

Overview

Place information directly into memory for access by Cread8, Cread16, Cread32 and Cread64.

Parameters

alphanumeric data can be any value, alphabetic character, or string enclosed in quotes (") or numeric data without quotes.

Example

  Device = 24FJ64GA002
                                        ' Select the device to compile for
  Declare Xtal = 8
  Dim MyByte as Byte
  Dim bIndex as Byte
  RPOR7 = 3
                                        ' Make PPS Pin RP14 U1TX
  For bIndex = 0 to 11
                                        ' Create a loop of 12
     MyByte = Cread8 MyLabel[bIndex]  ' Read memory location MyLabel + bIndex
     Hrsout MyByte
                                        ' Display the value read
  Next
  Stop
MyLabel:
  Cdata "Hello World\r"
                              ' Create a string of text in code memory

The program above reads and displays 12 values from the address located by the Label accompanying the Cdata command. Resulting in "Hello World" being displayed.

Formatting a Cdata table.

Sometimes it is necessary to create a data table with a known format for its values. For example all values will occupy 4 bytes of data space even though the value itself would only occupy 1 or 2 bytes.

Cdata 100000, 10000, 1000, 100, 10, 1

The above line of code would produce an uneven code space usage, as each value requires a different amount of code space to hold the values. 100000 would require 4 bytes of code space, 10000 and 1000 would require 2 bytes, but 100, 10, and 1 would only require 1 byte.

Reading these values using one of the Cread commands would cause problems because there is no way of knowing the amount of bytes to read in order to increment to the next valid value.

The answer is to use formatters to ensure that a value occupies a predetermined amount of bytes. These are: -

Byte

Word

Dword

Float

Double

Placing one of these formatters at the beginning of the table will force a given length.

Cdata  As Dword 100000, 10000, 1000, 100, 10, 1

Byte will force all values in the table to occupy one byte of code space, regardless of its value. Any values above 255 will be truncated to the least significant byte.

Word will force the values in the table to occupy 2 bytes of code space, regardless of its value. Any values above 65535 will be truncated to the two least significant bytes. Any value below 255 will be padded to bring the memory count to 2 bytes.

Dword will force the values in the table to occupy 4 bytes of code space, regardless of its value. Any value below 65535 will be padded to bring the memory count to 4 bytes. The line of code shown above uses the Dword formatter to ensure all the values in the Cdata table occupy 4 bytes of code space.

Float will force the values in the table to their floating point equivalent, which always takes up 4 bytes of code space.

The example below illustrates the formatters in use.

      ' Convert a Dword value into a string. Using only BASIC commands
      ' Similar principle to the Str$ command
         Device = 24FJ64GA002
                                       ' Select the device to compile for
         Declare Xtal = 8
         Dim Pow10 as Dword
                                       ' Power of 10 variable
         Dim bCount as Byte
         Dim bTableIndex as Byte
         Dim dValue as Dword
                                       ' Value to convert
         Dim MyString as String * 12 ' Holds the converted value
         Dim bIndex as Byte
                                       ' Index within the string
         RPOR7 = 3                    ' Make PPS Pin RP14 U1TX
         DelayMs 10
                                       ' Wait for things to stabilise
         Clear
                                       ' Clear all RAM before we start
         Value = 1234576
                                       ' Value to convert
         Gosub DwordToStr
                                       ' Convert Value to string
         Hrsout MyString
                                       ' Display the result
         Stop
      ' Convert a Dword value into a string array
      ' Value to convert is placed in 'Value'
      ' Byte array 'Array1' is built up with the ASCII equivalent
      '
      DwordToStr:
         bIndex = 0
         bTableIndex = 0
         Repeat
           Pow10 = Cread32 Dword_Table[bTableIndex]
           bCount = 0
           While dValue >= Pow10
              dValue = dValue - Pow10
              Inc bCount
           Wend
           If bCount <> 0 Then
              MyString [bIndex] = bCount + "0"
              Inc bIndex
           EndIf
           Inc bTableIndex
         Until bTableIndex > 8
  MyString[bIndex] = dValue + "0"
  Inc bIndex
  MyString[bIndex] = 0
                                ' Add the null to terminate the string
  Return
'
' Cdata table is formatted for all 32-bit values.
' Which means each value will require 4 bytes of code space
Dword_Table:
  Cdata as Dword 1000000000, 100000000, 10000000, 1000000, 100000,_
                  10000, 1000, 100, 10

Label names as pointers.

If a label's name is used in the list of values in a Cdata table, the labels address will be used. This is useful for accessing other tables of data using their address from a lookup table. See example below.

' Transmit serially text from two code memory tables
' Based on their address located in a separate table
    Device = 24FJ64GA002
                                     ' Select the device to compile for
    Declare Xtal = 16
    Declare Hserial_Baud = 9600      ' USART1 Baud rate
    Declare Hrsout1_Pin = PORTB.14  ' Select the pin used for TX with USART1
     Dim MyByte As Byte
     Dim MyString1 As Code = "Hello",0
     Dim MyString2 As Code = "World",0
     Dim wAddress As Word
'
' Table of address's
'
    Dim AddrTable As Code = As Word MyString1, MyString2
    RPOR7 = 3                        ' Make PPS Pin RP14 U1TX
    wAddress = CRead16 AddrTable[0]  ' Locate the address of first string
    Do
                                        ' Create an infinite loop
        MyByte = cPtr8(wAddress++)   ' Read each character from code memory
        If MyByte = 0 Then Break     ' Exit when null found
        HRSOut MyByte                ' Display the character
    Loop
                                        ' Close the loop
    HRSOut 13
    wAddress = CRead16 AddrTable[1]  ' Locate the address of second string
    Do
                                        ' Create an infinite loop
        MyByte = cPtr8(wAddress++)    ' Read each character from code memory
        If MyByte = 0 Then Break
                                        ' Exit when null found
        HRSOut MyByte
                                        ' Display the character
    Loop
                                        ' Close the loop

Note.

It is not recommended to use Cdata in a new program, and may be dropped from future compiler versions. It is recommended to use the Dim As Code construct.

See also : cPtr8, cPtr16, cPtr32, cPtr64, Cread8, Cread16, Cread32, Cread64, Dim.