Lecture 8: Verilog Code: EE533: Network Processor Design and Programming
Lecture 8: Verilog Code: EE533: Network Processor Design and Programming
Behavioral
• Implementation in terms of the desired design algorithm
without concern for the hardware implementation details.
module Mux_4to1(
input [3:0] i,
input [1:0] s,
output reg o
);
always @(s or i)
begin
case (s)
2'b00 : o = i[0];
2'b01 : o = i[1];
2'b10 : o = i[2];
2'b11 : o = i[3];
default : o = 1'bx;
endcase
end
endmodule
Dataflow
module Mux_4to1_df(
input [3:0] i,
input [1:0] s,
output o
);
assign o = (~s[1] & ~s[0] & i[0]) | (~s[1] & s[0] & i[1]) | (s[1] & ~s[0] & i[2]) | (s[1] & s[0] & i[3]);
endmodule
Structural
• Initial
– Starts at time 0
– Executes only once
– Is typically used for initialization in behavioral code (e.g., testbenches)
– Syntax
initial
begin
a = 1’b1;
b = 1’b0;
end
• Always
– Starts at time 0
– Executes as an infinite loop similar to a infinite loop in C
– Syntax
always
begin
#5 clock = ~clock;
end
Always
• begin/end
– Event happens in sequence
– Example
Begin
#10 a=1; At time = 10, a is set to 1
#20 b=1; At time = 30, b is set to1
End
– control passes out of the block after the last statement executes
• fork/join
– Event happens in parallel
– Example
fork
#10 a=1; At time = 10, a is set to 1
#20 b=1; At time = 20, b is set to1
Join
– control passes out of the block after time 20
Combinational Circuit Example I
always @(a or b)
begin
if (a == 1’b1)
q = b;
// missing “else statement” infers latch.
end
end
Combinational Circuit Example II
always @(a or b)
begin
if (a == 1’b1)
q = b;
else // include “else statement”
q = 1’b0;
end
Combinational Circuit Example III
always @(d)
begin
case (d)
2’b00: z = 1’b1; // missing “s” assignment
2’b01: z = 1’b0; // missing “s” assignment
2’b10: z = 1’b1; s = 1’b1;
// missing condition “2’b11”.
endcase
end
Sequential process assignment
• Use non-blocking assign. in always @(posedge clk) block
// Poor coding style
always @(posedge clk)
b = a; // assignment of values depends on which
always @(posedge clk) // always block scheduler chooses
a = b; // first