module CTPEmulator( input clk, input reset, input enable, input pulser, input start_single, input [1:0] mode, input BUSY_int, input BUSY_GTU, input [5:0] LMtoLMSA, input [25:0] CTPEmu_freq, input [31:0] CTPEmu_threshold, input [31:0] CTPEmu_LMSA_threshold, output reg LM, output reg LM_SA, output [31:0] rnd, output reg [8:0] state_mon); reg [2:0] state; reg enable_local; reg [1:0] mode_local; reg [25:0] counter; reg [5:0] LMtoLMSA_local; reg [25:0] CTPEmu_freq_local; reg [31:0] CTPEmu_threshold_local; reg [31:0] CTPEmu_LMSA_threshold_local; MersenneTwister mt( .clk(clk), .rnd(rnd)); parameter M_single = 2'b00, M_pulser = 2'b01, M_BC = 2'b10, M_rnd = 2'b11; parameter S_GS = 3'b000, S_LM_sent = 3'b010, S_LMSA_sent = 3'b011, S_WAIT_NEXT = 3'b100, S_OFF = 3'b111; initial begin LM <= 0; LM_SA <= 0; state <= S_OFF; counter <= 0; end always @(posedge clk) begin if (reset) begin LM <= 0; LM_SA <= 0; state <= S_OFF; counter <= 0; end else begin LM <= 0; LM_SA <= 0; enable_local <= enable; state_mon <= {enable,mode_local,BUSY_int,LM,LM_SA,state}; case (state) S_GS: begin mode_local <= mode; LMtoLMSA_local <= LMtoLMSA; CTPEmu_freq_local <= CTPEmu_freq; CTPEmu_threshold_local <= CTPEmu_threshold; CTPEmu_LMSA_threshold_local <= CTPEmu_LMSA_threshold; counter <= 0; if (BUSY_GTU == 1'b0 && BUSY_int == 1'b0) begin case(mode_local) M_single: begin if (start_single == 1'b1) begin LM <= 1'b1; state <= S_LM_sent; counter <= 1'b1; end end M_pulser: begin if (pulser == 1'b1) begin LM <= 1'b1; state <= S_LM_sent; counter <= 1'b1; end end M_BC: begin LM <= 1'b1; state <= S_LM_sent; counter <= 1'b1; end M_rnd: begin if (rnd >= CTPEmu_threshold_local) begin LM <= 1'b1; state <= S_LM_sent; counter <= 1'b1; end end endcase end end S_LM_sent: begin if (counter >= LMtoLMSA_local) begin if (rnd >= CTPEmu_LMSA_threshold_local) begin LM_SA <= 1'b1; end state <= S_LMSA_sent; end counter <= counter + 1'b1; end S_LMSA_sent: begin if (mode_local == M_BC) state <= S_WAIT_NEXT; else state <= S_GS; counter <= counter + 1'b1; end S_WAIT_NEXT: begin if (counter >= CTPEmu_freq_local) state <= S_GS; counter <= counter + 1'b1; end S_OFF: begin end default: begin state <= S_GS; end endcase if (enable == 1'b1 && enable_local == 1'b0) begin state <= S_GS; end if (enable == 1'b0 && enable_local == 1'b1) begin state <= S_OFF; end end end endmodule