Next Previous Contents

## 8. Hello World! Example

The following short example demonstrates programming in C using the cc65 toolset with a custom run-time environment. In this example, a Xilinx FPGA contains a UART which is connected to a 65c02 processor with FIFO (First-In, First-Out) storage to buffer the data. The C program will wait for an interrupt generated by the receive UART and then respond by transmitting the string "Hello World! " every time a question mark character is received via a call to the hardware driver `rs232_tx ()`. The driver prototype uses the `__fastcall__` extension to indicate that the driver does not use the stack. The FIFO data interface is at address $1000 and is defined as the symbolic constant `FIFO_DATA`. Writing to `FIFO_DATA` transfers a byte of data into the transmit FIFO for subsequent transmission over the serial interface. Reading from `FIFO_DATA` transfers a byte of previously received data out of the receive FIFO. The FIFO status data is at address $1001 and is defined as the symbolic constant `FIFO_STATUS`. For convenience, the symbolic constants `TX_FIFO_FULL` (which isolates bit 0 of the register) and `RX_FIFO_EMPTY` (which isolates bit 1 of the register) have been defined to read the FIFO status.

The following C code is saved in the file "main.c". As this example demonstrates, the run-time environment has been set up such that all of the behind-the-scene work is transparent to the user.

> `

>     #define FIFO_DATA     (*(unsigned char *) 0x1000)
>     #define FIFO_STATUS   (*(unsigned char *) 0x1001)

>     #define TX_FIFO_FULL  (FIFO_STATUS & 0x01)
>     #define RX_FIFO_EMPTY (FIFO_STATUS & 0x02)

>     extern void wait ();
>     extern void __fastcall__ rs232_tx (char *str);

>     int main () {
>       while (1) {                                     //  Run forever
>         wait ();                                      //  Wait for an RX FIFO interrupt

>         while (RX_FIFO_EMPTY == 0) {                  //  While the RX FIFO is not empty
>           if (FIFO_DATA == '?') {                     //  Does the RX character = '?'
>             rs232_tx ("Hello World!");                //  Transmit "Hello World!"
>           }                                           //  Discard any other RX characters
>         }
>       }

>       return (0);                                     //  We should never get here!
>     }

> `

Next Previous Contents