Professional Documents
Culture Documents
Data Flow & Behavioral Modeling
Data Flow & Behavioral Modeling
Expressions,operators,operands
describes system in terms of expressions instead of primitive gates. Expression formed using operands & operators. Operands are data types used in expression.they are : constant,parameter,net,register,bit select,part select,memory element. eg., c= a+ b => a, b,c operands Operators act on operand to produce desired result.
Logical Operators
&& p logical AND || p logical OR
p logical NOT Operands evaluated to ONE bit value: 0, 1 or x Result is ONE bit value: 0, 1 or x
!
A = 6; B = 0; C = x; A && B p 1 && 0 p 0 A || !B p 1 || 1 p 1 C || B p x || 0 p x
but C&&B=0
p bitwise AND | p bitwise OR ~ p bitwise NOT ^ p bitwise XOR ~^ or ^~ p bitwise XNOR Operation on bit by bit basis
a = 4b1010; b = 4b1100;
c = a ^ b;
a = 4b1010; b = 2b11;
Reduction Operators
& | ^ ~& ~| ~^ or ^~
Shift Operators
>> p shift right << p shift left
Concatenation Operator
{op1, op2, ..} p concatenates op1, op2, .. to single number Operands must be sized !!
reg a; reg [2:0] b, c; .. a = 1 b 1; b = 3 b 010; c = 3 b 101; catx = {a, b, c}; caty = {b, 2 b11, a}; catz = {b, 1}; Replication .. catr = {4{a}, b, 2{c}};
Relational Operators
p greater than < p less than >= p greater or equal than <= p less or equal than
>
b1x1 <= 0
10 < z
p1 px px
Equality Operators
== != === !==
Return 0, 1 or x
Return 0 or 1
4b 1z0x == 4b 1z0x
px
Conditional Operator
cond_expr ? true_expr : false_expr
Operator Precedence
Continuous Assignements
a closer look
Syntax:
assign #del <id> = <expr>;
optional net type !!
Properties:
they all execute in parallel are order independent are continuously active
Multiplexer
Initial Blocks
Start execution at sim time zero and finish when their last statement executes
module nothing; initial $display(Im first); initial begin #50; $display(Really?); end endmodule
Always Blocks
Start execution at sim time zero and continue until sim finishes
Events (i)
@
always @(signal1 or signal2 or ..) begin .. execution triggers end
every time any signal changes execution triggers every time clk changes from 0 to 1 execution triggers every time clk changes from 1 to 0
Examples
3rd half adder implem
module half_adder(S, C, A, B); output S, C; input A, B; reg S,C; wire A, B; always @(A or B) begin S = A ^ B; C = A && B; end endmodule
Events (ii)
wait (expr)
always begin wait (ctrl) #10 cnt = cnt + 1; #10 cnt2 = cnt2 + 2; end
Example
res a b Y
c clk
always @(res or posedge clk) begin if (res) begin Y = 0; W = 0; end else begin Y = a & b; W = ~c; end end
Timing (i)
d initial begin #5 c = 1; #5 b = 0; #5 d = c; end c b 0 Each assignment is blocked by its previous one 5 Time 10 15
Timing (ii)
d initial begin fork #5 c = 1; #5 b = 0; #5 d = c; join end Assignments are not blocked here c b 0 5 Time 10 15
Procedural assignment
Assignment statement within an initial or an always statement . 2 kinds :i)blocking assignments (ii)non-blocking Blocking: = operator is used. statements executed in they order they are specified. reg a = 2b 11; reg b = reg a Non-blocking: <= operator used allows scheduling without blocking execution of statements in sequential block. always @ (posedge clk) begin reg1 <= in1; reg2 <= reg 1;//old value of reg1 end
Timing controls
Provides simulation time at which procedural statements execute. 3 forms:1)delay based(2)event(3)level sensitive Delay : i)regular :time between when statement is encountered and when executed. # 3 a=2b11; ii)intra-assignment :assignment to right x = 0;z=0; y = #10 x+z; iii)zero delay : statement executed last # 0 x=1; Level sensitive: waits for certain condition to be true before statement wait(enable) # 20 c=c+1;
Contd.,
Event based timing control: change in value on reg or net.
Regular event: @(clk) q=d; Event or control:-transition on any one of multiple signals. always @ ( rst or clk or d) - Named event : declared by keyword event, triggered by symbol.
Block statements
Used to group multiple statement to act together. Sequential block: begin and end executed sequentially. Parallel block : fork and join executed concurrently. ordering controlled by delay. reg x,y; reg[1:0]z,w; initial fork x=1b0; #5 y=1b1; # 10 z = {x,y}; # 20 w = {y,x}; join
Procedural Statements: if
E.g. 4-to-1 mux:
module mux4_1(out, in, sel); output out; input [3:0] in; input [1:0] sel; reg out; wire [3:0] in; wire [1:0] sel; always @(in or sel) if (sel == 0) out = in[0]; else if (sel == 1) out = in[1]; else if (sel == 2) out = in[2]; else out = in[3]; endmodule
case (expr) item_1, .., item_n: stmt1; item_n+1, .., item_m: stmt2; .. default: def_stmt; endcase
module mux4_1(out, in, sel); output out; input [3:0] in; input [1:0] sel; reg out; wire [3:0] in; wire [1:0] sel; always @(in or sel) case (sel) 0: out = in[0]; 1: out = in[1]; 2: out = in[2]; 3: out = in[3]; endcase endmodule
forever stmt;
Executes until sim finishes
initial begin clk = 0; forever #10 clk = ~clk; end other_module1 o1(clk, ..); other_module2 o2(.., clk, ..); endmodule
Mixed Model
Code that contains various both structure and behavioral styles
module simple(Y, c, clk, res); output Y; input c, clk, res; reg Y; wire c, clk, res; wire n; Y not(n, c); // gate-level always @(res or posedge clk) if (res) Y = 0; else Y = n; endmodule
res c clk n