4

In Python, I can select even or odd bits like this:

>>> bits = ['a','b','c','d'];
>>> bits[0::2]
['a', 'c']
>>> bits[1::2]
['b', 'd']

It would be very practical if I could do this in Verilog, so that I wouldn't have to expand the expression and do it manually. Expanded (i.e. {a[0], a[2]} and {a[1], a[3]}), it obviously wouldn't work with my otherwise parameterized wire set.

2 Answers 2

2

There is no mechanism in Verilog or SystemVerilog to do a bit slice like the Python example you gave. That is, you cannot specify a step of 2 between bits.

You can do this with a for-loop, and it doesn't need to be in a generate block like in your own answer.

Modified example from your answer:

always @(*) begin
   for (int i = 0; i < FLOORS; i++) begin
      RELEVANT[i] <= FLOOR_REQUEST[i*2+FORWARD];
   end
end

This should synthesize okay as long as FLOORS is a constant.

Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't this solution require that RELEVANT is a reg? Would it synthesize to a flip-flop in this case?
It would need to declared as a reg type, but that does not automatically mean it will synthesize to a flop. In this case it would not.
1

It can be done with a generate block. Example:

wire [FLOORS-1:0] RELEVANT;

genvar i;
generate
    for (i=0; i<FLOORS; i=i+1) begin
        assign RELEVANT[i] = FLOOR_REQUEST[i*2+FORWARD];
    end
endgenerate
  • FLOORS is the width of the output wire (half the width of the input wire).
  • RELEVANT is the result.
  • FORWARD is the even/odd selector (0 or 1).
  • FLOOR_REQUEST is the input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.