38
2006/11/28 ©2006, Masaharu Imai 1 7章 ステートマシンの設計 大阪大学 大学院 情報科学研究科 今井 正治 E-mail: [email protected] http://www-ise1.ist.osaka-u.ac.jp/~imai/

7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 1

第7章 ステートマシンの設計

大阪大学 大学院 情報科学研究科今井 正治

E-mail: [email protected]://www-ise1.ist.osaka-u.ac.jp/~imai/

Page 2: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 2

講義内容

ステートマシンの定義Mealy型ステートマシン

Moore型ステートマシン

Mealy型からMoore型への変換

VHDLを用いたステートマシンの記述

データパス付きステートマシン

階層化ステートマシン

Page 3: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 3

Mealy型ステートマシン

出力関数 :時刻 における出力

状態遷移関数 :時刻 における状態

( ))()()( , kkk XQZ ω=

( ))()()1( , kkk XQQ δ=+

ω

δ

kt

1+kt

Page 4: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 4

順序回路のモデル(Mealy型)

出力関数(組合せ回路)

状態遷移関数(組合せ回路)

内部状態(記憶素子)

クロック

入力

出力

)(kX

)(kQ

( ))()()( , kkk XQZ ω=

( ))()()1( , kkk XQQ δ=+

Page 5: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 5

Mealy型2進カウンタ

aQ bQ

0/0 0/1 0/0

1/1

0 1 0 1

0 0

0 1

QX

δ ω

aQ

bQ

aQ

aQbQ

bQ

Page 6: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 6

Moore型ステートマシン

出力関数 :時刻 における出力(状態のみに依存する)

状態遷移関数 :時刻 における状態

( ))()( kk QZ ω=

( ))()()1( , kkk XQQ δ=+

ω

δ

kt

1+kt

Page 7: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 7

順序回路のモデル(Moore型)

出力関数(組合せ回路)

状態遷移関数(組合せ回路)

内部状態(記憶素子)

クロック

入力

出力

)(kX

)(kQ

( ))()( kk QZ ω=

( ))()()1( , kkk XQQ δ=+

Page 8: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 8

Moore型2進カウンタ

0 1 -

0

1

0

'δ 'ω

QX

)0,(aQ

)0,(bQ

)0,(bQ)0,(aQ

)0,(bQ

)1,(aQ )0,(aQ )0,(bQ

)1,(aQ

0/)0,(aQ

0

10

0/)0,(bQ

1/)1,(aQ1

1

0

Page 9: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 9

Mealy型とMoore型の比較

Mealy型出力は状態と入力の両方に依存

Moore型よりも少ない状

態数で実現可能

出力はすぐに現れる

出力にハザードが生じやすい

Moore型出力が状態だけで決る

Mealy型よりも状態数が多くなる

出力は次のクロックで変化する

出力にハザードが生じにくい

ZXQ →×:ω ZQ→:ω

Page 10: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 10

講義内容

ステートマシンの定義Mealy型ステートマシン

Moore型ステートマシン

Mealy型からMoore型への変換

VHDLを用いたステートマシンの記述

データパス付きステートマシン

階層化ステートマシン

Page 11: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 11

Mealy型からMoore型への変換 (1)

任意のMealy型の順序回路S に対して,これと等価なMoore型の順序回路S’を構成でき

ZQQXQ

ZQQZQXS

ZQXS

→→×

×⊆

=

=

'':''':

'',',,','

,,,,

ωδ

ωδ

ωδ

Page 12: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 12

Mealy型からMoore型への変換 (2)

順序回路Sの状態推移関数ωに対して,

ならば,

( )( ) qji

pji

ZXQ

QXQ

=

=

,

,

δ

ω

( )( ) qZp

ZqpjZi

ZQ

QXQ

q=

=

),(

),(),(

'

,'

δ

ω

iQ pQqj ZX /

qZp ZQq

/),(

),( 1ZiQ

),( 2ZiQ

),( nZiQ

jX

jX

jXM

Page 13: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 13

Mealy型2進カウンタ

aQ bQ

0/0 0/1 0/0

1/1

0 1 0 1

0 0

0 1

QX

δ ω

aQ

bQ

aQ

aQbQ

bQ

Page 14: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 14

Moore型順序回路への変換

Mealy型 Moore型

0 1 0 1

0 0

0 1

QX

δ ω

aQ

bQ

0 1 -

0

1

0

'δ 'ω

QX

)0,(aQ

)0,(bQ

)0,(bQaQ

aQbQ

bQ )0,(aQ

)0,(bQ

)1,(aQ )0,(aQ )0,(bQ

)1,(aQ

Page 15: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 15

Moore型2進カウンタ

0 1 -

0

1

0

'δ 'ω

QX

)0,(aQ

)0,(bQ

)0,(bQ)0,(aQ

)0,(bQ

)1,(aQ )0,(aQ )0,(bQ

)1,(aQ

0/)0,(aQ

0

10

0/)0,(bQ

1/)1,(aQ1

1

0

Page 16: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 16

講義内容

ステートマシンの定義Mealy型ステートマシン

Moore型ステートマシン

Mealy型からMoore型への変換

VHDLを用いたステートマシンの記述

データパス付きステートマシン

階層化ステートマシン

Page 17: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 17

Mealy型ステートマシンの記述例(1)library ieee;use ieee.std_logic_1164.all;

entity mealy isport(

clock: in std_logic;reset: in std_logic;d_in: in std_logic;d_out: out std_logic );

end entity mealy;

aQ bQ

0/0 0/1 0/0

1/1

Page 18: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 18

Mealy型ステートマシンの記述例(2)architecture behavior of mealy isbegin

P1: process( clock, reset )type state_t is ( Qa, Qb );variable cur_state, next_state: state_t;

beginif reset = '1' then

next_state := Qa;elsif rising_edge( clock ) then

cur_state := next_state;

aQ bQ

0/0 0/1 0/0

1/1

リセット動作

Page 19: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 19

Mealy型ステートマシンの記述例(3)

case cur_state iswhen Qa =>

if d_in = '1' thend_out <= '0';next_state := Qb;

elsed_out <= '0';next_state := Qa;

end if;aQ bQ

0/0 0/1 0/0

1/1

出力の指定

次状態の指定

入力値による判定

Page 20: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 20

Mealy型ステートマシンの記述例(4)

when Qb =>if d_in = '1' then

d_out <= '1';next_state := Qa;

elsed_out <= '0';next_state := Qb;

end if;end case;

end if;end process P1;

end architecture behavior;

aQ bQ

0/0 0/1 0/0

1/1

Page 21: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 21

Moore型ステートマシンの記述例(1)

library ieee;use ieee.std_logic_1164.all;

entity moore isport(

clock: in std_logic;reset: in std_logic;d_in: in std_logic;d_out: out std_logic );

end entity moore;

0/)0,(aQ

0

10

0/)0,(bQ

1/)1,(aQ1

1

0

Page 22: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 22

Moore型ステートマシンの記述例(2)

architecture behavior of moore isbegin

P1: process( clock, reset )type state_t is ( Qa, Qb, Qc );variable cur_state, next_state: state_t;

beginif reset = '1' then

next_state := Qa;elsif rising_edge( clock ) then

cur_state := next_state;

0/)0,(aQ

0

10

0/)0,(bQ

1/)1,(aQ1

1

0

Page 23: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 23

Moore型ステートマシンの記述例(3)

case cur_state iswhen Qa =>

d_out <= '0';if d_in = '1' then

next_state := Qb;end if;

when Qb =>d_out <= '0';if d_in = '1' then

next_state := Qc;end if;

0/)0,(aQ

0

10

0/)0,(bQ

1/)1,(aQ1

1

0

出力値は、入力値に依存せず、状態のみで決まる

Page 24: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 24

Moore型ステートマシンの記述例(4)

when Qc =>d_out <= '1';if d_in = '1' then

next_state := Qb;else

next_state := Qa;end if;

end case;end if;

end process P1;end architecture behavior;

0/)0,(aQ

0

10

0/)0,(bQ

1/)1,(aQ1

1

0

Page 25: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 25

講義内容

ステートマシンの定義Mealy型ステートマシン

Moore型ステートマシン

Mealy型からMoore型への変換

VHDLを用いたステートマシンの記述

データパス付きステートマシン

階層化ステートマシン

Page 26: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 26

データパス付きステートマシン

ステートマシンにデータパスを追加

カウンタ

加減算器

比較器

など 変数 := 式

信号 <= 式・・・

条件1

条件2

条件n

状態 S1

状態 S2

状態 Sn

Page 27: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 27

データパス付きステートマシンの記述例

初期化S0: d_out <= v;

count := N - 1;if count = 0 then

next_state := S2;else

next_state := S1;end if;

カウントダウンと終了判定S1: d_out <= v;

count := count – 1;if count = 0 then

next_state := S2;end if;

d_out <= v;count := N - 1;

d_out <= v;count := count – 1;

count = 0

S0

S1

S2

1 Cycle

N-1 Cycle

count = 0

count /= 0

Page 28: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 28

講義内容

ステートマシンの定義Mealy型ステートマシン

Moore型ステートマシン

Mealy型からMoore型への変換

VHDLを用いたステートマシンの記述

データパス付きステートマシン

階層化ステートマシン

Page 29: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 29

階層化ステートマシン

マスターSMスレーブSMに起動信号

を送る

スレーブSMの終了を待

スレーブSMマスターSMからの起動

信号を待つ

マスターSMに処理の終

了を伝える

start_s

done_s

S0

S1

done_s = ‘1’ /start_s <= ‘1’

done_s = ‘0’ /start_s <= ‘0’

S0

S1

start_s = ‘1’ /done_s <= ‘0’

処理終了 /done_s <= ‘1’

Page 30: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 30

ステートマシン間の同期

start_s

done_s

‘0’

‘1’ ‘0’

‘1’ ‘0’

‘1’

処理開始 処理終了

Time

clock

Page 31: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 31

マスターSMの記述例(1)library ieee;use ieee.std_logic_1164.all;

entity Master_SM isport(

clock: in std_logic;reset: in std_logic;done_s: in std_logic;start_s: out std_logic );

end entity Master_SM;

Page 32: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 32

マスターSMの記述例(2)architecture BEHAVIOR of Master_SM isbegin

process( clock, reset )type state_t is ( S0, S1 );variable cur_state, next_state: state_t;

beginif reset = '1' then

start_s <= '0';next_state := S0;

Page 33: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 33

マスターSMの記述例(3)elsif rising_edge( clock ) then

cur_state := next_state;case cur_state is

when S0 =>-- Do somethingif done_s = '1' then

start_s <= '1';next_state := S1;

elsestart_s <= '0';next_state := S0;

end if;

Page 34: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 34

マスターSMの記述例(4)when S1 =>

-- Do somethingif done_s = '0' then

start_s <= '0';next_state := S1;

elsenext_state := S0;

end if;end case;

end if;end process;

end architecture BEHAVIOR;

Page 35: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 35

スレーブSMの記述例(1)library ieee;use ieee.std_logic_1164.all;

entity Slave_SM isport(

clock: in std_logic;reset: in std_logic;start_s: in std_logic;done_s: out std_logic );

end entity Slave_SM;

Page 36: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 36

スレーブSMの記述例(2)architecture BEHAVIOR of Slave_SM isbegin

process( clock, reset )constant N: natural := 8;type state_t is ( S0, S1 );variable cur_state, next_state: state_t;variable count: natural;

beginif reset = '1' then

done_s <= '1';next_state := S0;

Page 37: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 37

スレーブSMの記述例(3)elsif rising_edge( clock ) then

cur_state := next_state;case cur_state is

when S0 =>-- Do somethingcount := N;if start_s = '1' then

done_s <= '0';next_state := S1;

elsedone_s <= '1';next_state := S0;

end if;

Page 38: 7章ステートマシンの設計imai/class/DIGITAL/PDF/Digital-07-A.pdf · 2006/11/28 ©2006, Masaharu Imai 1 第7章ステートマシンの設計 大阪大学大学院情報科学研究科

2006/11/28 ©2006, Masaharu Imai 38

スレーブSMの記述例(4)when S1 =>

-- Do somethingcount := count -1;if count = 0 then

done_s <= '1'; next_state := S0;

elsedone_s <= '0';next_state := S1;

end if;end case;

end if;end process;

end architecture BEHAVIOR;