// chitchat_tx chitchat_rx pair for communication between LCLS-II
// RFS, Resonance, and Interlock chassis.

// Protocol first described to the collaboration in a July 7, 2016 email
// Subject: first draft inter-chassis comm

// A (possibly updated) copy of that file should be nearby, called
// interchassis.txt.  Refer to that document for important details
// like the contents of the cavity*_status words.

module chitchat_tx #(
	parameter gateware_type = 0,
	parameter protocol_ver = 1,
	parameter rev_id = 0  // 32-bit, git commit or similar
) (
	// Simple single-clock-domain design
	input clk,
	// Application-specific inputs
	input enable,  // Tx cycles won't start unless enable is high
	input [2:0] location,  // within cryomodule
	input [31:0] cavity0_status,
	input [31:0] cavity1_status,
	input [15:0] loopback_frame_counter,  // intended from chitchat_rx
	// The above four inputs are captured atomically on a cycle
	// (one in 11) marked by assertion of this capture bit.
	output capture,
	output [15:0] local_frame_counter,  // see usage notes below
	// Output to GTX (possibly through clock-domain crosser)
	output [15:0] gtx_d,
	output gtx_s // flag that a comma code is in lower byte of Tx data
);

// Timing generator
reg start=0, sync=0, sync2=0, last=0, last1=0, crc_time=0;
reg [3:0] word_count=0;
wire increment = enable | (word_count!=0);
always @(posedge clk) begin
	word_count <= word_count==10 ? 0 : word_count + increment;
	start <= word_count==1;
	sync <= start;
	sync2 <= sync;
	last <= word_count==10;
	last1 <= last;
	crc_time <= last1;
end

// Frame creation
reg [15:0] frame_counter=0;
reg [11*16-1:0] frame=0;  // parallel in, 16-bit words out
// Fixed-bit-width form of input parameters; yes I'm paranoid
wire [3:0] protocol_ver_fix = protocol_ver;
wire [2:0] gateware_type_fix = gateware_type;
wire [31:0] rev_id_fix = rev_id;
wire [7:0] comma_pad = 8'b10111100;  // K28.5
wire [9:0] reserved = 0;
wire [15:0] crc_pad = 0;
always @(posedge clk) begin
	frame_counter <= frame_counter + start;
	frame <= start ? {
	// one full frame is 11 x 16-bit words
		4'd6, protocol_ver_fix, comma_pad,
		gateware_type_fix, location, reserved,
		rev_id_fix,
		cavity0_status,
		cavity1_status,
		frame_counter,
		loopback_frame_counter,
		crc_pad} : {frame[10*16-1:0],16'b0};
	// Note the big-endian treatment of 32-bit inputs
end

// CRC generation
wire [15:0] crc_tx;
wire [15:0] inner_data = frame[11*16-1:10*16];
crc16 crc16_tx(.clk(clk), .din(inner_data), .zero(sync), .crc(crc_tx));
reg [15:0] outer_data = 0;
always @(posedge clk) outer_data <= crc_time ? crc_tx : inner_data;

// Assign output ports
assign capture = start;
assign local_frame_counter = frame_counter;
assign gtx_d = outer_data;
assign gtx_s = sync2;

endmodule
// ==========

// This module depends on the K28.5 comma code (inserted as frame marker
// by chitchat_tx, above) being preserved in the data stream when gtx_s is
// high.  This is because that "data" gets routed through the CRC logic.
module chitchat_rx(
	// Simple single-clock-domain design
	input clk,
	// Input from GTX (possibly via clock-domain crosser)
	input [15:0] gtx_d,
	input gtx_s,  // comma detected in lower byte, see above
	// Application-specific outputs
	output reg [3:0] protocol_ver,
	output reg [2:0] gateware_type,
	output reg [2:0] location,
	output reg [31:0] rev_id,
	output reg [31:0] cavity0_status,
	output reg [31:0] cavity1_status,
	output reg [15:0] frame_counter,
	output reg [15:0] loopback_frame_counter,
	// The outputs above are only necessarily valid when valid_sync is high
	output valid_sync,
	output [3:0] faults,
	output error
);
// To set up the latency-measuring feature, route this module's
// frame_counter output to chitchat_tx's loopback_frame_counter.
// The difference between chitchat_tx's local_frame_counter and this
// module's loopback_frame_counter represents round-trip system latency.

// Timing generator
reg [3:0] word_count=0;
reg sync=0, sync2=0, valid_r=0;
wire timeout = &word_count;
wire increment = ~timeout;  // word_count stops at 15
always @(posedge clk) begin
	word_count <= gtx_s ? 0 : word_count + increment;
	sync <= word_count == 9;
	sync2 <= sync;
end

// CRC calculation
wire [15:0] crc_rx;
crc16 crc16_rx(.clk(clk), .din(gtx_d), .zero(sync), .crc(crc_rx));
wire crc_zero = crc_rx==0;

// Error checking
reg [2:0] valid_count=0;
wire valid_count_max = &valid_count;
reg wrong_frame=0;
wire wrong_prot;
wire crc_fault = sync & ~crc_zero;
assign faults = {wrong_frame, crc_fault, wrong_prot, timeout};
wire fault = |faults;
wire valid_count_inc = ~valid_count_max;
always @(posedge clk) begin
	if (sync|timeout) valid_count <= fault ? 0 : valid_count + valid_count_inc;
	valid_r <= sync2 & valid_count_max;
end
assign valid_sync = valid_r;
assign error = ~valid_count_max;

// Output value exploding
reg [15:0] gtx_dd=0;
reg [15:0] next_frame_counter=0;
initial frame_counter=0;
reg [3:0] protocol_cat=0;
assign wrong_prot = protocol_cat!=6;
reg [7:0] comma_pad=0;
reg [9:0] reserved=0;
always @(posedge clk) begin
	gtx_dd <= gtx_d;
	next_frame_counter <= frame_counter+1;
	if (gtx_s) {protocol_cat, protocol_ver, comma_pad} <= gtx_d;
	if (word_count==0) {gateware_type, location, reserved} <= gtx_d;
	if (word_count==2) rev_id <= {gtx_dd, gtx_d};
	if (word_count==4) cavity0_status <= {gtx_dd, gtx_d};
	if (word_count==6) cavity1_status <= {gtx_dd, gtx_d};
	if (word_count==7) begin
		frame_counter <= gtx_d;
		wrong_frame <= gtx_d != next_frame_counter;
	end
	if (word_count==8) loopback_frame_counter <= gtx_d;
end

endmodule
