EDA2016 秋季计数器仿真实验作业
目录
计数器作业内容
手工绘制的电路结构RTL设计图
Quartus扫描生成的电路RTL图
计数器仿真截图
![该计数器在电路复位后会循环的从0值递增计数到最大值,计数最大值是一个循环变化的过程,计数器复位之后,第一次计数最大值是6,然后是7、8、9,然后计数最大值又变成6]
代码块
计数器代码如下:
module counter(
CLK ,
RST ,
CNT ,
OV );
input CLK;
input RST;
output CNT;
output OV;
parameter ST_0 = 0;
parameter ST_1 = 1;
parameter ST_2 = 2;
parameter ST_3 = 3;
reg[3:0] CNT;
reg[1:0] stateR,next_state;
reg[3:0] cnt_temp;
reg OV;
// calc next state
always @ (posedge CLK) begin
case (stateR)
ST_0 :begin if(cnt_temp>=6) begin cnt_temp=0;next_state=ST_1;end else begin cnt_temp=cnt_temp+1;end end
ST_1 :begin if(cnt_temp>=7) begin cnt_temp=0;next_state=ST_2;end else begin cnt_temp=cnt_temp+1;end end
ST_2 :begin if(cnt_temp>=8) begin cnt_temp=0;next_state=ST_3;end else begin cnt_temp=cnt_temp+1;end end
ST_3 :begin if(cnt_temp>=9) begin cnt_temp=0;next_state=ST_0;end else begin cnt_temp=cnt_temp+1;end end
default:begin next_state=ST_0;cnt_temp=0;end
endcase
end
// calc output
always @ (stateR) begin
if((stateR==ST_0&&cnt_temp==6)||(stateR==ST_1&&cnt_temp==7)||(stateR==ST_2&&cnt_temp==8)||(stateR==ST_3&&cnt_temp==9))
OV = 1'b1;
else
OV = 1'b0;
end
// state DFF
always @ (next_state or RST)begin
if(RST) begin
stateR <= ST_0;
CNT=0;end
else begin
stateR <= next_state;
CNT=cnt_temp;end
end
endmodule