Advertisement
1WaKa_WaKa1

count_free

Apr 6th, 2023
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns/1ps
  2.  
  3. module count_free (
  4.     input clk,
  5.     input rst,
  6.     input start_req_i,
  7.     input start_data_i,
  8.     input ready_i,
  9.     output reg result_rsp_o,
  10.     output reg busy_o
  11. );
  12.  
  13. parameter IDLE =0, RECEIVE_DATA = 1, CHECK_DATA = 2, DONE = 3;
  14. reg[5:0] amount_of_edinichkas = 0;
  15. reg[5:0] check_amount_of_edinichkas = 0;
  16. reg[31:0] count_of_ticks = 0;
  17. reg[31:0] data = 0;
  18. reg[31:0] temp_data = 0;
  19. reg[1:0] state;
  20. reg[1:0] next_state = IDLE;
  21.  
  22. always @(negedge clk) begin
  23.     case (state)
  24.         IDLE: begin
  25.             next_state = RECEIVE_DATA;
  26.             busy_o = 0;
  27.             result_rsp_o = 0;
  28.         end
  29.         RECEIVE_DATA : begin
  30.             next_state = CHECK_DATA;
  31.             busy_o = 1;
  32.         end
  33.         CHECK_DATA : begin
  34.             next_state = DONE;
  35.             busy_o = 1;
  36.         end
  37.         DONE : begin    
  38.             next_state = IDLE;
  39.             result_rsp_o = 1;
  40.             busy_o = 0;
  41.         end
  42. endcase
  43. end
  44.  
  45. always @(posedge clk or posedge rst) begin
  46.     if (rst) begin
  47.         state <= IDLE;
  48.         amount_of_edinichkas <= 0;
  49.         check_amount_of_edinichkas <= 0;
  50.         data <= 0;
  51.         count_of_ticks <= 0;
  52.         temp_data <= 0;
  53.        
  54.     end
  55. end
  56.  
  57. always @(posedge clk) begin
  58.     case (state)
  59.         IDLE : begin
  60.             if (start_req_i) begin
  61.                 data <= start_data_i;
  62.                 state <= next_state;
  63.             end
  64.         end
  65.         RECEIVE_DATA : begin
  66.             if (start_data_i) begin
  67.                 amount_of_edinichkas <= amount_of_edinichkas + 1;
  68.             end
  69.             if (start_req_i) begin
  70.                 data <= (data << 1) + start_data_i;
  71.             end else begin
  72.                 temp_data <= data;
  73.                 state <= next_state;
  74.             end
  75.         end
  76.         CHECK_DATA: begin
  77.             if (count_of_ticks == data && amount_of_edinichkas == check_amount_of_edinichkas) begin
  78.                 state <= next_state;
  79.             end
  80.             else begin
  81.                 count_of_ticks <= count_of_ticks + 1;
  82.                 if (temp_data[0] & temp_data !=0) begin
  83.                     check_amount_of_edinichkas <= check_amount_of_edinichkas + 1;
  84.                 end
  85.                 temp_data <= temp_data >> 1;
  86.             end
  87.         end
  88.         DONE : begin
  89.             count_of_ticks <= 0;
  90.             if (ready_i) begin
  91.                 state <= next_state;
  92.             end    
  93.         end
  94.         endcase
  95. end
  96. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement