第 5 学 年 工 学 実 験 Ⅱ VHDL による 論 理 回 路 設 計 13.パッケージとコンポーネント, Function と Procedure 平 成 26 年 4 月 電 子 システム 工 学 科 香 川 高 等 専 門 学 校
目 次 1. はじめに... 1 2. パッケージ(PACKAGE)とコンポーネント(COMPONENT)... 1 2.1 VHDL の PACKAGE... 1 2.2 PACKAGE の 作 成... 1 2.3 COMPONENT... 2 2.4 GENERIC MAP... 5 2.5 GENERATE と PORT MAP... 6 2.6 CONFIGURATION... 8 2.7 BLOCK... 10 2.8 VHDL2008... 11 3. FUNCTION と PROCEDURE... 13 3.1 SUBPROGRAM... 13 3.2 ASSERT 文... 13 3.3 FUNCTION 文... 14 3.4 PROCEDURE 文... 18 参 考 文 献... 20 i
1. はじめに 文 献 [1]の 8 章 パッケージとコンポーネント,9 章 function と procedure の 抄 訳 を 提 供 する 2. パッケージ(package)とコンポーネント(component) 2.1 VHDL の package VHDL コードの 分 割, 共 有, 再 利 用 には package,component,function,procedure を 使 用 する これらは 主 コードの 外 側,すなわち,ライブラリにおかれるので,システムレベルの 参 照 になる ライブラリは,function, procedure と 定 義 文 を 含 む 複 数 の package から 構 成 される この 章 で は,ライブラリの 構 築 を 扱 う 2.2 package の 作 成 パッケージの 作 成 には,package と package body と 呼 ばれる 2 種 類 のコードが 必 要 である 以 下 に 構 文 を 示 す package package_name is declarative_part end [package][package_name]; -------------------------------- [package body package_name is [subprogram_body] [deferred_constant_specifications] end [package_body][package_name]]; 最 初 の package の 部 分 は 宣 言 のみである これには,subprogram 宣 言, 型 宣 言,subtype 宣 言, 定 数 宣 言, 信 号 宣 言,shared variable 宣 言,file 宣 言,alias 宣 言,コンポーネント 宣 言,グル ープ template 宣 言,グループ 宣 言 がある, 次 の package body 部 は,subprogram すなわち function と procedure や deferred constant ( 設 定 延 期 定 数 )がある 場 合 にのみ 必 要 とされる この 場 合 には,subprogram の 内 容 や 定 数 宣 言 がすべ て 規 定 されなければならない 例 ) my_package と 名 付 けた type,signal, 定 数 のみの 例 を 示 す この 例 では package body 部 は 不 要 である - 1 -
package my_package is type matrix is array(1 to 3, 1 to 3) of bit; signal x: matrix; constant max1, max2: integer := 255; end package; VHDL2008 では,package にも generic 定 数 宣 言 を 許 す 次 は,package body 部 が 必 要 な 例 である 例 )function と deferred constant をもつ package これは,"clk'event and clk='0'"と 同 等 である library ieee; use ieee.std_logic_1164.all; -------------------------------- package my_package is constant flag: std_logic; function down_edge(signal s: std_logic)return boolean; end my_package; -------------------------------- package body my_package is constant flag: std_logic := '1'; function down_edge(signal s: std_logic) return boolean is return (s'event and s='0'); end down_edge; end my_package; 2.3 component component は, 単 純 に,これまでのコード 構 成 と 同 じ 構 成 をとる,すなわち,library/package 宣 言 +entity 部 +architecture 部 である ただ,component として 再 利 用 性 や 階 層 設 計 を 意 図 して 宣 言 している 一 般 的 によく 使 用 する 加 算 器, 乗 算 器,マルチプレクサなどは component 化 される component を 用 いる 設 計 は 構 造 的 設 計 と 呼 ばれる component は,architecture, package, generate,block 内 で 定 義 できる component の 使 用 には, 宣 言 部 と 実 装 部 (instantiation)が 必 要 である 以 下 に component 宣 言 の 構 文 を 示 す component component_name [is] [ generic ( const_name: const_type := const_value; const_name: const_type := const_value;...);] port( port_name: portmode signal_type; port_name: portmode signal_type;...); end component [component name]; - 2 -
次 に component 実 装 宣 言 の 構 文 を 示 す label: [component] component_name [generic map (generic_list)] port map(port_list); component 宣 言 は, 設 計 回 路 の entity 部 の 正 確 なコピーである generic 部 は, 実 装 時 に generic map により 変 更 する generic list がある 場 合 に 記 述 する component 実 装 では,label が 必 須 である 例 ) 3 入 力 NAND --- component declaration ------- component nand3 is port(a1, a2, a3: in std_logic; b: out std_logic); end component; --- component instantiation ------ nand_gate: nand3 port map (x1, x2, x3, y); この 例 では,component のオリジナルの 信 号 a1, a2, a3, b と 回 路 の 信 号 x1, x2, x3, y の 関 連 付 けを 位 置 によって 行 っている (1) マッピングのオプション マッピングには, 位 置 による 関 連 付 けと 名 前 による 関 連 付 けがある VHDL2008 の 拡 張 も 含 めて 以 下 に 示 す ---- component instantiation ----- nand3_1: nand3 port map (x1, x2, x3, y); -- positional mapping nand3_2: nand3 port map (a1=>x1, a2=>x2, a3=>x3, b=>y); -- nominal mapping nand3_3: nand3 port map (x1, x2, x3, open); -- positional mapping nand3_4: nand3 port map (a1=>x1, a2=>x2, a3=>x3, b=>open); -- nominal mapping 上 の 2 行 と 下 の 2 行 は 同 等 である キーワード open は, 出 力 が 未 接 続 を 表 す (2) component 宣 言 のオプション 図 1は component の 一 般 的 な 使 用 法 を 示 す (a)は,library に component 宣 言 があり,main の 中 で,component 宣 言 と component 実 装 の 両 方 を 記 述 する 方 法 である (b)は,component がライ ブラリ 中 のパッケージにある 場 合 であり,この 場 合 には main 部 には component 実 装 のみを 記 述 する (3) File/Project Assembling オプション component を 使 用 するコードのコンパイル 法 にはいくつかの 方 法 がある (a) すべてのコードを entity と 同 じ 名 前 の 1 ファイルに 収 める この 場 合 は,component 宣 言 も main 部 の 中 に 入 れる( 図 1(a)) (b) component を 別 のプロジェクトでコンパイルしておき,main 部 では component 宣 言 をする work ライブラリが 異 なる 場 合 には,use 節 によりライブラリを 明 示 する( 図 1(b)) (c) (b)と 同 様 に component を 別 にコンパイルする component 宣 言 も package に 入 れる main 部 では use 節 が 必 要 である - 3 -
library component nand Main code component 宣 言 library component nand component xor component 実 装 component xor Main code package component 実 装 component 宣 言 (a)library 中 の component 図 1 component の 使 用 (b)component 宣 言 の package 化 例 )load 付 巡 回 シフトレジスタ マルチプレクサと DFF を component として 図 2 に 示 す 巡 回 シフトレジスタを 1 ファイルでコード 化 する d 0 d 1 d 2 d 3 1 0 D Q 1 0 D Q 1 0 D Q 1 0 D Q CK CK CK CK load clk q 0 q 1 q 2 q 3 図 2 load 付 巡 回 シフトレジスタ ---- Multiplexer ----------------------------- entity mux is port (a, b, sel: in bit; x:out bit); end entity; --------------------------------- architecture mux of mux is x <= a when sel='0' else b; end architecture; ---- flipflop ----------------------------- entity flipflop is port (d, clk: in bit; q:out bit); end entity; --------------------------------- architecture flipflop of flipflop is process(clk) if (clk'event and clk='1') then q <= d; end if; end process; end architecture; - 4 -
---- main code ----------------------------- entity circular_shift is port (clk, load: in bit; d: in bit_vector(0 to 3); q: buffer_bit_vector(0 to 3)); end entity; --------------------------------- architecture structural of circular_shift is signal i: bit_vector(0 to 3); ------------------------------- component mux is port (a, b, sel: in bit; x:out bit); snd component; ------------------------------- component flipflop is port (d, clk: in bit; q:out bit); snd component; ------------------------------- mux1: mux port map (q(3), d(0), load, i(0)); mux2: mux port map (q(0), d(1), load, i(1)); mux3: mux port map (q(1), d(2), load, i(2)); mux4: mux port map (q(2), d(3), load, i(3)); dff1: flipflop port map (i(0), clk, q(0)); dff2: flipflop port map (i(1), clk, q(1)); dff3: flipflop port map (i(2), clk, q(2)); dff4: flipflop port map (i(3), clk, q(3)); end architecture; 2.4 generic map component 文 に generic 文 が 含 まれている 場 合 には,port map 文 に generic map を 記 述 する 例 ) --- component 宣 言 ----- component and_gate is generic(inputs: positive :=8); port(a: in bit_vector(1 to inputs); b: out bit); end component; ---- component instantiation ---- a1: and_gate generic map(16) port map(x, y); a2: and_gate generic map (inputs=>16) port map (a=>x, b=>y); inputs と 名 付 けたデフォルト 定 数 を 8 として 実 装 時 に 16 に 変 更 する 上 の 2 例 a1,a2 は 同 等 である component 文 中 の generic ではデフォルト 値 がなくてもよいが,generic map 文 では 規 定 しなけ ればならない - 5 -
例 ) component と generic map を 用 いたプライオリティ 検 出 器 N ビット 入 力 x 中 の 1 の 数 が 奇 数 のとき 出 力 y=1 となるプライオリティ 検 出 器 の 例 である component 宣 言 では,デフォルト 値 のない 定 数 として bits を 宣 言 しておき, 実 装 時 に generic map により main 部 で 宣 言 した 定 数 N を 設 定 している --------- The component --------------- entity par_detector is generic (bits: positive); port (input: in bit_vector(bits-1 downto 0); output: out bit); end par_detector; ----------------------------------------- architecture behavior of par_detector is process(input) variable temp: bit: temp := '0'; for i in input'range loop temp := temp xor input(i); end loop; output <= temp; end process; end behavior; ----- main code ------------------- entity parity_detector is generic (N: positive := 8); port(x: in bit_vector(n-1 downto 0); y : out bit); end parity_detector; ----------------------------------------- architecture structural of parity_detector is ------------ component par_detector is generic (bits: positive); port (input: in bit_vector(bits-1 downto 0); output: out bit); end component; ---------------- det: par_detector generic map(bits=>n) port map(input=>x, output=>y); end structural; 2.5 generate と port map 同 じ component を 多 数 使 用 する 場 合 には,generate loop 中 に port map をおいて component を 実 装 すると 便 利 である 代 表 的 な 使 用 例 を 次 に 示 す - 6 -
gen: for ion 0 to max generate comp: my_component port map (x(i), y(i), z(i)); end generate gen; 図 2 の load 付 シフトレジスタで 段 数 M, 各 データのビット 幅 を N とする 場 合 の 例 を 以 下 に 示 す 例 )component と generate を 用 いたシフトレジスタ この 例 では, 巡 回 ではなく 初 段 のマルチプレクサには 入 力 x(ビット 幅 N)が 入 るものとする ---- package ------------------------------------- package my_declarations is type twod is array (natural range <>, natural range <>) of bit; -------------- component mux is port (a, b, sel: in bit; x: out bit); end component; -------------- component flipflop is port (d, clk: in bit; q: out bit); end component; -------------- end package; ---- main code ------------------------------------ use work.my_declarations.all; ----- entity shift_register is generic (M: positive := 4; N: positive := 8); port (clk, load: in bit; x: in bit_vector(n-1 downto 0); d: in twod(0 to M-1, N-1 downto 0); y: out bit_vector(n-1 downto 0)); end entity; ----- - 7 -
architecture structural of shift_register is signal u: twod(0 to M, N-1 downto 0); signal v: twod(0 to M-1, N-1 downto 0); ---- transfer x->u and u->y ------ gen1: for i in N-1 downto 0 generate u(0,i) <= x(i); y(i) <= u(m,i); end generate gen1; ---- update internal array ------- gen2: for i in 0 to M-1 generate gen3: for j in N-1 downto 0 generate mux1: mux port map (u(i,j),d(i,j),load,v(i,j)); dff1: flipflop port map (v(i,j), clk, u(i+1,j)); end generate gen3; end generate gen2; end architecture; 2.6 configuration configuration 文 は,entity と architecture を 結 びつける 文 である プロジェクト 内 に 複 数 の architecture がある 場 合 や 複 雑 な 階 層 設 計 に 使 用 される configuration 文 は,entity や architecture の 外 に 置 かれる 以 下 の 2 形 式 がある (1) ダイレクト 形 式 configuration config_name of entity_name is for arch_name end for; end [configuration][config_name]; (2)コンポーネントの 実 装 configuration config_name of entity_name is for arch_name for label: component_name -- or for others/all: component_name use entity entity_name[(arch_name)]; end for; end for; end [configuration][config_name]; 以 下 の 例 は,2つの architecture をもつ entity に arch1 を 設 定 する 例 である - 8 -
例 ) entity test... end test; ------------------------------- architecture arch1... end arch1; ------------------------------- architecture arch2... end arch2; ------------------------------- configuration config1 of test is for arch1 end for; end configuration; 例 ) 次 は,component と generic map を 用 いたプライオリティ 検 出 器 を configuration により 書 き 直 す 例 である main code 中 の component 名 には 仮 の 名 前 detector を 使 用 し,configuration の 中 で behavior の par_detector と 接 続 する project ディレクトリは work である --------- The component --------------- entity par_detector is (ここは, 前 述 のプライオリティ 検 出 器 のコードと 同 じ) end behavior; ----------------------------------------- ----- main code ------------------- entity parity_detector is generic (N: positive := 8); port(x: in bit_vector(n-1 downto 0); y : out bit); end parity_detector; ----------------------------------------- architecture structural of parity_detector is ------------ component detector is generic (bits: positive); port (input: in bit_vector(bits-1 downto 0); output: out bit); end component; ---------------- det: detector generic map (N) port map(x,y); end structural; ----------------------------------------- configuration my_config of parity_detector is for structural for det: detector use entity work.par_detector(behavior); end for; end for; end my_config; 通 常 のコードでは,1つの entity に1つの architecture が 記 述 されるため,configuration は - 9 -
不 要 となる 2.7 block block は,システムレベル 設 計 においてコードの 分 割 を 促 進 するための 同 時 処 理 文 である block は architecture 内 で 使 用 する これを 用 いることにより 可 読 性 と 管 理 性 が 高 まる block は 順 次 処 理 内 では 使 用 できない しかし,process も 同 時 処 理 であったから,block 内 で process 文 は 使 用 できる 次 に block の 構 文 を 示 す label は 必 須 であり,guard_expression はオプションである declarative_part には,generic,generic map,port,port map と architecture の 宣 言 部 で 使 用 できるすべての 宣 言 をおくことができる label: block [(guard_expression)] [is] [declarative_part] concurrent_statements_part end block [label]; block をネストにして 複 雑 な 構 造 にもできるが, 以 下 は 単 純 な 例 である architecture example of...... controller: block... end block controller;... end example; guard_expression を 含 む block は guard block と 呼 ばれ,guard_expression 部 が true の ときのみ block 部 が 評 価 される 次 の 例 はこの 例 を 示 す しかし,guard_expression の 目 的 はドライバの 接 続, 非 接 続 にあるため 実 用 回 路 では 推 奨 しない 例 )guard block を 用 いたラッチ - 10 -
library ieee; use ieee.std_logic_1164.all; entity latch is port (d, clk: in std_logic; q: out std_logic); end entity; architecture block_latch of latch is blk: block (clk='1') q <= guarded d; end block blk; end architecture; 2.8 VHDL2008 package と component に 関 して VHDL2008 では 次 の 拡 張 がなされた (1) package の 宣 言 部 で 以 下 の 宣 言 が 可 能 である subprogram 実 装 宣 言,package 宣 言,package 実 装 宣 言 (2) package のヘッダにおいて generic 宣 言 が 可 能 以 下 に, 構 文 と 例 を 示 す package package_name is [generic (generic_list);] declarative_part end [package][package_name]; 例 ) package generic_type is generic (constant words: natural; type: word_type); type gen_type is array 1 to words of word_type; end package; (3) generic_list をもつ package は, 以 下 の 構 文 により package 実 装 宣 言 をしなければならな い package package_name is new uninstantiated_package_name generic map (instantiation_list); 上 の 例 では, 次 のようになる - 11 -
library ieee; use ieee.std_logic_1164; package memory_array is new work.generic_type generic map (instantiation_list); (4) port map の 指 定 では 以 下 のような 表 現 が 可 能 である cir: my_circuit port map I(inp=>a and b, outp => c); - 12 -
3. function と procedure 3.1 subprogram function と procedure は subprogram と 呼 ばれ, 順 次 に 処 理 されるため process とよく 似 てい る すなわち,これらでは 順 次 処 理 文 (if,wait,loop,case)のみが 使 用 可 能 である しかし,process は architecture 内 におかれるのに 対 して,subprogram は,package, entity, architecture, process のどこにおいてもよい 主 として subprogram は package におくため,システムレベルの1 ユニットと 解 釈 できる 3.2 assert 文 subprogram を 見 る 前 に 特 にシミュレーション 時 に 入 力 をチェックするのに 便 利 な assert 文 を 見 て おく assert 文 は 同 時 処 理 でも 順 次 処 理 でもある これは 論 理 合 成 には 関 係 がなくコンパイラまたはシ ミュレータへの 要 求 である assert 文 の 構 文 を 示 す [label:] assert boolean_expression [report string_expression] [severity severity_level]; string_expression は, 定 数 または string 型 の signal である 連 結 演 算 子 &が 使 える 例 ) s が"idle"であるとき report "Attention: s=" & s & "!" は,"Attention: s=idle!"というメッセージとしてスクリーンに 表 示 される severity_level は,note,warning,failure である このメッセージは 条 件 が false の 場 合 に 発 行 される error, failure が 発 生 するとコンパイラやシミュレータは 停 止 する 例 ) ある 関 数 が 2 個 の 同 一 サイズのベクトル a,b を 受 け 取 るとする assert (a'length=b'length) report "Signal a and b do not have the same length!" severity failure; このような 例 は 条 件 付 き assert 文 と 呼 ばれる, 無 条 件 assert 文 もあり, 次 の 構 文 をもつ - 13 -
[label:] assert false [report string_expression] [severity severity_level]; メッセージは 条 件 が false の 場 合 に 発 行 されるため,この 場 合 は, 強 制 発 行 になる 無 条 件 assert は,コンパイラやシミュレータがどの 時 点 まで 実 行 したかを 知 るための 有 効 な 手 段 で ある 複 雑 な assert 文 でよく 用 いられる 属 性 として,t'image(x)がある これは 型 t の 値 x を string に 変 換 する 属 性 である ただし, 元 の 型 は, 数, 列 挙 子, 物 理 型 でなければならない 例 を 示 す use ieee.std_logic_unsigned.all;... signal x, y: std_logic_vector(3 downto 0); signal n: integer range 0 to 255; signal t: time range 0ns to 200ns;... assert (x=y and n=ref) report "Mismatch at t=" & time'image(t) & "(for n=" & integer'image(n) & ", x=" & integer'image(conv_integer(x)) & ", y=" & integer'image(conv_integer(y)) & ")." severity failure;... 3.3 function 文 function は,signal と 同 様 に 順 次 処 理 文 であるから,if,wait,loop,case のみが 使 用 できる VHDL2008 拡 張 では,when と select も 使 用 できる 宣 言 部 も process 文 と 同 様 であり,signal の 宣 言 はできない 以 下 に function の 構 文 を 示 す [pure impure] function function_name [(input_list)] return return_value_type is [declarative part] statement_part [label:] return expression; end [function] [function_name]; 構 文 の 最 初 は, 後 述 の pure,impure である 指 定 がなければ pure がデフォルトである input_list は, 入 力 パラメータである これには constant,signal,file が 適 用 され,variable は 不 可 である function の 出 力 は return 文 で 示 される 1 つの 変 数 である - 14 -
例 ) function positive_edge (signal s: std_logic) return boolean is return (s'event and s='1'); end function positive_edge; function は,package,entity,architecture,process,block におくことができるが,package が 最 も 一 般 的 である 例 を 示 す ---- package ------------------------------- package my_subprograms is function positive_edge (signal s: std_logic) return boolean; end package; ---- package body ---------------------------- package body my_subprograms is function positive_edge (signal s: std_logic) return boolean is return (s'event and s='1'); end function positive_edge; end package body; (1) function call function はどこからでも 呼 び 出 すことができる 例 に 見 るように function 呼 び 出 しは expression の 1 つである 例 ) 以 下 の 2 行 は 同 等 である if positive_edge(clk) then... if clk'event and clk='1' then... (2) 位 置 による 関 連 付 け map と 名 前 による 関 連 付 け component の port map と 同 様 に function と function call の 関 連 付 けには 2 種 類 がある 例 ) ---- function declaration ------------------ function my_function (signal a, b: bit) return bit; ---- equivalent function calls ------------ y <= my_function(x1, x2); -- positional mapping y <= my_function(a=>x1, b=>x2); -- nominal mapping y <= my_function(b=>x2, a=>x1); -- nominal mapping (3) Pure と impure function function は 自 身 の 変 数 だけを 変 更 するとき,pure であると 言 われる この 場 合 は,どの 時 点 での function call も 結 果 は 同 じである これに 対 して,architecture,process,subprogram から - 15 -
呼 ばれ,signal や variable を 変 更 する 場 合 は impure であると 言 われ, 呼 び 出 すタイミングにより 同 じパラメータでも 結 果 が 異 なることがある impure 呼 び 出 しには 注 意 を 要 する VHDL2008 では function 内 で generic list が 可 能 となった 例 )architecture 内 の function max entity comparator is port (a, b, c: in integer range 0 to 255); y: out integer range 0 to 255); end entity; architecture comparator of comparator is function max (in1, in2, in3: integer) return integer is --- check in-out signal ------- assert (y'left=a'left and y'left=b'left and y'left=c'left and y'right = a'right and y'right=b'right and y'right=c'right) report "Signal sizes are not all equal!" severity failure; ---- Find maximum ------------ if (in1>=in2 and in1>=in3) then return in1; elsif (in2>=in1 and in2>=in3) then return in2; else return in3; end if; end function; y <= max(a, b, c)-- positional mapping -- y <= max(in1=> a, in2 => b; in3 => c) -- hominal mapping; eng architectute 例 )package 中 の function 入 力 の 添 え 字 を 降 順 に 並 べて, 最 後 の 数 を 0 にする 並 べ 替 えを 行 う library ieee; use ieee.numeric_std.all; package my_package is function order_and_fill (input: unsigned; bits: natural) return unsigned; end package; package body my_package is function order_and_fill (input: unsigned; bits: natural) return unsigned is variable a: unsigned (input'length-1 downto 0); variable result: unsigned (bits-1 downto 0); - 16 -
---- check input size ---- assert (input'length <= bits) report "Improper input size!" severity failure; ---- organize input ------ if (input'left > input'right) then a:= input; else for i in a'range loop a(i) := input(input'left+i); end loop; end if; ---- fill with zeros ------ if (a'length < bits) then result(bits-1 downto s'length) := (others =>'0'); result(a'length-1 sownto 0):= a; else result := a; end if; return result; end function; end package body; ---- main code --------------- library ieee; use ieee.numeric_std.all; use work.my_package.all; entity organizer is generic(size: natural :=5); port (x: in unsigned(2 to 5); y: out unsigned(size-1 downto 0)); end entity; architecture organizer of organizer is y <= order_and_fill(x, size) end architecture; - 17 -
例 )entity 内 の function std_logic_vector から integer への 変 換 library ieee; use ieee.std_logic_1164.all; entity... port (...) function slv_to_integer (signal s: std_logic_vector) return integer is alias ss: std_logic_vector(1 to s'length) is s; variable result: integer range 0 to 2**s'length-1; result := 0; for i in 1 to s'length loop result := result * 2; if (ss(i)='1' or ss(i)='h') then result := result + 1; elsif (ss(i)/='0' and ss(i)/='l') then assert false report "There is a invalid input!" severity failure; end if; end loop; return result; end function slv_to_integer; architecture... 3.4 procedure 文 procedure は,function とほとんど 同 じであるが, 返 り 値 が 複 数 になる 点 が 異 なる procedure の 構 文 を 示 す prodcedure procedure_name (input_output_list) is [declarative_part] statement_part end [procedure] [procedure_name]; input_output_list には,constant,signal,variable が 入 る モードは in,out,inout である in の 場 合 のデフォルトは constant となり,out,または,inout の 場 合 は variable がデ フォルトである 宣 言 は 次 の 形 式 をもつ constant constant_name: mode constant_type; signal signal_name: mode signal_type; variable variable_name: mode variable_type; - 18 -
function も procedure も 順 次 処 理 であるから, 順 次 処 理 文 のみが 許 される 例 )package 中 の procedure min_max ------- package ----------------------------- package my_package is procedure min_max (signal a, b, c: in integer; signal min, max: out integer); end package; package body my_package is procedure min_max (signal a, b, c: in integer range 0 to 255; signal min, max: out integer range 0 to 255) is if (a>=b) then if (a>=c) then max <= a; if (b>=c) then min <= c; else min <= b; end if; else max <= c; min <= b; end if; else if (b>=c) then max <= b; if (a>=c) then min <= c; else min <= a; end if; else max <= c; min <= a; end if; end if; end procedure; end package body; - ------- main code --------------------------- use work.my_package.all; entity comparator is port (a, b, c: in integer range -256 to 255); min, max: out integer range -256 to 255); end entity; architecture comparator of comparator is min_max(a,b,c,min,max); end architecture; - 19 -
文 献 [1] Volnei A. Pedroni:Circuit Design and Simulation with VHDL 2 nd ed., MIT Press, 2010. - 20 -