Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
dsd1-10 / dsd-07=Verilog / vlogmsg.pdf
Скачиваний:
95
Добавлен:
05.06.2015
Размер:
276.27 Кб
Скачать

Verilog-XL Modeling Style Guide

Modeling Your Hardware

Verilog-XL does not regard as hardware registers. Integers are convenient and help to distinguish between hardware registers and other quantities. Integers have a size of 32 bits.

The following example shows a common use for an integer variable: a counter for loop control. “See Step n” comments in the example correspond to descriptive steps that follow the example.

Example: Declaring and using integers

...

task memory_clear;

 

integer i;

// See Step 1

for

(i=0;i<255;i=i+1)

// See Step 2

begin

$display("Initializing word %d", i);

// See Step 3

 

end

mem[i] = 0;

// See Step 4

 

 

endtask

// memory_clear

 

...

 

 

1.To declare integers, specify the integer keyword followed by one or more variable names separated by commas. This declaration defines the integer i.

2.You can use integers in expressions. The for loop, which initializes a memory, uses the integer i as the loop counter.

3.Use %d (decimal), %h (hex), %o (octal ), or %b (binary) in formatted-output tasks (such as $display) to display integer values.

4.Use integers to index arrays and vectors.

Declaring and Using Reals

With the real keyword, you can specify and display real numbers in either decimal or scientific notation (decimal format must have at least one digit on each side of the decimal point).

The real type behaves similarly to the integer and time types. However, not all Verilog HDL operators work on real numbers, ranges are not allowed, and real number variables default to an initial value of 0.

The example shows how you declare and display real values, and how you can convert real numbers to integers. “See Step n” comments in the example correspond to descriptive steps that follow the example.

Example: Declaring and using reals

January 2001

16

Product Version 3.2

Verilog-XL Modeling Style Guide

Modeling Your Hardware

 

 

module reals;

// See Step 1

real real1, real2, real3;

integer int1, int2;

 

initial

 

begin

 

$display(“real1 = %f, int1 = %d”, real1,

// See Step 2

int1);

real1 = 1.2; real2 = 1e4;

// See Step 3

int1 = real1; real3 = int2;

// See Step 4

#real1 int2 = 2;

// See Step 5

$display (“int1 = %d, real3 = %f”, int1, real3);

end

 

endmodule // reals

 

% verilog reals.v

 

...

 

real1 = 0.000000, int1 = x

// See Step 6

int1 = 1, real3 = 2.000000

 

1.To declare reals, specify the real keyword followed by one or more variable names separated by commas.

2.Use %f (decimal format), %e (exponential format), or %g (whichever format is shorter) in formatted-output tasks (such as $display) to display real values.

3.Assign values to reals by specifying either decimal or exponential formats. If you specify a decimal point, you must include at least one digit on each side. For exponential format, you can specify either e or E.

4.When you assign a real to an integer, Verilog-XL rounds to the nearest integer. If you want to truncate the real value, use $rtoi. You can assign an integer to a real directly, or with $itor. In both cases, Verilog-XL converts Z and X integer values to a real value of 0.

5.Use real values as you would integer or time values. Note, however, that not all operators work on real values (for example, concatenation).

The delay specification has a delay value of 1.2.

6.Note that uninitialized reals default to 0 whereas integers default to X.

Declaring and Using Strings

Strings are a series of characters, each represented by an 8-bit ASCII code. Verilog HDL operators manipulate strings as though they were a single numerical value. You can store string variables in registers (reg) and make assignments by specifying a string surrounded by quotation marks (" "), or by specifying the ASCII codes.

January 2001

17

Product Version 3.2

Verilog-XL Modeling Style Guide

Modeling Your Hardware

The example shows how you declare string variables and how the concatenation operator concatenates strings. “See Step n” comments in the example correspond to descriptive steps that follow the example.

Example: Declaring and using strings

module string_concatenation;

// See Step 1

reg [8*14:1] stringvar;

initial begin

// See Step 2

stringvar = "Hello world";

$display("%s is stored as %h", stringvar, stringvar;

// See Step 3

stringvar = {stringvar, "!!!"};

// See Step 4

$display("%s is stored as %h", stringvar, stringvar;

 

end

 

endmodule // string_concatenation

 

% verilog string.v

// See Step 5

...

 

Compiling source file "string.v"

 

Highest level modules:

 

string_concatenation

 

Hello world is stored as 00000048656c6c6f20776f726c64

 

Hello world!!! is stored as 48656c6c6f20776f726c64212121

 

7 simulation events

 

CPU time: 0.8 secs to compile + 0.1 secs to link + 0.1 secs

 

1.Specify the reg keyword with a string name variable to declare strings. The stringvar variable can store up to 14 ASCII characters. The register must have 8 bits for every character in the string that the variable stores.

2.Assign values to a string variable by specifying the string in quotation marks (" "). You can also assign the ASCII code to the variable instead of specifying the string itself.

3.Display string variables with the %s format specifier.

4.All Verilog HDL operators can manipulate string variables. The operators treat string operands as a single numerical value. The concatenation operator adds three exclamation points to stringvar.

5.The output shows the ASCII code for the Hello world string. The string Hello world is assigned to stringvar.

Declaring Vectors

You can create vectors—buses and multibit registers—by specifying bit ranges when you declare nets and registers (not time or integer types). Specify vectors by preceding the name of the net or register variable with a range specification. You can reference a vector as a single entity or as a group of scalars (single-bit values). Both array and memory declarations are closely related to vector declarations.

January 2001

18

Product Version 3.2

Verilog-XL Modeling Style Guide

Modeling Your Hardware

The example shows how you declare a 4-bit register using four instances of a flip-flop, where some of the input and output signals of the 4-bit register are vectors. “See Step n” comments in the example correspond to descriptive steps that follow the example.

Example: Declaring vectors

module hardreg (d, clk, clrb, q);

// See Step 1

input clk, clrb;

input [3:0] d;

// See Step 2

output [3:0] q;

// See Step 3

flop f1

(d[0], clk, clrb, q[0],),

f2

(d[1], clk, clrb, q[1],),

 

f3

(d[2], clk, clrb, q[2],),

 

f4

(d[3], clk, clrb, q[3],);

 

endmodule // hardreg

1.When you do not explicitly declare a vector, the result is a scalar variable—a single bit entity.

Both clk and clrb are single-bit inputs.

2.Vector declarations appear between the data type name and variable name, and take the form [most-significant bit:least-significant bit].

The hardreg module defines a 4-bit register, and therefore has a 4-bit input (d) and a 4-bit output (q).

3.Access the individual bits of a vector by following the vector name with a bit index: vector_name[index_expr]. Note that unlike arrays, you can operate on the entire vector as a single entity by using just the vector name.

Each input to and output from each flop instance is a single bit of the hardreg vectors d and q.

Declaring Arrays

You can use an array to declare multiple registers (reg, time, and integer). To specify an array, specify the variable name of a net or register followed by an array index declaration.

You can operate only on individual elements of the array. Verilog HDL does not support multidimensional arrays. Arrays are closely related to vectors. A common use of arrays is to create memories.

The example shows how an array of time variables can be used to store the simulation times when a task is called. “See Step n” comments in the example correspond to descriptive steps that follow the example.

January 2001

19

Product Version 3.2

Соседние файлы в папке dsd-07=Verilog