题解
基础¶
1-14: 9.20花了1h不到写完.
-
输出1
-
输出0
-
wire
-
多个端口的模块
-
非门
-
与门
-
或非门
-
同或门
-
线网型中间信号
-
向量
-
向量_续 1
-
向量_续 2
-
位操作
笨拙的写法:
module top_module( input [2:0] a, input [2:0] b, output [2:0] out_or_bitwise, output out_or_logical, output [5:0] out_not ); assign out_or_bitwise[2] = a[2] || b[2]; assign out_or_bitwise[1] = a[1] || b[1]; assign out_or_bitwise[0] = a[0] || b[0]; assign out_or_logical = a || b; assign out_not[5] = ~b[2]; assign out_not[4] = ~b[1]; assign out_not[3] = ~b[0]; assign out_not[2] = ~a[2]; assign out_not[1] = ~a[1]; assign out_not[0] = ~a[0]; endmodule优雅的写法:
-
位操作
module top_module( input [3:0] in, output out_and, output out_or, output out_xor ); and(out_and, in[3], in[2], in[1], in[0]); or(out_or, in[3], in[2], in[1], in[0]); xor(out_xor, in[3], in[2], in[1], in[0]); // 还有一种写法是: // assign out_and = ∈ // assign out_or = |in; // assign out_xor = ^in; endmodule -
向量拼接
最为简单暴力的做法:
-
向量翻转
使用generate for循环语句:
module top_module( input [7:0] in, output [7:0] out ); genvar k; generate for (k = 0; k < 8; k = k + 1) begin : bit_reverse // 事实上,: bit_reverse 可删除; assign out[k] = in[7-k]; end endgenerate endmodule或者使用向量拼接的方法:
-
复制算子