Verilog HDL
2007/06/29 15:29
http://blog.naver.com/PostPrint.nhn?blogId=babojay&logNo=40039249819#
Chapter 7.
Behavioral Modeling
*Objectives
- always, initial in behavioral
modeling
- blocking, nonblocking procedural assignments
-
Delay-based timing control. regular delay, intra-assignment delay, zero
delay
- Event-based timing control. regular event control, named event
control, event OR control
- Level-sensitive timing control
-
Conditional statement. if and else.
- Multiway branching. case, casex,
casez
- Looping. while, for, repeat, forever.
- Sequential
block, parallel block.
- Naming of blocks, diabling of named
blocks
7.1 Structured
Procedures
- always, initial
: °¡Àå ±âº»ÀûÀÎ structuered procedure ±¸¹®
- always¿Í initial ¾È¿¡ ´Ù¸¥ ¸ðµç behavioral ±¸¹®µéÀÌ
À§Ä¡ÇÑ´Ù.
- Concurrency : always,
initial±¸¹® µ¿ÀÛÈ帧Àº µ¿½Ã¿¡ º´·Ä·Î ÀÌ·ç¾îÁø´Ù.
- ÀÌ·± µ¿ÀÛ È帧µéÀº ½Ã¹Ä·¹ÀÌ¼Ç ½Ã°£
0¿¡¼ ½ÃÀÛÇÑ´Ù.
- nestedµÉ ¼ö ¾ø´Ù.
7.1.1 initial statement
- initial ±¸¹®¾È¿¡ ÀÖ´Â ¸ðµç ±¸¹®Àº
initial blockÀ» ±¸¼ºÇÑ´Ù.
- initial blockÀº ½Ã°£ 0¿¡¼ ½ÃÀÛÇÏ¸ç ½Ã¹Ä·¹ÀÌ¼Ç µ¿¾È ´Ü Çѹø
½ÇÇàµÈ´Ù.
- ¿©·¯°³ÀÇ initial blockÀÌ ÀÖ´Â °æ¿ì °¢°¢ ½Ã°£ 0¿¡¼ ½ÇÇà½ÃÀÛÇÏ¸ç ´Ù¸¥ block¿¡ ¿µÇâÀ» ÁÖÁöµµ
¹ÞÁöµµ ¾Ê°í µ¶¸³ÀûÀ¸·Î ½ÇÇàµÈ´Ù.
- ´Ù¼öÀÇ behavioral ±¸¹®Àº bigin°ú end·Î ¹¾îÁÖ¾î¾ß ÇÑ´Ù. ´Ü ÇÑ °³ÀÇ ±¸¹®À» ¹¾îÁÙ ÇÊ¿ä´Â ¾ø´Ù. ÇÏÁö¸¸
°¡µ¶¼º°ú ±¸¹® Ãß°¡ÀÇ ¿©Áö¸¦ À§Çؼ begin°ú
end¸¦ »ý·«ÇÏÁö ¾Ê´Â °ÍÀÌ
ÁÁ´Ù.
Ex)
// initial Statement
module stimulus;
reg x, y, a, b, m;
initial
m =
1'b0; //
time 0¿¡¼ ½ÇÇà
initial
begin
#5 a =
1'b1; // time 5¿¡¼
½ÇÇà
#25 b =
1'b0; // time 5ÀÇ ½ÃÁ¡¿¡¼ 25µÚ(30)¿¡ b = 1'b0;
½ÇÇà
end
initial
begin
#10 x =
1'b0; // time 10¿¡
½ÇÇà
#25 y =
1'b1; // time 10ÀÇ ½ÃÁ¡¿¡¼ 25µÚ(35)¿¡ y =
1'b1; ½ÇÇà
end
initial
#50
$finish; // time 50¿¡
½ÇÇà
endmodule
* À§ÀÇ ¿¹Á¦¿¡¼ 3°³ÀÇ initial blockÀÌ µ¿½Ã¿¡ ½ÇÇà
** #<delay>µÚ¿¡ ¿À´Â ±¸¹®Àº ÇöÀç½ÃÁ¡(½ÇÇ൵Áß)¿¡¼
delay¸¸Å ÈÄ¿¡ ½ÇÇàµÈ´Ù.
- ½ÇÇà°á°ú´Â ´ÙÀ½°ú °°´Ù.
time
½ÇÇàµÇ´Â ±¸¹®
0
m = 1'b0;
5
a = 1'b1;
10
x = 1'b0;
30
b = 1'b0;
35
y = 1'b1;
50
$finish;
7.1.2 always Statement
- always±¸¹® ¾È¿¡ ÀÖ´Â ¸ðµç ±¸¹®Àº
always blockÀ» ±¸¼ºÇÑ´Ù.
- time 0¿¡¼ ½ÃÀÛÇؼ °è¼Ó ¹Ýº¹µÇ´Â ÇüÅ·Π½ÇÇàµÈ´Ù.
- clock
generator°°Àº Àü¿øÀÌ ÀÖ´Â µ¿¾È °è¼Ó ¹Ýº¹µÇ´Â µ¿ÀÛÀ» ±â¼úÇÒ¶§ À¯¿ëÇÏ´Ù.
Ex)
// always
Statement
module clock_gen;
reg clock;
initial
clock =
1'b0;
// ÃʱâÈ
always
#10 clock =
~clock; // 10¸¶´Ù ¹ÝÀü (ÁÖ±â 20)
initial
#1000 $finish;
endmodule
7.2 Procedural Assignments
- reg, integer, real, time
º¯¼öµéÀÇ °ªÀ» º¯°æÇÒ ¶§ »ç¿ëÇÑ´Ù.
- º¯°æµÈ °ªµéÀº ´Ù¸¥ °ªÀ¸·Î reassignÇϱâ Àü±îÁö Àý´ë º¯ÇÏÁö
¾Ê´Â´Ù.
- ¿À¸¥ÂÊ ¼ö½ÄÀÇ º¯È°¡ ¿ÞÂÊÀÇ °ªÀ» º¯È½ÃÅ°´Â continuous assignment¿Í ±¸º°µÈ´Ù.
Syntax]
<assignment>
::=<lvalue> =
<expression>
- <lvalue>¿¡
¿Ã¼ö ÀÖ´Â °ÍµéÀº ´ÙÀ½°ú °°´Ù.
* reg, integer, real, time register
variable, memory element
* À§ÀÇ º¯¼öÀÇ bit-select (ex.,
addr[0])
* À§ÀÇ º¯¼öÀÇ part-select (ex.,
addr[31:19])
* À§ÀÇ º¯¼öÀÇ concatenation
7.2.1 Blocking Assignments
- ±¸¹®ÀÌ ¸í½ÃµÈ ¼ø¼´ë·Î ½ÇÇàµÈ´Ù
- = ¿¬»êÀÚ¸¦ »ç¿ëÇÑ´Ù.
Ex)
// Blocking
Statements
reg x, y,
z;
reg [15:0] reg_a, reg_b;
integer
count;
initial
begin
x = 0; y = 1; z =
1;
// time 0 ¿¡
½ÇÇà
count =
0;
// time 0 ¿¡ ½ÇÇà
reg_a = 16'b0; reg_b = reg_a; // time 0 ¿¡ ½ÇÇà
#15 reg_a[2] =
1'b1;
// time 15¿¡ ½ÇÇà
#10 reg_b[15:13] = {x, y, z}; // time 25¿¡
½ÇÇà
count = count +
1;
// time 25¿¡ ½ÇÇà
end
* x = 0;±¸¹®ÀÌ ½ÇÇàµÇ°í y
= 1;±¸¹®ÀÌ ½ÇÇàµÈ´Ù.
** count = count +
1;±¸¹®Àº °¡Àå ¸¶Áö¸·¿¡ ½ÇÇàµÈ´Ù.
*** ¿À¸¥ÂÊ ¼ö½ÄÀÇ bit¼ö°¡ ¿ÞÂʺ¸´Ù Å©¸é LSBÂÊÀ» ¼±ÅÃÇÏ°í ¿ÞÂÊ¿¡
¸ÂÃß¾î ÀÚ¸¥´Ù.
**** ¿ÞÂÊ ¼ö½ÄÀÇ bit¼ö°¡ Å©¸é ³²´Â ºÎºÐÀº 0À¸·Î ä¿öÁø´Ù.
7.2.2 Nonblocking Assignments
- sequential blockÀÇ ±¸¹®ÀÇ ½ÇÇàÀ» ¹æÇØÇÏÁö ¾Ê°í ½Ã°£°èȹ¿¡ µû¶ó assignÀÌ ÀÌ·ç¾îÁø´Ù.
-
<= ¿¬»êÀÚ¸¦ »ç¿ëÇÑ´Ù.
Ex)
// Nonblocking
Assignments
reg x, y, z;
reg [15:0]
reg_a, reg_b;
integer count;
initial
begin
x = 0; y = 1; z =
1;
count =
0;
reg_a = 16'b0; reg_b =
reg_a;
reg_a[2] <= #15
1'b1;
// 15 tu¿¡ ½ÇÇà
reg_b[15:13]
<= #10 {x, y, z}; // 10 tu¿¡
½ÇÇà
count <= count +
1;
// 0 tu¿¡ ½ÇÇà
end
* x = 0;¿¡¼ºÎÅÍreg_b
= reg_a;±¸¹®±îÁö´Â 0 tu¿¡ ¼øÂ÷ÀûÀ¸·Î ½ÇÇàµÈ´Ù.
** nonblocking(<= ¿¬»êÀÚ¸¦ »ç¿ëÇÑ ±¸¹®)Àº µ¿½Ã¿¡ ½ÇÇàµÈ´Ù.
*** °°Àº tu¿¡
blocking°ú nonblockingÀÌ Á¸ÀçÇÑ´Ù¸é(count = 0; °ú count <= count +1;) blockingÀÌ ¸ÕÀú ½ÇÇàµÇ°í ³ ÈÄ nonblockingÀÌ
½ÇÇàµÈ´Ù.
¡á Application of nonblocking assignments
- nonblocking assignment´Â µ¿½Ã¿¡ ÀϾ´Â µ¥¾îÅÍ Àü¼Û¿¡ »ç¿ëµÈ´Ù.
Ex)
// Concurrent
data transfer
always @(posedge
clock) // at positive edge of
clock
begin
reg1 <= #1 in1;
reg2 <=
@(negedge clock) in2 ^ in3;
reg3
<= #1 reg1;
end
* read : clockÀÇ positive edge¿¡¼ ¿À¸¥ÂÊ ¼ö½ÄÀÇ
º¯¼öin1, in2, in3, reg1¸¦ Àоîµé¿© °è»êÇÏ°í Àӽà ÀúÀåÇÑ´Ù.
**
write : °¢°¢ Á¤ÇØÁø delay¿Í event¿¡ µû¶ó ¶§°¡ µÇ¸é ¿ÞÂÊÀ¸·Î assignµÈ´Ù.
- ÀÌÇظ¦ µ½±â À§ÇØ µÎ º¯¼öÀÇ °ªÀ» ¼·Î ¹Ù²Ù´Â data swapÀ» blocking°ú nonblockingÀ¸·Î Ç¥ÇöÇØ º¸ÀÚ
Ex)
// Blocking
Statement
always @(posedge
clock)
a =
b;
always @(posedge
clock)
b = a;
// Nonblocking
Statement
always @(posedge
clock)
a <=
b;
always @(posedge
clock)
b <= a;
* À§ÀÇ blockingÀÇ °æ¿ì a = b;¿Í
b = a;Áß¿¡¼ ¾î´À°ÍÀÌ ¸ÕÀú ½ÇÇàµÉÁö´Â ¾Æ¹«µµ ¸ð¸¥´Ù. ÀÌ·± °æ¿ì µÎ º¯¼öÀÇ °ªÀÌ Á¦´ë·Î
swapµÇ´Â °ÍÀ» º¸ÀåÇÒ ¼ö ¾ø´Ù. ¾Æ¸¶µµ °á°ú´Â µÎ º¯¼ö¿¡ ¸ðµÎ °°Àº °ª(a, bÁß Çϳª)ÀÌ ÀúÀåµÇ°Ô µÉ °ÍÀÌ´Ù.
** nonblockingÀÇ °æ¿ì b¿Í aÀÇ °ªÀ» Àоî Àӽà ÀúÀå¼Ò¿¡ ÀúÀåÇÏ¿´´Ù°¡ a¿Í b¿¡ °¢°¢ assignÇÏ°Ô µÈ´Ù. À̸¦
blockingÀ¸·Î Ç¥ÇöÇÏ¸é ´ÙÀ½ÀÇ ¿¹¿Í °°ÀÌ µÉ °ÍÀÌ´Ù.
Ex)
// Nonblocking
expressed with blocking statement
always @(posedge
clock)
begin
temp_a =
a;
temp_b =
b;
a =
temp_b;
b =
temp_a;
end
- nonblockingÀ» »ç¿ëÇÒ °æ¿ì ½Ã¹Ä·¹ÀÌÅÍÀÇ ¼º´ÉÀúÇϸ¦ À¯¹ßÇϰųª ¸Þ¸ð¸® »ç¿ë·®ÀÌ ´Ã¾î³¯ °¡´É¼ºÀÌ ÀÖ´Ù.
7.3 Timing Controls
- ´Ù¾çÇÑ behavioral timing controlµéÀÌ ÀÖ´Ù.
- ÀÌ·¯ÇÑ timing control ±¸¹®ÀÌ
¾øÀ¸¸é ½Ã¹Ä·¹ÀÌ¼Ç timeÀº ÁøÇàµÇÁö ¾Ê´Â´Ù.
7.3.1 Delay-Based Timing
Control
- ±¸¹®ÀÌ ³ªÅ¸³ª¼ ½ÇÇàµÇ±â±îÁöÀÇ ½Ã°£À» ÁöÁ¤ÇÑ´Ù.
Syntax]
<delay>
::=
#<NUMBER>
||=
#<identifier>
||=
#(<mintypmax_expression>
<,<mintypmax_expression>>*)
¡á Regular delay control
- Procedural assignmentÀÇ ¿ÞÂÊ¿¡ ÁöÁ¤ÇÑ non-zero delay.
- ¸í½ÃµÈ delay¸¸Å
±â´Ù·È´Ù°¡ ¿À¸¥ÂÊ statement¸¦ ½ÇÇàÇÑ´Ù.
Ex)
//
Regular Delay Control
parameter latency =
20;
parameter delta = 2;
reg x, y, z, p, q;
initial
begin
x =
0;
// no delay
#10 y =
1;
// number delay
#latency z =
0;
// identifier delay
#(latency + delta) p = 1; // expression delay
#y x = x +1; // identifier delay
#(4:5:6) q =
0;
// min, typ, max delay
end
¡á Intra-assignment delay control
- assignment ¿¬»êÀÚÀÇ ¿À¸¥ÂÊ¿¡ ¸í½ÃÇÑ delay
- ¿¬»êÀÚ ¿À¸¥ÂÊÀÇ ¼ö½ÄÀ» °è»êÇÏ°í ³ ÈÄ ¸í½ÃµÈ
delay¸¸Å ±â´Ù·È´Ù°¡ assignÀ» ¼öÇà. Regular delay¿Í ±¸º°
Ex)
//
Intra-assignment delay
reg x, y, z;
initial
begin
x = 0; z =
0;
y = #5 x +
z; // 0 tu¿¡ x¿Í zÀÇ °ªÀ» ÃëÇØ ¼ö½ÄÀ»
°è»êÇÑÈÄ
// 5 tuµ¿¾È ±â´Ù·È´Ù°¡ y¿¡ assign
end
// Regular delay·Î
Ç¥ÇöÇϸé..
initial
begin
x = 0; z =
0;
temp_xz = x +
z;
#5 y =
temp_xz;
end
¡á Zero delay control
- º°µµÀÇ always blockÀ̳ª initial block¿¡ ÀÖ´Â procedural ±¸¹®Àº °°Àº ½Ã¹Ä·¹ÀÌ¼Ç ½Ã°£¿¡
½ÇÇàµÈ´Ù.
- ÀÌ·¸°Ô °°Àº ½Ã°£¿¡ ½ÇÇàµÇ´Â ±¸¹®µéÀÇ ½ÇÇà¼ø¼´Â Á¦¸Ú´ë·ÎÀÌ´Ù.
- zero delay´Â °°Àº
½Ã°£¿¡ ½ÇÇàµÇ´Â ±¸¹®µéÁß¿¡¼ °¡Àå ³ªÁß¿¡ ½ÇÇàÇÏ°Ô ÇÑ´Ù.
- Race conditionÀ» ÇؼÒÇϴµ¥ »ç¿ëµÈ´Ù.
Ex)
// Zero delay
control
initial
begin
x =
0;
y = 0;
end
initial
begin
#0 x =
1; // zero delay
control
#0 y =
1;
end
* x = 0; y = 0; x = 1; y = 1;
±¸¹® ¸ðµÎ 0 tu¿¡ ½ÇÇàµÇ°Ô µÈ´Ù.
** ±× Áß¿¡¼µµ #0(zero delay)°¡ Àû¿ëµÈ x = 1; y =
1; ±¸¹®ÀÌ °¡Àå ³ªÁß¿¡ ½ÇÇàµÈ´Ù.
*** ±×·¯³ª x = 1; y =
1; ±¸¹®µéÀÇ ½ÇÇà ¼ø¼´Â ¾î´À°ÍÀÌ ¸ÕÀúÀÏÁö ¸ð¸¥´Ù.
7.3.2 Event-Based Timing Control
- Event : register³ª netÀÇ °ªÀÌ º¯ÇÏ´Â °ÍÀ» ¸»ÇÑ´Ù.
¡á Regular
event control
- @ : event controlÀ» ¸í½ÃÇÏ´Â
½Éº¼
- ½ÅÈ£ÀÇ º¯È, ½ÅÈ£ÀÇ positive(¡è) Àüȯ, negative(¡é)ÀüȯÀ» °¨ÁöÇÏ¿© µÚ¿¡¿À´Â ±¸¹®À»
½ÇÇà½ÃŲ´Ù.
- Positive transition : 0 ¡æ (1, x, z), x ¡æ 1, z ¡æ 1 ÀÎ
°æ¿ì
- Negative transition : 1 ¡æ (0, x, z), x ¡æ 0, z ¡æ 0 ÀÎ °æ¿ì
Ex)
//Regular
Event Control
@(clock) q =
d;
// clock½ÅÈ£°¡ ¹Ù²ð¶§¸¶´Ù q = d;½ÇÇà
@(posedge clock) q =
d; // clock½ÅÈ£°¡ pos transitionÀ϶§ q = d;¸¦
½ÇÇà
@(negedge clock) q =
d; // clock½ÅÈ£°¡ neg transitionÀ϶§ q = d;¸¦
½ÇÇà
q = @(posedge clock) d; // clock½ÅÈ£ÀÇ positive edgeÀ϶§
// Áï½Ã d¸¦ °è»êÇÏ°í q¿¡ ÀúÀå
¡á Named event control
- »ç¿ëÀÚ°¡ event¸¦ ¼±¾ðÇÏ°í ±× event¿¡ trigger¿Í recognize¸¦ ÁöÁ¤ÇÒ ¼ö ÀÖ´Ù.
-
event : event À̸§À» ¼±¾ðÇÏ´Â
keyword
- -> : event¸¦
triggerÇÏ´Â ½Éº¼
- @ :
triggerµÈ event¸¦ ÀνÄÇÏ´Â ½Éº¼
Ex)
// Named Event
Control
// ¸¶Áö¸· packetÀÌ µé¾î¿Â ÈÄ¿¡ ¹öÆÛ¿¡ ÀúÀåÇÏ´Â ¿¹Á¦ÀÌ´Ù.
event received_data; // event ¼±¾ð name : received_data
always @(posedge
clock)
begin
if(last_data_packet)
->received_data;
end
always
@(received_data)
data_buf =
{data_pkt[0], data_pkt[1], data_pkt[2], data_pkt[3]};
¡á Event OR control
- ´Ù¼öÀÇ event¿¡ ÀÇÇØ ½ÇÇàµÇ´Â ±¸¹®À̳ª ±¸¹® blockÀÌ ÀÖ´Â °æ¿ì event¸¦ ORÇÏ¿© »ç¿ë
Ex)
// Event OR
control
always @(reset or clock or
d) // reset, clock, dÁß ¾î´ÀÇϳª¶óµµ ¹Ù²î¸é
// begin~end½ÇÇà
begin
if
(reset)
q =
1'b0;
else if
(clock)
q
= d;
end
7.3.3 Level-Sensitive Timing Control
- @ ´Â edge¸¦ °¨ÁöÇÏ´Â
controlÀÌ´Ù.
- wait : ¾î¶°ÇÑ
Á¶°ÇÀÌ true(level = 1)°¡ µÇ±â Àü¿¡´Â µÚµû¶ó¿À´Â ±¸¹®À» ½ÇÇàÇÏÁö ¾Ê´Â´Ù.
Ex)
//
Level-sensitive timing control
always
wait (count_enable) #20
count = count + 1;
* count_enableÀ» °è¼Ó °¨½ÃÇÏ´Ù°¡ ±× °ªÀÌ 1ÀÌ µÇ¸é 20 tuÈÄ¿¡ count °ªÀ»
Áõ°¡½ÃŲ´Ù.
** count_enableÀÌ °è¼Ó 1À» À¯ÁöÇÏ°Ô µÇ¸é count°ªÀº 20 tu¸¶´Ù °è¼Ó Áõ°¡ÇÏ°Ô
µÈ´Ù.
7.4 Conditional Statements
- ¾î¶² Á¶°ÇÀÌ ¸¸Á·ÇÏ´ÂÁöÀÇ ¿©ºÎ¿¡ µû¶ó ½ÇÇàÇÒ ±¸¹®À» °áÁ¤ÇÑ´Ù.
- if, else : C programming°ú ºñ½ÁÇÏ´Ù.
Syntax]
// Type 1
if
(<expression>) true_statement;
// Type 2
if
(<expression>) true_statement;
else false_statement;
// Type 3
if
(<expression1>) true_statement1;
else if (<expression2>)
true_statement2;
else if
(<expression3>) true_statement3;
else default_statement;
- <expression>À» °è»êÇÏ¿©
true(non-zero)À̸é true_statement¸¦,
false(0,x,z)À̸é false_statement
½ÇÇàÇÑ´Ù.
- true_statement, false_statement°¡ ¿©·¯°³ÀÇ ±¸¹®À¸·Î µÇ¾î ÀÖ´Â °æ¿ì begin,
end·Î ±×·ìÈÇÑ´Ù.
Ex)
// Conditional
Statement Examples
if (!lock) buffer =
data;
if (enable) out = in;
if (number_queued
< MAX_Q_DEPTH)
begin
data_queue = data;
number_queued = number_queued +
1;
end
else
$display("Queue Full. Try
again");
if (alu_control ==
0)
y = x +
z;
else if (alu_control ==
1)
y = x -
z;
else if (alu_control ==
2)
y = x *
z;
else
$display("Invalid ALU control signal");
7.5 Multiway Branching
- ¾Õ¿¡¼ ¾ð±ÞÇÑ if-else if
±¸¹®(Type 3 conditional statement)ÀÇ ´Ù¸¥ ÇüÅÂ
- Á¶°ÇÀÌ ¸¹¾ÆÁö¸é if-else if ±¸¹®À¸·Î´Â Ç¥ÇöÇϱ⠹ø°Å·Ó´Ù.
-
case : Á¶°ÇÀÇ °á°ú¿¡ µû¶ó ½ÇÇ౸¹®ÀÌ ´Þ¶óÁø´Ù.
7.5.1 case Statement
// case Statement structure
case
(expression)
alternative1:
statement1;
alternative2:
statement2;
alternative3:
statement3;
:
default:
default_statement;
endcase
- expressionÀº alternative1¿¡¼ºÎÅÍ ¾²¿©ÀÖ´Â ¼ø¼´ë·Î Â÷·Ê·Î ºñ±³µÈ´Ù.
-
ºñ±³ °á°ú ÀÏÄ¡ÇÑ °æ¿ì ±×¿¡ µû¸¥ ±¸¹®À» ½ÇÇàÇÏ°í case±¸¹®À»
ºüÁ® ³ª¿Â´Ù.
- ÀÏÄ¡ÇÏ´Â °æ¿ì°¡ ¾ø´Â °æ¿ì default_statement¸¦ ½ÇÇàÇÏ°í case±¸¹®À» ºüÁ® ³ª¿Â´Ù.
- case¹®Àº
nestingÀÌ °¡´ÉÇÏ´Ù.
Ex)
// case±¸¹®À¸·Î
ÀÛ¼ºÇÑ 7.4ÀÇ type 3¿¹Á¦
reg [1:0] alu_control;
case
(alu_control)
2'd0: y = x +
z;
2'd1: y = x -
z;
2'd2: y = x *
z;
default: $display("Invalid ALU
control signal");
endcase
- Many-to one multiplexer¿Í °°ÀÌ µ¿ÀÛÇÑ´Ù.
Ex)
// 4-to-1
Multiplexer with case statement
module mux4to1 (out, i0,
i1, i2, i3, s1, s0);
output
out;
input i0, i1, i2, i3;
input s1,
s0;
reg out;
always @(s1 or s0 or
i0 or i1 or i2 or i3)
case ({s1,
s0})
2'd0 : out =
i0;
2'd1 : out =
i1;
2'd2 : out =
i2;
2'd3 : out =
i3;
default : $display("Invalid
control signals");
endcase
endmodule
- case ±¸¹®Àº expression°ú alternativeÀÇ °ªÀ» bit´ë bit·Î ºñ±³ÇÑ´Ù.
- 0, 1, x, z ¸ðµÎ bitº°·Î ÀÏ´ëÀÏ ºñ±³ÇÑ´Ù.
-
bit width°¡ ´Ù¸£¸é Å« ÂÊÀ¸·Î ¸ÂÃß¾î ºñ±³ÇÑ´Ù. (³²´ÂºÎºÐÀº 0À¸·Î ä¿ò)
Ex)
// Case with x
& z
module demux1to4 (out0, out1, out2, out3, in, s1,
s0);
output out0, out1,
out2, out3;
reg out0, out1, out2,
out3;
input in;
input s1,
s0;
always @(s1 ro s0 or
in)
case ({s1,
s0})
2'b00 : begin out0 = in; out1
= 1'bz; out2 = 1'bz, out3 = 1'bz;
end
2'b01 : begin out0 = 1'bz;
out1 = in; out2 = 1'bz, out3 = 1'bz;
end
2'b10 : begin out0 = 1'bz;
out1 = 1'bz; out2 = in, out3 = 1'bz;
end
2'b11 : begin out0 = 1'bz;
out1 = 1'bz; out2 = 1'bz, out3 = in; end
2'bx0, 2'bx1, 2'bxz,
2'bxx, 2'b0x, 2'b1x, 2'bzx
:
begin
out0 = 1'bx; out1 = 1'bx; out2 = 1'bx; out3 =
1'bx;
end
2'bz0, 2'bz1, 2'bzz, 2'b0z,
2'b1z :
begin
out0 = 1'bz; out1 = 1'bz; out2 = 1'bz; out3 =
1'bz;
end
default : $
display("Unspecified control signals");
endcase
endmodule
* °°Àº ½ÇÇ౸¹®À» °®´Â alternativesµéÀº À§ÀÇ ¿¹Ã³·³ comma(,)·Î ±¸ºÐÇÏ¿© ÇÔ²² Àû¾îÁÙ ¼ö ÀÖ´Ù.
7.5.2 casex, casez Keyowrds
- casez : case expression°ú
alternative¿¡ ÀÖ´Â ¸ðµç z¸¦ don't care·Î
Ãë±ÞÇÑ´Ù.
- casex : case
expression°ú alternative¿¡ ÀÖ´Â ¸ðµç x¿Í
z¸¦ don't care·Î Ãë±ÞÇÑ´Ù.
Ex)
// casex
Usage
reg [3:0] encoding;
integer
next_state;
casex
(encoding)
4'b1xxx : next_state =
3;
4'bx1xx : next_state =
2;
4'bxx1x : next_state =
1;
4'bxxx1 : next_state =
0;
default : next_state =
0;
endcase
7.6 Loops
7.6.1 While Loop
- while-expressionÀÌ °ÅÁþ(false)ÀÌ µÉ ¶§±îÁö loop¸¦ ½ÇÇàÇÑ´Ù.
Ex)
// While
Loop
// ¿¹Á¦ 1
integer
count;
initial
begin
count =
0;
while (count <
128) // count = 128ÀÌ µÇ¸é
begin~end¸¦
// ½ÇÇàÇÏÁö ¾Ê°í ºüÁ®³ª¿Â´Ù.
begin
$display("Count = %d",
count);
count = count + 1;
end
end
// ¿¹Á¦
2
'define TURE 1'b1;
'define FALSE
1'b0;
reg [15:0]
flag;
integer i;
reg
continue;
initial
begin
flag = 16'b
0010_0000_0000_0000;
i =
0;
continue = 'TRUE;
while ((i<16)
&& continue) // ´Ù¼öÀÇ expression
»ç¿ë°¡´É
begin
if
(flag[i])
begin
$display("Encountered a TRUE bit at element number %",
i);
continue =
'FALSE;
end
i = i
+ 1;
end
end
7.6.2 For Loop
- ÃʱⰪ°ú Á¾·áÁ¶°ÇÀ» °¡Áö¸ç, º¯¼ö°ªÀ» º¯È½ÃÅ°´Â ±¸¹®À» ÇÔ²² °®°í ÀÖ´Ù.
- while
±¸¹®¿¡ ºñÇØ Ãà¾àµÇ°í ÇÔÃàÀûÀÌÁö¸¸ ÀϹÝÀûÀÌÁö ¾Ê¾Æ »ç¿ë¹üÀ§°¡ ³ÐÁö ¾Ê´Ù.
Ex)
// For
Loop
integer count;
initial
for (count = 0; count <
128; count = count
+1)
$display("Count = %d", count);
7.6.3 Repeat Loop
- ¹Ýº¹ÇÒ È½¼ö¸¦ ¼ýÀÚ·Î ¸í½ÃÇÏ´Â LoopÀÌ´Ù.
- Loop¿¡ µé¾î°¥ ¼ø°£¿¡ ÀÐÀº ¹Ýº¹È½¼ö´Â loop°¡ ÁøÇàµÇ´Â
µ¿¾È °ªÀÌ ¹Ù²î¾îµµ ¹Ýº¹È½¼ö°¡ ¹Ù²îÁø ¾Ê´Â´Ù.
Ex)
// Repeat
Loop
// Illustration #1
integer
count;
initial
begin
count =
0;
repeat(128)
begin
$display("Count = %d",
count);
count = count + 1;
end
end
// Illustration
#2
module data_buffer (data_start, data,
clock);
parameter cycles =
8;
input data_start;
input [15:0]
data;
input clock;
reg [15:0] buffer
[0:7};
integer i;
always
@(posedge clock)
begin
if(data_start)
begin
i =
0;
repeat(cycles) // begin ~ end¸¦ 8¹ø
¹Ýº¹
begin
@(posedge clock) buffer[i] = data; // clockÀÇ posedge¸¦ ±â´Ù·È´Ù°¡
// data¸¦
ÀúÀå
i = i + 1;
end
end
end
endmodule
7.6.4 Forever Loop
- $finish¸¦ ¸¸³ª±â Àü±îÁö´Â ºüÁ®³ª¿ÀÁö
¾Ê´Â loop
- while(1)°ú
°°´Ù.
- disable±¸¹®À¸·Î ºüÁ®³ª¿Ã ¼ö
ÀÖ´Ù.
Ex)
// Forever
Loop
// #1 Clock Generator
reg
clock;
initial
begin
clock =
1'b0;
forever #10 clock =
~clock;
end
//
#2
reg clock;
reg x,
y;
initial
forever @(posedge clock) x
= y;
7.7 Sequential and Parallel Blocks
- BlockÀ̶õ ¾Õ¿¡¼ º¸¾Æ¿Ô´ø begin,
end·Î ¹ÀÎ ±¸¹®µéÀÇ ÁýÇÕÀÌ´Ù.
-
Block¿¡´Â ¿©·¯°¡Áö ÇüÅÂ¿Í Æ¯Â¡µéÀ» °®°í ÀÖ´Ù.
7.7.1 Block Types
¡á Sequential Blocks
- begin, end·Î
¹ÀÎ ±¸¹®µéÀÇ ÁýÇÕ
- ÀÛ¼ºµÈ ¼ø¼´ë·Î ¼øÂ÷ÀûÀ¸·Î ½ÇÇàµÈ´Ù. Áï ¾Õ¼± ±¸¹®ÀÇ ½ÇÇàÀÌ ¿ÏÀüÈ÷ ³¡³ª¾ß ´ÙÀ½ ±¸¹®ÀÌ
½ÇÇàµÈ´Ù.
- delay³ª event controlÀÌ ¸í½ÃµÇ¾î ÀÖ´Ù¸é ¾Õ¼± ±¸¹®ÀÇ ½ÇÇàÀÌ ¿Ï·áµÈ ½ÃÁ¡À» ±âÁØÀ¸·Î ÇÑ´Ù.
Ex)
// Sequential
Block
// #1
reg x,
y;
reg [1:0] z, w;
initial
begin
x =
1'b0;
y =
1'b1;
z = {x,
y};
w = {y,
x};
end
//
#2
reg x, y;
reg [1:0] x,
w;
initial
begin
x =
1'b0;
// 0 tu : ½ÇÇà ¿Ï·á
#5 y =
1'b1;
// 5 tu : ½ÇÇà ¿Ï·á
#10 z = {x,
y}; // 15 tu : ½ÇÇà
¿Ï·á
#30 w = {y,
x}; // 45 tu : ½ÇÇà
¿Ï·á
end
¡á Parallel Blocks
- fork, joinÀ¸·Î
¹ÀÎ ±¸¹®µéÀÇ ÁýÇÕ
- ¸ðµç ±¸¹®µéÀº ÀÛ¼º¼ø¼¿¡ °ü°è¾øÀÌ µ¿½Ã¿¡ ½ÇÇà
- ±¸¹®µéÀÇ ½ÇÇà¼ø¼´Â delay¿Í
event control¿¡ ÀÇÇØ Á¶ÀýµÈ´Ù.
- delay³ª event controlÀº block¿¡ µé¾î°£ ½ÃÁ¡À» ±âÁØÀ¸·Î
ÇÑ´Ù.
Ex)
// Parallel
Blocks
reg x, y;
reg [1:0] z,
w;
initial
fork
x =
1'b0;
// 0 tu : ½ÇÇà ¿Ï·á
#5 y =
1'b1; // 5 tu
: ½ÇÇà ¿Ï·á
#10 z = {x,
y}; // 10 tu : ½ÇÇà
¿Ï·á
#20 w = {y,
x}; // 20 tu : ½ÇÇà ¿Ï·á
join
- Parallel block¿¡¼ ÁÖÀÇÇÒ Á¡Àº ¸ðµç ±¸¹®µéÀÌ µ¿½Ã¿¡ ½ÇÇàµÇ¸é¼ race condition¿¡ ºüÁú ¼ö ÀÖ´Ù´Â
°ÍÀÌ´Ù.
- À§ÀÇ ¿¹¿¡¼ delay°¡ ¸í½Ã µÇÁö ¾Ê´Â ´Ù¸é ¾î¶² °á°ú°¡ ³ª¿ÃÁö ¾Æ¹«µµ Àå´ã ¸øÇÑ´Ù.
- ±×¸² 7.1¿¡¼ ó·³ 4°³ÀÇ ±¸¹®ÀÌ ÇѲ¨¹ø¿¡ ½ÇÇàµÈ´Ù¸é z¿Í wÀÇ °ªÀº º¸ÀåÇÒ ¼ö ¾ø°Ô µÈ´Ù.
±×¸² 7.1 parallel block race condition
7.7.2 Special Features of Blocks
¡á Nested Blocks
- BlockÀº nestingÀÌ °¡´ÉÇϸç, sequential°ú parallel blockÀ» ¼¯¾î »ç¿ëÇÒ ¼öµµ ÀÖ´Ù.
Ex)
// Nested
Blocks
initial
begin
x =
1'b0;
fork
#5 y
= 1'b1;
#10 z = {x, y};
join
#20 w = {y,
x};
end
¡á Named Blocks
- Block¿¡ À̸§À» Á¤ÇØÁÙ ¼ö ÀÖ´Ù.
- À̸§ÀÌ Á¤ÇØÁø block³»¿¡¼¸¸ »ç¿ëÇÏ´Â local
variableÀ» ¼±¾ðÇÒ ¼ö ÀÖ´Ù.
- Hierarchical nameÀ¸·Î access°¡´ÉÇÏ´Ù.
- disable·Î ½ÇÇàÀ» Á¤Áö½Ãų ¼ö ÀÖ´Ù.
Ex)
// Named
blocks
module top;
initial
begin:
block1 // Sequential
block, name : block1
integer
i;
// static & local to block1
...
...
end
initial
fork:
block2 //
Parallel block, name : block2
reg
i;
// static & local to block2
...
...
join
¡á Disabling Named Blocks
- disable : blockÀÇ ½ÇÇàÀ» ÁßÁö½ÃÅ°´Â ±â´É
Ex)
// Disabling
Named Blocks
reg [15:0] flag;
integer i;
initial
begin
flag = 16'b
0010_0000_0000_0000;
i =
0;
begin:
block1
while(i <
16)
begin
if
(flag[i])
begin
$display("Encountered a TRUE bit at element number %d",
i);
disable
block1;
end
i = i + 1;
end
end
end
7.8 Examples
7.8.1 4-to1 Multiplexer
Ex)
// 4-to1
multiplexer
module mux4to1 (out, i0, i1, i2, i3, s1,
s0);
output
out;
input i0, i1, i2, i3;
input s1,
s0;
reg out;
// ÀÔ·ÂÁß ¾î´À Çϳª¶óµµ °ªÀÌ º¯Çϸé
Ãâ·ÂÀ» ´Ù½Ã °è»êÇϵµ·Ï ÇÑ´Ù.
// ¸ðµç ÀÔ·ÂÀÌ Ãâ·Â Àç°è»êÀÇ ¿øÀÎÀÌ µÇ¹Ç·Î always @(...)ÀÇ
¸®½ºÆ®°¡ µÈ´Ù.
always @(s1 or s0 or i0 or i1 or i2 or
i3)
begin
case ({s1,
s0)}
2'b00: out =
i0;
2'b01:
out = i1;
2'b10: out =
i2;
2'b11:
out = i3;
default: out = 1'bx;
endcase
end
endmodule
7.8.2 4-bit Counter
Ex)
// 4-bit
Counter
module counter(Q, clock, clear);
output [3:0]
Q;
input clock, clear;
reg [3:0]
Q;
always @(posedge
clear or negedge clock)
begin
if
(clear)
Q
= 4'd0;
else
Q =
(Q + 1) % 16;
end
endmodule
7.8.3 Traffic Signal Controller
¡á Specification
- HighwayÀÇ ±³Åë½ÅÈ£°¡ ÃÖ¿ì¼±À̸ç, ±âº»ÀûÀ¸·Î ÆĶõºÒÀÌ´Ù.
- ¶§¶§·Î Country road¿¡ Â÷°¡
µµÂøÇÏ¸é ´ë±âÁßÀÎ Â÷°¡ Áö³ª°¥¸¸ÅÀÇ ½Ã°£µ¿¾È ÆĶõºÒÀÌ µÈ´Ù.
- Country road¿¡ Â÷°¡ ´Ù Áö³ª°¡¸é Áï½Ã ³ë¶õºÒ·Î ¹Ù²î°í
°ð »¡°£ºÒ·Î ¹Ù²î¸é¼ highwayÀÇ ½ÅÈ£°¡ ÆĶõºÒ·Î ¹Ù²ï´Ù.
- ¼¾¼´Â country road¿¡ ´ë±âÁßÀÎ Â÷°¡ ÀÖ´ÂÁö¸¦
°¨ÁöÇÏ°í X ½ÅÈ£·Î controller¿¡°Ô ½ÅÈ£¸¦ º¸³½´Ù. country road¿¡ ´ë±âÁßÀÎ Â÷°¡ ÀÖÀ¸¸é X = 1ÀÌ µÈ´Ù.
±×¸² 7.3 Dataflow Diagram of Statemachine
¡á Verilog Description
// Traffic signal
Controller
'define TRUE
1'b1
'define FALSE 1'b0
'define
RED 2'd0
'define
YELLOW 2'd1
'define GREEN 2'd2
// Define
State
HWY
CNTRY
'define S0
3'd0 //
Green
Red
'define S1
3'd1 //
Yellow
Red
'define S2
3'd2 //
Red
Red
'define S3
3'd3 //
Red
Green
'define S4
3'd4 //
Red
Yellow
// Delay
define
'define Y2RDELAY 3 //
Yellow to Red delay
'define R2GDELAY
2 // Red to Green delay
module sig_control (hwy, cntry, X, clock, clear);
output [1:0] hwy,
cntry;
reg [1:0] hwy, cntry;
input
X;
input clock, clear;
// Internal
variables
reg [2:0] state;
reg [2:0]
next_state;
initial
begin
state =
'S0;
next_state =
'S0;
hwy =
'GREEN;
cntry =
'RED;
end
always @(posedge
clock)
state =
next_state;
always
@(state)
begin
case(state)
'S0:
begin
hwy =
'GREEN;
cntry =
'RED;
end
'S1:
begin
hwy =
'YELLOW;
cntry =
'RED;
end
'S2:
begin
hwy =
'RED;
cntry =
'RED;
end
'S3:
begin
hwy =
'RED;
cntry =
'GREEN;
end
'S4:
begin
hwy =
'RED;
cntry =
'YELLOW;
end
endcase
end
always @(state or
clear or X)
begin
if
(clear)
next_state = 'S0;
else
case
(state)
'S0: if
(X)
next_state =
'S1;
else
next_state =
'S0;
'S1:
begin
repeat('Y2RDELAY) @(posedge
clock);
next_state =
'S2:
end
'S2:
begin
repeat('R2GDELAY) @(posedge
clock);
next_state =
'S3;
end
'S3: if
(X)
next_state =
'S3;
else
next_state =
'S4;
'S4:
begin
repeat('Y2RDELAY) @(posedge
clock);
next_state =
'S0;
end
default: next_state =
'S0;
endcase
end
endmodule
¡á Stimulus
// Stimulus
Module
module stimulus
wire [1:0] MAIN_SIG,
CNTRY_SIG;
reg CAR_ON_CNTRY_RD;
reg
CLOCK, CLEAR;
sig_control SC(MAIN_SIG, CNTRY_SIG, CAR_ON_CNTRY_RD, CLOCK, CLEAR);
initial
$monitor($time, "Main Sig
= %b Country Sig = %b Car_on_cntry =
%b",
MAIN_SIG, CNTRY_SIG, CAR_ON_CNTRY_RD);
initial
begin
CLOCK =
'FALSE;
forever #5 CLOCK =
~CLOCK;
end
initial
begin
CLEAR =
'TRUE;
repeat (5) @(negedge
CLOCK);
CLEAR =
'FALSE;
end
initial
begin
CAR_ON_CNTRY_RD =
'FALSE;
#200 CAR_ON_CNTRY_RD =
'TRUE;
#100 CAR_ON_CNTRY_RD =
'FALSE;
#200 CAR_ON_CNTRY_RD =
'TRUE;
#100 CAR_ON_CNTRY_RD =
'FALSE;
#200 CAR_ON_CNTRY_RD =
'TRUE;
#100 CAR_ON_CNTRY_RD =
'FALSE;
#100
$stop;
end
endmodule