home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / simtel / hitech-c / motorola.exe / FEAT6809.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-21  |  4.4 KB  |  160 lines

  1. #include    <intrpt.h>
  2.  
  3. /*
  4.  *    This source file demonstrates some of the special features
  5.  *    of the HI-TECH 6809 C Cross Compiler.  This program should be
  6.  *      compiled with 6809 code generation selected.
  7.  *
  8.  *    To generate the best possible code for this program, select
  9.  *    "6809 Code" from the options menu and "Full Optimization" from
  10.  *    the optimization menu.
  11.  *
  12.  *    HI-TECH C has a number of features designed to make programming
  13.  *    embedded processors simpler and more efficient, these include:
  14.  *
  15.  *    interrupt functions: interrupt handlers may be written entirely
  16.  *                 in C (no messy assembler code required).
  17.  *                 The 6809 compiler also supports a second
  18.  *                 interrupt type, "fast interrupt", which
  19.  *                 allows C code to handle the 6809 FIRQ.
  20.  *
  21.  *    absolute variables:  variables of any type (even complex
  22.  *                 structures and arrays) can be placed at
  23.  *                 any absolute address in memory - great
  24.  *                 for handling memory mapped devices.
  25.  *
  26.  *    in-line assembler:   assembler code can be place in line.  This
  27.  *                 is useful for accessing special instructions.
  28.  *
  29.  *    You may notice that the code generated by the compiler includes
  30.  *    "long" branch instructions like LBRA and LBEQ.  The 6809 assembler
  31.  *    performs optimization of such branches, replacing long branches
  32.  *    with the shorter form if the destination address is within
  33.  *    range.
  34.  *
  35.  *    The 6809 architecture is very well suited to a block structured
  36.  *    high level language like C.  Many of the addressing modes are
  37.  *    perfectly suited for implementation of local variables within
  38.  *    a stack frame.  The HI-TECH 6809 C Cross Compiler takes advantage
  39.  *    of all addressing modes of the 6809 to produce highly optimized
  40.  *    code.  We find that for a given piece of source code, the 6809
  41.  *    compiler usually produces the most compact code of any of our
  42.  *    C compilers
  43.  */
  44.  
  45. #ifndef    m6809
  46. #error    This program should be compiled to 6809 code
  47. #endif
  48.  
  49. volatile unsigned char    PIA_STATUS @ 0xC000;    /* mem mapped PIA */
  50. volatile unsigned char    PIA_BUFFER @ 0xC001;    /* at C000 - C001 */
  51.  
  52. unsigned char        rxbuf[256];        /* 256 byte circular buffer */
  53. near unsigned char    ri_ptr, ro_ptr;        /* head, tail pointers */
  54.  
  55. volatile struct {
  56.     unsigned char    baud;        /* baud rate register */
  57.     unsigned char    sccr1;        /* control reg 1 */
  58.     unsigned char    sccr2;        /* control reg 2 */
  59.     unsigned char    scsr;        /* status register */
  60.     unsigned char    scdr;        /* data register */
  61. } SCC @ 0x102B;
  62.  
  63. /*
  64.  *    rx_intr handles receive interrupts from the serial port at 0x102B
  65.  *
  66.  *    Note the efficiency of the code generated for this function.  With
  67.  *    HI-TECH C there is no need to revert to assembly language coding
  68.  *    for interrupt handlers.
  69.  */
  70.  
  71. interrupt void
  72. rx_intr(void)
  73. {
  74.     rxbuf[ri_ptr++] = SCC.scdr;
  75.     if (ro_ptr == ri_ptr)        /* buffer overflow ? */
  76.         --ri_ptr;
  77. }
  78.  
  79. /*
  80.  *    serial_char_avail: returns 1 if a char is available, 0 otherwise
  81.  */
  82.  
  83. unsigned char
  84. serial_char_avail(void)
  85. {
  86.     return (ri_ptr != ro_ptr);
  87. }
  88.  
  89. /*
  90.  *    serial_get_char: returns one char from the tail of the circular
  91.  *                  buffer.  Assumes that a char is available.
  92.  */
  93.  
  94. unsigned char
  95. serial_get_char(void)
  96. {
  97.     unsigned char    ch;
  98.  
  99.     di();
  100.     ch = rxbuf[ro_ptr++];
  101.     ei();
  102.     return ch;
  103. }
  104.  
  105. /*
  106.  *    Transmit a character via the memory mapped PIA
  107.  *    bit 0 of STATUS register indicates it is ready to accept data
  108.  *
  109.  *    Note: the paramter "ch" will be passed in the B register.
  110.  */
  111.  
  112. void
  113. pia_send(unsigned char ch)
  114. {
  115.     while (!(PIA_STATUS & 1))
  116.         continue;
  117.     PIA_BUFFER = ch;
  118. }
  119.  
  120. /*
  121.  *    Initialize the serial port and PIA - demo code only, not intended to be
  122.  *    a completely accurate example of 6809 programming.
  123.  *
  124.  *    Notes on code generated:
  125.  *
  126.  *        1.    Note the quality of the I/O port manipulation code
  127.  *            generated by the compiler for the serial port
  128.  *            and PIA initialization sequence below.
  129.  */
  130.  
  131. #define    BAUD_VAL    0x30;
  132.  
  133. void
  134. device_inits(void)
  135. {
  136.     unsigned char    i;
  137.  
  138.     di();                /* ints of while programming devices */
  139.     ROM_VECTOR(0xFFF8, rx_intr);
  140.     SCC.baud = BAUD_VAL;
  141.     SCC.sccr1 = 0;
  142.     SCC.sccr2 = 0x0C;        /* rx and tx enable */
  143.     i = SCC.scsr;            /* read status register */
  144.     i = SCC.scdr;            /* read data register */
  145.     PIA_STATUS = 0x80;        /* reset PIA */
  146.     PIA_STATUS = 0x40;        /* mode 2: bidirectional, ints off */
  147.     ri_ptr = ro_ptr = 0;
  148.     ei();
  149. }
  150.  
  151. main()
  152. {
  153.     device_inits();
  154.     for (;;) {
  155.         if (serial_char_avail())
  156.             pia_send(serial_get_char());
  157.         /* other processing */
  158.     }
  159. }
  160.