题目没什么意思,给的参考波形也有问题,不过可以拓展一点东西。

题目

编写一个4bit乘法器模块,并例化该乘法器求解c=12*a+5*b,其中输入信号a,b为4bit无符号数,c为输出。注意请不要直接使用*符号实现乘法功能。

module calculation(
    input clk,
    input rst_n,
    input [3:0] a,
    input [3:0] b,
    output [8:0] c
    );
endmodule

答案解析

无非就是用移位加法实现常值乘法呀,题目没讲清楚哪拍出:

module calculation(
    input clk,
    input rst_n,
    input [3:0] a,
    input [3:0] b,
    output [8:0] c
    );

    reg [8:0]a_mul_12;
    reg [8:0]b_mul_5;

    always @(posedge clk or negedge rst_n)begin
        if(~rst_n) a_mul_12 <= 0;
        else       a_mul_12 <= (a<<3) + (a<<2);
    end
    always @(posedge clk or negedge rst_n)begin
        if(~rst_n) b_mul_5 <= 0;
        else       b_mul_5 <= (b<<2) + b;
    end
    assign c = a_mul_12 + b_mul_5;
endmodule

用auto_verifition仿一下:

 好了这个题没啥,注意一点,+优先级高于<<,因此别写成a<<3 + a<<2就行,否则逻辑出错。

延伸一下

如果我是面试官呢,我会继续延伸一步,不使用*写一下c=255*a的逻辑,当然了,这样写肯定是没有问题的:

c = (a << 7) + (a << 6) + (a << 5) + (a << 4) + (a << 3) + (a << 2) + (a << 1) + a

但是肯定有更好的逻辑优化解:

c = (a << 8) - a

要知道,减法和加法的逻辑开销是相差很小的。