1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ずっとFPGA初心者(その2:状態を覚える)

Posted at

その2

最初は続くかどうか心配していたが、めでたくその2を迎える。今後、その1の内容から、初心者レベルで、少しずつトライ&アレンジしてみたい。なお、その2以降、用いるボードおよび開発環境は、すべてその1と同じである。

状態を覚えておく

今回のお題は、回路内で状態を覚えておくというもの。

ソフトウェアエンジニアには馴染みがないところ

FPGAのHDL(言語)には、同時に処理を行うものと順番に処理を行うものとがある。後者については、VHDLでは、Process文というのものを使う。これらについては、このサイトなどに説明があり、同時処理文、順次処理文などと呼ばれるらしい。

例題

ボタンを押したらLEDが点灯し、再度ボタンを押したら消灯するもの。

コード

上記を実現するVHDLのコードは次のようなものとなる。

BtnLedOnOff.vhd
library ieee;
use ieee.std_logic_1164.all;

entity BtnLedOnOff is
	port (
		BTN:	in std_logic;
		LED: 	out std_logic);
end BtnLedOnOff;

architecture RTL of BtnLedOnOff is
signal LedFlag: std_logic := '1'; -- 1:消灯 0:点灯
	
begin -- 実コードはじまり	
	process (BTN) -- ① ここから
	begin
		if (BTN='0') then
			LedFlag <= not LedFlag;
		end if;
	end process;  -- ① ここまで
	
	LED <= LedFlag; -- ②
	
	-- #1 and #2 go at the same time.
end RTL; -- 実コードおわり

コードのはじまりからおわり(「begin」から「end RTL」)において、①と②とは同時に処理され(同時処理文)、①内は順番に処理される(順次処理文)。①では、「Process」の次のカッコ内の信号が変化した時に、①内の処理が順番の処理される。カッコ内の信号を「センシティビティリスト」と呼ぶ。

ボタンが押された時(負論理:押下は'0')に、内部信号であるLedFlagを反転させる。いわば、LedFlagがStaticな状態変数のようなものとなる。(シロートなのでこの説明の仕方が適切か不明であるが。)

ちなみに、下記コード

-- これはNGです
	process (BTN)
	begin
		if (BTN='0') then
			LED <= not LED;
		end if;
	end process; 

は、Quartusの「Start Analysis & Elaboration」でエラーが発生し、Buildできず。Quartusのエラーメッセージは下記のようなもの。

"Error (10309): VHDL Interface Declaration error in BtnLedOnOff.vhd(18): interface object "LED" of mode out cannot be read. Change object mode to buffer."というエラーである。直接信号を変化させることができないのであろうか?? (勉強不足状態)

なお、実動作(写真など)は自明なので省略。

最後に

ソフト屋には、まだ馴染めない。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?