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

Subversion Repositories versatile_counter

[/] [versatile_counter/] [trunk/] [rtl/] [verilog/] [versatile_counter_generator.php] - Blame information for rev 35

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

Line No. Rev Author Line
1 20 unneback
#!/usr/bin/php
2
<?php
3
 
4
function __autoload($className) {
5
    include "$className.class.php";
6
}
7
 
8
if(!isset($argv[1])) {
9
    ?>
10
 
11
        Usage: <?php echo basename($argv[0]); ?> <fileName>
12
 
13
<?php
14
    die(1);
15
}
16
 
17
$csv = new CSV($argv[1]);
18
 
19
$counter    $csv->getRow();
20
$csv->parseHeader();
21
$inputs     $csv->getRow();
22
$csv->parseHeader();
23
$outputs    $csv->getRow();
24
$csv->parseHeader();
25
$wrap       $csv->getRow();
26
$csv->parseHeader();
27
$parameters $csv->getRow();
28
 
29
$length $parameters['length'];
30
 
31
# copyright
32
echo "//////////////////////////////////////////////////////////////////////" PHP_EOL;
33
echo "////                                                              ////" PHP_EOL;
34
echo "////  Versatile counter                                           ////" PHP_EOL;
35
echo "////                                                              ////" PHP_EOL;
36
echo "////  Description                                                 ////" PHP_EOL;
37
echo "////  Versatile counter, a reconfigurable binary, gray or LFSR    ////" PHP_EOL;
38
echo "////  counter                                                     ////" PHP_EOL;
39
echo "////                                                              ////" PHP_EOL;
40
echo "////  To Do:                                                      ////" PHP_EOL;
41
echo "////   - add LFSR with more taps                                  ////" PHP_EOL;
42
echo "////                                                              ////" PHP_EOL;
43
echo "////  Author(s):                                                  ////" PHP_EOL;
44
echo "////      - Michael Unneback, unneback@opencores.org              ////" PHP_EOL;
45
echo "////        ORSoC AB                                              ////" PHP_EOL;
46
echo "////                                                              ////" PHP_EOL;
47
echo "//////////////////////////////////////////////////////////////////////" PHP_EOL;
48
echo "////                                                              ////" PHP_EOL;
49
echo "//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////" PHP_EOL;
50
echo "////                                                              ////" PHP_EOL;
51
echo "//// This source file may be used and distributed without         ////" PHP_EOL;
52
echo "//// restriction provided that this copyright statement is not    ////" PHP_EOL;
53
echo "//// removed from the file and that any derivative work contains  ////" PHP_EOL;
54
echo "//// the original copyright notice and the associated disclaimer. ////" PHP_EOL;
55
echo "////                                                              ////" PHP_EOL;
56
echo "//// This source file is free software; you can redistribute it   ////" PHP_EOL;
57
echo "//// and/or modify it under the terms of the GNU Lesser General   ////" PHP_EOL;
58
echo "//// Public License as published by the Free Software Foundation; ////" PHP_EOL;
59
echo "//// either version 2.1 of the License, or (at your option) any   ////" PHP_EOL;
60
echo "//// later version.                                               ////" PHP_EOL;
61
echo "////                                                              ////" PHP_EOL;
62
echo "//// This source is distributed in the hope that it will be       ////" PHP_EOL;
63
echo "//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////" PHP_EOL;
64
echo "//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////" PHP_EOL;
65
echo "//// PURPOSE.  See the GNU Lesser General Public License for more ////" PHP_EOL;
66
echo "//// details.                                                     ////" PHP_EOL;
67
echo "////                                                              ////" PHP_EOL;
68
echo "//// You should have received a copy of the GNU Lesser General    ////" PHP_EOL;
69
echo "//// Public License along with this source; if not, download it   ////" PHP_EOL;
70
echo "//// from http://www.opencores.org/lgpl.shtml                     ////" PHP_EOL;
71
echo "////                                                              ////" PHP_EOL;
72
echo "//////////////////////////////////////////////////////////////////////" PHP_EOL;
73
 
74
echo PHP_EOL "// " $counter['type'] . " counter" PHP_EOL;
75
echo "module " $counter['Name']. " (";
76
 
77 26 unneback
if ($inputs['clear']=="1") { echo " clear,"; }
78 20 unneback
if ($inputs['set']=="1")   { echo " set,"; }
79 26 unneback
if ($inputs['cke']=="1")   { echo " cke,"; }
80 20 unneback
if ($inputs['rew']=="1")   { echo " rew,"; }
81
 
