data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v
1: b'%Warning-IMPLICIT: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:366: Signal definition not found, creating implicitly: \'TimeStamp_Delta_lsb_reg_we_dcd\'\nassign TimeStamp_Delta_lsb_reg_we_dcd    = (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_GENERIC_LSB}) & wb_cyc & wb_stb &  wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;\n       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n                   ... Use "/* verilator lint_off IMPLICIT */" and lint_on around source to disable this message.\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:458: Operator ASSIGNDLY expects 1 bits on the Assign RHS, but Assign RHS\'s CONST \'4\'h0\' generates 4 bits.\n                                                                                                                                       : ... In instance SensorManager\n        SensorInterrupt_0_1ff    <=  4\'h0;\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:459: Operator ASSIGNDLY expects 1 bits on the Assign RHS, but Assign RHS\'s CONST \'4\'h0\' generates 4 bits.\n                                                                                                                                       : ... In instance SensorManager\n        SensorInterrupt_0_2ff    <=  4\'h0;\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:460: Operator ASSIGNDLY expects 1 bits on the Assign RHS, but Assign RHS\'s CONST \'4\'h0\' generates 4 bits.\n                                                                                                                                       : ... In instance SensorManager\n        SensorInterrupt_0_3ff    <=  4\'h0;\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:462: Operator ASSIGNDLY expects 1 bits on the Assign RHS, but Assign RHS\'s CONST \'4\'h0\' generates 4 bits.\n                                                                                                                                       : ... In instance SensorManager\n        SensorInterrupt_1_1ff    <=  4\'h0;\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:463: Operator ASSIGNDLY expects 1 bits on the Assign RHS, but Assign RHS\'s CONST \'4\'h0\' generates 4 bits.\n                                                                                                                                       : ... In instance SensorManager\n        SensorInterrupt_1_2ff    <=  4\'h0;\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:464: Operator ASSIGNDLY expects 1 bits on the Assign RHS, but Assign RHS\'s CONST \'4\'h0\' generates 4 bits.\n                                                                                                                                       : ... In instance SensorManager\n        SensorInterrupt_1_3ff    <=  4\'h0;\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:466: Operator ASSIGNDLY expects 1 bits on the Assign RHS, but Assign RHS\'s CONST \'4\'h0\' generates 4 bits.\n                                                                                                                                       : ... In instance SensorManager\n        SensorInterrupt_2_1ff    <=  4\'h0;\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:467: Operator ASSIGNDLY expects 1 bits on the Assign RHS, but Assign RHS\'s CONST \'4\'h0\' generates 4 bits.\n                                                                                                                                       : ... In instance SensorManager\n        SensorInterrupt_2_2ff    <=  4\'h0;\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:468: Operator ASSIGNDLY expects 1 bits on the Assign RHS, but Assign RHS\'s CONST \'4\'h0\' generates 4 bits.\n                                                                                                                                       : ... In instance SensorManager\n        SensorInterrupt_2_3ff    <=  4\'h0;\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:470: Operator ASSIGNDLY expects 1 bits on the Assign RHS, but Assign RHS\'s CONST \'4\'h0\' generates 4 bits.\n                                                                                                                                       : ... In instance SensorManager\n        SensorInterrupt_3_1ff    <=  4\'h0;\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:471: Operator ASSIGNDLY expects 1 bits on the Assign RHS, but Assign RHS\'s CONST \'4\'h0\' generates 4 bits.\n                                                                                                                                       : ... In instance SensorManager\n        SensorInterrupt_3_2ff    <=  4\'h0;\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:472: Operator ASSIGNDLY expects 1 bits on the Assign RHS, but Assign RHS\'s CONST \'4\'h0\' generates 4 bits.\n                                                                                                                                       : ... In instance SensorManager\n        SensorInterrupt_3_3ff    <=  4\'h0;\n                                 ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:607: Operator ASSIGNDLY expects 8 bits on the Assign RHS, but Assign RHS\'s VARREF \'TimeStamp_Delta_capt\' generates 16 bits.\n                                                                                                                                       : ... In instance SensorManager\n            TimeStamp_Delta_sensor_0 <=  TimeStamp_Delta_capt;\n                                     ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:611: Operator ASSIGNDLY expects 8 bits on the Assign RHS, but Assign RHS\'s VARREF \'TimeStamp_Delta_capt\' generates 16 bits.\n                                                                                                                                       : ... In instance SensorManager\n            TimeStamp_Delta_sensor_1 <=  TimeStamp_Delta_capt;\n                                     ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:615: Operator ASSIGNDLY expects 8 bits on the Assign RHS, but Assign RHS\'s VARREF \'TimeStamp_Delta_capt\' generates 16 bits.\n                                                                                                                                       : ... In instance SensorManager\n            TimeStamp_Delta_sensor_2 <=  TimeStamp_Delta_capt;\n                                     ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:619: Operator ASSIGNDLY expects 8 bits on the Assign RHS, but Assign RHS\'s VARREF \'TimeStamp_Delta_capt\' generates 16 bits.\n                                                                                                                                       : ... In instance SensorManager\n            TimeStamp_Delta_sensor_3 <=  TimeStamp_Delta_capt;\n                                     ^~\n%Warning-WIDTH: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:859: Operator ASSIGNDLY expects 9 bits on the Assign RHS, but Assign RHS\'s REPLICATE generates 8 bits.\n                                                                                                                                       : ... In instance SensorManager\n        TIMESTAMP_DELTA_INT_EVENT    : MemWriteDataOut <= {4\'h0, SensorInterrupt_3_o,\n                                                       ^~\n%Error: data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src/SensorManager.v:900: Cannot find file containing module: \'StateMachine\'\nStateMachine StateMachine_inst      (\n^~~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src,data/full_repos/permissive/137416811/StateMachine\n             data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src,data/full_repos/permissive/137416811/StateMachine.v\n             data/full_repos/permissive/137416811/openfpga_flow/benchmarks/quicklogic_tests/ULPSH_fabric/rtl/src,data/full_repos/permissive/137416811/StateMachine.sv\n             StateMachine\n             StateMachine.v\n             StateMachine.sv\n             obj_dir/StateMachine\n             obj_dir/StateMachine.v\n             obj_dir/StateMachine.sv\n%Error: Exiting due to 1 error(s), 18 warning(s)\n'
module SensorManager (


	                ClockIn,
	                ResetIn,
                    StartFromFFE,
                    StartFromTLC,
	                BusyOut,
                    TimeStamp_Delta_i,
                    TimeStamp_Delta_Tog_i,
                    SensorInterrupt_0_i,
                    SensorInterrupt_1_i,
                    SensorInterrupt_2_i,
                    SensorInterrupt_3_i,
                    SensorInterrupt_0_o,
                    SensorInterrupt_1_o,
                    SensorInterrupt_2_o,
                    SensorInterrupt_3_o,
                    CtrlRunTimeAddressSM,
                    CtrlRunTimeAddressOut,
                    CtrlRunTimeAddressReg,
	                MemReadAddressOut,
	                MemReadEnableOut,
	                MemReadDataIn,
	                MemWriteAddressOut,
	                MemWriteEnableOut,
	                MemWriteDataOut,
	                MemClockOut,
	                I2C_wb_adr_o,
	                I2C_wb_dat_o,
	                I2C_wb_dat_i,
	                I2C_wb_we_o,
	                I2C_wb_stb_o,
	                I2C_wb_cyc_o,
	                I2C_wb_ack_i,
	                I2C_tip_i,
	                TP1,
	                TP2,
	                TP3,
					SmClockSelect_o
);	
	



input               ClockIn;
input               ResetIn;
input           	StartFromFFE;
input               StartFromTLC;
output              BusyOut;
input       [15:0]  TimeStamp_Delta_i;
input               TimeStamp_Delta_Tog_i;
input               SensorInterrupt_0_i;
input               SensorInterrupt_1_i;
input               SensorInterrupt_2_i;
input               SensorInterrupt_3_i;
output              SensorInterrupt_0_o;
output              SensorInterrupt_1_o;
output              SensorInterrupt_2_o;
output              SensorInterrupt_3_o;
input               CtrlRunTimeAddressSM;
input        [9:0]  CtrlRunTimeAddressOut;
output       [9:0]  CtrlRunTimeAddressReg;
output       [9:0]  MemReadAddressOut;
output              MemReadEnableOut;
input       [17:0]  MemReadDataIn;
output       [9:0]  MemWriteAddressOut;
output              MemWriteEnableOut;
output       [8:0]  MemWriteDataOut;
output              MemClockOut;
output       [2:0]  I2C_wb_adr_o;
output       [7:0]  I2C_wb_dat_o;
input        [7:0]  I2C_wb_dat_i;
output              I2C_wb_we_o;
output              I2C_wb_stb_o;
output              I2C_wb_cyc_o;
input               I2C_wb_ack_i;
input               I2C_tip_i;
output              TP1;
output              TP2;
output              TP3;
output				SmClockSelect_o;


