题解 | 移位运算与乘法

移位运算与乘法

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] mul_case = 0;
    reg [7:0] d_reg = 0;


    always @(posedge clk or negedge rst) begin
        if (!rst) begin
            out <= 0;
            d_reg<=d;
            input_grant <= 0;
        end else begin
            case (mul_case)
                2'b00: begin
                    d_reg<=d;
                    input_grant <= 1;
                    out<=d;
                end
                2'b01: begin
                    out <= (d_reg << 1) + d_reg;
                    input_grant <= 0;
                    
                end
                2'b10: begin
                    out <= (d_reg << 3) - d_reg;
                    input_grant <= 0;
                    
                end
                2'b11: begin
                    out <= (d_reg << 3);
                    input_grant <= 0;
                    
                end
            endcase

        end
    end

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

endmodule  //shift_mul

状态机要单独写在另一个always块中,不然会扰乱时序!!!

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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