-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blackjack.v
351 lines (325 loc) · 6.58 KB
/
Blackjack.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
module Blackjack(
input rst, // PIN_Y24 SW[16]
input clk, // PIN_Y2
input [15:0]random_seed, // SW[0] - SW[15]
input new_random_seed, //SW[17]
input key1, // KEY[0] -> PIN_M23
input key2, // KEY[1] -> PIN_M21
input key3, // KEY[2] -> PIN_N21
input key4, // KEY[3] -> PIN_R24
output [6:0]seg7_dig1, // HEX[0]
output [6:0]seg7_dig2, // HEX[1]
output [6:0]seg7_dig3, // HEX[2]
output [6:0]seg7_dig4, // HEX[3]
output [6:0]seg7_dig5, // HEX[4]
output [6:0]seg7_dig6, // HEX[5]
output [6:0]seg7_dig7, // HEX[6]
output [6:0]seg7_dig8, // HEX[7]
output reg win,
output reg tie,
output reg lose,
output [0:4]aces
);
// Defining Round Deal
reg deal;
// Defining Money Variables
wire [15:0]bet = {12'd0, random_seed[3:0]};
reg [15:0]total_money;
// Seven Segment Display Controls
reg [7:0]display1;
reg [7:0]display2;
// Defining Player Card Value
reg [31:0]value;
wire [31:0]val2;
wire [31:0]val3;
wire [31:0]val4;
wire [31:0]val5;
// Player Cards Initiated
wire [7:0]card1;
wire [7:0]card2;
wire [7:0]card3;
wire [7:0]card4;
wire [7:0]card5;
// Deal Player Cards
player PLAYER(clk, rst, deal, random_seed, new_random_seed, card1, card2, card3, card4, card5, val2, val3, val4, val5, aces);
// Defining Dealer Card Value
wire [31:0]dvalue1;
wire [31:0]dvalue;
wire [7:0]dcardsnum;
// Dealer Cards Initiated
wire [7:0]dcard1;
wire [7:0]dcard2;
wire [7:0]dcard3;
wire [7:0]dcard4;
wire [7:0]dcard5;
// Deal Dealer Logic
dealer DEALER(clk, rst, deal, {random_seed[2:0], random_seed[15:6], random_seed[2:0]}, new_random_seed, dcard1, dcard2, dcard3, dcard4, dcard5, dvalue1, dvalue, dcardsnum);
// Defining State and Next State
reg [5:0]S;
reg [5:0]NS;
// Defining Parameters
parameter INITS = 6'b000000,
START = 6'b000001,
BET_WAIT = 6'b100010,
BET = 6'b000010,
C_2_WAIT = 6'b100011,
C_2 = 6'b000011,
C_3_WAIT = 6'b100100,
C_3 = 6'b000100,
C_4_WAIT = 6'b100101,
C_4 = 6'b000101,
C_5_WAIT = 6'b100110,
C_5 = 6'b000110,
BJ_5 = 6'b000111,
BUST = 6'b001000,
DEAL = 6'b001001,
DEAL_BJ = 6'b001010,
DEAL_BUST = 6'b001011,
DEAL_TIE = 6'b001100,
DEAL_HIGH = 6'b001101,
DEAL_LOW = 6'b001110,
LOST = 6'b001111,
TIE = 6'b010000,
WIN = 6'b010001,
END = 6'b010010;
// Handle State Conditions
always@(*)
case(S)
INITS: NS = START;
START: if (!key4)
NS = BET_WAIT;
else
NS = START;
BET_WAIT: if(key4)
NS = BET;
else
NS = BET_WAIT;
BET: if (!key4 & (bet > 16'd0))
NS = C_2_WAIT;
else
NS = BET;
C_2_WAIT: if(key4)
NS = C_2;
else
NS = C_2_WAIT;
C_2: if (!key2) // key2 == hit
NS = C_3_WAIT;
else if (!key1) // key1 == stand
NS = DEAL;
else
NS = C_2;
C_3_WAIT: if(key2)
NS = C_3;
else
NS = C_3_WAIT;
C_3: if (value > 32'd21)
NS = BUST;
else if (!key2)
NS = C_4_WAIT;
else if (!key1)
NS = DEAL;
else
NS = C_3;
C_4_WAIT: if(key2)
NS = C_4;
else
NS = C_4_WAIT;
C_4: if (value > 32'd21)
NS = BUST;
else if (!key2)
NS = C_5_WAIT;
else if (!key1)
NS = DEAL;
else
NS = C_4;
C_5_WAIT: if(key2)
NS = C_5;
else
NS = C_5_WAIT;
C_5: if (value > 32'd21)
NS = BUST;
else
NS = BJ_5;
BJ_5: if ((dvalue == 32'd21) & (dcardsnum == 32'd2))
NS = DEAL_BJ;
else
NS = WIN;
BUST: NS = LOST;
DEAL: if((dvalue == 32'd21) & (dcardsnum == 32'd2))
NS = DEAL_BJ;
else if (dvalue > 32'd21)
NS = DEAL_BUST;
else if (value > dvalue)
NS = DEAL_LOW;
else if (value == dvalue)
NS = DEAL_TIE;
else
NS = DEAL_HIGH;
DEAL_BJ: if ((val2 == value) & (value == 32'd21))
NS = TIE;
else
NS = LOST;
DEAL_BUST: NS = WIN;
DEAL_TIE: if (val2 == 32'd21)
NS = WIN;
else
NS = TIE;
DEAL_HIGH: NS = LOST;
DEAL_LOW: NS = WIN;
LOST: NS = END;
TIE: NS = END;
WIN: NS = END;
END: if (!key4)
NS = START;
else
NS = END;
default: NS = INITS;
endcase
// Reset Condition and Next State Condition
always@(posedge clk or negedge rst)
begin
if (rst == 1'b0)
S <= INITS;
else
begin
case(S)
INITS: begin
total_money <= 8'd200;
deal <= 1'b0;
end
START: begin
deal <= 1'b0;
value <= 32'd0;
total_money <= total_money;
end
C_2_WAIT: begin
deal <= 1'b1;
total_money <= total_money;
end
C_2: begin
deal <= 1'b0;
value <= val2;
total_money <= total_money;
end
C_3: begin
deal <= 1'b0;
value <= val3;
total_money <= total_money;
end
C_4: begin
deal <= 1'b0;
value <= val4;
total_money <= total_money;
end
C_5: begin
deal <= 1'b0;
value <= val5;
total_money <= total_money;
end
LOST: begin
total_money <= total_money + bet;
deal <= 1'b0;
end
TIE: begin
total_money <= total_money;
deal <= 1'b0;
end
WIN: begin
total_money <= total_money - bet;
deal <= 1'b0;
end
default: begin
total_money <= total_money;
deal <= deal;
value <= value;
end
endcase
S <= NS;
end
end
// Output Condition
always@(*)
case (S)
START: begin
win = win;
tie = tie;
lose = lose;
end
C_2: begin
win = win;
tie = tie;
lose = lose;
end
C_3: begin
win = win;
tie = tie;
lose = lose;
end
C_4: begin
win = win;
tie = tie;
lose = lose;
end
C_5: begin
win = win;
tie = tie;
lose = lose;
end
WIN: begin
win = 1'b1;
tie = 1'b0;
lose = 1'b0;
end
TIE: begin
win = 1'b0;
tie = 1'b1;
lose = 1'b0;
end
LOST: begin
win = 1'b0;
tie = 1'b0;
lose = 1'b1;
end
END: begin
win = win;
tie = tie;
lose = lose;
end
default: begin
win = 1'b0;
tie = 1'b0;
lose = 1'b0;
end
endcase
// Display Controls
always@(*)
begin
if (!key3)
begin
if (((S > 6'b001001) & (S < 6'b100000)))
begin
display1 = value[7:0];
display2 = dvalue[7:0];
end
else if ((S == 6'b000010) | (S == 6'b000001))
begin
display1 = 8'd0;
display2 = 8'd0;
end
else
begin
display2 = dvalue1[7:0];
display1 = value[7:0];
end
end
else
begin
display1 = bet[7:0];
display2 = {2'b00, S};
end
end
// Display Modules
four_decimal_vals MONEY(total_money, seg7_dig1, seg7_dig2, seg7_dig3, seg7_dig4);
two_decimal_vals BETS(display1, seg7_dig5, seg7_dig6);
two_decimal_vals STATE(display2, seg7_dig7, seg7_dig8);
endmodule