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

Subversion Repositories simple_customized_counter

[/] [simple_customized_counter/] [trunk/] [OpenCoresLib/] [simple_customized_counter/] [CounterLib.psC] - Blame information for rev 58

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 50 lmorin12
// ===================================================================
2 58 lmorin12
// This is a library of counter templates
3 50 lmorin12
// ===================================================================
4 58 lmorin12
// Template parameter:
5
//    - TYPE determines the type of the counter
6
//      Ex: TYPE = ubyte creates an 8 bits unsigned counter
7
//
8
// ===================================================================
9 50 lmorin12
// Each counter supports:
10
//    - Resetting
11
//    - Counting up
12
//    - Counting down
13
//    - Loading a value from the input iLoadValue
14
//
15
// The code is straightforward, so you can easily create your own counter
16 58 lmorin12
//      by changing the operations. In fact you can create any functions
17
//      like a shift register or even an ALU (Arithmetic and Logic Unit)
18
//      or a calculator.
19 50 lmorin12
//
20
// The output always generates an event.
21 55 lmorin12
//     If you don't want the event, remove the colon ':' in the assignment.
22 50 lmorin12
//         Ex: change oValue := 0; to oValue = 0;
23
//
24
// ===================================================================
25 58 lmorin12
// There are four counter templates in the library.
26 50 lmorin12
//
27
//   The first two executes the operation on input events:
28 58 lmorin12
//    - CCounterEvent_T:    operations depends on inputs: iReset, iUp, iDown, iLoad
29
//    - CCounterOprEvent_T: operations depends on a single input:  iOpr
30 50 lmorin12
//
31
//   The last two executes the operation at each step i.e. FPGA clock:
32 58 lmorin12
//    - CCounterLevel_T:    operations depends on inputs: iReset, iUp, iDown, iLoad
33
//    - CCounterOprLevel_T: operations depends on a single input:  iOpr
34 50 lmorin12
//
35
// ===================================================================
36
// TEST BENCHES:
37
//
38 58 lmorin12
//    - CCounterEvent_T:    Manual test with control panel
39
//                              Project in "TestCounterEvent" folder
40 50 lmorin12
//
41 58 lmorin12
//    - CCounterOprEvent_T: Simulated DE1SoC board, you need the BSP
42
//                          Real DE1SoC board,      you need the real board
43
//                              Project in "TestCounterOprEventBoard" folder
44
//                          Performed by a C++ program
45
//                              Project in "TestCounterOprEventAPI" folder
46
//                              << This project requires a paid license >>
47 50 lmorin12
//
48 58 lmorin12
//    - CCounterLevel_T:    Using signal editor and viewer
49
//                              Project in "TestCounterLevel" folder
50 50 lmorin12
//
51 58 lmorin12
//    - CCounterOprLevel_T: Using signal editor and viewer
52
//                              Project in "TestCounterOprLevel" folder
53 50 lmorin12
// ===================================================================
54
 
55
library CounterLib
56
{
57
    // -------------------------------------------------------------------
58
    // This counter executes the operation on input events
59
    // There is a priority assigned to each function, in parenthesis
60
    //    0 indicates highest priority
61
    //      and priority decreases with increasing value
62
    // -------------------------------------------------------------------
63 58 lmorin12
    template 
64
    component CCounterEvent_T (in  active  bit  iReset,
65
                               in  active  bit  iUp,
66
                               in  active  bit  iDown,
67
                               in  active  bit  iLoad,
68
                               in  passive TYPE iLoadValue,
69
                               out active  TYPE oValue)
70 50 lmorin12
    {
71
        DoReset(0) on iReset
72
        {
73 58 lmorin12
            oValue := (TYPE)0;
74 50 lmorin12
        }
75
 
76
        CountUp(1) on iUp
77
        {
78
            oValue:++;
79
        }
80
 
81
        CountDown(2) on iDown
82
        {
83
            oValue:--;
84
        }
85
 
86
        LoadValue(3) on iLoad
87
        {
88
            oValue := iLoadValue;
89
        }
90
    };
91
 
92
    // -------------------------------------------------------------------
93
    // This counter executes the operation at each step or FPGA clock cycle
94
    // The priority is implemented with if and else instructions
95
    // -------------------------------------------------------------------
96 58 lmorin12
    template 
97
    component CCounterLevel_T (in  active  bit  iReset,
98
                               in  active  bit  iUp,
99
                               in  active  bit  iDown,
100
                               in  active  bit  iLoad,
101
                               in  passive TYPE iLoadValue,
102
                               out active  TYPE oValue)
103 50 lmorin12
    {
104
        always()
105
        {
106
            if(iReset)
107
            {
108 58 lmorin12
                oValue := (TYPE)0;
109 50 lmorin12
            }
110
 
111
            else if(iUp)
112
            {
113
                oValue:++;
114
            }
115
 
116
            else if(iDown)
117
            {
118
                oValue:--;
119
            }
120
 
121
            else if(iLoad)
122
            {
123
                oValue := iLoadValue;
124
            }
125
        }
126
    };
127
 
128
    // -------------------------------------------------------------------
129
    // Define type and constants for the counter operation
130
    // -------------------------------------------------------------------
131
    enum Opr_t { cOprNone, cOprReset, cOprUp, cOprDown, cOprLoad };
132
 
133
    // -------------------------------------------------------------------
134
    // This counter executes the operation on input events.
135
    // The input iOpr has the type Opr_t and determines the operation.
136
    // There is no priority on the operation.
137
    // -------------------------------------------------------------------
138
    // The switch statement has no break, only one case is executed
139
    // -------------------------------------------------------------------
140 58 lmorin12
    template 
141
    component CCounterOprEvent_T (in  active  Opr_t iOpr,
142
                                  in  passive TYPE  iLoadValue,
143
                                  out active  TYPE  oValue)
144 50 lmorin12
    {
145
        ExecuteOpr(0) on iOpr
146
        {
147
            // The switch statement has no break, only one case is executed
148
            switch(iOpr)
149
            {
150 58 lmorin12
                case cOprReset: oValue:= (TYPE)0;
151 50 lmorin12
                case cOprUp:    oValue:++;
152
                case cOprDown:  oValue:--;
153
                case cOprLoad:  oValue := iLoadValue;
154
            }
155
        }
156
    };
157
 
158
    // -------------------------------------------------------------------
159
    // This counter executes the operation at each step or FPGA clock cycle
160
    // The input iOpr has the type Opr_t and determines the operation
161
    // There is no priority on the operations
162
    // -------------------------------------------------------------------
163 58 lmorin12
    template 
164
    component CCounterOprLevel_T (in  active  Opr_t iOpr,
165
                                  in  passive TYPE  iLoadValue,
166
                                  out active  TYPE  oValue)
167 50 lmorin12
    {
168
        always()
169
        {
170
            // Send the event
171
            oValue:;
172
 
173
            // Assign the value
174
            // The switch instruction returns a value
175
            oValue = switch(iOpr)
176
                     {
177 58 lmorin12
                         case cOprReset: (TYPE)0;
178 50 lmorin12
                         case cOprUp:    oValue + 1ub;
179
                         case cOprDown:  oValue - 1ub;
180
                         case cOprLoad:  iLoadValue;
181
                     };
182
        }
183
    };
184
};

powered by: WebSVN 2.1.0

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