wire                ClockIn;
wire                ResetIn;
wire                StartFromFFE;
wire            	StartFromTLC;
wire                BusyOut;
wire        [15:0]  TimeStamp_Delta_i;
wire                TimeStamp_Delta_Tog_i;
wire                SensorInterrupt_0_i;
wire                SensorInterrupt_1_i;
wire                SensorInterrupt_2_i;
wire                SensorInterrupt_3_i;
reg                 SensorInterrupt_0_o;
reg                 SensorInterrupt_1_o;
reg                 SensorInterrupt_2_o;
reg                 SensorInterrupt_3_o;
wire                CtrlRunTimeAddressSM;
wire         [9:0]  CtrlRunTimeAddressOut;
reg          [9:0]  CtrlRunTimeAddressReg;
wire  		 [9:0]  MemReadAddressOut;
wire                MemReadEnableOut;
wire        [17:0]  MemReadDataIn;
wire         [9:0]  MemWriteAddressOut;
wire                MemWriteEnableOut;
reg          [8:0]  MemWriteDataOut;
wire         [2:0]  I2C_wb_adr_o;
wire         [7:0]  I2C_wb_dat_o;
wire         [7:0]  I2C_wb_dat_i;
wire                I2C_wb_we_o;
wire                I2C_wb_stb_o;
wire                I2C_wb_cyc_o;
wire                I2C_wb_ack_i;
wire                I2C_tip_i;
wire                MemClockOut;
wire                TP1;
wire                TP2;
wire                TP3;
reg					SmClockSelect_o;





wire		    	wb_cyc;

wire			    wb_we;
wire			    wb_stb;

wire                wb_ack;
reg                 wb_ack_sm;

wire                wb_busy_poll;

reg          [9:0]  CtrlReceiveAddressReg;

reg          [5:0]  CtrlMailBoxSegmentCtr;
reg                 CtrlMailBoxSegmentCtr_ce;

reg          [9:2]  CtrlMailBoxTablePtr;

reg          [9:0]  CtrlMailBoxJumpInstPtr;

reg                 CtrlMailBoxJumpInstCycle;
reg                 CtrlMailBoxJumpInstCycle_ce;
	
wire         [9:0]  StateMachineCtrlMemAddr;
wire         [7:0]  i2c_masterDataToMem;

wire                save_reg_2_mem;

wire                control_sensor_manager_reg_dcd;
wire                control_receive_reg_dcd;
wire                control_runtime_reg_dcd;
wire                control_jump_reg_dcd;
wire                control_mailbox_tbl_ptr_dcd;
wire                control_mailbox_jump_inst_ptr_dcd;

reg                 RunSM;
wire                BusySM;

wire                StartIn_stb;

reg                 StartFromFFE_1ff;
reg                 StartFromFFE_2ff;
reg                 StartFromFFE_3ff;
reg                 StartFromFFE_4ff;
reg                 StartFromFFE_5ff;
reg                 StartFromFFE_6ff;

reg                 StartFromTLC_1ff;
reg                 StartFromTLC_2ff;

reg                 CtrlRunTimeAddressSM_1ff;
reg                 CtrlRunTimeAddressSM_2ff;

reg					s1_BusyOut, s2_BusyOut;



wire                SensorInterrupt_event_reg_we_dcd;

wire                SensorInterrupt_event_mask_we_dcd;

reg                 SensorInterrupt_event_mask_0;
reg                 SensorInterrupt_event_mask_1;
reg                 SensorInterrupt_event_mask_2;
reg                 SensorInterrupt_event_mask_3;




reg          [7:0]  TimeStamp_Delta_sensor_0;
reg          [7:0]  TimeStamp_Delta_sensor_1;
reg          [7:0]  TimeStamp_Delta_sensor_2;
reg          [7:0]  TimeStamp_Delta_sensor_3;

reg         [15:0]  TimeStamp_Delta_capt;
reg         [15:0]  TimeStamp_Delta_readback;

reg                 TimeStamp_Delta_Tog_1ff;
reg                 TimeStamp_Delta_Tog_2ff;
reg                 TimeStamp_Delta_Tog_3ff;



reg                 SensorInterrupt_0_1ff;
reg                 SensorInterrupt_0_2ff;
reg                 SensorInterrupt_0_3ff;

reg                 SensorInterrupt_1_1ff;
reg                 SensorInterrupt_1_2ff;
reg                 SensorInterrupt_1_3ff;

reg                 SensorInterrupt_2_1ff;
reg                 SensorInterrupt_2_2ff;
reg                 SensorInterrupt_2_3ff;

reg                 SensorInterrupt_3_1ff;
reg                 SensorInterrupt_3_2ff;
reg                 SensorInterrupt_3_3ff;




wire                control_wait_instr_reg_dcd;

reg         [12:0]  control_wait_instr_cntr;
reg         [12:0]  control_wait_instr_cntr_nxt;

reg                 control_wait_instr_cntr_tc;
reg                 control_wait_instr_cntr_tc_nxt;

reg                 control_wait_instr_busy;
wire                control_wait_instr_busy_nxt;











parameter SENSOR_MANAGER_ADR      = 5'h0;
parameter I2C_MASTER_ADR          = 5'h1;
parameter TIMESTAMP_DELTA_ADR     = 5'h2;








parameter CONTROL_SENSOR_MANAGER_ADDRESS  = 3'h0;
parameter CONTROL_RECEIVE_ADDRESS         = 3'h1;
parameter CONTROL_RUNTIME_ADDRESS         = 3'h2;
parameter CONTROL_WAIT_INSTR_ADDRESS      = 3'h4;
parameter CONTROL_MAILBOX_TABLE_PTR       = 3'h5;
parameter CONTROL_MAILBOX_JUMP_INST_PTR   = 3'h6;
parameter CONTROL_JUMP_ADDRESS            = 3'h7;







parameter I2C_MASTER_PRELO		  = 3'h0;
parameter I2C_MASTER_PREHI        = 3'h1;
parameter I2C_MASTER_CTR          = 3'h2;
parameter I2C_MASTER_TXR          = 3'h3;
parameter I2C_MASTER_CR           = 3'h4;






parameter TIMESTAMP_DELTA_SENSOR_0    = 3'h0;
parameter TIMESTAMP_DELTA_SENSOR_1    = 3'h1;
parameter TIMESTAMP_DELTA_SENSOR_2    = 3'h2;
parameter TIMESTAMP_DELTA_SENSOR_3    = 3'h3;
parameter TIMESTAMP_DELTA_GENERIC_LSB = 3'h4;
parameter TIMESTAMP_DELTA_GENERIC_MSB = 3'h5;
parameter TIMESTAMP_DELTA_INT_EVENT   = 3'h6;





parameter MAILBOX_SM_ADDRESS      = 8'hFF;






assign  I2C_wb_dat_o                     = MemReadDataIn[15:8];
assign  i2c_masterDataToMem              = I2C_wb_dat_i;
assign  I2C_wb_we_o                      = wb_we;
assign  I2C_wb_stb_o                     = wb_stb;



assign I2C_wb_cyc_o                      = (MemReadDataIn[7:3] == I2C_MASTER_ADR) & wb_cyc & ~CtrlMailBoxJumpInstCycle;




assign control_sensor_manager_reg_dcd  = (MemReadDataIn[7:0] == {SENSOR_MANAGER_ADR, CONTROL_SENSOR_MANAGER_ADDRESS}) & wb_cyc & wb_stb & wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;
assign control_runtime_reg_dcd         = (MemReadDataIn[7:0] == {SENSOR_MANAGER_ADR, CONTROL_RUNTIME_ADDRESS})        & wb_cyc & wb_stb & wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;

assign control_wait_instr_reg_dcd      = (MemReadDataIn[7:0] == {SENSOR_MANAGER_ADR, CONTROL_WAIT_INSTR_ADDRESS})     & wb_cyc & wb_stb & wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;

assign control_receive_reg_dcd         = ((MemReadDataIn[7:0] == { SENSOR_MANAGER_ADR, CONTROL_RECEIVE_ADDRESS})
		                               |  (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_0})
                                       |  (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_1})
                                       |  (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_2})
                                       |  (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_3})
                                       |  (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_GENERIC_MSB})
                                       |  (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_GENERIC_LSB})) & wb_cyc & wb_stb & wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;







assign SensorInterrupt_event_reg_we_dcd  = (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_INT_EVENT})   & wb_cyc & wb_stb & ~wb_we &  wb_ack_sm & ~CtrlMailBoxJumpInstCycle;
assign SensorInterrupt_event_mask_we_dcd = (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_INT_EVENT})   & wb_cyc & wb_stb &  wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;
assign TimeStamp_Delta_lsb_reg_we_dcd    = (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_GENERIC_LSB}) & wb_cyc & wb_stb &  wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;










assign control_jump_reg_dcd              = (MemReadDataIn[7:0] == {SENSOR_MANAGER_ADR, CONTROL_JUMP_ADDRESS})                                                 & ~CtrlMailBoxJumpInstCycle;









assign control_mailbox_tbl_ptr_dcd       = (MemReadDataIn[7:0] == {SENSOR_MANAGER_ADR, CONTROL_MAILBOX_TABLE_PTR})     & wb_cyc & wb_stb & wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;
assign control_mailbox_jump_inst_ptr_dcd = (MemReadDataIn[7:0] == {SENSOR_MANAGER_ADR, CONTROL_MAILBOX_JUMP_INST_PTR}) & wb_cyc & wb_stb & wb_we &  wb_ack_sm & ~CtrlMailBoxJumpInstCycle;







assign save_reg_2_mem                  = ((MemReadDataIn[7:0] == {     I2C_MASTER_ADR, I2C_MASTER_CR}) &  MemReadDataIn[13] & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_0})           & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_1})           & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_2})           & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_3})           & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_GENERIC_LSB})        & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_GENERIC_MSB})        & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_INT_EVENT})          & ~CtrlMailBoxJumpInstCycle);








assign wb_busy_poll                    = ((MemReadDataIn[7:0] == {I2C_MASTER_ADR,     I2C_MASTER_CR             }) & (MemReadDataIn[12] | MemReadDataIn[13]) & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {SENSOR_MANAGER_ADR, CONTROL_WAIT_INSTR_ADDRESS})                                           & ~CtrlMailBoxJumpInstCycle);









assign StartIn_stb = (StartFromFFE_5ff ^ StartFromFFE_6ff)
                   | (StartFromTLC_1ff ^ StartFromTLC_2ff);




always @(posedge ClockIn or posedge ResetIn) 
begin
    if (ResetIn)
    begin
		wb_ack_sm             <= 1'b0;

        StartFromFFE_1ff      <= 1'b0;
        StartFromFFE_2ff      <= 1'b0;
		StartFromFFE_3ff      <= 1'b0;
        StartFromFFE_4ff      <= 1'b0;
		StartFromFFE_5ff      <= 1'b0;
        StartFromFFE_6ff      <= 1'b0;

        StartFromTLC_1ff      <= 1'b0;
        StartFromTLC_2ff      <= 1'b0;

		RunSM                 <= 1'b0;

		CtrlReceiveAddressReg <= 10'h0;
		CtrlRunTimeAddressReg <= 10'h0;

        CtrlRunTimeAddressSM_1ff <=  1'b0;
        CtrlRunTimeAddressSM_2ff <=  1'b0;

        TimeStamp_Delta_sensor_0 <=  8'h0;
        TimeStamp_Delta_sensor_1 <=  8'h0;
        TimeStamp_Delta_sensor_2 <=  8'h0;
        TimeStamp_Delta_sensor_3 <=  8'h0;

        SensorInterrupt_0_1ff    <=  4'h0;
        SensorInterrupt_0_2ff    <=  4'h0;
        SensorInterrupt_0_3ff    <=  4'h0;

        SensorInterrupt_1_1ff    <=  4'h0;
        SensorInterrupt_1_2ff    <=  4'h0;
        SensorInterrupt_1_3ff    <=  4'h0;

        SensorInterrupt_2_1ff    <=  4'h0;
        SensorInterrupt_2_2ff    <=  4'h0;
        SensorInterrupt_2_3ff    <=  4'h0;

        SensorInterrupt_3_1ff    <=  4'h0;
        SensorInterrupt_3_2ff    <=  4'h0;
        SensorInterrupt_3_3ff    <=  4'h0;

        SensorInterrupt_0_o      <=  1'b0;
        SensorInterrupt_1_o      <=  1'b0;
        SensorInterrupt_2_o      <=  1'b0;
        SensorInterrupt_3_o      <=  1'b0;

        SensorInterrupt_event_mask_0 <= 1'b0;
        SensorInterrupt_event_mask_1 <= 1'b0;
        SensorInterrupt_event_mask_2 <= 1'b0;
        SensorInterrupt_event_mask_3 <= 1'b0;

        TimeStamp_Delta_capt     <= 16'h0;
        TimeStamp_Delta_readback <= 16'h0;

        TimeStamp_Delta_Tog_1ff  <=  1'b0;
        TimeStamp_Delta_Tog_2ff  <=  1'b0;
        TimeStamp_Delta_Tog_3ff  <=  1'b0;

        CtrlMailBoxSegmentCtr       <=  6'h0;
        CtrlMailBoxSegmentCtr_ce    <=  1'b0;

        CtrlMailBoxTablePtr         <= MAILBOX_SM_ADDRESS;
        CtrlMailBoxJumpInstPtr      <= 10'h0;

        CtrlMailBoxJumpInstCycle    <=  1'b0;
        CtrlMailBoxJumpInstCycle_ce <=  1'b0;

        control_wait_instr_cntr     <= 13'h0;
        control_wait_instr_cntr_tc  <=  1'b0;
        control_wait_instr_busy     <=  1'b0;
    end
    else 
    begin  
		wb_ack_sm             <= ((MemReadDataIn[7:3] == SENSOR_MANAGER_ADR )
		                       |  (MemReadDataIn[7:3] == TIMESTAMP_DELTA_ADR)
		                       |  (CtrlMailBoxJumpInstCycle))                 & wb_cyc & wb_stb & ~wb_ack_sm;




        StartFromFFE_1ff      <= StartFromFFE;
        StartFromFFE_2ff      <= StartFromFFE_1ff;
		StartFromFFE_3ff      <= StartFromFFE_2ff;
        StartFromFFE_4ff      <= StartFromFFE_3ff;
		StartFromFFE_5ff      <= StartFromFFE_4ff;
        StartFromFFE_6ff      <= StartFromFFE_5ff;

        StartFromTLC_1ff      <= StartFromTLC;
        StartFromTLC_2ff      <= StartFromTLC_1ff;

        CtrlRunTimeAddressSM_1ff <= CtrlRunTimeAddressSM;
        CtrlRunTimeAddressSM_2ff <= CtrlRunTimeAddressSM_1ff;







		if (StartIn_stb)
            RunSM             <= 1'b1;
        else if (control_sensor_manager_reg_dcd)
            RunSM             <= MemReadDataIn[8];



        if (control_receive_reg_dcd)
			CtrlReceiveAddressReg <= MemReadDataIn[17:8];



        if (control_runtime_reg_dcd)
		    CtrlRunTimeAddressReg <= MemReadDataIn[17:8];
	    else if ( CtrlRunTimeAddressSM_1ff ^ CtrlRunTimeAddressSM_2ff)
		    CtrlRunTimeAddressReg <= CtrlRunTimeAddressOut;




        SensorInterrupt_0_1ff    <=  SensorInterrupt_0_i;
        SensorInterrupt_0_2ff    <=  SensorInterrupt_0_1ff;
        SensorInterrupt_0_3ff    <=  SensorInterrupt_0_2ff;

        SensorInterrupt_1_1ff    <=  SensorInterrupt_1_i;
        SensorInterrupt_1_2ff    <=  SensorInterrupt_1_1ff;
        SensorInterrupt_1_3ff    <=  SensorInterrupt_1_2ff;

        SensorInterrupt_2_1ff    <=  SensorInterrupt_2_i;
        SensorInterrupt_2_2ff    <=  SensorInterrupt_2_1ff;
        SensorInterrupt_2_3ff    <=  SensorInterrupt_2_2ff;

        SensorInterrupt_3_1ff    <=  SensorInterrupt_3_i;
        SensorInterrupt_3_2ff    <=  SensorInterrupt_3_1ff;
        SensorInterrupt_3_3ff    <=  SensorInterrupt_3_2ff;

        TimeStamp_Delta_Tog_1ff  <=  TimeStamp_Delta_Tog_i;
        TimeStamp_Delta_Tog_2ff  <=  TimeStamp_Delta_Tog_1ff;
        TimeStamp_Delta_Tog_3ff  <=  TimeStamp_Delta_Tog_2ff;















		if (TimeStamp_Delta_Tog_2ff ^ TimeStamp_Delta_Tog_3ff)
            TimeStamp_Delta_capt     <= TimeStamp_Delta_i;










        if (TimeStamp_Delta_lsb_reg_we_dcd)
            TimeStamp_Delta_readback <= TimeStamp_Delta_capt;






        if (SensorInterrupt_0_2ff && (!SensorInterrupt_0_3ff))
            TimeStamp_Delta_sensor_0 <=  TimeStamp_Delta_capt;


        if (SensorInterrupt_1_2ff && (!SensorInterrupt_1_3ff))
            TimeStamp_Delta_sensor_1 <=  TimeStamp_Delta_capt;


        if (SensorInterrupt_2_2ff && (!SensorInterrupt_2_3ff))
            TimeStamp_Delta_sensor_2 <=  TimeStamp_Delta_capt;


        if (SensorInterrupt_3_2ff && (!SensorInterrupt_3_3ff))
            TimeStamp_Delta_sensor_3 <=  TimeStamp_Delta_capt;















        if (SensorInterrupt_event_mask_we_dcd)
		begin
            SensorInterrupt_event_mask_0 <= MemReadDataIn[8];
            SensorInterrupt_event_mask_1 <= MemReadDataIn[9];
            SensorInterrupt_event_mask_2 <= MemReadDataIn[10];
            SensorInterrupt_event_mask_3 <= MemReadDataIn[11];
		end














        if (SensorInterrupt_event_reg_we_dcd && SensorInterrupt_event_mask_0)
            SensorInterrupt_0_o       <=  1'h0;
        else if (SensorInterrupt_0_2ff && (!SensorInterrupt_0_3ff))
            SensorInterrupt_0_o       <=  1'h1;


        if (SensorInterrupt_event_reg_we_dcd && SensorInterrupt_event_mask_1)
            SensorInterrupt_1_o       <=  1'h0;
        else if (SensorInterrupt_1_2ff && (!SensorInterrupt_1_3ff))
            SensorInterrupt_1_o       <=  1'h1;


        if (SensorInterrupt_event_reg_we_dcd && SensorInterrupt_event_mask_2)
            SensorInterrupt_2_o       <=  1'h0;
        else if (SensorInterrupt_2_2ff && (!SensorInterrupt_2_3ff))
            SensorInterrupt_2_o       <=  1'h1;


        if (SensorInterrupt_event_reg_we_dcd && SensorInterrupt_event_mask_3)
            SensorInterrupt_3_o       <=  1'h0;
        else if (SensorInterrupt_3_2ff && (!SensorInterrupt_3_3ff))
            SensorInterrupt_3_o       <=  1'h1;









        if (!BusySM)
            CtrlMailBoxSegmentCtr    <=  6'h0;
        else if (CtrlMailBoxSegmentCtr_ce)
            CtrlMailBoxSegmentCtr    <=  CtrlMailBoxSegmentCtr + 1'b1;

        CtrlMailBoxSegmentCtr_ce     <=  wb_cyc & wb_stb & wb_we & ~wb_ack_sm & CtrlMailBoxJumpInstCycle;










        if (control_mailbox_tbl_ptr_dcd)
            CtrlMailBoxTablePtr      <=  MemReadDataIn[17:10];









        if (control_mailbox_jump_inst_ptr_dcd)
            CtrlMailBoxJumpInstPtr   <=  MemReadDataIn[17:8];















        if (control_mailbox_jump_inst_ptr_dcd)
            CtrlMailBoxJumpInstCycle <= 1'b1;
        else if (CtrlMailBoxJumpInstCycle_ce)
            CtrlMailBoxJumpInstCycle <= 1'b0;

        CtrlMailBoxJumpInstCycle_ce  <= wb_cyc & wb_stb & wb_we &  wb_ack_sm & ~control_mailbox_jump_inst_ptr_dcd;




        if (control_wait_instr_reg_dcd || control_wait_instr_busy)
		begin
            control_wait_instr_cntr      <= control_wait_instr_cntr_nxt;
            control_wait_instr_cntr_tc   <= control_wait_instr_cntr_tc_nxt;
		end

        control_wait_instr_busy          <= control_wait_instr_busy_nxt;
    end
end   









assign control_wait_instr_busy_nxt = (~control_wait_instr_busy &  control_wait_instr_reg_dcd)
                                   | ( control_wait_instr_busy & ~control_wait_instr_cntr_tc);
	



always @(MemReadDataIn           or
         control_wait_instr_busy or
         control_wait_instr_cntr
        )
begin

    case({control_wait_instr_busy, MemReadDataIn[17]})
	2'b00:
	begin
        control_wait_instr_cntr_nxt      <= {4'h0, MemReadDataIn[16:8]        };
        control_wait_instr_cntr_tc_nxt   <= (      MemReadDataIn[16:8] == 9'h0);
	end
	2'b01:
	begin
        control_wait_instr_cntr_nxt      <= {      MemReadDataIn[16:8],   4'hF};
        control_wait_instr_cntr_tc_nxt   <= 1'b0;
	end
	2'b10:
	begin
        control_wait_instr_cntr_nxt      <=  control_wait_instr_cntr -  13'h1;
        control_wait_instr_cntr_tc_nxt   <= (control_wait_instr_cntr == 13'h1); 
	end
	2'b11:
	begin
        control_wait_instr_cntr_nxt      <=  control_wait_instr_cntr -  13'h1;
        control_wait_instr_cntr_tc_nxt   <= (control_wait_instr_cntr == 13'h1); 
	end
	endcase
end





assign BusyOut            = RunSM | BusySM;
	











assign MemReadAddressOut  = CtrlMailBoxJumpInstCycle ? {CtrlMailBoxTablePtr, CtrlMailBoxSegmentCtr[5:4]} 
                                                     :  StateMachineCtrlMemAddr ;






assign MemWriteAddressOut = CtrlReceiveAddressReg;













always @(MemReadDataIn            or
		 MemWriteDataOut          or
         i2c_masterDataToMem      or
         TimeStamp_Delta_sensor_0 or
         TimeStamp_Delta_sensor_1 or
         TimeStamp_Delta_sensor_2 or
         TimeStamp_Delta_sensor_3 or
		 TimeStamp_Delta_readback or
         SensorInterrupt_0_o      or
         SensorInterrupt_1_o      or
         SensorInterrupt_2_o      or
         SensorInterrupt_3_o    
        )
begin
	case(MemReadDataIn[7:3])
    I2C_MASTER_ADR:                    MemWriteDataOut <= {1'b0, i2c_masterDataToMem};
	TIMESTAMP_DELTA_ADR:
	begin
		case(MemReadDataIn[2:0])
        TIMESTAMP_DELTA_SENSOR_0     : MemWriteDataOut <= {1'b0, TimeStamp_Delta_sensor_0};
        TIMESTAMP_DELTA_SENSOR_1     : MemWriteDataOut <= {1'b0, TimeStamp_Delta_sensor_1};
        TIMESTAMP_DELTA_SENSOR_2     : MemWriteDataOut <= {1'b0, TimeStamp_Delta_sensor_2};
        TIMESTAMP_DELTA_SENSOR_3     : MemWriteDataOut <= {1'b0, TimeStamp_Delta_sensor_3};
        TIMESTAMP_DELTA_GENERIC_LSB  : MemWriteDataOut <= {1'b0, TimeStamp_Delta_readback[7:0]};
        TIMESTAMP_DELTA_GENERIC_MSB  : MemWriteDataOut <= {1'b0, TimeStamp_Delta_readback[15:8]};
        TIMESTAMP_DELTA_INT_EVENT    : MemWriteDataOut <= {4'h0, SensorInterrupt_3_o,
				                                                 SensorInterrupt_2_o,
															     SensorInterrupt_1_o,
															     SensorInterrupt_0_o};
		default:                       MemWriteDataOut <= {1'b0, i2c_masterDataToMem};
        endcase
	end
	default:                           MemWriteDataOut <= {1'b0, i2c_masterDataToMem};
	endcase
end






assign MemClockOut        = ClockIn;






assign wb_ack             = wb_ack_sm | I2C_wb_ack_i;








assign I2C_wb_adr_o       = ((MemReadDataIn[7:0] == {I2C_MASTER_ADR, I2C_MASTER_CR}) &  MemReadDataIn[13] & (~wb_we))   
                                                                   ? I2C_MASTER_TXR  :  MemReadDataIn[2:0];







StateMachine StateMachine_inst      (

        .CLK_IN                     ( ClockIn                               ),
        .RESET_IN                   ( ResetIn                               ),

		.RUNTIME_ADDRESS            ( CtrlRunTimeAddressReg                 ),
        .CONTROL_JUMP_REG_DCD       ( control_jump_reg_dcd                  ),
        .SAVE_REG_2_MEM             ( save_reg_2_mem                        ),

        .MAILBOX_JUMP_INST_CYCLE    (CtrlMailBoxJumpInstCycle               ),
        .MAILBOX_JUMP_INST_PTR      (CtrlMailBoxJumpInstPtr                 ),
        .MAILBOX_SEGMENT_CTR        (CtrlMailBoxSegmentCtr[3:0]             ),

        .WB_ACK_I                   ( wb_ack                                ),
        .WB_BUSY_I                  ( I2C_tip_i | control_wait_instr_busy   ),
		.WB_BUSY_POLL_I             ( wb_busy_poll                          ),

        .WB_WE_O                    ( wb_we                                 ),
        .WB_STB_O                   ( wb_stb                                ),
        .WB_CYC_O                   ( wb_cyc                                ),

        .SM_CNTL_REG_RUN            ( RunSM                                 ),
        .SM_READ_DATA               ( MemReadDataIn                         ),

        .SM_INSTR_PTR               ( StateMachineCtrlMemAddr               ),
        .SM_READ_SELECT             ( MemReadEnableOut                      ),

        .SM_WRITE_SELECT            ( MemWriteEnableOut                     ),
        .SM_BUSY                    ( BusySM                                )

         );




assign TP1 = I2C_tip_i;
assign TP2 = BusyOut;
assign TP3 = 0;


wire	d_SmClockSelect;

always @(posedge ClockIn or posedge ResetIn) 
begin
    if (ResetIn)
    begin
		s1_BusyOut  <= 1'b0;
		s2_BusyOut	<= 1'b0;
		SmClockSelect_o <= 1'b0;
	end
	else
	begin
		s1_BusyOut  <= BusyOut;
		s2_BusyOut	<= s1_BusyOut;
		SmClockSelect_o <= d_SmClockSelect;
	end
end

assign d_SmClockSelect = SmClockSelect_o ? ((!s1_BusyOut && s2_BusyOut) ? 1'b0 : 1'b1) : ((StartFromFFE_1ff ^ StartFromFFE_2ff) ? 1'b1: 1'b0);	
	
endmodule
module SensorManager (


	                ClockIn,
	                ResetIn,
                    StartFromFFE,
                    StartFromTLC,
	                BusyOut,
                    TimeStamp_Delta_i,
                    TimeStamp_Delta_Tog_i,
                    SensorInterrupt_0_i,
                    SensorInterrupt_1_i,
                    SensorInterrupt_2_i,
                    SensorInterrupt_3_i,
                    SensorInterrupt_0_o,
                    SensorInterrupt_1_o,
                    SensorInterrupt_2_o,
                    SensorInterrupt_3_o,
                    CtrlRunTimeAddressSM,
                    CtrlRunTimeAddressOut,
                    CtrlRunTimeAddressReg,
	                MemReadAddressOut,
	                MemReadEnableOut,
	                MemReadDataIn,
	                MemWriteAddressOut,
	                MemWriteEnableOut,
	                MemWriteDataOut,
	                MemClockOut,
	                I2C_wb_adr_o,
	                I2C_wb_dat_o,
	                I2C_wb_dat_i,
	                I2C_wb_we_o,
	                I2C_wb_stb_o,
	                I2C_wb_cyc_o,
	                I2C_wb_ack_i,
	                I2C_tip_i,
	                TP1,
	                TP2,
	                TP3,
					SmClockSelect_o
);
	
	



input               ClockIn;
input               ResetIn;
input           	StartFromFFE;
input               StartFromTLC;
output              BusyOut;
input       [15:0]  TimeStamp_Delta_i;
input               TimeStamp_Delta_Tog_i;
input               SensorInterrupt_0_i;
input               SensorInterrupt_1_i;
input               SensorInterrupt_2_i;
input               SensorInterrupt_3_i;
output              SensorInterrupt_0_o;
output              SensorInterrupt_1_o;
output              SensorInterrupt_2_o;
output              SensorInterrupt_3_o;
input               CtrlRunTimeAddressSM;
input        [9:0]  CtrlRunTimeAddressOut;
output       [9:0]  CtrlRunTimeAddressReg;
output       [9:0]  MemReadAddressOut;
output              MemReadEnableOut;
input       [17:0]  MemReadDataIn;
output       [9:0]  MemWriteAddressOut;
output              MemWriteEnableOut;
output       [8:0]  MemWriteDataOut;
output              MemClockOut;
output       [2:0]  I2C_wb_adr_o;
output       [7:0]  I2C_wb_dat_o;
input        [7:0]  I2C_wb_dat_i;
output              I2C_wb_we_o;
output              I2C_wb_stb_o;
output              I2C_wb_cyc_o;
input               I2C_wb_ack_i;
input               I2C_tip_i;
output              TP1;
output              TP2;
output              TP3;
output				SmClockSelect_o;


wire                ClockIn;
wire                ResetIn;
wire                StartFromFFE;
wire            	StartFromTLC;
wire                BusyOut;
wire        [15:0]  TimeStamp_Delta_i;
wire                TimeStamp_Delta_Tog_i;
wire                SensorInterrupt_0_i;
wire                SensorInterrupt_1_i;
wire                SensorInterrupt_2_i;
wire                SensorInterrupt_3_i;
reg                 SensorInterrupt_0_o;
reg                 SensorInterrupt_1_o;
reg                 SensorInterrupt_2_o;
reg                 SensorInterrupt_3_o;
wire                CtrlRunTimeAddressSM;
wire         [9:0]  CtrlRunTimeAddressOut;
reg          [9:0]  CtrlRunTimeAddressReg;
wire  		 [9:0]  MemReadAddressOut;
wire                MemReadEnableOut;
wire        [17:0]  MemReadDataIn;
wire         [9:0]  MemWriteAddressOut;
wire                MemWriteEnableOut;
reg          [8:0]  MemWriteDataOut;
wire         [2:0]  I2C_wb_adr_o;
wire         [7:0]  I2C_wb_dat_o;
wire         [7:0]  I2C_wb_dat_i;
wire                I2C_wb_we_o;
wire                I2C_wb_stb_o;
wire                I2C_wb_cyc_o;
wire                I2C_wb_ack_i;
wire                I2C_tip_i;
wire                MemClockOut;
wire                TP1;
wire                TP2;
wire                TP3;
reg					SmClockSelect_o;





wire		    	wb_cyc;

wire			    wb_we;
wire			    wb_stb;

wire                wb_ack;
reg                 wb_ack_sm;

wire                wb_busy_poll;

reg          [9:0]  CtrlReceiveAddressReg;

reg          [5:0]  CtrlMailBoxSegmentCtr;
reg                 CtrlMailBoxSegmentCtr_ce;

reg          [9:2]  CtrlMailBoxTablePtr;

reg          [9:0]  CtrlMailBoxJumpInstPtr;

reg                 CtrlMailBoxJumpInstCycle;
reg                 CtrlMailBoxJumpInstCycle_ce;
	
wire         [9:0]  StateMachineCtrlMemAddr;
wire         [7:0]  i2c_masterDataToMem;

wire                save_reg_2_mem;

wire                control_sensor_manager_reg_dcd;
wire                control_receive_reg_dcd;
wire                control_runtime_reg_dcd;
wire                control_jump_reg_dcd;
wire                control_mailbox_tbl_ptr_dcd;
wire                control_mailbox_jump_inst_ptr_dcd;

reg                 RunSM;
wire                BusySM;

wire                StartIn_stb;

reg                 StartFromFFE_1ff;
reg                 StartFromFFE_2ff;
reg                 StartFromFFE_3ff;
reg                 StartFromFFE_4ff;
reg                 StartFromFFE_5ff;
reg                 StartFromFFE_6ff;

reg                 StartFromTLC_1ff;
reg                 StartFromTLC_2ff;

reg                 CtrlRunTimeAddressSM_1ff;
reg                 CtrlRunTimeAddressSM_2ff;

reg					s1_BusyOut, s2_BusyOut;



wire                SensorInterrupt_event_reg_we_dcd;

wire                SensorInterrupt_event_mask_we_dcd;

reg                 SensorInterrupt_event_mask_0;
reg                 SensorInterrupt_event_mask_1;
reg                 SensorInterrupt_event_mask_2;
reg                 SensorInterrupt_event_mask_3;




reg          [7:0]  TimeStamp_Delta_sensor_0;
reg          [7:0]  TimeStamp_Delta_sensor_1;
reg          [7:0]  TimeStamp_Delta_sensor_2;
reg          [7:0]  TimeStamp_Delta_sensor_3;

reg         [15:0]  TimeStamp_Delta_capt;
reg         [15:0]  TimeStamp_Delta_readback;

reg                 TimeStamp_Delta_Tog_1ff;
reg                 TimeStamp_Delta_Tog_2ff;
reg                 TimeStamp_Delta_Tog_3ff;



reg                 SensorInterrupt_0_1ff;
reg                 SensorInterrupt_0_2ff;
reg                 SensorInterrupt_0_3ff;

reg                 SensorInterrupt_1_1ff;
reg                 SensorInterrupt_1_2ff;
reg                 SensorInterrupt_1_3ff;

reg                 SensorInterrupt_2_1ff;
reg                 SensorInterrupt_2_2ff;
reg                 SensorInterrupt_2_3ff;

reg                 SensorInterrupt_3_1ff;
reg                 SensorInterrupt_3_2ff;
reg                 SensorInterrupt_3_3ff;




wire                control_wait_instr_reg_dcd;

reg         [12:0]  control_wait_instr_cntr;
reg         [12:0]  control_wait_instr_cntr_nxt;

reg                 control_wait_instr_cntr_tc;
reg                 control_wait_instr_cntr_tc_nxt;

reg                 control_wait_instr_busy;
wire                control_wait_instr_busy_nxt;











parameter SENSOR_MANAGER_ADR      = 5'h0;
parameter I2C_MASTER_ADR          = 5'h1;
parameter TIMESTAMP_DELTA_ADR     = 5'h2;








parameter CONTROL_SENSOR_MANAGER_ADDRESS  = 3'h0;
parameter CONTROL_RECEIVE_ADDRESS         = 3'h1;
parameter CONTROL_RUNTIME_ADDRESS         = 3'h2;
parameter CONTROL_WAIT_INSTR_ADDRESS      = 3'h4;
parameter CONTROL_MAILBOX_TABLE_PTR       = 3'h5;
parameter CONTROL_MAILBOX_JUMP_INST_PTR   = 3'h6;
parameter CONTROL_JUMP_ADDRESS            = 3'h7;







parameter I2C_MASTER_PRELO		  = 3'h0;
parameter I2C_MASTER_PREHI        = 3'h1;
parameter I2C_MASTER_CTR          = 3'h2;
parameter I2C_MASTER_TXR          = 3'h3;
parameter I2C_MASTER_CR           = 3'h4;






parameter TIMESTAMP_DELTA_SENSOR_0    = 3'h0;
parameter TIMESTAMP_DELTA_SENSOR_1    = 3'h1;
parameter TIMESTAMP_DELTA_SENSOR_2    = 3'h2;
parameter TIMESTAMP_DELTA_SENSOR_3    = 3'h3;
parameter TIMESTAMP_DELTA_GENERIC_LSB = 3'h4;
parameter TIMESTAMP_DELTA_GENERIC_MSB = 3'h5;
parameter TIMESTAMP_DELTA_INT_EVENT   = 3'h6;





parameter MAILBOX_SM_ADDRESS      = 8'hFF;






assign  I2C_wb_dat_o                     = MemReadDataIn[15:8];
assign  i2c_masterDataToMem              = I2C_wb_dat_i;
assign  I2C_wb_we_o                      = wb_we;
assign  I2C_wb_stb_o                     = wb_stb;



assign I2C_wb_cyc_o                      = (MemReadDataIn[7:3] == I2C_MASTER_ADR) & wb_cyc & ~CtrlMailBoxJumpInstCycle;




assign control_sensor_manager_reg_dcd  = (MemReadDataIn[7:0] == {SENSOR_MANAGER_ADR, CONTROL_SENSOR_MANAGER_ADDRESS}) & wb_cyc & wb_stb & wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;
assign control_runtime_reg_dcd         = (MemReadDataIn[7:0] == {SENSOR_MANAGER_ADR, CONTROL_RUNTIME_ADDRESS})        & wb_cyc & wb_stb & wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;

assign control_wait_instr_reg_dcd      = (MemReadDataIn[7:0] == {SENSOR_MANAGER_ADR, CONTROL_WAIT_INSTR_ADDRESS})     & wb_cyc & wb_stb & wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;

assign control_receive_reg_dcd         = ((MemReadDataIn[7:0] == { SENSOR_MANAGER_ADR, CONTROL_RECEIVE_ADDRESS})
		                               |  (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_0})
                                       |  (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_1})
                                       |  (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_2})
                                       |  (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_3})
                                       |  (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_GENERIC_MSB})
                                       |  (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_GENERIC_LSB})) & wb_cyc & wb_stb & wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;







assign SensorInterrupt_event_reg_we_dcd  = (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_INT_EVENT})   & wb_cyc & wb_stb & ~wb_we &  wb_ack_sm & ~CtrlMailBoxJumpInstCycle;
assign SensorInterrupt_event_mask_we_dcd = (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_INT_EVENT})   & wb_cyc & wb_stb &  wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;
assign TimeStamp_Delta_lsb_reg_we_dcd    = (MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_GENERIC_LSB}) & wb_cyc & wb_stb &  wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;










assign control_jump_reg_dcd              = (MemReadDataIn[7:0] == {SENSOR_MANAGER_ADR, CONTROL_JUMP_ADDRESS})                                                 & ~CtrlMailBoxJumpInstCycle;









assign control_mailbox_tbl_ptr_dcd       = (MemReadDataIn[7:0] == {SENSOR_MANAGER_ADR, CONTROL_MAILBOX_TABLE_PTR})     & wb_cyc & wb_stb & wb_we & ~wb_ack_sm & ~CtrlMailBoxJumpInstCycle;
assign control_mailbox_jump_inst_ptr_dcd = (MemReadDataIn[7:0] == {SENSOR_MANAGER_ADR, CONTROL_MAILBOX_JUMP_INST_PTR}) & wb_cyc & wb_stb & wb_we &  wb_ack_sm & ~CtrlMailBoxJumpInstCycle;







assign save_reg_2_mem                  = ((MemReadDataIn[7:0] == {     I2C_MASTER_ADR, I2C_MASTER_CR}) &  MemReadDataIn[13] & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_0})           & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_1})           & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_2})           & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_SENSOR_3})           & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_GENERIC_LSB})        & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_GENERIC_MSB})        & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {TIMESTAMP_DELTA_ADR, TIMESTAMP_DELTA_INT_EVENT})          & ~CtrlMailBoxJumpInstCycle);








