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 22

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

powered by: WebSVN 2.1.0

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