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

Subversion Repositories othellogame

[/] [othellogame/] [trunk/] [rtl/] [reversi.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:    23:08:07 05/13/2009 
7
// Design Name:    glue logic for reversi game.
8
// Module Name:    reversi 
9
// Project Name:   The FPGA Othello Game
10
// Target Devices: Spartan3E
11
// Tool versions: 
12
// Description: 
13
//
14
// Dependencies: all
15
//
16
// Revision: 
17
// Revision 0.01 - File Created
18
// Additional Comments: 
19
//     Marius TIVADAR
20
//
21
//////////////////////////////////////////////////////////////////////////////////
22
module reversi(
23
    input  clk,                         // global clock
24
    input  RST,                         // global RESET
25
         output vga_h_sync,             // H sync out
26
         output vga_v_sync,             // V sync out
27
         output vga_R,                  // vga R
28
         output vga_G,                  // vga G
29
         output vga_B,                  // vga B
30
         input  west,                           // pushbutton west
31
         input  north,                          // pushbutton north
32
         input  east,                           // pushbutton east
33
         input  south,                          // pushbutton south
34
         input  knob,                           // pushbutton knob
35
         output pass_led,                       // player has to pass LED
36
         output gameover_led,   // gameover LED
37
         output thinking_led,   // thinking LED
38
         output TxD                                     // RS232 Tx
39
    );
40
 
41
 
42
 
43
 
44
   wire [9:0] cntX;
45
  wire [8:0] cntY;
46
  wire inWin;
47
  wire ok;
48
 
49
  reg [31:0] time_cnt_d;
50
  reg [31:0] time_cnt_q;
51
 
52
  reg [31:0] think_time_q;
53
  reg [31:0] think_time_d;
54
 
55
  reg [0:0] nodes_sent_d;
56
  reg [0:0] nodes_sent_q;
57
 
58
  wire [63:0] M;
59
  wire [0:0] enter_btn;
60
 
61
 
62
 
63
 
64
 wire [2:0] X;
65
 wire [2:0] Y;
66
 
67
 
68
 reg [63:0] board_R_d;
69
 reg [63:0] board_R_q;
70
 
71
 reg [63:0] board_B_d;
72
 reg [63:0] board_B_q;
73
 
74
 reg go_d;
75
 reg go_q;
76
 
77
 reg pl_d;
78
 reg pl_q;
79
 
80
 reg [3:0] state_d;
81
 reg [3:0] state_q;
82
 
83
 reg [2:0] move_x_d;
84
 reg [2:0] move_x_q;
85
 
86
 reg [2:0] move_y_d;
87
 reg [2:0] move_y_q;
88
 
89
 wire [2:0] bmove_in_x;
90
 wire [2:0] bmove_in_y;
91
 
92
 wire [63:0] bmove_out_Rw;
93
 wire [63:0] bmove_out_Bw;
94
 
95
 wire [63:0] bmove_in_Rw;
96
 wire [63:0] bmove_in_Bw;
97
 
98
 
99
 wire [63:0] gai_out_Rw;
100
 wire [63:0] gai_out_Bw;
101
 
102
 wire [2:0] gai_out_x;
103
 wire [2:0] gai_out_y;
104
 
105
 wire [2:0] gai_out_best_x;
106
 wire [2:0] gai_out_best_y;
107
 
108
 // DEBUG
109
 wire [31:0] dbg_node_cnt_w;
110
 
111
 wire gai_out_pl;
112
 
113
 wire done;
114
 wire pass;
115
 wire thinking;
116
 wire gameover;
117
 wire ai_pass;
118
 wire TxD_busy;
119
 
120
 wire [19:0] dbg_w;
121
 
122
 
123
 
124
 parameter HUMAN            = 4'b0000;
125
 parameter HUMAN_MOVE            = 4'b0001;
126
 parameter HUMAN_MOVE_WAIT  = 4'b0010;
127
 parameter START_AI         = 4'b0011;
128
 parameter AI               = 4'b0101;
129
 parameter MOVE_BEST_WAIT   = 4'b0110;
130
 parameter MOVE_BEST        = 4'b0111;
131
 parameter RS232_0                       = 4'b1000;
132
 parameter RS232_1                       = 4'b1001;
133
 parameter RS232_00         = 4'b1010;
134
 
135
 
136
 assign pass = (M[63:0] == 64'h0);
137
 assign pass_led = pass;
138
 assign gameover = ( ai_pass && pass );//~(board_R_q | board_B_q) == 64'b0);
139
 assign gameover_led = gameover;
140
 assign thinking_led = thinking;
141
 
142
 assign bmove_in_Rw     = ((state_q == AI) || (state_q == START_AI)) ? gai_out_Rw : board_R_q;
143
 assign bmove_in_Bw     = ((state_q == AI) || (state_q == START_AI)) ? gai_out_Bw : board_B_q;
144
 assign bmove_in_player = ((state_q == AI) || (state_q == START_AI)) ? gai_out_pl : pl_q;
145
 
146
 assign bmove_in_x = ((state_q == AI) || (state_q == START_AI)) ? gai_out_x : move_x_q;
147
 assign bmove_in_y = ((state_q == AI) || (state_q == START_AI)) ? gai_out_y : move_y_q;
148
 
149
 moves_map mm(.clk(clk),
150
                                  .RST(RST),
151
                                  .R_(board_R_q),
152
                                  .B_(board_B_q),
153
                                  .M_(M),
154
                                  .player(pl_q)
155
                                  );
156
 
157
 b_move bm   (.clk(clk),
158
                                  .RST(RST),
159
                                  .B_(bmove_in_Bw),
160
                                  .R_(bmove_in_Rw),
161
                                  .X(bmove_in_x),
162
                                  .Y(bmove_in_y),
163
                                  .R_OUT(bmove_out_Rw),
164
                                  .B_OUT(bmove_out_Bw),
165
                                  .player(bmove_in_player)
166
                                  );
167
 
168
 game_ai gai (.clk(clk),
169
                                  .RST(RST),
170
                                  .go(go_q),
171
                                  .init_red(board_R_q),
172
                                  .init_blue(board_B_q),
173
                                  .red_in(bmove_out_Rw),
174
                                  .blue_in(bmove_out_Bw),
175
                                  .n_blue(gai_out_Bw),
176
                                  .n_red(gai_out_Rw),
177
                                  .o_pl(gai_out_pl),
178
                                  .m_x(gai_out_x),
179
                                  .m_y(gai_out_y),
180
                                  .bestX(gai_out_best_x),
181
                                  .bestY(gai_out_best_y),
182
                             .thinking(thinking),
183
                                  .ai_pass(ai_pass),
184
                                  .done(done),
185
                                  .dbg_heur(dbg_w),
186
                                  .dbg_node_cnt(dbg_node_cnt_w)
187
                                  );
188
 
189
 xy_calculate xy(.clk(clk),
190
                                          .RST(RST),
191
                                          .enter(enter_btn),
192
                                          .X(X),
193
                                          .Y(Y),
194
                                          .east(east),
195
                                          .west(west),
196
                                          .north(north),
197
                                          .south(south),
198
                                          .knob(knob)
199
                                          );
200
 
201
 time_analysis ta(
202
                                                .clk(clk),
203
                                                .RST(RST),
204
                                                .start(state_q == RS232_0),
205
                                                .time_cnt(time_cnt_q),
206
                                                .busy(TxD_busy),
207
                                                .TxD(TxD)
208
                                         );
209
 
210
 
211
 vga_controller vga_ctrl(.clk(clk),
212
                                                                 .vga_h_sync(vga_h_sync),
213
                                                                 .vga_v_sync(vga_v_sync),
214
                                                                 .vga_R(vga_R),
215
                                                                 .vga_G(vga_G),
216
                                                                 .vga_B(vga_B),
217
                                                                 .boardR(board_R_q),
218
                                                                 .boardB(board_B_q),
219
                                                                 .boardM(M),
220
                                                                 .coordX(X),
221
                                                                 .coordY(Y)
222
                                                                );
223
 
224
 always @( * )
225
 begin
226
   move_x_d = move_x_q;
227
        move_y_d = move_y_q;
228
        state_d  = state_q;
229
        board_R_d = board_R_q;
230
        board_B_d = board_B_q;
231
        pl_d      = pl_q;
232
        go_d      = go_q;
233
        nodes_sent_d = nodes_sent_q;
234
 
235
        time_cnt_d = time_cnt_q;
236
        think_time_d = think_time_q;
237
 
238
        case ( state_q )
239
           RS232_00 : begin
240
                                // sa se stabilizeze intrarile
241
                                                        state_d = RS232_0;
242
                                          end
243
                RS232_0 : begin
244
 
245
                                        // transmit
246
                                        state_d = RS232_1;
247
                                        end
248
 
249
                RS232_1 : begin
250
                                                if ( TxD_busy ) begin
251
                                                        state_d = RS232_1;
252
                                                end
253
                                                else begin
254
                                                   if ( nodes_sent_q == 1'b1)
255
                                                        begin
256
                                                                nodes_sent_d = 1'b0;
257
                                                                state_d = HUMAN;
258
                                                        end
259
                                                        else begin
260
                                                                nodes_sent_d = 1'b1;
261
                                                                state_d = RS232_00;
262
                                                                time_cnt_d = think_time_q;
263
                                                        end
264
                                                        //state_d = HUMAN;
265
                                                end
266
 
267
                                         end
268
                HUMAN: begin
269
                                        if ( enter_btn ) begin
270
                                                if ( gameover ) begin
271
                                                        state_d = HUMAN;
272
                                                end
273
                                                else
274
                                                if ( pass ) begin
275
                                                        state_d = HUMAN_MOVE;
276
                                                end
277
                                                else
278
                                                if ( M[Y*8 + X] ) begin
279
                                                        move_x_d = X;
280
                                                        move_y_d = Y;
281
                                                        state_d = HUMAN_MOVE;
282
                                                end
283
                                                else begin
284
                                                        state_d = HUMAN;
285
                                                end
286
                                        end
287
                            end
288
                HUMAN_MOVE: begin
289
                                                        // inputs for bmove prepared
290
                                                        state_d = HUMAN_MOVE_WAIT;
291
                                           end
292
 
293
                HUMAN_MOVE_WAIT: begin
294
                                                                // bmove completed
295
                                                                state_d = START_AI;
296
                                                                if ( ~pass ) begin
297
                                                                        board_R_d = bmove_out_Rw;
298
                                                                        board_B_d = bmove_out_Bw;
299
                                                                end
300
                                                                pl_d = ~pl_q;
301
                                                                go_d = 1'b1;
302
                                                          end
303
 
304
                START_AI: begin
305
                            // board_R_q, board_B_q  au noile valori
306
                                                // move_q_x,y  au valorile ok
307
                                                // pl_q = oponent
308
                                                // go_q = 1
309
                                                // bmove e conectat la game_ai
310
                                                state_d = AI;
311
                                                time_cnt_d = 32'b0;
312
                                                think_time_d = 32'b0;
313
                                         end
314
                AI: begin
315
                           if ( ai_pass) begin
316
                                        state_d = RS232_0;
317
                                        pl_d    = ~pl_q;
318
                                end
319
                                else
320
                                if ( done ) begin
321
                                        move_x_d = gai_out_best_x;
322
                                        move_y_d = gai_out_best_y;
323
                                        state_d  = MOVE_BEST_WAIT;
324
                                        //time_cnt_d = dbg_w;
325
                                        // aquire nodes
326
                                        time_cnt_d = dbg_node_cnt_w;
327
                                end
328
                                else begin
329
                                        state_d = AI;
330
                                        //time_cnt_d = time_cnt_q + 1;
331
                                        // thinking time
332
                                        think_time_d = think_time_q + 1;
333
 
334
                                end
335
 
336
                                go_d = 1'b0;
337
                         end
338
 
339
                MOVE_BEST_WAIT: begin
340
                                                           // prepare inputs for bmove
341
                                                                // move_x_q move_y_q prepared
342
                                                                state_d   = MOVE_BEST;
343
                                                         end
344
                MOVE_BEST:      begin
345
                                                        // am mutat
346
                                                        board_R_d = bmove_out_Rw;
347
                                                        board_B_d = bmove_out_Bw;
348
 
349
                                                        state_d = RS232_0;
350
                                                        pl_d    = ~pl_q;
351
                                                end
352
                default: begin
353
                                                state_d = HUMAN;
354
                                        end
355
        endcase
356
 end
357
 
358
 always @(posedge clk)
359
 begin
360
        if ( RST ) begin
361
                state_q   <= HUMAN;
362
                pl_q      <= 1'b0;
363
                move_x_q  <= 3'b0;
364
                move_y_q  <= 3'b0;
365
                go_q      <= 1'b0;
366
                time_cnt_q <= 32'b0;
367
                think_time_q <= 32'b0;
368
                nodes_sent_q <= 1'b0;
369
 
370
                board_R_q <= 64'b00000000_00000000_00000000_00010000_00001000_00000000_00000000_00000000;
371
                board_B_q <= 64'b00000000_00000000_00000000_00001000_00010000_00000000_00000000_00000000;
372
//              board_R_q <= 64'b00000111_00000011_00000001_00000011_00001001_00100001_00110011_01111111;
373
//              board_B_q <= 64'b00001000_00001100_00011110_01111100_01110110_00011110_00001100_00000000;       
374
 
375
        end
376
        else begin
377
                //time_cnt_q <= 32'b0010_0000_1010_1110_1000_1111_1010_0001;//time_cnt_d;
378
                time_cnt_q <= time_cnt_d;
379
                nodes_sent_q <= nodes_sent_d;
380
                think_time_q <= think_time_d;
381
                state_q   <= state_d;
382
                pl_q      <= pl_d;
383
                move_x_q  <= move_x_d;
384
                move_y_q  <= move_y_d;
385
                board_R_q <= board_R_d;
386
                board_B_q <= board_B_d;
387
                go_q      <= go_d;
388
        end
389
 end
390
 
391
 
392
endmodule

powered by: WebSVN 2.1.0

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