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 33

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

powered by: WebSVN 2.1.0

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