回顾:运算符挑战
本课属于 Coddy Verilog 学习旅程的“基础知识”单元。第 29 课,共 90 课。
挑战
为每项任务写出正确的表达式,完成代码。这个挑战涵盖本章中的所有运算符。
要做什么:
- 逻辑:检查
value1和value2是否都不为零,并将结果存入logic_out - 归约:检查
vector的所有位是否都为 1,并将结果存入reduction_out - 移位:将
data左移 2 位,并将结果存入shift_out - 拼接:将
high和low合并为一个 8 位值,并将结果存入concat_out - 条件:将 `a` 和 `b` 中较大的值存入
cond_out
亲手试试
module operator_challenge;
reg [3:0] value1, value2;
reg logic_out;
reg [3:0] vector;
reg reduction_out;
reg [7:0] data;
reg [7:0] shift_out;
reg [3:0] high, low;
reg [7:0] concat_out;
reg [3:0] a, b;
reg [3:0] cond_out;
initial begin
// 逻辑运算
value1 = 4'd6;
value2 = 4'd0;
logic_out = ______; // 检查 value1 和 value2 是否都不为零
// 归约运算
vector = 4'b1111;
reduction_out = ______; // Check if all bits of vector are 1
// Shift
data = 8'b00001111;
shift_out = ______; // 将 data 左移 2 位
// 拼接
high = 4'b1010;
low = 4'b1100;
concat_out = ______; // 将 high 和 low 合并成一个 8 位值
// 条件运算
a = 4'd7;
b = 4'd12;
cond_out = ______; // 存储 `a` 和 `b` 中较大的值
$display("6 && 0 = %d", logic_out);
$display("&4'b1111 = %d", reduction_out);
$display("00001111 << 2 = %b", shift_out);
$display("{1010, 1100} = %b", concat_out);
$display("max(7, 12) = %d", cond_out);
$finish;
end
endmodule基础知识中的所有课
自己练习: Verilog 在线编译器