基本组合逻辑电路练习

  基本组合逻辑电路

  ·  编码器

  ·  译码器/数据分配器

  ·  数据选择器

  ·  数值比较器

  ·  算术运算电路(加法器、减法器、乘法器)

 

  1.0 编码器(以4线-2线编码器为例)

  采用数据流方式描述:

  

代码
1 //Dataflow description of 4to2 encoder
2  module encoder(
3 a,b,c,d,
4 out0,out1
5 );
6
7 input a,b,c,d;
8 output out0,out1;
9
10 assign out0 = ((~a)&(~b)&(c)&(~d))|((~a)&(~b)&(~c)&(d));
11 assign out1 = ((~a)&(b)&(~c)&(~d))|((~a)&(~b)&(~c)&(d));
12
13 endmodule
14

  

  查看RTL视图:

        

  

  跟我在纸上画的不太一样,其实out0和out1的逻辑表达式中有一项是完全一样的,也就是说可以只用三个4输入与门,它综合成的电路总跟想象的有点其别,我估计这个跟cyclone iii的基本单元LEs有关系。

  再看一下仿真波形:

         

  符合要求。

 

  这个编码器有他的不足就是当我输入信号同时几个为高的时候就会出错,也就是说没有优先级。在来看一下有优先级的编码器还是以4-2为例。

  用行为级方式描述4-2优先级编码器:

 

代码
1 module encoder_2(
2 In,
3 Out
4 );
5
6 input[3:0] In;
7 output[1:0] Out;
8 reg[1:0] Out;
9
10 always @ (In)
11 begin
12 case(In)
13 4'b1???:Out = 2'b00;
14 4'b01??:Out = 2'b01;
15 4'b001?:Out = 2'b10;
16 4'b0001:Out = 2'b11;
17 default:Out = 2'b00;
18 endcase
19 end
20
21 endmodule

  本以为这种写法可以完成优先级的功能,出现警告Warning (10059): Verilog HDL Case Statement warning at encoder_2.v(13): case item expression never matches the case expression because it contains an 'x' or 'z' value。没理他,结果仿真波形不对,只要在输入0001的时候out为11,0000时out为00,其他的有效状态均不对。将问号改成0,1特定的值就对。要查阅相关的资料搞清楚。

 

  一般简单的组合逻辑电路会出现三种比较常见的警告

  (1) Warning: Some pins have incomplete I/O assignments. Refer to the I/O Assignment Warnings report for details

  (2)Critical Warning: No exact pin location assignment(s) for 6 pins of 6 total pins

  (3)Critical Warning: Synopsys Design Constraints File file not found: 'encoder_2.sdc'. A Synopsys Design Constraints File is required by the TimeQuest Timing Analyzer to get proper timing constraints. Without it, the Compiler will not properly optimize the design.

  (4)Warning: No clocks defined in design.

 

  1.1 译码器

  以3-8译码器为例,用行为级描述

 

代码
1 module Decoder_0(
2 In,
3 Out
4 );
5
6  input[2:0] In;
7  output[7:0] Out;
8  reg[7:0] Out;
9
10  always @ (In)
11 begin
12 case(In)
13 3'b000: Out = 8'b0000_0001;
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 default:Out = 8'b0000_0000;
22 endcase
23 end
24
25 endmodule

  查看RTL视图:

          

  为什么Qii 9.1用行为级描述的时候RTL视图就不能以最基本的逻辑门呈现出来了?

  仿真波形正确;

            

 

  1.2  数据分配器和数据选择器(略)

 

  1.3  数值比较器

  用数据流的方式描述4位比较器

 

1 module Comparator_0(
2 A,B,
3 ALTB,AGTB,AEQB
4 );
5
6  input[3:0] A,B;
7  output ALTB,AGTB,AEQB;
8
9  assign ALTB = (A<B);
10 assign AGTB = (A>B);
11 assign AEQB = (A==B);
12
13 endmodule
14

  至于是一个多少位的比较器,用数据流或则是行为级来描述的话就不是那么的重要了。查看RTL视图:
      

  写testbench:

 

代码
1 `timescale 1 ps/ 1 ps
2 module Comparator_0_vlg_tst();
3
4 reg [3:0] A;
5 reg [3:0] B;
6 // wires
7 wire AEQB;
8 wire AGTB;
9 wire ALTB;
10
11 // assign statements (if any)
12 Comparator_0 i1 (
13 // port map - connection between master ports and signals/registers
14 .A(A),
15 .AEQB(AEQB),
16 .AGTB(AGTB),
17 .ALTB(ALTB),
18 .B(B)
19 );
20 initial
21 begin
22 A = 4'b0000;
23 B = 4'b0000;
24 end
25
26 always
27 begin
28 #5 A = {$random}%17;
29 end
30
31 always
32 begin
33 #5 B = {$random}%17;
34 end
35
36 endmodule

  在这里用了一下Verilog中产生随机数的系统任务 $random用来对A,B赋予随机数。仿真波形正确:

       

 

  1.4  算术运算电路 

   

 

 

  

  

       

posted on 2010-09-19 21:32  zxl2431  阅读(6010)  评论(0编辑  收藏  举报

导航