1 / 52

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

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

Télécharger la présentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VHDL 硬體描述語言數位電路設計實務 第三章 VHDL 的語法協定以及資料型態

  2. 3-1 VHDL 的語法協定 以及如何寫出易讀的程式碼 • 3-1.1 VHDL 的語法協定 VHDL 是由一連串的標記 (token) 所組成,這些標記可以是:空白 (Whitespace)、註解 (Comment)、運算子 (Operator)、數值 (Number)、識別字 (Identifier) 與關鍵字 (Keyword) 以及底線 (Underscore character) 和問號 (Question Mark)。 • VHDL 是一種“大小寫”沒分別的硬體描述語言 • 識別字 (Identifier) • 可以適當地加入空白和換行 • 所有的關鍵字 (keyword) 名稱必須使用小寫來表示 • 註解 (Comment) • 運算子 (Operator) • 單元 (Unary) 運算子 /二元 (Binary) 運算子 • 數值 (Number) • 關鍵字 (Keyword) • 底線 (Underscore characters) 和問號 (Question Marks)

  3. 3-1.2 如何寫出易讀的程式碼 • 用內縮(ident) 的方式去對齊相對應的敘述 • 用空行(空列才對) 的方式來區分出entity、architecture、process …等等 • 用空格(空白) 對齊 • 將很長的一行(一列才對) 分成二行(二列才對以上來寫) • 為process、function 以及procedure 加上標記(label)

  4. 3-2 VHDL 的標準邏輯值(Standard Logic Value) 標準邏輯值是定義在IEEE std_logic_1164 這個package 裡頭。 要用use 這個敘述才能使用IEEE std_logic_1164 所定義在的標準邏輯值。 library IEEE; use IEEE.std_logic_1164.all; library 名稱 package 名稱 要使用 package 裡頭所有的定義

  5. 標準邏輯值的定義在stdlogic.vhd 檔案,std_logic_1164套件中,如下: Type STD_ULOGIC is ( ‘U’, -- Uninitialized ‘X’, -- Forcing Unknown ‘0’, -- Forcing 0 ‘1’, -- Forcing 1 ‘Z’, -- High Impedance ‘W’, -- Weak Unknown ‘L’, -- Weak 0 ‘H’, -- Weak 1 ‘-’, -- Don’t care ); -- industry standard logic type subtype std_logic is resolved STD_ULOGIC; -- unconstrained array of std_logic for use in declaring signal arrays type std_logic_vector is array ( natural range <>) of std_logic;

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

  7. resolution function function resolved ( s : std_ulogic_vector ) return std_ulogic is variable result : std_ulogic := 'z'; -- weakest state default begin -- the test for a single driver is essential otherwise the -- loop would return 'x' for a single driver of '-' and that -- would conflict with the value of a single driver unresolved -- signal. if (s'length = 1) then return s(s'low); else for i in s'range loop result := resolution_table(result, s(i)); end loop; end if; return result; end resolved;

  8. std_logic

  9. std_logic_vector 標準邏輯值向量,可以存放一個以上VHDL 所提供的標準邏輯值。 例如:

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

  11. 3-3.1 type 以及 subtype 敘述 • type 敘述用來宣告一種新的資料型態。 語法:type type_name is type_definition; 例如:底下這一節“純量(Scalar)”中的“整數(integer)”。 type integer is range -2147483648 to 2147483647; integer 整數,可以儲存的資料範圍從-2147483648到2147483647。 又例如: type byte is array ( 7 downto 0) of bit; type word is array ( 15 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 unlimite_memory is array ( integer range <>) of dboule_word; type munlimite_atrix is array ( integer range <>, integer range <>) of byte;

  12. 可以運用subtype 敘述,將現存的資料型態來產生一種較“原先該資料型態”可以表示資料範圍還小的一種“新的資料型態” 語法:subtype subtype_name is subtype_indication; 例如:底下這一節“純量(Scalar)”中的“整數(integer)”之natural 自然數。 subtype natural is integer range 0 to integer'High; natural 自然數,可以儲存的資料範圍從0到2147483647。 subtype SMALL_INT is INTEGER range 0 to 1; -- 可以儲存0或1 又例如:物理量(Physical) 分類中的time type time is range -2147483647 to 2147483647 units … end units; subtype delay_length is time range 0 fs to time'high;

  13. 又例如: subtype my_integer is integer range -128 to 127; variable a_my_integer : my_integer := 43; subtype my_name is string( 1 to 31) ; variable a_name : my_name := "Johnson Cheng"; subtype word is std_logic_vector( 15 downto 0); signal word_h : word := x"FFFF"; signal word_d : word := conv_std_logic_vector( 65535, 17)( 15 downto 0); -- o"177777" ModelSim 5.6b compile 有錯, 只好分二段給 signal word_o : word := '1' & o"77777"; -- String length is 18. Expected length is 16 -- signal word_o2 : word := o"177777"; signal word_b : word := b"1111111111111111"; 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 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')

  14. 3-3.2 純量(Scalar) 純量(Scalar) 資料型態,包括:整數(integer)、實數(real)、列舉(enumerated) 以及物理量(Physical)。 其它還有:bit、boolean、bit_vector、character、string,以及time。 • 整數(integer) type integer is range -2147483648 to 2147483647; 整數,可以儲存的資料範圍從–231 到231 - 1。例如: signal Integer_32Bits : integer; -- 需要32個位元 -- 加上range 識別字讓儲存範圍可以限定 signal unsigned_int : integer range 0 to 15; -- 0 ~ 15 無號數整數 signal signed_int : integer range -8 to 7; -- -8 ~ 7 有號數整數 signal Table_Index : integer range 1 to 30; -- 1 ~ 31, 只需要5個位元 variable value_8bits : integer range 0 to 255; -- 0 ~ 255, 只需要8個位元

  15. 整數(integer) 給值的方式 • 語法:radix#number_in_radix# 其中radix 是基底,可以是2 ~ 16其中一種,各代表2進制值~ 16進制值的表示方法。 例如: signal INT_2_Data : INTEGER range 0 to 3 := 2#10#; -- 2 signal INT_3_Data : INTEGER range 0 to 31 := 3#210#; -- 21 signal INT_4_Data : INTEGER range 0 to 255 := 4#3210#; -- 228 signal INT_5_Data : INTEGER := 5#43210#; -- 2930 signal INT_6_Data : INTEGER := 6#543210#; -- 44790

  16. 實數(real) type real is range -1.0E308 to 1.0E308; 實數,可以儲存的資料範圍從–1*10308 到1*10308。例如: signal Tousand : real := 1.0E3; variable PI : real := 3.14; variable rval : real; variable int_val : integer:=20; variable sign : integer := -1.0; rval := rval*(10.0**(int_val * sign)); -- rval * 10-20 rval := 3; -- 錯誤的寫法,要改成3.0 才對

  17. 實數(real) 給值的方式 語法:radix#number_in_radix# 其中radix 是基底,同樣可以是2 ~ 16其中一種,各代表2進制值~ 16進制值的表示方法。 例如: signal REAL_2_Data : REAL := 2#10.01#; -- 2.25 signal REAL_3_Data : REAL := 3#210.01#; -- 21.1111 signal REAL_4_Data : REAL := 4#3210.01#; -- 228.063 signal REAL_5_Data : REAL := 5#43210.01#; -- 2930.04 …… signal REAL_15_Data : REAL := 15#EDCBA9876543210.01#; -- 4.3566e+017 signal REAL_16_Data : REAL := 16#FEDCBA9876543210.01#; -- 1.83648e+019 一般來說“合成軟體”並不支援實數 (real) 這種資料型態,但是可以用在電路的模擬。

  18. 物理量(Physical) 分類中的time type time is range -2147483647 to 2147483647 units fs; ps = 1000 fs; -- 1 ps等於1000 fs ns = 1000 ps; -- 1 ns等於1000 ps us = 1000 ns; -- 1 us等於1000 ns ms = 1000 us; -- 1 ms等於1000 us sec = 1000 ms; -- 1秒等於1000 ms min = 60 sec; -- 1分鐘等於60秒 hr = 60 min; -- 1小時等於60分鐘 end units; 它是屬由:物理(Physical) 量這個分類。 一般來說“合成軟體”並不支援物理量(Physical) 分類中的time 這種資料型態,但是可以用在電路的模擬。

  19. 列舉(enumerated) 列舉,將數值依序列出的一種資料定義方式。 語法:type type_name is ( enum0, enum1, … ) 例如: type Bit_UX01Z is ( 'U', 'X', '0', '1', 'Z' ); 在3-2 VHDL 的標準邏輯值(Standard Logic Value),std_ulogic 的定義也是用列舉的方式定出來的。 type std_ulogic is ( ’U’, ‘X’, ‘0’, ‘1’, ‘Z’, ‘W’, ‘L’, ‘H’, ‘-‘ ); type My_state is ( ST0, ST1, ST2, ST3, ST4 ); signal State : My_state; 其實它等於底下的這種寫法: constant ST0 : std_logic_vector( 2 downto 0) := "001"; constant ST1 : std_logic_vector( 2 downto 0) := "010"; constant ST2 : std_logic_vector( 2 downto 0) := "011"; constant ST3 : std_logic_vector( 2 downto 0) := "100"; constant ST4 : std_logic_vector( 2 downto 0) := "101"; signal State : std_logic_vector( 2 downto 0);

  20. boolean 布林值,只能放true 或者是false。 u例 1 type boolean is (false, true); signal a, b : boolean; a <= true; b <= false;

  21. u例 2 function ToSig (bool : Boolean ) return std_logic is variable sig : std_logic; begin if (bool) then sig := '1'; else sig := '0'; end if; return sig; end ToSig; “合成軟體“一般來說會將 boolean 轉換成 std_logic,例如: signal a, b : boolean; 相等於 signal a, b : std_logic;

  22. bit type bit is ('0', '1'); 一個位元的邏值值,只能放‘1’或者是‘0’。例如: signal d, e : bit; d <= ‘1’; e <= ‘0’; “合成軟體“一般來說會將bit 轉換成std_logic,例如: signal d, e : bit; 相等於signal d, e : std_logic;

  23. bit_vector type bit_vector is array (natural range <>) of bit; bit_vector 是由bit 所組成的。例如: signal value : bit_vector(3 downto 0) := "0100"; “合成軟體“一般來說會將bit_vector 轉換成std_logic_vector,例如: signal value : bit_vector(3 downto 0) := "0100"; 相等於signal value : std_logic_vector( 3 downto 0) := "0100";

  24. character type character is ( nul, soh, stx, etx, eot, enq, ack, bel, bs, ht, lf, vt, ff, cr, so, si, dle, dc1, dc2, dc3, dc4, nak, syn, etb, can, em, sub, esc, fsp, gsp, rsp, usp, ' ', '!', '"', '#', '$', '%', '&', ''', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', del, -- 字元128 到255 不在ASCII 表的範圍中,印不出來 ); character 字元,可以儲存一個字元(character)。例如: variable c : character := ‘A’;

  25. 將std_logic 轉換成character type stdlogic_to_char_t is array( std_logic ) of character; constant to_char : stdlogic_to_char_t := ( 'U' => 'U', 'X' => 'X', '0' => '0', '1' => '1', 'Z' => 'Z', 'W' => 'W', 'L' => 'L', 'H' => 'H', '-' => '-' ); function lower_case(c : character) return character is begin if c >= 'A' and c <= 'Z' then return character'val(character'pos(c) + 32); else return c; end if; end; 一般來說“合成軟體”並不支援 character 字元這種資料型態,但是可以用在電路的模擬。

  26. string type string is array (positive range <>) of character; string 字串,可以儲存的多個字元(character)。例如: constant Result_File : string := "./Result.txt"; constant Source_File : string := string'( "Source_File.dat" ); variable I_am_str : string(1 to 80); I_am_str := “Hello”; Write ( OutString, String'( I_am_str ) ); 一般來說“合成軟體”並不支援string 字串這種資料型態,但是可以用在電路的模擬。

  27. 3-3.3 合成(Composite) 合成(Composite) 資料型態,包括:陣列(array) 以及記錄(record)。 陣列(array) 陣列(array) 是由一個以上相同資料所組合而成的一種資料定義方式。 語法:type array_name is array( n1 to n2 ) 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 WORD is array( 15 downto 0) of std_logic; type DWORD is array( 31 downto 0) of std_logic; signal byte_1 : BYTE;

  28. 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; -- ROM_Code 有4 個ROM Code, 且每個Register 擁有8 個Bits signal ROM_Code : ROM_Table_4x8 := ( "00000000", "11111111", "01010101", "10101010" ); 相當於: ROM_Code(0) <= "00000000"; ROM_Code(1) <= "11111111"; ROM_Code(2) <= "01010101"; ROM_Code(3) <= "10101010"; signal Get_ROM_Code is array( 7 downto 0) of t_0_to_7; Get_ROM_Code <= ROM_Code(2); -- 取得"01010101"

  29. 記錄(record) 記錄(record) 是由一個以上相同資料或者是不相同資料所組合而成的一種資料定義方式。 語法:type record_name is record rec_1 : type; … rec_n : type; end record;

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

  31. 3-3.4 存取(access) access 敘述就像是C 語言中的指標(pointer) 一樣,用於動態的記憶體配置。 對應用於:佇列Queue、先進先出FIFO 這一類的資料結構模擬是非常好用的。 不過access 是不能用於電路合成的。 一般會寫在“測試平台”(test bench) 的.vhd 檔案中,主要是用來產生測試用的輸入訊號以及記錄電路運算過程或結果中所成的紀錄。 語法:type access_name is access subtype_indication;

  32. 例如: architecture arc of test_access1 is type pointer is access std_logic_vector( 3 downto 0); begin pointer_test: process variable ptr : pointer := null; begin ptr := new std_logic_vector( 3 downto 0); ptr.all := ('0', '1', 'X', 'Z'); wait; end process; end arc;

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

  34. 3-3.5 檔案(File) file 是不能用於電路合成的。 VHDL 提供的“檔案“機制,可以讀取已存在的檔案,也可以將資訊寫入到檔案中。 VHDL 提供的“檔案“機制,一般會寫在“測試平台”(test bench) 的.vhd 檔案中,讓您將外界的輸入訊號從檔案內循序地載入到entity 的輸入埠中,也可以將電路中的資訊產生到檔案中、方便您除錯(debug)。 詳細內容請參考:第十一章檔案處理與測試平台。

  35. 3-4 VHDL 的使用者自行定義的型態(User-define type) 它可以讓您自行定義您所需要的列舉型態(enumerated type)。語法如下: type type-name is (value-list); 其中: l type … is … type 以及is 關鍵字,一定要有。 l type-name 用來描述這個type 的名稱。 lvalue-list 用來描述『列舉』的值,二個『列舉』的值之間要用分號(,) 區隔開來。 例如:type Traffic_State is ( RED, GREEN, YELLOW ); type mem_t is array (0 to 7) of std_logic_vector(15 downto 0);

  36. 3-5 VHDL 的資料子型態(Sub-type) 基於VHDL的資料型態,可以讓您定義有限制(constraint) 的資料型態產生出資料子型態(Subtype)。 VHDL 的資料子型態,語法如下: subtype subtype-name is type range n1 to n2; subtype subtype-name is type range n1 downto n2; 其中: lsubtype … is … subtype 以及is 關鍵字,一定要有。 l subtype-name 用來描述這個subtype 的名稱。 lrange 用來限制這個subtype 可以表示的範圍。

  37. 例如:subtype Integer32Bits is integer range 31 downto 0; subtype int_string_buf is string (1 to 80); subtype Delay_length is Time range 0 fs to Time'High; 使用資料子型態(Subtype) 不能超出所定義的範圍,在作電路模擬時如果超出所定義的範圍則會停止電路的模擬。 在編譯的時候並不會作檢查的。

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

  39. 3-6.1 向量(vector) 的表示法 • 向量是一個多位元的元件(element)。 • 向量可定義為(High# downto Low#) 或(Low# to High#)。以(High# downto Low#) 來說,在括號內之downto 或者是to 左邊的部份為:最高位元(Most Significant Bit, MSB)、在括號內之downto 或者是to 右邊的部份為: 最低位元(Least Significant Bit, LSB)。 • 如果沒有定義位元的長度、則視為被宣告的變數它的位元長度為1,也就是只有一個位元。 • 我們可以用向量的表示法,來取得向量內之部份訊號。 例如:signal even : std_logic; -- 宣告1 條接線even

  40. 3-6.2 陣列(array) 的表示法 「陣列」是由相同型態的元素有順序地結合而成的。 想取得「陣列」中的某個資料要用“索引”(array index) 來達成。 定義一個陣列叫作byte,它是由8 個std_logic 元素所組成的、順序是由0 to 7。 type t_byte_0_7 is array (0 to 7) of std_logic; signal byte : t_byte_0_7; signal bit5, bit6 : std_logic; byte <= "-----10-"; -- 放入值 bit5 <= byte(5); -- 1取出值 bit6 <= byte(6); -- 0取出值 byte <= ('1', '1', '1', '1', '1', '1', '1', '1'); -- byte = 11111111 byte <= ( others => '0' ); -- byte = 00000000 byte <= ( 0 => '0', 5 => '0', 6 => '0', others => '1'); -- "01111001" byte <= ( 6 => '0', 5 => '0', 0 => '0', others => '1'); -- "01111001"

  41. 先定義一個陣列叫作word,它是由Word_Len (32) 個std_logic 元素所組成的、順序是由Word_Len-1 (32-1) down to 0。;接著再定義一個陣列叫作reg_file,它是由8 個剛才才定義好的word 元素所組成的、順序是由1 down to 8。 constant Word_Len : integer := 32; type word is array (Word_Len -1 downto 0) of std_logic; type reg_file is array (1 to 128) of word; signal memory_128_m_16 : reg_file; signal reg_file_10 : word; memory_128_m_16(10) <= "0101010101010101"; -- 放入值 reg_file_10 <= memory_128_m_16(10); -- 取出值

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

  43. 3-6.3位元選擇(Bit select) 與部份選擇(partial select)、串接& 以及聚集(Aggregate) 位元選擇(Bit-Selects) 與部份選擇(Part-Selects) 是指選擇某個由向量(Vectors) 表示法或者是陣列(Arrays) 表示法的變數之某些特定的部份。 • 位元選擇(Bit-Selects) 位元選擇是選擇來自於某個訊號或某個訊號中的單1個位元。例如: signal a, b, c : std_logic_vector( 7 downto 0); c(5) <= a(3) and b(4); -- 將a(3) 和b(4) 作and 之後, 給c(6) • 部份選擇(Part-Selects) 部份選擇是選擇來自於某個訊號中的的一群位元。例如: signal d : std_logic_vector( 3 downto 0); d(3 downto 0) <= a(7 downto 4) and b(3 downto 0);

  44. signal Data_0_3 : STD_LOGIC_VECTOR( 0 to 3) := "0011"; signal Data_7_4 : STD_LOGIC_VECTOR( 7 downto 4); -- "0011" "0011" Data_7_4(7 downto 4) <= Data_0_3(0 to 3); -- 相當於 Data_7_4(7) <= Data_0_3(0); -- '0' Data_7_4(6) <= Data_0_3(1); -- '0' Data_7_4(5) <= Data_0_3(2); -- '1' Data_7_4(4) <= Data_0_3(3); -- '1'

  45. 訊號的串接 將二個或以上的訊號串接起來給另一個長度等於將被串接訊號總和的訊號。例如: signal a, b, c : std_logic_vector( 7 downto 0); c(7 downto 0) <= 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(3) <= b(7) c(2) <= b(6) c(1) <= b(5) c(0) <= b(4)

  46. signal databus : std_logic_vector( 15 downto 0); signal rev_databus : std_logic_vector( 0 to 15); -- databus(15:0) 存放到rev_databus(0:15) rev_databus( 0 to 15) <= databus( 15 downto 0); -- 相當於rev_databus(0) <= databus(15); -- rev_databus(1) <= databus(14); -- …… -- rev_databus(14) <= databus(1); -- rev_databus(15) <= databus(10); op_addr_data(31 downto 26) <= opcode; -- 存放到31:26 op_addr_data(25 downto 16) <= addrbus; -- 存放到25:16 op_addr_data(15 downto 0) <= databus; -- 存放到15: 0 更簡單的寫法為op_addr_data <= opcode & addrbus & databus;

  47. 聚集(Aggregate) VHDL 的聚集(Aggregate) 是指將聚集的資料分散給各個獨立的訊號或者是變數,另一種情形是將各個獨立的訊號或者是變數的資料群聚起來給某一個訊號。 分散給各個獨立的訊號或者是變數的例子: CONSTANT IS_1 : UNSIGNED(7 downto 0) := ( 0 => '1', others => '0'); IS_1 會等於"00000001" 共8個位元 signal SL_1, SL_2, SL_3 : STD_LOGIC; -- '1' '0' 'Z' (SL_1, SL_2, SL_3) <= UNSIGNED'("10Z"); -- 分散給各個獨立的訊號

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

  49. 3-7 constant 常數 用來描述“常數”,跟C 語言的#define 或者是Verilog 的parameter 一樣,目的在於讓VHDL 程式碼比較讓人容易看得懂、以後容易除錯以及維。 語法:constant constant_name : type [:= constant_expression;]; 例如: constant BUS_SIZE, DATA_SIZE : integer := 32; -- 匯流排的寬度 constant MSB : integer := BUS_SIZE - 1; -- 匯流排的最高位元 constant Max_Adder_Bits : integer := 8; constant Max_int : integer := 2 ** (Max_Adder_Bits - 1) - 1; constant RED_Light_Time : std_logic_vector( 3 downto 0) := conv_std_logic_vector( 9, 5)( 3 downto 0); -- 9 紅燈亮的時間 constant PI : real := 3.141592653589716; constant Half_PI : real := Pi / 2.0; constant ST0 : std_logic_vector( 2 downto 0) := "001"; constant Result_File : string := "./Result.txt"; constant Cycle_Time : time := 20 ns; -- 50.0 MHz constant Half_Cycle_time : time := Cycle_Time / 2.0;

More Related