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

移位运算与乘法

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] count;
reg [10:0] d_mid;
reg [10:0] out_mid1;
reg [10:0] out_mid2;
always @(posedge clk or negedge rst)begin
    if(!rst)begin
        count <= 2'b00;
    end
    else begin
        count <= count + 1;
    end
end

always @(posedge clk or negedge rst)begin
    if(!rst)begin
        input_grant <= 1'b0;
    end
    else begin
        if(count == 2'b00)begin
           input_grant <= 1'b1; 
        end
        else begin
           input_grant <= 1'b0;
        end
    end
end

always @(posedge clk or negedge rst)begin
    if(!rst)begin
        out <= 11'b00000000000;
    end
    else begin
        case(count)
        2'b00:begin
            out <= d;
            d_mid <= d;
            out_mid1 <= d<<1;
            out_mid2 <= d<<2;
        end
        2'b01:begin
            out <= out_mid1 + d_mid;
        end
        2'b10:begin
            out <= out_mid1 + out_mid2 + d_mid;
        end
        2'b11:begin
            out <= d_mid<<3;
        end
        endcase
    end
end

//*************code***********//
endmodule

全部评论

相关推荐

昨天 14:44
济南大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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