Advertisement
Guest User

Final version

a guest
Apr 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Snake404
  2. (
  3. input [1:0] KEY , // keys
  4. input clockInp,
  5. input sw,
  6. output reg [9:0] LED , // leds
  7. output reg [47:0] HEX // displays
  8. );
  9.  
  10.  
  11. integer i;
  12. reg direction = 0;
  13. reg horizontal = 0;
  14. reg vertical = 0;
  15.  
  16. initial
  17. begin
  18.     clk_count = 0;
  19.     for (i = 0; i <= 47; i = i + 1)
  20.         HEX[i] = 1;
  21.     for (i = 0; i <= 9; i = i + 1)
  22.         LED[i] = 0;
  23. end
  24.  
  25. reg clk;
  26. integer clockInp_count = 0;
  27.  
  28. always @(posedge clockInp)
  29. begin
  30.     clockInp_count= clockInp_count + 1;
  31.     if (clockInp_count >= 10000)
  32.     begin
  33.         clockInp_count = 0;
  34.         clk = ~clk;
  35.     end
  36. end
  37.  
  38.  
  39. integer clk_count = 0;
  40. integer apple_clk_count = 0;
  41. reg apple_blink;
  42. reg move_clk;
  43. integer delay = 20;
  44.  
  45.  
  46. always @(posedge clk)
  47. begin
  48.     clk_count = clk_count + 1;
  49.     if (clk_count >= delay)
  50.     begin
  51.         clk_count = 0; 
  52.         move_clk = ~move_clk;
  53.         LED[3] = ~LED[3];
  54.     end
  55.    
  56.    
  57.     apple_clk_count = apple_clk_count + 1;
  58.     if (apple_clk_count >= 6)
  59.     begin
  60.         apple_clk_count = 0;
  61.         apple_blink = ~apple_blink;
  62.     end
  63. end
  64.  
  65. integer Queue[0:30];
  66. integer head = 0;
  67. integer length = 1;
  68.  
  69. integer d = 0;
  70. integer p8 = 0;
  71. integer right_pos1 = 0;
  72. integer left_pos1 = 0;
  73. //integer right_pos2 = 0;
  74. //integer left_pos2 = 0;
  75. integer funct_index;
  76. reg cross_tail = 0;
  77. reg cross1 = 0;
  78. reg cross2 = 0;
  79. reg cross3 = 0;
  80. reg end_game = 0;
  81. integer end_game_delay = 20;
  82.  
  83. integer apple_pos = 0;
  84. integer apple = 0;
  85.  
  86. always @(negedge move_clk)
  87. begin
  88. //  HEX = 48'b0;
  89.     if(end_game)
  90.         begin
  91.         if(end_game_delay <= 0)
  92.         begin
  93.             for (i = 0; i <= 47; i = i + 1)
  94.                     HEX[i] = 1;
  95.             end_game = 0;
  96.             end_game_delay = 20;
  97.         end
  98.         else
  99.         begin
  100.             end_game_delay = end_game_delay - 1;
  101.         end
  102.         end
  103.     else
  104.     begin
  105.  
  106.     HEX[apple_pos] <= ~HEX[apple_pos];
  107.    
  108.     apple = (apple + 1) % 5;
  109.     if (apple == 0)
  110.     begin
  111.       HEX[Queue[(head - length + 31) % 30]] = 1;
  112.       p8 = Queue[head] % 8;
  113.      
  114.       if(direction == 0)
  115.       begin
  116.           right_pos1 = move(~direction, horizontal, 0, p8);
  117.           left_pos1 = move (~direction, horizontal, 1, p8);
  118.         end
  119.         else
  120.         begin
  121.             right_pos1 = move(~direction, 0, vertical, p8);
  122.            left_pos1 = move (~direction, 1, vertical, p8);
  123.         end
  124.      
  125.       d = move(direction, horizontal, vertical, p8);
  126.      
  127.       cross1 = 0;
  128.       cross2 = 0;
  129.       cross3 = 0;
  130.      
  131.       if (length > 1)
  132.       begin
  133.           for(funct_index = 0; funct_index <= 30; funct_index = funct_index + 1)
  134.             begin
  135.                 if(((head - length + 32) % 30 <= funct_index && funct_index <= head) || ((head - length + 32) % 30 > head && ((head - length + 32) % 30 <= funct_index || funct_index <= head)))
  136.                 begin
  137.                     if(Queue[funct_index] == (Queue[head] + d + 48) % 48)
  138.                     begin
  139.                         cross3 = 1;
  140.                     end
  141.                 end
  142.             end
  143.         end
  144.        
  145.         if (length > 2)
  146.         begin
  147.             for(funct_index = 0; funct_index <= 30; funct_index = funct_index + 1)
  148.             begin
  149.                 if(((head - length + 33) % 30 <= funct_index && funct_index <= head) || ((head - length + 33) % 30 > head && ((head - length + 33) % 30 <= funct_index || funct_index <= head)))
  150.                 begin
  151.                     if(Queue[funct_index] == ((Queue[head] + right_pos1 + 48) % 48))
  152.                     begin
  153.                         cross1 = 1;
  154.                     end
  155.                     if(Queue[funct_index] == ((Queue[head] + left_pos1 + 48) % 48))
  156.                     begin
  157.                         cross2 = 1;
  158.                     end
  159.                 end
  160.             end
  161.         end
  162.        
  163.         if(cross1 && cross2 || cross3)
  164.         begin
  165.             cross_tail = 1;
  166.         end
  167.      
  168.      
  169.       if(~cross_tail)
  170.       begin
  171.           head = (head + 1) % 30;
  172.           Queue[head] = (Queue[(head + 29) % 30] + d + 48) % 48;
  173.           HEX[Queue[head]] = 0;
  174.          
  175.           //Generate apple
  176.          
  177.           if(apple_pos == Queue[head])
  178.           begin
  179.                 LED[8] = ~LED[8];///////////////
  180.                 length = length + 1;
  181.                 apple_pos = (apple_pos + 31) % 48;
  182.                 if (apple_pos % 8 == 7)
  183.                     apple_pos = apple_pos - 1;
  184.                    
  185.                 //apple_taken <= 1;
  186.           end
  187.          
  188.           if(length == 6)
  189.           begin
  190.                  if (delay > 10)
  191.                  begin
  192.                     delay = delay - 1;
  193.                     length = 1;
  194.                     HEX = 48'hFFFFFFFFFFFF;
  195.                  end
  196.           end
  197.        end
  198.         else
  199.         begin
  200.  
  201.             HEX = 48'b100010011111111110001111100010001010001010000110;
  202.                
  203.             for (i = 0; i <= 30; i = i + 1)
  204.                 Queue[i] = 0;
  205.                
  206.             end_game = 1;
  207.             apple = 0;
  208.             apple_pos = 0;
  209.             delay = 20;
  210.             length = 1;
  211.             cross_tail = 0;
  212.             cross1 = 0;
  213.             cross2 = 0;
  214.             cross3 = 0;
  215.         end
  216.         end
  217.     end
  218. end
  219.  
  220. function integer move;
  221.  
  222. input integer direction, horizontal, vertical, p8;
  223.  
  224. begin
  225. if (direction == 0)
  226.       begin
  227.             if (p8 % 3 == 0)
  228.             begin
  229.                     if (horizontal == 0) move = 8;
  230.                     else move = -8;
  231.             end
  232.             else
  233.             begin
  234.                     if ((vertical == 0) && ((p8 == 2) || (p8 == 5)))
  235.                     begin
  236.                             if (p8 == 2)
  237.                             begin
  238.                                     if (horizontal == 0) move = 4;
  239.                                     else move = -4;
  240.                             end
  241.                             else
  242.                             begin
  243.                                     if (horizontal == 0) move = 9;
  244.                                     else move = 1;
  245.                             end
  246.                     end
  247.                     else if (vertical == 1 && (p8 == 1 || p8 == 4))
  248.                     begin
  249.                             if (p8 == 1)
  250.                             begin
  251.                                     if (horizontal == 0) move = -1;
  252.                                     else move = -9;
  253.                             end
  254.                             else
  255.                             begin
  256.                                     if (horizontal == 0) move = 4;
  257.                                     else move = -4;
  258.                             end
  259.                     end
  260.                     else
  261.                     begin
  262.                             if (p8 == 1 || p8 == 2)
  263.                             begin
  264.                                     if (horizontal == 0) move = 2;
  265.                                     else move = -6;
  266.                             end
  267.                             else
  268.                             begin
  269.                                     if (horizontal == 0) move = 7;
  270.                                     else move = -1;
  271.                             end
  272.                            
  273.                             if (p8 == 2 || p8 == 5)
  274.                                     move = move - 1;
  275.                     end
  276.             end
  277.       end
  278.       else
  279.       begin // direction == 1
  280.             if (p8 == 1 || p8 == 4) move = 1;
  281.             else if (p8 == 5 || p8 == 2) move = -1;
  282.             else if (p8 == 0)
  283.             begin
  284.                     if (horizontal == 0)
  285.                     begin
  286.                             if (vertical == 0) move = 4;
  287.                             else move = 5;
  288.                     end
  289.                     else
  290.                     begin
  291.                             if (vertical == 0) move = 1;
  292.                             else move = 2;
  293.                     end
  294.             end
  295.             else if (p8 == 3)
  296.             begin
  297.                     if (horizontal == 0)
  298.                     begin
  299.                             if (vertical == 0) move = 2;
  300.                             else move = 1;
  301.                     end
  302.                     else
  303.                     begin
  304.                             if (vertical == 0) move = -1;
  305.                             else move = -2;
  306.                     end
  307.             end
  308.             else if (p8 == 6)
  309.             begin
  310.                     if (horizontal == 0)
  311.                     begin
  312.                             if (vertical == 0) move = -2;
  313.                             else move = -1;
  314.                     end
  315.                     else
  316.                     begin
  317.                             if (vertical == 0) move = -5;
  318.                             else move = -4;
  319.                     end
  320.             end
  321.       end
  322.      
  323. end
  324.  
  325. endfunction
  326.  
  327.  
  328. integer j = 0;
  329. reg pressed = 0;
  330.  
  331. always @(posedge move_clk)
  332. begin
  333.     if (KEY[0] == 0 && ~pressed)
  334.     begin
  335.         pressed = 1;
  336.         if (direction == 0)
  337.         begin
  338.             if (horizontal == 0)
  339.             begin
  340.                 direction <= 1;
  341.                 vertical <= 1;
  342.             end
  343.             else
  344.             begin
  345.                 direction <= 1;
  346.                 vertical <= 0;
  347.             end
  348.         end
  349.         else
  350.         begin
  351.             if (vertical == 0)
  352.             begin
  353.                 direction <= 0;
  354.                 horizontal <= 0;
  355.             end
  356.             else
  357.             begin
  358.                 direction <= 0;
  359.                 horizontal <= 1;
  360.             end
  361.         end
  362.     end
  363.    
  364.     if (KEY[1] == 0 && ~pressed)
  365.     begin
  366.         pressed = 1;
  367.         if (direction == 0)
  368.         begin
  369.             if (horizontal == 0)
  370.             begin
  371.                 direction <= 1;
  372.                 vertical <= 0;
  373.             end
  374.             else
  375.             begin
  376.                 direction <= 1;
  377.                 vertical <= 1;
  378.             end
  379.         end
  380.         else
  381.         begin
  382.             if (vertical == 0)
  383.             begin
  384.                 direction <= 0;
  385.                 horizontal <= 1;
  386.             end
  387.             else
  388.             begin
  389.                 direction <= 0;
  390.                 horizontal <= 0;
  391.             end
  392.         end
  393.     end
  394.     if (apple == 4)
  395.     begin
  396.         pressed = 0;
  397.     end
  398. end
  399.  
  400. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement