Verilog Example
Verilog Example
Courtesy of Arvind
L03-1
High-Level Behavioral
Courtesy of Arvind
L03-2
always @(*)
begin
out = 2d0;
if (in1 == 1)
out = 2d1;
else if (in2 == 1)
out = 2d2;
end
L03-3
L03-4
An example
wire A_in, B_in, C_in;
reg A_out, B_out, C_out;
always @( posedge clk )
begin
A_out <= A_in;
B_out <= A_out + 1;
C_out <= B_out + 1;
end
+1
+1
Courtesy of Arvind
L03-5
Another way
wire A_in, B_in, C_in;
reg A_out, B_out, C_out;
always @( posedge clk )
begin
A_out <= A_in;
B_out <= B_in;
C_out <= C_in;
end
assign B_in = A_out + 1;
assign C_in = B_out + 1;
+1
+1
Courtesy of Arvind
L03-6
An example:
Some wrong solutions
wire A_in, B_in, C_in;
reg A_out, B_out, C_out;
Courtesy of Arvind
+1
+1
Syntactically illegal
L03-7
+1
+1
Courtesy of Arvind
L03-8
+1
posedge clk )
A_in;
B_in;
C_in;
+1
Is it correct?
No; Do not use blocking
assignments in @posedge clk
blocks. It is forbidden in this
class.
Courtesy of Arvind
L03-9
Courtesy of Arvind
L03-10
!"#$%&'()*+,-$+.+/0)1/&+$%2)*/,"34+5()
If you treat verilog as a language for coding up hardware you have already
designed on paper/whiteboard, you will not need to rely on this.
"$6"#())
))78-%(/02/)5$H:)
));/2+3)
))))))))I<).%%?)
))))))))I<);"&?
))/30)
"$6"#())
))78-%(/02/)5$H:)
));/2+3)
))))))")I<)
))))))J)I<
))/30)
"$6"#())
))78-%(/02/)5$H:)
"$6"#())
))78-%(/02/)5$H:)
));/2+3)
)))))).%%)<)K?)
))))));"&)<)&?
))/30)
N3%&0/&/0)
(sequential
right hand side)
@AB@AC@)
));/2+3)
))))))K)I<)
))))))&)I<
))/30)
"$6"#()789:)
));/2+3)
)))));))))<).8":)
)))))5))))<)0)=)>?)
)/30)
"((+23)0)<);=B?)
"((+23)/)<)5=B?)
)
"N4%F%&0/&/0) (combinational)
N3%&0/&/0)
(sequential
left hand side)
L$%5H)M02/)
DCEFG)
!"#$%&'()*+,-$+.+/0)1/&+$%2)*/,"34+5()
"$6"#()789:)
));/2+3)
)))));))))<).8":)
)))))5))))<)0)=)>?)
)/30)
"((+23)0)<);=B?)
"((+23)/)<)5=B?)
"$6"#())
))78-%(/02/)5$G:)
));/2+3)
))))))))H<).%%?)
))))))))H<);"&?
))/30)
"$6"#())
))78-%(/02/)5$G:)
));/2+3)
))))))")H<)
))))))I)H<
))/30)
"$6"#())
))78-%(/02/)5$G:)
"$6"#())
))78-%(/02/)5$G:)
));/2+3)
)))))).%%)<)J?)
))))));"&)<)&?
))/30)
));/2+3)
))))))")H<)
))))))I)H<
))/30)
K$%5G)L02/)
@AB@AC@)
DCEF@)
Courtesy of Arvind
L03-13
GCD in C
int GCD( int inA, int inB)
{
int done = 0;
int A = inA;
int B = inB;
while ( !done )
{ if ( A < B )
{ swap = A;
A = B;
B = swap;
}
else if ( B != 0 )
A = A - B;
else
done = 1;
}
return A;
}
L03-14
GCD in C
int GCD( int inA, int inB)
{
int done = 0;
int A = inA;
int B = inB;
while ( !done )
{ if ( A < B )
{ swap = A;
A = B;
B = swap;
}
else if ( B != 0 )
A = A - B;
else
done = 1;
}
return A;
}
State
Less-Than Comparator
Equal Comparator
Subtractor
L03-15
Step 1: Design an
appropriate port interface
input_available
result_rdy
idle
result_taken
operand_A
result_data
operand_B
clk
reset
Courtesy of Arvind
L03-16
zero?
lt
A = inA; B = inB;
A
sub
B
Courtesy of Arvind
while ( !done )
begin
if ( A < B )
swap = A;
A = B;
B = swap;
else if (B != 0)
A = A - B;
else
done = 1;
End
Y = A;
L03-17
B
B
sel en
B=0
A<B
zero?
lt
designed to
be either busy
or waiting for
input or
waiting for
output to be
picked up
A = inA; B = inB;
A
sub
B
Courtesy of Arvind
while ( !done )
begin
if ( A < B )
swap = A;
A = B;
B = swap;
else if (B != 0)
A = A - B;
else
done = 1;
End
Y = A;
L03-18
A A
sel en
B B
sel en
B=0
A<B
zero?
lt
sub
Courtesy of Arvind
L03-19
A A
sel en
B B
sel en
B=0
A<B
zero?
lt
sub
wire [W-1:0] A;
vcEDFF_pf#(W) A_pf
( .clk (clk),
.en_p (A_en),
.d_p (A_out),
.q_np (A)
);
Courtesy of Arvind
L03-20
verilog
vcMux2#(W) B_mux
for datapath
( .in0 (operand_B), registers.
.in1 (A),
.sel (B_sel),
.out (B_out)
);
vcEDFF_pf#(W) B_pf
( .clk (clk),
.en_p (B_en),
.d_p (B_out),
.q_np (B)
);
assign
assign
assign
assign
B_zero = (B==0);
A_lt_B = (A < B);
sub_out = A - B;
result_data = A;
Courtesy of Arvind
L03-21
input_availble
CALC
result_taken DONE
Courtesy of Arvind
L03-22
Courtesy of Arvind
WAIT: begin
A_sel
= A_SEL_IN;
A_en
= 1'b1;
B_sel
= B_SEL_IN;
B_en
= 1'b1;
input_available = 1'b1;
end
CALC: if ( A_lt_B )
A_sel = A_SEL_B;
A_en
= 1'b1;
B_sel = B_SEL_A;
B_en
= 1'b1;
else if ( !B_zero )
A_sel = A_SEL_SUB;
A_en
= 1'b1;
end
DONE: result_rdy = 1'b1;
Courtesy of Arvind
L03-24
always @(*)
begin
// Default is to stay in
the same state
state_next = state;
if (reset)
case ( state )
WAIT :
if ( input_available )
state_next = CALC;
CALC :
if ( B_zero )
state_next = DONE;
DONE :
if ( result_taken )
state_next = WAIT;
endcase
end
Courtesy of Arvind
reset
WAIT
input_availble
CALC
(B=0)
result_taken
DONE
L03-25
Generic
Test
Source
B B
sel en
B=0
A<B
zero?
lt
sub
B
Courtesy of Arvind
Generic
Test
Sink
L03-26
C
Model
RTL
Model
Test Outputs
Test Outputs
Identical?
Courtesy of Arvind
L03-27
Courtesy of Arvind
L03-28
5 instructions
Lecture examples
Courtesy of Arvind
L03-29
SMIPSv1 ISA
Instruction
Semantics
Hardware
Requirements
if ( R[rs] != R[rt] )
pc := pc + sext
(offset) + 4
lw rt, offset(rs)
sw rt, offset(rs)
M[R[rs] + sext(offset)]
= R[rt]
Courtesy of Arvind
Courtesy of Arvind
L03-31
Identify memories,
datapaths, and random logic
L03-32
Courtesy of Arvind
L03-33
SMIPSv1 datapath
module smipsProcDpath_pstr
( input clk, reset,
// Memory ports
output [31:0] imemreq_addr,
output [31:0] dmemreq_addr,
output [31:0] dmemreq_data,
input [31:0] dmemresp_data,
// Controls signals (ctrl->dpath)
input
pc_sel,
input [ 4:0] rf_raddr0,
input [ 4:0] rf_raddr1,
input
rf_wen,
input [ 4:0] rf_waddr,
input
op0_sel,
input
op1_sel,
input [15:0] inst_imm,
input
wb_sel,
// Control signals (dpath->ctrl)
output
branch_cond_eq,
output [7:0] tohost_next
);
Courtesy of Arvind
L03-34
L03-35
LW
SW
ADDIU
BNE
32'b100011_?????_?????_?????_?????_??????
32'b101011_?????_?????_?????_?????_??????
32'b001001_?????_?????_?????_?????_??????
32'b000101_?????_?????_?????_?????_??????
localparam cs_sz = 8;
reg [cs_sz-1:0] cs;
always @(*)
begin
cs = {cs_sz{1'b0}};
casez ( imemresp_data )
//
op0 mux
//
br type sel
`ADDIU: cs ={br_pc4, op0_sx,
`BNE : cs ={br_neq, op0_sx2,
`LW
: cs ={br_pc4, op0_sx,
`SW
: cs ={br_pc4, op0_sx,
`MTC0 : cs ={br_pc4, op0_x,
endcase
end
Courtesy of Arvind
wb mux
sel
wmx_alu,
wmx_x,
wmx_mem,
wmx_x,
wmx_x,
rfile
wen
1'b1,
1'b0,
1'b1,
1'b0,
1'b0,
mreq
r/w
mreq_x,
mreq_x,
mreq_r,
mreq_w,
mreq_x,
mreq
val
1'b0,
1'b0,
1'b1,
1'b1,
1'b0,
tohost
en
1'b0};
1'b0};
1'b0};
1'b0};
1'b1};
L03-36
L03-37
synthesizable Verilog
! Parameterized models provide the foundation for
reusable libraries of components
! Use explicit state to prevent unwanted state
inference and to more directly represent the
desired hardware
! Begin your RTL design by identifying the external
interface and then move on to partition your
design into the memories, datapaths, and control
logic
Courtesy of Arvind
L03-38
Behavioral Verilog:
For TestBenchs Only
! Characterized by heavy use of sequential
Courtesy of Arvind
L03-39
L03-40
L03-41