题解 | 移位运算与乘法
移位运算与乘法
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 [2:0] cnt;
reg [10:0] d_latch;
always @(posedge clk or negedge rst) begin
if (!rst) begin
d_latch <= 0;
input_grant <= 1'b0;
out <= 0;
end
else if (cnt == 0) begin
d_latch <= d;
out <= d;
input_grant <= 1'b1;
end
else if (cnt == 1) begin
out <= d_latch * 3;
input_grant <= 1'b0;
end
else if (cnt == 2) begin
out <= d_latch * 7;
input_grant <= 1'b0;
end
else if (cnt == 3) begin
out <= d_latch * 8;
input_grant <= 1'b0;
end
end
always @(posedge clk or negedge rst) begin
if (!rst) begin
cnt <= 0;
end
else if (cnt >= 3) begin
cnt <= 0;
end else begin
cnt <= cnt + 1;
end
end
//*************code***********//
endmodule
题目针对输入分别进行*1、*3、*7、*8的村换操作。
因此通过引入一个cnt,指示这四个操作数的循环。同时在*1时进行锁存。
