FPGA project : MUX4_1

本文详细描述了一个4:1多路选择器(MUX)的Verilog代码实现,包括使用task、function和case结构的选择逻辑。同时给出了一个测试bench来验证选择器在不同输入条件下的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MUX4_1 works as follows:

  • Select the "din" according to the value of "sel"

  • When the asynchronous reset key is pressed (rst_n == 0), the output is in a high resistance state.

  • The 4 option 1 data selector is implemented with task, function and case respectively.

code :

module MUX4_1 (

    input                   clk       ,

    input                   rst_n     ,

    input           [3:0]   din_1     ,

    input           [3:0]   din_2     ,

    input           [3:0]   din_3     ,

    input           [3:0]   din_4     ,

    input           [1:0]   sel       ,

    output     reg  [3:0]   dout        //reg,If it is not reg, the task cannot be get a value

);

    /***********************task***********************/

    // task                    f_1     ;

    // input   [1:0]           sel_r   ;

    // output  [3:0]           dout_r  ;

    //         case (sel_r)

    //             0 : dout_r <= din_1 ;// = or <=

    //             1 : dout_r <= din_2 ;

    //             2 : dout_r <= din_3 ;

    //             3 : dout_r <= din_4 ;

    //             default: dout_r <= 4'bzzzz ;

    //         endcase

    // endtask

    // always @(posedge clk or negedge rst_n)begin

    //     if(~rst_n)begin

    //         dout <= 4'bzzzz ;

    //     end else begin

    //         f_1 (sel,dout) ;

    //     end

    // end

    /***********************function***********************/

    // function  [3:0] f_1     ;

    //     input [1:0] sel_r   ;

    //      begin

    //             case (sel_r)

    //             0 : f_1 = din_1 ;// = or <=  You can only use =. Otherwise, an error will be reported. Functions cannot contain non-blocking assignments.

    //             1 : f_1 = din_2 ;

    //             2 : f_1 = din_3 ;

    //             3 : f_1 = din_4 ;

    //             default: f_1 = 4'bzzzz ;

    //             endcase

    //      end

    // endfunction

    // always @(posedge clk or negedge rst_n) begin

    //     if(~rst_n)begin

    //         dout <= 4'bzzzz ;

    //     end else begin

    //         dout <= f_1(sel) ;

    //     end

    // end

    /***********************case***********************/

    always @(posedge clk or negedge rst_n) begin

        if(~rst_n) begin

            dout <= 4'bzzzz ;

        end else begin

            case (sel)

                0 : dout <= din_1 ;

                1 : dout <= din_2 ;

                2 : dout <= din_3 ;

                3 : dout <= din_4 ;

                default: dout <= 4'bzzzz ;

            endcase

        end

    end

endmodule

testbench :

