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 27

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 22 unneback
if ($outputs['q_bin']=="1")  { 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
 
92
echo "   parameter length = " $length ";" PHP_EOL;
93
 
94
if ($inputs['clear']=="1") { echo "   input " "clear;" PHP_EOL; }
95
if ($inputs['set']=="1")   { echo "   input " "set;" PHP_EOL; }
96
if ($inputs['cke']=="1")   { echo "   input " "cke;" PHP_EOL; }
97
if ($inputs['rew']=="1")   { echo "   input " "rew;" PHP_EOL; }
98
 
99 25 unneback
if ($counter['type']=="GRAY") {
100
    if ($outputs['q']=="1")      { echo "   output reg [length:1] q;" PHP_EOL; }    
101
} else {
102
    if ($outputs['q']=="1")      { echo "   output [length:1] q;" PHP_EOL; }
103
}
104 20 unneback
if ($outputs['q_bin']=="1")  { echo "   output [length:1] q_bin;" PHP_EOL; }
105
if ($outputs['z']=="1")      { echo "   output z;" PHP_EOL; }
106
if ($outputs['zq']=="1")     { echo "   output reg zq;" PHP_EOL; }
107
if ($outputs['level1']=="1") { echo "   output reg level1;" PHP_EOL; }
108
if ($outputs['level2']=="1") { echo "   output reg level2;" PHP_EOL; }
109
 
110
echo "   input rst;" PHP_EOL;
111
echo "   input clk;" PHP_EOL;
112
echo PHP_EOL;
113
 
114 24 unneback
    if ($parameters['clear_value']!="") { echo "   parameter clear_value = " $parameters['clear_value'] . ";" PHP_EOL; }
115
    if ($parameters['set_value']!="")   { echo "   parameter set_value = " $parameters['set_value'] . ";" PHP_EOL; }
116
    if ($parameters['wrap_value']!="")  { echo "   parameter wrap_value = " $parameters['wrap_value'] . ";" PHP_EOL; }
117
    if ($parameters['level1']!="")      { echo "   parameter level1_value = " $parameters['level1'] . ";" PHP_EOL; }
118
    if ($parameters['level2']!="")      { echo "   parameter level2_value = " $parameters['level2'] . ";" PHP_EOL; }
119 20 unneback
 
120
echo PHP_EOL "   reg  [length:1] qi;" PHP_EOL;
121
if ($counter['type']=="LFSR") { echo "   reg lfsr_fb";}
122 22 unneback
if ($counter['type']=="LFSR" and $inputs['rew']==1) { echo ", lfsr_fb_rew;" PHP_EOL; } else { if ($counter['type']=="LFSR") echo ";" PHP_EOL; }
123 20 unneback
if ($inputs['rew']==1) { echo "   wire  [length:1] q_next, q_next_fw, q_next_rew;" PHP_EOL; }
124
else { echo "   wire [length:1] q_next;" PHP_EOL; }
125
if ($counter['type']=="LFSR" and $inputs['rew']==1) {
126
    echo "   reg [32:1] polynom_rew;" PHP_EOL;
127
    echo "   integer j;" PHP_EOL;
128
}
129
 
130
if ($counter['type']=="LFSR") {
131
    echo "   reg [32:1] polynom;" PHP_EOL;
132
    echo "   integer i;" PHP_EOL PHP_EOL;
133
    echo "   always @ (qi)" PHP_EOL;
134
    echo "   begin
135
        case (length) 
136
         2: polynom = 32'b11;                               // 0x3
137
         3: polynom = 32'b110;                              // 0x6
138
         4: polynom = 32'b1100;                             // 0xC
139
         5: polynom = 32'b10100;                            // 0x14
140
         6: polynom = 32'b110000;                           // 0x30
141
         7: polynom = 32'b1100000;                          // 0x60
142
         8: polynom = 32'b10111000;                         // 0xb8
143
         9: polynom = 32'b100010000;                        // 0x110
144
        10: polynom = 32'b1001000000;                       // 0x240
145
        11: polynom = 32'b10100000000;                      // 0x500
146
        12: polynom = 32'b100000101001;                     // 0x829
147
        13: polynom = 32'b1000000001100;                    // 0x100C
148
        14: polynom = 32'b10000000010101;                   // 0x2015
149
        15: polynom = 32'b110000000000000;                  // 0x6000
150
        16: polynom = 32'b1101000000001000;                 // 0xD008
151
        17: polynom = 32'b10010000000000000;                // 0x12000
152
        18: polynom = 32'b100000010000000000;               // 0x20400
153
        19: polynom = 32'b1000000000000100011;              // 0x40023
154
        20: polynom = 32'b10000010000000000000;             // 0x82000
155
        21: polynom = 32'b101000000000000000000;            // 0x140000
156
        22: polynom = 32'b1100000000000000000000;           // 0x300000
157
        23: polynom = 32'b10000100000000000000000;          // 0x420000
158
        24: polynom = 32'b111000010000000000000000;         // 0xE10000
159
        25: polynom = 32'b1001000000000000000000000;        // 0x1200000
160
        26: polynom = 32'b10000000000000000000100011;       // 0x2000023
161
        27: polynom = 32'b100000000000000000000010011;      // 0x4000013
162
        28: polynom = 32'b1100100000000000000000000000;     // 0xC800000
163
        29: polynom = 32'b10100000000000000000000000000;    // 0x14000000
164
        30: polynom = 32'b100000000000000000000000101001;   // 0x20000029
165
        31: polynom = 32'b1001000000000000000000000000000;  // 0x48000000
166
        32: polynom = 32'b10000000001000000000000000000011; // 0x80200003
167
        default: polynom = 32'b0;
168
        endcase
169
        lfsr_fb = qi[length];
170
        for (i=length-1; i>=1; i=i-1) begin
171
            if (polynom[i])
172
                lfsr_fb = lfsr_fb  ~^ qi[i];
173
        end
174
    end";
175
echo PHP_EOL;
176
}
177
 
178
if ($inputs['rew']!=1) { echo "   assign q_next = "; } else { echo "   assign q_next_fw  = "; }
179
if ($inputs['clear']==1)  { echo " clear ? {length{1'b0}} :";}
180
if ($inputs['set']==1)    { echo " set ? set_value :";}
181
if ($wrap['wrap']==1)     { echo "(qi == wrap_value) ? {length{1'b0}} :";}
182 25 unneback
if ($counter['type']=="LFSR") { echo "{qi[length-1:1],lfsr_fb};"; } else { echo "qi + 1;"; }
183 20 unneback
echo PHP_EOL;
184
 
185
if ($inputs['rew']) {
186
    if ($counter['type']=="LFSR") {
187
            echo "   always @ (qi)" PHP_EOL;
188
    echo "   begin
189
        case (length) 
190
         2: polynom_rew = 32'b11;
191
         3: polynom_rew = 32'b110;
192
         4: polynom_rew = 32'b1100;
193
         5: polynom_rew = 32'b10100;
194
         6: polynom_rew = 32'b110000;
195
         7: polynom_rew = 32'b1100000;
196
         8: polynom_rew = 32'b10111000;
197
         9: polynom_rew = 32'b100010000;
198
        10: polynom_rew = 32'b1001000000;
199
        11: polynom_rew = 32'b10100000000;
200
        12: polynom_rew = 32'b100000101001;
201
        13: polynom_rew = 32'b1000000001100;
202
        14: polynom_rew = 32'b10000000010101;
203
        15: polynom_rew = 32'b110000000000000;
204
        16: polynom_rew = 32'b1101000000001000;
205
        17: polynom_rew = 32'b10010000000000000;
206
        18: polynom_rew = 32'b100000010000000000;
207
        19: polynom_rew = 32'b1000000000000100011;
208
        20: polynom_rew = 32'b10000010000000000000;
209
        21: polynom_rew = 32'b101000000000000000000;
210
        22: polynom_rew = 32'b1100000000000000000000;
211
        23: polynom_rew = 32'b10000100000000000000000;
212
        24: polynom_rew = 32'b111000010000000000000000;
213
        25: polynom_rew = 32'b1001000000000000000000000;
214
        26: polynom_rew = 32'b10000000000000000000100011;
215
        27: polynom_rew = 32'b100000000000000000000010011;
216
        28: polynom_rew = 32'b1100100000000000000000000000;
217
        29: polynom_rew = 32'b10100000000000000000000000000;
218
        30: polynom_rew = 32'b100000000000000000000000101001;
219
        31: polynom_rew = 32'b1001000000000000000000000000000;
220
        32: polynom_rew = 32'b10000000001000000000000000000011;
221
        default: polynom_rew = 32'b0;
222
        endcase
223
        // rotate left
224
        polynom_rew[length:1] = { polynom_rew[length-2:1],polynom_rew[length] };
225
        lfsr_fb_rew = qi[length];
226
        for (i=length-1; i>=1; i=i-1) begin
227
            if (polynom_rew[i])
228
                lfsr_fb_rew = lfsr_fb_rew  ~^ qi[i];
229
        end
230
    end";
231
echo PHP_EOL;
232
 
233
    }
234
    echo "   assign q_next_rew = ";
235
    if ($inputs['clear']==1)  { echo " clear ? clear_value :";}
236
    if ($inputs['set']==1)    { echo " set ? set_value :";}
237
    if ($wrap['wrap']==1)     { echo "(qi == wrap_value) ? {length{1'b0}} :";}
238 25 unneback
    if ($counter['type']=="LFSR") { echo "{lfsr_fb_rew,qi[length:2]};"; } else { echo "qi - 1;"; }
239 20 unneback
    echo PHP_EOL;
240
    echo "   assign q_next = rew ? q_next_rew : q_next_fw;" PHP_EOL;
241
242
 
243
echo "
244
   always @ (posedge clk or posedge rst)
245
     if (rst)
246
       qi <= {length{1'b0}};
247
     else" PHP_EOL;
248
if ($inputs['cke']) { echo "     if (cke)" PHP_EOL;}
249 25 unneback
echo "       qi <= q_next;" PHP_EOL;
250 20 unneback
echo PHP_EOL;
251
 
252
if ($outputs['q']) {
253
    if ($counter['type'] == "GRAY" or $counter['type'] == "gray") {
254 26 unneback
        echo "   always @ (posedge clk or posedge rst)
255 20 unneback
     if (rst)
256 26 unneback
       q <= {length{1'b0}};
257 25 unneback
     else" PHP_EOL;
258
        if ($inputs['cke']) { echo "       if (cke)" PHP_EOL; }
259
        echo "         q <= (q_next>>1) ^ q_next;" PHP_EOL;
260 26 unneback
        if ($outputs['q_bin']) { echo PHP_EOL "   assign q_bin = qi;" PHP_EOL; }
261 20 unneback
    } else {
262
        echo "   assign q = qi;" PHP_EOL;
263
    }
264
}
265
echo PHP_EOL;
266
 
267
if ($outputs['z']) { echo "   assign z = (q == {length{1'b0}});" PHP_EOL; }
268
 
269
if ($outputs['zq']) {
270
    echo "
271
   always @ (posedge clk or posedge rst)
272
     if (rst)
273
       zq <= 1'b1;
274
     else" PHP_EOL;
275
    if ($inputs['cke']) { echo "     if (cke)" PHP_EOL; }
276
    echo "       zq <= q_next == {length{1'b0}};" PHP_EOL;
277
}
278
 
279
if ($outputs['level1']) {
280
    echo "
281
    always @ (posedge clk or posedge rst)
282
    if (rst)
283
        level1 <= 1'b0;
284
    else" PHP_EOL;
285
    if ($inputs['cke']) { echo "    if (cke)" PHP_EOL; }
286
    echo "    if (q_next == level1_value)
287
        level1 <= 1'b1;
288
    else if (q == level1_value & rew)
289
        level1 <= 1'b0;" PHP_EOL;
290
}
291
 
292
if ($outputs['level2']) {
293
    echo "
294
    always @ (posedge clk or posedge rst)
295
    if (rst)
296
        level2 <= 1'b0;
297
    else" PHP_EOL;
298
    if ($inputs['cke']) { echo "    if (cke)" PHP_EOL; }
299
    echo "    if (q_next == level2_value)
300
        level2 <= 1'b1;
301
    else if (q == level2_value & rew)
302
        level2 <= 1'b0;" PHP_EOL;
303
}
304
 
305
echo "endmodule" PHP_EOL;

powered by: WebSVN 2.1.0

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