数电第二周总结_CC

重点:
模块实例化、仿真测试、数值表示、参数、表达式、

  • 模块实例化端口连接方法:
    A.顺序端口连接:需严格按照模块定义时的顺序

    B.明明端口连接:对端口信号顺序不做要求

Ex-1
//通过例化二路选择器(mux21)实现四路选择器(mux41)。要求采用“名称连接方式”进行例化。
module mux41(
    input a, b, c, d, sel0, sel1,
    output out);
    
    mux21 mux1(
        .data0(a),
        .data1(b),
        .sel(sel0 & ~sel1),
        .out(out0));
    
    mux21 mux2(
        .data0(out0),
        .data1(c),
        .sel(~sel0 & sel1),
        .out(out1));
    
    mux21 mux3(
        .data0(out1),
        .data1(d),
        .sel(~sel0 & ~sel1),
        .out(out2));
    
    assign out = out2;
    
endmodule
  • 仿真测试: 测试通过Verilog编写的电路功能是否正确

Ex-2
// 为四路选择器(mux41)编写测试平台,验证其功能正确性。
module mux41_tb();
    reg a, b, c, d, sel0, sel1;
    wire out;
    
    mux41 u1(out, a, b, c, d, sel0, sel1);
    initial begin
        a=0; b=1; c=1; d=1; sel0=1; sel1=1;
        #10 a=1; b=0; c=1; d=1; sel0=0; sel1=1;
        #10 a=1; b=0; c=1; d=0; sel0=1; sel1=0;
        #10 a=1; b=1; c=0; d=1; sel0=0; sel1=0;
        #10 a=0; b=1; c=0; d=1; sel0=1; sel1=1;
        #10 a=1; b=0; c=0; d=0; sel0=0; sel1=1;
        #10 a=0; b=1; c=1; d=1; sel0=1; sel1=1;
        #10 a=0; b=0; c=1; d=1; sel0=1; sel1=0;
        #10 a=0; b=0; c=0; d=0; sel0=1; sel1=0;
        #10 a=0; b=0; c=0; d=0; sel0=0; sel1=0;  //Q:为什么需要添加这样的复位操作?
    end
    
    initial
        $display($time, "a=%b b=%b c=%b d=%b sel0=%b sel1=%b out=%b", a, b, c, d, sel0, sel1, out);

endmodule
  • 数值表示——进制:

 

  (1) 4'b1001表示的十进制数为:\(\underline 9\)
  (2) -4'b1001表示的十进制数为:\(\underline {-7}\)
  (3) 4'sb1001表示的十进制数为:\(\underline {-7}\)
  (4) -4'sb1001表示的十进制数为:\(\underline 7\)

  • 数值表示——位宽对齐:

    • 位宽大于实际位数:最高位为x/z,左补x/z;最高位为0/1,左补0.
    • 位宽小于实际位数:低位截断.
  • 参数:

// 参数的申明
module AAAA
#(
    parameter MSB=3,
    parameter LSB=0
)
(
    input [MSB:LSB] in,
    input clk,
    output [MSB:LSB] out,
    output full, empty
);
......
endmodule

// 参数传递——顺序列表连接
AAAA
#(MSB3, LSB2)   //不可直接写后者,会对前者的值进行覆盖
AAAA_inst(
    .in(in),
    .clk(clk),
    .out(out),
    .full(full),
    .empty(empty)
);

// 参数传递——参数名称连接
AAAA
#(.MSB(MSB3),
  .LSB(LSB2))   //无关乎先后顺序
    .in(in),
    .clk(clk),
    .out(out),
    .full(full),
    .empty(empty)
);
  • 表达式:
      A.算术运算符:

    以下式为例:

\[{
Y = (A + C) + (B + D)
}\]

    \({(A+C)+(B+D)}\)的结果长度取每个操作的最大操作数的长度,最终\(Y\)的结果取决于\(Y\)的长度

    B.位运算: 输出长度取决于输入

数电第二周总结_by_yc-小白菜博客


    C.逻辑运算: 输出长度为1

数电第二周总结_by_yc-小白菜博客

    D.缩位运算: 单目运算

数电第二周总结_by_yc-小白菜博客

    E.关系运算符:

    F.移位运算符:

    G.拼接运算符:

数电第二周总结_by_yc-小白菜博客

    H.条件运算符: (? :)

 

运算符练习题:

  已知变量定义为:reg [3:0] a, b; reg [2:0] c; reg[4:0] d; 若a = 4'b0000; b = 4'b1101; c = 3'b100; d = b + c; 请给出下列函数的对应二进制输出
  (1) (b + c) 输出结果为:\(\underline {0001}\)
  (2) d 输出结果为: \(\underline {10001}\)
  (3) (d? c : a) 输出结果为:\(\underline {0100}\)
  (4) (d? a : c) 输出结果为:\(\underline {0000}\)

 
  已知d=6'001010,e=5'bx1100,$display(d|e): 6b'0x1110