Streaming operators usage in the context of serializers in PHY - system-verilog

I have 8:1 serializers and de-serializers based on the data width in our RTL code.As of now we are using for loops for the data path loading and data path reading from the serializers. Can we use streaming operators for this functionality.
Iam new to the streaming operators so exactly i am not getting how to use them in this context.
input [8*DATA_WIDTH-1:0] data_from_user; //WRITE DATA
output [8*DATA_WIDTH-1:0] data_to_user; //READ DATA
output [7:0] data_to_phy_serializer [DATA_WIDTH-1:0];
input [7:0] data_from_phy_deserializer [DATA_WIDTH-1:0];
//WRITE DATA PATH FLOW
always#(posedge clk) begin:WRITE_PATH
for(i = 0 ; i < DATA_WIDTH ; i = i+ 1 )
data_to_phy_serializer[i] = '{
data_from_user[DATA_WIDTH*7 + i],
data_from_user[DATA_WIDTH*6 + i],
data_from_user[DATA_WIDTH*5 + i],
data_from_user[DATA_WIDTH*4 + i],
data_from_user[DATA_WIDTH*3 + i],
data_from_user[DATA_WIDTH*2 + i],
data_from_user[DATA_WIDTH*1 + i],
data_from_user[DATA_WIDTH*0 + i]
} ;
end
//READ DATA PATH FLOW
always#(posedge clk) begin:READ_PATH
for(j= 0 ; j < DATA_WIDTH ; j = j + 1)begin
{
data_to_user[j+DATA_WIDTH*7],
data_to_user[j+DATA_WIDTH*6],
data_to_user[j+DATA_WIDTH*5],
data_to_user[j+DATA_WIDTH*4],
data_to_user[j+DATA_WIDTH*3],
data_to_user[j+DATA_WIDTH*2],
data_to_user[j+DATA_WIDTH*1],
data_to_user[j+DATA_WIDTH*0]
} <= #TCQ data_from_phy_deserializer[j] ;
end
input will be in the form of 8 data words concatenated and i need to the send the data to PHY of each data bit separately by picking up that elements correspondingly from the input data.
this code is working fine but only doubt is can i use streaming operators in this context . Please don't tell the basics of streaming operators, like its for packed to unpacked conversions and viceversa. I need to stream the data for PHY . if i can use streaming operators in this context it will help me so much.
example code for write data path of 4 bit data width to 8:1 serializers
//write data for data width of 4
assign [8*4 -1:0] data = {4'hF,4'hE,4'hD,4'hC,4'hB,4'hA,4'h9,4'h8};
//so now data to each data bit serializer will be
//8:1 data for serializers of
// bit-- 3-- 2-- 1-- 0
// 4'b___1___1___1___1
// 4'b___1___1___1___0
// 4'b___1___1___0___1
// 4'b___1___1___0___0
// 4'b___1___0___1___1
// 4'b___1___0___1___0
// 4'b___1___0___0___1
// 4'b___1___0___0___0
// data for serializer of bit 0 is 8'b10101010
// data for serializer of bit 1 is 8'b11001100
// data for serializer of bit 2 is 8'b11110000
// data for serializer of bit 3 is 8'b11111111
assign [7:0] data_to_phy_serializers [3:0] = '{
8'b11111111,
8'b11110000,
8'b11001100,
8'b10101010
};

Yes you can use it in both cases.I guess this will work:
data_to_phy_serializer = {>>{data_from_user}};
and
data_to_user <= #TCD {>>{data_from_phy_deserializer}};
I have a small experimental example here which you can play with.
module ab;
logic [3:0][1:0]a;
logic [3:0]b[1:0];
logic [3:0][1:0]c;
initial begin
a = 8'hAB;
b = {>>{a}};
c = {>>{b}};
$displayh(a,b[1],b[0],c);
end
endmodule

Related

2D arrays: A net is not a legal lvalue in this context [duplicate]

