aboutsummaryrefslogtreecommitdiff
path: root/task5/tb_statemachine.sv
blob: da4c601aa92b6cc7170a84059a1aa5ab2b948374 (plain) (blame)
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
module tb_statemachine();
	logic err, slow_clock, resetb, load_pcard1, load_pcard2, load_pcard3,
		load_dcard1, load_dcard2, load_dcard3,
		player_win_light, dealer_win_light;
	logic [3:0] dscore, pscore, pcard3;

	statemachine DUT(slow_clock, resetb, dscore, pscore, pcard3,
		load_pcard1, load_pcard2, load_pcard3,
		load_dcard1, load_dcard2, load_dcard3,
		player_win_light, dealer_win_light);

	task check;
		input logic [7:0] expected;

		if (load_pcard1 !== expected[7]) begin
			err = 1;
			$display("FAILED: load_pcard1 is incorrect.",);
		end
		if (load_pcard2 !== expected[6]) begin
			err = 1;
			$display("FAILED: load_pcard2 is incorrect.",);
		end
		if (load_pcard3 !== expected[5]) begin
			err = 1;
			$display("FAILED: load_pcard3 is incorrect.",);
		end
		if (load_dcard1 !== expected[4]) begin
			err = 1;
			$display("FAILED: load_dcard1 is incorrect.",);
		end
		if (load_dcard2 !== expected[3]) begin
			err = 1;
			$display("FAILED: load_dcard2 is incorrect.",);
		end
		if (load_dcard3 !== expected[2]) begin
			err = 1;
			$display("FAILED: load_dcard3 is incorrect.",);
		end
		if (player_win_light !== expected[1]) begin
			err = 1;
			$display("FAILED: player_win_light is incorrect.",);
		end
		if (dealer_win_light !== expected[0]) begin
			err = 1;
			$display("FAILED: dealer_win_light is incorrect.",);
		end
	endtask: check

	initial forever begin
		slow_clock = 1'b0;
		#5;
		slow_clock = 1'b1;
		#5;
	end

	initial begin
		#10000;
		$stop;
	end

	initial begin
		err = 0;
		pcard3 = 4'b0;

		/* Natural 9 vs natural 8: deal four cards, player win. */

		$display("Test 1");

		resetb = 1'b0;
		pscore = 4'd9;
		dscore = 4'd8;

		#10;

		resetb = 1'b1;

		check(8'b10000000); #10;
		check(8'b00010000); #10;
		check(8'b01000000); #10;
		check(8'b00001000); #10;
		check(8'b00000000); #10;
		check(8'b00000010); #10;

		/* Natural 8 vs natural 9: deal four cards, dealer win. */

		$display("Test 2");

		resetb = 1'b0;
		pscore = 4'd8;
		dscore = 4'd9;

		#10;

		resetb = 1'b1;

		check(8'b10000000); #10;
		check(8'b00010000); #10;
		check(8'b01000000); #10;
		check(8'b00001000); #10;
		check(8'b00000000); #10;
		check(8'b00000001); #10;

		/* 7 vs 6: deal four cards, player win. */

		$display("Test 3");

		resetb = 1'b0;
		pscore = 4'd7;
		dscore = 4'd6;

		#10;

		resetb = 1'b1;

		check(8'b10000000); #10;
		check(8'b00010000); #10;
		check(8'b01000000); #10;
		check(8'b00001000); #10;
		check(8'b00000000); #10;
		check(8'b00000010); #10;

		/* 7 vs 5: player stands, dealer hits, dealer win. */

		$display("Test 4");

		resetb = 1'b0;
		pscore = 4'd7;
		dscore = 4'd5;

		#10;

		resetb = 1'b1;

		check(8'b10000000); #10;
		check(8'b00010000); #10;
		check(8'b01000000); #10;
		check(8'b00001000); #10;

		/* Dealer 3rd card is a 3. */
		check(8'b00000100);
		dscore = 4'd8;
		#10;

		check(8'b00000001); #10;

		/* 3 vs 4: player hits and gets 7, dealer hits, dealer win. */

		$display("Test 5");

		resetb = 1'b0;
		pscore = 4'd3;
		dscore = 4'd4;

		#10;

		resetb = 1'b1;

		check(8'b10000000); #10;
		check(8'b00010000); #10;
		check(8'b01000000); #10;
		check(8'b00001000); #10;

		/* Player draws a 7. */
		pcard3 = 4'd7;
		pscore = 4'b0;
		check(8'b00100000);
		#10;

		/* Dealer 3rd card is a 3. */
		check(8'b00000100);
		dscore = 4'd8;
		#10;

		check(8'b00000001); #10;

		/* 0 vs 3: player hits and gets 8, dealer stands, player win. */

		$display("Test 6");

		resetb = 1'b0;
		pscore = 4'd0;
		dscore = 4'd3;

		#10;

		resetb = 1'b1;

		check(8'b10000000); #10;
		check(8'b00010000); #10;
		check(8'b01000000); #10;
		check(8'b00001000); #10;

		/* Player draws a 8. */
		pcard3 = 4'd8;
		pscore = 4'd8;
		check(8'b00100000);
		#10;

		check(8'b00000010); #10;

		/* 5 vs 0: player hits and gets 9, dealer hits, tie. */

		$display("Test 7");

		resetb = 1'b0;
		pscore = 4'd5;
		dscore = 4'd0;

		#10;

		resetb = 1'b1;

		check(8'b10000000); #10;
		check(8'b00010000); #10;
		check(8'b01000000); #10;
		check(8'b00001000); #10;

		/* Player draws a 9. */
		pcard3 = 4'd9;
		pscore = 4'd4;
		check(8'b00100000);
		#10;

		/* Dealer 3rd card is a 4. */
		check(8'b00000100);
		dscore = 4'd4;
		#10;

		check(8'b00000011); #10;

		/* 5 vs 0: player hits and gets 9, dealer hits, dealer win. */

		$display("Test 8");

		resetb = 1'b0;
		pscore = 4'd5;
		dscore = 4'd0;

		#10;

		resetb = 1'b1;

		check(8'b10000000); #10;
		check(8'b00010000); #10;
		check(8'b01000000); #10;
		check(8'b00001000); #10;

		/* Player draws a 9. */
		pcard3 = 4'd9;
		pscore = 4'd4;
		check(8'b00100000);
		#10;

		/* Dealer 3rd card is a 5. */
		check(8'b00000100);
		dscore = 4'd5;
		#10;

		check(8'b00000001); #10;

		if (~err)
			$display("All tests passed.");

		#10;
		$stop;
	end
endmodule: tb_statemachine