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

移位运算与乘法

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 [7:0] d_ff1;
  reg [7:0] d_ff2;
  reg [7:0] d_ff3;
  always@(posedge clk or negedge rst)
  begin
    if(!rst)
    begin
      input_grant <= 1'b0;
      out <= 11'b0;
    end
    else
    begin
      case(count)
        2'b00:
        begin
          input_grant <= 1'b1;
          out <= {3'b0,d};
        end
        2'b01:
        begin
          input_grant <= 1'b0;
          out <= {2'b0,d_ff1,1'b0} + {3'b0,d_ff1};
        end
        2'b10:
        begin
          input_grant <= 1'b0;
          out <= {2'b0,d_ff2,1'b0} + {3'b0,d_ff2} + {1'b0,d_ff2,2'b0};
        end
        2'b11:
        begin
          input_grant <= 1'b0;
          out <= {d_ff3,3'b0};
        end
        default:
        begin
          input_grant <= 1'b0;
          out <= 11'b0;
        end
      endcase
    end
  end

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

  always@(posedge clk or negedge rst)
  begin
    if(!rst)
    begin
      d_ff1 <= 8'b0;
      d_ff2 <= 8'b0;
      d_ff3 <= 8'b0;
    end
    else
    begin
      d_ff1 <= d;
      d_ff2 <= d_ff1;
      d_ff3 <= d_ff2;
    end
  end
  //*************code***********//
endmodule

1、不需要用状态机。

2、设置一个计数器来控制乘1、3、7、8。

3,乘法用移位来做。

全部评论

相关推荐

这不纯纯作弊了吗😢😢😢
编程界菜鸡:信这个的这辈子有了,这智商你靠啥都没用
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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