`timescale 1ns/1ps

module test();

    reg                    clk       ;

    reg                    rst_n     ;

    reg            [3:0]   din_1     ;

    reg            [3:0]   din_2     ;

    reg            [3:0]   din_3     ;

    reg            [3:0]   din_4     ;

    reg            [1:0]   sel       ;

    wire           [3:0]   dout      ;

    parameter           CYCLE = 20 ;

MUX4_1 MUX4_1_insert (

    .clk                ( clk   ) ,

    .rst_n              ( rst_n ) ,

    .din_1              ( din_1 ) ,

    .din_2              ( din_2 ) ,

    .din_3              ( din_3 ) ,

    .din_4              ( din_4 ) ,

    .sel                ( sel   ) ,

    .dout               ( dout  )  

);

    initial begin

        clk = 1'b1       ;

        rst_n = 1'b1     ;

        #( CYCLE * 5 )   ;

        rst_n = 1'b0     ;

        din_1  = 4'b0001 ;

        din_2  = 4'b0010 ;

        din_3  = 4'b0100 ;

        din_4  = 4'b1000 ;

        #( CYCLE * 5 )   ;

        rst_n = 1'b1     ;

        #( CYCLE )       ;

        sel   = 0        ;

        #( CYCLE * 10 )  ;

        sel   = 1        ;

        #( CYCLE * 10 )  ;

        sel   = 2        ;

        #( CYCLE * 10 )  ;

        sel   = 3        ;

        #( CYCLE * 10 )  ;

        $stop            ;

    end

    always #(CYCLE / 2) clk = ~clk ;

endmodule

simulation waveform :

Info: ******************************************************************* Info: Running Quartus Prime Analysis & Synthesis Info: Version 18.1.0 Build 625 09/12/2018 SJ Lite Edition Info: Processing started: Thu Jun 19 20:28:49 2025 Info: Command: quartus_map --read_settings_files=on --write_settings_files=off digital_calendar_top -c digital_calendar_top Warning (18236): Number of processors has not been specified which may cause overloading on shared machines. Set the global assignment NUM_PARALLEL_PROCESSORS in your QSF to an appropriate value for best performance. Info (20030): Parallel compilation is enabled and will use 16 of the 16 processors detected Info (12021): Found 1 design units, including 1 entities, in source file /shixun2/timer/timer.v Info (12023): Found entity 1: timer Info (12021): Found 1 design units, including 1 entities, in source file /shixun2/seg7_driver/seg7_driver.v Info (12023): Found entity 1: seg7_driver Info (12021): Found 1 design units, including 1 entities, in source file /shixun2/nyr2009/nyr2009.v Info (12023): Found entity 1: nyr2009 Info (12021): Found 1 design units, including 1 entities, in source file /shixun2/display_mux/display_mux.v Info (12023): Found entity 1: display_mux Info (12021): Found 1 design units, including 1 entities, in source file /shixun2/control/control.v Info (12023): Found entity 1: control Info (12021): Found 1 design units, including 1 entities, in source file /shixun2/clk_div/clk_div.v Info (12023): Found entity 1: clk_div Info (12021): Found 1 design units, including 1 entities, in source file /shixun2/adjust_mux/adjust_mux.v Info (12023): Found entity 1: adjust_mux Info (12021): Found 1 design units, including 1 entities, in source file digital_calendar_top.v Info (12023): Found entity 1: digital_calendar_top Info (12127): Elaborating entity "digital_calendar_top" for the top level hierarchy Info (12128): Elaborating entity "clk_div" for hierarchy "clk_div:u_clk_div" Warning (10230): Verilog HDL assignment warning at clk_div.v(22): truncated value with size 32 to match size of target (26) Warning (10230): Verilog HDL assignment warning at clk_div.v(30): truncated value with size 32 to match size of target (28) Info (12128): Elaborating entity "control" for hierarchy "control:u_control" Info (12128): Elaborating entity "adjust_mux" for hierarchy "adjust_mux:u_adjust_mux" Info (12128): Elaborating entity "timer" for hierarchy "timer:u_timer" Warning (10230): Verilog HDL assignment warning at timer.v(18): truncated value with size 32 to match size of target (6) Warning (10230): Verilog HDL assignment warning at timer.v(19): truncated value with size 32 to match size of target (6) Warning (10230): Verilog HDL assignment warning at timer.v(20): truncated value with size 32 to match size of target (5) Warning (10230): Verilog HDL assignment warning at timer.v(24): truncated value with size 32 to match size of target (6) Warning (10230): Verilog HDL assignment warning at timer.v(27): truncated value with size 32 to match size of target (6) Warning (10230): Verilog HDL assignment warning at timer.v(30): truncated value with size 32 to match size of target (5) Info (12128): Elaborating entity "display_mux" for hierarchy "display_mux:u_display_mux" Warning (10230): Verilog HDL assignment warning at display_mux.v(13): truncated value with size 32 to match size of target (4) Warning (10230): Verilog HDL assignment warning at display_mux.v(14): truncated value with size 32 to match size of target (4) Warning (10230): Verilog HDL assignment warning at display_mux.v(15): truncated value with size 32 to match size of target (4) Warning (10230): Verilog HDL assignment warning at display_mux.v(16): truncated value with size 32 to match size of target (4) Warning (10230): Verilog HDL assignment warning at display_mux.v(17): truncated value with size 32 to match size of target (4) Warning (10230): Verilog HDL assignment warning at display_mux.v(18): truncated value with size 32 to match size of target (4) Warning (10230): Verilog HDL assignment warning at display_mux.v(20): truncated value with size 32 to match size of target (4) Warning (10230): Verilog HDL assignment warning at display_mux.v(21): truncated value with size 32 to match size of target (4) Warning (10230): Verilog HDL assignment warning at display_mux.v(22): truncated value with size 32 to match size of target (4) Warning (10230): Verilog HDL assignment warning at display_mux.v(23): truncated value with size 32 to match size of target (4) Warning (10230): Verilog HDL assignment warning at display_mux.v(24): truncated value with size 32 to match size of target (4) Warning (10230): Verilog HDL assignment warning at display_mux.v(25): truncated value with size 32 to match size of target (4) Info (12128): Elaborating entity "seg7_driver" for hierarchy "seg7_driver:u_seg7_driver" Error (12006): Node instance "u_date" instantiates undefined entity "date". Ensure that required library paths are specified correctly, define the specified entity, or change the instantiation. If this entity represents Intel FPGA or third-party IP, generate the synthesis files for the IP. Error: Quartus Prime Analysis & Synthesis was unsuccessful. 1 error, 21 warnings Error: Peak virtual memory: 4767 megabytes Error: Processing ended: Thu Jun 19 20:28:56 2025 Error: Elapsed time: 00:00:07 Error: Total CPU time (on all processors): 00:00:16 Error (293001): Quartus Prime Full Compilation was unsuccessful. 3 errors, 21 warnings
06-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值