解题思路

先写一个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