---------------------------------------------------------------- -- Chapter 2 Scalar Data Types and Operations ---------------------------------------------------------------- ---------------------------------------------------------------- -- 2.1 Constants and Variables ---------------------------------------------------------------- ---------------------------------------------------------------- -- 2.1.1 Constant and Variable Declarations ---------------------------------------------------------------- -- Page 32 constant number_of_bytes : integer := 4; constant number_of_bits : integer := 8 * number_of_bytes; constant e : real := 2.718281828; constant prop_delay : time := 3 ns; constant size_limit, count_limit : integer := 255; variable index : integer := 0; variable sum, average, largest : real; variable start, finish : time := 0 ns; variable start : time := 0 ns; variable finish : time := 0 ns; -- Example 2.1 entity ent is end entity ent; architecture sample of ent is constant pi : real := 3.14159; begin process is variable counter : integer; begin -- ... -- statements using pi and counter end process; end architecture sample; ---------------------------------------------------------------- -- 2.1.2 Variable Assignment ---------------------------------------------------------------- -- Page 33 program_counter := 0; index := index + 1; ---------------------------------------------------------------- -- 2.2 Scalar Types ---------------------------------------------------------------- ---------------------------------------------------------------- -- 2.2.1 Type Declarations ---------------------------------------------------------------- -- Page 34 type apples is range 0 to 100; type oranges is range 0 to 100; -- Page 35 package int_types is type small_int is range 0 to 255; end package int_types; use work.int_types.all; entity small_adder is port ( a, b : in small_int; s : out small_int ); end entity small_adder; ---------------------------------------------------------------- -- 2.2.2 Integer Types ---------------------------------------------------------------- -- Page 35 type day_of_month is range 0 to 31; type year is range 0 to 2100; -- Page 36 start_year := today; constant number_of_bits : integer := 32; type bit_index is range 0 to number_of_bits - 1; -- Page 37 if A > B then greater := A; else greater := B; end if; greater := maximum(A, B); type set_index_range is range 21 downto 11; type mode_pos_range is range 5 to 7; variable set_index : set_index_range; variable mode_pos : mode_pos_range; ---------------------------------------------------------------- -- 2.2.3 Floating-Point Types ---------------------------------------------------------------- -- Page 38 type input_level is range -10.0 to +10.0; type probability is range 0.0 to 1.0; variable input_A : input_level; ---------------------------------------------------------------- -- 2.2.4 Physical Types ---------------------------------------------------------------- -- Page 39 type resistance is range 0 to 1E9 units ohm; end units resistance; -- Page 40 type resistance is range 0 to 1E9 units ohm; kohm = 1000 ohm; Mohm = 1000 kohm; end units resistance; type length is range 0 to 1E9 units um; -- primary unit: micron mm = 1000 um; -- metric units m = 1000 mm; inch = 25400 um; -- imperial units foot = 12 inch; end units length; ---------------------------------------------------------------- -- Time ---------------------------------------------------------------- -- Page 42 type time is range implementation_defined units fs; ps = 1000 fs; ns = 1000 ps; us = 1000 ns; ms = 1000 us; sec = 1000 ms; min = 60 sec; hr = 60 min; end units; -- Example 2.2 signal triangle_wave : real; ... wave_proc : process is variable phase : time; begin phase := (now + t_offset) mod t_period_wave; if phase <= t_period_wave/2 then triangle_wave <= 4.0 * real(phase/t_period_wave) - 1.0; else triangle_wave <= 3.0 - 4.0 * real(phase/t_period_wave); end if; wait for tperiod_sample; end process wave_proc; ---------------------------------------------------------------- -- 2.2.5 Enumeration Types ---------------------------------------------------------------- -- Page 43 type alu_function is (disable, pass, add, subtract, multiply, divide); type octal_digit is ('0', '1', '2', '3', '4', '5', '6', '7'); variable alu_op : alu_function; variable last_digit : octal_digit := '0'; alu_op := subtract; last_digit := '7'; type logic_level is (unknown, low, undriven, high); variable control : logic_level; type water_level is (dangerously_low, low, ok); variable water_sensor : water_level; -- Page 44 control := low; water_sensor := low; type severity_level is (note, warning, error, failure); type file_open_status is (open_ok, status_error, name_error, mode_error); type file_open_kind is (read_mode, write_mode, append_mode); ---------------------------------------------------------------- -- Characters ---------------------------------------------------------------- -- Page 44 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, c128, c129, c130, c131, c132, c133, c134, c135, c136, c137, c138, c139, c140, c141, c142, c143, c144, c145, c146, c147, c148, c149, c150, c151, c152, c153, c154, c155, c156, c157, c158, c159, ' ', '¡', '¢', '£', '¤', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯', '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ'); -- Page 45 variable cmd_char, terminator : character; cmd_char := 'P'; terminator := cr; ---------------------------------------------------------------- -- Booleans ---------------------------------------------------------------- -- Page 46 type boolean is (false, true); ---------------------------------------------------------------- -- Bits ---------------------------------------------------------------- -- Page 47 type bit is ('0', '1'); -- Page 48 write_reg_n := not ( not write_enable_n and not select_reg_n ); ---------------------------------------------------------------- -- Standard Logic ---------------------------------------------------------------- -- Page 48 type std_ulogic is ( 'U', -- Uninitialized 'X', -- Forcing Unknown '0', -- Forcing zero '1', -- Forcing one 'Z', -- High Impedance 'W', -- Weak Unknown 'L', -- Weak zero 'H', -- Weak one '-' ); -- Don't care ---------------------------------------------------------------- -- Condition Conversion ---------------------------------------------------------------- -- Page 50 signal cs1, ncs2, cs3 : std_ulogic; if cs1 and not ncs2 and cs3 then ... end if; wait until clk; if ?? (cs1 and not ncs2 and cs3) then ... end if; if cs1 and cs3 and alu_op = pass then ... -- illegal if cs1 = '1' and cs3 = '1' and alu_op = pass then ... -- Page 51 if cs1 = '1' and ncs2 = '0' and cs3 = '1' then ... end if; ---------------------------------------------------------------- -- 2.3 Type Classification ---------------------------------------------------------------- ---------------------------------------------------------------- -- 2.3.1 Subtypes ---------------------------------------------------------------- -- Page 52 subtype small_int is integer range -128 to 127; variable deviation : small_int; variable adjustment : integer; deviation := deviation + adjustment; -- Page 53 subtype bit_index is integer range 31 downto 0; subtype natural is integer range 0 to highest_integer; subtype positive is integer range 1 to highest_integer; subtype delay_length is time range 0 fs to highest_time; ---------------------------------------------------------------- -- 2.3.2 Type Qualification ---------------------------------------------------------------- -- Page 53 type logic_level is (unknown, low, undriven, high); type system_state is (unknown, ready, busy); subtype valid_level is logic_level range low to high; ---------------------------------------------------------------- -- 2.3.3 Type Conversion ---------------------------------------------------------------- ---------------------------------------------------------------- -- 2.4 Attributes of Scalar Types ---------------------------------------------------------------- -- Page 55 type resistance is range 0 to 1E9 units ohm; kohm = 1000 ohm; Mohm = 1000 kohm; end units resistance; type set_index_range is range 21 downto 11; type logic_level is (unknown, low, undriven, high); assert resistance'left = 0 ohm; assert resistance'right = 1E9 ohm; assert resistance'low = 0 ohm; assert resistance'high = 1E9 ohm; assert resistance'ascending = true; assert resistance'image(2 kohm) = "2000 ohm"; assert resistance'value("5 Mohm") = 5_000_000 ohm; assert set_index_range'left = 21; assert set_index_range'right = 11; assert set_index_range'low = 11; assert set_index_range'high = 21; assert set_index_range'ascending = false; assert set_index_range'image(14) = "14"; assert set_index_range'value("20") = 20; assert logic_level'left = unknown; assert logic_level'right = high; assert logic_level'low = unknown; assert logic_level'high = high; assert logic_level'ascending = true; assert logic_level'image(undriven) = "undriven"; assert logic_level'value("Low") = low; -- Page 56 assert logic_level'pos(unknown) = 0; assert logic_level'val(3) = high; assert logic_level'succ(unknown) = low; assert logic_level'pred(undriven) = low; assert time'pos(4 ns) = 4_000_000; type length is range integer'low to integer'high units mm; end units length; type area is range integer'low to integer'high units square_mm; end units area; variable L1, L2 : length; variable A : area; -- Page 57 A := L1 * L2; -- this is incorrect A := area'val( length'pos(L1) * length'pos(L2) ); type opcode is (nop, load, store, add, subtract, negate, branch, halt); subtype arith_op is opcode range add to negate; assert arith_op'base'left = nop; assert arith_op'base'succ(negate) = branch; ---------------------------------------------------------------- -- 2.5 Expressions and Predefined Operations ---------------------------------------------------------------- -- Page 60 assert to_string(123) = "123"; assert to_string(456.78) = "4.5678e+2"; assert to_string(warning) = "warning"; assert to_string(456.78, 4) = "456.7800"; assert to_string(456.78, 1) = "456.8"; -- Page 61 assert to_string(456.78, "%10.3f") = " 456.780"; assert to_string(456.78, "%-12.3E") = "4.568E+02 "; assert to_string(2.2 kohm) = "2200 ohm"; assert to_string(29.5 us) = "29500 ns"; assert to_string(29500 ns, us) = "29.5 us"; signal clk, d, q : bit; ... dff : process is begin if rising_edge(clk) then q <= d; end if; wait on clk; end process dff; ---------------------------------------------------------------- -- Exercises ----------------------------------------------------------------