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

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

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

用generate简化代码

`timescale 1ns/1ns


module mul(
    input [3:0] a,
    input [3:0] b,
    output [7:0] c
);
    wire [7:0] tmp [3:0];
    genvar i;
    generate for (i=0; i<4; i=i+1) begin
        assign tmp[i] = a[i] ? b << i : 'd0;
    end
    endgenerate
    
    assign c = tmp[0] + tmp[1] + tmp[2] + tmp[3];
    
endmodule



module calculation(
	input clk,
	input rst_n,
	input [3:0] a,
	input [3:0] b,
	output [8:0] c
	);
    
    wire [7:0] tmp0;
    wire [7:0] tmp1;
    reg  [8:0] c_r;
    
    mul mul_0 (a, 12, tmp0);
    mul mul_1 (b, 5 , tmp1);
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            c_r <= 'd0;
        end
        else begin
            c_r <= tmp0 + tmp1;
        end
    end
    assign c = c_r;
endmodule
全部评论
完蛋了 完全看不懂了
点赞 回复 分享
发布于 2022-09-07 16:54 四川

相关推荐

10-17 23:18
已编辑
西北农林科技大学 Web前端
独行m:给25可以试试,但他只能给12,那就是纯纯的事精
秋招,不懂就问
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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