• FPGA硬件工程师Verilog面试题(基础篇二)


    ✅作者简介:大家好我是:嵌入式基地,是一名嵌入式工程师,希望一起努力,一起进步!
    📃个人主页:嵌入式基地
    🔥系列专栏:FPGA Verilog 习题专栏
    微信公众号嵌入式基地

    习题一:多功能数据处理器

    • 点击进行在线练习
      描述

    • 根据指示信号select的不同,对输入信号a,b实现不同的运算。输入信号a,b为8bit有符号数,当select信号为0,输出a;当select信号为1,输出b;当select信号为2,输出a+b;当select信号为3,输出a-b.

    • 信号示意图:

    • 使用Verilog HDL实现以上功能并编写testbench验证。

    输入描述

    • clk:系统时钟
    • rst_n:复位信号,低电平有效
    • a,b:8bit位宽的有符号数
    • select:2bit位宽的无符号数

    输出描述

    • c:9bit位宽的有符号数

    代码实现

    `timescale 1ns/1ns
    module data_select(
    	input clk,
    	input rst_n,
    	input signed[7:0]a,
    	input signed[7:0]b,
    	input [1:0]select,
    	output reg signed [8:0]c
    );
    	  
    always @(posedge clk or negedge rst_n)
    	if(!rst_n)
    		c <= 9'd0;
    	else case(select)
    	2'b00:	c <= a;
    	2'b01:	c <= b;
    	2'b10:	c <= a+b;
    	2'b11:	c <= a-b;
    	default: c <= 9'd0;
    	endcase
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    习题二:求两个数的差值

    • 点击进行在线练习
      描述

    • 根据输入信号a,b的大小关系,求解两个数的差值:输入信号a,b为8bit位宽的无符号数。如果a>b,则输出a-b,如果a≤b,则输出b-a。

    • 接口信号示意图:

    输入描述

    • clk:系统时钟
    • rst_n:复位信号,低电平有效
    • a,b:8bit位宽的无符号数

    输出描述

    • c:8bit位宽的无符号数

    代码实现

    `timescale 1ns/1ns
    module data_minus(
    	input clk,
    	input rst_n,
    	input [7:0]a,
    	input [7:0]b,
    
    	output  reg [8:0]c
    );
        
    always @ (posedge clk&nbs***bsp;negedge rst_n)
    begin
        if( ~rst_n ) begin
            c <= 8'b0;
        end 
        else begin
            if( a > b ) begin
                c <= a - b;
            end 
            else begin
                c <= b - a;
            end 
        end 
    end    
        
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    习题三:使用generate…for语句简化代码

    • 点击进行在线练习
      描述
    • 在某个module中包含了很多相似的连续赋值语句,请使用generata…for语句编写代码,替代该语句,要求不能改变原module的功能。
    • 使用Verilog HDL实现以上功能并编写testbench验证。

    module template_module(
    input [7:0] data_in,
    output [7:0] data_out );
    assign data_out [0] = data_in [7];
    assign data_out [1] = data_in [6];
    assign data_out [2] = data_in [5];
    assign data_out [3] = data_in [4];
    assign data_out [4] = data_in [3];
    assign data_out [5] = data_in [2];
    assign data_out [6] = data_in [1];
    assign data_out [7] = data_in [0];
    endmodule

    输入描述

    • data_in:8bit位宽的无符号数

    输出描述

    • data_out:8bit位宽的无符号数

    代码实现

    `timescale 1ns/1ns
    module gen_for_module( 
        input [7:0] data_in,
        output [7:0] data_out
    );
        genvar i;
        generate
            for(i = 0; i < 8; i = i + 1) 
    		begin : bit_reverse
                assign data_out[i] = data_in[7 - i];
            end
            
        endgenerate
     
    endmodule
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    习题四:使用子模块实现三输入数的大小比较

    • 点击进行在线练习
      描述

    • 在数字芯片设计中,通常把完成特定功能且相对独立的代码编写成子模块,在需要的时候再在主模块中例化使用,以提高代码的可复用性和设计的层次性,方便后续的修改。

    • 请编写一个子模块,将输入两个8bit位宽的变量data_a,data_b,并输出data_a,data_b之中较小的数。并在主模块中例化,实现输出三个8bit输入信号的最小值的功能。

    • 子模块的信号接口图如下:

    • 主模块的信号接口图如下:

    • 使用Verilog HDL实现以上功能并编写testbench验证。

    输入描述

    • clk:系统时钟
    • rst_n:异步复位信号,低电平有效
    • a,b,c:8bit位宽的无符号数

    输出描述

    • d:8bit位宽的无符号数,表示a,b,c中的最小值

    代码实现

    timescale 1ns/1ns
    module main_mod(
    	input clk,
    	input rst_n,
    	input [7:0]a,
    	input [7:0]b,
    	input [7:0]c,
    	
    	output [7:0]d
    );
    
        wire [7:0] min_ab;
        wire [7:0] min_ac;
        wire [7:0] min_abc;
        
        sub_mod sub_mod_U0(
            .clk      (clk),
            .rst_n    (rst_n),
            .a        (a),
            .b        (b),
            
            .c        (min_ab)
        );
        
        sub_mod sub_mod_U1(
            .clk      (clk),
            .rst_n    (rst_n),
            .a        (a),
            .b        (c),
            
            .c        (min_ac)
        );
    
        sub_mod sub_mod_U2(
            .clk      (clk),
            .rst_n    (rst_n),
            .a        (min_ab),
            .b        (min_ac),
            
            .c        (min_abc)
        );
    
        assign d = min_abc;
        
    endmodule
    
    module sub_mod(
        input clk,
        input rst_n,
        input [7:0] a,
        input [7:0] b,
        
        output reg [7:0] c
    );
        
        always@(posedge clk or negedge rst_n) begin: compare_2_inputs
            if(~rst_n)
                c <= 8'b0;
            else if(a > b)
                c <= b;
            else
                c <= a;
        end
        
    endmodule
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    习题五:使用函数实现数据大小端转换

    描述

    • 在数字芯片设计中,经常把实现特定功能的模块编写成函数,在需要的时候再在主模块中调用,以提高代码的复用性和提高设计的层次,分别后续的修改。

    • 请用函数实现一个4bit数据大小端转换的功能。实现对两个不同的输入分别转换并输出。

    • 程序的接口信号图如下:

    输入描述

    • clk:系统时钟
    • rst_n:异步复位信号,低电平有效
    • a,b:4bit位宽的无符号数

    输出描述

    • c,d:8bit位宽的无符号数

    代码实现

    ````timescale 1ns/1ns
    module function_mod(
        input clk,
        input rst_n,
        
    	input [3:0]a,
    	input [3:0]b,
    	
    	output [3:0]c,
    	output [3:0]d
    );
        assign c = rst_n?revrs(a):0;
        assign d = rst_n?revrs(b):0;
     
        function [3:0] revrs;
            input [3:0] datain;
            integer i;
                for (i=0;i<4;i=i+1)
                    begin :reverse
                        revrs[i] = datain[3-i];
                    end
        endfunction
    
    endmodule
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    VBA技术资料MF70:从单元格文本中取消或删除上标
    浅析量化交易系统交易者的核心理念
    英语——分享篇——每日100词——801-900
    TCP/IP协议专栏——以太帧结构 详解——网络入门和工程维护必看
    五大资源之Service(可以固定IP)
    记录一次难搞的编译错误-- qml-rust 项目编译无法找到QtCore库的问题
    Git 代码提交注释管理规范
    Hadoop-28 ZooKeeper集群 ZNode简介概念和测试 数据结构与监听机制 持久性节点 持久顺序节点 事务ID Watcher机制
    计算机架构史上的一次伟大失败,多数人都不知道
    Taro3的编译配置
  • 原文地址:https://blog.csdn.net/m0_51061483/article/details/127470789