52
VHDL VHDL 硬硬硬硬 硬硬硬硬 硬硬硬硬硬硬硬硬 硬硬硬硬硬硬硬硬 第第第 第第第 VHDL VHDL 第第第第第第第第第第第 第第第第第第第第第第第

VHDL 硬體描述語言 數位電路設計實務

Embed Size (px)

DESCRIPTION

VHDL 硬體描述語言 數位電路設計實務. 第三章 VHDL 的語法協定以及資料型態. 3-1 VHDL 的語法協定 以及 如何寫出易讀的程式碼. 3-1.1 VHDL 的語法協定 VHDL 是由一連串的標記 (token) 所組成,這些標記可以是:空白 (Whitespace) 、註解 (Comment) 、運算子 (Operator) 、數值 (Number) 、識別字 (Identifier) 與關鍵字 (Keyword) 以及底線 (Underscore character) 和問號 (Question Mark) 。 - PowerPoint PPT Presentation

Citation preview

Page 1: VHDL  硬體描述語言 數位電路設計實務

VHDL VHDL 硬體描述語言硬體描述語言數位電路設計實務數位電路設計實務

第三章第三章VHDL VHDL 的語法協定以及資料型態的語法協定以及資料型態

Page 2: VHDL  硬體描述語言 數位電路設計實務

3-1 VHDL 3-1 VHDL 的語法協定 以及的語法協定 以及如何寫出易讀的程式碼如何寫出易讀的程式碼

3-1.1 VHDL 3-1.1 VHDL 的語法協定的語法協定 VHDL VHDL 是由一連串的標記 是由一連串的標記 (token) (token) 所組成,這些標記可以是:空白 所組成,這些標記可以是:空白 (Whitespace)(Whitespace) 、、

註解 註解 (Comment)(Comment) 、運算子 、運算子 (Operator)(Operator) 、數值 、數值 (Number)(Number) 、識別字 、識別字 (Identifier) (Identifier) 與與關鍵字 關鍵字 (Keyword) (Keyword) 以及底線 以及底線 (Underscore character) (Underscore character) 和問號 和問號 (Question Mark)(Question Mark) 。。 VHDL VHDL 是一種“大小寫”沒分別的硬體描述語言是一種“大小寫”沒分別的硬體描述語言 識別字 識別字 (Identifier)(Identifier) 可以適當地加入空白和換行 可以適當地加入空白和換行 所有的關鍵字 所有的關鍵字 (keyword) (keyword) 名稱必須使用小寫來表示名稱必須使用小寫來表示 註解 註解 (Comment) (Comment) 運算子 運算子 (Operator) (Operator)

單元 單元 (Unary) (Unary) 運算子 運算子 // 二元 二元 (Binary) (Binary) 運算子 運算子 數值 數值 (Number) (Number) 關鍵字 關鍵字 (Keyword) (Keyword) 底線 底線 (Underscore characters) (Underscore characters) 和問號 和問號 (Question Marks) (Question Marks)

Page 3: VHDL  硬體描述語言 數位電路設計實務

3-1.2 3-1.2 如何寫出易讀的程式碼如何寫出易讀的程式碼 用內縮用內縮 (ident) (ident) 的方式去對齊相對應的敘述的方式去對齊相對應的敘述 用空行用空行 (( 空列空列 才對才對 ) ) 的方式來區分出的方式來區分出 entityentity 、、

architecturearchitecture 、、 process … process … 等等等等 用空格用空格 (( 空白空白 ) ) 對齊對齊 將很長的一行將很長的一行 (( 一列一列 才對才對 ) ) 分成二行分成二行 (( 二列二列

才對以上來寫才對以上來寫 )) 為為 processprocess 、、 function function 以及以及 procedure procedure 加上加上

標記標記 (label)(label)

Page 4: VHDL  硬體描述語言 數位電路設計實務

3-2 VHDL 3-2 VHDL 的標準邏輯值的標準邏輯值 (Standard Logic Value)(Standard Logic Value)

標準邏輯值是定義在標準邏輯值是定義在 IEEE std_logic_1164 IEEE std_logic_1164 這個這個 package package 裡頭。裡頭。

要用要用 use use 這個敘述才能使用這個敘述才能使用 IEEE std_logic_1164 IEEE std_logic_1164 所定義在的所定義在的標準邏輯值。標準邏輯值。

library IEEE;library IEEE;

use IEEE.std_logic_1164.all;use IEEE.std_logic_1164.all;

  

library library 名稱 名稱 package package 名稱 要使用 名稱 要使用 package package 裡頭所有的定裡頭所有的定義 義

Page 5: VHDL  硬體描述語言 數位電路設計實務

標準邏輯值的定義在標準邏輯值的定義在 stdlogic.vhd stdlogic.vhd 檔案,檔案, std_logic_1164std_logic_1164 套件中,如下:套件中,如下: Type Type STD_ULOGICSTD_ULOGIC is ( is (

‘‘UU’’, -- Uninitialized , -- Uninitialized ‘‘XX’’, -- Forcing Unknown, -- Forcing Unknown‘‘00’’, -- Forcing 0, -- Forcing 0‘‘11’’, -- Forcing 1, -- Forcing 1‘‘ZZ’’, -- High Impedance , -- High Impedance ‘‘WW’’, -- Weak Unknown, -- Weak Unknown‘‘LL’’, -- Weak 0, -- Weak 0‘‘HH’’, -- Weak 1, -- Weak 1

‘‘--’’, -- Don’t care , -- Don’t care ););

-- industry standard logic type-- industry standard logic type subtype subtype std_logicstd_logic is resolved is resolved STD_ULOGICSTD_ULOGIC;;   -- unconstrained array of std_logic for use in declaring signal arrays-- unconstrained array of std_logic for use in declaring signal arrays type std_logic_vector is array ( natural range <>) of std_logic;type std_logic_vector is array ( natural range <>) of std_logic;

Page 6: VHDL  硬體描述語言 數位電路設計實務

VHDL VHDL 所提供的標準邏輯值所提供的標準邏輯值

Page 7: VHDL  硬體描述語言 數位電路設計實務

resolution functionresolution function function resolved ( s : std_ulogic_vector ) return std_ulogic isfunction resolved ( s : std_ulogic_vector ) return std_ulogic is

variable result : std_ulogic := 'z'; -- weakest state defaultvariable result : std_ulogic := 'z'; -- weakest state default

beginbegin

-- the test for a single driver is essential otherwise the-- the test for a single driver is essential otherwise the

-- loop would return 'x' for a single driver of '-' and that-- loop would return 'x' for a single driver of '-' and that

-- would conflict with the value of a single driver unresolved-- would conflict with the value of a single driver unresolved

-- signal.-- signal.

if (s'length = 1) thenif (s'length = 1) then

return s(s'low);return s(s'low);

elseelse

for i in s'range loopfor i in s'range loop

result := resolution_table(result, s(i));result := resolution_table(result, s(i));

end loop;end loop;

end if;end if;

return result;return result;

end resolved;end resolved;

Page 8: VHDL  硬體描述語言 數位電路設計實務

std_logicstd_logic

Page 9: VHDL  硬體描述語言 數位電路設計實務

std_logic_vectorstd_logic_vector

標準邏輯值向量,可以存放一個以上標準邏輯值向量,可以存放一個以上 VHDL VHDL 所提供的標準邏輯值。所提供的標準邏輯值。

例如:例如:

Page 10: VHDL  硬體描述語言 數位電路設計實務

3-3 VHDL 3-3 VHDL 內定的內定的資料型態資料型態 (data type)(data type)

Page 11: VHDL  硬體描述語言 數位電路設計實務

3-3.1 type 3-3.1 type 以及 以及 subtype subtype 敘述敘述 type type 敘述用來宣告一種新的資料型態。敘述用來宣告一種新的資料型態。 語法:語法: type type_name is type_definition;type type_name is type_definition;

例如:底下這一節“純量例如:底下這一節“純量 (Scalar)(Scalar)”” 中的“整數中的“整數 (integer)(integer)”” 。。type integer is range -2147483648 to 2147483647;type integer is range -2147483648 to 2147483647;

integer integer 整數,可以儲存的資料範圍從整數,可以儲存的資料範圍從 -2147483648-2147483648 到到 21474836472147483647 。。又例如:又例如:type byte is array ( 7 downto 0) of bit;type byte is array ( 7 downto 0) of bit;

type word is array ( 15 downto 0) of bit;type word is array ( 15 downto 0) of bit;

type dboule_word is array ( 31 downto 0) of bit;type dboule_word is array ( 31 downto 0) of bit;

type data_array is array ( 0 to 15) of dboule_word;type data_array is array ( 0 to 15) of dboule_word;

type unlimite_memory is array ( integer range <>) of dboule_word;type unlimite_memory is array ( integer range <>) of dboule_word;

type munlimite_atrix is array ( integer range <>, integer range <>) of byte;type munlimite_atrix is array ( integer range <>, integer range <>) of byte;

Page 12: VHDL  硬體描述語言 數位電路設計實務

可以運用可以運用 subtype subtype 敘述,將現存的資料型態來產生一種較“敘述,將現存的資料型態來產生一種較“原先該資料型態”可以表示資料範圍還小的一種“新的資料原先該資料型態”可以表示資料範圍還小的一種“新的資料

型態”型態”語法:語法: subtype subtype_name is subtype_indication;subtype subtype_name is subtype_indication;

例如:底下這一節“純量例如:底下這一節“純量 (Scalar)(Scalar)”” 中的“整數中的“整數 (integer)(integer)”” 之之 natural natural 自然數。自然數。subtype natural is integer range 0 to integer'High;subtype natural is integer range 0 to integer'High;

natural natural 自然數,可以儲存的資料範圍從自然數,可以儲存的資料範圍從 00 到到 21474836472147483647 。。subtype SMALL_INT is INTEGER range 0 to 1; -- subtype SMALL_INT is INTEGER range 0 to 1; -- 可以儲存可以儲存 00 或或 11

  

又例如:物理量又例如:物理量 (Physical) (Physical) 分類中的分類中的 timetime

type time is range -2147483647 to 2147483647type time is range -2147483647 to 2147483647

unitsunits

… …

end units;end units;

subtype delay_length is time range 0 fs to time'high;subtype delay_length is time range 0 fs to time'high;

Page 13: VHDL  硬體描述語言 數位電路設計實務

又例如:又例如:subtype my_integer is integer range -128 to 127;subtype my_integer is integer range -128 to 127;variable a_my_integer : my_integer := 43;variable a_my_integer : my_integer := 43;  subtype my_name is string( 1 to 31) ;subtype my_name is string( 1 to 31) ;variable a_name : my_name := "Johnson Cheng";variable a_name : my_name := "Johnson Cheng";  subtype word is std_logic_vector( 15 downto 0);subtype word is std_logic_vector( 15 downto 0);signal word_h : word := x"FFFF";signal word_h : word := x"FFFF";signal word_d : word := conv_std_logic_vector( 65535, 17)( 15 downto 0);signal word_d : word := conv_std_logic_vector( 65535, 17)( 15 downto 0);-- o"177777" ModelSim 5.6b compile -- o"177777" ModelSim 5.6b compile 有錯有錯 , , 只好分二段給只好分二段給signal word_o : word := '1' & o"77777";signal word_o : word := '1' & o"77777";-- String length is 18. Expected length is 16-- String length is 18. Expected length is 16-- signal word_o2 : word := o"177777";-- signal word_o2 : word := o"177777";signal word_b : word := b"1111111111111111";signal word_b : word := b"1111111111111111";  subtype X01 IS resolved std_ulogic range 'X' to '1'; -- ('X','0','1')subtype X01 IS resolved std_ulogic range 'X' to '1'; -- ('X','0','1')subtype X01Z IS resolved std_ulogic range 'X' to 'Z'; -- ('X','0','1','Z')subtype X01Z IS resolved std_ulogic range 'X' to 'Z'; -- ('X','0','1','Z')subtype UX01 IS resolved std_ulogic range 'U' to '1'; -- ('U','X','0','1')subtype UX01 IS resolved std_ulogic range 'U' to '1'; -- ('U','X','0','1')subtype UX01Z IS resolved std_ulogic range 'U' to 'Z'; -- ('U','X','0','1','Z')subtype UX01Z IS resolved std_ulogic range 'U' to 'Z'; -- ('U','X','0','1','Z')

Page 14: VHDL  硬體描述語言 數位電路設計實務

3-3.2 3-3.2 純量純量 (Scalar)(Scalar) 純量純量 (Scalar) (Scalar) 資料型態,包括:整數資料型態,包括:整數 (integer)(integer) 、實數、實數 (real)(real) 、列舉、列舉 (enume(enume

rated) rated) 以及物理量以及物理量 (Physical)(Physical) 。。其它還有:其它還有: bitbit 、、 booleanboolean 、、 bit_vectorbit_vector 、、 charactercharacter 、、 stringstring ,以及,以及 timetime 。。 整數整數 (integer)(integer)

type integer is range -2147483648 to 2147483647;type integer is range -2147483648 to 2147483647;

  

整數,可以儲存的資料範圍從整數,可以儲存的資料範圍從 –– 2231 31 到到 223131 - 1 - 1 。例如:。例如:signal Integer_32Bits : integer; -- signal Integer_32Bits : integer; -- 需要需要 3232 個位元個位元  

-- -- 加上加上 range range 識別字讓儲存範圍可以限定識別字讓儲存範圍可以限定signal unsigned_int : integer range 0 to 15; -- 0 ~ 15 signal unsigned_int : integer range 0 to 15; -- 0 ~ 15 無號數整數無號數整數signal signed_int : integer range -8 to 7; -- -8 ~ 7 signal signed_int : integer range -8 to 7; -- -8 ~ 7 有號數整數有號數整數signal Table_Index : integer signal Table_Index : integer rangerange 1 to 30; -- 1 ~ 31, 1 to 30; -- 1 ~ 31, 只需要只需要 55 個位元個位元variable value_8bits : integer range 0 to 255; -- 0 ~ 255, variable value_8bits : integer range 0 to 255; -- 0 ~ 255, 只需要只需要 88 個位元個位元

Page 15: VHDL  硬體描述語言 數位電路設計實務

整數整數 (integer) (integer) 給值的方式給值的方式 語法:語法: radix#number_in_radix#radix#number_in_radix# 其中其中 radix radix 是基底,可以是是基底,可以是 2 ~ 162 ~ 16 其中一種,各代表其中一種,各代表 22 進制值進制值

~ 16~ 16 進制值的表示方法。進制值的表示方法。例如:例如:signal INT_2_Data : INTEGER range 0 to 3 := 2#10#; -- 2signal INT_2_Data : INTEGER range 0 to 3 := 2#10#; -- 2

signal INT_3_Data : INTEGER range 0 to 31 := 3#210#; -- 21signal INT_3_Data : INTEGER range 0 to 31 := 3#210#; -- 21

signal INT_4_Data : INTEGER range 0 to 255 := 4#3210#; -- 228signal INT_4_Data : INTEGER range 0 to 255 := 4#3210#; -- 228

signal INT_5_Data : INTEGER := 5#43210#; -- 2930signal INT_5_Data : INTEGER := 5#43210#; -- 2930

signal INT_6_Data : INTEGER := 6#543210#; -- 44790signal INT_6_Data : INTEGER := 6#543210#; -- 44790

Page 16: VHDL  硬體描述語言 數位電路設計實務

實數實數 (real)(real) type real is range -1.0E308 to 1.0E308;type real is range -1.0E308 to 1.0E308;  

實數,可以儲存的資料範圍從實數,可以儲存的資料範圍從 –– 1*101*10308 308 到到 1*101*10308308 。例如:。例如:signal Tousand : real := 1.0E3;signal Tousand : real := 1.0E3;variable PI : real := 3.14;variable PI : real := 3.14;  variable rval : real;variable rval : real;variable int_val : integer:=20;variable int_val : integer:=20;variable sign : integer := -1.0;variable sign : integer := -1.0;rval := rval*(10.0**(int_val * sign)); -- rval * 10rval := rval*(10.0**(int_val * sign)); -- rval * 10 -20-20

  rval := 3; -- rval := 3; -- 錯誤的寫法,要改成錯誤的寫法,要改成 3.0 3.0 才對才對

Page 17: VHDL  硬體描述語言 數位電路設計實務

實數實數 (real) (real) 給值的方式給值的方式 語法:語法: radix#number_in_radix#radix#number_in_radix#

其中其中 radix radix 是基底,同樣可以是是基底,同樣可以是 2 ~ 162 ~ 16 其中一種,各代表其中一種,各代表 22 進制值進制值 ~ 16~ 16 進制進制值的表示方法。值的表示方法。

例如:例如:signal REAL_2_Data : REAL := 2#10.01#; -- 2.25signal REAL_2_Data : REAL := 2#10.01#; -- 2.25

signal REAL_3_Data : REAL := 3#210.01#; -- 21.1111signal REAL_3_Data : REAL := 3#210.01#; -- 21.1111

signal REAL_4_Data : REAL := 4#3210.01#; -- 228.063signal REAL_4_Data : REAL := 4#3210.01#; -- 228.063

signal REAL_5_Data : REAL := 5#43210.01#; -- 2930.04signal REAL_5_Data : REAL := 5#43210.01#; -- 2930.04

…… ……

signal REAL_15_Data : REAL := 15#EDCBA9876543210.01#; -- 4.3566e+017signal REAL_15_Data : REAL := 15#EDCBA9876543210.01#; -- 4.3566e+017

signal REAL_16_Data : REAL := 16#FEDCBA9876543210.01#; -- 1.83648e+019signal REAL_16_Data : REAL := 16#FEDCBA9876543210.01#; -- 1.83648e+019

  

一般來說“合成軟體”並不支援實數 一般來說“合成軟體”並不支援實數 (real) (real) 這種資料型態,但是可以用在電路這種資料型態,但是可以用在電路的模擬。的模擬。

Page 18: VHDL  硬體描述語言 數位電路設計實務

物理量物理量 (Physical) (Physical) 分類中的分類中的 timetime type time is range -2147483647 to 2147483647type time is range -2147483647 to 2147483647

unitsunits fs;fs; ps = 1000 fs; -- 1 psps = 1000 fs; -- 1 ps 等於等於 1000 fs1000 fs

ns = 1000 ps; -- 1 nsns = 1000 ps; -- 1 ns 等於等於 1000 ps1000 ps us = 1000 ns; -- 1 usus = 1000 ns; -- 1 us 等於等於 1000 ns1000 ns ms = 1000 us; -- 1 msms = 1000 us; -- 1 ms 等於等於 1000 us1000 us sec = 1000 ms; -- 1sec = 1000 ms; -- 1 秒等於秒等於 1000 ms1000 ms min = 60 sec; -- 1min = 60 sec; -- 1 分鐘等於分鐘等於 6060 秒秒 hr = 60 min; -- 1hr = 60 min; -- 1 小時等於小時等於 6060 分鐘分鐘

end units;end units;  

它是屬由:物理它是屬由:物理 (Physical) (Physical) 量這個分類。量這個分類。一般來說“合成軟體”並不支援物理量一般來說“合成軟體”並不支援物理量 (Physical) (Physical) 分類中的分類中的 time time 這種資料這種資料

型態,但是可以用在電路的模擬。型態,但是可以用在電路的模擬。

Page 19: VHDL  硬體描述語言 數位電路設計實務

列舉列舉 (enumerated)(enumerated) 列舉,將數值依序列出的一種資料定義方式。列舉,將數值依序列出的一種資料定義方式。語法:語法: type type_name is ( enum0, enum1, … )type type_name is ( enum0, enum1, … )

例如:例如:type Bit_UX01Z is ( 'U', 'X', '0', '1', 'Z' );type Bit_UX01Z is ( 'U', 'X', '0', '1', 'Z' );  

在在 3-2 VHDL 3-2 VHDL 的標準邏輯值的標準邏輯值 (Standard Logic Value)(Standard Logic Value) ,, std_ulogic std_ulogic 的定義也是用列舉的方式的定義也是用列舉的方式定出來的。定出來的。

type type std_ulogicstd_ulogic is ( ’U’, ‘X’, ‘0’, ‘1’, ‘Z’, ‘W’, ‘L’, ‘H’, ‘-‘ ); is ( ’U’, ‘X’, ‘0’, ‘1’, ‘Z’, ‘W’, ‘L’, ‘H’, ‘-‘ );  type My_state is ( ST0, ST1, ST2, ST3, ST4 );type My_state is ( ST0, ST1, ST2, ST3, ST4 );signal State : My_state;signal State : My_state;  

其實它等於底下的這種寫法:其實它等於底下的這種寫法:constant ST0 : std_logic_vector( 2 downto 0) := "001";constant ST0 : std_logic_vector( 2 downto 0) := "001";constant ST1 : std_logic_vector( 2 downto 0) := "010";constant ST1 : std_logic_vector( 2 downto 0) := "010";constant ST2 : std_logic_vector( 2 downto 0) := "011";constant ST2 : std_logic_vector( 2 downto 0) := "011";constant ST3 : std_logic_vector( 2 downto 0) := "100";constant ST3 : std_logic_vector( 2 downto 0) := "100";constant ST4 : std_logic_vector( 2 downto 0) := "101";constant ST4 : std_logic_vector( 2 downto 0) := "101";signal State : std_logic_vector( 2 downto 0);signal State : std_logic_vector( 2 downto 0);

Page 20: VHDL  硬體描述語言 數位電路設計實務

booleanboolean 布林值,只能放布林值,只能放 true true 或者是或者是 falsefalse 。。

   例 1

type boolean is (false, true);type boolean is (false, true);

  

signal a, b : boolean;signal a, b : boolean;

a <= true;a <= true;

b <= false;b <= false;

Page 21: VHDL  硬體描述語言 數位電路設計實務

   例 2function ToSig (bool : Boolean ) return std_logic isfunction ToSig (bool : Boolean ) return std_logic is variable sig : std_logic;variable sig : std_logic;beginbegin if (bool) thenif (bool) then sig := '1';sig := '1'; elseelse sig := '0';sig := '0'; end if;end if; return sig;return sig;end ToSig;end ToSig;

““ 合成軟體“一般來說會將 合成軟體“一般來說會將 boolean boolean 轉換成 轉換成 std_logicstd_logic ,例如:,例如:signal a, b : boolean;signal a, b : boolean;

