//////////////////////////////////////////////////////
//
// Port Template
//
// This example shows how to minimise code size using
// function templates to read and write target ports.
// Same function templates can be used for more than
// one port in the same program.
//
// This code was designed for PIC16F877A. It can be used
// on other targets but you might need to alter the hardware
// initialisation part at the beginning of 'main' function.
//
// To test build the code unser SourceBoost IDE, start it under
// debugger and use the 'Button Block' plugin conigured for port B
// and 'Led Block' plugin configured for port C. For any button
// pressed on the 'Button Block' plugin a relevand LED will get lit
// on the 'Led Block' plugin.
//
// Author: Pavel Baranov (Jan 2009)
//
//////////////////////////////////////////////////////

#include <system.h>


//////////////////////////////////////////////////////
// Template function to write data into a port.
// Port address is specified in the ADDR template
// argument. A template argument works just like a
// function argument evaluated at compile time.
template <unsigned char ADDR>
inline void out( unsigned char data )
{
    unsigned char port @ ADDR;
    port = data;
}

//////////////////////////////////////////////////////
// Template function to read data from a port.
// Port address is specified in the ADDR template
// argument. A template argument works just like a
// function argument evaluated at compile time.
template <unsigned char ADDR>
inline unsigned char in( void )
{
    unsigned char port @ ADDR;
    return port;
}

void interrupt( void )
{
    //////////////////////////////////////////////
    // This sample does not use interrupt but
    // we will declare interrupt function anyway.
}

void main( void )
{
    //////////////////////////////////////////////
    // This code will just forward data from
    // port B to port C using function templates
    // above.

    // Initialise hardware (this initialisation was written for PIC16F877A, if you
    // change the target you might need to change the initialisation code as well)
    trisb = 0xFF;    //configure port B for input
    trisc = 0x00;    //configure port C for output

    // Endless cycle where data from port B
    // is copied into port C
    while( 1 )
        out<PORTC>( in<PORTB>() );
}