Verilog作业(一)

〇、关于本文

本文我的Verilog课程作业,由于我尚处在初学阶段,并且这门课和我实际工作的关系并不大,因此代码仅供参考。

我使用的编译环境为iverilog,在Windows下运行。

一、4-1选择器

文件a.v代码

module mux4_1(out, in0, in1, in2, in3, sel);

    output out;
    input in0, in1, in2, in3;
    input [1:0] sel;
    reg out;

    always @(in0 or in1 or in2 or in3 or sel)
    case(sel)
        2'b00: out = in0;
        2'b01: out = in1;
        2'b10: out = in2;
        2'b11: out = in3;
        default: out = 2'bx;
    endcase
    
endmodule

文件b.v代码

module test();

    wire out;
    reg in0, in1, in2, in3;
    reg [1:0] sel;
    
    mux4_1 dut(.out(out), .in0(in0), .in1(in1), .in2(in2), .in3(in3), .sel(sel));
    
    initial begin
        in0 = 2'b00;
        in1 = 2'b01;
        in2 = 2'b10;
        in3 = 2'b11;
        #1 sel = 2'b00;
        #1 sel = 2'b01;
        #1 sel = 2'b10;
        #1 sel = 2'b11;
    end
    
    initial begin
        $dumpfile("./test.vcd");
        $dumpvars(-1, test);
        $dumpon();
        #6
        $dumpoff();
        $finish;
    end
    
    always #1
    $display("%t:    cout=%b %h %h %h %h %b", $time, out, in0, in1, in2, in3, sel);
    
endmodule

运行结果

103220_MMtW_1425762.png

gtkwave.exe中显示的波形

103541_TywO_1425762.png

二、8位计数器

文件a.v代码

module count_en(clock, reset, out);

  input clock, reset;
  output [7:0] out;
  reg [7:0] out;
  
  always @(posedge clock or negedge reset)
    if(!reset)
      out = 8'b0;           
    else 
      out = out + 1;

endmodule

文件b.v代码

module test();

    reg clock, reset;
    wire[7:0] out;
    
    count_en dut(.clock(clock), .reset(reset), .out(out));
    
    initial begin
        #1 reset = 0;
        #1 reset = 1;
    end
    
    always begin
        #1 clock = 0;
        #1 clock = 1;
    end
    
    initial begin
        $dumpfile("./test.vcd");
        $dumpvars(-1, test);
        $dumpon();
        #40
        $dumpoff();
        $finish;
    end
    
    always #2
    $display("%t:    out=%b", $time, out);
    
endmodule

运行结果

104312_aidd_1425762.png

gtkwave.exe中显示的波形

104322_pyRa_1425762.png

三、设计一个通过函数完成的模块,实现16位无符号数乘法

文件a.v代码

module mult16(product,a,b);

    output [15:0] product;
    input [15:0] a;
    input [15:0] b;
    reg [15:0] product;

    always @(a,b)
        product = multiply(a, b);

    function [15:0] multiply;
        input [15:0] a;
        input [15:0] b;
        multiply = a * b;
    endfunction
    
endmodule

文件b.v代码

module test();

    reg [15:0] a, b;
    wire [15:0] product;
    
    mult16 dut(.a(a), .b(b), .product(product));
    
    initial begin
        #1 a = 16'b00000000; b = 16'b00000000;
        #1 a = 16'b00000010; b = 16'b00000011;
        #1 a = 16'b00010010; b = 16'b01000011;
        #1 a = 16'b01101010; b = 16'b00101011;
        #1 a = 16'b01111111; b = 16'b00001111;
    end
    
    initial begin
        $dumpfile("./test.vcd");
        $dumpvars(-1, test);
        $dumpon();
        #5
        $dumpoff();
        $finish;
    end
    
    always #1
    $display("%t:    a=%h    b=%h    product=%b", $time, a, b, product);
    
endmodule

运行结果

104600_MPt4_1425762.png

gtkwave.exe中显示的波形

104615_XL8I_1425762.png

转载于:https://my.oschina.net/Tsybius2014/blog/277526

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值