HDLBits刷题合集—3 Vectors
HDLBits-11 Vector0
Problem Statement
建立一个电路,有一个3位输入,然后输出相同的向量,并把它分成三个独立的1位输出。将输出o0连接到输入向量的位置0,o1连接到位置1,o2连接到位置2。
在下图中,带有数字的标记表示向量(或“总线”)的宽度,而不是向量中的每个位绘制单独的线。
代码如下:
module top_module (
input wire [2:0] vec,
output wire [2:0] outv,
output wire o2,
output wire o1,
output wire o0 ); // Module body starts after module declaration
assign outv = vec;
assign o0 = vec[0];
assign o1 = vec[1];
assign o2 = vec[2];
endmodule
HDLBits-12 Vector1
Problem Statement
建立一个组合电路,将一个输入的半个字(即一个32位的字,取16位,[15:0]),分成[7:0]和[15:8]两个字节。
代码如下:
`default_nettype none // Disable implicit nets. Reduces some types of bugs.
module top_module(
input wire [15:0] in,
output wire [7:0] out_hi,
output wire [7:0] out_lo );
assign out_hi = in[15:8];
assign out_lo = in[7:0];
endmodule
HDLBits-13 Vector2
Problem Statement
32位向量可以看作包含4个字节(分别是[31:24]、[23:16]、[15:8]和[7:0]等)。建立一个电路,将4字节字的顺序方向。如下所示:
AaaaaaaaBbbbbbbbCcccccccDddddddd=>DdddddddCcccccccBbbbbbbbAaaaaaaa
当需要交换一段数据的顺序时,通常会使用这种操作。
代码如下: