补充 一般时序逻辑电路及状态机设计VHDL
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
clock: in std_logic; din: in std_logic; dout: out std_logic_vector(2 downto 0) );
end;
architecture mooremachine of moore is type state_type is (s0,s1,s2,s3); signal state: state_type; begin
状态机可以说是一个广义时序电路,触发器、计数器、 移位寄存器等都算是它的特殊功能类型的一种。实际时序 电路中的状态数是有限的,因此又叫做有限状态机。
用VHDL设计状态机不必知道其电路具体实现的细节, 而只需要在逻辑上加以描述。
状态机又分为Moore型Mealy型,前者的输出仅取决于 其所处状态;而后者的输出则不仅与当前所处状态有关, 同时也与当前的输入有关。下面分别介绍这两种状态机在 VHDL中的实现。
end;
architecture mooremachine of moore1 is type state_type is (s0,s1,s2,s3); signal presentstate: state_type; signal nextstate: state_type; begin
change_state:
when s2=>
process(presentstate,din)
begin
-- 相当于图中的
case presentstate is “下一个状态逻
when s0=>
辑”和“输出逻
if din='1' then nextstate<=s3;
else nextstate<=s2;
if din='1' then nextstate<=s1;
qn1: out std_logic);
end;
例3:JK触发器
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
entity jk_ff is
end process; end;
第二种方法:
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
entity tribuffer2 is port (oe: in std_logic;
case state is
when s0=>
when s3=> if din='1' then
state<=s0;
if din='1' then
else
state<=s1; end if; when s1=>
state<=s1; end if;
if din='1' then
end case;
state<=s2; end if;
when s3=>
if din='1' then
nextstate<=s0;
else
nextstate<=s1;
end if;
dout<="111";
end case;
end process;
end;
dout<="011";
例3
library ieee; use ieee.std_logic_1164.all;
datain: in std_logic; dataout: out std_logic ); end;
architecture tristate of tribuffer2 is begin
dataout<=datain when oe='1' else 'z';
end;
状态机在VHDL中的实现
architecture aaa of tff1 is
signal q_n: std_logic;
begin
process(clk)
begin
if rising_edge(clk) then
q_n<=not q_n;
end if;
end process;
q<=q_n;
end;
例2:RS触发设计
end if; end process;
output_process:
--此进程决定 when s2=>
process(state,din) 输出值
if din='0' then
begin
dout <="100";
case state is
else
when s0=>
dout<="101";
if din='0' then
when s2=> if din='1' then state<=s3; end if; when s3=> if din='1' then state<=s0; else state<=s1; end if;
end case; end if; end process;
output_process: --此进程决定输出值 process (state) begin case state is when s0=>dout<="001"; when s1=>dout<="011"; when s2=>dout<="101"; when s3=>dout<="111"; end case; end process;
qb<=not d;
end if;
end process;
end;
三态缓冲器
从理论上说,用VHDL描述三态缓冲器的实 现方法有很多种,但实际上各种综合器对 三态缓冲器的描述都有比较严格的要求, 下面是默认的两种描述方式:
第一种方法:
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
例2
library ieee; use ieee.std_logic_1164.all;
entity moore1 is port (reset: in std_logic;
clock: in std_logic; din: in std_logic; dout: out std_logic_vector(2 downto 0) );
例4:D触发器设计
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
entity d_ff is
port
(d,clk,reset: in std_logic;
一般时序逻辑电路及状 态机的设计
例1:T触发器设计
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
entity tff1 is port (clk: in std_logic; q: out std_logic); end;
port
(j,k,qn,clk: in std_logic;
qn1: out std_logic);
end;
architecture aaa of jk_ff is begin process(j,k,qn,clk) begin if rising_edge(clk) then qn1<=(not qn and j) or (not k and qn); end if; end process; end;
change_state: --此进程用以确定状态的转移 process (reset,clock) begin if reset='1' then state<=s0; elsif rising_edge (clock) then case state is when s0=> if din='1' then state<=s1; end if; when s1=> if din='1' then state<=s2; end if;
begin
change_state:
--此进程用以确定 when s2=>
process(reset,clock) 状态的转移
if din='1' then
begin if reset='1' then state<=s0;
state<=s3; end if;
elsif rising_edge(clock) then
1、Moore状态机的VHDL描述
Moore状态机的输出仅由状态决定,一 个典型的Moore状态机的状态转移图如黑板 所示:
例1 library ieee; use ieee.std_logic_1164.all;
entity moore is port (reset: in std_logic;
entity tribuffer1 is port (oe: in std_logic;
datain: in std_logic; dataout: out std_logic ); end;
architecture tristate of tribuffer1 is begin
process(oe, datain) begin if oe='0' then dataout<='z'; else dataout<=datain; end if;
end if;
dout <="000";
when s9; then
dout<="001";
dout <="110";
end if;
else
when s1=>
dout<="111";
if din='0' then
end if;
dout <="010";
end case;
else
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
entity rs_ff is
port
(r,s,qn: in std_logic;
end;
仿真波形图:
例1是状态机描述格式之一, 因此其仿真波形图中将state以状态名的形式表示出 来,使仿真结果十分直观、易懂。
决定状态转移的进程chang_state 格式是相对固定 的,而决定输出值的进程则还可以用WITH_SELECT语句 来代替。
状态机除了上述描述的格式外,还有另外一种常用 的格式,如例2。该格式是严格按图1所示的时钟同步 状态机机构来写的。
辑” 的结合,也可以
end if; dout<="101";
else nextstate<=s0;
end if; dout<="001";
when s1=> if din='1' then nextstate<=s2; else nextstate<=s1; end if;
将输出逻辑象例1
一样独立出来
end process;
dout<="011";
end if;
end;
仿真波形图:
一般有两种处理方法:
1、单独设计一个状态(如Error),用以处理状态机 出错时的情况,然后用 WHEN OTHERS=>State<=Error; 使状态机从未定义状态中跳转到处理出错情况的状态。
q,qb: out std_logic);
end;
architecture aaa of d_ff is
begin
process(clk,reset)
begin
if (reset='1') then
q<='0';
qb<='1';
elsif rising_edge(clk) then
q<=d;
entity mealymachine is port (reset: in std_logic;
clock: in std_logic; din: in std_logic;
dout: out std_logic_vector(2 downto 0) ); end;
architecture statemachine of mealymachine is type state_type is (s0,s1,s2,s3); signal state: state_type;
end;
architecture mooremachine of moore is type state_type is (s0,s1,s2,s3); signal state: state_type; begin
状态机可以说是一个广义时序电路,触发器、计数器、 移位寄存器等都算是它的特殊功能类型的一种。实际时序 电路中的状态数是有限的,因此又叫做有限状态机。
用VHDL设计状态机不必知道其电路具体实现的细节, 而只需要在逻辑上加以描述。
状态机又分为Moore型Mealy型,前者的输出仅取决于 其所处状态;而后者的输出则不仅与当前所处状态有关, 同时也与当前的输入有关。下面分别介绍这两种状态机在 VHDL中的实现。
end;
architecture mooremachine of moore1 is type state_type is (s0,s1,s2,s3); signal presentstate: state_type; signal nextstate: state_type; begin
change_state:
when s2=>
process(presentstate,din)
begin
-- 相当于图中的
case presentstate is “下一个状态逻
when s0=>
辑”和“输出逻
if din='1' then nextstate<=s3;
else nextstate<=s2;
if din='1' then nextstate<=s1;
qn1: out std_logic);
end;
例3:JK触发器
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
entity jk_ff is
end process; end;
第二种方法:
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
entity tribuffer2 is port (oe: in std_logic;
case state is
when s0=>
when s3=> if din='1' then
state<=s0;
if din='1' then
else
state<=s1; end if; when s1=>
state<=s1; end if;
if din='1' then
end case;
state<=s2; end if;
when s3=>
if din='1' then
nextstate<=s0;
else
nextstate<=s1;
end if;
dout<="111";
end case;
end process;
end;
dout<="011";
例3
library ieee; use ieee.std_logic_1164.all;
datain: in std_logic; dataout: out std_logic ); end;
architecture tristate of tribuffer2 is begin
dataout<=datain when oe='1' else 'z';
end;
状态机在VHDL中的实现
architecture aaa of tff1 is
signal q_n: std_logic;
begin
process(clk)
begin
if rising_edge(clk) then
q_n<=not q_n;
end if;
end process;
q<=q_n;
end;
例2:RS触发设计
end if; end process;
output_process:
--此进程决定 when s2=>
process(state,din) 输出值
if din='0' then
begin
dout <="100";
case state is
else
when s0=>
dout<="101";
if din='0' then
when s2=> if din='1' then state<=s3; end if; when s3=> if din='1' then state<=s0; else state<=s1; end if;
end case; end if; end process;
output_process: --此进程决定输出值 process (state) begin case state is when s0=>dout<="001"; when s1=>dout<="011"; when s2=>dout<="101"; when s3=>dout<="111"; end case; end process;
qb<=not d;
end if;
end process;
end;
三态缓冲器
从理论上说,用VHDL描述三态缓冲器的实 现方法有很多种,但实际上各种综合器对 三态缓冲器的描述都有比较严格的要求, 下面是默认的两种描述方式:
第一种方法:
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
例2
library ieee; use ieee.std_logic_1164.all;
entity moore1 is port (reset: in std_logic;
clock: in std_logic; din: in std_logic; dout: out std_logic_vector(2 downto 0) );
例4:D触发器设计
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
entity d_ff is
port
(d,clk,reset: in std_logic;
一般时序逻辑电路及状 态机的设计
例1:T触发器设计
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
entity tff1 is port (clk: in std_logic; q: out std_logic); end;
port
(j,k,qn,clk: in std_logic;
qn1: out std_logic);
end;
architecture aaa of jk_ff is begin process(j,k,qn,clk) begin if rising_edge(clk) then qn1<=(not qn and j) or (not k and qn); end if; end process; end;
change_state: --此进程用以确定状态的转移 process (reset,clock) begin if reset='1' then state<=s0; elsif rising_edge (clock) then case state is when s0=> if din='1' then state<=s1; end if; when s1=> if din='1' then state<=s2; end if;
begin
change_state:
--此进程用以确定 when s2=>
process(reset,clock) 状态的转移
if din='1' then
begin if reset='1' then state<=s0;
state<=s3; end if;
elsif rising_edge(clock) then
1、Moore状态机的VHDL描述
Moore状态机的输出仅由状态决定,一 个典型的Moore状态机的状态转移图如黑板 所示:
例1 library ieee; use ieee.std_logic_1164.all;
entity moore is port (reset: in std_logic;
entity tribuffer1 is port (oe: in std_logic;
datain: in std_logic; dataout: out std_logic ); end;
architecture tristate of tribuffer1 is begin
process(oe, datain) begin if oe='0' then dataout<='z'; else dataout<=datain; end if;
end if;
dout <="000";
when s9; then
dout<="001";
dout <="110";
end if;
else
when s1=>
dout<="111";
if din='0' then
end if;
dout <="010";
end case;
else
library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
entity rs_ff is
port
(r,s,qn: in std_logic;
end;
仿真波形图:
例1是状态机描述格式之一, 因此其仿真波形图中将state以状态名的形式表示出 来,使仿真结果十分直观、易懂。
决定状态转移的进程chang_state 格式是相对固定 的,而决定输出值的进程则还可以用WITH_SELECT语句 来代替。
状态机除了上述描述的格式外,还有另外一种常用 的格式,如例2。该格式是严格按图1所示的时钟同步 状态机机构来写的。
辑” 的结合,也可以
end if; dout<="101";
else nextstate<=s0;
end if; dout<="001";
when s1=> if din='1' then nextstate<=s2; else nextstate<=s1; end if;
将输出逻辑象例1
一样独立出来
end process;
dout<="011";
end if;
end;
仿真波形图:
一般有两种处理方法:
1、单独设计一个状态(如Error),用以处理状态机 出错时的情况,然后用 WHEN OTHERS=>State<=Error; 使状态机从未定义状态中跳转到处理出错情况的状态。
q,qb: out std_logic);
end;
architecture aaa of d_ff is
begin
process(clk,reset)
begin
if (reset='1') then
q<='0';
qb<='1';
elsif rising_edge(clk) then
q<=d;
entity mealymachine is port (reset: in std_logic;
clock: in std_logic; din: in std_logic;
dout: out std_logic_vector(2 downto 0) ); end;
architecture statemachine of mealymachine is type state_type is (s0,s1,s2,s3); signal state: state_type;