Verilog快速入门
(1) 四选一多路器
(2)异步复位的串联T触发器
(3)奇偶校验
(4)移位运算与乘法
(5)位拆分与运算
(6)使用子模块实现三输入数的大小比较
(7)4位数值比较器电路
(8)4bit超前进位加法器电路
(9)优先编码器电路①
(10)用优先编码器①实现键盘编码电路
(11)8线-3线优先编码器
(12)使用8线-3线优先编码器实现16线-4线优先编码器
(13)用3-8译码器实现全减器
(14)使用3-8译码器①实现逻辑函数
(15)数据选择器实现逻辑函数
(16)状态机
一、题目描述
某同步时序电路转换表如下,请使用D触发器和必要的逻辑门实现此同步时序电路,用Verilog语言描述。
二、解析与代码
三段式状态机/两段式状态机,推荐阅读三段式状态机理解浅析
`timescale 1ns/1ns
module seq_circuit(
input A ,
input clk ,
input rst_n,
output wire Y
);
reg [1:0] current_state, next_state;
//第一段:描述状态寄存器,为时序逻辑
always@(posedge clk or negedge rst_n)begin
if(!rst_n)
current_state <= 2'b00;
else
current_state <= next_state;
end
//第二段:转移,为组合逻辑
always@(*)begin
case(current_state)
2'b00: begin
if(A) next_state = 2'b11;
else next_state = 2'b01;
end
2'b01: begin
if(A) next_state = 2'b00;
else next_state = 2'b10;
end
2'b10: begin
if(A) next_state = 2'b01;
else next_state = 2'b11;
end
2'b11: begin
if(A) next_state = 2'b10;
else next_state = 2'b00;
end
endcase
end
//第三段:输出,可组合也可时序
// always@(*)begin
// if(current_state == 2'b11) Y = 1;
// else Y=0;
// end
assign Y = (current_state == 2'b11)?1:0;
endmodule