Advertisement
regzarr

TimeMeasurement module (LSIC)

Nov 19th, 2024 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module TimeMeasurement(
  2.     input clk,
  3.     input rst,
  4.     input [1:0] opcode,
  5.     output [31:0] ms_count
  6. );
  7.  
  8.     reg [31:0] clk_counter_reg, clk_counter_nxt;
  9.     reg [31:0] ms_count_reg, ms_count_nxt;
  10.     reg running;
  11.  
  12.     always @(posedge clk or negedge rst) begin
  13.         if (!rst) begin
  14.             clk_counter_reg <= 0;
  15.             ms_count_reg <= 0;
  16.         end else begin
  17.             clk_counter_reg <= clk_counter_nxt;
  18.             ms_count_reg <= ms_count_nxt;
  19.         end
  20.     end
  21.  
  22.     always @(*) begin
  23.         clk_counter_nxt = clk_counter_reg;
  24.         ms_count_nxt = ms_count_reg;
  25.  
  26.         if (running) begin
  27.             if (clk_counter_reg == 2499) begin
  28.                 clk_counter_nxt = 0;
  29.                 ms_count_nxt = ms_count_reg + 1;
  30.             end else begin
  31.                 clk_counter_nxt = clk_counter_reg + 1;
  32.             end
  33.         end
  34.  
  35.         case (opcode)
  36.             2'b01: running = 1;   // Start
  37.             2'b11: running = 0;   // Pause
  38.             2'b10: begin          // Stop
  39.                 running = 0;
  40.                 ms_count_nxt = 0;
  41.             end
  42.             default: running = 0; // Default state
  43.         endcase
  44.     end
  45.  
  46.     assign ms_count = ms_count_reg;
  47.  
  48. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement