data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v
1: b'%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:59: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 0))? ootx_payload_o[0][15:0] :  \n                 ^\n                ... Use "/* verilator lint_off WIDTH */" and lint_on around source to disable this message.\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:61: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 2))? ootx_payload_o[0][63:48] :  \n                 ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:62: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 3))? ootx_payload_o[0][79:64] :  \n                 ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:63: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 4))? ootx_payload_o[0][95:80] :  \n                 ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:64: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 5))? ootx_payload_o[0][111:96] :  \n                 ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:65: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 8 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 6))? ootx_payload_o[0][119:112] :  \n                 ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:66: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 8 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 7))? ootx_payload_o[0][127:120] :  \n                 ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:67: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 8))? ootx_payload_o[0][143:128] :  \n                 ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:68: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 9))? ootx_payload_o[0][159:144] :  \n                 ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:69: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 24 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 10))? ootx_payload_o[0][183:160] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:70: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 11))? ootx_payload_o[0][199:184] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:71: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 12))? ootx_payload_o[0][215:200] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:72: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 13))? ootx_payload_o[0][231:216] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:73: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 14))? ootx_payload_o[0][247:232] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:74: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 8 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 15))? ootx_payload_o[0][255:248] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:75: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 8 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 16))? ootx_payload_o[0][263:256] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:77: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 18))? ootx_payload_o[1][15:0] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:79: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 20))? ootx_payload_o[1][63:48] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:80: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 21))? ootx_payload_o[1][79:64] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:81: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 22))? ootx_payload_o[1][95:80] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:82: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 23))? ootx_payload_o[1][111:96] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:83: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 8 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 24))? ootx_payload_o[1][119:112] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:84: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 8 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 25))? ootx_payload_o[1][127:120] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:85: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 26))? ootx_payload_o[1][143:128] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:86: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 27))? ootx_payload_o[1][159:144] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:87: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 24 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 28))? ootx_payload_o[1][183:160] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:88: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 29))? ootx_payload_o[1][199:184] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:89: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 30))? ootx_payload_o[1][215:200] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:90: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 31))? ootx_payload_o[1][231:216] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:91: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 16 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 32))? ootx_payload_o[1][247:232] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:92: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 8 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 33))? ootx_payload_o[1][255:248] :  \n                  ^\n%Warning-WIDTH: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:93: Operator COND expects 32 bits on the Conditional True, but Conditional True\'s SEL generates 8 bits.\n                                                                                                           : ... In instance DarkRoomOOTXdecoder\n ((address == 34))? ootx_payload_o[1][263:256] :  \n                  ^\n%Error: data/full_repos/permissive/132007518/darkroom/synthesis/submodules/DarkRoomOOTXdecoder.v:121: Cannot find file containing module: \'lighthouse_ootx_decoder\'\nlighthouse_ootx_decoder ootx_decoder(\n^~~~~~~~~~~~~~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/132007518/darkroom/synthesis/submodules,data/full_repos/permissive/132007518/lighthouse_ootx_decoder\n             data/full_repos/permissive/132007518/darkroom/synthesis/submodules,data/full_repos/permissive/132007518/lighthouse_ootx_decoder.v\n             data/full_repos/permissive/132007518/darkroom/synthesis/submodules,data/full_repos/permissive/132007518/lighthouse_ootx_decoder.sv\n             lighthouse_ootx_decoder\n             lighthouse_ootx_decoder.v\n             lighthouse_ootx_decoder.sv\n             obj_dir/lighthouse_ootx_decoder\n             obj_dir/lighthouse_ootx_decoder.v\n             obj_dir/lighthouse_ootx_decoder.sv\n%Error: Exiting due to 1 error(s), 32 warning(s)\n'
module DarkRoomOOTXdecoder (
	input clock,
	input reset,

	input [5:0] address,
	input read,
	output signed [31:0] readdata,
	input write,
	input signed [31:0] writedata,
	output waitrequest,

	output uart_tx,
	input [NUMBER_OF_SENSORS-1:0] sensor_signals,
	output [7:0] led
);

parameter NUMBER_OF_SENSORS = 8 ;
parameter ENABLE_UART_TRANSMITTER = 0;

assign readdata = 
	((address == 0))? ootx_payload_o[0][15:0] :
	((address == 1))? ootx_payload_o[0][47:16] :
	((address == 2))? ootx_payload_o[0][63:48] :
	((address == 3))? ootx_payload_o[0][79:64] :
	((address == 4))? ootx_payload_o[0][95:80] :
	((address == 5))? ootx_payload_o[0][111:96] :
	((address == 6))? ootx_payload_o[0][119:112] :
	((address == 7))? ootx_payload_o[0][127:120] :
	((address == 8))? ootx_payload_o[0][143:128] :
	((address == 9))? ootx_payload_o[0][159:144] :
	((address == 10))? ootx_payload_o[0][183:160] :
	((address == 11))? ootx_payload_o[0][199:184] :
	((address == 12))? ootx_payload_o[0][215:200] :
	((address == 13))? ootx_payload_o[0][231:216] :
	((address == 14))? ootx_payload_o[0][247:232] :
	((address == 15))? ootx_payload_o[0][255:248] :
	((address == 16))? ootx_payload_o[0][263:256] :
	((address == 17))? ootx_crc32_o[0][31:0] :
	((address == 18))? ootx_payload_o[1][15:0] :
	((address == 19))? ootx_payload_o[1][47:16] :
	((address == 20))? ootx_payload_o[1][63:48] :
	((address == 21))? ootx_payload_o[1][79:64] :
	((address == 22))? ootx_payload_o[1][95:80] :
	((address == 23))? ootx_payload_o[1][111:96] :
	((address == 24))? ootx_payload_o[1][119:112] :
	((address == 25))? ootx_payload_o[1][127:120] :
	((address == 26))? ootx_payload_o[1][143:128] :
	((address == 27))? ootx_payload_o[1][159:144] :
	((address == 28))? ootx_payload_o[1][183:160] :
	((address == 29))? ootx_payload_o[1][199:184] :
	((address == 30))? ootx_payload_o[1][215:200] :
	((address == 31))? ootx_payload_o[1][231:216] :
	((address == 32))? ootx_payload_o[1][247:232] :
	((address == 33))? ootx_payload_o[1][255:248] :
	((address == 34))? ootx_payload_o[1][263:256] :
	((address == 35))? ootx_crc32_o[1][31:0] :
	32'hDEAD_BEEF;

assign waitrequest = |sync;
	
always @(posedge clock, posedge reset) begin: SENSOR_SIGNAL_MUX_LOGIC
	if (reset == 1) begin
		sensor_select <= 0;
	end else begin

		if(write) begin
			if(writedata<NUMBER_OF_SENSORS && address==0)
				sensor_select <= writedata;
		end
	end 
end
	
reg [31:0] sensor_select;
wire sensor;
assign sensor = ~sensor_signals[sensor_select];
	
wire [1:0] sync;
wire uart_done;
reg [264:0] ootx_payload_o[1:0];
reg [31:0] ootx_crc32_o[1:0];
reg [15:0] ootx_payload_length_o[1:0];

lighthouse_ootx_decoder ootx_decoder(
	.clk(clock),
	.reset(reset),
	.sensor(sensor),
	.led(led),
	.sync(sync),
	.ootx_crc32_1_o(ootx_crc32_o[0]),
	.ootx_crc32_2_o(ootx_crc32_o[1]),
	.ootx_payload_length_1_o(ootx_payload_length_o[0]),
	.ootx_payload_length_2_o(ootx_payload_length_o[1]),
	.ootx_payload_1_o(ootx_payload_o[0]),
	.ootx_payload_2_o(ootx_payload_o[1])
);

generate
	if(ENABLE_UART_TRANSMITTER!=0) begin
		reg trigger_send;
		reg [0:7] data_byte;
		reg lighthouse;

		reg [8:0] byte_counter;
		always @(posedge clock) begin: UART_TRANSMISSION
			
			reg transmit;
			reg [1:0] uart_state;
			parameter IDLE  = 2'b00, TRIGGER_SEND = 2'b01, WAIT_FOR_NEXT_BYTE = 2'b10;
			
			trigger_send <= 0;
			case(uart_state) 
				IDLE: begin
							if(sync) begin
								uart_state <= TRIGGER_SEND;
								byte_counter <= 0;
								if(sync[0]==1)
									lighthouse<=0;
								if(sync[1]==1)
									lighthouse<=1;
							end
						end
				TRIGGER_SEND: begin		
							if(byte_counter< 37) begin
								case(byte_counter)

									0: data_byte <= ootx_payload_o[lighthouse][7:0];
									1: data_byte <= ootx_payload_o[lighthouse][15:8];

									2: data_byte <= ootx_payload_o[lighthouse][23:16];
									3: data_byte <= ootx_payload_o[lighthouse][31:24];
									4: data_byte <= ootx_payload_o[lighthouse][39:32];
									5: data_byte <= ootx_payload_o[lighthouse][47:40];

									6: data_byte <= ootx_payload_o[lighthouse][55:48];
									7: data_byte <= ootx_payload_o[lighthouse][63:56];

									8: data_byte <= ootx_payload_o[lighthouse][71:64];
									9: data_byte <= ootx_payload_o[lighthouse][79:72];

									10: data_byte <= ootx_payload_o[lighthouse][87:80];
									11: data_byte <= ootx_payload_o[lighthouse][95:88];

									12: data_byte <= ootx_payload_o[lighthouse][103:96];
									13: data_byte <= ootx_payload_o[lighthouse][111:104];

									14: data_byte <= ootx_payload_o[lighthouse][119:112];

									15: data_byte <= ootx_payload_o[lighthouse][127:120];

									16: data_byte <= ootx_payload_o[lighthouse][135:128];
									17: data_byte <= ootx_payload_o[lighthouse][143:136];

									18: data_byte <= ootx_payload_o[lighthouse][151:144];
									19: data_byte <= ootx_payload_o[lighthouse][159:152];

									20: data_byte <= ootx_payload_o[lighthouse][167:160];

									21: data_byte <= ootx_payload_o[lighthouse][175:168];

									22: data_byte <= ootx_payload_o[lighthouse][183:176];

									23: data_byte <= ootx_payload_o[lighthouse][191:184];
									24: data_byte <= ootx_payload_o[lighthouse][199:192];

									25: data_byte <= ootx_payload_o[lighthouse][207:200];
									26: data_byte <= ootx_payload_o[lighthouse][215:208];

									27: data_byte <= ootx_payload_o[lighthouse][223:216];
									28: data_byte <= ootx_payload_o[lighthouse][231:224];

									29: data_byte <= ootx_payload_o[lighthouse][239:232];
									30: data_byte <= ootx_payload_o[lighthouse][247:240];

									31: data_byte <= ootx_payload_o[lighthouse][255:248];

									32: data_byte <= ootx_payload_o[lighthouse][263:256];

									33: data_byte <= ootx_crc32_o[lighthouse][7:0];
									34: data_byte <= ootx_crc32_o[lighthouse][15:8];
									35: data_byte <= ootx_crc32_o[lighthouse][23:16];
									36: data_byte <= ootx_crc32_o[lighthouse][31:24];
									default: data_byte <= 0; 
								endcase
								trigger_send <= 1;
								uart_state <= WAIT_FOR_NEXT_BYTE;
							end else begin
								uart_state <= IDLE; 
							end
						end
				
				WAIT_FOR_NEXT_BYTE: begin
							if(uart_done) begin
								uart_state <= TRIGGER_SEND;
								byte_counter <= byte_counter+1;
							end
						end
				default: uart_state <= IDLE;
			endcase
		end 


		uart_tx #(434) uart(
			.i_Clock(clock),
			.i_Tx_DV(trigger_send),
			.i_Tx_Byte(data_byte),
			.o_Tx_Done(uart_done),
			.o_Tx_Serial(uart_tx)
		);

	end

endgenerate 

endmodule
module DarkRoomOOTXdecoder (
	input clock,
	input reset,

	input [5:0] address,
	input read,
	output signed [31:0] readdata,
	input write,
	input signed [31:0] writedata,
	output waitrequest,

	output uart_tx,
	input [NUMBER_OF_SENSORS-1:0] sensor_signals,
	output [7:0] led
);


parameter NUMBER_OF_SENSORS = 8 ;
parameter ENABLE_UART_TRANSMITTER = 0;

assign readdata = 
	((address == 0))? ootx_payload_o[0][15:0] :
	((address == 1))? ootx_payload_o[0][47:16] :
	((address == 2))? ootx_payload_o[0][63:48] :
	((address == 3))? ootx_payload_o[0][79:64] :
	((address == 4))? ootx_payload_o[0][95:80] :
	((address == 5))? ootx_payload_o[0][111:96] :
	((address == 6))? ootx_payload_o[0][119:112] :
	((address == 7))? ootx_payload_o[0][127:120] :
	((address == 8))? ootx_payload_o[0][143:128] :
	((address == 9))? ootx_payload_o[0][159:144] :
	((address == 10))? ootx_payload_o[0][183:160] :
	((address == 11))? ootx_payload_o[0][199:184] :
	((address == 12))? ootx_payload_o[0][215:200] :
	((address == 13))? ootx_payload_o[0][231:216] :
	((address == 14))? ootx_payload_o[0][247:232] :
	((address == 15))? ootx_payload_o[0][255:248] :
	((address == 16))? ootx_payload_o[0][263:256] :
	((address == 17))? ootx_crc32_o[0][31:0] :
	((address == 18))? ootx_payload_o[1][15:0] :
	((address == 19))? ootx_payload_o[1][47:16] :
	((address == 20))? ootx_payload_o[1][63:48] :
	((address == 21))? ootx_payload_o[1][79:64] :
	((address == 22))? ootx_payload_o[1][95:80] :
	((address == 23))? ootx_payload_o[1][111:96] :
	((address == 24))? ootx_payload_o[1][119:112] :
	((address == 25))? ootx_payload_o[1][127:120] :
	((address == 26))? ootx_payload_o[1][143:128] :
	((address == 27))? ootx_payload_o[1][159:144] :
	((address == 28))? ootx_payload_o[1][183:160] :
	((address == 29))? ootx_payload_o[1][199:184] :
	((address == 30))? ootx_payload_o[1][215:200] :
	((address == 31))? ootx_payload_o[1][231:216] :
	((address == 32))? ootx_payload_o[1][247:232] :
	((address == 33))? ootx_payload_o[1][255:248] :
	((address == 34))? ootx_payload_o[1][263:256] :
	((address == 35))? ootx_crc32_o[1][31:0] :
	32'hDEAD_BEEF;

assign waitrequest = |sync;
	
always @(posedge clock, posedge reset) begin: SENSOR_SIGNAL_MUX_LOGIC
	if (reset == 1) begin
		sensor_select <= 0;
	end else begin

		if(write) begin
			if(writedata<NUMBER_OF_SENSORS && address==0)
				sensor_select <= writedata;
		end
	end 
end
	
reg [31:0] sensor_select;
wire sensor;
assign sensor = ~sensor_signals[sensor_select];
	
wire [1:0] sync;
wire uart_done;
reg [264:0] ootx_payload_o[1:0];
reg [31:0] ootx_crc32_o[1:0];
reg [15:0] ootx_payload_length_o[1:0];

lighthouse_ootx_decoder ootx_decoder(
	.clk(clock),
	.reset(reset),
	.sensor(sensor),
	.led(led),
	.sync(sync),
	.ootx_crc32_1_o(ootx_crc32_o[0]),
	.ootx_crc32_2_o(ootx_crc32_o[1]),
	.ootx_payload_length_1_o(ootx_payload_length_o[0]),
	.ootx_payload_length_2_o(ootx_payload_length_o[1]),
	.ootx_payload_1_o(ootx_payload_o[0]),
	.ootx_payload_2_o(ootx_payload_o[1])
);

generate
	if(ENABLE_UART_TRANSMITTER!=0) begin
		reg trigger_send;
		reg [0:7] data_byte;
		reg lighthouse;

		reg [8:0] byte_counter;
		always @(posedge clock) begin: UART_TRANSMISSION
			
			reg transmit;
			reg [1:0] uart_state;
			parameter IDLE  = 2'b00, TRIGGER_SEND = 2'b01, WAIT_FOR_NEXT_BYTE = 2'b10;
			
			trigger_send <= 0;
			case(uart_state) 
				IDLE: begin
							if(sync) begin
								uart_state <= TRIGGER_SEND;
								byte_counter <= 0;
								if(sync[0]==1)
									lighthouse<=0;
								if(sync[1]==1)
									lighthouse<=1;
							end
						end
				TRIGGER_SEND: begin		
							if(byte_counter< 37) begin
								case(byte_counter)

									0: data_byte <= ootx_payload_o[lighthouse][7:0];
									1: data_byte <= ootx_payload_o[lighthouse][15:8];

									2: data_byte <= ootx_payload_o[lighthouse][23:16];
									3: data_byte <= ootx_payload_o[lighthouse][31:24];
									4: data_byte <= ootx_payload_o[lighthouse][39:32];
									5: data_byte <= ootx_payload_o[lighthouse][47:40];

									6: data_byte <= ootx_payload_o[lighthouse][55:48];
									7: data_byte <= ootx_payload_o[lighthouse][63:56];

									8: data_byte <= ootx_payload_o[lighthouse][71:64];
									9: data_byte <= ootx_payload_o[lighthouse][79:72];

									10: data_byte <= ootx_payload_o[lighthouse][87:80];
									11: data_byte <= ootx_payload_o[lighthouse][95:88];

									12: data_byte <= ootx_payload_o[lighthouse][103:96];
									13: data_byte <= ootx_payload_o[lighthouse][111:104];

									14: data_byte <= ootx_payload_o[lighthouse][119:112];

									15: data_byte <= ootx_payload_o[lighthouse][127:120];

									16: data_byte <= ootx_payload_o[lighthouse][135:128];
									17: data_byte <= ootx_payload_o[lighthouse][143:136];

									18: data_byte <= ootx_payload_o[lighthouse][151:144];
									19: data_byte <= ootx_payload_o[lighthouse][159:152];

									20: data_byte <= ootx_payload_o[lighthouse][167:160];

									21: data_byte <= ootx_payload_o[lighthouse][175:168];

									22: data_byte <= ootx_payload_o[lighthouse][183:176];

									23: data_byte <= ootx_payload_o[lighthouse][191:184];
									24: data_byte <= ootx_payload_o[lighthouse][199:192];

									25: data_byte <= ootx_payload_o[lighthouse][207:200];
									26: data_byte <= ootx_payload_o[lighthouse][215:208];

									27: data_byte <= ootx_payload_o[lighthouse][223:216];
									28: data_byte <= ootx_payload_o[lighthouse][231:224];

									29: data_byte <= ootx_payload_o[lighthouse][239:232];
									30: data_byte <= ootx_payload_o[lighthouse][247:240];

									31: data_byte <= ootx_payload_o[lighthouse][255:248];

									32: data_byte <= ootx_payload_o[lighthouse][263:256];

									33: data_byte <= ootx_crc32_o[lighthouse][7:0];
									34: data_byte <= ootx_crc32_o[lighthouse][15:8];
									35: data_byte <= ootx_crc32_o[lighthouse][23:16];
									36: data_byte <= ootx_crc32_o[lighthouse][31:24];
									default: data_byte <= 0; 
								endcase
								trigger_send <= 1;
								uart_state <= WAIT_FOR_NEXT_BYTE;
							end else begin
								uart_state <= IDLE; 
							end
						end
				
				WAIT_FOR_NEXT_BYTE: begin
							if(uart_done) begin
								uart_state <= TRIGGER_SEND;
								byte_counter <= byte_counter+1;
							end
						end
				default: uart_state <= IDLE;
			endcase
		end 


		uart_tx #(434) uart(
			.i_Clock(clock),
			.i_Tx_DV(trigger_send),
			.i_Tx_Byte(data_byte),
			.o_Tx_Done(uart_done),
			.o_Tx_Serial(uart_tx)
		);

	end

endgenerate 

endmodule