1

I was trying to store two specific spans of an array inside another array, but I get an error.

What I want to do:

I have [8-1:0]A as module input, and I wanna store :

logic [8-1:0]temp = {A[4:7],A[0:3]};

but, when I simulate my module in test bench, I get an error in modelSim:

error: Range of part-select into 'A' is reversed.

Ways I tried:

Convert logic to wire, Use assign

I think the idea is problematic.

example :

A = 8'b11000101 -> I want temp to be -> temp=8'b00111010

->explain:

A[0]=1,A[1]=0,A[2]=1,A[3]=0,A[4]=0,A[5]=0,A[6]=1,A[7]=1.
A[4..7]=4'b0011,A[0..3]=4'b1010

`timescale 1ns/1ns 
module examp(input [7:0]A,output [7:0]O);
    logic [7:0]temp = {A[4:7],A[0:3]};
//  I wanna temp be  8'b00111010.
    assign O = temp;
endmodule

`timescale 1ns/1ns 
module examp_tb();
    logic [7:0]aa=8'b11000101;
    wire [7:0]ww;

    examp MUX_TB(.A(aa),.O(ww));

    initial begin

    #200 aa=8'b01100111;
    
    #200 $stop;
    end
endmodule

enter image description here

Note : In the example above, I have a compile error, but in the main question, I have simulation error.

toolic
  • 57,801
  • 17
  • 75
  • 117
ryhn
  • 96
  • 6

2 Answers2

2

The streaming operator can be used to reverse a group of bits. So you could do:

logic [8-1:0]temp = { {<<{A[7:4]}} , {<<{A[3:0]}} };

The streaming operator also takes a slice argument, which is used to preserve a grouping of bits before performing the bit reversal. The problem with what you want is you are trying to reverse the bits within the slice. You can accomplish this by nesting the streaming operators. This approach with be more useful when dealing with larger vectors

logic [7:0] temp1 = {<<{A}}; // A[0:7]
logic [7:0] temp2 = {<<4{temp1}}; // A[4:7],A[03];

or in a single line

logic [7:0] temp = {<<4{ {<<{A}} }}; 
dave_59
  • 39,096
  • 3
  • 24
  • 63
1

One way to swap bits within a nibble is to use a function:

module examp (input [7:0] A, output [7:0] O);
    assign O = {swap_nib(A[7:4]),  swap_nib(A[3:0])};

    function logic [3:0] swap_nib (logic [3:0] in);
        swap_nib[3] = in[0];
        swap_nib[2] = in[1];
        swap_nib[1] = in[2];
        swap_nib[0] = in[3];
    endfunction
endmodule
toolic
  • 57,801
  • 17
  • 75
  • 117