相等於 相等於 signal a, b : std_logic;signal a, b : std_logic;

Page 22: VHDL  硬體描述語言 數位電路設計實務

bitbit

type bit is ('0', '1');type bit is ('0', '1');

  

一個位元的邏值值,只能放一個位元的邏值值,只能放 ‘ ‘ 1’ 1’ 或者是或者是 ‘ ‘ 0’0’ 。例如:。例如:signal d, e : bit;signal d, e : bit;

d <= ‘1’; e <= ‘0’;d <= ‘1’; e <= ‘0’;

  

““ 合成軟體“一般來說會將合成軟體“一般來說會將 bit bit 轉換成轉換成 std_logicstd_logic ,,例如:例如:

signal d, e : bit;signal d, e : bit;

相等於相等於 signal d, e : std_logic;signal d, e : std_logic;

Page 23: VHDL  硬體描述語言 數位電路設計實務

bit_vectorbit_vector

type bit_vector is array (natural range <>) of type bit_vector is array (natural range <>) of bitbit;;

  

bit_vector bit_vector 是由是由 bit bit 所組成的。例如:所組成的。例如:signal value : bit_vector(3 downto 0) := "0100";signal value : bit_vector(3 downto 0) := "0100";

  

““ 合成軟體“一般來說會將合成軟體“一般來說會將 bit_vector bit_vector 轉換成轉換成 std_lostd_logic_vectorgic_vector ,例如:,例如:

signal value : bit_vector(3 downto 0) := "0100";signal value : bit_vector(3 downto 0) := "0100";

相等於相等於 signal value : std_logic_vector( 3 downto 0) := "0signal value : std_logic_vector( 3 downto 0) := "0100";100";

Page 24: VHDL  硬體描述語言 數位電路設計實務

charactercharacter type character is (type character is (

nul, soh, stx, etx, eot, enq, ack, bel, nul, soh, stx, etx, eot, enq, ack, bel,

bs, ht, lf, vt, ff, cr, so, si, bs, ht, lf, vt, ff, cr, so, si,

dle, dc1, dc2, dc3, dc4, nak, syn, etb, dle, dc1, dc2, dc3, dc4, nak, syn, etb,

can, em, sub, esc, fsp, gsp, rsp, usp, can, em, sub, esc, fsp, gsp, rsp, usp,

  

' ', '!', '"', '#', '$', '%', '&', ''', ' ', '!', '"', '#', '$', '%', '&', ''',

'(', ')', '*', '+', ',', '-', '.', '/', '(', ')', '*', '+', ',', '-', '.', '/',

'0', '1', '2', '3', '4', '5', '6', '7', '0', '1', '2', '3', '4', '5', '6', '7',

'8', '9', ':', ';', '<', '=', '>', '?', '8', '9', ':', ';', '<', '=', '>', '?',

  

'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',

'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',

'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',

'X', 'Y', 'Z', '[', '\', ']', '^', '_', 'X', 'Y', 'Z', '[', '\', ']', '^', '_',

  

'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',

'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',

'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',

'x', 'y', 'z', '{', '|', '}', '~', del,'x', 'y', 'z', '{', '|', '}', '~', del,

-- -- 字元字元 128 128 到到 255 255 不在不在 ASCII ASCII 表的範圍中,印不出來表的範圍中,印不出來););

  

character character 字元,可以儲存一個字元字元,可以儲存一個字元 (character)(character) 。例如:。例如:variable c : character := ‘A’;variable c : character := ‘A’;

Page 25: VHDL  硬體描述語言 數位電路設計實務

將將 std_logic std_logic 轉換成轉換成 charactercharacter type type stdlogic_to_char_tstdlogic_to_char_t is array( is array( std_logicstd_logic ) of ) of charactercharacter;;  constant to_char : constant to_char : stdlogic_to_char_tstdlogic_to_char_t := ( := (

'U' => 'U','U' => 'U', 'X' => 'X','X' => 'X', '0' => '0','0' => '0', '1' => '1','1' => '1', 'Z' => 'Z','Z' => 'Z', 'W' => 'W','W' => 'W', 'L' => 'L','L' => 'L', 'H' => 'H','H' => 'H', '-' => '-' );'-' => '-' );

  function lower_case(c : character) return character isfunction lower_case(c : character) return character isbeginbegin if c >= 'A' and c <= 'Z' thenif c >= 'A' and c <= 'Z' then

return character'val(character'pos(c) + 32);return character'val(character'pos(c) + 32); elseelse

return c;return c; end if;end if;

end;end;

一般來說“合成軟體”並不支援 一般來說“合成軟體”並不支援 character character 字元這種資料型態,但是可以用在電路的模擬。字元這種資料型態,但是可以用在電路的模擬。

Page 26: VHDL  硬體描述語言 數位電路設計實務

stringstring

type string is array (type string is array (positivepositive range <>) of character; range <>) of character;

  

string string 字串,可以儲存的多個字元字串,可以儲存的多個字元 (character)(character) 。例如:。例如:constant Result_File : string := "./Result.txt";constant Result_File : string := "./Result.txt";

constant Source_File : string := string'( "Source_File.dat" );constant Source_File : string := string'( "Source_File.dat" );

variable I_am_str : string(1 to 80);variable I_am_str : string(1 to 80);

I_am_str := “Hello”;I_am_str := “Hello”;

Write ( OutString, String'( I_am_str ) );Write ( OutString, String'( I_am_str ) );

  

一般來說“合成軟體”並不支援一般來說“合成軟體”並不支援 string string 字串這種資料型態,但字串這種資料型態,但是可以用在電路的模擬。是可以用在電路的模擬。

Page 27: VHDL  硬體描述語言 數位電路設計實務

3-3.3 3-3.3 合成合成 (Composite)(Composite) 合成合成 (Composite) (Composite) 資料型態,包括:陣列資料型態,包括:陣列 (array) (array) 以及記錄以及記錄 (re(re

cord)cord) 。。

陣列陣列 (array)(array) 陣列陣列 (array) (array) 是由一個以上相同資料所組合而成的一種資料定是由一個以上相同資料所組合而成的一種資料定

義方式。義方式。

語法:語法: type array_name is array( n1 to n2 ) of data_type;type array_name is array( n1 to n2 ) of data_type;

或者是或者是 type array_name is array( n3 downto n4 ) of data_type;type array_name is array( n3 downto n4 ) of data_type;

例如:例如:type BYTE is array( 7 downto 0) of std_logic;type BYTE is array( 7 downto 0) of std_logic;type WORD is array( 15 downto 0) of std_logic;type WORD is array( 15 downto 0) of std_logic;type DWORD is array( 31 downto 0) of std_logic;type DWORD is array( 31 downto 0) of std_logic;signal byte_1 : BYTE;signal byte_1 : BYTE;

Page 28: VHDL  硬體描述語言 數位電路設計實務

type t_0_to_7 is array ( 0 to 7) of std_logic;type t_0_to_7 is array ( 0 to 7) of std_logic;type ROM_Table_4x8 is array ( 0 to 3) of t_0_to_7;type ROM_Table_4x8 is array ( 0 to 3) of t_0_to_7;-- ROM_Code -- ROM_Code 有有 4 4 個個 ROM Code, ROM Code, 且每個且每個 Register Register 擁有擁有 8 8 個個 BitsBitssignal ROM_Code : ROM_Table_4x8 :=signal ROM_Code : ROM_Table_4x8 := ( "00000000",( "00000000", "11111111","11111111", "01010101","01010101", "10101010" );"10101010" );

