题解 | #移位运算与乘法#

移位运算与乘法

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
);
//*************code***********//
    reg [1:0] out_num;
    reg [7:0] din;
    
    always @(posedge clk or negedge rst) begin
        if(~rst)
            out_num <= 'd0;
        else if(out_num == 'd3)
            out_num <= 'd0;
        else
            out_num <= out_num + 'd1;
    end
    
    always @(posedge clk or negedge rst) begin
        if(~rst)
            input_grant <= 1'b0;
        else if(out_num == 'd0)
            input_grant <= 1'b1;
        else 
            input_grant <= 1'b0;
    end

    always @(posedge clk or negedge rst)  begin
        if(~rst)
            din <= 'd0;
        else if(out_num == 'd0)
            din <= d;
    end
    
    always @(posedge clk or negedge rst) begin
        if(~rst)
            out <= 'd0;
        else begin
            case(out_num)
                'd0: out <= d;
                'd1: out <= (din << 1) + din;
                'd2: out <= (din << 3) - din;
                'd3: out <= (din << 3);
                default: out <= 'd0;
            endcase
        end
    end
    
//*************code***********//
endmodule


#不去互联网还可以去硬件行业#
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务