data/full_repos/permissive/125531440/core/src/main/verilog/loopback/tx_port_writer.v
 line:181: before: ","
1: b'%Error: data/full_repos/permissive/125531440/core/src/main/verilog/loopback/tx_port_writer.v:102: Cannot find include file: functions.vh\n`include "functions.vh" \n         ^~~~~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/125531440/core/src/main/verilog/loopback,data/full_repos/permissive/125531440/functions.vh\n             data/full_repos/permissive/125531440/core/src/main/verilog/loopback,data/full_repos/permissive/125531440/functions.vh.v\n             data/full_repos/permissive/125531440/core/src/main/verilog/loopback,data/full_repos/permissive/125531440/functions.vh.sv\n             functions.vh\n             functions.vh.v\n             functions.vh.sv\n             obj_dir/functions.vh\n             obj_dir/functions.vh.v\n             obj_dir/functions.vh.sv\n%Error: Exiting due to 1 error(s)\n'
module tx_port_writer (
	input CLK,
	input RST,
	input [2:0] CONFIG_MAX_PAYLOAD_SIZE,

	output TXN,
	input TXN_ACK,
	output [31:0] TXN_LEN,
	output [31:0] TXN_OFF_LAST,
	output [31:0] TXN_DONE_LEN,
	output TXN_DONE,
	output TXN_ERR,
	input TXN_DONE_ACK,

	input NEW_TXN,
	output NEW_TXN_ACK,
	input NEW_TXN_LAST,
	input [31:0] NEW_TXN_LEN,
	input [30:0] NEW_TXN_OFF,
	input [31:0] NEW_TXN_WORDS_RECVD,
	input NEW_TXN_DONE,

	input [63:0] SG_ELEM_ADDR,
	input [31:0] SG_ELEM_LEN,
	input SG_ELEM_RDY,
	input SG_ELEM_EMPTY,
	output SG_ELEM_REN,
	output SG_RST,
	input SG_ERR,
	
	output TX_REQ,
	input TX_REQ_ACK,
	output [63:0] TX_ADDR,
	output [9:0] TX_LEN,
	output TX_LAST,
	input TX_SENT
);

