Architecture ! Verilog An Extended Example: Always @ (Posedge CLK) Begin
Architecture ! Verilog An Extended Example: Always @ (Posedge CLK) Begin
Architecture ! Verilog An Extended Example: Always @ (Posedge CLK) Begin
Verilog
An Extended Example
Rc
: <2
5:2
A2
1>
SE
L assign pcinc = pc + 4;
1> 1 R
,…
t ,irq
e Inpu
es
c lk,r t [31
:0] m
ta( em_
e be data
l ;
du
mo
If (done) $finish; e
ul
od
dm
en
Verilog Gate
Logic Synthesis Place & route Mask
netlist
• HDL! logic • create floorplan blocks
• map to target library • place cells in block
• optimize speed, area • route interconnect
• optimize (iterate!)
PC 00
A Instruction
Memory
D
+4
XP 1
RA1
Register RA2
WD
Rc: <25:21> 0 WA
WA
File
RD1 RD2 WE WERF
Z
JT
C: SXT(<15:0>)
PC+4+4*SXT(C)
IRQ Z
ASEL 1 0 1 0 BSEL
Control Logic
PCSEL
RA2SEL A B
ASEL ALU WD R/W Wr
ALUFN
BSEL
WDSEL Data Memory
ALUFN Adr RD
Wr
WERF
WASEL
PC+4
0 1 2 WDSEL
Themes: draw as few fets as possible; maximize use of design techniques that offer
good wire management strategies; use special tools for each type of layout
Like optimizing compilers for C, tools are good for local optimizations but don’t
expect them to rewrite your code and change your algorithm. With practice you’ll
learn what works and what doesn’t…
*adapted from a Stanford EE271 lecture by Mark Horowitz
PCSEL 4 3 2 1 0
Step 1: identify memories
1 Step 2: identify datapaths
PC 00
A Instruction
Memory
What’s left is random logic…
D
+4
2 + WASEL
XP 1
RA1
Register RA2
WD
1
Rc: <25:21> 0 WA
WA
PC Z
RD1
File RD2 WE WERF
JT
C: SXT(<15:0>)
PC+4+4*SXT(C)
IRQ Z
ASEL 1 0 1 0 BSEL
Control Logic
PCSEL
RA2SEL A B
ASEL ALU WD R/W Wr
ALUFN
BSEL
WDSEL Data Memory
ALUFN Adr RD
Wr
WERF
WASEL
PC+4 1
0 1 2 WDSEL
2
Main Datapath
6.371 – Fall 2002 09/11/02 L03 – More Verilog 7
Choosing the right style…
• Structural Verilog
– Use for hierarchy (instantiating other modules)
– Floorplanning tools often require that modules which include structural
verilog not include other styles. In other words the leafs of the
hierarchy are dataflow/behavioral modules, all other modules are pure
structural verilog.
• Dataflow Verilog: assign target = expression
– Use for (most) combinational logic
– Avoids problems with activation list omissions These two styles
are often mixed in
• Behavioral Verilog: always @(…) begin … end a single module
– Use to model state elements (e.g., registers)
– Sometimes useful for combinational logic expressed using for or case
statements
– Simulates much faster than dataflow statements since no waveforms
are produced for signals internal to behavioral block. Here’s where you
can make the tradeoff between simulation speed and debugability.
Beta
Betaverilog
verilogcan
canbe
befound
foundinin/mit/6.371/examples/beta.vl
/mit/6.371/examples/beta.vlor
orHandouts
Handoutswebpage
webpage
6.371 – Fall 2002 09/11/02 L03 – More Verilog 9
Register File
// 2-read, 1-write 32-location register file
module regfile(ra1,rd1,ra2,rd2,clk,werf,wa,wd);
input [4:0] ra1; // address for read port 1 (Reg[RA])
output [31:0] rd1; // read data for port 1
input [4:0] ra2; // address for read port 2 (Reg[RB], Reg[RC] for ST)
output [31:0] rd2; // read data for port 2
input clk;
input werf; // write enable, active high
input [4:0] wa; // address for write port (Reg[RC])
input [31:0] wd; // write data
always
always @(posedge
@(posedge clk)
clk) aa == b;
b;
C always @(posedge clk) b = a;
always @(posedge clk) b = a;
initial begin
always
always @(posedge
@(posedge clk)
clk) aa <=
<= b;
b;
clk = 0; a = 0; b = 1; D always @(posedge clk) b <= a;
#10 clk = 1; always @(posedge clk) b <= a;
#10 $display("a=%d b=%d\n",a,b);
$finish; always
E always @(posedge
@(posedge clk)
clk) begin
begin
end aa <= b;
<= b;
endmodule bb == a;
a; //// urk!
urk! Be
Be consistent!
consistent!
end
end
Rule:
Rule:always
alwayschange
changestate
stateusing <=(e.g.,
using<= (e.g.,inside
insidealways
always @(posedge clk)…))
@(posedge clk)…
6.371 – Fall 2002 09/11/02 L03 – More Verilog 11
module pc(clk,reset,pcsel,offset,jump_addr,
PC
branch_addr,pc,pc_plus_4);
input clk;
input reset; // forces PC to 0x80000000
input [2:0] pcsel; // selects source of next PC
input [15:0] offset; // inst[15:0]
input [31:0] jump_addr; // from Reg[RA], used in JMP instruction
output [31:0] branch_addr; // send to datapath for LDR instruction
output [31:0] pc; // used as address for instruction fetch
output [31:0] pc_plus_4; // saved in regfile during branches, JMP, traps
// compute A and B inputs into alu, also Z bit for control logic
dp_misc misc(rd1zero,rd2zero,asel,bsel,inst,rd1,rd2,branch_addr,
alu_a,alu_b,mem_wdata,z);
// where all the heavy-lifting happens
dp_alu alu(alufn,alu_a,alu_b,mem_addr);
// select regfile write data from PC+4, alu output, and memory data
dp_wdata wdata(wdsel,mem_data,mem_addr,pc_plus_4,wdata);
endmodule
6.371 – Fall 2002 09/11/02 L03 – More Verilog 13
module dp_addsub(fn,alu_a,alu_b,result,n,v,z);
input fn; // 0 for add, 1 for subtract
input [31:0] alu_a;
input [31:0] alu_b;
// A operand
// B operand
Adder
output [31:0] result; // result
output n,v,z; // condition codes computed from result
n = result[31]; // negative
z = ~|result; // zero
v = (alu_a[31] & xb[31] & !n) | (~alu_a[31] & ~xb[31] & n); // overflow
end
endmodule
6.371 – Fall 2002 09/11/02 L03 – More Verilog 14
…
reg [17:0] ctl; // local
always @(inst or interrupt) begin // control rom
Instruction
if (interrupt)
ctl = 16'b0100100000000000; // interrupt decode
(fragment)
else case (inst[31:26])
// ppp aaaaaaww
// bcccw lllllldd
// tsssaabuuuuuuss
// eeeesssffffffeex
// sllleeennnnnnllw
// t210lll54321010r
default: ctl = 16'b0011100000000000; // illegal opcode
6'b011000: ctl = 16'b0000001000000100; // LD
6'b011001: ctl = 16'b0000001000000101; // ST
6'b011011: ctl = 16'b0010000000000000; // JMP
…
6'b111101: ctl = 16'b0000001100001010; // SHRC
6'b111110: ctl = 16'b0000001100011010; // SRAC
endcase
end
Test Jig
reg clk,reset,irq;
beta beta(clk,reset,irq,inst_addr,inst_data,
mem_addr,mem_rd_data,mem_we,mem_wr_data);