1
2-- Copyright (C) 1996 Morgan Kaufmann Publishers, Inc
3
4-- This file is part of VESTs (Vhdl tESTs).
5
6-- VESTs is free software; you can redistribute it and/or modify it
7-- under the terms of the GNU General Public License as published by the
8-- Free Software Foundation; either version 2 of the License, or (at
9-- your option) any later version.
10
11-- VESTs is distributed in the hope that it will be useful, but WITHOUT
12-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13-- FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14-- for more details.
15
16-- You should have received a copy of the GNU General Public License
17-- along with VESTs; if not, write to the Free Software Foundation,
18-- Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20-- ---------------------------------------------------------------------
21--
22-- $Id: ch_02_ch_02_01.vhd,v 1.2 2001-10-26 16:29:37 paw Exp $
23-- $Revision: 1.2 $
24--
25-- ---------------------------------------------------------------------
26
27entity ch_02_01 is
28
29end entity ch_02_01;
30
31
32----------------------------------------------------------------
33
34
35architecture test of ch_02_01 is
36begin
37
38
39  section_2_1_a : process is
40
41                            -- code from book:
42
43                            constant number_of_bytes : integer := 4;
44                          constant number_of_bits : integer := 8 * number_of_bytes;
45                          constant e : real := 2.718281828;
46                          constant prop_delay : time := 3 ns;
47                          constant size_limit, count_limit : integer := 255;
48
49                          --
50
51                          variable index : integer := 0;
52                          variable sum, average, largest : real;
53                          variable start, finish : time := 0 ns;
54
55                          -- end of code from book
56
57  begin
58    wait;
59  end process section_2_1_a;
60
61
62  ----------------
63
64
65  section_2_1_b : process is
66
67                            -- code from book:
68
69                            variable start : time := 0 ns;
70                          variable finish : time := 0 ns;
71
72                          -- end of code from book
73
74                          variable program_counter : integer;
75                          variable index : integer;
76
77  begin
78
79    -- code from book:
80
81    program_counter := 0;
82    index := index + 1;
83
84    -- end of code from book
85
86    wait;
87  end process section_2_1_b;
88
89
90  ----------------
91
92
93  section_2_2_a : process is
94
95                            -- code from book:
96
97                            type apples is range 0 to 100;
98                          type oranges is range 0 to 100;
99
100                          --
101
102                          type day_of_month is range 0 to 31;
103                          type year is range 0 to 2100;
104
105                          variable today : day_of_month := 9;
106                          variable start_year : year := 1987;
107
108                          --
109
110                          constant number_of_bits : integer := 32;
111                          type bit_index is range 0 to number_of_bits - 1;
112
113                          --
114
115                          type set_index_range is range 21 downto 11;
116                          type mode_pos_range is range 5 to 7;
117                          variable set_index : set_index_range;
118                          variable mode_pos : mode_pos_range;
119
120                          --
121
122                          type input_level is range -10.0 to +10.0;
123                          type probability is range 0.0 to 1.0;
124
125                          --
126
127                          variable input_A : input_level;
128
129                          -- end of code from book
130
131  begin
132
133    -- code from book:
134
135    -- error: Incompatible types for assignment
136    -- start_year := today;
137
138    -- end of code from book
139
140    wait;
141  end process section_2_2_a;
142
143
144  ----------------
145
146
147  section_2_2_b : process is
148
149                            -- code from book:
150
151                            type resistance is range 0 to 1E9
152                          units
153                            ohm;
154                          end units resistance;
155
156                          -- end of code from book
157
158  begin
159    wait;
160  end process section_2_2_b;
161
162
163  ----------------
164
165
166  section_2_2_c : process is
167
168                            -- code from book:
169
170                            type resistance is range 0 to 1E9
171                          units
172                            ohm;
173                            kohm = 1000 ohm;
174                            Mohm = 1000 kohm;
175                          end units resistance;
176
177                          -- end of code from book
178
179  begin
180    wait;
181  end process section_2_2_c;
182
183
184  ----------------
185
186
187  section_2_2_d : process is
188
189                            -- code from book:
190
191                            type length is range 0 to 1E9
192                          units
193                            um;			-- primary unit: micron
194                            mm = 1000 um;		-- metric units
195                            m = 1000 mm;
196                            mil = 254 um;		-- imperial units
197                            inch = 1000 mil;
198                          end units length;
199
200                          -- end of code from book
201
202  begin
203    wait;
204  end process section_2_2_d;
205
206
207  ----------------
208
209
210  section_2_2_e : process is
211
212                            -- code from book:
213
214                            -- type time is range implementation_defined
215                            type time is range integer'low to integer'high
216                          units
217                            fs;
218                            ps = 1000 fs;
219                            ns = 1000 ps;
220                            us = 1000 ns;
221                            ms = 1000 us;
222                            sec = 1000 ms;
223                            min = 60 sec;
224                            hr = 60 min;
225                          end units;
226
227                          -- end of code from book
228
229  begin
230    wait;
231  end process section_2_2_e;
232
233
234  ----------------
235
236
237  section_2_2_f : process is
238
239                            -- code from book:
240
241                            type alu_function is (disable, pass, add, subtract, multiply, divide);
242
243                          --
244
245                          type octal_digit is ('0', '1', '2', '3', '4', '5', '6', '7');
246
247                          --
248
249                          variable alu_op : alu_function;
250                          variable last_digit : octal_digit := '0';
251
252                          --
253
254                          type logic_level is (unknown, low, undriven, high);
255                          variable control : logic_level;
256                          type water_level is (dangerously_low, low, ok);
257                          variable water_sensor : water_level;
258
259                          -- end of code from book
260
261  begin
262
263    -- code from book:
264
265    alu_op := subtract;
266    last_digit := '7';
267
268    --
269
270    control := low;
271    water_sensor := low;
272
273    -- end of code from book
274
275    wait;
276  end process section_2_2_f;
277
278
279  ----------------
280
281
282  section_2_2_g : process is
283
284                            -- code from book:
285
286                            type severity_level is (note, warning, error, failure);
287
288                          -- end of code from book
289
290  begin
291    wait;
292  end process section_2_2_g;
293
294
295  ----------------
296
297
298  section_2_2_h : process is
299
300                            -- code from book:
301
302                            variable cmd_char, terminator : character;
303
304                          -- end of code from book
305
306  begin
307
308    -- code from book:
309
310    cmd_char := 'P';
311    terminator := cr;
312
313    -- end of code from book
314
315    wait;
316  end process section_2_2_h;
317
318
319  ----------------
320
321
322  section_2_2_i : process is
323
324                            -- code from book:
325
326                            type boolean is (false, true);
327
328                          --
329
330                          type bit is ('0', '1');
331
332                          -- end of code from book
333
334  begin
335    wait;
336  end process section_2_2_i;
337
338
339  ----------------
340
341
342  section_2_2_j : process is
343
344                            variable write_enable_n, select_reg_n, write_reg_n : bit;
345
346  begin
347
348    -- code from book:
349
350    write_reg_n := not ( not write_enable_n and not select_reg_n );
351
352    -- end of code from book
353
354    wait;
355  end process section_2_2_j;
356
357
358  ----------------
359
360
361  section_2_2_k : process is
362
363                            -- code from book:
364
365                            type std_ulogic is ( 'U',	-- Uninitialized
366                                                 'X',	-- Forcing Unknown
367                                                 '0',	-- Forcing zero
368                                                 '1',	-- Forcing one
369                                                 'Z',	-- High Impedance
370                                                 'W',	-- Weak Unknown
371                                                 'L',	-- Weak zero
372                                                 'H',	-- Weak one
373                                                 '-' );	-- Don't care
374
375                          -- end of code from book
376
377  begin
378    wait;
379  end process section_2_2_k;
380
381
382  ----------------
383
384
385  section_2_3_a : process is
386
387                            -- code from book:
388
389                            subtype small_int is integer range -128 to 127;
390
391                          --
392
393                          variable deviation : small_int;
394                          variable adjustment : integer;
395
396                          --
397
398                          subtype bit_index is integer range 31 downto 0;
399
400                          -- end of code from book
401
402  begin
403
404    deviation := 0;
405    adjustment := 0;
406
407    -- code from book:
408
409    deviation := deviation + adjustment;
410
411    -- end of code from book
412
413    wait;
414  end process section_2_3_a;
415
416
417  ----------------
418
419
420  section_2_3_b : process is
421
422                            constant highest_integer : integer := integer'high;
423
424                          constant highest_time : time := time'high;
425
426                          -- code from book:
427
428                          subtype natural is integer range 0 to highest_integer;
429                          subtype positive is integer range 1 to highest_integer;
430
431                          --
432
433                          subtype delay_length is time range 0 fs to highest_time;
434
435                          -- end of code from book
436
437  begin
438    wait;
439  end process section_2_3_b;
440
441
442  ----------------
443
444
445  section_2_3_c : process is
446
447                            -- code from book:
448
449                            type logic_level is (unknown, low, undriven, high);
450                          type system_state is (unknown, ready, busy);
451
452                          --
453
454                          subtype valid_level is logic_level range low to high;
455
456                          -- end of code from book
457
458  begin
459    wait;
460  end process section_2_3_c;
461
462
463  ----------------
464
465
466  section_2_4_a : process is
467
468                            -- code from book:
469
470                            type resistance is range 0 to 1E9
471                          units
472                            ohm;
473                            kohm = 1000 ohm;
474                            Mohm = 1000 kohm;
475                          end units resistance;
476
477                          type set_index_range is range 21 downto 11;
478
479                          type logic_level is (unknown, low, undriven, high);
480
481                          -- end of code from book
482
483  begin
484
485    -- output from vsim: "2000"
486    report resistance'image(2 kohm);
487
488    -- code from book:
489
490    assert resistance'left = 0 ohm;
491    assert resistance'right = 1E9 ohm;
492    assert resistance'low = 0 ohm;
493    assert resistance'high = 1E9 ohm;
494    assert resistance'ascending = true;
495    assert resistance'image(2 kohm) = "2000 ohm";
496    assert resistance'value("5 Mohm") = 5_000_000 ohm;
497
498    assert set_index_range'left = 21;
499    assert set_index_range'right = 11;
500    assert set_index_range'low = 11;
501    assert set_index_range'high = 21;
502    assert set_index_range'ascending = false;
503    assert set_index_range'image(14) = "14";
504    assert set_index_range'value("20") = 20;
505
506    assert logic_level'left = unknown;
507    assert logic_level'right = high;
508    assert logic_level'low = unknown;
509    assert logic_level'high = high;
510    assert logic_level'ascending = true;
511    assert logic_level'image(undriven) = "undriven";
512    assert logic_level'value("Low") = low;
513
514    --
515
516    assert logic_level'pos(unknown) = 0;
517    assert logic_level'val(3) = high;
518    assert logic_level'succ(unknown) = low;
519    assert logic_level'pred(undriven) = low;
520
521    --
522
523    assert time'pos(4 ns) = 4_000_000;
524
525    -- end of code from book
526
527    wait;
528  end process section_2_4_a;
529
530
531  ----------------
532
533
534  section_2_4_b : process is
535
536                            -- code from book:
537
538                            type length is range integer'low to integer'high
539                          units
540                            mm;
541                          end units length;
542
543                          type area is range integer'low to integer'high
544                            units
545                              square_mm;
546                            end units area;
547
548                          --
549
550                          variable L1, L2 : length;
551                          variable A : area;
552
553                          -- end of code from book
554
555  begin
556    -- TG: avoid overflow in multiplication
557    L1 := 1 mm;
558
559    -- code from book:
560
561    -- error: No feasible entries for infix op: "*"
562    -- A := L1 * L2;      -- this is incorrect
563
564    --
565
566    A := area'val( length'pos(L1) * length'pos(L2) );
567
568    -- end of code from book
569
570    wait;
571  end process section_2_4_b;
572
573
574  ----------------
575
576
577  section_2_4_c : process is
578
579                            -- code from book:
580
581                            type opcode is (nop, load, store, add, subtract, negate, branch, halt);
582                          subtype arith_op is opcode range add to negate;
583
584                          -- end of code from book
585
586  begin
587
588    -- code from book:
589
590    assert arith_op'base'left = nop;
591    assert arith_op'base'succ(negate) = branch;
592
593    -- end of code from book
594
595    wait;
596  end process section_2_4_c;
597
598
599end architecture test;
600