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