Professional Documents
Culture Documents
Session 2
BITS Pilani Pawan Sharma
Pilani Campus ps@pilani.bits-pilani.ac.in
Example
F = (in1 AND in2) OR (in3 AND in4)
//Structural //Behavioral
module and_or (out,in1, in2, in3, in4); module and_or (out,in1, in2, in3, in4);
input in1, in2, in3, in4; input in1, in2, in3, in4;
output out; output out;
wire tmp,tmp1; reg out;
and #10 u1 (tmp, in1, in2), u2 (tmp1, in3, in4);
or #20 (out, tmp, tmp1); always @(in1 or in2 or in3 or in4)
endmodule begin
if (in1 & in2)
//Data flow out = #30 1;
module and_or (out,in1, in2, in3, in4); else
input in1, in2, in3, in4; out = #30 (in3 & in4);
output out; end
wire tmp; endmodule
assign #10 tmp = in1 & in2;
wire #10 tmp1= in3 & in4;
assign #20 out = tmp | tmp1;
/*The three statements could be condensed
into one*/
//assign #30 out = (in1 & in2) | (in3 & in4);
endmodule
Contd..
//Test fixture for and_or module and_or simulation result
r1r2r3r4 = 0000, o = 0
module test_and_or r1r2r3r4 = 0001, o = 0
reg r1, r2, r3, r4; r1r2r3r4 = 0010, o = 0
wire o; r1r2r3r4 = 0011, o = 1
r1r2r3r4 = 0100, o = 0
and_or u2 (.in2(r2), .in1(r1), .in3(r3), .in4(r4), .out(o)); r1r2r3r4 = 0101, o = 0
r1r2r3r4 = 0110, o = 0
initial begin :b1 r1r2r3r4 = 0111, o = 1
reg [4:0] i1234; r1r2r3r4 = 1000, o = 0
for ( i1234 = 0; i1234 < 16; i1234 = i1234 + 1) r1r2r3r4 = 1001, o = 0
begin r1r2r3r4 = 1010, o = 0
{ r1, r2, r3, r4 } = i1234 [3:0]; r1r2r3r4 = 1011, o = 1
#31 $display (“r1r2r3r4 = %b%b%b%b, o = %b”, r1, r2, r3, r4, o); r1r2r3r4 = 1100, o = 1
end r1r2r3r4 = 1101, o = 1
end r1r2r3r4 = 1110, o = 1
endmodule r1r2r3r4 = 1111, o = 1
Continuous (Dataflow) Assignment
module maybe_mux_3to1(a, b, c,
sel, out);
input [1:0] sel;
input a,b,c; a
output out; 00
reg out; b D Q
01 out
c
always @(a or b or c or sel) 10
begin G
case (sel)
2
2'b00: out = a;
2'b01: out = b; sel
2'b10: out = c;
endcase sel[1]
end sel[0]
endmodule
20
ALU example
Procedural Modeling
Procedural Verilog code is like programming in a computer
language—with one large exception: Procedural Verilog code
adds a concept of time.
With a programming language, code is started at a particular
location, for example, at the first line or main function. In
Verilog, code starts running in one of two places: at the initial
statement and at the always statement.
However, if all the code is started at the initial and always
statements, how can you know the order in which the
statements will run? This is where the model of time comes
into effect.
initial keyword
Verilog interprets the initial keyword to Example:
mean “start here at time 0.”