82
if ($outputs['q']=="1")      { echo " q,"; }
83 28 unneback
if ($outputs['q_bin']=="1" and $counter['type']=="GRAY")  { echo " q_bin,"; }
84 20 unneback
if ($outputs['z']=="1")      { echo " z,"; }
85
if ($outputs['zq']=="1")     { echo " zq,"; }
86
if ($outputs['level1']=="1") { echo " level1,"; }
87
if ($outputs['level2']=="1") { echo " level2,"; }
88
 
89
echo " rst,";
90
echo " clk);" PHP_EOL;
91 30 unneback
echo PHP_EOL;
92 20 unneback
 
93
echo "   parameter length = " $length ";" PHP_EOL;
94
 
95
if ($inputs['clear']=="1") { echo "   input " "clear;" PHP_EOL; }
96
if ($inputs['set']=="1")   { echo "   input " "set;" PHP_EOL; }
97
if ($inputs['cke']=="1")   { echo "   input " "cke;" PHP_EOL; }
98
if ($inputs['rew']=="1")   { echo "   input " "rew;" PHP_EOL; }
99
 
100 25 unneback
if ($counter['type']=="GRAY") {
101
    if ($outputs['q']=="1")      { echo "   output reg [length:1] q;" PHP_EOL; }    
102
} else {
103
    if ($outputs['q']=="1")      { echo "   output [length:1] q;" PHP_EOL; }
104
}
105 29 unneback
if ($outputs['q_bin']=="1" and $counter['type']=="GRAY")  { echo "   output [length:1] q_bin;" PHP_EOL; }
106 20 unneback
if ($outputs['z']=="1")      { echo "   output z;" PHP_EOL; }
107
if ($outputs['zq']=="1")     { echo "   output reg zq;" PHP_EOL; }
108
if ($outputs['level1']=="1") { echo "   output reg level1;" PHP_EOL; }
109
if ($outputs['level2']=="1") { echo "   output reg level2;" PHP_EOL; }
110
 
111
echo "   input rst;" PHP_EOL;
112
echo "   input clk;" PHP_EOL;
113
echo PHP_EOL;
114
 
115 24 unneback
    if ($parameters['clear_value']!="") { echo "   parameter clear_value = " $parameters['clear_value'] . ";" PHP_EOL; }
116
    if ($parameters['set_value']!="")   { echo "   parameter set_value = " $parameters['set_value'] . ";" PHP_EOL; }
117
    if ($parameters['wrap_value']!="")  { echo "   parameter wrap_value = " $parameters['wrap_value'] . ";" PHP_EOL; }
118
    if ($parameters['level1']!="")      { echo "   parameter level1_value = " $parameters['level1'] . ";" PHP_EOL; }
119
    if ($parameters['level2']!="")      { echo "   parameter level2_value = " $parameters['level2'] . ";" PHP_EOL; }
120 20 unneback
 
121 34 unneback
echo PHP_EOL;
122
if ($outputs['level1']=="1" and $inputs['clear']=="0") { echo  "   wire clear;" PHP_EOL "   assign clear = 1'b0;" PHP_EOL; }
123
if ($outputs['level1']=="1" and $inputs['rew']=="0") { echo  "   wire rew;" PHP_EOL "   assign rew = 1'b0;" PHP_EOL; }
124
 
125
echo "   reg  [length:1] qi;" PHP_EOL;
126 20 unneback
if ($counter['type']=="LFSR") { echo "   reg lfsr_fb";}
127 22 unneback
if ($counter['type']=="LFSR" and $inputs['rew']==1) { echo ", lfsr_fb_rew;" PHP_EOL; } else { if ($counter['type']=="LFSR") echo ";" PHP_EOL; }
128 20 unneback
if ($inputs['rew']==1) { echo "   wire  [length:1] q_next, q_next_fw, q_next_rew;" PHP_EOL; }
129
else { echo "   wire [length:1] q_next;" PHP_EOL; }
130
if ($counter['type']=="LFSR" and $inputs['rew']==1) {
131
    echo "   reg [32:1] polynom_rew;" PHP_EOL;
132
    echo "   integer j;" PHP_EOL;
133
}
134
 