This question already has an answer here:
Compilation error: A net is not a legal lvalue in this context
(1 answer)
Closed last year.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
I am looking to create a FIFO using a 2D array (P_MDIM is the width of each vector, P_NDIM is the depth of the array/memory to store these vectors).
I am having trouble with the above error message on many spots in my code (assigning to out, full, and empty).
I don't know why it occurs. I thought my array ("memory") would be a 2D array of reg (not nets). Below is my full code:
module fifo( signal, enqueue, dequeue, clk, out, full, empty );
//depth of the memory
parameter P_NDIM = 16;
//width of each memory cell(in bits)
parameter P_MDIM = 16;
input [P_MDIM - 1:0] signal;
input enqueue;
input dequeue;
input clk;
output [P_MDIM - 1:0] out;
output full;
output empty;
logic [P_MDIM - 1:0] [P_NDIM - 1:0] memory = 0;
integer enqueue_pos = 0;
integer i = 0;
always #(posedge clk) begin
//enqueue data
if(enqueue) begin
//checking if memory is full
if(enqueue_pos == P_NDIM - 1) begin
$display("data not entered, memory full");
end
//add signal into memory
else begin
memory[enqueue_pos][P_MDIM-1:0] = signal;
enqueue_pos <= enqueue_pos + 1;
end//else
end//if enqueue
//dequeue data
if(dequeue) begin
//checking if memory is empty
if(enqueue_pos == 0) begin
$display("no data to dequeue, memory empty");
out[P_MDIM-1:0] = 0;
end
//output signal from memory, shift memory
else begin
out = memory[0][P_MDIM - 1:0];
enqueue_pos <= enqueue_pos - 1;
for(i = 0; i <= P_NDIM - 2; i = i + 1) begin
memory[i][P_MDIM-1:0] = memory[i + 1][P_NDIM-1:0];
end//for
end//else
end//if dequeue
//check full and empty
full = (enqueue_pos == P_NDIM-1 ? 1'b0 : 1'b1);
empty = (enqueue_pos == 0 ? 1'b0 : 1'b1);
end//always
endmodule
You declared out as a net type, but then you assigned to it inside a procedural always block. Signals assigned in a procedural block should be declared as logic:
Change:
output [P_MDIM - 1:0] out;
output full;
output empty;
to:
output logic [P_MDIM - 1:0] out;
output logic full;
output logic empty;
Unrelated to the error, in your always block, you are using a mixture of blocking (=) and nonblocking (<=) assignments. They should all be nonblocking.

How do I vary the lower index of a variable assignment?

I want to make an assignment to a variable with a variable lower index. This is what I want to do:
int i;
logic [63:0] data;
i = someCalculatedNumber;
data[63:(i*8)] = 'h0;
I know this won't compile. What is the best method to make this assignment?
If you are looking to zero-out the LSBs, then this should do it for you
data &= '1 << i*8;
or more readable
data = data & ('1 << i*8);
And if that's not exactly what you need, you can still use '1 << i*8 or its complement as a mask to select the portion of data you want to modify.
One way is to use a for loop:
module tb;
int i;
logic [63:0] data;
initial begin
data = '1;
$displayh(data);
i = 7;
for (int j=63; j>=(i*8); j--) data[j] = 0;
$displayh(data);
i = 2;
for (int j=63; j>=(i*8); j--) data[j] = 0;
$displayh(data);
end
endmodule
Output:
ffffffffffffffff
00ffffffffffffff
000000000000ffff
You can wrap the code in a function.

stm32f429, spi dr register not write data

code_1
code_2
register on debug
logic analyzer
void SPI_SendData(SPI_RegDef_t *pSPIx,uint8_t *pTxBuffer , uint32_t Len)
{
while(Len > 0)
{
// 1 . wait until TXE is set ,
while(SPI_GetFlagStatus(pSPIx, SPI_TXE_FLAG) == FLAG_RESET);
// 2. check the DFF bit in CR1
if( (pSPIx->CR1 & (1 << SPI_CR1_DFF) ) )
{
// 16 BIT DFF
pSPIx->DR = *((uint16_t*)pTxBuffer); // dereferencing(typecasting );
Len--;
Len--;
(uint16_t*)pTxBuffer++; // typecasting this pointer to uint16 type and incrementing by 2.
/* The buffer is a uint8_t pointer type. When using the 16-bit data frame,
* we pick up 16-bits of data, increment pointer by 2 bytes,*/
}else
{
// 8 BIT DFF
pSPIx->DR = *pTxBuffer;
Len--;
pTxBuffer++;
/*
*(( uint8_t*)&hspi->Instance->DR) = (*pData);
pData += sizeof(uint8_t);
hspi->TxXferCount--;
*/
}
}
}
i see, MOSI always send 255 on logic analyzer but wrong data.
(uint16_t*)pTxBuffer++; increments the pointer by 1 byte, not two that you say you hope it will in the comment.
If you want to do it by converting to halfword pointer and incrementing, then you need to do something like:
pTxBuffer = (uint8_t*)(((uint16_t*)pTxBuffer) + 1);
But that is just a silly way of saying pTxBuffer += 2;
Really it doesn't make sense to have the if inside the loop, because the value of the DFF bit doesn't change unless you write to it, and you don't. I suggest you write one loop over words and one loop over bytes and have the if at the top level.

How to generate a model for my code using boolector?

I'm experimenting a bit with boolector so I'm trying to create model for simple code. Suppose that I have the following pseudo code:
int a = 5;
int b = 4;
int c = 3;
For this simple set of instructions I can create the model and all works fine. The problem is when I have other instructions after that like
b = 10;
c = 20;
Obviously it fails to generate the model because b cannot be equal to 4 and 10 within the same module. One of the maintainer suggested me to use boolector_push and boolector_pop in order to create new Contexts when needed.
The code for boolector_push is :
void
boolector_push (Btor *btor, uint32_t level)
{
BTOR_ABORT_ARG_NULL (btor);
BTOR_TRAPI ("%u", level);
BTOR_ABORT (!btor_opt_get (btor, BTOR_OPT_INCREMENTAL),
"incremental usage has not been enabled");
if (level == 0) return;
uint32_t i;
for (i = 0; i < level; i++)
{
BTOR_PUSH_STACK (btor->assertions_trail,
BTOR_COUNT_STACK (btor->assertions));
}
btor->num_push_pop++;
}
Instead for boolector_pop is
void
boolector_pop (Btor *btor, uint32_t level)
{
BTOR_ABORT_ARG_NULL (btor);
BTOR_TRAPI ("%u", level);
BTOR_ABORT (!btor_opt_get (btor, BTOR_OPT_INCREMENTAL),
"incremental usage has not been enabled");
BTOR_ABORT (level > BTOR_COUNT_STACK (btor->assertions_trail),
"can not pop more levels (%u) than created via push (%u).",
level,
BTOR_COUNT_STACK (btor->assertions_trail));
if (level == 0) return;
uint32_t i, pos;
BtorNode *cur;
for (i = 0, pos = 0; i < level; i++)
pos = BTOR_POP_STACK (btor->assertions_trail);
while (BTOR_COUNT_STACK (btor->assertions) > pos)
{
cur = BTOR_POP_STACK (btor->assertions);
btor_hashint_table_remove (btor->assertions_cache, btor_node_get_id (cur));
btor_node_release (btor, cur);
}
btor->num_push_pop++;
}
In my opinion, those 2 functions maintains track of the assertions generated using boolector_assert so how is it possible to obtain the final and correct model using boolector_push and boolector_pop considering that the constraints are going to be the same?
What am I missing?
Thanks
As you suspected, solver's push and pop methods aren't what you're looking for here. Instead, you have to turn the program you are modeling into what's known as SSA (Static Single Assignment) form. Here's the wikipedia article on it, which is quite informative: https://en.wikipedia.org/wiki/Static_single_assignment_form
The basic idea is that you "treat" your mutable variables as time-varying values, and give them unique names as you make multiple assignments to them. So, the following:
a = 5
b = a + 2
c = b + 3
c = c + 1
b = c + 6
becomes:
a0 = 5
b0 = a0 + 2
c0 = b0 + 3
c1 = c0 + 1
b1 = c1 + 6
etc. Note that conditionals are tricky to deal with, and generally require what's known as phi-nodes. (i.e., merging the values of branches.) Most compilers do this sort of conversion automatically for you, as it enables many optimizations down the road. You can either do it by hand, or use an algorithm to do it for you, depending on your particular problem.
Here's another question on stack-overflow, that's essentially asking for something similar: Z3 Conditional Statement
Hope this helps!

SystemVerilog error in multiplexing channels : nonconstant index into instance array

I'm designing a module that accepts multiple channels and outputs one channel.
Each channel consists of valid signal and data of some widths.
If a channel has valid data, the module should output that channel. If multiple channels have valid data, the module should output one of them (in my case, channel with highest index) and rests are dropped.
My simple implementation looks like this:
module test1 #(
parameter NUM_CHANNEL = 8,
parameter DATA_WIDTH = 512
) (
input logic [DATA_WIDTH - 1 : 0] data_in [NUM_CHANNEL],
input logic valid_in [NUM_CHANNEL],
output logic [DATA_WIDTH - 1 : 0] data_out,
output logic valid_out
);
always_comb begin
valid_out = 0;
for (int i = 0; i < NUM_CHANNEL; ++i) begin
if (valid_in[i]) begin
valid_out = 1;
data_out = data_in[i];
end
end
end
endmodule
This works perfectly in both simulation and real circuit (FPGA).
However, channel can be complex type so I used interface like this:
interface channel #(
parameter DATA_WIDTH = 512
);
logic valid;
logic [DATA_WIDTH - 1 : 0] data;
modport in (
input valid,
input data
);
modport out (
output valid,
output data
);
endinterface // sub_csr_if
module test #(
parameter NUM_CHANNEL = 8,
parameter DATA_WIDTH = 512
) (
channel.in in[NUM_CHANNEL],
channel.out out
);
always_comb begin
out.valid = 0;
for (int i = 0; i < NUM_CHANNEL; ++i) begin
if (in[i].valid) begin
out.valid = 1;
out.data = in[i].data;
end
end
end
endmodule
Then, this code gets Nonconstant index into instance array 'sub_port'. error in ModelSim, and i is not a constant error in Quartus.
If I unroll the loop, it works but it becomes non-parametric code. (only works for fixed NUM_CHANNEL)
Why the latter one does not work, while the first one works flawlessly?
An array of instances (module or interface) is not a true array type. As your error message indicates, you cannot select a particular instance with a variable index. With a true array, every element is identical. Because of the way parameterization, defparam, and port connections work, each instance element could have differences. The elaboration process essentially flattens all hierarchy before simulation begins.
What you can do is use a generate construct to select your instance as follows
;
module test #(
parameter NUM_CHANNEL = 8,
parameter DATA_WIDTH = 512
) (
channel.in in[NUM_CHANNEL],
channel.out out
);
logic _valid[NUM_CHANNEL];
logic [DATA_WIDTH - 1 : 0] _data[NUM_CHANNEL];
for (genvar ii=0;ii<NUM_CHANNEL;ii++) begin
assign _valid[ii] = in[ii].valid;
assign _data[ii] = in[ii].data;
end
always_comb begin
out.valid = 0;
for (int i = 0; i < NUM_CHANNEL; ++i) begin
if (_valid[i]) begin
out.valid = 1;
out.data = _data[i];
end
end
end
endmodule