data/full_repos/permissive/194450802/enemy_updater.v
data/verilator_xmls/594fab18-0b62-41ad-bb65-44f9c34260de.xml
module _enemy_updater_datapath(clock, reset,
                               grid_x, grid_y, grid_out, grid_write, grid_in,
                               increment_grid_counter, check_possible_position, draw_new_position, erase_last_position, get_next_position, check_if_enemy, reset_counters,
                               is_enemy, can_goto_new_position, grid_counter_max, update_enemy_start);
     input clock, reset;

     output reg [5:0] grid_x;
     output reg [4:0] grid_y;
     input [2:0] grid_out;
     output reg grid_write;
     output reg [2:0] grid_in;


     input increment_grid_counter, check_possible_position, draw_new_position, erase_last_position, get_next_position, check_if_enemy, reset_counters;
     output reg is_enemy, can_goto_new_position, update_enemy_start;
     output grid_counter_max;


     reg [31:0] check_counter;
     always @(posedge clock) begin

       if (reset_counters | reset) begin
          update_enemy_start <= 0;
          check_counter <= 32'd200000;
       end
       if (check_counter != 32'b0)
          begin
              check_counter <= check_counter - 32'b1;
          end
       else begin
          update_enemy_start <= 1;
       end
     end







     reg [1:0] direction_counter;
     always @(posedge clock) begin
        if (direction_counter == 2'd3)
            direction_counter <= 2'd0;
        else
            direction_counter <= direction_counter + 1;
     end

     reg [5:0] curr_grid_x;
     reg [4:0] curr_grid_y;
     reg [5:0] next_grid_x;
     reg [4:0] next_grid_y;




     reg [2:0] grid_access;
     always @(posedge clock) begin
         if (check_possible_position)
             grid_access <= 3'd0;
         else if (draw_new_position)
             grid_access <= 3'd1;
         else if (erase_last_position)
             grid_access <= 3'd2;
         else if (check_if_enemy)
             grid_access <= 3'd3;
         else
             grid_access <= 3'd4;
     end

     always @(posedge clock) begin
         case (grid_access)
             3'd0: begin
               grid_x <= next_grid_x;
               grid_y <= next_grid_y;
               grid_write <= 0;
               grid_in <= 0;
             end
             3'd1: begin
               grid_x <= next_grid_x;
               grid_y <= next_grid_y;
               grid_write <= 1;
               grid_in <= 3'd4;
             end
             3'd2: begin
               grid_x <= curr_grid_x;
               grid_y <= curr_grid_y;
               grid_write <= 1;
               grid_in <= 3'd0;
             end
             3'd3: begin
               grid_x <= curr_grid_x;
               grid_y <= curr_grid_y;
	       grid_write <= 0;
             end
             default: begin
               grid_write <= 0;
             end
        endcase
     end

     always @(posedge clock) begin

         if (reset_counters | reset) begin
             curr_grid_x <= 6'b0;
             curr_grid_y <= 5'b0;
         end
         else if (increment_grid_counter) begin
             if (x_at_max) begin
                 curr_grid_x <= 0;
                 curr_grid_y <= curr_grid_y + 1;
             end
             else
                 curr_grid_x <= curr_grid_x + 1;
         end
         else if (check_if_enemy) begin
           is_enemy <= grid_out == 3'd4;
         end
         else if (get_next_position) begin

           if (direction_counter == 2'd0) begin
             next_grid_x <= curr_grid_x;
             next_grid_y <= curr_grid_y - 1;
           end

           if (direction_counter == 2'd1) begin
             next_grid_x <= curr_grid_x + 1;
             next_grid_y <= curr_grid_y;
           end

           if (direction_counter == 2'd2) begin
             next_grid_x <= curr_grid_x;
             next_grid_y <= curr_grid_y + 1;
           end

           if (direction_counter == 2'd3) begin
             next_grid_x <= curr_grid_x - 1;
             next_grid_y <= curr_grid_y;
           end
         end

         else if (check_possible_position) begin
           can_goto_new_position <= grid_out == 3'd0;
         end
     end


     assign grid_counter_max = x_at_max & y_at_max;



     wire x_at_max;
     wire y_at_max;
     assign x_at_max = curr_grid_x == 6'd39;
     assign y_at_max = curr_grid_y == 5'd29;
endmodule
module _enemy_updater_datapath(clock, reset,
                               grid_x, grid_y, grid_out, grid_write, grid_in,
                               increment_grid_counter, check_possible_position, draw_new_position, erase_last_position, get_next_position, check_if_enemy, reset_counters,
                               is_enemy, can_goto_new_position, grid_counter_max, update_enemy_start);

     input clock, reset;

     output reg [5:0] grid_x;
     output reg [4:0] grid_y;
     input [2:0] grid_out;
     output reg grid_write;
     output reg [2:0] grid_in;


     input increment_grid_counter, check_possible_position, draw_new_position, erase_last_position, get_next_position, check_if_enemy, reset_counters;
     output reg is_enemy, can_goto_new_position, update_enemy_start;
     output grid_counter_max;


     reg [31:0] check_counter;
     always @(posedge clock) begin

       if (reset_counters | reset) begin
          update_enemy_start <= 0;
          check_counter <= 32'd200000;
       end
       if (check_counter != 32'b0)
          begin
              check_counter <= check_counter - 32'b1;
          end
       else begin
          update_enemy_start <= 1;
       end
     end







     reg [1:0] direction_counter;
     always @(posedge clock) begin
        if (direction_counter == 2'd3)
            direction_counter <= 2'd0;
        else
            direction_counter <= direction_counter + 1;
     end

     reg [5:0] curr_grid_x;
     reg [4:0] curr_grid_y;
     reg [5:0] next_grid_x;
     reg [4:0] next_grid_y;




     reg [2:0] grid_access;
     always @(posedge clock) begin
         if (check_possible_position)
             grid_access <= 3'd0;
         else if (draw_new_position)
             grid_access <= 3'd1;
         else if (erase_last_position)
             grid_access <= 3'd2;
         else if (check_if_enemy)
             grid_access <= 3'd3;
         else
             grid_access <= 3'd4;
     end

     always @(posedge clock) begin
         case (grid_access)
             3'd0: begin
               grid_x <= next_grid_x;
               grid_y <= next_grid_y;
               grid_write <= 0;
               grid_in <= 0;
             end
             3'd1: begin
               grid_x <= next_grid_x;
               grid_y <= next_grid_y;
               grid_write <= 1;
               grid_in <= 3'd4;
             end
             3'd2: begin
               grid_x <= curr_grid_x;
               grid_y <= curr_grid_y;
               grid_write <= 1;
               grid_in <= 3'd0;
             end
             3'd3: begin
               grid_x <= curr_grid_x;
               grid_y <= curr_grid_y;
	       grid_write <= 0;
             end
             default: begin
               grid_write <= 0;
             end
        endcase
     end

     always @(posedge clock) begin

         if (reset_counters | reset) begin
             curr_grid_x <= 6'b0;
             curr_grid_y <= 5'b0;
         end
         else if (increment_grid_counter) begin
             if (x_at_max) begin
                 curr_grid_x <= 0;
                 curr_grid_y <= curr_grid_y + 1;
             end
             else
                 curr_grid_x <= curr_grid_x + 1;
         end
         else if (check_if_enemy) begin
           is_enemy <= grid_out == 3'd4;
         end
         else if (get_next_position) begin

           if (direction_counter == 2'd0) begin
             next_grid_x <= curr_grid_x;
             next_grid_y <= curr_grid_y - 1;
           end

           if (direction_counter == 2'd1) begin
             next_grid_x <= curr_grid_x + 1;
             next_grid_y <= curr_grid_y;
           end

           if (direction_counter == 2'd2) begin
             next_grid_x <= curr_grid_x;
             next_grid_y <= curr_grid_y + 1;
           end

           if (direction_counter == 2'd3) begin
             next_grid_x <= curr_grid_x - 1;
             next_grid_y <= curr_grid_y;
           end
         end

         else if (check_possible_position) begin
           can_goto_new_position <= grid_out == 3'd0;
         end
     end


     assign grid_counter_max = x_at_max & y_at_max;



     wire x_at_max;
     wire y_at_max;
     assign x_at_max = curr_grid_x == 6'd39;
     assign y_at_max = curr_grid_y == 5'd29;
endmodule