题解 | 移位运算与乘法

移位运算与乘法

https://www.nowcoder.com/practice/1dd22852bcac42ce8f781737f84a3272

`timescale 1ns/1ns
module multi_sel(
    input [7:0] d,
    input clk,
    input rst,
    output reg input_grant,//该数乘以一时输出为高,乘以其他倍数时为低
    output reg [10:0] out
);

reg [1:0] cnt;//乘以1/3/7/8四个倍数,两位足够
reg [7:0] stored_d;//用以存储该数乘以一时的值

always@(posedge clk or negedge rst)begin
    if(!rst)begin
        cnt <= 2'b00;
    end
    else begin
        cnt <= cnt + 1;
    end
end

always@(posedge clk or negedge rst)begin
    if(!rst)begin
        input_grant <= 1'b0;
        out <= 11'b0;
        stored_d <= 8'b0;
    end
    else begin
        case(cnt)
            2'b00:begin
                stored_d <= d;
                out <= {3'b0, d};//11-8=3,前补三位
                input_grant <= 1'b1;
            end
            2'b01:begin
                out <= (stored_d << 2) - stored_d;
				// a << 2 - a和 a*3等价,但前者比后者减少硬件实现消耗资源
                input_grant <= 1'b0;
            end
            2'b10:begin
                out <= (stored_d << 3) - stored_d;
                input_grant <= 1'b0;
            end
            2'b11:begin
                out <= stored_d << 3;  
                input_grant <= 1'b0;
            end
            default:begin
                out <= 11'b0;
                input_grant <= 1'b0;
            end
        endcase
    end
end

endmodule

verilog刷题记录 文章被收录于专栏

记录自己最近刷题掌握的点滴

全部评论

相关推荐

牛客79969855...:简历上面只有实习的话,面试官应该会深挖你的实习,你能应对自如就行,但是被拆穿了会很尴尬,然后就找下一个吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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