【发布时间】:2021-06-27 14:55:00
【问题描述】:
如何使用 Verilog counter FSM for up-down counter 实现输出为 0,1,2,3,4,5,6,7,6,5,4,4,3,2,1与重复。这四个应该重复两次在这里使用两个状态向上向下它工作正常向上向下。
代码
module top(
input clk,rst,
output logic [2:0] dout
);
typedef enum {up,down} state_type;
state_type state = up;
integer count = 0;
always@(posedge clk)
begin
if(rst == 1'b1) begin
//dout <= 0;
count <= 0;
end
else begin
case(state)
up:
begin
if(count == 7) begin
count <= count - 1;
state <= down;
end
else begin
state <= up;
count <= count - 1;
end
end
down: begin
if(count == 0) begin
count <= count + 1;
state <= up;
end
else begin
state <= down;
count <= count - 1;
end
end
default: begin
state <= up;
count <= 0;
end
endcase
end
end
assign dout = count;
endmodule
【问题讨论】:
-
显然存在计数器 4 和向下步进的特殊状态。您必须尝试实施它。此外,您在计数器递减时计数存在错误。