目录

一、3-8译码器

1、用 Logsim 绘制3-8译码器电路图

2、3-8译码器逻辑真值表

3、用 Verilog 编程(if-else)设计3-8译码器,生成RTL原理电路图并进行仿真

(1)、Verilog 实现三八译码器代码

(2)、生成RTL电路如下图

(3)、仿真

3、实验问题分析

二、全加器电路

1、1位全加器

(1)、Logsim逻辑电路图

(2)、Verilog代码及生成的RTL电路

  2、 4位全加器

(1)、Logsim逻辑电路图

(2)、Verilog代码及生成的RTL电路

3、用Verilog的行为级方式完成1位全加器和4位全加器

(1)、1位全加器

(2)、4位全加起

3、 8位全加器

一、3-8译码器

1、用 Logsim 绘制3-8译码器电路图

2、3-8译码器逻辑真值表

3-8译码器真值表

abcY0Y1Y2Y3Y4Y5Y6Y70000000000100100000010010000001000110000100010000010000101001000001100100000011110000000

3、用 Verilog 编程(if-else)设计3-8译码器,生成RTL原理电路图并进行仿真

(1)、Verilog 实现三八译码器代码

module decoder3_8

(

input wire in1 , //输入信号 in1

input wire in2 , //输入信号 in2

input wire in3 , //输入信号 in3

output reg [7:0] out //输出信号 out

);

always@(*)

if({in1, in2, in3} == 3'b000)

out = 8'b0000_0001;

else if({in1, in2, in3} == 3'b001)

out = 8'b0000_0010;

else if({in1, in2, in3} == 3'b010)

out = 8'b0000_0100;

else if({in1, in2, in3} == 3'b011)

out = 8'b0000_1000;

else if({in1, in2, in3} == 3'b100)

out = 8'b0001_0000;

else if({in1, in2, in3} == 3'b101)

out = 8'b0010_0000;

else if({in1, in2, in3} == 3'b110)

out = 8'b0100_0000;

else if({in1, in2, in3} == 3'b111)

out = 8'b1000_0000;

else

out = 8'b0000_0001;

endmodule

`timescale 1ns/1ns

module tb_decoder3_8();

reg in1;

reg in2;

reg in3;

wire [7:0] out;

initial begin

in1 <= 1'b0;

in2 <= 1'b0;

in3 <= 1'b0;

end

always #10 in1 <= {$random} % 2;

always #10 in2 <= {$random} % 2;

always #10 in3 <= {$random} % 2;

initial begin

$timeformat(-9, 0, "ns", 6);

$monitor("@time %t:in1=%b in2=%b in3=%b out=%b",$time,in1,in2,in3,out);

end

decoder3_8 decoder3_8_ins

(

.in1(in1), //input in1

.in2(in2), //input in2

.in3(in3), //input in3

.out(out) //output [7:0] out

);

endmodule

(2)、生成RTL电路如下图

(3)、仿真

仿真测试文件:

timescale 1ns/1ns

module three_to_eight_tb();

reg a;

reg b;

reg c;

wire [7:0] out;

initial begin

a <= 1'b1;

b <= 1'b0;

c <= 1'b1;

end

always #10 a <= {$random} % 2;

always #10 b <= {$random} % 2;

always #10 c <= {$random} % 2;

initial begin

$timeformat(-9, 0, "ns", 6);

$monitor("@time %t:a=%b b=%b c=%b out=%b",$time,a,b,c,out);

end

three_to_eight three_to_eight_ins

(

.a(a),

.b(b),

.c(c),

.out(out)

);

endmodule

仿真结果:

3、实验问题分析

(1)、Verilog综合生成的3-8译码器电路原理图与原始设计电路存在什么差异?仿真测试生成的结果是否与真值表一致?

       Verilog综合生成的3-8译码器电路原理图与原始设计电路在结构和连接上有所不同,原始设计电路时基于门电路的物理链接,而Verilog综合生成的电路是基于编程逻辑单元的配置,这种差异体现在实现的方式上,但两种方式在功能上是相同的。

(2)、Verilog代码设计的3-8译码器模块的输出信号为何要定义为reg类型而不用默认wire(导线)类型?改成wire型是否可以?(即是否可以把 output reg [7:0] out 改为 output [7:0] out)修改后会出现哪些错误?为什么会出现错误?

        reg类型用于定义寄存器,即具有存储功能的元件,而wire类型用于定义导线,及用于连接模块间的信号传输,3-8译码器模块中,输出信号需要根据输入信号的变化而变化,因此需要被定义为reg类型。   

        如果将3-8译码器模块的输出信号由reg类型改为wire类型,那么这个信号将不再有存储功能,也就是说,模块内部不能再对这个信号进行赋值、计算或驱动,浙江导致模块无法正常工作,因为输出信号需要根据输入信号的变化而变化。

        如果将output reg [7:0] out 改为output [7:0] out,那么在模块内部的赋值操作将无法执行,例如,例如 out = 3'b101;这样的语句将无法编译通过,因为 wire类型的信号不能在模块内部被赋值。

二、全加器电路

1、1位全加器

(1)、Logsim逻辑电路图

(2)、Verilog代码及生成的RTL电路

代码:

module full_adder(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电路

  2、 4位全加器

(1)、Logsim逻辑电路图

(2)、Verilog代码及生成的RTL电路

代码:

module eight(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电路:

3、用Verilog的行为级方式完成1位全加器和4位全加器

(1)、1位全加器

代码:

module fulladder(

input a,

input b,

input cin,

output sum,

output cout

);

assign sum = a ^ b ^ cin;

assign cout = (a & b) | (b & cin) | (a & cin);

endmodule

RTL电路

(2)、4位全加起

代码:

module addr4

(

input wire [3:0] ina ,

input wire [3:0] inb ,

input wire cin ,

output wire [3:0] sum ,

output wire cout

);

assign {cout,sum}=ina+inb+cin;

endmodule

RTL电路:

3、 8位全加器

代码:

module eight(

input clk,

input rst_n,

input [7:0] a,

input [7:0] b,

input cin,

input enable,

output reg [7:0] sum,

output reg cout

);

always@(posedge clk or negedge rst_n)begin

if(!rst_n)begin

sum <= 8'b0;

cout <= 1'b0;

end

else if(enable)begin

{cout,sum} <= a+b+cin;

end

else begin

sum <= sum;

cout <= cout;

end

end

endmodule

RTL电路:

 

好文阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。