`include "functions.vh"

(* syn_encoding = "user" *)
(* fsm_encoding = "user" *)
reg		[7:0]						rMainState=`S_TXPORTWR_MAIN_IDLE, _rMainState=`S_TXPORTWR_MAIN_IDLE;
reg		[31:0]						rOffLast=0, _rOffLast=0;
reg									rWordsEQ0=0, _rWordsEQ0=0;
reg									rStarted=0, _rStarted=0;
reg		[31:0]						rDoneLen=0, _rDoneLen=0;
reg									rSgErr=0, _rSgErr=0;
reg									rTxErrd=0, _rTxErrd=0;
reg									rTxnAck=0, _rTxnAck=0;

(* syn_encoding = "user" *)
(* fsm_encoding = "user" *)
reg		[7:0]						rTxState=`S_TXPORTWR_TX_IDLE, _rTxState=`S_TXPORTWR_TX_IDLE;
reg		[31:0]						rSentWords=0, _rSentWords=0;
reg		[31:0]						rWords=0, _rWords=0;
reg		[31:0]						rBufWords=0, _rBufWords=0;
reg		[31:0]						rBufWordsInit=0, _rBufWordsInit=0;
reg									rLargeBuf=0, _rLargeBuf=0;
reg		[63:0]						rAddr=64'd0, _rAddr=64'd0;
reg		[2:0]						rCarry=0, _rCarry=0;
reg									rValsPropagated=0, _rValsPropagated=0;
reg		[5:0]						rValsProp=0, _rValsProp=0;
reg									rCopyBufWords=0, _rCopyBufWords=0;
reg									rUseInit=0, _rUseInit=0;
reg		[10:0]						rPageRem=0, _rPageRem=0;
reg									rPageSpill=0, _rPageSpill=0;
reg									rPageSpillInit=0, _rPageSpillInit=0;
reg		[10:0]						rPreLen=0, _rPreLen=0;
reg		[2:0]						rMaxPayloadSize=0, _rMaxPayloadSize=0;
reg		[2:0]						rMaxPayloadShift=0, _rMaxPayloadShift=0;
reg		[9:0]						rMaxPayload=0, _rMaxPayload=0;
reg									rPayloadSpill=0, _rPayloadSpill=0;
reg									rMaxLen=1, _rMaxLen=1;
reg		[9:0]						rLen=0, _rLen=0;
reg		[31:0]						rSendingWords=0, _rSendingWords=0;
reg									rAvail=0, _rAvail=0;
reg		[1:0]						rTxnDone=0, _rTxnDone=0;
reg		[9:0]						rLastLen=0, _rLastLen=0;
reg									rLastLenEQ0=0, _rLastLenEQ0=0;
reg									rLenEQWords=0, _rLenEQWords=0;
reg									rLenEQBufWords=0, _rLenEQBufWords=0;

reg									rNotRequesting=1, _rNotRequesting=1;
reg		[63:0]						rReqAddr=64'd0, _rReqAddr=64'd0;
reg		[9:0]						rReqLen=0, _rReqLen=0;
reg									rReqLast=0, _rReqLast=0;
reg									rTxReqAck=0, _rTxReqAck=0;

reg									rDone=0, _rDone=0;
reg		[9:0]						rAckCount=0, _rAckCount=0;
reg									rTxSent=0, _rTxSent=0;
reg									rLastDoneRead=1, _rLastDoneRead=1;
reg									rTxnDoneAck=0, _rTxnDoneAck=0;

reg									rReqPartialDone=0, _rReqPartialDone=0;
reg									rPartialDone=0, _rPartialDone=0;


assign NEW_TXN_ACK = rMainState[1];

assign TXN = rMainState[2];
assign TXN_DONE = (rMainState[6] | rPartialDone);
assign TXN_LEN = rWords;
assign TXN_OFF_LAST = rOffLast;
assign TXN_DONE_LEN = rDoneLen;
assign TXN_ERR = rTxErrd;

assign SG_ELEM_REN = rTxState[2];
assign SG_RST = rMainState[3];

assign TX_REQ = !rNotRequesting;
assign TX_ADDR = rReqAddr; 
assign TX_LEN = rReqLen;
assign TX_LAST = rReqLast;



always @ (posedge CLK) begin
	rTxnAck <= #1 (RST ? 1'd0 : _rTxnAck);
	rTxnDoneAck <= #1 (RST ? 1'd0 : _rTxnDoneAck);
	rSgErr <= #1 (RST ? 1'd0 : _rSgErr);
	rTxReqAck <= #1 (RST ? 1'd0 : _rTxReqAck);
	rTxSent <= #1 (RST ? 1'd0 : _rTxSent);
end

always @ (*) begin
	_rTxnAck = TXN_ACK;
	_rTxnDoneAck = TXN_DONE_ACK;
	_rSgErr = SG_ERR;
	_rTxReqAck = TX_REQ_ACK;
	_rTxSent = TX_SENT;
end
	



always @ (posedge CLK) begin
	rMainState <= #1 (RST ? `S_TXPORTWR_MAIN_IDLE : _rMainState);
	rOffLast <= #1 _rOffLast;
	rWordsEQ0 <= #1 _rWordsEQ0;
	rStarted <= #1 _rStarted;
	rDoneLen <= #1 (RST ? 0 : _rDoneLen);
	rTxErrd <= #1 (RST ? 1'd0 : _rTxErrd);
end

always @ (*) begin
	_rMainState = rMainState;
	_rOffLast = rOffLast;
	_rWordsEQ0 = rWordsEQ0;
	_rStarted = rStarted;
	_rDoneLen = rDoneLen;
	_rTxErrd = rTxErrd;
	case (rMainState)

	`S_TXPORTWR_MAIN_IDLE: begin
		_rStarted = 0;
		_rWordsEQ0 = (NEW_TXN_LEN == 0);
		_rOffLast = {NEW_TXN_OFF, NEW_TXN_LAST};
		if (NEW_TXN)
			_rMainState = `S_TXPORTWR_MAIN_CHECK;
	end

	`S_TXPORTWR_MAIN_CHECK: begin
		if (rOffLast[0] | !rWordsEQ0)
			_rMainState = `S_TXPORTWR_MAIN_SIG_NEW;
		else
			_rMainState = `S_TXPORTWR_MAIN_RESET;
	end

	`S_TXPORTWR_MAIN_SIG_NEW: begin
		_rMainState = `S_TXPORTWR_MAIN_NEW_ACK;
	end

	`S_TXPORTWR_MAIN_NEW_ACK: begin
		if (rTxnAck)
			_rMainState = (rWordsEQ0 ? `S_TXPORTWR_MAIN_SIG_DONE : `S_TXPORTWR_MAIN_WRITE);
	end

	`S_TXPORTWR_MAIN_WRITE: begin
		_rStarted = (rStarted | rTxState[1]);
		_rTxErrd = (rTxErrd | rSgErr);
		if (rTxState[0] & rStarted)
			_rMainState = `S_TXPORTWR_MAIN_DONE;
	end

	`S_TXPORTWR_MAIN_DONE: begin
		if (rDone & rLastDoneRead) begin
			_rDoneLen = rSentWords;
			_rMainState = `S_TXPORTWR_MAIN_SIG_DONE;
		end
	end

	`S_TXPORTWR_MAIN_SIG_DONE: begin
		_rTxErrd = 0;
		_rMainState = `S_TXPORTWR_MAIN_RESET;
	end

	`S_TXPORTWR_MAIN_RESET: begin
		if (NEW_TXN_DONE)
			_rMainState = `S_TXPORTWR_MAIN_IDLE;
	end
	
	default: begin
		_rMainState = `S_TXPORTWR_MAIN_IDLE;
	end
	
	endcase
end






wire [9:0] wLastLen = (NEW_TXN_WORDS_RECVD - rSentWords);
wire [9:0] wAddrLoInv = ~rAddr[11:2];
wire [10:0] wPageRem = (wAddrLoInv + 1'd1);	
always @ (posedge CLK) begin
	rTxState <= #1 (RST | rSgErr ? `S_TXPORTWR_TX_IDLE : _rTxState);
	rSentWords <= #1 (rMainState[0] ? 0 : _rSentWords);
	rWords <= #1 _rWords;
	rBufWords <= #1 _rBufWords;
	rBufWordsInit <= #1 _rBufWordsInit;
	rAddr <= #1 _rAddr;
	rCarry <= #1 _rCarry;
	rValsPropagated <= #1 _rValsPropagated;
	rValsProp <= #1 _rValsProp;
	rLargeBuf <= #1 _rLargeBuf;
	rPageRem <= #1 _rPageRem;
	rPageSpill <= #1 _rPageSpill;
	rPageSpillInit <= #1 _rPageSpillInit;
	rCopyBufWords <= #1 _rCopyBufWords;
	rUseInit <= #1 _rUseInit;
	rPreLen <= #1 _rPreLen;
	rMaxPayloadSize <= #1 _rMaxPayloadSize;
	rMaxPayloadShift <= #1 _rMaxPayloadShift;
	rMaxPayload <= #1 _rMaxPayload;
	rPayloadSpill <= #1 _rPayloadSpill;
	rMaxLen <= #1 (RST ? 1'd1 : _rMaxLen);
	rLen <= #1 _rLen;
	rSendingWords <= #1 _rSendingWords;
	rAvail <= #1 _rAvail;
	rTxnDone <= #1 _rTxnDone;
	rLastLen <= #1 _rLastLen;
	rLastLenEQ0 <= #1 _rLastLenEQ0;
	rLenEQWords <= #1 _rLenEQWords;
	rLenEQBufWords <= #1 _rLenEQBufWords;
end

always @ (*) begin
	_rTxState = rTxState;
	_rCopyBufWords = rCopyBufWords;
	_rUseInit = rUseInit;
	
	_rValsProp = ((rValsProp<<1) | rTxState[3]);
	_rValsPropagated = (rValsProp == 6'd0);
	_rLargeBuf = (SG_ELEM_LEN > rWords);
	{_rCarry[0], _rAddr[15:0]} = (rTxState[1] ? SG_ELEM_ADDR[15:0] : (rAddr[15:0] + ({12{rTxState[6]}} & {rLen, 2'd0})));
	{_rCarry[1], _rAddr[31:16]} = (rTxState[1] ? SG_ELEM_ADDR[31:16] : (rAddr[31:16] + rCarry[0]));
	{_rCarry[2], _rAddr[47:32]} = (rTxState[1] ? SG_ELEM_ADDR[47:32] : (rAddr[47:32] + rCarry[1]));
				 _rAddr[63:48] = (rTxState[1] ? SG_ELEM_ADDR[63:48] : (rAddr[63:48] + rCarry[2]));
	_rSentWords = (rTxState[7] ? NEW_TXN_WORDS_RECVD : rSentWords) + ({10{rTxState[6]}} & rLen);
	_rWords = (NEW_TXN_ACK ? NEW_TXN_LEN : (rWords - ({10{rTxState[6]}} & rLen)));
	_rBufWordsInit = (rLargeBuf ? rWords : SG_ELEM_LEN); 
	_rBufWords = (rCopyBufWords ? rBufWordsInit : rBufWords) - ({10{rTxState[6]}} & rLen);
	_rPageRem = wPageRem;	
	_rPageSpillInit = (rBufWordsInit > wPageRem);	
	_rPageSpill = (rBufWords > wPageRem);	
	_rPreLen = ((rPageSpillInit & rUseInit) | (rPageSpill & !rUseInit) ? rPageRem : rBufWords[10:0]);
	_rMaxPayloadSize = CONFIG_MAX_PAYLOAD_SIZE;
	_rMaxPayloadShift = (rMaxPayloadSize > 3'd4 ? 3'd4 : rMaxPayloadSize);
	_rMaxPayload = (6'd32<<rMaxPayloadShift);
	_rPayloadSpill = (rPreLen > rMaxPayload);
	_rMaxLen = ((rMaxLen & !rValsProp[1]) | rTxState[6]);
	_rLen = (rPayloadSpill | rMaxLen ? rMaxPayload : rPreLen[9:0]);
	_rSendingWords = rSentWords + rLen;
	_rAvail = (NEW_TXN_WORDS_RECVD >= rSendingWords);
	_rTxnDone = ((rTxnDone<<1) | NEW_TXN_DONE);
	_rLastLen = wLastLen;
	_rLastLenEQ0 = (rLastLen == 10'd0);
	_rLenEQWords = (rLen == rWords);
	_rLenEQBufWords = (rLen == rBufWords);
	
	case (rTxState)

	`S_TXPORTWR_TX_IDLE: begin
		if (rMainState[4] & !rStarted)
			_rTxState = `S_TXPORTWR_TX_BUF;
	end

	`S_TXPORTWR_TX_BUF: begin
		if (SG_ELEM_RDY)
			_rTxState = `S_TXPORTWR_TX_ADJ_0;
	end

	`S_TXPORTWR_TX_ADJ_0: begin
		_rCopyBufWords = 1;
		_rTxState = `S_TXPORTWR_TX_ADJ_1;
	end

	`S_TXPORTWR_TX_ADJ_1: begin
		_rCopyBufWords = 0;
		_rUseInit = rCopyBufWords;
		_rTxState = `S_TXPORTWR_TX_ADJ_2;
	end

	`S_TXPORTWR_TX_ADJ_2: begin



		_rUseInit = 0;
		if (rValsProp[2])
			_rTxState = `S_TXPORTWR_TX_CHECK_DATA;
	end

	`S_TXPORTWR_TX_CHECK_DATA: begin
		if (rNotRequesting) begin
			if (rAvail)
				_rTxState = `S_TXPORTWR_TX_WRITE;
			else if (rValsPropagated & rTxnDone[1])
				_rTxState = (rLastLenEQ0 ? `S_TXPORTWR_TX_IDLE : `S_TXPORTWR_TX_WRITE_REM);
		end
	end

	`S_TXPORTWR_TX_WRITE: begin
		if (rLenEQWords)
			_rTxState = `S_TXPORTWR_TX_IDLE;
		else if (rLenEQBufWords)
			_rTxState = `S_TXPORTWR_TX_BUF;
		else
			_rTxState = `S_TXPORTWR_TX_ADJ_1;
	end

	`S_TXPORTWR_TX_WRITE_REM: begin
		_rTxState = `S_TXPORTWR_TX_IDLE;
	end
	
	default: begin
		_rTxState = `S_TXPORTWR_TX_IDLE;
	end
	
	endcase
end



always @ (posedge CLK) begin
	rAckCount <= #1 (RST ? 10'd0 : _rAckCount);
	rNotRequesting <= #1 (RST ? 1'd1 : _rNotRequesting);
	rReqAddr <= #1 _rReqAddr;
	rReqLen <= #1 _rReqLen;
	rReqLast <= #1 _rReqLast;
	rDone <= #1 _rDone;
	rLastDoneRead <= #1 (RST ? 1'd1 : _rLastDoneRead);
end

always @ (*) begin

	if (rTxState[6] | rTxState[7])
		_rNotRequesting = 0;
	else
		_rNotRequesting = (rNotRequesting | rTxReqAck);


	if (rTxState[6]) begin
		_rReqAddr = rAddr;
		_rReqLen = rLen;
		_rReqLast = rLenEQWords;
	end
	else if (rTxState[7]) begin
		_rReqAddr = rAddr;
		_rReqLen = rLastLen;
		_rReqLast = 1;
	end
	else begin
		_rReqAddr = rReqAddr;
		_rReqLen = rReqLen;
		_rReqLast = rReqLast;
	end


	_rDone = (rAckCount == 10'd0);
	if (rMainState[0])
		_rAckCount = 0;
	else
		_rAckCount = rAckCount + rTxState[6] + rTxState[7] - rTxSent;
		

	_rLastDoneRead = (rMainState[6] ? 1'd0 : (rLastDoneRead | rTxnDoneAck));
end







always @ (posedge CLK) begin
	rPartialDone <= #1 _rPartialDone;
	rReqPartialDone <= #1 (RST ? 1'd0 : _rReqPartialDone);
end

always @ (*) begin



	_rPartialDone = (rReqPartialDone & rDone & rNotRequesting & SG_ELEM_EMPTY & rTxState[1]);
	

	if ((rReqPartialDone & rDone & rNotRequesting & SG_ELEM_EMPTY & rTxState[1]) | rMainState[0])
		_rReqPartialDone = 0;
	else
		_rReqPartialDone = (rReqPartialDone | (rTxnAck & !rMainState[3]));
end




endmodule
module tx_port_writer (
	input CLK,
	input RST,
	input [2:0] CONFIG_MAX_PAYLOAD_SIZE,

	output TXN,
	input TXN_ACK,
	output [31:0] TXN_LEN,
	output [31:0] TXN_OFF_LAST,
	output [31:0] TXN_DONE_LEN,
	output TXN_DONE,
	output TXN_ERR,
	input TXN_DONE_ACK,

	input NEW_TXN,
	output NEW_TXN_ACK,
	input NEW_TXN_LAST,
	input [31:0] NEW_TXN_LEN,
	input [30:0] NEW_TXN_OFF,
	input [31:0] NEW_TXN_WORDS_RECVD,
	input NEW_TXN_DONE,

	input [63:0] SG_ELEM_ADDR,
	input [31:0] SG_ELEM_LEN,
	input SG_ELEM_RDY,
	input SG_ELEM_EMPTY,
	output SG_ELEM_REN,
	output SG_RST,
	input SG_ERR,
	
	output TX_REQ,
	input TX_REQ_ACK,
	output [63:0] TX_ADDR,
	output [9:0] TX_LEN,
	output TX_LAST,
	input TX_SENT
);


`include "functions.vh"

(* syn_encoding = "user" *)
(* fsm_encoding = "user" *)
reg		[7:0]						rMainState=`S_TXPORTWR_MAIN_IDLE, _rMainState=`S_TXPORTWR_MAIN_IDLE;
reg		[31:0]						rOffLast=0, _rOffLast=0;
reg									rWordsEQ0=0, _rWordsEQ0=0;
reg									rStarted=0, _rStarted=0;
reg		[31:0]						rDoneLen=0, _rDoneLen=0;
reg									rSgErr=0, _rSgErr=0;
reg									rTxErrd=0, _rTxErrd=0;
reg									rTxnAck=0, _rTxnAck=0;

(* syn_encoding = "user" *)
(* fsm_encoding = "user" *)
reg		[7:0]						rTxState=`S_TXPORTWR_TX_IDLE, _rTxState=`S_TXPORTWR_TX_IDLE;
reg		[31:0]						rSentWords=0, _rSentWords=0;
reg		[31:0]						rWords=0, _rWords=0;
reg		[31:0]						rBufWords=0, _rBufWords=0;
reg		[31:0]						rBufWordsInit=0, _rBufWordsInit=0;
reg									rLargeBuf=0, _rLargeBuf=0;
reg		[63:0]						rAddr=64'd0, _rAddr=64'd0;
reg		[2:0]						rCarry=0, _rCarry=0;
reg									rValsPropagated=0, _rValsPropagated=0;
reg		[5:0]						rValsProp=0, _rValsProp=0;
reg									rCopyBufWords=0, _rCopyBufWords=0;
reg									rUseInit=0, _rUseInit=0;
reg		[10:0]						rPageRem=0, _rPageRem=0;
reg									rPageSpill=0, _rPageSpill=0;
reg									rPageSpillInit=0, _rPageSpillInit=0;
reg		[10:0]						rPreLen=0, _rPreLen=0;
reg		[2:0]						rMaxPayloadSize=0, _rMaxPayloadSize=0;
reg		[2:0]						rMaxPayloadShift=0, _rMaxPayloadShift=0;
reg		[9:0]						rMaxPayload=0, _rMaxPayload=0;
reg									rPayloadSpill=0, _rPayloadSpill=0;
reg									rMaxLen=1, _rMaxLen=1;
reg		[9:0]						rLen=0, _rLen=0;
reg		[31:0]						rSendingWords=0, _rSendingWords=0;
reg									rAvail=0, _rAvail=0;
reg		[1:0]						rTxnDone=0, _rTxnDone=0;
reg		[9:0]						rLastLen=0, _rLastLen=0;
reg									rLastLenEQ0=0, _rLastLenEQ0=0;
reg									rLenEQWords=0, _rLenEQWords=0;
reg									rLenEQBufWords=0, _rLenEQBufWords=0;

reg									rNotRequesting=1, _rNotRequesting=1;
reg		[63:0]						rReqAddr=64'd0, _rReqAddr=64'd0;
reg		[9:0]						rReqLen=0, _rReqLen=0;
reg									rReqLast=0, _rReqLast=0;
reg									rTxReqAck=0, _rTxReqAck=0;

reg									rDone=0, _rDone=0;
reg		[9:0]						rAckCount=0, _rAckCount=0;
reg									rTxSent=0, _rTxSent=0;
reg									rLastDoneRead=1, _rLastDoneRead=1;
reg									rTxnDoneAck=0, _rTxnDoneAck=0;

reg									rReqPartialDone=0, _rReqPartialDone=0;
reg									rPartialDone=0, _rPartialDone=0;


assign NEW_TXN_ACK = rMainState[1];

assign TXN = rMainState[2];
assign TXN_DONE = (rMainState[6] | rPartialDone);
assign TXN_LEN = rWords;
assign TXN_OFF_LAST = rOffLast;
assign TXN_DONE_LEN = rDoneLen;
assign TXN_ERR = rTxErrd;

assign SG_ELEM_REN = rTxState[2];
assign SG_RST = rMainState[3];

assign TX_REQ = !rNotRequesting;
assign TX_ADDR = rReqAddr; 
assign TX_LEN = rReqLen;
assign TX_LAST = rReqLast;



always @ (posedge CLK) begin
	rTxnAck <= #1 (RST ? 1'd0 : _rTxnAck);
	rTxnDoneAck <= #1 (RST ? 1'd0 : _rTxnDoneAck);
	rSgErr <= #1 (RST ? 1'd0 : _rSgErr);
	rTxReqAck <= #1 (RST ? 1'd0 : _rTxReqAck);
	rTxSent <= #1 (RST ? 1'd0 : _rTxSent);
end

always @ (*) begin
	_rTxnAck = TXN_ACK;
	_rTxnDoneAck = TXN_DONE_ACK;
	_rSgErr = SG_ERR;
	_rTxReqAck = TX_REQ_ACK;
	_rTxSent = TX_SENT;
end
	



always @ (posedge CLK) begin
	rMainState <= #1 (RST ? `S_TXPORTWR_MAIN_IDLE : _rMainState);
	rOffLast <= #1 _rOffLast;
	rWordsEQ0 <= #1 _rWordsEQ0;
	rStarted <= #1 _rStarted;
	rDoneLen <= #1 (RST ? 0 : _rDoneLen);
	rTxErrd <= #1 (RST ? 1'd0 : _rTxErrd);
end

always @ (*) begin
	_rMainState = rMainState;
	_rOffLast = rOffLast;
	_rWordsEQ0 = rWordsEQ0;
	_rStarted = rStarted;
	_rDoneLen = rDoneLen;
	_rTxErrd = rTxErrd;
	case (rMainState)

	`S_TXPORTWR_MAIN_IDLE: begin
		_rStarted = 0;
		_rWordsEQ0 = (NEW_TXN_LEN == 0);
		_rOffLast = {NEW_TXN_OFF, NEW_TXN_LAST};
		if (NEW_TXN)
			_rMainState = `S_TXPORTWR_MAIN_CHECK;
	end

	`S_TXPORTWR_MAIN_CHECK: begin
		if (rOffLast[0] | !rWordsEQ0)
			_rMainState = `S_TXPORTWR_MAIN_SIG_NEW;
		else
			_rMainState = `S_TXPORTWR_MAIN_RESET;
	end

	`S_TXPORTWR_MAIN_SIG_NEW: begin
		_rMainState = `S_TXPORTWR_MAIN_NEW_ACK;
	end

	`S_TXPORTWR_MAIN_NEW_ACK: begin
		if (rTxnAck)
			_rMainState = (rWordsEQ0 ? `S_TXPORTWR_MAIN_SIG_DONE : `S_TXPORTWR_MAIN_WRITE);
	end

	`S_TXPORTWR_MAIN_WRITE: begin
		_rStarted = (rStarted | rTxState[1]);
		_rTxErrd = (rTxErrd | rSgErr);
		if (rTxState[0] & rStarted)
			_rMainState = `S_TXPORTWR_MAIN_DONE;
	end

	`S_TXPORTWR_MAIN_DONE: begin
		if (rDone & rLastDoneRead) begin
			_rDoneLen = rSentWords;
			_rMainState = `S_TXPORTWR_MAIN_SIG_DONE;
		end
	end

	`S_TXPORTWR_MAIN_SIG_DONE: begin
		_rTxErrd = 0;
		_rMainState = `S_TXPORTWR_MAIN_RESET;
	end

	`S_TXPORTWR_MAIN_RESET: begin
		if (NEW_TXN_DONE)
			_rMainState = `S_TXPORTWR_MAIN_IDLE;
	end
	
	default: begin
		_rMainState = `S_TXPORTWR_MAIN_IDLE;
	end
	
	endcase
end






wire [9:0] wLastLen = (NEW_TXN_WORDS_RECVD - rSentWords);
wire [9:0] wAddrLoInv = ~rAddr[11:2];
wire [10:0] wPageRem = (wAddrLoInv + 1'd1);	
always @ (posedge CLK) begin
	rTxState <= #1 (RST | rSgErr ? `S_TXPORTWR_TX_IDLE : _rTxState);
	rSentWords <= #1 (rMainState[0] ? 0 : _rSentWords);
	rWords <= #1 _rWords;
	rBufWords <= #1 _rBufWords;
	rBufWordsInit <= #1 _rBufWordsInit;
	rAddr <= #1 _rAddr;
	rCarry <= #1 _rCarry;
	rValsPropagated <= #1 _rValsPropagated;
	rValsProp <= #1 _rValsProp;
	rLargeBuf <= #1 _rLargeBuf;
	rPageRem <= #1 _rPageRem;
	rPageSpill <= #1 _rPageSpill;
	rPageSpillInit <= #1 _rPageSpillInit;
	rCopyBufWords <= #1 _rCopyBufWords;
	rUseInit <= #1 _rUseInit;
	rPreLen <= #1 _rPreLen;
	rMaxPayloadSize <= #1 _rMaxPayloadSize;
	rMaxPayloadShift <= #1 _rMaxPayloadShift;
	rMaxPayload <= #1 _rMaxPayload;
	rPayloadSpill <= #1 _rPayloadSpill;
	rMaxLen <= #1 (RST ? 1'd1 : _rMaxLen);
	rLen <= #1 _rLen;
	rSendingWords <= #1 _rSendingWords;
	rAvail <= #1 _rAvail;
	rTxnDone <= #1 _rTxnDone;
	rLastLen <= #1 _rLastLen;
	rLastLenEQ0 <= #1 _rLastLenEQ0;
	rLenEQWords <= #1 _rLenEQWords;
	rLenEQBufWords <= #1 _rLenEQBufWords;
end

always @ (*) begin
	_rTxState = rTxState;
	_rCopyBufWords = rCopyBufWords;
	_rUseInit = rUseInit;
	
	_rValsProp = ((rValsProp<<1) | rTxState[3]);
	_rValsPropagated = (rValsProp == 6'd0);
	_rLargeBuf = (SG_ELEM_LEN > rWords);
	{_rCarry[0], _rAddr[15:0]} = (rTxState[1] ? SG_ELEM_ADDR[15:0] : (rAddr[15:0] + ({12{rTxState[6]}} & {rLen, 2'd0})));
	{_rCarry[1], _rAddr[31:16]} = (rTxState[1] ? SG_ELEM_ADDR[31:16] : (rAddr[31:16] + rCarry[0]));
	{_rCarry[2], _rAddr[47:32]} = (rTxState[1] ? SG_ELEM_ADDR[47:32] : (rAddr[47:32] + rCarry[1]));
				 _rAddr[63:48] = (rTxState[1] ? SG_ELEM_ADDR[63:48] : (rAddr[63:48] + rCarry[2]));
	_rSentWords = (rTxState[7] ? NEW_TXN_WORDS_RECVD : rSentWords) + ({10{rTxState[6]}} & rLen);
	_rWords = (NEW_TXN_ACK ? NEW_TXN_LEN : (rWords - ({10{rTxState[6]}} & rLen)));
	_rBufWordsInit = (rLargeBuf ? rWords : SG_ELEM_LEN); 
	_rBufWords = (rCopyBufWords ? rBufWordsInit : rBufWords) - ({10{rTxState[6]}} & rLen);
	_rPageRem = wPageRem;	
	_rPageSpillInit = (rBufWordsInit > wPageRem);	
	_rPageSpill = (rBufWords > wPageRem);	
	_rPreLen = ((rPageSpillInit & rUseInit) | (rPageSpill & !rUseInit) ? rPageRem : rBufWords[10:0]);
	_rMaxPayloadSize = CONFIG_MAX_PAYLOAD_SIZE;
	_rMaxPayloadShift = (rMaxPayloadSize > 3'd4 ? 3'd4 : rMaxPayloadSize);
	_rMaxPayload = (6'd32<<rMaxPayloadShift);
	_rPayloadSpill = (rPreLen > rMaxPayload);
	_rMaxLen = ((rMaxLen & !rValsProp[1]) | rTxState[6]);
	_rLen = (rPayloadSpill | rMaxLen ? rMaxPayload : rPreLen[9:0]);
	_rSendingWords = rSentWords + rLen;
	_rAvail = (NEW_TXN_WORDS_RECVD >= rSendingWords);
	_rTxnDone = ((rTxnDone<<1) | NEW_TXN_DONE);
	_rLastLen = wLastLen;
	_rLastLenEQ0 = (rLastLen == 10'd0);
	_rLenEQWords = (rLen == rWords);
	_rLenEQBufWords = (rLen == rBufWords);
	
	case (rTxState)

	`S_TXPORTWR_TX_IDLE: begin
		if (rMainState[4] & !rStarted)
			_rTxState = `S_TXPORTWR_TX_BUF;
	end

	`S_TXPORTWR_TX_BUF: begin
		if (SG_ELEM_RDY)
			_rTxState = `S_TXPORTWR_TX_ADJ_0;
	end

	`S_TXPORTWR_TX_ADJ_0: begin
		_rCopyBufWords = 1;
		_rTxState = `S_TXPORTWR_TX_ADJ_1;
	end

	`S_TXPORTWR_TX_ADJ_1: begin
		_rCopyBufWords = 0;
		_rUseInit = rCopyBufWords;
		_rTxState = `S_TXPORTWR_TX_ADJ_2;
	end

	`S_TXPORTWR_TX_ADJ_2: begin



		_rUseInit = 0;
		if (rValsProp[2])
			_rTxState = `S_TXPORTWR_TX_CHECK_DATA;
	end

	`S_TXPORTWR_TX_CHECK_DATA: begin
		if (rNotRequesting) begin
			if (rAvail)
				_rTxState = `S_TXPORTWR_TX_WRITE;
			else if (rValsPropagated & rTxnDone[1])
				_rTxState = (rLastLenEQ0 ? `S_TXPORTWR_TX_IDLE : `S_TXPORTWR_TX_WRITE_REM);
		end
	end

	`S_TXPORTWR_TX_WRITE: begin
		if (rLenEQWords)
			_rTxState = `S_TXPORTWR_TX_IDLE;
		else if (rLenEQBufWords)
			_rTxState = `S_TXPORTWR_TX_BUF;
		else
			_rTxState = `S_TXPORTWR_TX_ADJ_1;
	end

	`S_TXPORTWR_TX_WRITE_REM: begin
		_rTxState = `S_TXPORTWR_TX_IDLE;
	end
	
	default: begin
		_rTxState = `S_TXPORTWR_TX_IDLE;
	end
	
	endcase
end



always @ (posedge CLK) begin
	rAckCount <= #1 (RST ? 10'd0 : _rAckCount);
	rNotRequesting <= #1 (RST ? 1'd1 : _rNotRequesting);
	rReqAddr <= #1 _rReqAddr;
	rReqLen <= #1 _rReqLen;
	rReqLast <= #1 _rReqLast;
	rDone <= #1 _rDone;
	rLastDoneRead <= #1 (RST ? 1'd1 : _rLastDoneRead);
end

always @ (*) begin

	if (rTxState[6] | rTxState[7])
		_rNotRequesting = 0;
	else
		_rNotRequesting = (rNotRequesting | rTxReqAck);


	if (rTxState[6]) begin
		_rReqAddr = rAddr;
		_rReqLen = rLen;
		_rReqLast = rLenEQWords;
	end
	else if (rTxState[7]) begin
		_rReqAddr = rAddr;
		_rReqLen = rLastLen;
		_rReqLast = 1;
	end
	else begin
		_rReqAddr = rReqAddr;
		_rReqLen = rReqLen;
		_rReqLast = rReqLast;
	end


	_rDone = (rAckCount == 10'd0);
	if (rMainState[0])
		_rAckCount = 0;
	else
		_rAckCount = rAckCount + rTxState[6] + rTxState[7] - rTxSent;
		

	_rLastDoneRead = (rMainState[6] ? 1'd0 : (rLastDoneRead | rTxnDoneAck));
end







always @ (posedge CLK) begin
	rPartialDone <= #1 _rPartialDone;
	rReqPartialDone <= #1 (RST ? 1'd0 : _rReqPartialDone);
end

always @ (*) begin



	_rPartialDone = (rReqPartialDone & rDone & rNotRequesting & SG_ELEM_EMPTY & rTxState[1]);
	

	if ((rReqPartialDone & rDone & rNotRequesting & SG_ELEM_EMPTY & rTxState[1]) | rMainState[0])
		_rReqPartialDone = 0;
	else
		_rReqPartialDone = (rReqPartialDone | (rTxnAck & !rMainState[3]));
end




endmodule