相當於:相當於:ROM_Code(0) <= "00000000"; ROM_Code(1) <= "11111111";ROM_Code(0) <= "00000000"; ROM_Code(1) <= "11111111";ROM_Code(2) <= "01010101"; ROM_Code(3) <= "10101010";ROM_Code(2) <= "01010101"; ROM_Code(3) <= "10101010";   signal Get_ROM_Code is array( 7 downto 0) of t_0_to_7;signal Get_ROM_Code is array( 7 downto 0) of t_0_to_7;Get_ROM_Code <= ROM_Code(2); -- Get_ROM_Code <= ROM_Code(2); -- 取得取得 "01010101""01010101"

Page 29: VHDL  硬體描述語言 數位電路設計實務

記錄記錄 (record)(record) 記錄記錄 (record) (record) 是由一個以上相同資料或者是不相同資料所組合是由一個以上相同資料或者是不相同資料所組合

而成的一種資料定義方式。而成的一種資料定義方式。

語法:語法: type record_name is recordtype record_name is record

rec_1 : type;rec_1 : type;

… …

rec_n : type;rec_n : type;

end record;end record;

Page 30: VHDL  硬體描述語言 數位電路設計實務

例如:例如: type X_Y_CHAR is recordtype X_Y_CHAR is record x : integer range 0 to 79;x : integer range 0 to 79; y : integer range 0 to 24;y : integer range 0 to 24; char : character;char : character; end record;end record; variable Position1, Position2 : X_Y_CHAR;variable Position1, Position2 : X_Y_CHAR; variable Position3, Position4 : X_Y_CHAR;variable Position3, Position4 : X_Y_CHAR; Position1.x := 21; Position1.y := 15; Position1.char := ‘V’;Position1.x := 21; Position1.y := 15; Position1.char := ‘V’; Position2.x := 22; Position2.y := 15; Position2.char := ‘H’;Position2.x := 22; Position2.y := 15; Position2.char := ‘H’; Position3.x := 23; Position3.y := 15; Position3.char := ‘D’;Position3.x := 23; Position3.y := 15; Position3.char := ‘D’; Position4.x := 24; Position4.y := 15; Position4.char := ‘L’;Position4.x := 24; Position4.y := 15; Position4.char := ‘L’; 它也等於底下的這種寫法:它也等於底下的這種寫法: Position1 := ( 21, 15, ‘V’ );Position1 := ( 21, 15, ‘V’ ); Position2 := ( 22, 15, ‘H’ );Position2 := ( 22, 15, ‘H’ ); Position3 := ( 23, 15, ‘D’ );Position3 := ( 23, 15, ‘D’ ); Position4 := ( 24, 15, ‘L’ );Position4 := ( 24, 15, ‘L’ );

Page 31: VHDL  硬體描述語言 數位電路設計實務

3-3.4 3-3.4 存取存取 (access)(access) access access 敘述就像是敘述就像是 C C 語言中的指標語言中的指標 (pointer) (pointer) 一樣,用於動態一樣,用於動態

的記憶體配置。的記憶體配置。

對應用於:佇列對應用於:佇列 QueueQueue 、先進先出、先進先出 FIFO FIFO 這一類的資料結構這一類的資料結構模擬是非常好用的。模擬是非常好用的。

不過不過 access access 是不能用於電路合成的。是不能用於電路合成的。

一般會寫在“測試平台”一般會寫在“測試平台” (test bench) (test bench) 的的 .vhd .vhd 檔案中,主要是檔案中,主要是用來產生測試用的輸入訊號以及記錄電路運算過程或結果中用來產生測試用的輸入訊號以及記錄電路運算過程或結果中所成的紀錄。所成的紀錄。

語法:語法: type access_name is access subtype_indication;type access_name is access subtype_indication;

Page 32: VHDL  硬體描述語言 數位電路設計實務

例如:例如:architecture arc of test_access1 isarchitecture arc of test_access1 is

type pointer is access std_logic_vector( 3 downto 0);type pointer is access std_logic_vector( 3 downto 0);

beginbegin

pointer_test:pointer_test:

processprocess

variable ptr : pointer := null;variable ptr : pointer := null;

beginbegin

ptr := new std_logic_vector( 3 downto 0);ptr := new std_logic_vector( 3 downto 0);

ptr.all := ('0', '1', 'X', 'Z');ptr.all := ('0', '1', 'X', 'Z');

wait;wait;

end process;end process;

end arc;end arc;

Page 33: VHDL  硬體描述語言 數位電路設計實務

又例如:又例如:architecture arc of test_access2 isarchitecture arc of test_access2 is type node; -- incomplete type declarationtype node; -- incomplete type declaration type node_ptr is access node;type node_ptr is access node; type color_type is (red, black);type color_type is (red, black); type node is -- binary treetype node is -- binary tree recordrecord key : string( 1 to 2);key : string( 1 to 2); data : integer;data : integer; left : node_ptr;left : node_ptr; right : node_ptr;right : node_ptr; color : color_type;color : color_type; end record;end record;beginbeginpointer_test:pointer_test: processprocess variable root, L1, R1 : node_ptr;variable root, L1, R1 : node_ptr; variable item : node;variable item : node; beginbegin root := new node'("00", 0, null, null, red);root := new node'("00", 0, null, null, red); item := root.all;item := root.all; L1 := new node'("L1", 1, root, null, red);L1 := new node'("L1", 1, root, null, red); R1 := new node'("R1", 1, root, null, black);R1 := new node'("R1", 1, root, null, black); wait;wait; end process;end process;end arc;end arc;

Page 34: VHDL  硬體描述語言 數位電路設計實務

3-3.5 3-3.5 檔案檔案 (File)(File) file file 是不能用於電路合成的。是不能用於電路合成的。

VHDL VHDL 提供的“檔案“機制,可以讀取已存在的檔案,也可以提供的“檔案“機制,可以讀取已存在的檔案,也可以將資訊寫入到檔案中。將資訊寫入到檔案中。

VHDL VHDL 提供的“檔案“機制,一般會寫在“測試平台”提供的“檔案“機制,一般會寫在“測試平台” (test b(test bench) ench) 的的 .vhd .vhd 檔案中,讓您將外界的輸入訊號從檔案內循檔案中,讓您將外界的輸入訊號從檔案內循序地載入到序地載入到 entity entity 的輸入埠中,也可以將電路中的資訊產生的輸入埠中,也可以將電路中的資訊產生到檔案中、方便您除錯到檔案中、方便您除錯 (debug)(debug) 。。

詳細內容請參考:第十一章詳細內容請參考:第十一章 檔案處理與測試平台。檔案處理與測試平台。

Page 35: VHDL  硬體描述語言 數位電路設計實務

3-4 VHDL 3-4 VHDL 的使用者自行定義的型態的使用者自行定義的型態 (User-define type)(User-define type)

它可以讓您自行定義您所需要的列舉型態它可以讓您自行定義您所需要的列舉型態 (enumerated type)(enumerated type) 。語法如下:。語法如下:type type-name is (value-list);type type-name is (value-list);

其中:其中: type … is …type … is …type type 以及以及 is is 關鍵字,一定要有。關鍵字,一定要有。

type-nametype-name

用來描述這個用來描述這個 type type 的名稱。的名稱。

  value-listvalue-list

用來描述『列舉』的值,二個『列舉』的值之間要用分號用來描述『列舉』的值,二個『列舉』的值之間要用分號 (,) (,) 區隔開來。區隔開來。

例如:例如: type Traffic_State is ( RED, GREEN, YELLOW );type Traffic_State is ( RED, GREEN, YELLOW );type mem_t is array (0 to 7) of std_logic_vector(15 downto 0);type mem_t is array (0 to 7) of std_logic_vector(15 downto 0);

Page 36: VHDL  硬體描述語言 數位電路設計實務

3-5 VHDL 3-5 VHDL 的資料子型態的資料子型態 (Sub-type)(Sub-type) 基於基於 VHDLVHDL 的資料型態,可以讓您定義有限制的資料型態,可以讓您定義有限制 (constraint) (constraint) 的資料型態產生的資料型態產生

出資料子型態出資料子型態 (Subtype)(Subtype) 。。

VHDL VHDL 的資料子型態,語法如下:的資料子型態,語法如下:subtype subtype-name is type range n1 to n2;subtype subtype-name is type range n1 to n2;subtype subtype-name is type range n1 downto n2;subtype subtype-name is type range n1 downto n2;

其中:其中:  subtype … is …subtype … is …subtype subtype 以及以及 is is 關鍵字,一定要有。關鍵字,一定要有。

subtype-namesubtype-name

用來描述這個用來描述這個 subtype subtype 的名稱。的名稱。

  rangerange

用來限制這個用來限制這個 subtype subtype 可以表示的範圍。可以表示的範圍。

Page 37: VHDL  硬體描述語言 數位電路設計實務

例如:例如: subtype Integer32Bits is integer range 31 downto 0;subtype Integer32Bits is integer range 31 downto 0;

subtype int_string_buf is string (1 to 80);subtype int_string_buf is string (1 to 80);

subtype Delay_length is Time range 0 fs to Time'High;subtype Delay_length is Time range 0 fs to Time'High;

  

使用資料子型態使用資料子型態 (Subtype) (Subtype) 不能超出所定義的範圍,在作電路不能超出所定義的範圍,在作電路模擬時如果超出所定義的範圍則會停止電路的模擬。模擬時如果超出所定義的範圍則會停止電路的模擬。

在編譯的時候並不會作檢查的。在編譯的時候並不會作檢查的。

Page 38: VHDL  硬體描述語言 數位電路設計實務

3-6 VHDL 3-6 VHDL 的資料表示法的資料表示法

包括:向量包括:向量 (Vectors) (Vectors) 的表示法、陣列的表示法、陣列 (Array) (Array) 的表示法、位元選擇的表示法、位元選擇 (Bit-Selects) (Bit-Selects) 與部份選與部份選擇擇 (Part-Selects) (Part-Selects) 以及串接以及串接 && 。。

Page 39: VHDL  硬體描述語言 數位電路設計實務

