题目
分析
需要给出一个计数器的状态机,注意d输入不是随时有效的,只有在cnt计数为0的那个时钟沿,d输入有效,因此需要设计一个寄存器din,在cnt为0时候锁存d的值。
`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] din;
always@(posedge clk or negedge rst) begin
if(~rst) begin
cnt <= 2'b0;
din <= 8'b0;
input_grant <= 0;
out <= 11'b0;
end
else begin
cnt <= cnt + 1;
case(cnt)
2'b00: begin
din <= d;
input_grant <= 1;
out <= d;
end
2'b01: begin
input_grant <= 0;
out <= (din<<2) - din;
end
2'b10: begin
input_grant <= 0;
out <= (din<<3) - din;
end
2'b11: begin
input_grant <= 0;
out <= din<<3;
end
endcase
end
end
//*************code***********//
endmodule