题解 | #编写乘法器求解算法表达式#

编写乘法器求解算法表达式

http://www.nowcoder.com/practice/c414335a34b842aeb9960acfe5fc879f

解题思路

先写一个4*4的子模块,然后实例化两次,再相加。其中子模块需要用移位相加的方式实现。

其实感觉这个题完全用组合逻辑就可以实现,无需时序。

代码实现

`timescale 1ns/1ns

module calculation(
	input clk,
	input rst_n,
	input [3:0] a,
	input [3:0] b,
	output reg [8:0] c
	);
    
    wire [7:0] temp12, temp5;
    
    calc_mult_4 calc_mult_4_inst1(a, 4'd12, temp12);
    calc_mult_4 calc_mult_4_inst2(b, 4'd5, temp5);
    
    always@(posedge clk or negedge rst_n)
        if(!rst_n)
            c <= 9'd0;
        else
            c <= temp12 + temp5;
    
endmodule


module calc_mult_4(
    input  wire [3:0] a      ,
    input  wire [3:0] b      ,
    output wire [7:0] result 
);
    reg [7:0] temp_a; 
    reg [3:0] temp_b;
    reg [7:0] temp;
    
    always@(*) begin
        temp_a = a;
        temp_b = b;
    end
    
    always@(*) begin
        temp = 8'b0;
        repeat(3'd4) begin
            if(temp_b[0])
                temp = temp + temp_a;
            temp_a = temp_a << 1'b1;
            temp_b = temp_b >> 1'b1;
        end
    end       
    
   assign result = temp;
    
endmodule
全部评论
这个乘法器模块在vivado上验证是无法综合的,实际工程里应该不会用这种纯组合逻辑来写,无法综合应该是跟这个模块里有loop和latch有关
点赞 回复 分享
发布于 2023-02-13 08:28 安徽

相关推荐

评论
4
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务