Passing ROM data to a subroutine with a pointer to ROM

In the previous section, each block of data in ROM was anonymous and the return address of the subroutine was used as a pointer to this block.

The ROM_ENTRY() macro allows to attach an identifier to a location in ROM as following:

ROM_ENTRY(hello)
{
   ROM_TXT("hello guys !\0") ;
}

The hello identifier has the type ROMptr and can be passed to any routine receiving this kind of pointer. The void lcd_Rputs(ROMptr) ; (from the LCD library) is an example of such a function.

void f()
{
  lcd_Rputs(hello) ; // displays «hello guys !»
}

As previously, it is easy to write code that receives a ROMptr at C level. For that purpose, the rom.h header provides a macro ROM_POINTER that allows to declare that a ROMptr will be used to access ROM.

For example, suppose we want to implement a new version of puts() that access the character to be printed from a ROM pointer.

void Rputs(ROMptr p) 
{
  ROM_POINTER(p) ;
  READ_ROMBYTE ; 
  while( prodl )
  {
    putchar( prodl ) ; READ_ROMBYTE ; 
  }
}

Not really complex, isn't it ? But the next way to access ROM is even more simple, and more powerful.

Alain Gibaud 2015-07-09