@@ -24,17 +24,22 @@ localparam None_Color = {4'd15, 4'd15, 4'd15},
24
24
Body_Color = {4'd09 , 4'd15 , 4'd00 },
25
25
Brick_Color = {4'd05 , 4'd05 , 4'd07 },
26
26
Apple_Color = {4'd15 , 4'd00 , 4'd00 },
27
- Score_Color = {4'd08 , 4'd08 , 4'h15 },
28
- HiSco_Color = {4'd15 , 4'd08 , 4'h00 };
29
-
30
- localparam Row = 5'd25 , Col= 5'd19 ;
31
- localparam Key_Left= 4'h2 , Key_Right= 4'h4 , Key_Down= 4'h3 , Key_Up= 4'h7 ;
32
- // localparam N=4; // latency
27
+ Score_Color = {4'd08 , 4'd08 , 4'd15 },
28
+ HiSco_Color = {4'd15 , 4'd08 , 4'd00 };
29
+ `define HD
30
+ `ifdef HD
31
+ localparam D= 5 , P= 4 , S= 2 ;
32
+ `else
33
+ localparam D= 4 , P= 5 , S= 1 ;
34
+ `endif
35
+ localparam Row = 5'd25 * S, Col= 5'd19 * S;
36
+ localparam Row_1 = Row- 1 , Col_1= Col- 1 ;
33
37
34
- reg [ 11 : 0 ] score, hi_score ;
38
+ localparam Key_Left = 4'h2 , Key_Right = 4'h4 , Key_Down = 4'h3 , Key_Up = 4'h7 ;
35
39
40
+ // /////////////////////////////////
36
41
// data and video buffer
37
- reg [8 :0 ] raddr, waddr;
42
+ reg [10 :0 ] raddr, waddr;
38
43
reg [3 :0 ] vdata;
39
44
wire [3 :0 ] vout;
40
45
vram u_vram (
@@ -50,20 +55,26 @@ vram u_vram(
50
55
.q_b (vout)
51
56
);
52
57
58
+ // /////////////////////////////////
59
+
53
60
reg [1 :0 ] head_dir;
54
- reg [4 :0 ] head_pos_x, head_pos_y;
55
- reg [4 :0 ] tail_pos_x, tail_pos_y;
56
- reg [4 :0 ] appl_pos_x, appl_pos_y;
61
+ reg [D:0 ] head_pos_x, head_pos_y;
62
+ reg [D:0 ] tail_pos_x, tail_pos_y;
63
+ reg [D:0 ] appl_pos_x, appl_pos_y;
64
+ reg [5 :0 ] speed_cnt;
65
+ reg [11 :0 ] score, hi_score;
66
+
67
+ // /////////////////////////////////
57
68
58
69
wire [3 :0 ] code;
59
70
wire keydown, scan_clk;
60
71
keypad4x4 key (v_cnt, rst, row, col, code, keydown, scan_clk);
61
72
73
+ // /////////////////////////////////
74
+
62
75
wire [3 :0 ] LED[7 :0 ];
63
76
led8 led (scan_clk, rst, LED[0 ], LED[1 ], LED[2 ], LED[3 ], LED[4 ], LED[5 ], LED[6 ], LED[7 ], LEDOut, DigitSelect);
64
77
65
- reg [4 :0 ] speed_cnt;
66
-
67
78
// debug info led /////////////////
68
79
// assign Light[0] = ~keydown;
69
80
// assign Light[1] = keydown;
@@ -76,31 +87,41 @@ assign LED[2] = score[ 3:0];
76
87
77
88
wire [7 :0 ] randq;
78
89
LFSR8_11D LFSR8_11D (clk, rst, randq);
79
- wire [4 :0 ] rand_x = randq[3 :0 ]+ randq[6 :4 ]; // 16+8=24
80
- wire [4 :0 ] rand_y = randq[7 :4 ]+ randq[1 :0 ]; // 16+3=19
90
+
91
+ `ifdef HD
92
+ wire [D:0 ] rand_x = randq[4 :0 ]+ randq[7 :4 ]; // 32+16=48
93
+ wire [D:0 ] rand_y = randq[7 :3 ]+ randq[1 :0 ]; // 32+3=38
94
+ `else
95
+ wire [D:0 ] rand_x = randq[3 :0 ]+ randq[6 :4 ]; // 16+8=24
96
+ wire [D:0 ] rand_y = randq[7 :4 ]+ randq[1 :0 ]; // 16+3=19
97
+ `endif
98
+
99
+ // /////////////////////////////////
81
100
82
101
wire [1 :0 ] frame_sync;
83
102
assign frame_sync[0 ] = v_cnt== 1 && h_cnt== 0 ;
84
103
assign frame_sync[1 ] = v_cnt== 1 && h_cnt== 2 ;
85
104
105
+ // /////////////////////////////////
106
+
86
107
reg [3 :0 ] ram_state;
87
108
localparam ram_clr= 0 , ram_gen_apple= 1 , ram_set_apple= 2 , ram_set_head= 3 ,
88
- ram_logic= 4 , ram_check= 5 , ram_clr_tail= 6 , ram_start= 7 ,
89
- ram_gen_apple_d= 8 , ram_check_d= 9 , ram_clr_tail_d= 10 ;
109
+ ram_logic= 4 , ram_check= 5 , ram_clr_tail= 6 , ram_start= 7 ,
110
+ ram_gen_apple_d= 8 , ram_check_d= 9 , ram_clr_tail_d= 10 ;
90
111
91
112
always @(posedge clk or negedge rst)
92
113
if (! rst) begin
93
114
hi_score <= 1'b0 ;
94
- { appl_pos_x, appl_pos_y} <= { 5'd10 , 5'd2 } ;
95
- { tail_pos_x, tail_pos_y} <= { 5 'd1, 5 'd2} ;
115
+ appl_pos_x<= 4'd9 ; appl_pos_y<= 4'd2 ;
116
+ tail_pos_x<= 4 'd1; tail_pos_y <= 4 'd2 ;
96
117
ram_state <= ram_start;
97
118
end
98
119
else if (ram_state== ram_start) begin // reset screen
99
120
speed_cnt <= 1'b0 ;
100
121
score <= 1'b0 ;
101
- vdata <= {Right, None} ;
102
- waddr <= 8'd0 ;
103
- raddr <= 8'd0 ;
122
+ vdata <= 1'b0 ;
123
+ waddr <= 1'b0 ;
124
+ raddr <= 1'b0 ;
104
125
ram_state <= ram_clr;
105
126
end
106
127
else if (ram_state== ram_clr) begin // reset screen
@@ -139,8 +160,8 @@ always @(posedge clk or negedge rst)
139
160
ram_state <= ram_set_head; // save head turn direct
140
161
else if (frame_sync[1 ]) // new frame begin, start snake speed count
141
162
speed_cnt <= speed_cnt+ 1'b1 ;
142
- if (x [9 :5 ]< Row)
143
- raddr <= Row* y[9 :5 ] + x [9 :5 ]; // read cell(32*32 pixel) for graphic render
163
+ if (x [9 :P ]< Row)
164
+ raddr <= Row* y[9 :P ] + x [9 :P ]; // read cell(32*32 pixel) for graphic render
144
165
end
145
166
else if (ram_state== ram_check_d) // vram read latecy 2 clk
146
167
ram_state <= ram_check;
@@ -173,8 +194,9 @@ always @(posedge clk or negedge rst)
173
194
wire clk_move= speed_cnt== Speed;
174
195
// go ahead, move head position, before check collision one clock
175
196
always @(posedge clk_move or negedge rst)
176
- if (! rst)
177
- {head_pos_x, head_pos_y} <= {5'd1 , 5'd2 };
197
+ if (! rst) begin
198
+ head_pos_x<= 4'd1 ; head_pos_y<= 4'd2 ;
199
+ end
178
200
else
179
201
move(head_dir, head_pos_x, head_pos_y);
180
202
@@ -193,12 +215,12 @@ always @(posedge keydown or negedge rst)
193
215
194
216
task move;
195
217
input wire [1 :0 ] dir;
196
- inout reg [4 :0 ] px, py;
218
+ inout reg [D :0 ] px, py;
197
219
case (dir) // dir
198
- Right: px <= px< 5'd24 ? px + 1'b1 : 5'd00 ;
199
- Left: px <= px> 5'd00 ? px - 1'b1 : 5'd24 ;
200
- Up: py <= py> 5'd00 ? py - 1'b1 : 5'd18 ;
201
- Down: py <= py< 5'd18 ? py + 1'b1 : 5'd00 ;
220
+ Right: px <= px< Row_1 ? px + 1'b1 : 1'b00 ;
221
+ Left: px <= px> 1'b00 ? px - 1'b1 : Row_1 ;
222
+ Up: py <= py> 1'b00 ? py - 1'b1 : Col_1 ;
223
+ Down: py <= py< Col_1 ? py + 1'b1 : 1'b00 ;
202
224
endcase
203
225
endtask
204
226
0 commit comments