题解 | #移位运算与乘法#
移位运算与乘法
https://www.nowcoder.com/practice/1dd22852bcac42ce8f781737f84a3272
移位运算,实现乘法与除法;
输入data_in,
若要实现2*data_in,则可以写成{data_in,1'b0};
若要实现4*data_in,则可以写成{data_in,2'b00};
若要实现3*data_in,则可以写成data_in + {data_in,1'b0};
`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] cnt; reg [7:0] d_reg; always@(posedge clk or negedge rst)begin if(!rst)begin cnt <= 2'd0; end else begin cnt <= cnt + 1'b1; end end always@(posedge clk or negedge rst)begin if(!rst)begin out <= 11'd0; input_grant <= 1'b0; end else begin case(cnt) 2'd0:begin out <= d; d_reg <= d; input_grant <= 1'b1; end 2'd1:begin out <= d_reg + {d_reg,1'b0}; input_grant <= 1'b0; end 2'd2:begin out <= d_reg + {d_reg,1'b0} + {d_reg,2'b00}; input_grant <= 1'b0; end 2'd3:begin out <= {d_reg,3'b00}; input_grant <= 1'b0; end default: input_grant <= 1'b0; endcase end end //*************code***********// endmodule#刷题#