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

Subversion Repositories sudoku

[/] [sudoku/] [trunk/] [parameterized_rtl/] [minPiece.v] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 dsheffie
module minPiece(/*AUTOARG*/
2
   // Outputs
3
   minPoss, minIdx,
4
   // Inputs
5
   clk, rst, inGrid
6
   );
7
 
8
   function integer my_clog2;
9
   input integer value;
10
   begin
11
      value = value-1;
12
      for (my_clog2=0; value>0; my_clog2=my_clog2+1)
13
        value = value>>1;
14
   end
15
   endfunction
16
 
17
   parameter DIM = 3;
18
   localparam DIM_S = DIM*DIM;
19
   localparam DIM_Q = DIM_S*DIM_S;
20
 
21
   localparam LG_DIM_S = my_clog2(DIM_S);
22
   localparam LG_DIM_Q = my_clog2(DIM_Q);
23
 
24
   localparam PAD_WIDTH = (1<<LG_DIM_Q);
25
   localparam PAD = PAD_WIDTH - DIM_Q;
26
 
27
 
28
   input clk;
29
   input rst;
30
 
31
   input [(DIM_S*DIM_S*DIM_S -1):0] inGrid;
32
 
33
   output [(LG_DIM_S-1):0]           minPoss;
34
   output [(LG_DIM_Q-1):0]           minIdx;
35
 
36
   reg [(LG_DIM_S-1):0]      r_minPoss;
37
   reg [(LG_DIM_Q-1):0]      r_minIdx;
38
 
39
   assign minPoss = r_minPoss;
40
   assign minIdx = r_minIdx;
41
 
42
   wire [(DIM_S-1):0] grid2d [(DIM_Q-1):0];
43
 
44
   wire [(LG_DIM_S-1):0] w_reduce_poss [(LG_DIM_Q):0][(PAD_WIDTH-1):0];
45
   wire [(LG_DIM_Q-1):0] w_reduce_indices [(LG_DIM_Q):0][(PAD_WIDTH-1):0];
46
 
47
   genvar        i,j;
48
 
49
   /* unflatten */
50
   generate
51
      for(i=0;i<PAD_WIDTH;i=i+1)
52
        begin: unflatten
53
           if(i < DIM_Q)
54
             begin
55
                assign grid2d[i] = inGrid[(DIM_S*(i+1))-1:DIM_S*i];
56
                countPoss #(.DIM(DIM)) cP1 (.clk(clk), .rst(rst), .in(grid2d[i]), .out(w_reduce_poss[0][i]));
57
                assign w_reduce_indices[0][i] = i;
58
             end
59
           else
60
             begin
61
                assign w_reduce_poss[0][i] = ~0;
62
                assign w_reduce_indices[0][i] = i;
63
             end // else: !if(i < DIM_Q)
64
           //assign w_reduce_indices[0][i] = i;
65
        end
66
   endgenerate
67
 
68
   localparam RIDX=LG_DIM_Q;
69
   generate
70
      for(i=1;i<(LG_DIM_Q+1);i=i+1)
71
        begin: level_reduce
72
           for(j=0;j<(1<<(LG_DIM_Q-i));j=j+1)
73
             begin: elem_reduce
74
                cmpPiece #(.DIM(DIM)) cmpr
75
                 (
76
                  .outPoss(w_reduce_poss[i][j]),
77
                  .outIdx(w_reduce_indices[i][j]),
78
                  .inPoss_0(w_reduce_poss[i-1][2*j]),
79
                  .inIdx_0(w_reduce_indices[i-1][2*j]),
80
                  .inPoss_1(w_reduce_poss[i-1][2*j+1]),
81
                  .inIdx_1(w_reduce_indices[i-1][2*j+1])
82
                  );
83
             end
84
        end
85
   endgenerate
86
 
87
   always@(posedge clk)
88
     begin
89
        if(rst)
90
          begin
