3-8译码器与全加器

一、3-8译码器

逻辑真值表如下:

实现 3-8 译码器功能的 Verilog 代码形式也有很多种,我们这里主要列举两种最容易理解的方法:

1)if-else方法

1 module decoder3_8
2 (
3 input wire in1 , //输入信号 in1
4 input wire in2 , //输入信号 in2
5 input wire in3 , //输入信号 in3
6 
7 output reg [7:0] out //输出信号 out
8 );
9 
10 //out:根据 3 个输入信号选择输出对应的 8bit out 信号
11 always@(*)
12 //使用"{}"位拼接符将 3 个 1bit 数据按照顺序拼成一个 3bit 数据
13 if({in1, in2, in3} == 3'b000)
14 out = 8'b0000_0001;
15 else if({in1, in2, in3} == 3'b001)
16 out = 8'b0000_0010;
17 else if({in1, in2, in3} == 3'b010)
18 out = 8'b0000_0100;
19 else if({in1, in2, in3} == 3'b011)
20 out = 8'b0000_1000;
21 else if({in1, in2, in3} == 3'b100)
22 out = 8'b0001_0000;
23 else if({in1, in2, in3} == 3'b101)
24 out = 8'b0010_0000;
25 else if({in1, in2, in3} == 3'b110)
26 out = 8'b0100_0000;
27 else if({in1, in2, in3} == 3'b111)
28 out = 8'b1000_0000;
29 else
30 //最后一个 else 对应的 if 中的条件只有一种情况,还可能产生以上另外的 7 种情况
31 //如果不加这个 else 综合器会把不符合该 if 中条件的上面另外 7 种情况都考虑进去
32 //会产生大量的冗余逻辑并产生 latch(锁存器),所以在组合逻辑中最后一个 if
33 //后一定要加上 else,并任意指定一种确定的输出情况
34 out = 8'b0000_0001;

2)case方法

1 module decoder3_8
2 (
3 input wire in1 , //输入信号 in1
4 input wire in2 , //输入信号 in2
5 input wire in3 , //输入信号 in3
6 
7 output reg [7:0] out //输出信号 out
8 );
9 
10 //out:根据输入的 3bit in 信号选择输出对应的 8bit out 信号
11 always@(*)
12 case({in1, in2, in3})
13 3'b000 : out = 8'b0000_0001; //输入与输出的 8 种译码对应关系
14 3'b001 : out = 8'b0000_0010;
15 3'b010 : out = 8'b0000_0100;
16 3'b011 : out = 8'b0000_1000;
17 3'b100 : out = 8'b0001_0000;
18 3'b101 : out = 8'b0010_0000;
19 3'b110 : out = 8'b0100_0000;
20 3'b111 : out = 8'b1000_0000;
21 //因为 case 中列举了 in 所有可能输入的 8 种情况,且每种情况都有对应确定的输出
22 //所以此处 default 可以省略,但是为了以后因不能够完全列举而产生 latch
23 //所以我们默认一定要加上 default,并任意指定一种确定的输出情况
24 default: out = 8'b0000_0001;
25 endcase
26 
27 endmodule

RTL视图:

二、全加器

1)1位全加器

Verilog:门级

module A1bits(        //Verilog-2001语法
    input A,B,Cin,
	 output Sum,Cout
	 );
	 wire t1,t2,t3;//电路功能描述
	 assign Sum=A^B^Cin;//表达式中的"^"为异或运算符
	 assign t1=A&B,//表达式中的"&"为与运算符
	        t2=A&Cin,
	        t3=B^Cin;
	 assign Cout=t1|t2|t3;//表达式中的"|"为或运算符
	 endmodule
	 

RTL电路:

Logsim

Verilog:行为级

module top_module ();
    reg clk=0;
    always #5 clk = ~clk;  // Create clock with period=10
    initial `probe_start;   // Start the timing diagram
 
    `probe(clk);        // Probe signal "clk"
 
    // A testbench
    reg ain,bin,cin;
    initial begin
        ain=0;
        bin=0;
        cin=0;
        #5 ain=1;
           bin=1;
        #5 ain=0;
           bin=0;
        #5 ain=1;
           cin=1;
        #5 bin=1;
        #60 $finish;            // Quit the simulation
    end
    full_add add(.ain(ain),.bin(bin),.cin(cin));  // Sub-modules work too.
endmodule
module  full_add(input ain,bin,cin,output sum,count);
    reg sum,count;
    always @(ain or bin or cin)begin
    sum=ain+cin+bin;//ain^bin^cin
    count=(ain&bin)|(ain&cin)|(bin&cin); 
    end
    `probe(sum);
    `probe(ain);
    `probe(bin);
    `probe(cin);
    `probe(count);
endmodule

2)4位全加器

Verilog

module A4bit(
       input x,Y,cin
       output f,cout)
;
       assign f=x^y^cin;
       assign cout=(x&y)1(x&cin)l(y&cin);
       endmodule
module CRA(
       input [3:0]x,y
       input cin,
       output [3:0]f,
       output cout;
       )
       wire[4:0]c;
       assign c[0]=cin;
       A4bit fa0(x[0],y[0],c[0],f[0],c[1]);
       A4bit fal(x[1],y[1],c[0],f[1],c[2]);
       A4bit fa2(x[2],y[2],c[0],f[2],c[3]);
       A4bit fa3(x[3],y[3],c[0],f[3],c[4]);
       assign cout=c[4];
  endmodule

RTL电路:

Logsim

Verilog:行为级

module top_module ();
    reg clk=0;
    always #5 clk = ~clk;  // Create clock with period=10
    initial `probe_start;   // Start the timing diagram
 
    `probe(clk);        // Probe signal "clk"
 
    // A testbench
    reg [3:0] ain,bin;
    reg cin;
    initial begin
        ain=0;
        bin=0;
        cin=0;
        #5 ain=2;
           bin=3;
        #5 ain=4;
           bin=2;
        #5 ain=14;
           cin=1;
        #5 bin=1;
        #60 $finish;            // Quit the simulation
    end
    four_add add(.ain(ain),.bin(bin),.cin(cin));  // Sub-modules work too.
endmodule
 
module four_add(ain,bin,cin,sum,count);
    input [3:0] ain,bin,cin;
    output reg count;
    output reg [3:0] sum;
   
    always @(*) begin
        {count,sum}=ain+bin+cin;
    end
    `probe(sum);
    `probe(ain);
    `probe(bin);
    `probe(cin);
    `probe(count);
endmodule

3)8位全加器

采用一位全加器模板实现8位全加器

module A8bit(
       input x,Y,cin
       output f,cout)
;
       assign f=x^y^cin;
       assign cout=(x&y)1(x&cin)l(y&cin);
       endmodule
module CRA(
       input [7:0]x,y
       input cin,
       output [7:0]f,
       output cout;
       )
       wire[8:0]c;
       assign c[0]=cin;
       A4bit fa0(x[0],y[0],c[0],f[0],c[1]);
       A4bit fal(x[1],y[1],c[0],f[1],c[2]);
       A4bit fa2(x[2],y[2],c[0],f[2],c[3]);
       A4bit fa3(x[3],y[3],c[0],f[3],c[4]);
       A4bit fa4(x[4],y[4],c[0],f[4],c[5]);
       A4bit fa5(x[5],y[5],c[0],f[5],c[6]);
       A4bit fa6(x[6],y[6],c[0],f[6],c[7]);
       A4bit fa7(x[7],y[7],c[0],f[7],c[8]);
       assign cout=c[8];
  endmodule

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值