Reputation: 33
I am making use of an error detction block. I need to randomly flip 1 bit of the data using SystemVerilig. How do I do this? I also want to do the same where I flip 2 random bits of data.
Hamming Code is being used. So random bits from parity and data need to be flipped.
Upvotes: 0
Views: 2879
Reputation: 337
You can declare a variable with bit-width same as your “data” size. Assuming your “data” size is 8bits(say data_8b), you can declare a temporary variable of size 8bits(say error_inject_8b) and you can use simple EXOR logic to insert multi-bit errors. Refer below snippet
error_inject_8b[0] = 1;
error_inject_8b[1] = 1;
data_8b = data_8b ^ error_inject_8b; //injects error in 0th and 1st bits of data
error_inject_8b = 0;//Reset the errors
error_inject_8b = $random;
data_8b = data_8b ^ error_inject_8b;//Injects error in randomly selected bits
You can also write constraints or randomize with concepts to randomize the error injection based on your requirement.
Upvotes: 0
Reputation: 156
The most straightforward way is to use $urandom_range(0, msb)
to select which bit you want to corrupt. An example:
module test();
parameter DATA_WIDTH = 29;
parameter IDX_WIDTH = $clog2(DATA_WIDTH);
logic [DATA_WIDTH-1:0] data;
logic [IDX_WIDTH-1:0] idx_to_flip;
initial begin
data = $urandom();
$display("Original data = %x", data);
idx_to_flip = $urandom_range(0, DATA_WIDTH-1);
$display("Flipping data bit idx %d", idx_to_flip);
data[idx_to_flip] = !data[idx_to_flip];
$display("Corrupted data = %x", data);
end
endmodule
This is very straightforward for a single bit. If you want to corrupt 2 bits, you will need a bit more complexity to ensure the second bit index selected is not the same as the first selected. There are two options for this:
1) Loop over the second $urandom_range() call until it produces a value different than the first random number.
2) Create a class with two 'rand' fields, and use a constraint to ensure the values are not the same:
class Corruption;
rand bit [IDX_WIDTH-1:0] corrupt_idx1;
rand bit [IDX_WIDTH-1:0] corrupt_idx2;
endclass
Corruption corr = new;
initial begin
corr.randomize() with {corrupt_idx1 != corrupt_idx2;};
$display("Bit indices to flip: %d and %d", corr.corrupt_idx1, corr.corrupt_idx2);
end
Upvotes: 1