Unit 1 - LP3
Unit 1 - LP3
General Objective
To understand the behavioural modelling and able to apply in digital
design.
Outcome:
At the end of this module the students will be develop the Verilog HDL
code for digital design
Specific Objective
1. List the importance of behavioural modelling.(E )
Formative Assessment 1
initial statements.
The Statement inside the always or initial cannot be nested (executes in
sequentially)
sequentially
When is it executed?
Occurrence of an event in the sensitivity list
Event: Change in the logical value
7
Initial Statement
It is typically used for initialization, monitoring , waveforms
and other processes.
Initial statements
Execution Sequence
Always Statement
Keyword – always
It starts at time 0.
Example: Clock Generator Module that toggles the clock signal every half cycle.
Initial v/s Always
initial always
begin begin
… imperative statements … … imperative statements …
End End
Non Blocking- During Positive Edge of clock, the value on RHS are
read RHS expression evaluated and stored in temporary variable.
During Write operation , the values stored in the temporary variables
are assigned to LHS. Separating RD and WR Operation ensures the
values of a and b gets swapped. It eliminate race around condition.
Specific Objective 2
Formulate the usage of conditional statements in Verilog HDL.(E)
Conditional Statements
It is used for making decisions based upon certain conditions.
Case Statement
Example :
case (X)
2’b00:Y = A + B;
2’b01:Y = A – B;
2’b10:Y = A / B;
endcase
26
Casex & Casez Keywords
casez – treat all z values in case expression as donot cares. All the
bit position with z can also represented by ? in that position.
Example:
casez (X)
2’b1z: A = B + C;
2’b11: A = B / C;
endcase
28
Case z - Example
module casez_example();
reg [3:0] opcode;
reg [1:0] a,b,c;
reg [1:0] out;
always @ (opcode or a or b or c)
casez(opcode)
4'b1zzx : begin // Don't care about lower 2:1 bit, bit 0 match with x
out = a;
end
4'b01?? : begin
out = b; // bit 1:0 is don't care
end
4'b001? : begin // bit 0 is don't care
out = c;
end
default : begin
out= x;
end
endcase
Case x - Example
module casex_example();
reg [3:0] opcode;
reg [1:0] a,b,c;
reg [1:0] out;
always @ (opcode or a or b or c)
casex(opcode)
4'b1zzx : begin // Don't care 2:0 bits
out = a;
end
4'b01?? : begin // bit 1:0 is don't care
out = b;
end
4'b001? : begin // bit 0 is don't care
out = c;
end
default : begin
out = x;
end
endcase
Mux using Case Statement
module MUX (C, D, E, F, S, MUX_OUT);
input C, D, E, F;
input [1:0] S;
output MUX_OUT;
reg MUX_OUT;
always @(C or D or E or F or S)
begin
case (S)
2'b00 : MUX_OUT = C;
2'b01 : MUX_OUT = D;
2'b10 : MUX_OUT = E;
default : MUX_OUT = F;
endcase
end
endmodule
Contd..,
module mux (a,b,c,d,sel,y);
input a, b, c, d;
input [1:0] sel;
output y;
reg y;
always @ (a or b or c or d or sel)
case (sel)
0 : y = a;
1 : y = b;
2 : y = c;
3 : y = d;
default : $display("Error in SEL");
endcase
endmodule
Contd..,
module case1 (output reg [7:0] d_out
input [7:0] a_in, b_in, c_in, d_in,
input [1:0] sel, );
always @(a_in or b_in or c_in or d_in or sel )
begin
case (sel)
2’b00 : d_out = a_in;
2’b01 : d_out = b_in;
2’b10 : d_out = c_in;
2’b11 : d_out = d_in;
default : d_out = 8’bx;
endcase
end
endmodule
Decoder module decode (Ain, En,Yout);
input En;
input [2:0] Ain;
output [7:0] Yout;
reg [7:0] Yout;
always @ (En or Ain)
begin
if (!En)
Yout = 8'b0;
else
case (Ain)
3'b000 :Yout = 8'b00000001;
3'b001 :Yout = 8'b00000010;
3'b010 :Yout = 8'b00000100;
3'b011 :Yout = 8'b00001000;
3'b100 :Yout = 8'b00010000;
3'b101 :Yout = 8'b00100000;
3'b110 :Yout = 8'b01000000;
3'b111 :Yout = 8'b10000000;
default :Yout = 8'b00000000;
endcase
end
endmodule
module srff(q,clk,rst,s,r);
SR Flip Flop input s,r,clk,rst;
output q;
reg q;
always @ (posedge clk or rst)
begin
if(rst)
q<=0;
else
begin
case({s,r})
2'b00:q<=q;
2'b01:q<=s;
2'b10:q<=s;
default:$display("Not Allowed");
endcase
end
end
endmodule
module jkff(q,clk,rst,j,k);