135
if ($counter['type']=="LFSR") {
136
    echo "   reg [32:1] polynom;" PHP_EOL;
137
    echo "   integer i;" PHP_EOL PHP_EOL;
138
    echo "   always @ (qi)" PHP_EOL;
139
    echo "   begin
140
        case (length) 
141
         2: polynom = 32'b11;                               // 0x3
142
         3: polynom = 32'b110;                              // 0x6
143
         4: polynom = 32'b1100;                             // 0xC
144
         5: polynom = 32'b10100;                            // 0x14
145
         6: polynom = 32'b110000;                           // 0x30
146
         7: polynom = 32'b1100000;                          // 0x60
147
         8: polynom = 32'b10111000;                         // 0xb8
148
         9: polynom = 32'b100010000;                        // 0x110
149
        10: polynom = 32'b1001000000;                       // 0x240
150
        11: polynom = 32'b10100000000;                      // 0x500
151
        12: polynom = 32'b100000101001;                     // 0x829
152
        13: polynom = 32'b1000000001100;                    // 0x100C
153
        14: polynom = 32'b10000000010101;                   // 0x2015
154
        15: polynom = 32'b110000000000000;                  // 0x6000
155
        16: polynom = 32'b1101000000001000;                 // 0xD008
156
        17: polynom = 32'b10010000000000000;                // 0x12000
157
        18: polynom = 32'b100000010000000000;               // 0x20400
158
        19: polynom = 32'b1000000000000100011;              // 0x40023
159
        20: polynom = 32'b10000010000000000000;             // 0x82000
160
        21: polynom = 32'b101000000000000000000;            // 0x140000
161
        22: polynom = 32'b1100000000000000000000;           // 0x300000
162
        23: polynom = 32'b10000100000000000000000;          // 0x420000
163
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
164
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
165
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
166
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
167
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
168
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
169
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
170
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
171
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
172
        default: polynom = 32'b0;
173
        endcase
174
        lfsr_fb = qi[length];
175
        for (i=length-1; i>=1; i=i-1) begin
176
            if (polynom[i])
177
                lfsr_fb = lfsr_fb  ~^ qi[i];
178
        end
179
    end";
180
echo PHP_EOL;
181
}
182
 
183
if ($inputs['rew']!=1) { echo "   assign q_next = "; } else { echo "   assign q_next_fw  = "; }
184
if ($inputs['clear']==1)  { echo " clear ? {length{1'b0}} :";}
185
if ($inputs['set']==1)    { echo " set ? set_value :";}
186
if ($wrap['wrap']==1)     { echo "(qi == wrap_value) ? {length{1'b0}} :";}
187 32 unneback
if ($counter['type']=="LFSR") { echo "{qi[length-1:1],lfsr_fb};"; } else { echo "qi + {{length-1{1'b0}},1'b1};"; }
188 20 unneback
echo PHP_EOL;
189
 