assign wb_busy_poll                    = ((MemReadDataIn[7:0] == {I2C_MASTER_ADR,     I2C_MASTER_CR             }) & (MemReadDataIn[12] | MemReadDataIn[13]) & ~CtrlMailBoxJumpInstCycle)
                                       | ((MemReadDataIn[7:0] == {SENSOR_MANAGER_ADR, CONTROL_WAIT_INSTR_ADDRESS})                                           & ~CtrlMailBoxJumpInstCycle);









assign StartIn_stb = (StartFromFFE_5ff ^ StartFromFFE_6ff)
                   | (StartFromTLC_1ff ^ StartFromTLC_2ff);




always @(posedge ClockIn or posedge ResetIn) 
begin
    if (ResetIn)
    begin
		wb_ack_sm             <= 1'b0;

        StartFromFFE_1ff      <= 1'b0;
        StartFromFFE_2ff      <= 1'b0;
		StartFromFFE_3ff      <= 1'b0;
        StartFromFFE_4ff      <= 1'b0;
		StartFromFFE_5ff      <= 1'b0;
        StartFromFFE_6ff      <= 1'b0;

        StartFromTLC_1ff      <= 1'b0;
        StartFromTLC_2ff      <= 1'b0;

		RunSM                 <= 1'b0;

		CtrlReceiveAddressReg <= 10'h0;
		CtrlRunTimeAddressReg <= 10'h0;

        CtrlRunTimeAddressSM_1ff <=  1'b0;
        CtrlRunTimeAddressSM_2ff <=  1'b0;

        TimeStamp_Delta_sensor_0 <=  8'h0;
        TimeStamp_Delta_sensor_1 <=  8'h0;
        TimeStamp_Delta_sensor_2 <=  8'h0;
        TimeStamp_Delta_sensor_3 <=  8'h0;

        SensorInterrupt_0_1ff    <=  4'h0;
        SensorInterrupt_0_2ff    <=  4'h0;
        SensorInterrupt_0_3ff    <=  4'h0;

        SensorInterrupt_1_1ff    <=  4'h0;
        SensorInterrupt_1_2ff    <=  4'h0;
        SensorInterrupt_1_3ff    <=  4'h0;

        SensorInterrupt_2_1ff    <=  4'h0;
        SensorInterrupt_2_2ff    <=  4'h0;
        SensorInterrupt_2_3ff    <=  4'h0;

        SensorInterrupt_3_1ff    <=  4'h0;
        SensorInterrupt_3_2ff    <=  4'h0;
        SensorInterrupt_3_3ff    <=  4'h0;

        SensorInterrupt_0_o      <=  1'b0;
        SensorInterrupt_1_o      <=  1'b0;
        SensorInterrupt_2_o      <=  1'b0;
        SensorInterrupt_3_o      <=  1'b0;

        SensorInterrupt_event_mask_0 <= 1'b0;
        SensorInterrupt_event_mask_1 <= 1'b0;
        SensorInterrupt_event_mask_2 <= 1'b0;
        SensorInterrupt_event_mask_3 <= 1'b0;

        TimeStamp_Delta_capt     <= 16'h0;
        TimeStamp_Delta_readback <= 16'h0;

        TimeStamp_Delta_Tog_1ff  <=  1'b0;
        TimeStamp_Delta_Tog_2ff  <=  1'b0;
        TimeStamp_Delta_Tog_3ff  <=  1'b0;

        CtrlMailBoxSegmentCtr       <=  6'h0;
        CtrlMailBoxSegmentCtr_ce    <=  1'b0;

        CtrlMailBoxTablePtr         <= MAILBOX_SM_ADDRESS;
        CtrlMailBoxJumpInstPtr      <= 10'h0;

        CtrlMailBoxJumpInstCycle    <=  1'b0;
        CtrlMailBoxJumpInstCycle_ce <=  1'b0;

        control_wait_instr_cntr     <= 13'h0;
        control_wait_instr_cntr_tc  <=  1'b0;
        control_wait_instr_busy     <=  1'b0;
    end
    else 
    begin  
		wb_ack_sm             <= ((MemReadDataIn[7:3] == SENSOR_MANAGER_ADR )
		                       |  (MemReadDataIn[7:3] == TIMESTAMP_DELTA_ADR)
		                       |  (CtrlMailBoxJumpInstCycle))                 & wb_cyc & wb_stb & ~wb_ack_sm;




        StartFromFFE_1ff      <= StartFromFFE;
        StartFromFFE_2ff      <= StartFromFFE_1ff;
		StartFromFFE_3ff      <= StartFromFFE_2ff;
        StartFromFFE_4ff      <= StartFromFFE_3ff;
		StartFromFFE_5ff      <= StartFromFFE_4ff;
        StartFromFFE_6ff      <= StartFromFFE_5ff;

        StartFromTLC_1ff      <= StartFromTLC;
        StartFromTLC_2ff      <= StartFromTLC_1ff;

        CtrlRunTimeAddressSM_1ff <= CtrlRunTimeAddressSM;
        CtrlRunTimeAddressSM_2ff <= CtrlRunTimeAddressSM_1ff;







		if (StartIn_stb)
            RunSM             <= 1'b1;
        else if (control_sensor_manager_reg_dcd)
            RunSM             <= MemReadDataIn[8];



        if (control_receive_reg_dcd)
			CtrlReceiveAddressReg <= MemReadDataIn[17:8];



        if (control_runtime_reg_dcd)
		    CtrlRunTimeAddressReg <= MemReadDataIn[17:8];
	    else if ( CtrlRunTimeAddressSM_1ff ^ CtrlRunTimeAddressSM_2ff)
		    CtrlRunTimeAddressReg <= CtrlRunTimeAddressOut;




        SensorInterrupt_0_1ff    <=  SensorInterrupt_0_i;
        SensorInterrupt_0_2ff    <=  SensorInterrupt_0_1ff;
        SensorInterrupt_0_3ff    <=  SensorInterrupt_0_2ff;

        SensorInterrupt_1_1ff    <=  SensorInterrupt_1_i;
        SensorInterrupt_1_2ff    <=  SensorInterrupt_1_1ff;
        SensorInterrupt_1_3ff    <=  SensorInterrupt_1_2ff;

        SensorInterrupt_2_1ff    <=  SensorInterrupt_2_i;
        SensorInterrupt_2_2ff    <=  SensorInterrupt_2_1ff;
        SensorInterrupt_2_3ff    <=  SensorInterrupt_2_2ff;

        SensorInterrupt_3_1ff    <=  SensorInterrupt_3_i;
        SensorInterrupt_3_2ff    <=  SensorInterrupt_3_1ff;
        SensorInterrupt_3_3ff    <=  SensorInterrupt_3_2ff;

        TimeStamp_Delta_Tog_1ff  <=  TimeStamp_Delta_Tog_i;
        TimeStamp_Delta_Tog_2ff  <=  TimeStamp_Delta_Tog_1ff;
        TimeStamp_Delta_Tog_3ff  <=  TimeStamp_Delta_Tog_2ff;















		if (TimeStamp_Delta_Tog_2ff ^ TimeStamp_Delta_Tog_3ff)
            TimeStamp_Delta_capt     <= TimeStamp_Delta_i;










        if (TimeStamp_Delta_lsb_reg_we_dcd)
            TimeStamp_Delta_readback <= TimeStamp_Delta_capt;






        if (SensorInterrupt_0_2ff && (!SensorInterrupt_0_3ff))
            TimeStamp_Delta_sensor_0 <=  TimeStamp_Delta_capt;


        if (SensorInterrupt_1_2ff && (!SensorInterrupt_1_3ff))
            TimeStamp_Delta_sensor_1 <=  TimeStamp_Delta_capt;


        if (SensorInterrupt_2_2ff && (!SensorInterrupt_2_3ff))
            TimeStamp_Delta_sensor_2 <=  TimeStamp_Delta_capt;


        if (SensorInterrupt_3_2ff && (!SensorInterrupt_3_3ff))
            TimeStamp_Delta_sensor_3 <=  TimeStamp_Delta_capt;















        if (SensorInterrupt_event_mask_we_dcd)
		begin
            SensorInterrupt_event_mask_0 <= MemReadDataIn[8];
            SensorInterrupt_event_mask_1 <= MemReadDataIn[9];
            SensorInterrupt_event_mask_2 <= MemReadDataIn[10];
            SensorInterrupt_event_mask_3 <= MemReadDataIn[11];
		end














        if (SensorInterrupt_event_reg_we_dcd && SensorInterrupt_event_mask_0)
            SensorInterrupt_0_o       <=  1'h0;
        else if (SensorInterrupt_0_2ff && (!SensorInterrupt_0_3ff))
            SensorInterrupt_0_o       <=  1'h1;


        if (SensorInterrupt_event_reg_we_dcd && SensorInterrupt_event_mask_1)
            SensorInterrupt_1_o       <=  1'h0;
        else if (SensorInterrupt_1_2ff && (!SensorInterrupt_1_3ff))
            SensorInterrupt_1_o       <=  1'h1;


        if (SensorInterrupt_event_reg_we_dcd && SensorInterrupt_event_mask_2)
            SensorInterrupt_2_o       <=  1'h0;
        else if (SensorInterrupt_2_2ff && (!SensorInterrupt_2_3ff))
            SensorInterrupt_2_o       <=  1'h1;


        if (SensorInterrupt_event_reg_we_dcd && SensorInterrupt_event_mask_3)
            SensorInterrupt_3_o       <=  1'h0;
        else if (SensorInterrupt_3_2ff && (!SensorInterrupt_3_3ff))
            SensorInterrupt_3_o       <=  1'h1;









        if (!BusySM)
            CtrlMailBoxSegmentCtr    <=  6'h0;
        else if (CtrlMailBoxSegmentCtr_ce)
            CtrlMailBoxSegmentCtr    <=  CtrlMailBoxSegmentCtr + 1'b1;

        CtrlMailBoxSegmentCtr_ce     <=  wb_cyc & wb_stb & wb_we & ~wb_ack_sm & CtrlMailBoxJumpInstCycle;










        if (control_mailbox_tbl_ptr_dcd)
            CtrlMailBoxTablePtr      <=  MemReadDataIn[17:10];









        if (control_mailbox_jump_inst_ptr_dcd)
            CtrlMailBoxJumpInstPtr   <=  MemReadDataIn[17:8];















        if (control_mailbox_jump_inst_ptr_dcd)
            CtrlMailBoxJumpInstCycle <= 1'b1;
        else if (CtrlMailBoxJumpInstCycle_ce)
            CtrlMailBoxJumpInstCycle <= 1'b0;

        CtrlMailBoxJumpInstCycle_ce  <= wb_cyc & wb_stb & wb_we &  wb_ack_sm & ~control_mailbox_jump_inst_ptr_dcd;




        if (control_wait_instr_reg_dcd || control_wait_instr_busy)
		begin
            control_wait_instr_cntr      <= control_wait_instr_cntr_nxt;
            control_wait_instr_cntr_tc   <= control_wait_instr_cntr_tc_nxt;
		end

        control_wait_instr_busy          <= control_wait_instr_busy_nxt;
    end
