home *** CD-ROM | disk | FTP | other *** search
- #include <intrpt.h>
-
- /*
- * This source file demonstrates some of the special features
- * of the HI-TECH 6809 C Cross Compiler. This program should be
- * compiled with 6809 code generation selected.
- *
- * To generate the best possible code for this program, select
- * "6809 Code" from the options menu and "Full Optimization" from
- * the optimization menu.
- *
- * HI-TECH C has a number of features designed to make programming
- * embedded processors simpler and more efficient, these include:
- *
- * interrupt functions: interrupt handlers may be written entirely
- * in C (no messy assembler code required).
- * The 6809 compiler also supports a second
- * interrupt type, "fast interrupt", which
- * allows C code to handle the 6809 FIRQ.
- *
- * absolute variables: variables of any type (even complex
- * structures and arrays) can be placed at
- * any absolute address in memory - great
- * for handling memory mapped devices.
- *
- * in-line assembler: assembler code can be place in line. This
- * is useful for accessing special instructions.
- *
- * You may notice that the code generated by the compiler includes
- * "long" branch instructions like LBRA and LBEQ. The 6809 assembler
- * performs optimization of such branches, replacing long branches
- * with the shorter form if the destination address is within
- * range.
- *
- * The 6809 architecture is very well suited to a block structured
- * high level language like C. Many of the addressing modes are
- * perfectly suited for implementation of local variables within
- * a stack frame. The HI-TECH 6809 C Cross Compiler takes advantage
- * of all addressing modes of the 6809 to produce highly optimized
- * code. We find that for a given piece of source code, the 6809
- * compiler usually produces the most compact code of any of our
- * C compilers
- */
-
- #ifndef m6809
- #error This program should be compiled to 6809 code
- #endif
-
- volatile unsigned char PIA_STATUS @ 0xC000; /* mem mapped PIA */
- volatile unsigned char PIA_BUFFER @ 0xC001; /* at C000 - C001 */
-
- unsigned char rxbuf[256]; /* 256 byte circular buffer */
- near unsigned char ri_ptr, ro_ptr; /* head, tail pointers */
-
- volatile struct {
- unsigned char baud; /* baud rate register */
- unsigned char sccr1; /* control reg 1 */
- unsigned char sccr2; /* control reg 2 */
- unsigned char scsr; /* status register */
- unsigned char scdr; /* data register */
- } SCC @ 0x102B;
-
- /*
- * rx_intr handles receive interrupts from the serial port at 0x102B
- *
- * Note the efficiency of the code generated for this function. With
- * HI-TECH C there is no need to revert to assembly language coding
- * for interrupt handlers.
- */
-
- interrupt void
- rx_intr(void)
- {
- rxbuf[ri_ptr++] = SCC.scdr;
- if (ro_ptr == ri_ptr) /* buffer overflow ? */
- --ri_ptr;
- }
-
- /*
- * serial_char_avail: returns 1 if a char is available, 0 otherwise
- */
-
- unsigned char
- serial_char_avail(void)
- {
- return (ri_ptr != ro_ptr);
- }
-
- /*
- * serial_get_char: returns one char from the tail of the circular
- * buffer. Assumes that a char is available.
- */
-
- unsigned char
- serial_get_char(void)
- {
- unsigned char ch;
-
- di();
- ch = rxbuf[ro_ptr++];
- ei();
- return ch;
- }
-
- /*
- * Transmit a character via the memory mapped PIA
- * bit 0 of STATUS register indicates it is ready to accept data
- *
- * Note: the paramter "ch" will be passed in the B register.
- */
-
- void
- pia_send(unsigned char ch)
- {
- while (!(PIA_STATUS & 1))
- continue;
- PIA_BUFFER = ch;
- }
-
- /*
- * Initialize the serial port and PIA - demo code only, not intended to be
- * a completely accurate example of 6809 programming.
- *
- * Notes on code generated:
- *
- * 1. Note the quality of the I/O port manipulation code
- * generated by the compiler for the serial port
- * and PIA initialization sequence below.
- */
-
- #define BAUD_VAL 0x30;
-
- void
- device_inits(void)
- {
- unsigned char i;
-
- di(); /* ints of while programming devices */
- ROM_VECTOR(0xFFF8, rx_intr);
- SCC.baud = BAUD_VAL;
- SCC.sccr1 = 0;
- SCC.sccr2 = 0x0C; /* rx and tx enable */
- i = SCC.scsr; /* read status register */
- i = SCC.scdr; /* read data register */
- PIA_STATUS = 0x80; /* reset PIA */
- PIA_STATUS = 0x40; /* mode 2: bidirectional, ints off */
- ri_ptr = ro_ptr = 0;
- ei();
- }
-
- main()
- {
- device_inits();
- for (;;) {
- if (serial_char_avail())
- pia_send(serial_get_char());
- /* other processing */
- }
- }
-