190
if ($inputs['rew']) {
191
    if ($counter['type']=="LFSR") {
192
            echo "   always @ (qi)" PHP_EOL;
193
    echo "   begin
194
        case (length) 
195
         2: polynom_rew = 32'b11;
196
         3: polynom_rew = 32'b110;
197
         4: polynom_rew = 32'b1100;
198
         5: polynom_rew = 32'b10100;
199
         6: polynom_rew = 32'b110000;
200
         7: polynom_rew = 32'b1100000;
201
         8: polynom_rew = 32'b10111000;
202
         9: polynom_rew = 32'b100010000;
203
        10: polynom_rew = 32'b1001000000;
204
        11: polynom_rew = 32'b10100000000;
205
        12: polynom_rew = 32'b100000101001;
206
        13: polynom_rew = 32'b1000000001100;
207
        14: polynom_rew = 32'b10000000010101;
208
        15: polynom_rew = 32'b110000000000000;
209
        16: polynom_rew = 32'b1101000000001000;
210
        17: polynom_rew = 32'b10010000000000000;
211
        18: polynom_rew = 32'b100000010000000000;
212
        19: polynom_rew = 32'b1000000000000100011;
213
        20: polynom_rew = 32'b10000010000000000000;
214
        21: polynom_rew = 32'b101000000000000000000;
215
        22: polynom_rew = 32'b1100000000000000000000;
216
        23: polynom_rew = 32'b10000100000000000000000;
217
        24: polynom_rew = 32'b111000010000000000000000;
218
        25: polynom_rew = 32'b1001000000000000000000000;
219
        26: polynom_rew = 32'b10000000000000000000100011;
220
        27: polynom_rew = 32'b100000000000000000000010011;
221
        28: polynom_rew = 32'b1100100000000000000000000000;
222
        29: polynom_rew = 32'b10100000000000000000000000000;
223
        30: polynom_rew = 32'b100000000000000000000000101001;
224
        31: polynom_rew = 32'b1001000000000000000000000000000;
225
        32: polynom_rew = 32'b10000000001000000000000000000011;
226
        default: polynom_rew = 32'b0;
227
        endcase
228
        // rotate left
229
        polynom_rew[length:1] = { polynom_rew[length-2:1],polynom_rew[length] };
230
        lfsr_fb_rew = qi[length];
231
        for (i=length-1; i>=1; i=i-1) begin
232
            if (polynom_rew[i])
233
                lfsr_fb_rew = lfsr_fb_rew  ~^ qi[i];
234
        end
235
    end";
236
echo PHP_EOL;
237
 
238
    }
239
    echo "   assign q_next_rew = ";
240
    if ($inputs['clear']==1)  { echo " clear ? clear_value :";}
241
    if ($inputs['set']==1)    { echo " set ? set_value :";}
242
    if ($wrap['wrap']==1)     { echo "(qi == wrap_value) ? {length{1'b0}} :";}
243 32 unneback
    if ($counter['type']=="LFSR") { echo "{lfsr_fb_rew,qi[length:2]};"; } else { echo "qi - {{length-1{1'b0}},1'b1};"; }
244 20 unneback
    echo PHP_EOL;
245
    echo "   assign q_next = rew ? q_next_rew : q_next_fw;" PHP_EOL;
246
247
 
248
echo "
249
   always @ (posedge clk or posedge rst)
250
     if (rst)
251
       qi <= {length{1'b0}};
252
     else" PHP_EOL;
253
if ($inputs['cke']) { echo "     if (cke)" PHP_EOL;}
254 25 unneback
echo "       qi <= q_next;" PHP_EOL;
255 20 unneback
echo PHP_EOL;
256
 
257
if ($outputs['q']) {
258
    if ($counter['type'] == "GRAY" or $counter['type'] == "gray") {
259 26 unneback
        echo "   always @ (posedge clk or posedge rst)
260 20 unneback
     if (rst)
261 26 unneback
       q <= {length{1'b0}};
262 25 unneback
     else" PHP_EOL;
263
        if ($inputs['cke']) { echo "       if (cke)" PHP_EOL; }
264
        echo "         q <= (q_next>>1) ^ q_next;" PHP_EOL;
265 26 unneback
        if ($outputs['q_bin']) { echo PHP_EOL "   assign q_bin = qi;" PHP_EOL; }
266 20 unneback
    } else {
267
        echo "   assign q = qi;" PHP_EOL;
268
    }
269
}
270
echo PHP_EOL;
271
 
272
if ($outputs['z']) { echo "   assign z = (q == {length{1'b0}});" PHP_EOL; }
273
 
274
if ($outputs['zq']) {
275
    echo "
276
   always @ (posedge clk or posedge rst)
277
     if (rst)
278
       zq <= 1'b1;
279
     else" PHP_EOL;
280
    if ($inputs['cke']) { echo "     if (cke)" PHP_EOL; }
281
    echo "       zq <= q_next == {length{1'b0}};" PHP_EOL;
282
}
283
 
284
if ($outputs['level1']) {
285
    echo "
286
    always @ (posedge clk or posedge rst)
287
    if (rst)
288
        level1 <= 1'b0;
289
    else" PHP_EOL;
290
    if ($inputs['cke']) { echo "    if (cke)" PHP_EOL; }
291 34 unneback
    echo "    if (clear)
292
        level1 <= 1'b0;
293
    else if (q_next == level1_value)
294 20 unneback
        level1 <= 1'b1;
295 33 unneback
    else if (qi == level1_value & rew)
296 20 unneback
        level1 <= 1'b0;" PHP_EOL;
297
}
298
 
299
if ($outputs['level2']) {
300
    echo "
301
    always @ (posedge clk or posedge rst)
302
    if (rst)
303
        level2 <= 1'b0;
304
    else" PHP_EOL;
305
    if ($inputs['cke']) { echo "    if (cke)" PHP_EOL; }
306 34 unneback
    echo "    if (clear)
307
        level2 <= 1'b0;
308
    else if (q_next == level2_value)
309 20 unneback
        level2 <= 1'b1;
310 33 unneback
    else if (qi == level2_value & rew)
311 20 unneback
        level2 <= 1'b0;" PHP_EOL;
312
}
313
 
314
echo "endmodule" PHP_EOL;

powered by: WebSVN 2.1.0

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