HDLBits (13) — 选择部分向量
2021-09-15 08:19 作者:僚机Wingplane | 我要投稿
本题链接:
https://hdlbits.01xz.net/wiki/Vector2
一个 32 位向量可以被视为包含 4 个字节(bits [31:24]、[23:16] 等)。构建一个电路来反转 4 字节字的字节顺序。
AaaaaaaaBbbbbbbbCcccccccDddddddd => DdddddddCcccccccBbbbbbbbAaaaaaaa
使用此操作通常会在需要转换一段数据的字节序时,例如在使用小端格式的 x86 系统和许多 使用大端格式的Internet 协议中之间。
模块声明:
module top_module(
input [31:0] in,
output [31:0] out );
提示
部分选择在语句的左侧和右侧都可以被使用

题目
module top_module(
input [31:0] in,
output [31:0] out );//
// assign out[31:24] = ...;
endmodule

答案
module top_module (
input [31:0] in,
output [31:0] out
);
assign out[31:24] = in[ 7: 0];
assign out[23:16] = in[15: 8];
assign out[15: 8] = in[23:16];
assign out[ 7: 0] = in[31:24];
endmodule

输出波形


Verillog 支持指定 bit 位后固定位宽的向量域选择访问。
[bit+: width] : 从起始 bit 位开始递增,位宽为 width。
[bit-: width] : 从起始 bit 位开始递减,位宽为 width。
参考内容:
2.3 Verilog 数据类型 | 菜鸟教程:
https://www.runoob.com/w3cnote/verilog-data-type.html