-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.v
384 lines (353 loc) · 14.3 KB
/
sudoku.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// ----------------------------------------------------------------------
// A Verilog module for a simple divider
//
// Written by Gandhi Puvvada Date: 7/17/98, 2/15/2008, 10/13/08, 2/22/2010
//
// File name: divider_combined_cu_dpu_with_single_step.v
// ------------------------------------------------------------------------
// This file is essentially same as the divider_combined_cu_dpu.v except for
// the SCEN additional input port pin which is used to single step the divider
// in the compute state.
// Note the following lines later in the code:
// COMPUTE :
// if (SCEN) // Notice SCEN
// begin
// ------------------------------------------------------------------------
module SudokuSolver (Prev, Next, Enter, Start, Clk, Reset, InputValue,
Init, Load, Forward, Check, Back, Disp, Fail,
Row, Col, OutputValue, OutputAttempt, Single, OutputFixed);
input Prev, Next, Enter, Start, Clk, Reset, Single;
input [3:0] InputValue;
output Init, Load, Forward, Check, Back, Disp, Fail;
output [3:0] Row, Col;
output [3:0] OutputValue, OutputAttempt;
output OutputFixed;
reg [6:0] state;
reg [3:0] Row, Col, rowNext, colNext, rowPrev, colPrev;
reg [3:0] OutputValue, OutputAttempt;
reg [8:0] inputOneHot;
reg [8:0] sudoku [8:0][8:0];
reg fixed [8:0][8:0];
reg [8:0] attempt, nextAttempt;
localparam
INIT = 7'b0000001,
LOAD = 7'b0000010,
FORWARD = 7'b0000100,
CHECK = 7'b0001000,
BACK = 7'b0010000,
DISP = 7'b0100000,
FAIL = 7'b1000000;
assign {Fail, Disp, Back, Check, Forward, Load, Init} = state;
assign OutputFixed = fixed[Row][Col];
always @(Row, Col)
begin
if(Col == 8)
begin
colNext <= 0;
if(Row == 8)
rowNext <= 0;
else
rowNext <= Row + 1;
end
else
begin
colNext <= Col + 1;
rowNext <= Row;
end
if(Col == 0)
begin
colPrev <= 8;
if(Row == 0)
rowPrev <= 8;
else
rowPrev <= Row - 1;
end
else
begin
colPrev <= Col - 1;
rowPrev <= Row;
end
end
always @ (InputValue)
begin : INPUT_TO_ONE_HOT
case (InputValue)
4'b0001: inputOneHot = 9'b000000001; // 1
4'b0010: inputOneHot = 9'b000000010; // 2
4'b0011: inputOneHot = 9'b000000100; // 3
4'b0100: inputOneHot = 9'b000001000; // 4
4'b0101: inputOneHot = 9'b000010000; // 5
4'b0110: inputOneHot = 9'b000100000; // 6
4'b0111: inputOneHot = 9'b001000000; // 7
4'b1000: inputOneHot = 9'b010000000; // 8
4'b1001: inputOneHot = 9'b100000000; // 9
default: inputOneHot = 9'b000000000; // default to 9'b0
endcase
end
always @ (*)
begin
case (sudoku[Row][Col])
9'b000000001: OutputValue = 4'b0001; // 1
9'b000000010: OutputValue = 4'b0010; // 2
9'b000000100: OutputValue = 4'b0011; // 3
9'b000001000: OutputValue = 4'b0100; // 4
9'b000010000: OutputValue = 4'b0101; // 5
9'b000100000: OutputValue = 4'b0110; // 6
9'b001000000: OutputValue = 4'b0111; // 7
9'b010000000: OutputValue = 4'b1000; // 8
9'b100000000: OutputValue = 4'b1001; // 9
default: OutputValue = 4'b0000; // default to 4'b0
endcase
end
always @ (attempt)
begin
case (attempt)
9'b000000001: OutputAttempt = 4'b0001; // 1
9'b000000010: OutputAttempt = 4'b0010; // 2
9'b000000100: OutputAttempt = 4'b0011; // 3
9'b000001000: OutputAttempt = 4'b0100; // 4
9'b000010000: OutputAttempt = 4'b0101; // 5
9'b000100000: OutputAttempt = 4'b0110; // 6
9'b001000000: OutputAttempt = 4'b0111; // 7
9'b010000000: OutputAttempt = 4'b1000; // 8
9'b100000000: OutputAttempt = 4'b1001; // 9
default: OutputAttempt = 4'b0000; // default to 4'b0
endcase
end
always @ (attempt)
begin : ONE_HOT_INCREMENTER
case (attempt)
9'b000000001: nextAttempt = 9'b000000010; // 1 + 1 = 2
9'b000000010: nextAttempt = 9'b000000100; // 2 + 1 = 3
9'b000000100: nextAttempt = 9'b000001000; // 3 + 1 = 4
9'b000001000: nextAttempt = 9'b000010000; // 4 + 1 = 5
9'b000010000: nextAttempt = 9'b000100000; // 5 + 1 = 6
9'b000100000: nextAttempt = 9'b001000000; // 6 + 1 = 7
9'b001000000: nextAttempt = 9'b010000000; // 7 + 1 = 8
9'b010000000: nextAttempt = 9'b100000000; // 8 + 1 = 9
default: nextAttempt = 9'bxxxxxxxxx;
endcase
end
always @(posedge Clk, posedge Reset)
begin : CU_n_DU
if (Reset)
begin : RESET_STATE_MACHINE
reg [3:0] i, j;
state <= INIT;
Row <= 4'bxxxx;
Col <= 4'bxxxx;
rowNext <= 4'bxxxx;
colNext <= 4'bxxxx;
rowPrev <= 4'bxxxx;
colPrev <= 4'bxxxx;
OutputValue <= 4'bxxxx;
OutputAttempt <= 4'bxxxx;
inputOneHot <= 9'bxxxxxxxxx;
attempt <= 9'bxxxxxxxxx;
nextAttempt <= 9'bxxxxxxxxx;
for (i = 0; i <= 8; i = i + 1)
begin
for (j = 0; j <= 8; j = j + 1)
begin
sudoku[i][j] <= 9'bxxxxxxxxx;
fixed[i][j] <= 1'bx;
end
end
end
else
begin
(* full_case, parallel_case *)
case (state)
INIT:
begin: INIT_STATE
reg [3:0] i, j;
// state transitions in the control unit
state <= LOAD;
// RTL operations in the Data Path
Col <= 0;
Row <= 0;
for (i = 0; i <= 8; i = i + 1)
begin
for (j = 0; j <= 8; j = j + 1)
begin
sudoku[i][j] <= 0;
fixed[i][j] <= 0;
end
end
end
LOAD:
begin
// state transition
if(Start)
state <= FORWARD;
// DPU
if(Next)
begin
Row <= rowNext;
Col <= colNext;
end
if(Prev)
begin
Row <= rowPrev;
Col <= colPrev;
end
if(Enter)
begin
sudoku[Row][Col] <= inputOneHot;
if(inputOneHot == 9'b0)
fixed[Row][Col] <= 1'b0;
else
fixed[Row][Col] <= 1'b1;
end
if(Start)
begin
Row <= 0;
Col <= 0;
end
end
FORWARD:
begin
// state transition
if(fixed[Row][Col] == 1'b0 && fixed[Row][Col] == 1'b0)
state <= CHECK;
if(fixed[Row][Col] == 1'b1 && Row == 8 && Col == 8)
state <= DISP;
// DPU
if(fixed[Row][Col] == 1'b1)
begin
Row <= rowNext;
Col <= colNext;
end
else
attempt <= 9'b000000001;
if(fixed[Row][Col] == 1'b1 && Row == 8 && Col == 8)
begin
Row <= 0;
Col <= 0;
end
end
CHECK:
begin: VALIDATE_ATTEMPT
reg isValid;
reg [3:0] i, j;
reg [8:0] rowMask, colMask;
isValid = 1'b1;
for(i = 0; i < 9; i = i + 1)
begin
if(sudoku[Row][i] & attempt || sudoku[i][Col] & attempt)
isValid = 1'b0;
end
if(Row < 3)
rowMask = 9'b000000111;
else if(Row < 6)
rowMask = 9'b000111000;
else
rowMask = 9'b111000000;
if(Col < 3)
colMask = 9'b000000111;
else if(Col < 6)
colMask = 9'b000111000;
else
colMask = 9'b111000000;
for(i = 0; i < 9; i = i + 1)
begin
for(j = 0; j < 9; j = j + 1)
begin
if(rowMask[i] && colMask[j] && (sudoku[i][j] & attempt))
isValid = 1'b0;
end
end
// state transition
if(!Single || Enter)
begin
if(isValid && !(Row == 8 && Col == 8))
state <= FORWARD;
if(isValid && Row == 8 && Col == 8)
state <= DISP;
end
if(!isValid && attempt[8]) // last attempt
begin
if(Row == 0 && Col == 0) // cannot go back anymore
state <= FAIL;
else
state <= BACK;
end
// DPU
if(!Single || Enter)
begin
if(isValid)
sudoku[Row][Col] <= attempt;
if(isValid && !(Row == 8 && Col == 8))
begin
Row <= rowNext;
Col <= colNext;
end
if(isValid && Row == 8 && Col == 8)
begin
Row <= 0;
Col <= 0 ;
end
end
if(!isValid && attempt[8]) // prepare for back track
begin
Row <= rowPrev;
Col <= colPrev;
attempt <= sudoku[rowPrev][colPrev]; // load the value in previous location to increment
end
if(!isValid && !attempt[8]) // try next number
attempt <= nextAttempt;
end
BACK:
begin: BACKTRACK
reg usable;
// we can update a location if it is not fixed and its value is not 9
if(fixed[Row][Col] == 1'b0 && sudoku[Row][Col] != 9'b100000000)
usable = 1'b1;
else
usable = 1'b0;
// state transition
if(usable) // if not fixed and currently not at 9
state <= CHECK;
// if at first location and it is fixed or it has value 9 then there are no soltion
if(Row == 0 && Col == 0 && !usable)
state <= FAIL;
// DPU
if(fixed[Row][Col] == 1'b0)
sudoku[Row][Col] <= 9'b0;
if(usable)
begin
attempt <= nextAttempt; // increment the attempt value by one
end
else // we need to backtrack further
begin
Row <= rowPrev;
Col <= colPrev;
attempt <= sudoku[rowPrev][colPrev]; // load the value in previous location to increment
end
end
DISP:
begin
// state transition
if(Start)
state <= INIT;
// DPU
if (Next)
begin
Row <= rowNext;
Col <= colNext;
end
if (Prev)
begin
Row <= rowPrev;
Col <= colPrev;
end
end
FAIL:
begin
// state transition
if(Start)
state <= INIT;
end
endcase
end
end
endmodule // SudokuSolver