`timescale 1 ns/1 ns module CTP_LTU_emu( input clk, input reset, input BUSY, input LM_SA, output reg LM_GL, output reg TTC_A); reg [2:0] state; reg clk_fast; integer GSrec, LMsent, L0sent, L1sent; integer counter; parameter GroundState = 3'b000, LevelM = 3'b001, Level0 = 3'b010, Level1 = 3'b011, standalone = 3'b100, standalone_0 = 3'b110, standalone_1 = 3'b111; initial begin LM_GL <= 0; TTC_A <= 0; state <= 0; clk_fast <= 0; counter <= 0; end always begin #1 clk_fast <= !clk_fast; end always @(posedge LM_SA) begin state = standalone_0; LM_GL <= 0; TTC_A <= 0; counter <= 0; end always @(clk_fast) begin if (reset == 1) begin state <= GroundState; counter <= 0; LM_GL <= 0; TTC_A <= 0; end else begin if (counter == 0) begin case (state) GroundState: begin if (BUSY == 0) begin GSrec <= $time; state <= LevelM; counter = $urandom_range(500,5); end end LevelM: begin LMsent <= $time; state <= Level0; counter = 300;//$urandom_range(305,245); if (counter > 300) $display("%t ns: L0 will arrive too late", $time/1000.0); if (counter < 250) $display("%t ns: L0 will arrive too early", $time/1000.0); LM_GL <= 1; #25 LM_GL <= 0; end Level0: begin L0sent <= $time; state <= Level1; counter = 5300;//$urandom_range(5305,5245); if (counter > 5300) $display("%t ns: L1 will arrive too late", $time/1000.0); if (counter < 5250) $display("%t ns: L1 will arrive too early", $time/1000.0); TTC_A <= 1; #25 TTC_A <= 0; end Level1: begin L1sent <= $time; state <= GroundState; TTC_A <= 1; #50 TTC_A <= 0; // $display("%t: Normal mode:", $time); // $display("Gs->LM: %t, LM: ",LMsent-GSrec, LMsent); // $display("LM->L0: %t, L0: ",L0sent-LMsent, L0sent); // $display("L0->L1: %t, L1:",L1sent-L0sent, L1sent); // $display(""); end standalone_0: begin L0sent <= $time; state <= standalone_1; counter = 5270;//$urandom_range(5285,5265); // 5300ns - 25ns (L0-Signal) +- 5ns (spare) +- 5ns (errors) = 5285/5265ns TTC_A <= 1; #25 TTC_A <= 0; end standalone_1: begin L1sent <= $time; state <= GroundState; TTC_A <= 1; #50 TTC_A <= 0; // $display("Standalone mode:"); // $display("Gs->LM: ",LMsent-GSrec); // $display("LM->L0: ",L0sent-LMsent); // $display("L0->L1: ",L1sent-L0sent); // $display(""); end endcase end else begin counter <= counter - 1; end end end endmodule