OpenCores
URL https://opencores.org/ocsvn/light8080/light8080/trunk

Subversion Repositories light8080

[/] [light8080/] [trunk/] [c/] [hello.c] - Diff between revs 66 and 67

Only display areas with differences | Details | Blame | View Log

Rev 66 Rev 67
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
//      Project:                        light8080 SOC           WiCores Solutions 
//      Project:                        light8080 SOC           WiCores Solutions 
//
//
//      File name:                      hello.c                                 (February 04, 2012)
//      File name:                      hello.c                                 (February 04, 2012)
//
//
//      Writer:                         Moti Litochevski 
//      Writer:                         Moti Litochevski 
//
//
//      Description:
//      Description:
//              This file contains a simple program written in Small-C that sends a string to 
//              This file contains a simple program written in Small-C that sends a string to 
//              the UART and then switches to echo received bytes. 
//              the UART and then switches to echo received bytes. 
//              This example also include a simple interrupt example which will work with the 
//              This example also include a simple interrupt example which will work with the 
//              verilog testbench. the testbench 
//              verilog testbench. the testbench 
//
//
//      Revision History:
//      Revision History:
//
//
//      Rev <revnumber>                 <Date>                  <owner> 
//      Rev <revnumber>                 <Date>                  <owner> 
//              <comment>
//              <comment>
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
 
 
// define interrupt vectors 
// define interrupt vectors 
// note that this file must be edited to enable interrupt used 
// note that this file must be edited to enable interrupt used 
#include intr_vec.h 
#include intr_vec.h 
// insert c80 assmbly library to the output file 
// insert c80 assmbly library to the output file 
#include ..\tools\c80\c80.lib
#include ..\tools\c80\c80.lib
 
 
// UART IO registers 
// UART IO registers 
port (128) UDATA;               // uart data register used for both transmit and receive 
port (128) UDATA;               // uart data register used for both transmit and receive 
port (129) UBAUDL;              // low byte of baud rate register 
port (129) UBAUDL;              // low byte of baud rate register 
port (130) UBAUDH;              // low byte of baud rate register 
port (130) UBAUDH;              // low byte of baud rate register 
port (131) USTAT;               // uart status register 
port (131) USTAT;               // uart status register 
// digital IO ports registers 
// digital IO ports registers 
port (132) P1DATA;      // port 1 data register 
port (132) P1DATA;      // port 1 data register 
port (133) P1DIR;               // port 1 direction register control 
port (133) P1DIR;               // port 1 direction register control 
port (134) P2DATA;              // port 2 data register 
port (134) P2DATA;              // port 2 data register 
port (135) P2DIR;               // port 2 direction register control 
port (135) P2DIR;               // port 2 direction register control 
// interrupt controller register 
// interrupt controller register 
port (136) INTRENA;             // interrupts enable register 
port (136) INTRENA;             // interrupts enable register 
// simulation end register 
// simulation end register 
// writing any value to this port will end the verilog simulation when using tb_l80soc 
// writing any value to this port will end the verilog simulation when using tb_l80soc 
// test bench. 
// test bench. 
port (255) SIMEND;
port (255) SIMEND;
 
 
// registers bit fields definition 
// registers bit fields definition 
// uart status register decoding 
// uart status register decoding 
#define UTXBUSY         1
#define UTXBUSY         1
#define URXFULL         16
#define URXFULL         16
 
 
// globals 
// globals 
char rxbyte;            // byte received from the uart 
char rxbyte;            // byte received from the uart 
int tstary[2] = {1234, 5678};
int tstary[2] = {1234, 5678};
 
 
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
// send a single byte to the UART 
// send a single byte to the UART 
sendbyte(by)
sendbyte(by)
char by;
char by;
{
{
        while (USTAT & UTXBUSY);
        while (USTAT & UTXBUSY);
        UDATA = by;
        UDATA = by;
}
}
 
 
// check if a byte was received by the uart 
// check if a byte was received by the uart 
getbyte()
getbyte()
{
{
        if (USTAT & URXFULL) {
        if (USTAT & URXFULL) {
                rxbyte = UDATA;
                rxbyte = UDATA;
                return 1;
                return 1;
        }
        }
        else
        else
                return 0;
                return 0;
}
}
 
 
// send new line to the UART 
// send new line to the UART 
nl()
nl()
{
{
        sendbyte(13);
        sendbyte(13);
        sendbyte(10);
        sendbyte(10);
}
}
 
 
// sends a string to the UART 
// sends a string to the UART 
printstr(sptr)
printstr(sptr)
char *sptr;
char *sptr;
{
{
        while (*sptr != 0)
        while (*sptr != 0)
                sendbyte(*sptr++);
                sendbyte(*sptr++);
}
}
 
 
// sends a decimal value to the UART 
// sends a decimal value to the UART 
printdec(dval)
printdec(dval)
int dval;
int dval;
{
{
        if (dval<0) {
        if (dval<0) {
                sendbyte('-');
                sendbyte('-');
                dval = -dval;
                dval = -dval;
        }
        }
        outint(dval);
        outint(dval);
}
}
 
 
// function copied from c80dos.c 
// function copied from c80dos.c 
outint(n)
outint(n)
int n;
int n;
{
{
int q;
int q;
 
 
        q = n/10;
        q = n/10;
        if (q) outint(q);
        if (q) outint(q);
        sendbyte('0'+(n-q*10));
        sendbyte('0'+(n-q*10));
}
}
 
 
// sends a hexadecimal value to the UART 
// sends a hexadecimal value to the UART 
printhex(hval)
printhex(hval)
int hval;
int hval;
{
{
int q;
int q;
 
 
        q = hval/16;
        q = hval/16;
        if (q) printhex(q);
        if (q) printhex(q);
        q = hval-q*16;
        q = hval-q*16;
        if (q > 9)
        if (q > 9)
                sendbyte('A'+q-10);
                sendbyte('A'+q-10);
        else
        else
                sendbyte('0'+q);
                sendbyte('0'+q);
}
}
 
 
// external interrupt 0 service routine 
// external interrupt 0 service routine 
int0_isr()
int0_isr()
{
{
        printstr("Interrupt 0 was asserted."); nl();
        printstr("Interrupt 0 was asserted."); nl();
}
}
 
 
// program main routine 
// program main routine 
main()
main()
{
{
        // configure UART baud rate - set to 9600 for 30MHz clock 
        // configure UART baud rate - set to 9600 for 30MHz clock 
        // BAUD = round(<clock>/<baud rate>/16) = round(30e6/9600/16) = 195 
        // BAUD = round(<clock>/<baud rate>/16) = round(30e6/9600/16) = 195 
//MOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTI
        // Note: Usage of a minimum divider value of 1 will accelerate the RTL simulation. 
//MOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTI
        UBAUDL = 195;
//      UBAUDL = 195;
 
        UBAUDL = 1;
 
//MOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTI
 
//MOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTIMOTI
 
        UBAUDH = 0;
        UBAUDH = 0;
 
 
        // configure both ports to output and digital outputs as zeros 
        // configure both ports to output and digital outputs as zeros 
        P1DATA = 0x00;
        P1DATA = 0x00;
        P1DIR = 0xff;
        P1DIR = 0xff;
        P2DATA = 0x00;
        P2DATA = 0x00;
        P2DIR = 0xff;
        P2DIR = 0xff;
        // enable interrupt 0 only 
        // enable interrupt 0 only 
        INTRENA = 0x01;
        INTRENA = 0x01;
        // enable CPU interrupt 
        // enable CPU interrupt 
#asm 
#asm 
        ei
        ei
#endasm
#endasm
 
 
        // print message 
        // print message 
        printstr("Hello World!!!"); nl();
        printstr("Hello World!!!"); nl();
        printstr("Dec value: "); printdec(tstary[1]); nl();
        printstr("Dec value: "); printdec(tstary[1]); nl();
        printstr("Hex value: 0x"); printhex(tstary[0]); nl();
        printstr("Hex value: 0x"); printhex(tstary[0]); nl();
 
 
        // assert bit 0 of port 1 to test external interrupt 0 
        // assert bit 0 of port 1 to test external interrupt 0 
        P1DATA = 0x01;
        P1DATA = 0x01;
 
 
        printstr("Echoing received bytes: "); nl();
        printstr("Echoing received bytes: "); nl();
        // loop forever 
        // loop forever 
        while (1) {
        while (1) {
                // check if a new byte was received 
                // check if a new byte was received 
                if (getbyte())
                if (getbyte())
                        // echo the received byte to the UART 
                        // echo the received byte to the UART 
                        sendbyte(rxbyte);
                        sendbyte(rxbyte);
        }
        }
}
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
//                                              Th.. Th.. Th.. Thats all folks !!!
//                                              Th.. Th.. Th.. Thats all folks !!!
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.