FPGA Prototyping Appendix A
FPGA Prototyping Appendix A
// c o n s t a n t d e c l a r a t i o n
l o c a l p a r a m MAX = 2**N - 1;
I? // s i g n a l d e c l a r a t i o n
r e g IN-1:01 r-reg, r - n e x t ;
// body
.............................................
20 // component instantiation
.............................................
/ / no i n s t a n t i a t i o n i n t h i s code
zr / / memory e l e m e n t s
// r e g i s t e r
a l w a y s Q ( p o s e d g e clk, p o s e d g e reset)
i f (reset)
1u r-reg <= 0 ;
else
r-reg <= r-next;
35 // c o m b i n a t i o n a l circuits
// n e x t - . s t a t e logic
always Q*
i f (syn-clr)
r-next = 0 ;
e l s e i f (load)
r-next = d ;
e l s e i f (en)
r-next = r-reg + 1 ;
45 else
r-next = r - r e g ;
// o u t p u t l o g i c
a s s i g n q = r-reg;
a s s i g n max-tick = (r-reg==2**N-1) ? l J b l : l J b O ;
50
endmodule
lo //body
// i n s t a n t i a t i o n o f 1 6 - b i t c o u n t e r , a l l p o r t s u s e d
bin-counter #(.N(16)) counter-16-unit
( . clk(c1k) , .reset (reset),
. syn-clr (syn_clrl6), . load(loadl6), . en(enl61,
IS .d(d), .max-tick(max_tickl6), . q ( q ) ) ;
// i n s t a n t i a t i o n of f r e e - r u n n i n g 8 - b i t c o u n t e r
// with only the max-tick s i g n a l
bin-counter counter-8-unit
(.clk(clk), .reset(reset),
20 . syn-clr (1 'bO), . load(lJbO), . en(lJbl) ,
.d(8'h00), .max-tick(max_tick8), . q O ) ;
endmodule
// C o n d i t i o n a l o p e r a t o r
a s s i g n yl = (r [41) ? 3lb100 : // can a l s o use ( r [ 4 ] = = l ' b l )
(r[31) ? 3'b011 :
(r [ 2 ] ) ? 3'bOlO :
// I f s t a t e m e n t
15 // - each branch can c o n t a i n m u l t i p l e s t a t e m e n t s
// with begin . . . end d e l i m i t e r s
a l w a y s Q*
i f (rC41)
y 2 = 3'blOO;
20 e l s e i f (r [31)
y 2 = 3'b011;
e l s e i f (r C21)
y 2 = 3'bOlO;
e l s e i f (r [I])
zj y 2 = 3'bOOl;
else
y 2 = 3'bOOO;
endmodule
// case s t a t e m e n t
// - each branch can c o n t a i n m u l t i p l e statements
// with begin . . . end d e l i m i t e r s
10 a l w a y s Q*
c a s e (r)
4'b1000, 4'b1001, 4'b1010, 4'b1011,
4'b1100, 4'b1101, 4'b1110, 4'bllll:
yl = 3'blOO;
4'b0100, 4'b0101, 4'b0110, 4'b0111:
yl = 3'b011;
4'b0010, 4'bOOll:
yl = 3'bOlO;
4'b0001:
20 yl = 3'bOOl;
4'boo00 : / / d e f a u l t c a n a l s o be u s e d
yl = 3'bOOO;
endcase
2% // c a s e z s t a t e m e n t
always Q*
c a s e z (r)
4'bl???: y 2 = 3'blOO; // u s e ? f o r don ' t - c a r e
4'b01??: y2 = 3'bOll;
30 4'b001?: y2 = 3'bOlO;
4'bOOOl: y2 = 3'bOOl;
4'bOOOO: y2 = 3'bOOO; // default can a l s o be used
endcase
35 endmodule
/ / - u s e @* t o i n c l u d e a l l i n p u t s i n sensitivity list
/ / - e l s e b r a n c h c a n n o t be o m i t t e d
// - a l l o u t p u t s must be a s s i g n e d i n
a l l branches
10 always O*
i f ( a > b)
begin
gt = l'bl;
eq = l J b O ;
end
e l s e i f (a == b)
begin
gt = l J b O ;
eq = l'bl;
end
else // e l s e branch cannot be o m i t t e d
begin
gt = l J b O ;
eq = l J b O ;
25 end
endmodule
// - u s e @* t o i n c l u d e a l l i n p u t s i n s e n s i t i v i t y list
// - a s s i g n each o u t p u t with a d e f a u l t value
always Q*
lo begin
gt = l'bO; // d e f a u l t value f o r g t
e q = I'bO; / / d e f a u l t v a l u e f o r eq
i f ( a > b)
gt = l'bl;
e l s e i f (a == b)
eq = l'bl;
end
endmodule
/ / u s e n o n b l o c k a s s i g n m e n t ( <= )
I always Q ( p o s e d g e c l k , posedge reset)
i f (reset)
q 2 - r e g <= 8'bO;
else
q 2 - r e g <= q 2 - n e x t ;
3
.............................................
// register w i t h e n a b l e and a s y n c h r o n o u s reset
.............................................
// use nonblock assignment ( <= )
474 SAMPLE VERILOG TEMPLATES
3 always @ ( p o s e d g e c l k , posedge r e s e t )
if (reset)
q 3 - r e g <= 8 ' b O ;
else i f (en)
q 3 - r e g <= q 3 - n e x t ;
35
endmodule
// s i g n a l d e c l a r a t i o n
15 r e g [B-1:Ol a r r a y - r e g [2**W-1:Ol ;
/ / body
// w r i t e
operation
always @ ( p o s e d g e c l k )
20 i f (wr-en)
a r r a y - r e g [w-addr] <= w - d a t a ;
// read o p e r a t i o n
assign r-data = array-reg [r-addrl ;
// s y n - c l r load en q* operation
5 //
// I - - 0 synchronous c l e a r
// 0 1 - d parallel load
// 0 0 I q+l c o u n t up
REGULAR SEQUENTIAL CIRCUITS 475
// 0 0 0 9 pause
module b i n - c o u n t e r
# ( p a r a m e t e r N=8) // default 8
(
input wire c l k , r e s e t , // c l o c k & r e s e t
IS input wire syn-clr, l o a d , e n , // i n p u t c o n t r o l
i n p u t w i r e [N-1:01 d , // i n p u t d a t a
output wire max-tick, // o u t p u t s t a t u s
o u t p u t w i r e [N-1:OI q // o u t p u t d a t a
1;
20
// c o n s t a n t d e c l a r a t i o n
l o c a l p a r a m MAX = 2**N - 1 ;
// s i g n a l d e c l a r a t i o n
r e g [N-1:Ol r - r e g , r - n e x t ;
// register
.............................................
30 // r e g i s t e r
always O(posedge c l k , posedge r e s e t )
if ( r e s e t )
r - r e g <= 0 ;
else
3s r - r e g <= r - n e x t ;
.............................................
// n e x t - s t a t e logic
.............................................
a l w a y s Q*
40 if (syn-clr)
r-next = 0;
else if (load)
r-next = d ;
else if (en)
r-next = r-reg + 1;
else
r-next = r-reg;
.............................................
// output logic
.............................................
assign q = r-reg;
a s s i g n max-tick = (r-reg==2**N-1) ? l'bl : 17bO;
endmodule
(a) State diagram (b) A S M chart
A.7 FSM
// symbolic s t a t e d e c l a r a t i o n
10 l o c a l p a r a m [1:0] SO = 2 I b 0 0 ,
sl = 2'b01,
s2 = 2'biO;
// s i g n a l d e c l a r a t i o n
r e g [I: 01 s t a t e - r e g , s t a t e - n e x t ;
FSM 477
// s t a t e
register
always Q ( p o s e d g e clk, posedge r e s e t )
if (reset)
state-reg <= s o ;
else
state-reg <= s t a t e - n e x t ;
// n e x t - s t a t e l o g i c and o u t p u t logic
always Q*
zs begin
state-next = state-reg; // d e f a u l t n e x t s t a t e : t h e same
y l = l'bO; // d e f a u l t o u t p u t : 0
yo = l ' b O ; // d e f a u l t o u t p u t : 0
case (state-reg)
SO : begin
yl = l'bl;
if (a)
if (b)
begin
state-next = s2;
yo = l ' b l ;
end
else
state-next = sl;
end
sl: begin
yl = l'bl;
if (a)
state-next = SO;
end
s 2 : s t a t e - n e x t = SO;
d e f a u l t : s t a t e - n e x t = SO;
endcase
end
sa endmodule
Figure A.2 ASMD chart of an FSMD template.
A.8 FSMD
// symbolic s t a t e declaration
l o c a l p a r a m 11 : 01
i d l e = 2 'b00,
op = 2'b01,
IS done = 2 ' b l O ;
// s i g n a l declaration
r e g [ I : 01 state-reg , state-next ;
reg [19:0] to-reg , to-next , t l - r e g , tl-next ;
20 reg [4:01 n-reg , n-next ;
// body
// s t a t e & d a t a registers
always O(posedge c l k , posedge r e s e t )
25 if (reset)
begin
s t a t e - r e g <= i d l e ;
t o - r e g <= 0 ;
t l - r e g <= 0 ;
n - r e g <= 0 ;
end
else
begin
s t a t e - r e g <= s t a t e - n e x t ;
t o - r e g <= t o - n e x t ;
t l - r e g <= t l - n e x t ;
n - r e g <= n - n e x t ;
end
// n e x t - s t a t e l o g i c and d a t a p a t h f u n c t i o n a l units
40 a l w a y s O*
begin
state-next = state-reg; // d e f a u l t r e t u r n t o same s t a t e
ready = l'bO; // default output 0
done-tick = l'bO; // default output 0
to-next = to-reg; // d e f a u l t keep p r e v i o u s value
tl-next = tl-reg; // d e f a u l t keep p r e v i o u s value
n-next = n-reg; // d e f a u l t keep p r e v i o u s value
case (state-reg)
idle :
begin
ready = l J b l ;
if (start)
begin
to-next = 0;
tl-next = 20'dl;
n-next = i ;
state-next = op;
end
end
61) op :
if (n-reg==O)
begin
tl-next = 0;
state-next = done;
end
else if (n-reg==l)
s t a t e - n e x t = done;
else
begin
tl-next = tl-reg + to-reg;
to-next = t l - r e g ;
n-next = n-reg - 1 ;
end
done :
begin
done-tick = l ' b l ;
state-next = idle;
end
default: state-next = idle;
endcase
end
/ / OLltplrt
assign f = tl-reg;
# c l o c k and r e s e t
.........................................................
NET " c l k " LOC = " T 9 " ;
NET " r e s e t " LOC = " L 1 4 " ;
# 8 slide switches
NET " s w < O > " LOC = " F 1 2 " ,
NET "sw<l>" LOC = "G12";
NET "sw<2>" LOC = "H14";
NET "sw<3>" LOC = "H13";
NET "sw<4>" LOC = "514";
NET "sw<5>" LOC = "513";
NET "sw<6>" LOC = "K14";
NET "sw<7>" LOC = "K13";
.........................................................
# RS232
.........................................................
NET " r x " LOC = " T 1 3 " 1 DRIVE=8 I SLEW=SLOW;
NET " t x " LOC = " R 1 3 " I DRIVE=8 I SLEW=SLOW;
.........................................................
# 4 - d i g i t t i m e - m u l t i p l e x e d 7-segment LED d i s p l a y
.........................................................
# d i g i t enable
NET " a n < O > " LOC = "D14";
NET " a n < l > " LOC = "G14";
NET " a n < 2 > " LOC = "F14";
NET " a n < 3 > " LOC = "E13";
# 7-segment l e d segments
NET " s s e g < 7 > " LOC = " P 1 6 " ; # decimul point
NET "sseg<6>' LOC = " E 1 4 " ; # segment a
NET " s s e g < 5 > " LOC = "G13" , # segment b
NET " s s e g < 4 > " LOC = " N 1 5 " ; # segment c
NET " s s e g < 3 > " LOC = " P 1 5 " ; # segment d
NET " s s e g < 2 > " LOC = " R 1 6 " ; # segment e
NET " s s e g < l > " LOC = " F 1 3 " ; # segment f
NET " s s e g < O > " LOC = " N 1 6 " ; # segment g
.........................................................
# 8 d i s c r e t e LEDs
.........................................................
NET "led<O>" LOC = "K12";
NET "led<l>" LOC = "P14";
NET "led<2>" LOC = "L12";
NET "led<3>" LOC = "N14";
NET "led<4>" LOC = "P13";
NET "led<5>" LOC = "N12";
NET "led<6>" LOC = '1P12";
NET "led<7>" LOC = "P11":
.........................................................
# VGA o u t p u t s
.........................................................
NET "rgb<2>" LOC = "R12" I DRIVE=8 I SLEW=FAST;
NET "rgb<i>" LOC = "T12" I DRIVE=8 1 SLEW=FAST;
NET "rgb<O>" LOC = "R11" I DRIVE=8 I SLEW=FAST;
NET "vsync" LOC = " T 1 0 " I DRIVE=8 I SLEW=FAST;
NET "hsync" LOC = "R9" I DRIVE=8 I SLEW=FAST;
.........................................................
# PS2 port
.........................................................
NET "ps2c" LOC="M1GW I IOSTANDARD=LVCMOS33 I DRIVE=8 ISLEW=SLOW:
NET "ps2d1'LOC="M15" I IOSTANDARD=LVCMOS33 I DRIVE=8 ISLEW=SLOW;
.........................................................
# two SRAM chips
.........................................................
# shared 18-bit memory a d d r e s s
NET "ad<17>" LOC="L3" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<16>" LOC="KS1' I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<15>" LOC="K3" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<14>" LOC="J3" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<13>I1 LOC="J4" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<12>" LOC="H4" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<ll>" LOC="H3" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<lO>" LOC="GS" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<9>" LOC="E4" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<8>" LOC="E3" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<7>" LOC="F4" I IOSTANDARD = LVCMOS33 I SLEW=FAST:
NET "ad<6>" LOC="F3" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<5>" LOC="G4" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
IOI NET "ad<4>" LOC="L4" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<3>" LOC="M3" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<2>I1 LOC="M4" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<l>" LOC="N3" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET "ad<O>" LOC="LSM I IOSTANDARD = LVCMOS33 I SLEW=FAST;
# shared oe, we
NET "oe-n" LOC="K4" I IOSTANDARD = LVCMOS33 I SLEW=FAST;
NET " we-n" LOC="G3" 1 IOSTANDARD = LVCMOS33 I SLEW=FAST;
# sram chip 1 data, ce, ub, lb
NET "dio-a<l5>" LOC="RIN I IOSTANDARD=LVCMOS33
NET "dio-a<l4>" LOC="PIM I IOSTANDARD=LVCMOS33
NET "dio-a<13>" LOC="L2" I IOSTANDARD=LVCMOS33
NET "dio-a <l2>It LOC="J2" I IOSTANDARD=LVCMOS33
NET "dio-a<ll>" LOC="Hll' I IOSTANDARD=LVCMOS33
NET "dio-a<lO>" LOC="F2" I IOSTANDARD=LVCMOS33
NET "dio-a <9>" LOC="P8" I IOSTANDARD=LVCMOS33
NET "dio-a<8>" LOC="D3" I IOSTANDARD=LVCMOS33
NET "dio-a<7>" LOC="B1" I IOSTANDARD=LVCMOS33
NET "dio-a<6>" LOC="C1" I IOSTANDARD=LVCMOS33
NET "dio-a<5>" LOC="C2" I IOSTANDARD=LVCMOS33
NET " dio-a <4>" LOC="RS1' I IOSTANDARD=LVCMOS33
NET "dio-a<3>" LOC="TSN I IOSTANDARD=LVCMOS33
NET "dio-a<2>" LOC="RGM I IOSTANDARD=LVCMOS33
NET "dio-a<l>" LOC="T8" I IOSTANDARD=LVCMOS33
NET " dio-a <O>" LOC="N7" I IOSTANDARD=LVCMOS33
NET "ce-a-nu LOC="P7" I IOSTANDARD=LVCMOS33
NET "ub-a-n" LOC="T4" I IOSTANDARD=LVCMOS33
53 BOARD CONSTRAINT FILE (S3.UCF) 483
.........................................................
# Timing constraint of S3 50-MHz onboard oscillator
# name of the clock signal is clk
.........................................................
NET "clk" TNM-NET = "clk";
TIMESPEC "TS-clk" = PERIOD "clk" 40 ns HIGH 50 %;