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

Subversion Repositories othellogame

[/] [othellogame/] [trunk/] [rtl/] [board_move.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 marius_mtm
`timescale 1ns / 1ps
2
//////////////////////////////////////////////////////////////////////////////////
3
// Company: 
4
// Engineer: 
5
// 
6
// Create Date:    18:14:24 03/08/2009 
7
// Design Name: 
8
// Module Name:    board_move 
9
// Project Name:   The FPGA Othello Game
10
// Target Devices: spartan3E
11
// Tool versions: 
12
// Description: 
13
//
14
// Dependencies: 
15
//
16
// Revision: 
17
// Revision 0.01 - File Created
18
// Additional Comments: 
19
//
20
//////////////////////////////////////////////////////////////////////////////////
21
module board_move(clk, B, R, X, Y, player, MB, MR, RST);
22
/* Input: Othello 64bit board R - red player, B - blue player*/
23
input [63:0] R;
24
input [63:0] B;
25
 
26
/* Input: clock signal */
27
input clk;
28
 
29
/* Input: reset signal: initializes FSM adn outputs */
30
input RST;
31
 
32
/* Input: X, Y coordinates in Othello board 8x8 */
33
input [2:0]  X;
34
input [2:0]  Y;
35
 
36
/* Input: current player color RED or BLUE */
37
input player;
38
 
39
/* Output: New 64bit Othello board for BLUE player (MB) and for RED player (MR)*/
40
output [63:0] MB;
41
output [63:0] MR;
42
 
43
/* internal: flip-flops */
44
/*
45
reg [7:0] oponent_ff_d[7:0];
46
reg [7:0] current_ff_d[7:0];
47
 
48
reg [7:0] oponent_ff_q[7:0];
49
reg [7:0] current_ff_q[7:0];
50
*/
51
 
52
reg [63:0] oponent_ff_d;
53
reg [63:0] current_ff_d;
54
 
55
reg [63:0] oponent_ff_q;
56
reg [63:0] current_ff_q;
57
 
58
 
59
reg [2:0] countx_ff_d;
60
reg [2:0] countx_ff_q;
61
 
62
reg [2:0] county_ff_d;
63
reg [2:0] county_ff_q;
64
 
65
reg [2:0] count2x_ff_d;
66
reg [2:0] count2x_ff_q;
67
 
68
reg [2:0] count2y_ff_d;
69
reg [2:0] count2y_ff_q;
70
 
71
/* internal: FSM state ff */
72
reg [1:0] state_ff_q;
73
reg [1:0] state_ff_d;
74
 
75
reg [1:0] state2_ff_q;
76
reg [1:0] state2_ff_d;
77
 
78
/* internal: FSM states */
79
parameter RESET  = 2'b00;
80
parameter FLIP   = 2'b01;
81
parameter WRONG  = 2'b10;
82
parameter FINISH = 2'b11;
83
 
84
/* constants */
85
parameter DISC_ON  = 1'b1;
86
parameter DISC_OFF = 1'b0;
87
parameter LIMIT    = 3'd7;
88
parameter NO_DISC  = 1'b0;
89
parameter RED            = 1'b1;
90
 
91
/* FSM Output logic */
92
always @(state_ff_q or countx_ff_q or county_ff_q)
93
   begin
94
                /* oponent */
95
                oponent_ff_d = oponent_ff_q;
96
 
97
                /* current player */
98
                current_ff_d = current_ff_q;
99
 
100
                case (state_ff_q)
101
 
102
                        /* FLIP state: flip the pieces */
103
                        FLIP: begin
104
                                                /* oponent will remain with no disc on that position */
105
                                                oponent_ff_d[county_ff_q*8 + countx_ff_q] = DISC_OFF;
106
                                                /* disc is ours */
107
                                                current_ff_d[county_ff_q*8 + countx_ff_q] = DISC_ON;
108
                                        end
109
 
110
                        /* WRONG state: flip back the pieces */
111
                        WRONG: begin
112
                                                /* give the disc back to oponent */
113
                                                oponent_ff_d[county_ff_q*8 + countx_ff_q] = DISC_ON;
114
                                                current_ff_d[county_ff_q*8 + countx_ff_q] = DISC_OFF;
115
                                         end
116
 
117
                        /* mantain the outputs */
118
                        default: begin
119
                                                        oponent_ff_d = oponent_ff_q;
120
                                                        current_ff_d = current_ff_q;
121
                                                end
122
 
123
                endcase
124
 
125
                case (state2_ff_q)
126
 
127
                        /* FLIP state: flip the pieces */
128
                        FLIP: begin
129
                                                /* oponent will remain with no disc on that position */
130
                                                oponent_ff_d[count2y_ff_q*8 + count2x_ff_q] = DISC_OFF;
131
                                                /* disc is ours */
132
                                                current_ff_d[count2y_ff_q*8 + count2x_ff_q] = DISC_ON;
133
                                        end
134
 
135
                        /* WRONG state: flip back the pieces */
136
                        WRONG: begin
137
                                                /* give the disc back to oponent */
138
                                                oponent_ff_d[count2y_ff_q*8 + count2x_ff_q] = DISC_ON;
139
                                                current_ff_d[count2y_ff_q*8 + count2x_ff_q] = DISC_OFF;
140
                                         end
141
 
142
                        /* mantain the outputs */
143
                        default: begin
144
                                                        oponent_ff_d = oponent_ff_q;
145
                                                        current_ff_d = current_ff_q;
146
                                                end
147
 
148
                endcase
149
 
150
        end
151
 
152
 
153
/* FSM next state logic */
154
always @(state_ff_q or countx_ff_q or county_ff_q)
155
begin
156
 
157
case (state_ff_q)
158
         /* RESET state, prepare to flip the discs */
159
         RESET: begin
160
                             if ( (oponent_ff_q[county_ff_q*8 + countx_ff_q] | current_ff_q[county_ff_q*8 + countx_ff_q]) == NO_DISC )
161
                                  /* if the square is empty */
162
                                  begin
163
                                                if ( countx_ff_q == LIMIT )
164
                                                /* if this is the last square, nothing to flip on this direction */
165
                                                begin
166
                                                        state_ff_d = FINISH;
167
                                                        countx_ff_d = countx_ff_q;
168
                                                        county_ff_d = county_ff_q;
169
                                                end
170
                                                else begin
171
                                                        if ( oponent_ff_q[county_ff_q*8 + countx_ff_q + 1] == DISC_ON )
172
                                                        /* check if there is an oponent disc */
173
                                                        begin
174
                                                                /* yes, go to FLIP state, and increment X */
175
                                                                state_ff_d = FLIP;
176
                                                                countx_ff_d = countx_ff_q + 1;
177
                                                                county_ff_d = county_ff_q;
178
                                                        end
179
                                                        else begin
180
                                                                /* if no, means that we have nothing to flip => FINISH */
181
                                                                state_ff_d = FINISH;
182
                                                                countx_ff_d = countx_ff_q;
183
                                                                county_ff_d = county_ff_q;
184
                                                        end
185
                                                end
186
                                  end
187
                                  else begin
188
                                                /* error: should happen */
189
                                                state_ff_d = RESET;
190
                                                countx_ff_d = countx_ff_q;
191
                                                county_ff_d = county_ff_q;
192
                                  end
193
                          end
194
         /* FLIP state: in this state we flip the discs, and see if we still have to flip or we are finished */
195
    FLIP: begin
196
                                 if (countx_ff_q == LIMIT) begin
197
                                                        /* if we are here, it means that we have no disc to flanc with */
198
                                                        state_ff_d = WRONG;
199
                                                        countx_ff_d = countx_ff_q;
200
                                                        county_ff_d = county_ff_q;
201
                                 end
202
             else begin
203
                                        if ( oponent_ff_q[county_ff_q*8 + countx_ff_q + 1] == DISC_ON )
204
                                        /* if next to us is an oponent disc, mantain FLIP */
205
                                        begin
206
                                                state_ff_d = state_ff_q;
207
                                                countx_ff_d = countx_ff_q + 1;
208
                                                county_ff_d = county_ff_q;
209
                                        end
210
                                        else begin
211
                                                if ( current_ff_q[county_ff_q*8 + countx_ff_q + 1] == DISC_ON )
212
                                                /* if it's our color the disc next to us, we can flanc all the oponent discs, so we are finish */
213
                                                begin
214
                                                        state_ff_d = FINISH;
215
                                                        countx_ff_d = countx_ff_q;
216
                                                        county_ff_d = county_ff_q;
217
                                                end
218
                                                else begin
219
                                                        /* if it's niether our disc or oponent's disc, means square is empty, so nothing to flip  */
220
                                                        state_ff_d = WRONG;
221
                                                        countx_ff_d = countx_ff_q;
222
                                                        county_ff_d = county_ff_q;
223
                                                end
224
                                        end
225
                                 end
226
                          end
227
         /* WRONG state: we flip the discs back in this state */
228
         WRONG:
229
                         begin
230
                                if (countx_ff_q - 1  == X)
231
                                /* check if we are finished */
232
                                begin
233
                                        state_ff_d = FINISH;
234
                                        countx_ff_d = countx_ff_q;
235
                                        county_ff_d = county_ff_q;
236
                                end
237
                                else begin
238
                                /* we mantain the state, keep flipping back */
239
                                   state_ff_d = WRONG;
240
                                        countx_ff_d = countx_ff_q - 1;
241
                                        county_ff_d = county_ff_q;
242
                                end
243
                         end
244
 
245
    /* default state, mantain counters and state */
246
         default:
247
             begin
248
                        state_ff_d = state_ff_q;
249
                        countx_ff_d = countx_ff_q;
250
                        county_ff_d = county_ff_q;
251
                  end
252
endcase
253
end
254
 
255
/* FSM next state logic */
256
always @(state2_ff_q or count2x_ff_q or count2y_ff_q)
257
begin
258
 
259
case (state2_ff_q)
260
         /* RESET state, prepare to flip the discs */
261
         RESET: begin
262
                             if ( (oponent_ff_q[count2y_ff_q*8 + count2x_ff_q] | current_ff_q[count2y_ff_q*8 + count2x_ff_q]) == NO_DISC )
263
                                  /* if the square is empty */
264
                                  begin
265
                                                if ( count2x_ff_q == LIMIT )
266
                                                /* if this is the last square, nothing to flip on this direction */
267
                                                begin
268
                                                        state2_ff_d = FINISH;
269
                                                        count2x_ff_d = count2x_ff_q;
270
                                                        count2y_ff_d = count2y_ff_q;
271
                                                end
272
                                                else begin
273
                                                        if ( oponent_ff_q[count2y_ff_q*8 + count2x_ff_q - 1] == DISC_ON )
274
                                                        /* check if there is an oponent disc */
275
                                                        begin
276
                                                                /* yes, go to FLIP state, and increment X */
277
                                                                state2_ff_d = FLIP;
278
                                                                count2x_ff_d = count2x_ff_q - 1;
279
                                                                count2y_ff_d = count2y_ff_q;
280
                                                        end
281
                                                        else begin
282
                                                                /* if no, means that we have nothing to flip => FINISH */
283
                                                                state2_ff_d = FINISH;
284
                                                                count2x_ff_d = count2x_ff_q;
285
                                                                count2y_ff_d = count2y_ff_q;
286
                                                        end
287
                                                end
288
                                  end
289
                                  else begin
290
                                                /* error: should happen */
291
                                                state2_ff_d = RESET;
292
                                                count2x_ff_d = count2x_ff_q;
293
                                                count2y_ff_d = count2y_ff_q;
294
                                  end
295
                          end
296
         /* FLIP state: in this state we flip the discs, and see if we still have to flip or we are finished */
297
    FLIP: begin
298
                                 if (count2x_ff_q == 3'b0) begin
299
                                                        /* if we are here, it means that we have no disc to flanc with */
300
                                                        state2_ff_d = WRONG;
301
                                                        count2x_ff_d = count2x_ff_q;
302
                                                        count2y_ff_d = count2y_ff_q;
303
                                 end
304
             else begin
305
                                        if ( oponent_ff_q[count2y_ff_q*8 + count2x_ff_q - 1] == DISC_ON )
306
                                        /* if next to us is an oponent disc, mantain FLIP */
307
                                        begin
308
                                                state2_ff_d = state2_ff_q;
309
                                                count2x_ff_d = count2x_ff_q + 1;
310
                                                count2y_ff_d = count2y_ff_q;
311
                                        end
312
                                        else begin
313
                                                if ( current_ff_q[count2y_ff_q*8 + count2x_ff_q - 1] == DISC_ON )
314
                                                /* if it's our color the disc next to us, we can flanc all the oponent discs, so we are finish */
315
                                                begin
316
                                                        state2_ff_d = FINISH;
317
                                                        count2x_ff_d = count2x_ff_q;
318
                                                        count2y_ff_d = count2y_ff_q;
319
                                                end
320
                                                else begin
321
                                                        /* if it's niether our disc or oponent's disc, means square is empty, so nothing to flip  */
322
                                                        state2_ff_d = WRONG;
323
                                                        count2x_ff_d = count2x_ff_q;
324
                                                        count2y_ff_d = count2y_ff_q;
325
                                                end
326
                                        end
327
                                 end
328
                          end
329
         /* WRONG state: we flip the discs back in this state */
330
         WRONG:
331
                         begin
332
                                if (count2x_ff_q + 1  == X)
333
                                /* check if we are finished */
334
                                begin
335
                                        state2_ff_d = FINISH;
336
                                        count2x_ff_d = count2x_ff_q;
337
                                        count2y_ff_d = count2y_ff_q;
338
                                end
339
                                else begin
340
                                /* we mantain the state, keep flipping back */
341
                                   state2_ff_d = WRONG;
342
                                        count2x_ff_d = count2x_ff_q + 1;
343
                                        count2y_ff_d = count2y_ff_q;
344
                                end
345
                         end
346
 
347
    /* default state, mantain counters and state */
348
         default:
349
             begin
350
                        state2_ff_d = state2_ff_q;
351
                        count2x_ff_d = count2x_ff_q;
352
                        count2y_ff_d = count2y_ff_q;
353
                  end
354
endcase
355
end
356
 
357
 
358
 
359
/* clocked procedure */
360
always @(posedge clk)
361
begin
362
        if (RST)
363
        /* reset signal */
364
        begin
365
                 state_ff_q <= RESET;
366
                 state2_ff_q <= RESET;
367
       /* we prepare the board */
368
                 current_ff_q <= B;
369
                 oponent_ff_q <= R;
370
 
371
                 /* if second player is the current one, switch the matrices */
372
                 if ( player == RED ) begin
373
                         current_ff_q <= R;
374
                         oponent_ff_q <= B;
375
 
376
 
377
                 end
378
 
379
                /* prepare the counters (directions) */
380
                countx_ff_q <= X;
381
                county_ff_q <= Y;
382
 
383
                count2x_ff_q <= X;
384
                count2y_ff_q <= Y;
385
 
386
        end
387
        else begin
388
           /* go to next state */
389
                state_ff_q  <= state_ff_d;
390
 
391
                /* go to next square */
392
                countx_ff_q <= countx_ff_d;
393
                county_ff_q <= county_ff_d;
394
 
395
                state2_ff_q  <= state2_ff_d;
396
 
397
                /* go to next square */
398
                count2x_ff_q <= count2x_ff_d;
399
                count2y_ff_q <= count2y_ff_d;
400
 
401
 
402
                /* validate outputs */
403
                oponent_ff_q <= oponent_ff_d;
404
                current_ff_q <= current_ff_d;
405
 
406
        end
407
end
408
 
409
 
410
/* continuous assignments, output Othello board 128bit */
411
assign MB = current_ff_q;
412
assign MR = oponent_ff_q;
413
 
414
endmodule

powered by: WebSVN 2.1.0

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