3-6.1 3-6.1 向量向量 (vector) (vector) 的表示法的表示法 向量是一個多位元的元件向量是一個多位元的元件 (element)(element) 。。 向量可定義為向量可定義為 (High# downto Low#) (High# downto Low#) 或或 (Low# to Hig(Low# to Hig

h#)h#) 。以。以 (High# downto Low#) (High# downto Low#) 來說,在括號內之來說,在括號內之 dodownto wnto 或者是或者是 to to 左邊的部份為:最高位元左邊的部份為:最高位元 (Most Sign(Most Significant Bit, MSB)ificant Bit, MSB) 、在括號內之、在括號內之 downto downto 或者是或者是 to to 右右邊的部份為邊的部份為 : : 最低位元最低位元 (Least Significant Bit, LSB)(Least Significant Bit, LSB) 。。

如果沒有定義位元的長度、則視為被宣告的變數它的如果沒有定義位元的長度、則視為被宣告的變數它的位元長度為位元長度為 11 ,也就是只有一個位元。,也就是只有一個位元。

我們可以用向量的表示法,來取得向量內之部份訊號。我們可以用向量的表示法,來取得向量內之部份訊號。 例如:例如: signal even : std_logic; -- signal even : std_logic; -- 宣告宣告 1 1 條接線條接線 eveneven

Page 40: VHDL  硬體描述語言 數位電路設計實務
Page 41: VHDL  硬體描述語言 數位電路設計實務

3-6.2 3-6.2 陣列陣列 (array) (array) 的表示法的表示法 「陣列」是由相同型態的元素有順序地結合而成的。「陣列」是由相同型態的元素有順序地結合而成的。 想取得「陣列」中的某個資料要用“索引”想取得「陣列」中的某個資料要用“索引” (array index) (array index) 來達成。來達成。

定義一個陣列叫作定義一個陣列叫作 bytebyte ,它是由,它是由 8 8 個個 std_logic std_logic 元素所組成的、順序是由元素所組成的、順序是由 0 to 70 to 7 。。

type t_byte_0_7 is array (0 to 7) of std_logic;type t_byte_0_7 is array (0 to 7) of std_logic;signal byte : t_byte_0_7;signal byte : t_byte_0_7;signal bit5, bit6 : std_logic;signal bit5, bit6 : std_logic;  byte <= "-----10-"; -- byte <= "-----10-"; -- 放入值放入值bit5 <= byte(5); -- 1bit5 <= byte(5); -- 1 取出值取出值bit6 <= byte(6); -- 0bit6 <= byte(6); -- 0 取出值取出值  byte <= ('1', '1', '1', '1', '1', '1', '1', '1'); -- byte = 11111111byte <= ('1', '1', '1', '1', '1', '1', '1', '1'); -- byte = 11111111byte <= ( others => '0' ); -- byte = 00000000byte <= ( others => '0' ); -- byte = 00000000byte <= ( 0 => '0', 5 => '0', 6 => '0', others => '1'); -- "01111001"byte <= ( 0 => '0', 5 => '0', 6 => '0', others => '1'); -- "01111001"byte <= ( 6 => '0', 5 => '0', 0 => '0', others => '1'); -- "01111001"byte <= ( 6 => '0', 5 => '0', 0 => '0', others => '1'); -- "01111001"

Page 42: VHDL  硬體描述語言 數位電路設計實務

先定義一個陣列叫作先定義一個陣列叫作 wordword ,它是由,它是由 Word_Len (32) Word_Len (32) 個個 std_logic std_logic 元素所組元素所組成的、順序是由成的、順序是由 Word_Len-1 (32-1) down to 0Word_Len-1 (32-1) down to 0 。;接著再定義一個陣列。;接著再定義一個陣列叫作叫作 reg_filereg_file ,它是由,它是由 8 8 個剛才才定義好的個剛才才定義好的 word word 元素所組成的、順序元素所組成的、順序是由是由 1 down to 81 down to 8 。。

constant Word_Len : integer := 32;constant Word_Len : integer := 32;

type word is array (Word_Len -1 downto 0) of std_logic;type word is array (Word_Len -1 downto 0) of std_logic;

type reg_file is array (1 to 128) of word;type reg_file is array (1 to 128) of word;

  

signal memory_128_m_16 : reg_file;signal memory_128_m_16 : reg_file;

signal reg_file_10 : word;signal reg_file_10 : word;

  

memory_128_m_16(10) <= "0101010101010101"; -- memory_128_m_16(10) <= "0101010101010101"; -- 放入值放入值reg_file_10 <= memory_128_m_16(10); -- reg_file_10 <= memory_128_m_16(10); -- 取出值取出值

Page 43: VHDL  硬體描述語言 數位電路設計實務

10241024 個個 1 Bit 1 Bit 的暫存器空間的暫存器空間 type t_Memory1Bit is array (1023 downto 0) of std_logic;type t_Memory1Bit is array (1023 downto 0) of std_logic;signal Memory1Bit : t_Memory1Bit;signal Memory1Bit : t_Memory1Bit;

10241024 個個 1 Byte (8 Bits) 1 Byte (8 Bits) 的暫存器空間的暫存器空間 type t_byte_7_0 is array (7 downto 0) of std_logic;type t_byte_7_0 is array (7 downto 0) of std_logic;signal Byte1, Byte2 : t_byte_7_0; -- 8 Bitssignal Byte1, Byte2 : t_byte_7_0; -- 8 Bitstype t_Memory1Byte is array (1023 downto 0) of t_byte_7_0;type t_Memory1Byte is array (1023 downto 0) of t_byte_7_0;signal Memory1Byte : t_Memory1Byte; -- 1024 * 8signal Memory1Byte : t_Memory1Byte; -- 1024 * 8signal Bit1, Bit2 : std_logic;signal Bit1, Bit2 : std_logic;  -- -- 提取位址為提取位址為 321 321 的的 1 Bit 1 Bit 的暫存器給的暫存器給 Bit1Bit1Bit1 <= Memory1Bit(321);Bit1 <= Memory1Bit(321);-- -- 將將 Byte1 Byte1 的值存入位址為的值存入位址為 586 586 的的 Memory1Byte Memory1Byte 中中Memory1Byte(586) <= Byte1;Memory1Byte(586) <= Byte1;  -- -- 提取在位址為提取在位址為 125 125 的的 Memory1Byte Memory1Byte 中之內容的第中之內容的第 44 個位元給個位元給 Bit2Bit2-- -- 必需藉由二次提取的方式才可以完成必需藉由二次提取的方式才可以完成Byte2 <= Memory1Byte(125); -- Byte2 <= Memory1Byte(125); -- 首先:提取位址為首先:提取位址為 Memory1Byte(125)Memory1Byte(125)Bit2 <= Byte2(4); -- Bit2 <= Byte2(4); -- 然後:提出第然後:提出第 44 個位元個位元

Page 44: VHDL  硬體描述語言 數位電路設計實務

3-6.33-6.3 位元選擇位元選擇 (Bit select) (Bit select) 與部份選擇與部份選擇 (partial selec(partial select)t) 、串接、串接 & & 以及聚集以及聚集 (Aggregate)(Aggregate)

位元選擇位元選擇 (Bit-Selects) (Bit-Selects) 與與 部份選擇部份選擇 (Part-Selects) (Part-Selects) 是指選擇某是指選擇某個由向量個由向量 (Vectors) (Vectors) 表示法或者是陣列表示法或者是陣列 (Arrays) (Arrays) 表示法的變表示法的變數之某些特定的部份。數之某些特定的部份。

位元選擇位元選擇 (Bit-Selects)(Bit-Selects)

位元選擇是選擇來自於某個訊號或某個訊號中的單位元選擇是選擇來自於某個訊號或某個訊號中的單 11 個位元。個位元。例如:例如:

signal a, b, c : std_logic_vector( 7 downto 0);signal a, b, c : std_logic_vector( 7 downto 0);

c(5) <= a(3) and b(4); -- c(5) <= a(3) and b(4); -- 將將 a(3) a(3) 和和 b(4) b(4) 作作 and and 之後之後 , , 給給 c(6)c(6) 部份選擇部份選擇 (Part-Selects)(Part-Selects)

部份選擇是選擇來自於某個訊號中的的一群位元。例如:部份選擇是選擇來自於某個訊號中的的一群位元。例如:signal d : std_logic_vector( 3 downto 0);signal d : std_logic_vector( 3 downto 0);

d(3 downto 0) <= a(7 downto 4) and b(3 downto 0);d(3 downto 0) <= a(7 downto 4) and b(3 downto 0);

Page 45: VHDL  硬體描述語言 數位電路設計實務

signal Data_0_3 : STD_LOGIC_VECTOR( 0 to 3) := "0011";signal Data_0_3 : STD_LOGIC_VECTOR( 0 to 3) := "0011";

signal Data_7_4 : STD_LOGIC_VECTOR( 7 downto 4);signal Data_7_4 : STD_LOGIC_VECTOR( 7 downto 4);

-- "0011" "0011"-- "0011" "0011"

Data_7_4(7 downto 4) <= Data_0_3(0 to 3);Data_7_4(7 downto 4) <= Data_0_3(0 to 3);

-- -- 相當於相當於Data_7_4(7) <= Data_0_3(0); -- '0'Data_7_4(7) <= Data_0_3(0); -- '0'

Data_7_4(6) <= Data_0_3(1); -- '0'Data_7_4(6) <= Data_0_3(1); -- '0'

Data_7_4(5) <= Data_0_3(2); -- '1'Data_7_4(5) <= Data_0_3(2); -- '1'

Data_7_4(4) <= Data_0_3(3); -- '1'Data_7_4(4) <= Data_0_3(3); -- '1'

Page 46: VHDL  硬體描述語言 數位電路設計實務

訊號的串接訊號的串接

將二個或以上的訊號串接起來給另一個長度等於將被串接訊號將二個或以上的訊號串接起來給另一個長度等於將被串接訊號總和的訊號。例如:總和的訊號。例如:

signal a, b, c : std_logic_vector( 7 downto 0);signal a, b, c : std_logic_vector( 7 downto 0);

c(7 downto 0) <= b(3 downto 0) & b(7 downto 4);c(7 downto 0) <= b(3 downto 0) & b(7 downto 4);

c <= b(3 downto 0) & b(7 downto 4);c <= b(3 downto 0) & b(7 downto 4);

-- -- 相當於相當於 c(7) <= b(3) c(6) <= b(2) c(5) <= b(1) c(4) <= b(0)c(7) <= b(3) c(6) <= b(2) c(5) <= b(1) c(4) <= b(0)

-- -- 相當於相當於 c(3) <= b(7) c(2) <= b(6) c(1) <= b(5) c(0) <= b(4)c(3) <= b(7) c(2) <= b(6) c(1) <= b(5) c(0) <= b(4)

Page 47: VHDL  硬體描述語言 數位電路設計實務

signal databus : std_logic_vector( 15 downto 0);signal databus : std_logic_vector( 15 downto 0);signal rev_databus : std_logic_vector( 0 to 15);signal rev_databus : std_logic_vector( 0 to 15);-- databus(15:0) -- databus(15:0) 存放到存放到 rev_databus(0:15)rev_databus(0:15)rev_databus( 0 to 15) <= databus( 15 downto 0);rev_databus( 0 to 15) <= databus( 15 downto 0);-- -- 相當於相當於 rev_databus(0) <= databus(15);rev_databus(0) <= databus(15);-- rev_databus(1) <= databus(14);-- rev_databus(1) <= databus(14);-- ……-- ……-- rev_databus(14) <= databus(1);-- rev_databus(14) <= databus(1);-- rev_databus(15) <= databus(10);-- rev_databus(15) <= databus(10);  op_addr_data(31 downto 26) <= opcode; -- op_addr_data(31 downto 26) <= opcode; -- 存放到存放到 31:2631:26op_addr_data(25 downto 16) <= addrbus; -- op_addr_data(25 downto 16) <= addrbus; -- 存放到存放到 25:1625:16op_addr_data(15 downto 0) <= databus; -- op_addr_data(15 downto 0) <= databus; -- 存放到存放到 15: 015: 0

更簡單的寫法為更簡單的寫法為 op_addr_data <= opcode & addrbus & databus;op_addr_data <= opcode & addrbus & databus;

Page 48: VHDL  硬體描述語言 數位電路設計實務

聚集聚集 (Aggregate)(Aggregate)

VHDL VHDL 的聚集的聚集 (Aggregate) (Aggregate) 是指將聚集的資料分散給各個獨立是指將聚集的資料分散給各個獨立的訊號或者是變數,另一種情形是將各個獨立的訊號或者是的訊號或者是變數,另一種情形是將各個獨立的訊號或者是變數的資料群聚起來給某一個訊號。變數的資料群聚起來給某一個訊號。

分散給各個獨立的訊號或者是變數的例子:分散給各個獨立的訊號或者是變數的例子:CONSTANT IS_1 : UNSIGNED(7 downto 0) := ( 0 => '1', others =CONSTANT IS_1 : UNSIGNED(7 downto 0) := ( 0 => '1', others =

> '0');> '0');

IS_1 IS_1 會等於會等於 "00000001" "00000001" 共共 88 個位元個位元  

signal SL_1, SL_2, SL_3 : STD_LOGIC;signal SL_1, SL_2, SL_3 : STD_LOGIC;

-- '1' '0' 'Z'-- '1' '0' 'Z'

(SL_1, SL_2, SL_3) <= UNSIGNED'("10Z"); -- (SL_1, SL_2, SL_3) <= UNSIGNED'("10Z"); -- 分散給各個獨立分散給各個獨立的訊號的訊號

Page 49: VHDL  硬體描述語言 數位電路設計實務

分散給各個獨立的訊號或者是變數的例子:分散給各個獨立的訊號或者是變數的例子:signal BIT_DATA1 : BIT_VECTOR( 7 downto 0);signal BIT_DATA1 : BIT_VECTOR( 7 downto 0);signal BIT_DATA2 : BIT_VECTOR( 3 downto 0);signal BIT_DATA2 : BIT_VECTOR( 3 downto 0);signal A_BIT : BIT := ‘0’;signal A_BIT : BIT := ‘0’;signal B_BIT : BIT := ‘1’;signal B_BIT : BIT := ‘1’;signal C_BIT : BIT := ‘0’;signal C_BIT : BIT := ‘0’;  -- BIT_DATA1 = “00010000”-- BIT_DATA1 = “00010000”BIT_DATA1 <= (7 downto 5 => '0', 4 => '1', 3 => A_BIT, others =BIT_DATA1 <= (7 downto 5 => '0', 4 => '1', 3 => A_BIT, others =

> '0');> '0');  -- BIT_DATA2 = “0100”-- BIT_DATA2 = “0100”BIT_DATA2 <= ( A_BIT, B_BIT, A_BIT, C_BIT );BIT_DATA2 <= ( A_BIT, B_BIT, A_BIT, C_BIT );

Page 50: VHDL  硬體描述語言 數位電路設計實務

3-7 constant 3-7 constant 常數常數 用來描述“常數”,跟用來描述“常數”,跟 C C 語言的語言的 #define #define 或者是或者是 Verilog Verilog 的的 parameter parameter 一樣,一樣,目的在於讓目的在於讓 VHDL VHDL 程式碼比較讓人容易看得懂、以後容易除錯以及維。程式碼比較讓人容易看得懂、以後容易除錯以及維。

語法:語法: constant constant_name : type [:= constant_expression;];constant constant_name : type [:= constant_expression;];

例如:例如:constant BUS_SIZE, DATA_SIZE : integer := 32; -- constant BUS_SIZE, DATA_SIZE : integer := 32; -- 匯流排的寬度匯流排的寬度constant MSB : integer := BUS_SIZE - 1; -- constant MSB : integer := BUS_SIZE - 1; -- 匯流排的最高位元匯流排的最高位元constant Max_Adder_Bits : integer := 8;constant Max_Adder_Bits : integer := 8;constant Max_int : integer := 2 ** (Max_Adder_Bits - 1) - 1;constant Max_int : integer := 2 ** (Max_Adder_Bits - 1) - 1;constant RED_Light_Time : std_logic_vector( 3 downto 0) :=constant RED_Light_Time : std_logic_vector( 3 downto 0) := conv_std_logic_vector( 9, 5)( 3 downto 0); -- 9 conv_std_logic_vector( 9, 5)( 3 downto 0); -- 9 紅燈亮的時間紅燈亮的時間constant PI : real := 3.141592653589716;constant PI : real := 3.141592653589716;constant Half_PI : real := Pi / 2.0;constant Half_PI : real := Pi / 2.0;constant ST0 : std_logic_vector( 2 downto 0) := "001";constant ST0 : std_logic_vector( 2 downto 0) := "001";constant Result_File : string := "./Result.txt";constant Result_File : string := "./Result.txt";constant Cycle_Time : time := 20 ns; -- 50.0 MHzconstant Cycle_Time : time := 20 ns; -- 50.0 MHzconstant Half_Cycle_time : time := Cycle_Time / 2.0;constant Half_Cycle_time : time := Cycle_Time / 2.0;

Page 51: VHDL  硬體描述語言 數位電路設計實務

3-8 alias 3-8 alias 別名別名 在電路中如果有個在電路中如果有個 bit_vector bit_vector 或者是或者是 std_logic_vector std_logic_vector 之類的訊號或者是變數需之類的訊號或者是變數需

要劃分成不同的訊號名稱,這時候我們就可以用要劃分成不同的訊號名稱,這時候我們就可以用 alias alias 敘述來達成。新產生出敘述來達成。新產生出來的“別名”並不會佔用電路的面積。來的“別名”並不會佔用電路的面積。

語法:語法: valias alias_name is existing_type;valias alias_name is existing_type;

例如:例如:alias mantissa : std_logic_vector( 23 downto 0) is real( 8 to 31);alias mantissa : std_logic_vector( 23 downto 0) is real( 8 to 31);alias exponent : std_logic_vector( 7 downto 0) is real( 0 to 7);alias exponent : std_logic_vector( 7 downto 0) is real( 0 to 7);

又例如:又例如:signal instruction : std_logic_vector ( 19 downto 0);signal instruction : std_logic_vector ( 19 downto 0);alias opcode : std_logic_vector ( 3 downto 0) is instruction ( 19 downto 16);alias opcode : std_logic_vector ( 3 downto 0) is instruction ( 19 downto 16);alias data1 : std_logic_vector ( 7 downto 0) is instruction ( 15 downto 8);alias data1 : std_logic_vector ( 7 downto 0) is instruction ( 15 downto 8);alias data2 : std_logic_vector ( 7 downto 0) is instruction ( 7 downto 0);alias data2 : std_logic_vector ( 7 downto 0) is instruction ( 7 downto 0);

Page 52: VHDL  硬體描述語言 數位電路設計實務

如果改成底下的寫法,則會另外產生如果改成底下的寫法,則會另外產生 24 24 個暫存器,產生出額外的電路,讓個暫存器,產生出額外的電路,讓合成出來的電路面積增大。合成出來的電路面積增大。

signal instruction : std_logic_vector ( 19 downto 0);signal instruction : std_logic_vector ( 19 downto 0);signal opcode : std_logic_vector ( 3 downto 0);signal opcode : std_logic_vector ( 3 downto 0);signal data1 : std_logic_vector ( 7 downto 0);signal data1 : std_logic_vector ( 7 downto 0);signal data2 : std_logic_vector ( 7 downto 0);signal data2 : std_logic_vector ( 7 downto 0);  opcode <= instruction ( 19 downto 16);opcode <= instruction ( 19 downto 16);data1 <= instruction ( 15 downto 8);data1 <= instruction ( 15 downto 8);data2 <= instruction ( 7 downto 0);data2 <= instruction ( 7 downto 0);