1.25k likes | 1.71k Vues
LOGIC DESIGN WITH VHDL. 一、 What is VHDL ?. V HSIC (Very High Speed Integrated Circuit) H ardware D escription L anguage VHDL is a Design Description Language VHDL is a Design Documentation Language VHDL is a Simulation Language It is an IEEE Standard Language (IEEE1076 & 1164).
E N D
一、What is VHDL ? VHSIC (Very High Speed Integrated Circuit) Hardware Description Language • VHDL is a Design Description Language • VHDL is a Design Documentation Language • VHDL is a Simulation Language • It is an IEEE Standard Language (IEEE1076 & 1164)
2、Why Use VHDL? • Very Fast Time-to-Market • Allows designers to quickly develop designs requiring tens of thousands of logic gates or more • Provides powerful high-level constructs for describing complex logic • Supports modular design methodology and multiple levels of hierarchy • One language for design and simulation • Allows creation of device-independent designs that are portable to multiple PLD vendors • Allows user to pick any synthesis tool, vendor, or device
二、VHDL Design Descriptions • VHDL design descriptions consist of an ENTITY declaration and an ARCHITECTURE body • The ENTITY declaration describes the design I/O • The ARCHITECTURE body describes the content or function of the design • Every architecture needs an entity so it is common to refer to them together as an ENTITY/ARCHITECTURE PAIR
1、The Entity • A “BLACK BOX” • The ENTITY describes the I/O of the black box BLACK_BOX rst q[7:0] d[7:0] co clk
BLACK_BOX rst q[7:0] d[7:0] co clk Example Entity declaration ENTITY black_box IS PORT ( clk, rst: INstd_logic; d: INstd_logic_vector(7 DOWNTO 0); q: OUTstd_logic_vector(7 DOWNTO 0); co: OUTstd_logic); END black_box; • What does it all mean ?
(2)Ports • The Entity (“BLACK BOX”) has PORTS • PORTS are the points of communication • PORTS are usually the device pins • PORTS have an associated name, mode, and type
(3)Port Modes A port’s MODE indicates the direction that data is transferred: • IN Data goes into the entity only • OUT Data goes out of the entity only (and is not used internally) • INOUT Data is bi-directional (goes into and out of the entity) • BUFFER Data that goes out of the entity and is also fed-back internally Entity
(4)IEEE 1076 Types • Every port on the entity has a Type. The type is always checked during an assignment or comparison. • BIT - a port of type bit that can only take values of '0' or '1' • BIT_VECTOR - a grouping of bits (each can be '0' or '1') ENTITY type_example IS PORT ( a: IN BIT; b: OUTBIT_VECTOR(0 TO 3); -- ascending range c: OUTBIT_VECTOR(3 DOWNTO 0); -- descending range END type_example; b <= "0111"; -- Note: <= is an assignment c <= "0101"; -- double quotes (“”) used for vectors This means that: b(0) = '0' c(0) = '1' b(1) = '1' c(1) = '0' b(2) = '1' c(2) = '1' b(3) = '1' c(3) = '0'
IEEE 1076 Types (contd.) • INTEGER • useful as index holders for loops, constants, arithmetic functions, or simulation modeling • BOOLEAN • can take values ‘TRUE’ or ‘FALSE’ • ENUMERATED • has user defined set of possible values. e.g.: TYPE traffic_light IS (red, yellow, green);
IEEE 1164 • A package created to solve the limitations of the BIT type • Nine values instead of just two ('0' and '1') • Allows increased flexibility in VHDL coding, synthesis, and simulation • STD_LOGIC and STD_LOGIC_VECTOR are used instead of BIT and BIT_VECTOR when a multi-valued logic system is required • STD_LOGIC and STD_LOGIC _VECTOR must be used when tri-state logic (Z) is required • To be able to use this new type, you need to add 2 lines to your code: LIBRARY ieee; USE ieee.std_logic_1164.ALL;
IEEE-1164 Types • STD_LOGIC and STD_LOGIC_VECTOR arenowthe industry standard logic type for digital design • All 9 values are valid in a VHDL simulator, however only: • ‘0’ -- Hard ‘0’ • ‘1’ -- Hard ‘1’ • ‘Z’ -- High Impedance • ‘L’ -- Weak ‘0’ (like resistor pull down) • ‘H’ -- Weak ‘1’ (like resistor pull up) • ‘-’ -- Don’t care are recognized for logic synthesis
BLACK_BOX MODE TYPE rst q[7:0] d[7:0] co clk Entity Declaration Example LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY black_box IS PORT ( clk, rst: INstd_logic; d: INstd_logic_vector(7 DOWNTO 0); q: OUTstd_logic_vector(7 DOWNTO 0); co: OUTstd_logic); END black_box;
Exercise #1: The Entity • Write an entity declaration for the following: Port D is a 12-bit bus, input only Port OE and CLK are each input bits Port AD is a 12-bit bi-directional bus Port A is a 12-bit bus, output only Port INT is an output Port AS is an output also used internally my_design ad[11:0] d[11:0] a[11:0] oe int clk as
my_design ad[11:0] d[11:0] a[11:0] oe int clk as Exercise #1: Solution LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY my_design IS PORT ( d: IN std_logic_vector(11 DOWNTO 0); oe, clk: IN std_logic; ad: INOUT std_logic_vector(11 DOWNTO 0); a: OUT std_logic_vector(11 DOWNTO 0); int: OUT std_logic; as: BUFFER std_logic); END my_design; -- In this presentation, VHDL keywords -- are highlighted in bold, CAPITALS. -- However, VHDL is not case sensitive: -- clock, Clock, CLOCK all refer to the -- same signal
3、The Architecture • Architectures describe what is in the black box (i.e. the function or behavior of entities) • Descriptions can be either a combination of • Structural descriptions(结构描述) • Instantiations of building blocks (placement of componentsjust like a schematicand their connections) • Behavioral descriptions(行为描述) • Algorithmic (or “high-level”) descriptions: IF a = b THEN state <= state5; • Boolean equations: x <= (a OR b) AND c;
Behavioral Architecture Example • 8x 2 Input AND gate: ENTITY black_box IS PORT ( a, b: INstd_logic_vector(7 DOWNTO 0); y: OUTstd_logic_vector(7 DOWNTO 0)); END black_box; ARCHITECTURE example OF black_box IS BEGIN y <= a AND b; END example; • This example shows how to drive the device pins (the entity ports). How do we handle internal signals (or nets) that do not connect directly to the device pins ?
4、Signals • Typically used to represent wires (or nets) • Entity Ports are a special type of signal • Like ports, they have a name and type (however, there is no mode) • Signals are declared inside the architecture before the BEGIN • For Example, to create an internal 4 bit bus: ARCHITECTURE signal_example OF black_box IS SIGNAL count: std_logic_vector (3 DOWNTO 0); BEGIN .. <Many VHDL Statements> END signal_example;
三、Combinatorial Logic • There are many ways to describe combinatorial circuits • In the next few slides, we will take a look at some examples of how to describe combinatorial logic • You should refer back to these slides for some ideas when you start writing your first designs
A 2 d0 x d1 mux d2 d3 1、VHDL Statement Examples (1) Boolean Equations • All standard Boolean operators are supported in VHDL • AND, OR, NOT, XOR, XNOR, NAND • For example, a 4-1 multiplexer is shown below x <= (d0 AND NOT(A(1)) AND NOT(A(0))) OR (d1 AND NOT(A(1)) AND A(0)) OR (d2 AND A(1) AND NOT(A(0))) OR (d3 AND A(1) AND A(0)) ;
2、VHDL Statement Examples (2) WITH-SELECT-WHEN • Assignment based on a selection signal • WHEN clauses must be mutually exclusive (all different) • Always use “WHEN OTHERS” to cover unspecified cases • Only one reference to the signal, only one assignment operator (<=) WITH selection_signal SELECT signal_name <= value_1 WHEN value_1 of selection_signal, value_2 WHEN value_2 of selection_signal, ... value_n WHEN value_n of selection_signal, value_x WHEN OTHERS;
A 2 d0 x d1 mux d2 d3 VHDL Statement Examples (2) WITH-SELECT-WHEN • The same 4-1 multiplexer we saw earlier could also be described as follows: WITH A SELECT x <= d0 WHEN “00”, -- means when A=“00” d1 WHEN “01”, d2 WHEN “10”, d3 WHENOTHERS;
VHDL Statement Examples (2) WITH-SELECT-WHEN • There can be multiple conditions on each line: WITH s SELECT x <= a WHEN ”000” | “001” | “010”, b WHEN "101" | "111", -- ‘|’ means “or” in this case c WHENOTHERS;
VHDL Statement Examples (3) WHEN- ELSE • Signal is assigned a value based on conditions • Any simple expression can be a condition • Priority goes in order of appearance • Only one reference to the signal, only one assignment operator (<=) • Always end with ELSE to cover unspecified conditions signal_name <= value_1 WHEN condition1 ELSE value_2 WHEN condition2 ELSE ... value_n WHEN conditionn ELSE value_x;
A 2 d0 x d1 mux d2 d3 VHDL Statement Examples (3) WHEN-ELSE • The same example 4-1 multiplexer could also be described as follows: x <= d0 when (A = “00”) else d1 when (A = “01”) else d2 when (A = “10”) else d3 ;
VHDL Statement Examples (3) WHEN-ELSE • What is the difference between WITH-SELECT-WHEN and WHEN-ELSE ? • WITH-SELECT-WHEN allows only one control signal • WHEN-ELSE supports many different control signals • Example: A priority encoder j <= w when (a = ‘1’) else x when (b = ‘1’) else y when (c = ‘1’) else z when (d = ‘1’) else ‘0’ ;
四、VHDL Statements • There are two types of statements, Concurrentand Sequential • Concurrent Statements(means in parallel) • Concurrent statements are “executed” concurrently (at the same time) • The examples we have seen so far are all concurrent statements: • Boolean Equations • WHEN-ELSE • WITH-SELECT-WHEN • The order of concurrent statements is not important
The order of concurrent statements • For example, suppose we had the following 2 lines of code: x <= a AND b; y <= x NAND C; • This will produce exactly the same result as: y <= x NAND C; x <= a AND b; • The order that you write the statements makes no difference, because they are concurrent (working in parallel)
2、VHDL Statements (cont.) • Sequential Statements(means in series) • Sometimes we need to model complex functions. In that case, we can use an “algorithm” or a model to describe the function. This is done with Sequential Statements • With Sequential statements, the ORDER of the statements is important (example later) • Therefore, we use aprocessto mark the beginning and end of a block of sequential statements • Each completed process is considered to be one big concurrent statement (there can be many processes inside one architecture)
3、What is a VHDL “Process” ? • Processes are either awake or asleep (active or inactive) • A process normally has a sensitivity list(敏感参数表) • When a signal in that sensitivity list changes value, the process wakes up and all of the sequential statements are “executed” • For example, a process with a clock signal in its sensitivity list will become active on changes of the clock signal • At the end of the process, all the outputs are updated and the process goes back to sleep until the next time a signal changes in the sensitivity list
The Process: An Example mux: PROCESS (a, b, s) BEGIN IF s = '0' THEN x <= a; ELSE x <= b; END IF; END PROCESS mux; s a(3 DOWNTO 0) x(3 DOWNTO 0) b(3 DOWNTO 0) • The process mux is sensitive to signals a, b, and s. That means that whenever any of those signals changes value, the process wakes up, the sequential statements are executed and the output x is updated • Note 1: The logic could be registered (synchronous) or combinatorial • Note 2: The order of the signals in the sensitivity list is not important
4、Combinatorial Logic using Sequential Statements • We have already looked at some examples of combinatorial logic using Concurrent Statements • Let’s take a look at how to create combinatorial logic with sequential statements...
A 2 d0 x d1 mux d2 d3 (1)Sequential Statement Examples (1) IF-THEN-ELSE • For example, a 4 to 1 mulitplexer could be described as follows: mux4_1: PROCESS (d0, d1, d2, d3, A) BEGIN IF A = “00” THEN x <= d0 ; ELSIF A = “01” THEN x <= d1 ; ELSIF A = “10” THEN x <= d2 ; ELSE x <= d3 ; END IF; END PROCESS mux4_1 ; • Anytime you want to use IF-THEN-ELSE, then you MUST use a process, because it is a sequential statement
How can the order of sequential statements make a difference ? ex1: PROCESS (a, b) BEGIN IF a=‘1’ THEN c<=‘0’;-- if a and b are END IF; -- both ‘1’ then IF b=‘1’ THEN c<=‘1’; -- b has priority END IF; -- so c <= ‘1’; END PROCESS ex1; ex2: PROCESS (a, b) BEGIN IF b=‘1’ THEN c<=‘1’; -- if a and b are END IF; -- both ‘1’ then IF a=‘1’ THEN c<=‘0’;-- a has priority END IF; -- so c <= ‘0’; END PROCESS ex2;
A 2 d0 x d1 mux d2 d3 (2)Sequential Statement Examples (2) CASE-WHEN • Another way to describe the same 4 to 1 mux: mux4_1: PROCESS (d0,d1,d2,d3,A) BEGIN CASE A IS WHEN "00" => x <= d0; WHEN "01" => x <= d1; WHEN "10” => x <= d2; WHEN OTHERS => x <= d3; END CASE; END PROCESS mux4_1; • Anytime you want to use CASE-WHEN, then you MUST use a process, because it is a sequential statement
c 5、A Note about ProcessesSignal Assignment • Take a look at the following piece of code. Which circuit do you think will be synthesized ? PROCESS (clock) BEGIN IF rising_edge(clock) THEN b <= a; -- after the rising clock edge, a goes to b c <= b; -- after the rising clock edge, b goes to c END IF; END PROCESS ; b c a a OR clock clock
Signal Assignment in Processes • Inside processes, signals are not updated immediately. Instead, they are scheduled to be updated • The signals are not actually updated until the END PROCESS statement is reached • Therefore, on the previous slide, two registers will be synthesized (c <= b will be the old b) • In some cases, the use of a concurrent statement outside the process will fix the problem, but this is not always possible • So how else can we fix this problem ?
6、Variables • When a concurrent signal assignment outside the process cannot be used, the previous problem can be avoided using a variable • Variables are like signals, BUT they can only be used inside a PROCESS. They cannot be used to communicate information between processes • Variables can be of any valid VHDL data type • The value assigned to a variable is available immediately • Assignment of variables is done using a colon (:), like this: c := a AND b;
Using Variables vs. Signals • Solution using a variable within a process: PROCESS (clock) VARIABLE b : std_logic ; BEGIN IF rising_edge(clock) THEN b := a ; -- this is immediate c <= b ; -- this is scheduled END IF; END PROCESS ; a c clock
五、Native Operators (IEEE-1076) • Logical - defined for type BIT, BIT_VECTOR, BOOLEAN • AND, NAND • OR, NOR • XOR, XNOR • NOT • Relational - defined for types BIT, BIT_VECTOR, INTEGER • = (equal to) • /= (not equal to) • < (less than) • <= (less than or equal to) • > (greater than) • >= (greater than or equal to)
Native Operators (continued) • Arithmetic - defined for type INTEGER • + (addition), * (multiplication) • - (subtraction) • Concatenation - defined for STRING • & • A STRING is any sequence of characters • std_logic_vector is an example of a STRING Note: None of these operators were defined to support std_logic or std_logic_vector types because in IEEE-1076, std_logic did not exist yet. How can we fix this problem ?
2、Overloaded Operators • In VHDL, any native operator can be overloaded (means re-defined) to accept any other VHDL type. This is very useful. For example: SIGNAL counter: std_logic_vector(15 DOWNTO 0); counter <= counter + 3; -- the native '+' operator supports integers only, but -- we can overload it to accept std_logic_vectors also • The std_arith package from Cypress defines overloaded logical operators (AND, OR, NOT, etc.,) for the std_logic and std_logic_vector types • To get the compiler to recognize std_logic and to overload the operators you need to add 4 lines to the start your VHDL file: LIBRARY ieee; USE ieee.std_logic_1164.all; -- add the std_logic type LIBRARY cypress; USE cypress.std_arith.all; -- add overloaded operators
a(0 TO 3) aeqb b(0 TO 3) Exercise #2: Architecture Declaration of a Comparator • The entity declaration is as follows: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY compare ISPORT ( a, b: IN std_logic_vector(0 TO 3); aeqb: OUT std_logic); END compare; • Write an architecture that causes aeqb to be asserted high only when a is equal to b • Multiple solutions exist. Look back at the 4-1 mux examples
Ex2 Solution: 3 possible solutions • Concurrent statement solution using a conditional assignment: ARCHITECTURE archcompare OF compare IS BEGIN aeqb <= '1' WHEN a = b ELSE '0'; END archcompare; • Concurrent statement solution using boolean equations: ARCHITECTURE archcompare OF compare IS BEGIN aeqb <= NOT( (a(0) XOR b(0)) OR (a(1) XOR b(1)) OR (a(2) XOR b(2)) OR (a(3) XOR b(3))); END archcompare;
a(0 TO 3) aeqb b(0 TO 3) 3 possible solutions (contd.) • Solution using a process with sequential statements: ARCHITECTURE archcompare OF compare IS BEGIN comp: PROCESS (a, b) BEGIN IF a = b THEN aeqb <= '1'; ELSE aeqb <= '0'; END IF; END PROCESS comp; END archcompare;
3、Aggregates and Subscripts • The aggregate assignment joins signals together • Good for creating a bus from several single bits • Concatenation operator can be used as well • Same number of array elements on both sides tmp <= (a,b,c,d); -- called an aggregate tmp <= a & b & c & d; -- concatenation operator • Signals can be extracted from larger vectors • Good for grouping outputs as an “alias” • Sizes on both sides must match rw <= ctrl(0); ce <= ctrl(1); oe <= ctrl(2); highcount <= count(7 DOWNTO 4);
4、七段显示译码器设计 LIBRARY Ieee; USE Ieee.std_logic_1164.ALL; --程序中用到的函数库 Entity decoder_7seg is port ( indata :in std_logic_vector(3 downto 0); ena :in std_logic; --bit disp :out std_logic_vector(6 downto 0)); --bit_vector end decoder_7seg; architecture a of decoder_7seg is begin process(indata, ena) begin --如果不要use语句,将std_logic_...改成bit,就可以了
续前 • if ena='1' then • case indata is • -- abcdefg • when "0000"=>disp<="1111110"; • when "0001"=>disp<="0110000"; • when "0010"=>disp<="1101101"; • when "0011"=>disp<="1111001"; • when "0100"=>disp<="0110011"; • when "0101"=>disp<="1011011"; • when "0110"=>disp<="1011111"; • when "0111"=>disp<="1110000"; • when "1000"=>disp<="1111111"; • when "1001"=>disp<="1111011"; • when others=>disp<= "1110110"; • end case; • else disp<="1111110"; • end if; • end process; • end a;
5、八位加法器设计-并行语句实现 LIBRARY ieee; ENTITY adder_8bits IS PORT (op1, op2 :in integer range 0 to 255); c :out bit; sum :out integer); END adder_8bits; ARCHITECTURE a1 OF adder_8bits IS BEGIN sum <= op1 + op2; END a1; sum :buffer integer --不行的,因为sum定义为out,不能读取其数据 c <= '1' when sum>=255 else '0'; --integer range 0 to 255就规定了数据宽度为8位