91
             r_minIdx <= 0;
92
             r_minPoss <= (~0);
93
          end
94
        else
95
          begin
96
             r_minIdx <= w_reduce_indices[RIDX][0];
97
             r_minPoss <= w_reduce_poss[RIDX][0];
98
          end
99
     end // always@ (posedge clk)
100
 
101
endmodule // minPiece
102
 
103
 
104
module cmpPiece(/*AUTOARG*/
105
   // Outputs
106
   outPoss, outIdx,
107
   // Inputs
108
   inPoss_0, inIdx_0, inPoss_1, inIdx_1
109
   );
110
 
111
   function integer my_clog2;
112
      input integer value;
113
      begin
114
         value = value-1;
115
         for (my_clog2=0; value>0; my_clog2=my_clog2+1)
116
           value = value>>1;
117
      end
118
   endfunction // for
119
 
120
   parameter DIM=3;
121
   localparam DIM_S = DIM*DIM;
122
   localparam DIM_Q = DIM_S*DIM_S;
123
   localparam LG_DIM_S = my_clog2(DIM_S);
124
   localparam LG_DIM_Q = my_clog2(DIM_Q);
125
 
126
   input [(LG_DIM_S-1):0] inPoss_0;
127
   input [(LG_DIM_Q-1):0] inIdx_0;
128
 
129
   input [(LG_DIM_S-1):0] inPoss_1;
130
   input [(LG_DIM_Q-1):0] inIdx_1;
131
 
132
   output [(LG_DIM_S-1):0] outPoss;
133
   output [(LG_DIM_Q-1):0] outIdx;
134
 
135
   wire         w_cmp = (inPoss_0 < inPoss_1);
136
 
137
   assign outPoss = w_cmp ? inPoss_0 : inPoss_1;
138
   assign outIdx = w_cmp ? inIdx_0 : inIdx_1;
139
 
140
endmodule // cmpPiece
141
 
142
module countPoss(clk,rst,in,out);
143
   function integer my_clog2;
144
      input integer value;
145
      begin
146
         value = value-1;
147
         for (my_clog2=0; value>0; my_clog2=my_clog2+1)
148
           value = value>>1;
149
      end
150
   endfunction // for
151
 
152
   parameter DIM=3;
153
   localparam DIM_S = DIM*DIM;
154
   localparam DIM_Q = DIM_S*DIM_S;
155
 
156
   localparam LG_DIM_S = my_clog2(DIM_S);
157
   localparam LG_DIM_Q = my_clog2(DIM_Q);
158
 
159
   localparam PAD = (1<<LG_DIM_S) - DIM_S;
160
   localparam PAD_WIDTH = (1<<LG_DIM_S);
161
 
162
   input [(DIM_S-1):0] in;
163
   input               clk;
164
   input               rst;
165
 
166
   output [(LG_DIM_S-1):0] out;
167
   reg [(LG_DIM_S-1):0]    r_out;
168
   assign out = r_out;
169
 
170
 
171
   wire [(PAD_WIDTH-1):0]  w_pad_in;
172
   generate
173
      begin: padding
174
         if(PAD > 0)
175
           begin
176
              assign w_pad_in = { {PAD{1'b0}}, in};
177
           end
178
         else
179
           begin
180
              assign w_pad_in = in;
181
           end
182
      end
183
   endgenerate
184
 
185
   wire [(LG_DIM_S-1):0]   w_cnt;
186
 
187
   ones_count #(.LG_IN_WIDTH(LG_DIM_S)) c0 (.in(w_pad_in), .out(w_cnt));
188
 
189
   wire [(LG_DIM_S-1):0]   w_out = (w_cnt == 'd1) ? ~0 : w_cnt;
190
 
191
   always@(posedge clk)
192
     begin
193
        r_out <= rst ? ~0 : w_out;
194
     end
195
 
196
endmodule // countPoss
197
 

powered by: WebSVN 2.1.0

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