end   









assign control_wait_instr_busy_nxt = (~control_wait_instr_busy &  control_wait_instr_reg_dcd)
                                   | ( control_wait_instr_busy & ~control_wait_instr_cntr_tc);
	



always @(MemReadDataIn           or
         control_wait_instr_busy or
         control_wait_instr_cntr
        )
begin

    case({control_wait_instr_busy, MemReadDataIn[17]})
	2'b00:
	begin
        control_wait_instr_cntr_nxt      <= {4'h0, MemReadDataIn[16:8]        };
        control_wait_instr_cntr_tc_nxt   <= (      MemReadDataIn[16:8] == 9'h0);
	end
	2'b01:
	begin
        control_wait_instr_cntr_nxt      <= {      MemReadDataIn[16:8],   4'hF};
        control_wait_instr_cntr_tc_nxt   <= 1'b0;
	end
	2'b10:
	begin
        control_wait_instr_cntr_nxt      <=  control_wait_instr_cntr -  13'h1;
        control_wait_instr_cntr_tc_nxt   <= (control_wait_instr_cntr == 13'h1); 
	end
	2'b11:
	begin
        control_wait_instr_cntr_nxt      <=  control_wait_instr_cntr -  13'h1;
        control_wait_instr_cntr_tc_nxt   <= (control_wait_instr_cntr == 13'h1); 
	end
	endcase
end





assign BusyOut            = RunSM | BusySM;
	











assign MemReadAddressOut  = CtrlMailBoxJumpInstCycle ? {CtrlMailBoxTablePtr, CtrlMailBoxSegmentCtr[5:4]} 
                                                     :  StateMachineCtrlMemAddr ;






assign MemWriteAddressOut = CtrlReceiveAddressReg;













always @(MemReadDataIn            or
		 MemWriteDataOut          or
         i2c_masterDataToMem      or
         TimeStamp_Delta_sensor_0 or
         TimeStamp_Delta_sensor_1 or
         TimeStamp_Delta_sensor_2 or
         TimeStamp_Delta_sensor_3 or
		 TimeStamp_Delta_readback or
         SensorInterrupt_0_o      or
         SensorInterrupt_1_o      or
         SensorInterrupt_2_o      or
         SensorInterrupt_3_o    
        )
begin
	case(MemReadDataIn[7:3])
    I2C_MASTER_ADR:                    MemWriteDataOut <= {1'b0, i2c_masterDataToMem};
	TIMESTAMP_DELTA_ADR:
	begin
		case(MemReadDataIn[2:0])
        TIMESTAMP_DELTA_SENSOR_0     : MemWriteDataOut <= {1'b0, TimeStamp_Delta_sensor_0};
        TIMESTAMP_DELTA_SENSOR_1     : MemWriteDataOut <= {1'b0, TimeStamp_Delta_sensor_1};
        TIMESTAMP_DELTA_SENSOR_2     : MemWriteDataOut <= {1'b0, TimeStamp_Delta_sensor_2};
        TIMESTAMP_DELTA_SENSOR_3     : MemWriteDataOut <= {1'b0, TimeStamp_Delta_sensor_3};
        TIMESTAMP_DELTA_GENERIC_LSB  : MemWriteDataOut <= {1'b0, TimeStamp_Delta_readback[7:0]};
        TIMESTAMP_DELTA_GENERIC_MSB  : MemWriteDataOut <= {1'b0, TimeStamp_Delta_readback[15:8]};
        TIMESTAMP_DELTA_INT_EVENT    : MemWriteDataOut <= {4'h0, SensorInterrupt_3_o,
				                                                 SensorInterrupt_2_o,
															     SensorInterrupt_1_o,
															     SensorInterrupt_0_o};
		default:                       MemWriteDataOut <= {1'b0, i2c_masterDataToMem};
        endcase
	end
	default:                           MemWriteDataOut <= {1'b0, i2c_masterDataToMem};
	endcase
end






assign MemClockOut        = ClockIn;






assign wb_ack             = wb_ack_sm | I2C_wb_ack_i;








assign I2C_wb_adr_o       = ((MemReadDataIn[7:0] == {I2C_MASTER_ADR, I2C_MASTER_CR}) &  MemReadDataIn[13] & (~wb_we))   
                                                                   ? I2C_MASTER_TXR  :  MemReadDataIn[2:0];







StateMachine StateMachine_inst      (

        .CLK_IN                     ( ClockIn                               ),
        .RESET_IN                   ( ResetIn                               ),

		.RUNTIME_ADDRESS            ( CtrlRunTimeAddressReg                 ),
        .CONTROL_JUMP_REG_DCD       ( control_jump_reg_dcd                  ),
        .SAVE_REG_2_MEM             ( save_reg_2_mem                        ),

        .MAILBOX_JUMP_INST_CYCLE    (CtrlMailBoxJumpInstCycle               ),
        .MAILBOX_JUMP_INST_PTR      (CtrlMailBoxJumpInstPtr                 ),
        .MAILBOX_SEGMENT_CTR        (CtrlMailBoxSegmentCtr[3:0]             ),

        .WB_ACK_I                   ( wb_ack                                ),
        .WB_BUSY_I                  ( I2C_tip_i | control_wait_instr_busy   ),
		.WB_BUSY_POLL_I             ( wb_busy_poll                          ),

        .WB_WE_O                    ( wb_we                                 ),
        .WB_STB_O                   ( wb_stb                                ),
        .WB_CYC_O                   ( wb_cyc                                ),

        .SM_CNTL_REG_RUN            ( RunSM                                 ),
        .SM_READ_DATA               ( MemReadDataIn                         ),

        .SM_INSTR_PTR               ( StateMachineCtrlMemAddr               ),
        .SM_READ_SELECT             ( MemReadEnableOut                      ),

        .SM_WRITE_SELECT            ( MemWriteEnableOut                     ),
        .SM_BUSY                    ( BusySM                                )

         );




assign TP1 = I2C_tip_i;
assign TP2 = BusyOut;
assign TP3 = 0;


wire	d_SmClockSelect;

always @(posedge ClockIn or posedge ResetIn) 
begin
    if (ResetIn)
    begin
		s1_BusyOut  <= 1'b0;
		s2_BusyOut	<= 1'b0;
		SmClockSelect_o <= 1'b0;
	end
	else
	begin
		s1_BusyOut  <= BusyOut;
		s2_BusyOut	<= s1_BusyOut;
		SmClockSelect_o <= d_SmClockSelect;
	end
end

assign d_SmClockSelect = SmClockSelect_o ? ((!s1_BusyOut && s2_BusyOut) ? 1'b0 : 1'b1) : ((StartFromFFE_1ff ^ StartFromFFE_2ff) ? 1'b1: 1'b0);	
	
endmodule