一、Quartus、ModelSim下载及其破解
-
电脑准备:需要用到两个软件:Quartus II和Modelsim SE,具体安装和破解请参照以下教程
Quartus II 13.1的安装及使用
Modelsim SE安装与介绍
二、3-8译码器
(一)3-8译码器简介介绍:
3-8译码器是一种数字电路元件,用于将3位二进制输入信号转换为8位输出信号。这种译码器的主要功能是根据输入信号的组合,将其中一个输出线激活。每个可能的输入组合对应一个唯一的输出线,这使得3-8译码器在数字系统中的许多应用中都非常有用。
1.输入: 3-8译码器有3个输入线,每一条线都可以是逻辑0或逻辑1。
2.输出: 有8个输出线,每个输出线对应于一个可能的3位输入组合。
3. 功能:3-8译码器的主要功能是根据输入的二进制组合激活相应的输出线。只有一个输出线处于逻辑高电平状态,其余输出线处于逻辑低电平状态。
4.应用:3-8译码器在数字系统中有许多应用,其中一种主要的用途是将特定的输入模式映射到相应的输出。它常用于地址译码、显示驱动和其他需要多个输出状态的场合。
(二)Logsim绘制电路图
1.使用logisim-evolution绘制3-8译码器电路图:
2.真值表:
a | b | c | Y0 | Y1 | Y2 | Y3 | Y4 | Y5 | Y6 | Y7 |
---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
0 | 1 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
(三)用Verilog语言进行3-8译码器仿真实验
1.verilog代码如下:
module test(data_out, data_in ) ;
input [2:0] data_in;
output [7:0] data_out;
reg [7:0] data_out;
always @(data_in)
begin
case (data_in )
3'b000: data_out=8'b11111110;
3'b001: data_out=8'b11111101;
3'b010: data_out=8'b11111011;
3'b011: data_out=8'b11110111;
3'b100: data_out=8'b11101111;
3'b101: data_out=8'b11011111;
3'b110: data_out=8'b10111111;
3'b111: data_out=8'b01111111;
default: data_out=8'bxxxxxxxx;
endcase
end
endmodule
2.仿真得到RTE电路图
3.编写一个仿真测试文件,对3-8译码器进行仿真测试,输出测试波形图和Transcript结果
(四)结论:
分析并回答下列问题:
1)Verilog 综合生成的3-8译码器电路原理图与原始设计电路存在什么差异? 仿真测试生成的结果是否与真值表一致?
答:输入端与输出端,其余基本无差异。Verilog综合生成的电路图中,内部的基本门电路被封装成高层次的模块,用户只需与模块的输入和输出进行交互。
仿真测试结果与真值表一致
2) Verilog代码设计的3-8译码器模块的输出信号 为何要定义为 reg类型而不用默认wire(导线)类型?改成wire型是否可以? (即是否可以把 output reg [7:0] out 改为 output [7:0] out) 修改后会出现什么错误?为什么会出错?
答:Verilog中,reg
和 wire
是两种不同的数据类型,用于描述信号的行为。reg
用于描述存储器元素,而 wire
用于描述组合逻辑元素。在3-8译码器这样的组合逻辑电路中,通常使用 wire
来表示输出信号。如果你将其连接到其他模块的输出或者在其他模块中对其进行赋值,可能需要注意确保与其他 wire
类型兼容。在一般的组合逻辑中,wire
是更常用的类型。
三、全加器的仿真实验
(一)一位全加器:
1.一位全加器logsim电路图:
2.verilog语言代码:
module test(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
3.RTL电路图:
(二)四位全加器:
1.四位全加器logsim电路图:
2.verilog语言代码:
module quanjiaqi4(
input [3:0] A,
input [3:0] B,
input Cin,
output [3:0] Sum,
output Cout
);
wire [3:0] carry_out_intermediate;
wire [3:0] sum_intermediate;
FullAdder1Bit fa0 (.A(A[0]), .B(B[0]), .Cin(Cin), .Sum(sum_intermediate[0]), .Cout(carry_out_intermediate[0]));
FullAdder1Bit fa1 (.A(A[1]), .B(B[1]), .Cin(carry_out_intermediate[0]), .Sum(sum_intermediate[1]), .Cout(carry_out_intermediate[1]));
FullAdder1Bit fa2 (.A(A[2]), .B(B[2]), .Cin(carry_out_intermediate[1]), .Sum(sum_intermediate[2]), .Cout(carry_out_intermediate[2]));
FullAdder1Bit fa3 (.A(A[3]), .B(B[3]), .Cin(carry_out_intermediate[2]), .Sum(sum_intermediate[3]), .Cout(carry_out_intermediate[3]));
assign Sum = sum_intermediate;
assign Cout = carry_out_intermediate[3];
endmodule
module FullAdder1Bit(
input A,
input B,
input Cin,
output Sum,
output Cout
);
assign Sum = A ^ B ^ Cin;
assign Cout = (A & B) | (B & Cin) | (A & Cin);
endmodule
3.RTL电路图:
(三)八位全加器:
1.Verilog的八位全加器代码:
module test8 (
input wire [7:0] A, // 8位输入A
input wire [7:0] B, // 8位输入B
output wire [7:0] Sum, // 8位和输出
output wire Cout // 进位输出
);
// 内部信号
wire [7:0] Carry;
// 生成八个一位全加器实例
OneBitAdder FA0(A[0], B[0], 1'b0, Sum[0], Carry[0]);
OneBitAdder FA1(A[1], B[1], Carry[0], Sum[1], Carry[1]);
OneBitAdder FA2(A[2], B[2], Carry[1], Sum[2], Carry[2]);
OneBitAdder FA3(A[3], B[3], Carry[2], Sum[3], Carry[3]);
OneBitAdder FA4(A[4], B[4], Carry[3], Sum[4], Carry[4]);
OneBitAdder FA5(A[5], B[5], Carry[4], Sum[5], Carry[5]);
OneBitAdder FA6(A[6], B[6], Carry[5], Sum[6], Carry[6]);
OneBitAdder FA7(A[7], B[7], Carry[6], Sum[7], Cout);
endmodule
module OneBitAdder (
input wire a,
input wire b,
input wire Cin,
output wire Sum,
output wire Cout
);
// 位运算:a、b和进位相加,结果保存在Sum中
assign Sum = a ^ b ^ Cin;
// 计算进位
assign Cout = (a & b) | (b & Cin) | (a & Cin);
endmodule
RTL电路图:
四、扩展实验
(一)题目:学习教材上的 并行加法器原理(先行进位加法器),参考附件资料,完成一个16位ALU(算术逻辑单元)的电路设计,采用Verilog设计模式,生成RTL电路。
Verilog代码:
module test16 (
input wire [15:0] A,
input wire [15:0] B,
input wire [3:0] opcode,
output reg [15:0] result,
output reg zero,
output reg overflow,
output reg carry
);
always @(A or B or opcode) begin
case(opcode)
4'b0000: // Addition
begin
result = A + B;
zero = (result == 16'b0);
overflow = (A[15] & B[15] & ~result[15]) | (~A[15] & ~B[15] & result[15]);
carry = (A[15] & B[15]) | (~result[15] & (A[15] | B[15]));
end
4'b0001: // Subtraction
begin
result = A - B;
zero = (result == 16'b0);
overflow = (A[15] & ~B[15] & ~result[15]) | (~A[15] & B[15] & result[15]);
carry = (A[15] | ~B[15]) & (~result[15] | (A[15] & ~B[15]));
end
4'b0010: // AND
begin
result = A & B;
zero = (result == 16'b0);
overflow = 1'b0;
carry = 1'b0;
end
4'b0011: // OR
begin
result = A | B;
zero = (result == 16'b0);
overflow = 1'b0;
carry = 1'b0;
end
4'b0100: // XOR
begin
result = A ^ B;
zero = (result == 16'b0);
overflow = 1'b0;
carry = 1'b0;
end
// Add more operations as needed
// ...
default:
begin
result = 16'b0;
zero = 1'b0;
overflow = 1'b0;
carry = 1'b0;
end
endcase
end
endmodule
电路图: