A Sequential Primitive
Primitive dff( q, clk, data);
output q; reg q;
input clk, data;
table
// clk data q new-q
(01) 0 : ? : 0;
// Latch a 0
(01) 1 : ? : 1; // Latch a 1
(0x) 1 : 1 : 1; // Hold when d and q both 1
(0x) 0 : 0 : 0; // Hold when d and q both 0
(?0) ? : ? : -; // Hold when clk falls
? (??) : ? : -; // Hold when clk stable
endtable
endprimitive
Continuous Assignment
Another way to describe combinational function
Convenient for logical or datapath specifications
wire [8:0] sum;
Define bus widths
wire [7:0] a, b;
wire carryin;
assign sum = a + b + carryin;
Continuous
assignment:
permanently
sets the value of
sum to be
a+b+carryin.
Recomputed
when a, b, or
carryin changes
Initial and Always Blocks
initial
begin
// imperative statements
end
Runs when simulation starts
Terminates when control
reaches the end
Good for providing stimulus
always
begin
// imperative statements
end
Runs when simulation starts
Restarts when control
reaches the end
Good for modeling or
specifying hardware
Initial and Always
Run until they encounter a delay
initial begin
#10 a = 1; b = 0;
#10 a = 0; b = 1;
end
or a wait for an event
always @(posedge clk) q = d;
always begin
wait(i);
a = 0;
wait( i);
a = 1;
end
Procedural Assignment
Inside an initial or always block:
sum = a + b + cin;
Just like in C: RHS evaluated and assigned to LHS before
next statement executes
RHS may contain wires and/or regs
LHS must be a reg
(only primitives or continuous assignment may set wire
values)
Imperative Statements
if (select == 1) y = a;
else y = b;
case (op)
2’b00: y = a + b;
2’b01: y = a - b;
2’b10: y = a ˆ b;
default: y = ’hxxxx;
endcase
For Loops
Example generates an increasing sequence of values on
an output
reg [3:0] i, output;
for ( i = 0 ; i <= 15 ; i = i + 1 ) begin
output = i;
#10;
end
While Loops
A increasing sequence of values on an output
reg [3:0] i, output;
i = 0;
while (i <= 15) begin
output = i;
#10 i = i + 1;
end