This repository has been archived by the owner on Nov 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect4.v
172 lines (159 loc) · 4.14 KB
/
connect4.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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 13:15:45 05/21/2015
// Design Name:
// Module Name: connect4
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module connect4(
input clk,
input [10:0] PixelX,
input [10:0] PixelY,
input left,
input right,
input select,
input reset,
output reg [2:0] R,
output reg [2:0] G,
output reg [1:0] B
);
wire RED;
assign RED = 0;
wire BLACK;
assign BLACK = 1;
reg currPlayer; // 0: Red, 1: Black
reg [2:0] currColumn;;
reg [7:0] gameStatePIECE [7:0];
reg [7:0] gameStateSIDE [7:0];
// Initialize game state data
reg [3:0] i;
reg [3:0] j;
initial begin
currColumn <= 3;
currPlayer <= RED;
for (i = 0; i < 8; i = i + 1) begin
for (j = 0; j < 8; j = j + 1) begin
gameStatePIECE[i][j] <= 0;
gameStateSIDE[i][j] <= 0;
end
end
end
// Input Left/Right
always @(posedge left or posedge right) begin
if (left == 1 && currColumn == 0)
currColumn <= 7; // Wrap around
else if (left == 1)
currColumn <= currColumn - 1;
if (right == 1 && currColumn == 7)
currColumn <= 0; // Wrap around
else if (right == 1)
currColumn <= currColumn + 1;
end
// Input Select
wire [2:0] highPos;
wire isNotEmpty;
encoder8_3 _encoder(
.data(gameStatePIECE[currColumn]),
.O(highPos),
.V(isNotEmpty)
);
always @(posedge clk) begin
// Determine top most position with encoder
if (select == 1)
if (isNotEmpty == 0) begin
gameStatePIECE[currColumn][0] <= 1;
gameStateSIDE[currColumn][0] <= currPlayer;
currPlayer <= ~currPlayer; // Change player turn
end
else if (highPos < 7) begin
gameStatePIECE[currColumn][highPos+1] <= 1;
gameStateSIDE[currColumn][highPos+1] <= currPlayer;
currPlayer <= ~currPlayer; // Change player turn
end
// Do nothing if highPos == 7; column full
// Reset
if (reset == 1) begin
currPlayer <= RED;
for (i = 0; i < 8; i = i + 1) begin
gameStatePIECE[i] <= 0;
gameStateSIDE[i] <= 0;
end
end
end
// Display pixels
integer pixelCol;
integer pixelRow;
always @(posedge clk) begin
pixelCol <= (PixelX - 120) / 70;
pixelRow <= (PixelY - 60) / 60;
if (PixelX < 120 || PixelX > 680 || PixelY < 60 || PixelY > 540) begin
// Show current player color on border
if (currPlayer == BLACK) begin
// Black border
R = 3'b000;
G = 3'b000;
B = 2'b00;
end
else begin
// Red border
R = 3'b111;
G = 3'b000;
B = 2'b00;
end
end
else if (gameStatePIECE[pixelCol][7-pixelRow] == 1 &&
((PixelX - (155+pixelCol*70))*(PixelX - (155+pixelCol*70)) +
(PixelY - (90+pixelRow*60))*(PixelY - (90+pixelRow*60))) <250) begin
// Check piece color
if (gameStateSIDE[pixelCol][7-pixelRow] == BLACK) begin
// Black
R = 3'b000;
G = 3'b000;
B = 2'b00;
end
else begin
// Red
R = 3'b111;
G = 3'b000;
B = 2'b00;
end
end
else if (pixelCol % 2 == pixelRow % 2) begin
// Yellow checkerboard squares
R = 3'b111;
G = 3'b111;
B = 2'b00;
// Highlight column if selected
if (pixelCol == currColumn) begin
R = 3'b111;
G = 3'b111;
B = 2'b11;
end
end
else begin
// Blue default checkerboard
R = 3'b000;
G = 3'b000;
B = 2'b11;
// Highlight column if selected
if (pixelCol == currColumn) begin
R = 3'b111;
G = 3'b111;
B = 2'b11;
end
end
end
endmodule