Verilog编程基础练习
一、Verilog设计一个3-8译码器
(一)Logsim电路绘制一个3-8译码器的电路图:
(二)3-8译码器的真值表:
(三)Verilog设计3-8译码器并生成RTL电路图
- Verilog代码如下:
module YiMa(
a,
b,
c,
out
);
input a; //输入端口a
input b; //输入端口b
input c; //输入端口c
output [7:0]out;//输出端口out
reg [7:0]out;
always@(a,b,c)
begin
case({a,b,c})
3'b000:out = 8'b0000_0001;
3'b001:out = 8'b0000_0010;
3'b010:out = 8'b0000_0100;
3'b011:out = 8'b0000_1000;
3'b100:out = 8'b0001_0000;
3'b101:out = 8'b0010_0000;
3'b110:out = 8'b0100_0000;
3'b111:out = 8'b1000_0000;
endcase
end
endmodule
- RTL电路图如下:
(四)modelsim中仿真生成波形图
(五)问题
-
Verilog综合生成的3-8译码器电路原理与原始设计电路存在什么差别?仿真测试生成的结果是否与真值表一致?
答:Verilog综合生成的电路将内部的基本门电路进行封装,只通过输入输出来观察实行对应的功能,简化电路图,仿真测试生成的结果与真值表相同
-
Verilog代码设计的3-8译码器模块的输出信号 为何要定义为 reg类型而不用默认wire(导线)类型?改成wire型是否可以?
答:reg型表示寄存器类型,用于always模块中被赋值的信号,通常用于存储状态或时序逻辑。wire型表示连续赋值类型,用于assign关键字指定的组合逻辑信号,输入输出都默认为wire类型,通常用于组合逻辑。
二、Verilog设计全加器
(一)门级描述
1.一位全加器
- Verilog代码如下:
module ad(A,B,cin,sum,cout);
input A,B,cin;
output sum,cout;
wire t1,t2,t3,t4;
and U1(t1,A,B);
and U2(t2,A,cin);
and U3(t3,B,cin);
or U4(cout,t1,t2,t3);
xor U5(t4,A,B);
xor U6(sum,t4,cin);
endmodule
- RTL电路图如下:
- Logsim电路图如下:
2.四位全加器
- Verilog代码如下:
module ad(A,B,cin,sum,cout);
input A,B,cin;
output sum,cout;
wire t1,t2,t3,t4;
and U1(t1,A,B);
and U2(t2,A,cin);
and U3(t3,B,cin);
or U4(cout,t1,t2,t3);
xor U5(t4,A,B);
xor U6(sum,t4,cin);
endmodule
module ad4
(
input [3:0]A,B,
input cin,
output [3:0]sum,
output cout
);
wire[4:0]c;
assign c[0] = cin;
ad1 ad10(A[0],B[0],c[0],sum[0],c[1]);
ad1 ad11(A[1],B[1],c[1],sum[1],c[2]);
ad1 ad12(A[2],B[2],c[2],sum[2],c[3]);
ad1 ad13(A[3],B[3],c[3],sum[3],c[4]);
assign cout = c[4];
endmodule
- RTL电路图如下:
-
Logsim电路图如下:
三、Verilog行为级描述设计全加器
(一)一位全加器
- Verilog代码如下 :
module ad_1 (
input A, B, Cin,
output Sum, Cout
);
assign {Cout, Sum} = A + B + Cin;
endmodule
- RTL电路图如下:
(二)四位全加器
-
Verilog代码如下:
module ad1 ( input A, B, Cin, output Sum, Cout ); assign {Cout, Sum} = A + B + Cin; endmodule module ad4 ( input [3:0]A,B, input cin, output [3:0]sum, output cout ); wire[4:0]c; assign c[0] = cin; ad1 ad10(A[0],B[0],c[0],sum[0],c[1]); ad1 ad11(A[1],B[1],c[1],sum[1],c[2]); ad1 ad12(A[2],B[2],c[2],sum[2],c[3]); ad1 ad13(A[3],B[3],c[3],sum[3],c[4]); assign cout = c[4]; endmodule
2.RTL电路图如下:
四、Verilog设计一个8位全加器
- Verilog代码如下:
module ad8 (
input [7:0] A, B,
input cin,
output[7:0]sum,
output cout
);
assign {cout, sum} = A + B + cin;
endmodule
- RTL电路图如下:
五、扩展实验
-
Verilog代码如下:
module ALU16 ( input [15:0] operand_A, input [15:0] operand_B, input [2:0] opcode, // 0: ADD, 1: SUB, 2: AND, 3: OR, 4: XOR, 5: NOT output reg [15:0] result, output reg zero_flag ); always @* begin case(opcode) 3'b000: result = operand_A + operand_B; // 加法 3'b001: result = operand_A - operand_B; // 减法 3'b010: result = operand_A & operand_B; // 与 3'b011: result = operand_A | operand_B; // 或 3'b100: result = operand_A ^ operand_B; // 或非 3'b101: result = ~operand_A; // 非 default: result = 16'b0; // Default case (optional) endcase // Set zero_flag zero_flag = (result == 16'b0); end endmodule
-
RTL电路图如下: