147
Verilog HDL을 이용한 디지털시스템 설계 및 실습 10 조합논리회로 모델링 10. 조합논리회로 모델링 1 한국기술교육대학교 정보기술공학부

10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

Embed Size (px)

Citation preview

Page 1: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

Verilog HDL을이용한디지털시스템설계및실습

10 조합논리회로 모델링10. 조합논리회로 모델링

1한국기술교육대학교 정보기술공학부

Page 2: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

조합논리회로모델링

조합논리회로의형태와설계에사용되는Verilog 구문

조합논리회로의형태

·논리게이트·Multiplexer·Encoder·Decoder

·Adder·Subtractor·ALU·Lookup Table·Decoder

·Random Logic·Lookup Table·Comparator

·게이트프리미티브연속할당문 ( i 문)

조합논리회로설계에사용되는 Verilog 구문

·연속할당문 (assign문)·행위수준모델링 (if문, case문, for문)·함수및 task (시간또는 event 제어를갖지못한다)·모듈인스턴스모듈인스턴스

논리합성이지원되지않는

·initial문·스위치프리미티브 (cmos, nmos, tran등)·forever while repeat등의반복문논리합성이지원되지않는

Verilog 구문·forever, while, repeat등의반복문·wait, event, 지연등타이밍제어구문·force-release, fork-join·시스템 task ($finish, $time등)

2한국기술교육대학교 정보기술공학부

Page 3: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

조합논리회로모델링

조합논리회로모델링시유의사항

always구문always 구문감지신호목록 (sensitivity list)에회로 (즉, always 블록으로모델링되는회로)의입력신호들이빠짐없이모두포함되어야함그렇지않은경우 ; 합성전과합성후의시뮬레이션결과가다를수있음

if 조건문과 case 문모든입력조건들에대한출력값이명시적으로지정되어야함모든입력조건들에대한출력값이명시적으로지정되어야함

그렇지않은경우 ;래치가생성되어순차논리회로가될수있음

논리최적화가용이한구조와표현을사용

최소의게이트수와최소지연경로를갖는회로가합성되도록해야함

소스코드가간결해지도록모델링

소스코드의가독성 (readability)을좋게하여오류발생가능성을줄여주고, 디버깅을용이하게하여설계생산성을높여줌

3한국기술교육대학교 정보기술공학부

Page 4: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.1.1기본논리게이트모델링4입력 NAND 게이트

a[0]a[1]a[2]a[3]

ya[3]

비트연산자, 축약연산자, 게이트프리미티브module nand4_op1(a, y);

input [3:0] a;output y;

비 자,축약 자,게이 리미티

output y;

assign y = ~(a[0] & a[1] & a[2] & a[3]); // 비트 연산자// assign y = ~&a; // 축약 연산자// nand U0(y, a[0], a[1], a[2], a[3]); // 게이트 프리미티브

endmodule 코드 10.1 ~ 10.3

4한국기술교육대학교 정보기술공학부

Page 5: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.1.1기본논리게이트모델링4입력 NAND 게이트

if조건문module nand4_if(a, y);input [3:0] a;output y;

if 조건문

output y;reg y;

always @(a) beginy gif(a == 4'b1111) y = 1'b0; else y = 1'b1;

endd d lendmodule 코드 10.4

5한국기술교육대학교 정보기술공학부

Page 6: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.1.1기본논리게이트모델링4입력 NAND 게이트 (테스트벤치)

module tb_nand4;코드 10.5

reg [3:0] a;wire y;integer i;nand4 op1 U0(a y);

10.5

nand4_op1 U0(a, y);// nand4_op2 U0(a, y);// nand4_gate U0(a, y);// nand4 if U0(a, y); _ yinitial begin

a = 0;for(i=1; i < 32; i=i+1)

#20#20 a = i; endendmodule

6한국기술교육대학교 정보기술공학부

Page 7: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.1.1기본논리게이트모델링

3입력 NOR 게이트를아래의방식으로모델링하고, 테스트벤치를작성하여기능을검증

① 비트단위연산자를사용하는방법

② 축약연산자를사용하는방법

③ 게이트프리미티브를사용하는방법

④ if 조건문을사용하는방법

7한국기술교육대학교 정보기술공학부

Page 8: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.1.2멀티비트논리연산의모델링4비트 2입력 NOR 게이트

module nor_op(a, b, y);a[3:0]

비트연산자

input [3:0] a, b;output [3:0] y;

i ( | b)

b[3:0]

assign y = ~(a | b);

// assign y[0] = ~(a[0] | b[0]); // assign y[1] = ~(a[1] | b[1]);

y[3] y[2] y[1] y[0]// assign y[1] (a[1] | b[1]);// assign y[2] = ~(a[2] | b[2]);// assign y[3] = ~(a[3] | b[3]);

endmodule 코드 10.6

8한국기술교육대학교 정보기술공학부

Page 9: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.1.2멀티비트논리연산의모델링4비트 2입력 NOR 게이트

module nor for(a b y);

for 반복문module nor_for(a, b, y);input [3:0] a, b;output [3:0] y;reg [3:0] y;게이트프리미티브배열

module nor_gate(a, b, y);input [3:0] a, b;output [3:0] y;

integer i;

always @(a or b) beginf (i 0 i < 4 i i+1)

nor U0 [3:0](y, a, b);

endmodule 코드 10 7

for(i=0; i < 4; i=i+1)y[i] = ~(a[i] | b[i]);

endendmodule 코드 10 8endmodule 코드 10.7 endmodule 코드 10.8

9한국기술교육대학교 정보기술공학부

Page 10: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.1.2멀티비트논리연산의모델링

4비트 2입력 XOR게이트를아래의방식으로모델링하고, 테스트벤치를작성하여기능을검증

① 비트단위연산자를사용하는방법

② 게이트프리미티브를사용하는방법

③ for 반복문을사용하는방법

10한국기술교육대학교 정보기술공학부

Page 11: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

1110.1.3부울함수의모델링부울함수의모델링

비 연산자

edcbay ⋅+⋅+= )()(

module comb_gate(a, b, c, d, e, y);input a, b, c, d, e;

비트연산자

output y;

assign y = ~((a | b) &(c | d) & e);

endmodule 코드 10.10

edcbay ⋅++⋅=

Page 12: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

1210.1.4진리표의모델링진리표의모델링

module booth enc(xin, y, y2, neg);

case 문module booth_enc(xin, y, y2, neg);

input [2:0] xin;output y, y2, neg;reg [2:0] tmp;

xin[2:0] y y2 neg

표 10.2 Booth 인코딩진리표

assign y = tmp[2];assign y2 = tmp[1];assign neg = tmp[0];

xin[2:0] y y2 neg

000 0 0 0

001 1 0 0

always @(xin) begincase(xin)

0 : tmp = 3'b000;

010 1 0 0

011 0 1 0

100 0 1 1 p ;1,2 : tmp = 3'b100;3 : tmp = 3'b010;4 : tmp = 3'b011;5 6 : tmp = 3'b101;

100 0 1 1

101 1 0 1

110 1 0 1 5,6 : tmp = 3 b101;7 : tmp = 3'b001;

endcaseendd d l 코드 10 11

111 0 0 1

endmodule 코드 10.11

Page 13: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

1310.1.4진리표의모델링진리표의모델링

module tb_booth_enc;

테스트벤치

reg [2:0] xin;wire y, y2, neg;integer i;

booth_enc U0(xin, y, y2, neg);

initial beginxin = 0;xin = 0;for(i=1; i < 16; i=i+1)

#20 xin = i;end

endmodule 코드 10.12

Page 14: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

1410.1.4진리표의모델링시뮬레이션결과

그림 10 6코드 10 11의합성결과그림 10.6 코드 10.11의합성결과

Page 15: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

1510.1.4진리표의모델링

아래의진리표를 Verilog HDL로모델링하고, 테스트벤치를작성하여기능을검증

xin[2:0] yz y2 neg add

000 0 1 0 1

표 10.3 Booth 인코딩진리표(2)

000 0 1 0 1

001 1 0 0 0

010 1 0 0 0

011 1 1 0 0

100 1 1 1 1

101 1 0 1 1

110 1 0 1 1

111 0 1 1 1111 0 1 1 1

Page 16: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.1.5 Lookup Table의모델링Lookup Table회로동작에필요한많은양의고정된데이터값을저장하기위해사용되는회로동작에필요한많은양의고정된데이터값을저장하기위해사용되는

조합논리회로블록

함수를이용하여독립된파일로모델링되고, 소스코드에서 lookup 테이블함수를호출하여저장된데이터를읽어내는방법을사용

저장될데이터들을 case 문으로표현lookup 테이블의주소가 case 문의조건으로사용n-비트의주소를갖는 lookup 테이블은최대 개의데이터를저장n2

16한국기술교육대학교 정보기술공학부

Page 17: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

1710.1.5 Lookup Table의모델링Lookup Table의모델링 (함수이용)

function [7:0] data_line1;input [3:0] addr_in;

b ibegincase(addr_in)

0 : data_line1 = 8'b0010_0000;1 : data line1 = 8'b0100 0100;1 : data_line1 8 b0100_0100;2 : data_line1 = 8'b0110_1001;3 : data_line1 = 8'b0110_0111;

. . . . . .14 : data_line1 = 8'b0110_1110;15 : data_line1 = 8'b0010_0000;

default : data_line1 = 8'b0000_0000;dendcase

endendfunction 코드 10.13

Page 18: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.2멀티플렉서4비트 4:1 멀티플렉서

4개의 4비트입력중에서하나를선택하여출력4개의 4비트입력중에서하나를선택하여출력if 조건문, case 문, 조건연산자등을이용하여모델링

module mux41_if(sel, a, b, c, d, y);input [1:0] sel;input [3:0] a, b, c, d;output [3:0] y;

if 조건문

output [3:0] y;reg [3:0] y;

always @(sel or a or b or c or d) beginy @( ) gif (sel == 2'b00) y = a;else if(sel == 2'b01) y = b;else if(sel == 2'b10) y = c;else if(sel == 2'b11) y = d;else y = 4'bx;

endendmodule 코드 10 14

18

endmodule 코드 10.14

한국기술교육대학교 정보기술공학부

Page 19: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.2멀티플렉서모델링4비트 4:1 멀티플렉서

module mux41_case(sel, a, b, c, d, y);input [1:0] sel;input [3:0] a, b, c, d;

case 문

input [3:0] a, b, c, d;output [3:0] y;reg [3:0] y;

always @(sel or a or b or c or d) begincase(sel)

0 : y = a;1 : y = b;1 : y = b;2 : y = c;3 : y = d;

default : y = 4'bx;y ;endcase

endendmodule 코드 10.15

19한국기술교육대학교 정보기술공학부

Page 20: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.2멀티플렉서모델링4비트 4:1 멀티플렉서

d l 41 ( l b d ) 조건연산자module mux41_conop(sel, a, b, c, d, y);input [1:0] sel;input [3:0] a, b, c, d;output [3:0] y;

조건연산자

output [3:0] y;

assign y =(sel == 0) ? a :(sel == 1) ? b :(sel == 2) ? c :(sel == 3) ? d : 4'bx;

endmodule 코드 10.16

20한국기술교육대학교 정보기술공학부

Page 21: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.2멀티플렉서모델링

module tb mux41; 테스트벤치

4비트 4:1 멀티플렉서

module tb_mux41;reg [3:0] a, b, c, d;reg [1:0] sel;wire [3:0] y;

테스트벤치

mux41_if U0(sel, a, b, c, d, y);// mux41_case U0(sel, a, b, c, d, y);// mux41_conop U0(sel, a, b, c, d, y);_

initial begina = 4'b0001; b = 4'b0010;c = 4'b0100; d = 4'b1000;c 4 b0100; d 4 b1000;

#80 a = 4'b1100; b = 4'b0011;c = 4'b0110; d = 4'b1001;

end

initial sel = 2'b00;always #20 sel = sel + 1;

21

endmodule 코드 10.17

한국기술교육대학교 정보기술공학부

Page 22: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.2멀티플렉서모델링4비트 4:1 멀티플렉서

시뮬레이션결과

22한국기술교육대학교 정보기술공학부

Page 23: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.2멀티플렉서모델링4비트 4:1 멀티플렉서

그림 코드 의합성결과

23

그림 10.8 코드 10.14 ~ 10.16의합성결과

한국기술교육대학교 정보기술공학부

Page 24: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.2멀티플렉서모델링

8비트 8:1 멀티플렉서

d l 81 ( l i d t bit) 문module mux81_case(sel, in_word, out_bit);input [2:0] sel;input [7:0] in_word;output out bit;

case 문

output out_bit;reg out_bit;

always @(sel or in_word) begin_case(sel)

d

코드를완성한다.

endcaseend

endmodule 코드 10.18

24한국기술교육대학교 정보기술공학부

Page 25: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.1 2진인코더n:m 2진인코더

n-비트의입력을m-비트의출력으로변환(단, n=2m)비 의 력 비 의 력 ( , )if 조건문, case 문, for 반복문등여러가지방법으로모델링

반복문 ; 입력의비트수가큰경우또는입/력비트수를파라미터화하여모델링하는경우에유용

진리표에나열된입력조건이외의경우에는출력에 don’t care 값을할당하여최소화된회로가합성되도록함할당하여최소화된회로가합성되도록함

표 10.4 4:2 2진인코더의진리표

입력 a[3:0] 출력 y[1:0]

0001 00

0010 01

0100 10

1000 11

25

1000 11

한국기술교육대학교 정보기술공학부

Page 26: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.1 2진인코더4:2 2진인코더

module enc_4to2_case(a, y);input [3:0] a;

case 문

input [3:0] a;output [1:0] y;reg [1:0] y;

module enc_4to2_if(a, y);input [3:0] a;output [1:0] y;

if 조건문

always @(a) begincasex(a)

4'b0001 : y = 0;

p [ ] yreg [1:0] y;

always @(a) begin4'b0010 : y = 1;4'b0100 : y = 2; 4'b1000 : y = 3;default : y = 2'bx;

if (a == 4'b0001) y = 0;else if(a == 4'b0010) y = 1;else if(a == 4'b0100) y = 2;else if(a == 4'b1000) y = 3;default : y = 2 bx;

endcaseend

endmodule 코드 10.19

else if(a == 4 b1000) y = 3;else y = 2'bx;

endendmodule 코드 10.20

26한국기술교육대학교 정보기술공학부

Page 27: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.1 2진인코더4:2 2진인코더

module enc_4to2_for(a, y);input [3:0] a;output [1:0] y;

[1 0]

for 반복문

reg [1:0] y;reg [3:0] temp;integer n;

always @(a) begintemp = 4'b0001;y = 2'bx;for(n = 0; n < 4; n = n + 1) begin

if(a == temp)y = n;

temp temp << 1;temp = temp << 1;end

endendmodule 코드 10.21

27

e d odu e 코드 10.21

한국기술교육대학교 정보기술공학부

Page 28: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.1 2진인코더4:2 2진인코더

module tb_enc_4to2;reg [3:0] a;wire [1:0] y;

테스트벤치

enc_4to2_case U0(a, y);// enc_4to2_if U0(a, y);// enc 4to2 for U0(a, y);// enc_4to2_for U0(a, y);

always begina = 4'b0001;

#20 a = 4'b0010;#20 a = 4'b0100;#20 a = 4'b1000;#20;#20;

end

endmodule 코드 10.22

28

e d odu e 코드 10.22

한국기술교육대학교 정보기술공학부

Page 29: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.1 2진인코더코드 10.19의합성결과

코드 10.20 코드 10.21의합성결과코드 10.20, 코드 10.21의합성결과

29한국기술교육대학교 정보기술공학부

Page 30: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.1 2진인코더

enable 신호를갖는 4:2 이진인코더를다음의방법으로모델링하고, 시뮬레이션을통해검증시뮬레이션을통해검증

① case 문을사용하는방법② if 조건문을사용하는방법② 건문을사용하는방법

③ for 반복문을사용하는방법

30한국기술교육대학교 정보기술공학부

Page 31: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.2우선순위인코더4:2 우선순위인코더

표 10 5 4:2우선순위인코더의진리표표 10.5 4:2 우선순위인코더의진리표

입력 출력

a[3:0] y[1:0] valid[ ] y[ ]

1xxx 11 1

01xx 10 1

001x 01 1

0001 00 1

0000 xx 0

31한국기술교육대학교 정보기술공학부

Page 32: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.2우선순위인코더4:2 우선순위인코더

module pri_enc_4to2_if(a, valid, y);input [3:0] a;output valid;output [1:0] y;

if 조건문사용

p [ ] y;reg valid;reg [1:0] y;

always @(a) beginalways @(a) beginvalid = 1;if (a[3]) y = 3;else if(a[2]) y = 2;l if( [1]) 1else if(a[1]) y = 1;else if(a[0]) y = 0;else begin

valid = 0;y = 2'bx;

endend

endmodule 코드 10.23

32

endmodule 코드 10.23

한국기술교육대학교 정보기술공학부

Page 33: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.2우선순위인코더4:2 우선순위인코더

module pri enc 4to2 case(a valid y); 문사용module pri_enc_4to2_case(a, valid, y);input [3:0] a;output valid;output [1:0] y;reg valid;

casex 문사용

reg valid;reg [1:0] y;

always @(a) beginvalid = 1;valid = 1;casex(a)

4'b1xxx : y = 3;4'b01xx : y = 2;4'b001 14'b001x : y = 1;4'b0001 : y = 0;default : begin

valid = 0;2'by = 2'bx;

endendcase

endd d l 코드 10 24

33

endmodule 코드 10.24

한국기술교육대학교 정보기술공학부

Page 34: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.2우선순위인코더4:2 우선순위인코더

module pri_enc_4to2_for(a, valid, y);input [3:0] a;output valid;t t [1 0]

for 문사용

output [1:0] y;reg valid;reg [1:0] y;integer n;

always @(a) beginvalid = 0;y = 2'bx;y 2 bx;for(n = 0; n < 4; n = n+1)

if(a[n]) beginvalid = 1;y = n;y = n;

endend

endmodule 코드 10.25

34한국기술교육대학교 정보기술공학부

Page 35: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.2우선순위인코더4:2 우선순위인코더

module tb_pri_enc;reg [3:0] a;wire valid;

테스트벤치

;wire [1:0] y;

pri_enc_4to2_case U0(a, valid, y);// pri enc 4to2 if U0(a valid y);// pri_enc_4to2_if U0(a, valid, y);// pri_enc_4to2_for U0(a, valid, y);

always begin4'b0001a = 4'b0001;

#20 a = 4'b0010;#20 a = 4'b0100;#20 a = 4'b1000;#20;

endendmodule 코드 10.26

35한국기술교육대학교 정보기술공학부

Page 36: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.2우선순위인코더4:2 우선순위인코더

시뮬레이션결과

36한국기술교육대학교 정보기술공학부

그림 10.12 코드 10.23 ~ 10.25의합성결과

Page 37: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.2우선순위인코더

enable 신호를갖는 4:2 우선순위인코더를다음의방법으로모델링하고, 시뮬레이션을통해검증① case 문을사용하는방법② if 조건문을사용하는방법③ f 반복문을사용하는방법③ for 반복문을사용하는방법

37한국기술교육대학교 정보기술공학부

Page 38: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.3디코더m:n 2진디코더

m-비트의입력을 n-비트의출력으로변환(단 n=2m)m 비트의입력을 n 비트의출력으로변환(단, n 2 )n:m 2진인코더로인코딩된데이터를복원시킴if 조건문, case 문, for 반복문등여러가지방법으로모델링

반복문 ; 입력의비트수가큰경우또는입/력비트수를파라미터화하여모델링하는경우에유용

입력 a[1:0] 출력 y[3:0]

표 10.6 2:4 2진디코더의진리표

입력 a[1:0] 출력 y[3:0]

00 0001

01 001001 0010

10 0100

11 1000

38한국기술교육대학교 정보기술공학부

Page 39: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.3디코더2:4 이진디코더

module dec_2to4_if(a, y);input [1:0] a;

if 조건문사용input [1:0] a;output [3:0] y;reg [3:0] y;

always @(a) beginif(a == 0) y = 4'b0001;else if(a ==1) y = 4'b0010;l if( 2) 4'b0100else if(a ==2) y = 4'b0100;else y = 4'b1000;

endendmodule 코드 10 27endmodule 코드 10.27

39한국기술교육대학교 정보기술공학부

Page 40: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.3디코더2:4 이진디코더

module dec_2to4_case(a, y);input [1:0] a;output [3:0] y;

case 문사용

output [3:0] y;reg [3:0] y;

always @(a) begincase(a)

0 : y = 4'b0001;1 : y = 4'b0010;2 4'b01002 : y = 4'b0100;3 : y = 4'b1000;

default: y = 4'bx;endcaseendcase

endendmodule 코드 10.28

40한국기술교육대학교 정보기술공학부

Page 41: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.3디코더2:4 이진디코더

module dec_2to4_for(a, y);input [1:0] a;

for 문사용

output [3:0] y;reg [3:0] y;integer n;

always @(a) beginfor(n = 0; n <= 3; n = n + 1)

if(a == n)y[n] = 1'b1;y[n] = 1 b1;

elsey[n] = 1'b0;

endd d l 코드 10 29endmodule 코드 10.29

41한국기술교육대학교 정보기술공학부

Page 42: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.3디코더2:4 이진디코더

module tb_dec_2to4;reg [1:0] a;wire [3:0] y;

테스트벤치

dec_2to4_case U0(a, y);// dec_2to4_if U0(a, y);// dec 2to4 for U0(a, y);// _ _ ( , y);

always begina = 4'b00;

#20 a = 4'b01;#20 a = 4'b01;#20 a = 4'b10;#20 a = 4'b11;#20;

endendmodule 코드 10.30

42한국기술교육대학교 정보기술공학부

Page 43: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.3디코더2:4 이진디코더

시뮬레이션결과

43한국기술교육대학교 정보기술공학부

그림 10.14 코드 10.27 ~ 10.29의합성결과

Page 44: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.3디코더

Active-high enable 신호를갖는 3:6 디코더 (표 10.7)를다음의방법으로모델링하고, 시뮬레이션을통해검증

if 조건문을사용하는방법enable 신호와입력 a를결합연산자 { }로묶어 case 문의조건으로사용하는방법

enable 입력 a[2:0] 출력 y[5:0]enable 입력 a[2:0] 출력 y[5:0]0 xxx 000000

000 000001001 000010

표 10.7

1

001 000010010 000100011 001000100 010000100 010000101 100000110 000000

44

111 000000

한국기술교육대학교 정보기술공학부

Page 45: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.3디코더파라미터화된디코더

module dec param(en, a, y);module dec_param(en, a, y);parameter in_width = 3, out_width = 8;input en;input [in_width-1:0] a;output [out_width-1:0] y;_reg [out_width-1:0] y;integer n;

always @(en or a) begin(! )if(!en)y = 0;

else if(a > out_width-1)

for(n = 0; n <= out width 1; n = n+1)for(n = 0; n <= out_width-1; n = n+1)y[n] = 1'bx;

else for(n = 0; n <= out_width-1; n = n+1)

if(a == n)if(a n)y[n] = 1'b1;

elsey[n] = 1'b0;

end

45

endmodule 코드 10.31

한국기술교육대학교 정보기술공학부

Page 46: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.3.3디코더모듈 dec_param을이용한 3:6 디코더

d l d i t( dd d dd )module dec_param_inst(en, addr, dec_addr);input en;input [2:0] addr;output [5:0] dec addr;output [5:0] dec_addr;

// 3:6 디코더 구현dec_param #(3, 6) decoder_3to6(en, addr, dec_addr);

endmodule 코드 10.32

46한국기술교육대학교 정보기술공학부

Page 47: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.4비교기비교기

두입력의상대적인크기를비교두입력의상대적인크기를비교

관계연산자, if 조건문, for 반복문등여러가지방법으로모델링

4비트비교기

module comp_assign(a, b, agtb, altb, aeqb);input [3:0] a, b;t t ltb tb b

4비트비교기

output altb, agtb, aeqb;

assign altb =(a < b);assign agtb =(a > b);assign aeqb =(a == b);

endmodule 코드 10.33관계연산자사용

47한국기술교육대학교 정보기술공학부

Page 48: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.4비교기4비트비교기

module comp_if(a, b, agtb, altb, aeqb);input [3:0] a, b;output altb agtb aeqb;

if 조건문사용

output altb, agtb, aeqb;reg altb, agtb, aeqb;

always @(a or b) beginaltb = 1'b0;agtb = 1'b0;aeqb = 1'b0;if( b) b 1'b1if(a == b) aeqb = 1'b1;else if(a > b) agtb = 1'b1;else altb = 1'b1;

endendendmodule 코드 10.34

48한국기술교육대학교 정보기술공학부

Page 49: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.4비교기4비트비교기module comp for(a b agtb altb aeqb); f 문사용module comp_for(a, b, agtb, altb, aeqb);

parameter size=4;input [size-1:0] a, b;output altb, agtb, aeqb;reg altb agtb aeqb;

for 문사용

reg altb, agtb, aeqb;integer i;

always @(a or b) beginaltb 1'b0altb = 1'b0;agtb = 1'b0;aeqb = 1'b1;for(i = size-1; i > = 0; i=i-1) begin : comp_loop

if( [i] ! b[i]) b iif(a[i] != b[i]) beginagtb = a[i];altb = ~a[i];aeqb = 0;di bl ldisable comp_loop;

endend

endd d l 코드 10 35

49

endmodule 코드 10.35

한국기술교육대학교 정보기술공학부

Page 50: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.4비교기4비트비교기

d l tbmodule tb_comp;reg [3:0] a, b;wire agtb, altb, aeqb;

// comp assign Uo(a b agtb altb aeqb);

테스트벤치

// comp_assign Uo(a, b, agtb, altb, aeqb);// comp_if Uo(a, b, agtb, altb, aeqb);

comp_for Uo(a, b, agtb, altb, aeqb);

always beginalways begina = 4'b0001; b = 4'b1001;

#20 a = 4'b0010; b = 4'b0000;#20 a = 4'b1100; b = 4'b1101;#20 a = 4'b1001; b = 4'b1001;#20 a = 4'b0111; b = 4'b0100;#20 a = 4'b1111; b = 4'b1111;#20 a = 4'b1101; b = 4'b1001;#20 a = 4'b0000; b = 4'b0000;#20 1010 1010#20 a = 4'b1010; b = 4'b1010;#20 a = 4'b0001; b = 4'b1011;#20;

endendmodule 코드 10 36

50

endmodule 코드 10.36

한국기술교육대학교 정보기술공학부

Page 51: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.4비교기4비트비교기

시뮬레이션결과

51한국기술교육대학교 정보기술공학부

Page 52: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.4비교기코드 10.33의합성결과

코드 10.34의합성결과

52한국기술교육대학교 정보기술공학부

Page 53: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.4비교기4비트비교기

코드 10.35의합성결과

53한국기술교육대학교 정보기술공학부

Page 54: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.5 Tri-state 버스3상태버스드라이버회로의여러부분에서생성된신호들을공통버스를통해회로의다른회로의여러부분에서생성된신호들을공통버스를통해회로의다른

부분으로전송하기위해사용

제어신호는데이터를버스에보내거나또는데이터소스를버스로부터

격리시켜 high impedance 상태로만듬

조건연산자또는게이트프리미티브를사용하여모델링

CircuitBlock-A

data_a

bus_dataenable_a

CircuitBlock-B

data_b

54

enable_b

한국기술교육대학교 정보기술공학부

Page 55: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.5 Tri-state 버스3상태버스드라이버

module tristate_conop(in, oe, out);input in, oe;output out;

조건연산자사용

output out;

assign out =(oe) ? in : 1'bz;endmodule 코드 10.37

module tristate_gate(in, oe, out); 게이트프리미티브사용input in, oe;output out;

bufif1 b1(out in oe); // Active-high oebufif1 b1(out, in, oe); // Active high oe

endmodule 코드 10.38

55한국기술교육대학교 정보기술공학부

Page 56: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.5 Tri-state 버스3상태버스드라이버`define PERIOD 20 테스트벤치define PERIOD 20

module tb_tristate;reg in, oe;

테스트벤치

// tristate_gate U0_tristate_1(in, oe, out);tristate_conop U0_tristate_2(in, oe, out);

initial begininitial begin#0 oe = 1'b0; in = 1'b1;#(`PERIOD) in = 1'b0; #(`PERIOD) in = 1'b1; oe = 1'b1;#(`PERIOD) i 1'b0#( PERIOD) in = 1'b0;#(`PERIOD*2) in = 1'b1;#(`PERIOD*3) in = 1'b0;#(`PERIOD) oe = 1'b0;#(`PERIOD*2) i 1'b1#(`PERIOD*2) in = 1'b1;#(`PERIOD) in = 1'b1;#(`PERIOD*15) $stop;

endd d l 코드 10 39

56

endmodule 코드 10.39

한국기술교육대학교 정보기술공학부

Page 57: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.5 Tri-state 버스3상태버스드라이버

시뮬레이션결과

57한국기술교육대학교 정보기술공학부

Page 58: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.5 Tri-state 버스양방향버스드라이버

보내기와받기를동시에처리

rcv

received_data보내기와받기를동시에처리

Circuit bus_datasend_dataBlock

sendmodule bidir_bus(send, send_data, rcv, received_data, bus_data);

input send, rcv;input [7:0] send_data;inout [7:0] bus_data;output [7:0] received_data;

assign received_data =(rcv) ? bus_data : 8'bz;assign bus_data =(send) ? send_data : bus_data;

58한국기술교육대학교 정보기술공학부

endmodule 코드 10.40

Page 59: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.5 Tri-state 버스양방향버스드라이버

‘define PERIOD 20 테스트벤치module tb_bidir_bus;

reg send, rcv;reg [7:0] send_data;wire [7:0] received_data, bus_data;

테스트벤치

bidir_bus U0(send, send_data, rcv, received_data, bus_data);

initial begin#0 send = 1'b0; rcv = 1'b0; send_data = 8'h00;#(‘PERIOD) send_data = 8'h07; #(‘PERIOD) send_data = 8'h15; send = 1'b1;#(‘PERIOD) send_data = 8'hAB; #(‘PERIOD) send_data = 8'h34; #(‘PERIOD) send data = 8'h11; send = 1'b0; rcv = 1'b1;#( PERIOD) send_data = 8 h11; send = 1 b0; rcv = 1 b1;#(‘PERIOD*2) send_data = 8'h21; #(‘PERIOD) send_data = 8'h77;#(‘PERIOD*2) send_data = 8'h66; send = 1'b1; rcv = 1'b1;#(‘PERIOD) send_data = 8'h12; ##(‘PERIOD) send_data = 8'hCF; send = 1'b0; rcv = 1'b0;#(‘PERIOD) send_data = 8'h89;#(‘PERIOD) send_data = 8'h65; #(‘PERIOD*15) $stop;

end

59

endendmodule 코드 10.41

한국기술교육대학교 정보기술공학부

Page 60: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.5 Tri-state 버스양방향버스드라이버

시뮬레이션결과

60한국기술교육대학교 정보기술공학부

Page 61: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.5 Tri-state 버스

그림 10 21의버스드라이버를조건연산자를사용하여모델링하고그림 10.21의버스드라이버를조건연산자를사용하여모델링하고, 시뮬레이션을통해검증 (단, 데이터는 8비트로설계)

CircuitBlock-A

data_a

Ci it

bus_dataMUX

CircuitBlock-B

bl

data_b

enable_a

enable_b

그림 10 21

61

그림 10.21

한국기술교육대학교 정보기술공학부

Page 62: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.6원하지않는래치의합성원하지않는래치의합성

case문에모든가능한 casecase 문에모든가능한 case 항목들이포함되지않는경우

if 조건문에서 else 블록이래치로합성되는 case 문의예

생략되는경우

순수한조합논리회로의합성

module case_latch(sel,in1,in2,y_out);input [1:0] sel;input [3:0] in1, in2;

모든가능한입력조건들에

대해출력값을명시적으로

output [3:0] y_out;reg [3:0] y_out;

지정

default 값을지정always @(sel or in1 or in2) begin

case(sel)2'b10 : y_out = in1;2'b01 : y_out = in2;

endcaseend

62한국기술교육대학교 정보기술공학부

endmodule 코드 10.42

Page 63: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.6원하지않는래치의합성래치로합성되는 case 문의예

코드 10.42의시뮬레이션결과

63한국기술교육대학교 정보기술공학부

Page 64: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.6원하지않는래치의합성래치로합성되는 case 문의예

코드 10.42의합성결과

래치

64한국기술교육대학교 정보기술공학부

Page 65: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.6원하지않는래치의합성래치로합성되는 if 문의예

module if_latch(a, b, y_out);i t binput a, b;output [3:0] y_out;reg [3:0] y_out;

always @(a or b) beginif (({a, b}) == 2'b11) y_out = 5;else if(({a b}) == 2'b10) y out = 2;else if(({a, b}) == 2'b10) y_out = 2;else if(({a, b}) == 2'b01) y_out = 3; // No else block

endendmodule 코드 10 43endmodule 코드 10.43

65한국기술교육대학교 정보기술공학부

Page 66: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.6원하지않는래치의합성래치로합성되는 if 문의예

코드 10.43의합성결과

래치

66한국기술교육대학교 정보기술공학부

Page 67: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.6원하지않는래치의합성case 문에 default 문이포함된경우래치가합성되지않음래치가합성되지않음

module case_no_latch1(sel, in1, in2, y_out);_ _ _input [1:0] sel, in1, in2;output [1:0] y_out;reg [1:0] y_out;

always @(sel or in1 or in2) begincase(sel)

2'b10 : y_out = in1;2'b01 : y_out = in2;default : y_out = 2'bx;

endcaseend

endmodule 코드 10.44

67한국기술교육대학교 정보기술공학부

Page 68: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.6원하지않는래치의합성case 문에 default 문이포함된경우

코드 10.44의합성결과

68한국기술교육대학교 정보기술공학부

Page 69: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.6원하지않는래치의합성case 문이초기값을갖는경우래치가합성되지않음래치가합성되지않음

module case_no_latch2(memce0, memce1, cs, en, addr);t t 0 1output memce0, memce1, cs;

input en;input [31:30] addr;reg memce0, memce1, cs;reg memce0, memce1, cs;

always @(addr or en) begin{memce0, memce1, cs} = 3'b0; // 초기값을 할당casez({addr, en})

3'b101: memce0 = 1'b1;3'b111: memce1 = 1'b1;3'b0?1: cs = 1'b1;3'b0?1: cs = 1'b1;

endcaseend

endmodule 코드 10.45

69

코드 10.45

한국기술교육대학교 정보기술공학부

Page 70: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

10.6원하지않는래치의합성case 문이초기값을갖는경우

코드 10.45의합성결과

70한국기술교육대학교 정보기술공학부

Page 71: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

Verilog HDL을이용한디지털시스템설계및실습

11 순차회로 모델링11. 순차회로 모델링

71한국기술교육대학교 정보기술공학부

Page 72: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

순차회로모델링순차회로

현재의입력, 과거의입력, 회로에기억된상태값에의해출력이결정과거의입력, 현재의상태값을저장하는기억소자(래치또는플립플롭)와조합논리회로로구성

데이터레지스터 시프트레지스터 계수기(counter) 직렬/병렬변환기데이터레지스터, 시프트레지스터, 계수기(counter), 직렬/병렬변환기, 유한상태머신(Finite State Machine; FSM), 주파수분주기, 펄스발생기등

클록신호에의해동작되는래치또는플립플롭을포함

래치와플립플롭

래치 : 클록신호의레벨(즉, 0 또는 1)에따라동작하는저장소자플립플롭 :클록신호의상승또는하강에지에동기되어동작하는저장소자플립플롭 : 클록신호의상승또는하강에지에동기되어동작하는저장소자always 구문내부에 if 조건문을이용하여모델링

순차회로의모델링

always 블록을이용한행위수준모델링, 게이트프리미티브및하위모듈인스턴스, 연속할당문등다양한 Verilog 구문들이 사용됨

할당문의형태 ( bl ki 또는 bl ki )에따라회로의동작과구조가달라짐

72

할당문의형태 (nonblocking 또는 blocking) 에따라회로의동작과구조가달라짐

한국기술교육대학교 정보기술공학부

Page 73: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.1 래치(Latch)Positive level-sensitive D latch

DQ q

dclock

dGclock

d

q

module dlatch(clock, d, q);input clock, d;output q;

Positive level-sensitive D latch

reg q;

always @(clock or d) begin if( l k)if(clock)

q = d;end

endmodule 코드 11 1

73

endmodule 코드 11.1

한국기술교육대학교 정보기술공학부

Page 74: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.1 래치(Latch)

module tb_dlatch ;lk d

Testbench for D latchreg clk, d;

dlatch U0(clk, d, q);

initial beginclk = 1'b0;forever #10 clk = ~clk;

end initial begin

d = 1'b0;fore er beginforever begin#15 d = 1'b1; #20 d = 1'b0;#10 d = 1'b1; #10 d = 1'b0;#10 d = 1'b1; #15 d = 1'b0;#10 d 1 b1; #15 d 1 b0;

endend

endmodule 코드 11.2

74한국기술교육대학교 정보기술공학부

Page 75: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.1 래치(Latch)

코드 11 1의시뮬레이션결과코드 11.1의시뮬레이션결과

Negative level-sensitive 방식으로동작하는 8비트 D 래치회로를설계하고, 테스트벤치를작성하여기능을검증한다.

75한국기술교육대학교 정보기술공학부

Page 76: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.1 래치(Latch)Active-low 리셋을갖는 positive level-sensitive D latch

module dlatch_rst(rst, clock, d, q);input rst, clock, d;put st, c oc , d;output q;reg q;

always @(clock or rst or d) begin if(!rst)

q = 1'b0;else if(clock)else if(clock)

q = d;end

endmodule 코드 11.3코드 11.3

76한국기술교육대학교 정보기술공학부

Page 77: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.1 래치(Latch)

코드 11.3의시뮬레이션결과.3의시뮬레이션결과

Active-low 셋과리셋을갖는 negative level-sensitive 8비트 D 래치회로를설계하고, 테스트벤치를작성하여기능을검증한다.

77한국기술교육대학교 정보기술공학부

Page 78: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.1 래치(Latch)Latch가포함된회로에 blocking 할당문이사용된경우

module latch_blk(en, a, b, c, y);input en, a, b, c;output y;reg m y;

코드 11.4

reg m, y;

always @(en or a or b or c) begin if(en) begin( ) g

m = ~(a | b);y = ~(m & c);

endend

endmodule 합성결과

Latch

78한국기술교육대학교 정보기술공학부

Page 79: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.1 래치(Latch)Latch가포함된회로에 nonblocking 할당문이사용된경우

module latch nonblk(en, a, b, c, y); 코드 11.5_ yinput en, a, b, c;output y;reg m, y;

always @(en or a or b or c) begin if(en) begin

m <= ~(a | b);m <= ~(a | b);y <= ~(m & c);

endend

합성결과endmodule 합성결과

Latch

Latch

Latch

79한국기술교육대학교 정보기술공학부

Page 80: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.1 래치(Latch)

코드 11 6은코드 11 4에서 bl ki 할당문의순서를바꾼경우이다코드 11.6은코드 11.4에서 blocking 할당문의순서를바꾼경우이다. 코드 11.4와동일한회로인지, 만약다른회로라면어떤회로가되는지시뮬레이션과합성을통해확인하고 그이유를생각해본다시뮬레이션과합성을통해확인하고, 그이유를생각해본다.

module latch blk2(en, a, b, c, y); 코드 11 6module latch_blk2(en, a, b, c, y);input en, a, b, c;output y;reg m, y;

코드 11.6

always @(en or a or b or c) begin if(en) begin

y = ~(m & c);m = ~(a | b);

endend

endmodule

80한국기술교육대학교 정보기술공학부

Page 81: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.1 래치(Latch)

코드 11 7은코드 11 5에서 bl ki 할당문의순서를바꾼코드 11.7은코드 11.5에서 nonblocking 할당문의순서를바꾼경우이다. 코드 11.5와동일한회로인지, 만약다른회로라면어떤회로가되는지시뮬레이션과합성을통해확인하고 그이유를생각해본다되는지시뮬레이션과합성을통해확인하고, 그이유를생각해본다.

module latch nonblk2(en, a, b, c, y); 코드 11 7module latch_nonblk2(en, a, b, c, y);input en, a, b, c;output y;reg m, y;

코드 11.7

always @(en or a or b or c) begin if(en) begin

y <= ~(m & c);m <= ~(a | b);

endend

endmodule

81한국기술교육대학교 정보기술공학부

Page 82: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.2 플립플롭(Flip flop)Positive edge-triggered D Flip-flop

D

Q qd

clk

dclock q

module dff(clk, d, q);input d ,clk;output q;

Positive edge-triggered D Flip-flop

output q;reg q;

always @(posedge clk)y p gq <= d;

endmodule 코드 11.8

82한국기술교육대학교 정보기술공학부

Page 83: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.2 플립플롭(Flip flop)

코드 11.8의시뮬레이션결과

83한국기술교육대학교 정보기술공학부

Page 84: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.2 플립플롭(Flip flop)Edge-triggered D Flip-flop with q and qb outputsmodule dff bad1(clk d q q bar);module dff_bad1(clk, d, q, q_bar);input d, clk;output q, q_bar;reg q, q_bar;

always @(posedge clk) begin // nonblocking assignmentsq <= d; q_bar <= ~d;

endendmodule 코드 11.9(a)

module dff_bad2(clk, d, q, q_bar);input d clk;

Not Recommendedinput d, clk;output q, q_bar;reg q, q_bar;

l @( d lk) b i // bl ki ialways @(posedge clk) begin // blocking assignmentsq = d;q_bar = ~d;

end

84

endmodule 코드 11.9(b)

한국기술교육대학교 정보기술공학부

Page 85: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.2 플립플롭(Flip flop)Edge-triggered D Flip-flop with q and qb outputs

module dff_good(clk, d, q, q_bar);input d, clk;output q, q bar;

Recommended

p q, q_reg q;

// using assign statement for q_barassign q_bar = ~q;

always @(posedge clk)q <= d;q <= d;

endmodule 코드 11.9(c)

85한국기술교육대학교 정보기술공학부

Page 86: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.2 플립플롭(Flip flop)Edge-triggered D Flip-flop with q and qb outputs

Flip flop

코드 11.9 (a), (b)의합성결과

Flip flop

코드 11 9 (c)의Flip flop

코드 11.9 (c)의합성결과

86한국기술교육대학교 정보기술공학부

Page 87: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.2 플립플롭(Flip flop)

와 b 출력을갖는 D플립플롭을코드 11 10과같이모델링하면q와 q_bar 출력을갖는 D 플립플롭을코드 11.10과같이모델링하면, 단순 D 플립플롭이아닌다른회로가된다. 시뮬레이션과합성을통해코드 11 10의모델링이어떤회로로동작하는지확인하고 그이유를코드 11.10의모델링이어떤회로로동작하는지확인하고, 그이유를생각해본다.

module dff_bad3(clk, d, q, q_bar);input d, clk;output q, q_bar;

코드 11.10

reg q, q_bar;

always @(posedge clk) begin q <= d;qq_bar <= ~q;

endendmodule

87한국기술교육대학교 정보기술공학부

Page 88: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.2 플립플롭(Flip flop)Edge-triggered D Flip-flop with synchronous active-low reset

module dff_sync_rst(clk, d, rst_n, q, qb);input clk, d, rst_n;output q qb;output q, qb;reg q;

assign qb = ~q;

always @(posedge clk) // include only clkbegin

if(!rst_n) // active-low reset_q <= 1'b0;

elseq <= d;

endendendmodule 코드 11.11

88한국기술교육대학교 정보기술공학부

Page 89: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.2 플립플롭(Flip flop)Edge-triggered D Flip-flop with synchronous active-low reset

코드 11 11의시뮬레이션결과코드 11.11의시뮬레이션결과

89한국기술교육대학교 정보기술공학부

Page 90: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.2 플립플롭(Flip flop)Edge-triggered D Flip-flop with asynchronous active-low reset

module dff_async_rst(clk, d, rst_n, q, qb);input clk, d, rst_n;output q, qb;reg q;

assign qb = ~q;

always @(posedge clk or negedge rst_n) // both clk and rst_nbegin

if(!rst n) // active-low resetif(!rst_n) // active low resetq <= 1'b0;

elseq <= d;

endendmodule 코드 11.12

90한국기술교육대학교 정보기술공학부

Page 91: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.2 플립플롭(Flip flop)Edge-triggered D Flip-flop with asynchronous active-low reset

코드 11 12의시뮬레이션결과코드 11.12의시뮬레이션결과

91한국기술교육대학교 정보기술공학부

Page 92: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.2 플립플롭(Flip flop)

다음의 D 플립플롭을 Verilog HDL로모델링하고, 시뮬레이션을통해동작을확인한다.

①동기식 active-high 리셋을갖는 D 플립플롭②동기식 active-high 셋을갖는 D 플립플롭③동기식 active-low 셋과리셋을갖는 D 플립플롭④비동기식 active-high 리셋을갖는 D 플립플롭⑤비동기식 active-high 셋을갖는 D 플립플롭⑥비동기식 active-low 셋과리셋을갖는 D 플립플롭

92한국기술교육대학교 정보기술공학부

Page 93: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.2 플립플롭(Flip flop)플립플롭이포함된회로에 blocking 할당문이사용된경우

module seq_blk(clk, a, b, c, d, e, y); 코드 11.13input clk, a, b, c, d, e;output y;reg m, n, y;

always @(posedge clk) begin m = ~(a & b);n = c | d;y = ~(m | n | e);y = ~(m | n | e);

endendmodule 합성결과

Fli FlFlip Flop

93한국기술교육대학교 정보기술공학부

Page 94: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.2 플립플롭(Flip flop)플립플롭이포함된회로에 nonblocking 할당문이사용된경우

module seq_nonblk(clk, a, b, c, d, e, y); 코드 11.14input clk, a, b, c, d, e;output y;reg m, n, y;

always @(posedge clk) begin m <= ~(a & b);n <= c | d;y <= ~(m | n | e);y <= ~(m | n | e);

endendmodule 합성결과

Flip Flop

Flip FlopFlip Flop

Flip Flop

94한국기술교육대학교 정보기술공학부

Page 95: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.1.2 플립플롭(Flip flop)

코드 11 15는코드 11 13에서 bl ki 할당문의순서를바꾼경우이다코드 11.15는코드 11.13에서 blocking 할당문의순서를바꾼경우이다. 코드 11.13과동일한회로인지, 만약다른회로라면어떤회로가되는지시뮬레이션과합성을통해확인하고 그이유를생각해본다시뮬레이션과합성을통해확인하고, 그이유를생각해본다.

module seq_blk2(clk, a, b, c, d, e, y);input clk, a, b, c, d, e;output y;reg m, n, y;

코드 11.15

reg m, n, y;

always @(posedge clk) begin y = ~(m | n | e);m = ~(a & b);m = ~(a & b);n = c | d;

endendmodule

95한국기술교육대학교 정보기술공학부

Page 96: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.2 Blocking과 Nonblocking 할당문

순차회로에서 blocking할당문순서의영향

d l blk1( lk d 3) d l blk2( lk d 3)module blk1(clk, d, q3);input clk;output q3;input d;

module blk2(clk, d, q3);input clk;output q3;input d;

reg q3, q2, q1, q0;

always @(posedge clk) beginq0 = d; q1 = q0;

reg q3, q2, q1, q0;

always @(posedge clk) beginq3 = q2; q2 = q1;q0 d; q1 q0;

q2 = q1; q3 = q2;end

endmodule 코드 11.16(a)

q3 q2; q2 q1;q1 = q0; q0 = d;

endendmodule 코드 11.16(b)

Flip Flop

Shift registerFlip Flop Flip Flop Flip FlopFlip Flop

Shift register

96한국기술교육대학교 정보기술공학부

Page 97: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.2 Blocking과 Nonblocking 할당문

순차회로에서 nonblocking할당문순서의영향

module non_blk1(clk, d, q3);input clk;output q3;input d;

module non_blk2(clk, d, q3);input clk;output q3;input d;input d;

reg q3, q2, q1, q0;

always @(posedge clk) begin

input d;reg q3, q2, q1, q0;

always @(posedge clk) beginq0 <= d;q1 <= q0;q2 <= q1;3 2

q3 <= q2;q2 <= q1;q1 <= q0;0 dq3 <= q2;

endendmodule 코드 11.17(a)

q0 <= d;end

endmodule 코드 11.17(b)

Flip Flop Flip Flop Flip FlopFlip Flop

97한국기술교육대학교 정보기술공학부

Page 98: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.2 Blocking과 Nonblocking 할당문

코딩가이드라인

가이드라인-1:순차회로또는래치를모델링하는 always블록에서는가이드라인 1 순차회로또는래치를모델링하는 always 블록에서는nonblocking할당문을사용한다.가이드라인-2:조합논리회로를모델링하는 always 블록에서는 blocking할당문을사용한다.

98한국기술교육대학교 정보기술공학부

Page 99: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.2 Blocking과 Nonblocking 할당문

가이드라인-3:동일한 always 블록에서순차회로와조합논리회로를함께표현하는경우에는 nonblocking할당문을사용한다.표현하는경우에는 nonblocking 할당문을사용한다.

99한국기술교육대학교 정보기술공학부

Page 100: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.2 Blocking과 Nonblocking 할당문

가이드라인-4:동일한 always 블록내에서 blocking할당문과nonblocking할당문을혼합해서사용하지않는다.

module ba_nba1(q, a, b, clk, rst_n);output q;input a, b, rst_n, clk;reg q, tmp;always @(posedge clk or negedge rst_n)

if(!rst_n) q <= 1'b0;else begin

tmp = a & b;< tq <= tmp;

endendmodule

module ba_nba2(q, a, b, clk, rst_n);

코드 11.18(a)Bad Coding

output q;input a, b, rst_n, clk;reg q, tmp;always @(posedge clk or negedge rst_n)

if(! t ) 1'b0 //bl kiif(!rst_n) q = 1'b0; //blockingelse begin

tmp = a & b;q <= tmp; //nonblocking

end

100

endendmodule 코드 11.18(b)

한국기술교육대학교 정보기술공학부

Page 101: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.2 Blocking과 Nonblocking 할당문

module ba_nba3(q, a, b, clk, rst_n);output q;i t b t lkinput a, b, rst_n, clk;reg q;

always @(posedge clk or negedge rst_n)if(!rst_n) q <= 1'b0;else q <= a & b;

endmodule 코드 11.18(c)d l b b 4( b lk t )

Good Codingmodule ba_nba4(q, a, b, clk, rst_n);

output q;input a, b, rst_n, clk;reg q;

Coding

wire tmp;

assign tmp = a & b;

always @(posedge clk or negedge rst_n)if(!rst_n) q <= 1'b0;else q <= tmp;

d d l 코드 11 18(d)

101

endmodule 코드 11.18(d)

한국기술교육대학교 정보기술공학부

Page 102: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.2 Blocking과 Nonblocking 할당문

가이드라인-5:다수의 always 블록에서동일한 reg 변수에값을할당하지않는다.

module badcode1(q, d1, d2, clk, rst_n);output q;input d1, d2, clk, rst_n;reg q;

always @(posedge clk or negedge rst n)always @(posedge clk or negedge rst_n)if(!rst_n) q <= 1'b0;else q <= d1;

l @( d lk d t )always @(posedge clk or negedge rst_n)if(!rst_n) q <= 1'b0;else q <= d2;

endmodule 코드 11.19

Multiple source driving이발생하는코딩

102한국기술교육대학교 정보기술공학부

Page 103: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.2 Blocking과 Nonblocking 할당문

코드 11 20 (a) (b)의출력 y1 y2를확인하고 그이유를생각해본다코드 11.20 (a), (b)의출력 y1, y2를확인하고, 그이유를생각해본다.코드 11.20 (a), (b)의합성결과, 동일한회로가되는지, 아니면서로다른회로가되는지확인한다.

module fbosc_blk(y1, y2, clk, rst);output y1, y2;i t lk t

Blocking 할당문사용

input clk, rst;reg y1, y2;

always @(posedge clk or posedge rst)always @(posedge clk or posedge rst)if(rst) y1 = 0; // resetelse y1 = y2;

always @(posedge clk or posedge rst)if(rst) y2 = 1; // setelse y2 = y1;

d d l 코드 11 20( )

103

endmodule 코드 11.20(a)

한국기술교육대학교 정보기술공학부

Page 104: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.2 Blocking과 Nonblocking 할당문

module fbosc_nonblk(y1, y2, clk, rst);t t 1 2

Nonblocking 할당문사용output y1, y2;input clk, rst;reg y1, y2;

always @(posedge clk or posedge rst)if(rst) y1 <= 0; // resetelse y1 <= y2;

always @(posedge clk or posedge rst)if(rst) y2 <= 1; // setl 2 < 1else y2 <= y1;

endmodule 코드 11.20(b)

104한국기술교육대학교 정보기술공학부

Page 105: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3 시프트레지스터시프트레지스터

클록신호가인가될때마다데이터가왼쪽또는오른쪽으로이동되는회로

여러개의플립플롭이직렬로연결된구조

형태

직렬입력-직렬출력(Serial-In, Serial-Out)직렬입력-병렬출력(Serial-In, Parallel-Out)병렬입력-직렬출력(Parallel-In, Serial-Out)병렬입력-병렬출력(Parallel-In, Parallel-Out) 왼쪽시프트, 오른쪽시프트, 양방향시프트

nonblocking할당문 시프트연산자 결합연산자 반복문등다양한nonblocking 할당문, 시프트연산자, 결합연산자, 반복문등다양한구문으로모델링

105한국기술교육대학교 정보기술공학부

Page 106: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.1 직렬입력-직렬출력시프트레지스터

Q D Q DQ D DQ sinsoutq[0]q[1]q[2]q[7]

clk

DFF DFFDFF DFF

rst

그림 11.17 직렬입력-직렬출력시프트레지스터

module shift_reg_nblk1(clk, rst, sin, sout);input clk, rst, sin;output sout;

q[0] <= sin;q[1] <= q[0];q[2] <= q[1];

reg [7:0] q;

assign sout = q[7];

q[3] <= q[2];q[4] <= q[3];q[5] <= q[4];q[6] <= q[5];

always @(posedge clk) beginif(!rst)

q <= 8'b0;else begin 코드 11 21

q[ ] q[ ]q[7] <= q[6];end

endendmodule

106

else begin 코드 11.21 endmodule

한국기술교육대학교 정보기술공학부

Page 107: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.1 직렬입력-직렬출력시프트레지스터

module shift_reg_nblk2(clk, rst, sin, sout);input clk, rst, sin;output sout;

[7 0]reg [7:0] q;

assign sout = q[7];

always @(posedge clk) beginif(!rst)

q <= 0;else begin

q[0] <= sin;q[7:1] <= q[6:0];

dendend

endmodule 코드 11.22벡터할당문을사용한경우

107한국기술교육대학교 정보기술공학부

Page 108: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.1 직렬입력-직렬출력시프트레지스터

코드 11.22의시뮬레이션결과

그림 11.17의시프트레지스터를다음의방법으로모델링하고, 시뮬레이션한다.①결합연산자를사용하는방법

②시프트연산자를사용하는방법

③ for반복문을사용하는방법

108

③ for 반복문을사용하는방법

한국기술교육대학교 정보기술공학부

Page 109: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.2 병렬입력-병렬출력시프트레지스터

병렬입력-병렬출력시프트레지스터

pout[0]pout[1]pout[2]pout[8] din[0]din[1]din[2]din[8]

Q D Q D DQ

MUX

Q D

MUXMUXload load load

DFFQ D

DFFQ D

DFFDQ

DFFQ D

clkrst

그림 11 19 병렬입력-직렬출력시프트레지스터그림 11.19 병렬입력 직렬출력시프트레지스터

109한국기술교육대학교 정보기술공학부

Page 110: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.2 병렬입력-병렬출력시프트레지스터

module pld_shift_reg(clk, rst, load, din, pout);input clk, rst, load;input [7:0] din;t t [7 0] toutput [7:0] pout;

reg [7:0] data_reg;

assign pout = data reg;assign pout data_reg;

always @(posedge clk) beginif(!rst) data_reg <= 0;else

if(load) data_reg <= din;else data_reg <= data_reg << 1;

dendendmodule 코드 11.23

110한국기술교육대학교 정보기술공학부

Page 111: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.2 병렬입력-병렬출력시프트레지스터

코드 11.23의시뮬레이션결과

직렬입력-병렬출력 8비트시프트레지스터를모델링하고, 시뮬레이션을통해동작을확인한다.

111한국기술교육대학교 정보기술공학부

Page 112: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.2 병렬입력-병렬출력시프트레지스터

양방향병렬포트를갖는시프트레지스터

데이터의병렬로드(wr=0)와시프팅동작(en=0)이동시에일어나지않으며, ( ) ( ) ,data_io 포트가 inout 이므로 rd 신호와 wr 신호가동시에 0이되지않는다.

표 11 1 양방향병렬포트를갖는시프트레지스터의신호정의

신호이름 기 능

clk 클록신호

표 11.1 양방향병렬포트를갖는시프트레지스터의신호정의

clk 클록신호

rst 리셋신호(Active Low)en 시프팅동작을위한 enable 신호(Active Low)wr 병렬로드를위한 enable 신호(Active Low)rd 병렬출력을위한 enable 신호(Active Low)si 직렬입력si 직렬입력

so 직렬출력

data_io 병렬입/출력데이터(inout)

112한국기술교육대학교 정보기술공학부

Page 113: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.2 병렬입력-병렬출력시프트레지스터

module shifter(clk, rst, en, wr, rd, si, so, data io);

양방향병렬포트를갖는시프트레지스터

( , , , , , , , _ );parameter Len = 8;input clk, rst, en, wr, rd, si;output so;inout [Len-1:0] data_io;_reg [Len-1:0] shift_reg;

assign data_io = !rd ? shift_reg : {Len{1'bz}};assign so = shift_reg[7];

always @(posedge clk) beginif(!rst)

shift_reg <= {Len{1'b0}};else beginelse begin

if(!en) beginshift_reg <= shift_reg << 1;shift_reg[0] <= si;

endendelse if(!wr)

shift_reg <= data_io;end

end

113

endmodule 코드 11.24

한국기술교육대학교 정보기술공학부

Page 114: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.2 병렬입력-병렬출력시프트레지스터

양방향병렬포트를갖는시프트레지스터

코드 11.24의시뮬레이션결과(직렬입력데이터의시프팅동작)

114

( )

한국기술교육대학교 정보기술공학부

Page 115: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.2 병렬입력-병렬출력시프트레지스터

양방향병렬포트를갖는시프트레지스터

코드 11.24의시뮬레이션결과(병렬입력데이터의시프팅동작)

115

(병렬입력데이터의시프팅동작)

한국기술교육대학교 정보기술공학부

Page 116: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.2 병렬입력-병렬출력시프트레지스터

코드 11 24의시프트레지스터에좌/우시프팅기능을추가하여설계코드 11.24의시프트레지스터에좌/우시프팅기능을추가하여설계하고, 시뮬레이션을통해동작을확인한다. 신호의정의는다음과같다.

신호이름 기 능

clk 클록신호

rst 리셋신호(Active Low)rst 리셋신호(Active Low)en 시프팅동작을위한 enable 신호(Active Low)wr 병렬로드를위한 enable 신호(Active Low)rd 병렬출력을위한 enable 신호(Active Low)si 직렬입력

so 직렬출력

data_io 병렬입/출력데이터(inout)

mode 좌/우시프팅선택신호(mode=0;오른쪽시프트 mode=1;왼쪽시프트)

116

(mode 0; 오른쪽시프트, mode 1; 왼쪽시프트)

한국기술교육대학교 정보기술공학부

Page 117: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.3 선형귀환시프트레지스터

선형귀환시프트레지스터(Linear Feedback Shift Register; LFSR)선형귀환을통해 2진비트열을생성

N-비트시프트레지스터에의해, 최대 2N개의의사난수시퀀스생성

계수기, 의사난수(pseudo-random sequence) 발생, 데이터암호/복호, 데이터압축 데이터무결성검사합(d t i t it h k )등디지털데이터압축, 데이터무결성검사합(data integrity checksum) 등디지털시스템에서매우다양한목적으로사용

시스템 IC의내장형자기진단(Built-In Self-Test; BIST)회로에사용되어시스템 IC의내장형자기진단(Built In Self Test; BIST) 회로에사용되어테스트벡터생성및테스트응답분석에도사용

DFFd q sout

DFFd q

DFFd q

DFFd q

DFFd q

DFFd q

DFFd q

DFFd q

clkrst

그림 11 23 LFSR회로

117

그림 11.23 LFSR 회로

한국기술교육대학교 정보기술공학부

Page 118: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.3 선형귀환시프트레지스터

선형귀환시프트레지스터(Linear Feedback Shift Register; LFSR)시프트레지스터의초기화시프트레지스터의초기화

XOR게이트가사용되는 LFSR : 레지스터를 0으로초기화시키지않는다.0이계속시프팅되므로, LFSR로동작할수없다.

XNOR게이트가사용되는 LFSR : 레지스터를 1로초기화시키지않는다.1이계속시프팅되므로, LFSR로동작할수없다.

3비트 LFSR

118한국기술교육대학교 정보기술공학부

Page 119: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.3 선형귀환시프트레지스터

module lfsr Bad1(q3 clk pre n); Bad Code

3비트 LFSR

module lfsr_Bad1(q3, clk, pre_n);output q3;input clk, pre_n;reg q3, q2, q1;

Bad Code

assign n1 = q1 ^ q3;

// Blocking 할당문을 사용한 경우always @(posedge clk or negedge pre_n)

if(!pre_n) beginq3 = 1'b1;q2 = 1'b1;q2 1 b1;q1 = 1'b1;

endelse begin

q3 = q2;q3 = q2;q2 = n1;q1 = q3;

end

119

endmodule 코드 11.25(a)

한국기술교육대학교 정보기술공학부

Page 120: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.3 선형귀환시프트레지스터3비트 LFSR

module lfsr_Good1(q3, clk, pre_n);output q3;input clk, pre_n;reg q3 q2 q1;

Good Code

reg q3, q2, q1;

// Nonblocking 할당문을 사용한 경우always @(posedge clk or negedge pre_n)

if(! ) b iif(!pre_n) beginq3 <= 1'b1;q2 <= 1'b1;q1 <= 1'b1;q

endelse begin

q3 <= q2;q2 <= q1 ^ q3;q2 < q1 q3;q1 <= q3;

endendmodule 코드 11.25(b)

120한국기술교육대학교 정보기술공학부

Page 121: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.3 선형귀환시프트레지스터3비트 LFSR

module lfsr_Good2(q3, clk, pre_n);o tp t q3

Good Codeoutput q3;input clk, pre_n;reg q3, q2, q1;

할당 을 사용한 경우// Nonblocking 할당문을 사용한 경우always @(posedge clk or negedge pre_n)

if(!pre_n) {q3,q2,q1} <= 3'b111;{q3,q ,q } 3 b ;

else {q3,q2,q1} <= {q2,(q1^q3),q3};

endmodule 코드 11 25(c)endmodule 코드 11.25(c)

121한국기술교육대학교 정보기술공학부

Page 122: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.3 선형귀환시프트레지스터3비트 LFSR

module lfsr_Bad2(q3, clk, pre_n);output q3;input clk, pre_n;

Bad Code

reg q3, q2, q1;

always @(posedge clk or negedge pre_n)if(! )if(!pre_n)

{q3,q2,q1} <= 3'b000;else

{q3,q2,q1} <= {q2,(q1 ^ q3),q3};{q3,q2,q1} < {q2,(q1 q3),q3};

endmodule 코드 11.25(d)

122한국기술교육대학교 정보기술공학부

Page 123: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.3 선형귀환시프트레지스터3비트 LFSR

(a)

(b)

(c)

(d)

(c)

코드 11 25의시뮬레이션결과

123

코드 11.25의시뮬레이션결과

한국기술교육대학교 정보기술공학부

Page 124: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.3.3 선형귀환시프트레지스터

그림 11.23의 8비트 LFSR 회로를모델링하고, 시뮬레이션을통해동작을확인한다.

DFFd q sout

DFFd q

DFFd q

DFFd q

DFFd q

DFFd q

DFFd q

DFFd q

clkrst

그림 11.23

124한국기술교육대학교 정보기술공학부

Page 125: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.4 계수기 (Counter)계수기(counter)

클록펄스가인가될때마다값을증가또는감소시키는회로

주파수분주기, 타이밍제어신호생성등에사용동기식계수기

모든플립플롭이하나의공통클록신호에의해구동되며, 모든플립플롭의상태변경이동시에일어남

장점 : 설계와검증이용이하며, 계수속도가빠름단점 : 비동기식카운터에비하여회로가복잡함

비동기식계수기

첫단의플립플롭에클록신호가인가되면 플립플롭의출력이다음단의첫단의플립플롭에클록신호가인가되면, 플립플롭의출력이다음단의플립플롭을트리거시키는방식으로동작

리플계수기(ripple counter)라고도함장점 : 동기식계수기에비해회로가단순해짐단점 : 각플립플롭의전파지연시간이누적되어최종단의출력에나타나므로계수속도가느림

125한국기술교육대학교 정보기술공학부

Page 126: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.4 계수기 (Counter)8비트증가계수기

module counter_up(clk, rst, cnt);i t lk tinput clk, rst;output [7:0] cnt;reg [7:0] cnt;

always @(posedge clk or negedge rst) beginif(!rst) cnt <= 0;else cnt <= cnt + 1;

endendmodule 코드 11.26

126한국기술교육대학교 정보기술공학부

Page 127: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.4 계수기 (Counter)8비트증가계수기

코드 11.26

코드 11 26의시뮬레이션결과코드 11.26의시뮬레이션결과

127한국기술교육대학교 정보기술공학부

Page 128: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.4 계수기 (Counter)

신 를갖는 비 감소계수기를설계하 시뮬레이션을Enable 신호(active high)를갖는 8비트감소계수기를설계하고, 시뮬레이션을통해동작을확인한다. Enable 신호 en=1이면계수기가동작하고, en=0이면계수동작을멈추는기능을갖는다.

Mode 신호에따라계수기값이증가(mode=1) 또는감소(mode=0)되는 8비트증가/감소계수기를설계하고, 시뮬레이션을통해동작을확인한다.

설계과제 11 13과 11 14에서설계된기능에추가하여 8비트데이터의설계과제 11.13과 11.14에서설계된기능에추가하여 8비트데이터의병렬로드(load=1) 기능을갖는 8비트증가/감소계수기를설계하고, 시뮬레이션을통해동작을확인한다.

128한국기술교육대학교 정보기술공학부

Page 129: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.4 계수기 (Counter)1/10 주파수분주기(frequency divider)

module frq_div(mclk, rst, clk_div);input rst, mclk;output clk_div;reg [3:0] cnt;

lk direg clk_div;

always @(posedge mclk or posedge rst) beginif(!rst) begin

cnt <= 0;clk_div <= 0;

endelse begin

if(cnt == 9) begincnt <= 0;cnt < 0;clk_div <= 1'b1;

endelse begin

clk_div <= 1'b0;cnt <= cnt + 1;cnt <= cnt + 1;

endend

endendmodule 코드 11.27

129한국기술교육대학교 정보기술공학부

Page 130: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.4 계수기 (Counter)1/10 주파수분주기(frequency divider)

코드 11.27의시뮬레이션결과

Duty cycle이 50%인대칭분주클록을생성하는 1/10 주파수분주기를설계하고 시뮬레이션을통해동작을확인한다

130

설계하고, 시뮬레이션을통해동작을확인한다.

한국기술교육대학교 정보기술공학부

Page 131: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5 유한상태머신(FSM)유한상태머신(Finite State Machine; FSM)

지정된수의상태를가지고상태들간의천이에의해출력을생성하는회로

디지털시스템의제어회로구성에사용

Moore 머신 : 출력이단지현재상태에의해서결정Mealy 머신 : 현재상태와입력에의해출력이결정

M l 머신의경우

Next state logic State register Output logic출력

입력

Mealy 머신의경우

g(조합논리회로)

g(순차회로)

p g(조합논리회로) 출력

클록 리셋

그림 11.28 유한상태머신의구조

131한국기술교육대학교 정보기술공학부

Page 132: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5 유한상태머신(FSM)상태값의이진데이터의인코딩방식에따라상태레지스터의비트수가

달라짐

표 11.3 상태인코딩형식

상태번호 2진인코딩 Gray 인코딩 Johnson 인코딩 One-hot 인코딩

0 000 000 0000 00000001

1 001 001 0001 00000010

2 010 011 0011 00000100

3 011 010 0111 000010003 011 010 0111 00001000

4 100 110 1111 00010000

5 101 111 1110 00100000

6 110 101 1100 01000000

7 111 100 1000 10000000

132한국기술교육대학교 정보기술공학부

Page 133: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5 유한상태머신(FSM)유한상태머신(FSM)의코딩가이드라인

각각의상태머신을독립된 Verilog 모듈로설계한다.gFSM 모델의유지가용이하고, FSM 합성툴의최적화작업에도움이된다.

FSM의상태이름을 로정의하여사용한다FSM의상태이름을 parameter로정의하여사용한다. 컴파일러지시어인 ‘define을이용하여상태이름을정의할수도있으나

‘define은컴파일과정에서광역적으로영향을미치므로, 다수의 FSM에서동일한상태이름이사용되는경우에오류가발생된다. parameter는국부적인영향을미치므로다른 FSM에영향을미치지않는다.

FSM이비동기리셋을갖도록설계한다. 리셋을갖지않는 FSM은초기전원인가후, 상태레지스터의초기값이불확정적이므로정의되지않은상태에갇혀서빠져나오지못하는상황이

발생할수있다.

133한국기술교육대학교 정보기술공학부

Page 134: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5 유한상태머신(FSM)유한상태머신 (FSM) 의코딩가이드라인

그림 11.28의 FSM을구성하는 3개의블록(next state logic, state ( g ,register, output logic)을분리된 always 블록또는 assign 문으로구현한다.

FSM 코딩의일관성을좋게하고, 수정및변경을용이하게한다.

상태레지스터는래치보다는플립플롭을사용하는것이좋다.상태레지스터는래치보다는플립플롭을사용하는것이좋다. 래치를사용하는경우, 래치가 transparent 상태일때상태발진(state oscillation)을일으킬수있다.

Next state logic은 case 문을이용하여모델링하는것이바람직하며, FSM에서정의되지않은상태들은 default문으로표현한다.FSM에서정의되지않은상태들은 default 문 현한다.

134한국기술교육대학교 정보기술공학부

Page 135: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5.1 Moore FSM4개의상태를갖는 FSM

135한국기술교육대학교 정보기술공학부

Page 136: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5.1 Moore FSMmodule fsm_ex1(clk, rst_n, go, ws, rd, ds);input clk, rst_n, go, ws;output rd, ds;

assign 문으로출력을생성하는경우output rd, ds;

parameter IDLE = 2'b00, READ = 2'b01,DLY = 2'b10, DONE = 2'b11;

reg [1:0] state, next;

always @(posedge clk or negedge rst n) //State Registeralways @(posedge clk or negedge rst_n) //State Registerif(!rst_n) state <= IDLE;else state <= next;

always @(state or go or ws) begin //Next State Logic2'bnext = 2'bx;

case(state)IDLE : if(go) next = READ;

else next = IDLE;READ : next = DLY;DLY : if(!ws) next = DONE;

else next = READ;DONE : next = IDLE;

endcaseendend

// Output Logicassign rd =((state==READ)||(state==DLY));assign ds =(state==DONE);

endmodule 코드 11 28(a)

136

endmodule 코드 11.28(a)

한국기술교육대학교 정보기술공학부

Page 137: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5.1 Moore FSM

module fsm ex2(clk rst n go ws rd ds);

always 블록에서출력을생성하는경우

module fsm_ex2(clk, rst_n, go, ws, rd, ds);input clk, rst_n, go, ws;output rd, ds;parameter IDLE = 2'b00, READ = 2'b01,

DLY 2'b10 DONE 2'b11DLY = 2'b10, DONE = 2'b11;reg [1:0] state, next;reg rd, ds;

l @( d lk d t ) //St t R i talways @(posedge clk or negedge rst_n) //State Registerif(!rst_n) state <= IDLE;else state <= next; 코드 11.28(b)

137한국기술교육대학교 정보기술공학부

Page 138: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5.1 Moore FSM

//Next State and Output Logicl @( t t ) b ialways @(state or go or ws) beginnext = 2'bx; rd = 1'b0; ds = 1'b0;case(state)

IDLE : if(go) next = READ;else next = IDLE;else next = IDLE;

READ : begin rd = 1'b1;next = DLY;

endDLY : begin

rd = 1'b1;if(!ws) next = DONE;else next = READ;

endDONE : begin

ds = 1'b1;next = IDLE;dend

endcaseend

endmodule 코드 11.28(b)

138한국기술교육대학교 정보기술공학부

Page 139: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5.1 Moore FSMmodule fsm_ex3(clk, rst_n, go, ws, rd, ds);input clk, rst_n, go, ws;

t t d d

레지스터를통해출력을생성하는경우output rd, ds;

// 상태 선언parameter IDLE = 2'b00, READ = 2'b01,

DLY = 2'b10, DONE = 2'b11;reg [1:0] state next;reg [1:0] state, next;reg rd, ds;

always @(posedge clk or negedge rst_n) //State Registerif(!rst n) state <= IDLE;( _ ) ;else state <= next;

always @(state or go or ws) begin // Next State Logicnext = 2'bx;case(state)

IDLE : if(go) next = READ;else next = IDLE;

READ : next = DLY;DLY if(! ) t DONEDLY : if(!ws) next = DONE;

else next = READ;DONE : next = IDLE;

endcaseend 코드 11 28(c)

139

end 코드 11.28(c)

한국기술교육대학교 정보기술공학부

Page 140: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5.1 Moore FSM

//Output Logic and output registeralways @(posedge clk or negedge rst_n)if(!rst_n) begin

ds <= 1'b0;ds <= 1 b0;rd <= 1'b0;

endelse begin

ds < 1'b0ds <= 1'b0;rd <= 1'b0;case(next)

READ: rd <= 1'b1;DLY : rd <= 1'b1;DONE: ds <= 1'b1;

endcaseend

endmodule 코드 11.28(c)

140한국기술교육대학교 정보기술공학부

Page 141: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5.1 Moore FSM

아래의상태천이도를갖는 Moore FSM회로를설계하고,시뮬레이션을통해아래의상태천이도를갖는 Moore FSM 회로를설계하고, 시뮬레이션을통해동작을확인한다.

out=0 out=1

ST1ST0reset

bypass

ST3 ST2 out=2out=3

설계과제 11.17에서설계된Moore FSM의출력이레지스터를통해생성되도록설계하고시뮬레이션을통해동작을확인한다

141

설계하고시뮬레이션을통해동작을확인한다.

한국기술교육대학교 정보기술공학부

Page 142: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5.2 Mealy FSM연속된 0 또는 1 입력을검출

0 1 0 1 1 1 0 1 0 0 1 1 0 1 0 0

0 0 0 0 1 1 0 0 0 1 0 1 0 0 0 1

din_bit :

dout bit : 0 0 0 0 1 1 0 0 0 1 0 1 0 0 0 1dout_bit :

startreset

1/00/0

rd1_oncerd0_once1/0

0/0

0/0 1/0

rd0_twice rd1_twice

1/00/0 1/0

0/01/10/1

142한국기술교육대학교 정보기술공학부

Page 143: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5.2 Mealy FSM

module seq_det_mealy(clk, rst, din_bit, dout_bit);input clk, rst, din_bit;output dout_bit;reg [2:0] state_reg, next_state;

// 상태 선언parameter start = 3'b000;parameter rd0_once = 3'b001;parameter rd1 once = 3'b010;parameter rd1_once 3 b010;parameter rd0_twice = 3'b011;parameter rd1_twice = 3'b100;

코드 11.29

143한국기술교육대학교 정보기술공학부

Page 144: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5.2 Mealy FSM//Next State Logicalways @(state reg or din bit) beginalways @(state_reg or din_bit) begincase(state_reg)start : if (din_bit == 0) next_state <= rd0_once;

else if(din_bit == 1) next_state <= rd1_once;else next state <= start;else next_state <= start;

rd0_once : if(din_bit == 0) next_state <= rd0_twice; else if(din_bit == 1) next_state <= rd1_once;else next_state <= start;

rd0 t ice if(din bit 0) ne t state < rd0 t icerd0_twice : if(din_bit == 0) next_state <= rd0_twice; else if(din_bit == 1) next_state <= rd1_once;else next_state <= start;

rd1_once : if(din_bit == 0) next_state <= rd0_once; else if(din_bit == 1) next_state <= rd1_twice;else next_state <= start;

rd1_twice : if(din_bit == 0) next_state <= rd0_once; else if(din_bit == 1) next_state <= rd1_twice;else next_state <= start;

default : next_state <= start;endcaseend 코드 11.29

144한국기술교육대학교 정보기술공학부

Page 145: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5.2 Mealy FSM

//State Registeralways @(posedge clk or posedge rst) begin

if(rst == 1) state_reg <= start;else state_reg <= next_state;

endend

//Output Logicassign dout_bit =(((state_reg == rd0_twice) &&(din_bit == 0) ||

(state_reg == rd1_twice) &&(din_bit == 1))) ? 1 : 0;

endmodule 코드 11.29

145한국기술교육대학교 정보기술공학부

Page 146: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5.2 Mealy FSM

코드 11.29의시뮬레이션결과

146한국기술교육대학교 정보기술공학부

Page 147: 10 10. 조합논리회로모델링 - cms3.koreatech.ac.krcms3.koreatech.ac.kr/sites/yjjang/down/dsys10/M03_VerilogHDL03.pdf · 조합논리회로모델링 조합논리회로모델링시유의사항

11.5.2 Mealy FSM

0 1 0 1 1 0 0 1 0 0 1 1 0 1 1 0

0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1

din_bit :

detect_out :

start

1/0

reset

_

0/0

0/00/0

st1st4

1/0

0/0

0/1 1/00/0

st3 st2

1/00/1 0/0

1/0

147한국기술교육대학교 정보기술공학부