6.programming Basics 1up
6.programming Basics 1up
edu/~myadav/Research 1
Outline
• New Operators
Increment and decrement operator
► Used the same way as C
• Assignment Operators
Equality operators
► Logical equality operator ==, != (returns 1’bx on comparing x or z values)
► Case equality operator === , !== (looks for an exact match of x or z
values)
► SystemVerilog introduces two new operators ==? And !=? (wildcard
equality operators)
– Performs a bitwise operation and treats a z or an x as a wildcard value
– These operators only consider x or z bits in the right-hand operand as mask
bits
A = 010z,
b=0101
a==?b
unknown
logic [2:0] a;
if ((a==3’b001)||(a==3’b010)||(a==3’b100))
Equivalent to
logic [2:0] a;
if (a inside {3’b001, 3’b010, 3’b100}
• Type Casting
Verilog does type conversion using assignments
SystemVerilog adds a typecast operator
► Typecasting allows the designer to specify that a conversion
should occur at any point during the evaluation of an expression,
instead of just part of an assignment
type’ (expression)
example
longint a,y;
real r;
y=a+longint’(r**3)
size’ (expression)
• Size Casting
SystemVerilog allows vector widths to be cast to a different size
► If an expression is cast to a smaller size than the number of bits in
the expression, the left-most bits of the expression are truncated
► If an expression is cast to a larger vector size, then the expression
is left extended
example
logic [15:0]
a,b,c,sum;
logic carry;
sum=a+16’(5); Cast operand
{carry,sum}=17’(a+3); Cast result
sum=a+16’(b-2)/c; Cast intermediate result
• Sign Casting
SystemVerilog allows casting the signed-ness of a value
► Signed-ness of an operand can be cast
► Signed-ness of an operation result can be cast
signed’ (expression)
unsigned’ (expression)
example
• do…while loop
A while loop in Verilog might not execute at all
► If the test of control values is false the first time the loop is encountered in
the execution flow
SystemVerilog adds a do..while loop (similar to C)
► A do while loop executes atleast once
► Control of the loop is tested at the end of each pass of the loop
– All logic for setting the outputs of the loop can be placed inside the loop
– Makes code more concise and intuitive
• Tasks
Tasks can have input, multiple outputs or inout ports
Tasks consume time and can contain
► Delay
► Timing
► Events
task task_name;
parameters
input declarations
output declarations
reg declarations
… text body …
endtask
task reset();
reset_l = 1’b0;
function int add2(int n);
#100
return n + 2;
reset_l = 1’b1;
endfunction
endtask
• Return Statement
SystemVerilog adds a return statement
► Ifa return statement is executed that value is returned else the last
value assigned to the function name is the return value
► A return statement can be used to exit a task or a function
• Void functions
Void functions do not return a value
Output and inout formal arguments allow a void function to
propagate changes to the scope
A void function can be called like a task but must adhere to the
restriction of function contents
typedef struct{
logic valid;
logic [7:0] check;
logic [63:0] data;
} packet_t;
always@(posedge clock)
if(data_ready)
fill_packet (.data_in(raw_data), .data_out(data_packet));
Sensitive to change
task automatic check_results (
input packet_t sent, ref packet_t received, ref logic done);
wait (done)
…
endfunction
function void bad(ref int a[], int start=0); Watch out for ref
followed by input