Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Snake404
- (
- input [1:0] KEY , // keys
- input clockInp,
- input sw,
- output reg [9:0] LED , // leds
- output reg [47:0] HEX // displays
- );
- integer i;
- reg direction = 0;
- reg horizontal = 0;
- reg vertical = 0;
- initial
- begin
- clk_count = 0;
- for (i = 0; i <= 47; i = i + 1)
- HEX[i] = 1;
- for (i = 0; i <= 9; i = i + 1)
- LED[i] = 0;
- end
- reg clk;
- integer clockInp_count = 0;
- always @(posedge clockInp)
- begin
- clockInp_count= clockInp_count + 1;
- if (clockInp_count >= 10000)
- begin
- clockInp_count = 0;
- clk = ~clk;
- end
- end
- integer clk_count = 0;
- integer apple_clk_count = 0;
- reg apple_blink;
- reg move_clk;
- integer delay = 20;
- always @(posedge clk)
- begin
- clk_count = clk_count + 1;
- if (clk_count >= delay)
- begin
- clk_count = 0;
- move_clk = ~move_clk;
- LED[3] = ~LED[3];
- end
- apple_clk_count = apple_clk_count + 1;
- if (apple_clk_count >= 6)
- begin
- apple_clk_count = 0;
- apple_blink = ~apple_blink;
- end
- end
- integer Queue[0:30];
- integer head = 0;
- integer length = 1;
- integer d = 0;
- integer p8 = 0;
- integer right_pos1 = 0;
- integer left_pos1 = 0;
- //integer right_pos2 = 0;
- //integer left_pos2 = 0;
- integer funct_index;
- reg cross_tail = 0;
- reg cross1 = 0;
- reg cross2 = 0;
- reg cross3 = 0;
- reg end_game = 0;
- integer end_game_delay = 20;
- integer apple_pos = 0;
- integer apple = 0;
- always @(negedge move_clk)
- begin
- // HEX = 48'b0;
- if(end_game)
- begin
- if(end_game_delay <= 0)
- begin
- for (i = 0; i <= 47; i = i + 1)
- HEX[i] = 1;
- end_game = 0;
- end_game_delay = 20;
- end
- else
- begin
- end_game_delay = end_game_delay - 1;
- end
- end
- else
- begin
- HEX[apple_pos] <= ~HEX[apple_pos];
- apple = (apple + 1) % 5;
- if (apple == 0)
- begin
- HEX[Queue[(head - length + 31) % 30]] = 1;
- p8 = Queue[head] % 8;
- if(direction == 0)
- begin
- right_pos1 = move(~direction, horizontal, 0, p8);
- left_pos1 = move (~direction, horizontal, 1, p8);
- end
- else
- begin
- right_pos1 = move(~direction, 0, vertical, p8);
- left_pos1 = move (~direction, 1, vertical, p8);
- end
- d = move(direction, horizontal, vertical, p8);
- cross1 = 0;
- cross2 = 0;
- cross3 = 0;
- if (length > 1)
- begin
- for(funct_index = 0; funct_index <= 30; funct_index = funct_index + 1)
- begin
- if(((head - length + 32) % 30 <= funct_index && funct_index <= head) || ((head - length + 32) % 30 > head && ((head - length + 32) % 30 <= funct_index || funct_index <= head)))
- begin
- if(Queue[funct_index] == (Queue[head] + d + 48) % 48)
- begin
- cross3 = 1;
- end
- end
- end
- end
- if (length > 2)
- begin
- for(funct_index = 0; funct_index <= 30; funct_index = funct_index + 1)
- begin
- if(((head - length + 33) % 30 <= funct_index && funct_index <= head) || ((head - length + 33) % 30 > head && ((head - length + 33) % 30 <= funct_index || funct_index <= head)))
- begin
- if(Queue[funct_index] == ((Queue[head] + right_pos1 + 48) % 48))
- begin
- cross1 = 1;
- end
- if(Queue[funct_index] == ((Queue[head] + left_pos1 + 48) % 48))
- begin
- cross2 = 1;
- end
- end
- end
- end
- if(cross1 && cross2 || cross3)
- begin
- cross_tail = 1;
- end
- if(~cross_tail)
- begin
- head = (head + 1) % 30;
- Queue[head] = (Queue[(head + 29) % 30] + d + 48) % 48;
- HEX[Queue[head]] = 0;
- //Generate apple
- if(apple_pos == Queue[head])
- begin
- LED[8] = ~LED[8];///////////////
- length = length + 1;
- apple_pos = (apple_pos + 31) % 48;
- if (apple_pos % 8 == 7)
- apple_pos = apple_pos - 1;
- //apple_taken <= 1;
- end
- if(length == 6)
- begin
- if (delay > 10)
- begin
- delay = delay - 1;
- length = 1;
- HEX = 48'hFFFFFFFFFFFF;
- end
- end
- end
- else
- begin
- HEX = 48'b100010011111111110001111100010001010001010000110;
- for (i = 0; i <= 30; i = i + 1)
- Queue[i] = 0;
- end_game = 1;
- apple = 0;
- apple_pos = 0;
- delay = 20;
- length = 1;
- cross_tail = 0;
- cross1 = 0;
- cross2 = 0;
- cross3 = 0;
- end
- end
- end
- end
- function integer move;
- input integer direction, horizontal, vertical, p8;
- begin
- if (direction == 0)
- begin
- if (p8 % 3 == 0)
- begin
- if (horizontal == 0) move = 8;
- else move = -8;
- end
- else
- begin
- if ((vertical == 0) && ((p8 == 2) || (p8 == 5)))
- begin
- if (p8 == 2)
- begin
- if (horizontal == 0) move = 4;
- else move = -4;
- end
- else
- begin
- if (horizontal == 0) move = 9;
- else move = 1;
- end
- end
- else if (vertical == 1 && (p8 == 1 || p8 == 4))
- begin
- if (p8 == 1)
- begin
- if (horizontal == 0) move = -1;
- else move = -9;
- end
- else
- begin
- if (horizontal == 0) move = 4;
- else move = -4;
- end
- end
- else
- begin
- if (p8 == 1 || p8 == 2)
- begin
- if (horizontal == 0) move = 2;
- else move = -6;
- end
- else
- begin
- if (horizontal == 0) move = 7;
- else move = -1;
- end
- if (p8 == 2 || p8 == 5)
- move = move - 1;
- end
- end
- end
- else
- begin // direction == 1
- if (p8 == 1 || p8 == 4) move = 1;
- else if (p8 == 5 || p8 == 2) move = -1;
- else if (p8 == 0)
- begin
- if (horizontal == 0)
- begin
- if (vertical == 0) move = 4;
- else move = 5;
- end
- else
- begin
- if (vertical == 0) move = 1;
- else move = 2;
- end
- end
- else if (p8 == 3)
- begin
- if (horizontal == 0)
- begin
- if (vertical == 0) move = 2;
- else move = 1;
- end
- else
- begin
- if (vertical == 0) move = -1;
- else move = -2;
- end
- end
- else if (p8 == 6)
- begin
- if (horizontal == 0)
- begin
- if (vertical == 0) move = -2;
- else move = -1;
- end
- else
- begin
- if (vertical == 0) move = -5;
- else move = -4;
- end
- end
- end
- end
- endfunction
- integer j = 0;
- reg pressed = 0;
- always @(posedge move_clk)
- begin
- if (KEY[0] == 0 && ~pressed)
- begin
- pressed = 1;
- if (direction == 0)
- begin
- if (horizontal == 0)
- begin
- direction <= 1;
- vertical <= 1;
- end
- else
- begin
- direction <= 1;
- vertical <= 0;
- end
- end
- else
- begin
- if (vertical == 0)
- begin
- direction <= 0;
- horizontal <= 0;
- end
- else
- begin
- direction <= 0;
- horizontal <= 1;
- end
- end
- end
- if (KEY[1] == 0 && ~pressed)
- begin
- pressed = 1;
- if (direction == 0)
- begin
- if (horizontal == 0)
- begin
- direction <= 1;
- vertical <= 0;
- end
- else
- begin
- direction <= 1;
- vertical <= 1;
- end
- end
- else
- begin
- if (vertical == 0)
- begin
- direction <= 0;
- horizontal <= 1;
- end
- else
- begin
- direction <= 0;
- horizontal <= 0;
- end
- end
- end
- if (apple == 4)
- begin
- pressed = 0;
- end
- end
- endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement