Accessing data in ROM with a ROM accessor

In the previous sections, ROM data was traversed in sequence. Using a ROM accessor, ROM data can be traversed randomly. A ROM accessor is simply a function that mimic the behavior of an access to array's elements.

Eight macros are available to declare a ROM accessor. For example, the ROMF_TXT macro both allows to declare a text, and the way to access it:

ROMF_TXT( atext , "whiizz !\0")

Here, atext is a ROM accessor for the specified string. It means that atext(0) returns 'w', atext(1) returns 'h' , and so on. The type of atext is romf_txt. The f stands for function, because accessors are technically functions receiving an unsigned int.

Here is another flavor of the usual puts() function that receive a pointer to a romf_txt :

void RFputs(romf_txt *p)
{
  uint8_t k ;
  for( k = 0 ; p(k) ; ++k)
    putchar(p(k)) ;
}

The following table shows the available accessors, and their corresponding types.

accessor declaration accessor type value type example
ROMF_TXT romf_txt int8_t ROMF_TXT(a, "hello\0")
ROMF_DATA8 romf_data8 int8_t ROMF_DATA8(b, -1,2,0xFF)
ROMF_DATA8U romf_data8u uint8_t ROMF_DATA8U(c, 1,2,0xFF)
ROMF_DATA16 romf_data16 int16_t ROMF_DATA16(d, -1,0xFF34)
ROMF_DATA16U romf_data16u uint16_t ROMF_DATA16U(e, 1,12300)
ROMF_DATA32 romf_data32 int32_t ROMF_DATA32(f, -1,0xdeadbeef)
ROMF_DATA32U romf_data32u uint32_t ROMF_DATA32U(g, 22, 34)
ROMF_DATAF romf_dataflt float ROMF_DATAF(h, 3.14,-22.0/9)

Since V0.7.4, these macros use the __data...__ instructions (see section 12.12.4), so they can receive any valid C constant expression.

Alain Gibaud 2015-07-09