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

Subversion Repositories light8080

[/] [light8080/] [trunk/] [c/] [hello.c] - Blame information for rev 65

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 65 motilito
//---------------------------------------------------------------------------------------
2
//      Project:                        light8080 SOC           WiCores Solutions 
3
//
4
//      File name:                      hello.c                                 (February 04, 2012)
5
//
6
//      Writer:                         Moti Litochevski 
7
//
8
//      Description:
9
//              This file contains a simple program written in Small-C that sends a string to 
10
//              the UART and then switches to echo received bytes. 
11
//
12
//      Revision History:
13
//
14
//      Rev <revnumber>                 <Date>                  <owner> 
15
//              <comment>
16
//---------------------------------------------------------------------------------------
17
 
18
#include ..\tools\c80\c80.lib
19
 
20
// UART IO registers 
21
port (128) UDATA;               // uart data register used for both transmit and receive 
22
port (129) UBAUDL;              // low byte of baud rate register 
23
port (130) UBAUDH;              // low byte of baud rate register 
24
port (131) USTAT;               // uart status register 
25
// digital IO ports registers 
26
port (132) P1REG;       // output port1 - used as first attenuator control 
27
port (133) P2REG;               // output port2 - used as low digit LCD 
28
port (134) P3REG;               // output port3 - used as high digit LCD 
29
port (135) P4REG;               // output port4 
30
// simulation end register 
31
// writing any value to this port will end the verilog simulation when using tb_l80soc 
32
// test bench. 
33
port (255) SIMEND;
34
 
35
// registers bit fields definition 
36
// uart status register decoding 
37
#define UTXBUSY         1
38
#define URXFULL         16
39
 
40
// globals 
41
char rxbyte;            // byte received from the uart 
42
int tstary[2] = {1234, 5678};
43
 
44
//---------------------------------------------------------------------------------------
45
// send a single byte to the UART 
46
sendbyte(by)
47
char by;
48
{
49
        while (USTAT & UTXBUSY);
50
        UDATA = by;
51
}
52
 
53
// check if a byte was received by the uart 
54
getbyte()
55
{
56
        if (USTAT & URXFULL) {
57
                rxbyte = UDATA;
58
                return 1;
59
        }
60
        else
61
                return 0;
62
}
63
 
64
// send new line to the UART 
65
nl()
66
{
67
        sendbyte(13);
68
        sendbyte(10);
69
}
70
 
71
// sends a string to the UART 
72
printstr(sptr)
73
char *sptr;
74
{
75
        while (*sptr != 0)
76
                sendbyte(*sptr++);
77
}
78
 
79
// sends a decimal value to the UART 
80
printdec(dval)
81
int dval;
82
{
83
        if (dval<0) {
84
                sendbyte('-');
85
                dval = -dval;
86
        }
87
        outint(dval);
88
}
89
 
90
// function copied from c80dos.c 
91
outint(n)
92
int n;
93
{
94
int q;
95
 
96
        q = n/10;
97
        if (q) outint(q);
98
        sendbyte('0'+(n-q*10));
99
}
100
 
101
// sends a hexadecimal value to the UART 
102
printhex(hval)
103
int hval;
104
{
105
int q;
106
 
107
        q = hval/16;
108
        if (q) printhex(q);
109
        q = hval-q*16;
110
        if (q > 9)
111
                sendbyte('A'+q-10);
112
        else
113
                sendbyte('0'+q);
114
}
115
 
116
// program main routine 
117
main()
118
{
119
        // configure UART baud rate - set to 9600 for 30MHz clock 
120
        // BAUD = round(<clock>/<baud rate>/16) = round(30e6/9600/16) = 195 
121
        UBAUDL = 195;
122
        UBAUDH = 0;
123
 
124
        // print message 
125
        printstr("Hello World!!!"); nl();
126
        printstr("Dec value: "); printdec(tstary[1]); nl();
127
        printstr("Hex value: 0x"); printhex(tstary[0]); nl();
128
        printstr("Echoing received bytes: "); nl();
129
 
130
        // loop forever 
131
        while (1) {
132
                // check if a new byte was received 
133
                if (getbyte())
134
                        // echo the received byte to the UART 
135
                        sendbyte(rxbyte);
136
        }
137
}
138
 
139
//---------------------------------------------------------------------------------------
140
//                                              Th.. Th.. Th.. Thats all folks !!!
141
//---------------------------------------------------------------------------------------
142
 

powered by: WebSVN 2.1.0

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