Replication operator¶
What is replication operator and why we need them?¶
The concatenation operator allowed concatenating together vectors to form a larger vector. But sometimes you want the same thing concatenated together many times, and it is still tedious to do something like assign a = {b,b,b,b,b,b};
. The replication operator allows repeating a vector and concatenating them together:
Verilog | |
---|---|
This replicates vector by num times. num must be a constant. Both sets of braces are required.
Examples:
Verilog | |
---|---|
Practice¶
Background information¶
One common place to see a replication operator is when sign-extending a smaller number to a larger one, while preserving its signed value. This is done by replicating the sign bit (the most significant bit) of the smaller number to the left. For example, sign-extending 4'b0101 (5) to 8 bits results in 8'b00000101 (5), while sign-extending 4'b1101 (-3) to 8 bits results in 8'b11111101 (-3).
Problem statement¶
Build a circuit that sign-extends an 8-bit number to 32 bits. This requires a concatenation of 24 copies of the sign bit (i.e., replicate bit[7] 24 times) followed by the 8-bit number itself.
Verilog | |
---|---|
Solution¶
Verilog | |
---|---|
- 尤其需要注意的是,{num{vector}}只有在左右都加上括号的情况下才是一个向量