전자과 과목/디지털시스템설계

Verilog 기초 문법 #2

가은가은 2023. 3. 24. 01:24

1. circuit modelling

1) structral model

: gate와 component들이 어떻게 객체화 되어 있는지 describe한 것

: 서로 어떻게 연결되어 있는지

 

  • built-in gate

  • module instantiation

-> built-in gate를 사용해서 user-defined module 생성

 

-> user-defined module을 instantiate

-> instantion 순서는 상관없음 -> 모든 인스턴스들은 병렬적으로 실행

 

 

2) dataflow model

  • data의 flow와 operation으로 combinational circuit을 describe함.
    • explicit한 gate대신 bitwise operators 사용
  • continurous-assignment statements

-> assign 선언은 wire로 선언됨

 

-> 모든 assign문들은 병렬적으로 실행

 

 

3) behavior model

: algorithm 같은 procedural한 statements들을 describe

: always block 사용

 

  • always statement
    • An always block always executes
    • 지정한 signal의 변화가 있을 때마다, block안의 procedual-statements들이 sequentially 하게 실행됨

 

  • nonblocking, blocking
    • blocking는 순차적으로 statement가 실행되고 nonblocking은 오른쪽 열 먼저 실행된 후 왼쪽 열 실행 
      • Guide line
        • combinataional logic을 만들기 위해서, always문은 blocking assignments 사용
        • sequential logic을 만들기 위해서, always문은 nonbloking assignments 사용
        • 두 assignments를 같은 always block에서 섞어쓰면 안된다....
        • 다른 always block에서 같은 변수로 assignments 만들면 안된다... 

 

  • Begin-end Block
    • multiple procedure statement를 적고 싶을 때 사용

 

  • if - else if - else statement 
    • inferred Latches

-> output F의 값이 나와야 하는데, 위 block에서 if-else if 조건을 모두 만족하지 못할 경우에 빠질 else문이 없음

 

-> edge-sensitive하게 동작하는 sequential logic이 아닌 combination logic을 설계할 때에는 alway block안에 모든 조건에 대한 값이 제시되어야함

->  output에서 나갈 registor값이 always block 안에서 결정되어야함.

 

*** synthesis report를 확인하면 latch가 생성되었는지 안되었는지 확인 가능

 

  • inferred latches 해결 방안
  • assign default  values
    • if / if-else / case statements 앞에 default values를 assign 해준다.

2. case statement

<syntax>

<example>

module Vrpimecs (N, F);
    input [3:0] N;
    output reg F;
    always@ (N)
    	case(N)
            4'd1, 4'd2, 4'd5, 4'd7, 4'd11, 4'd13 : F = 1;
            default : F = 0; // default를 지정해야 inferred latch 생성 막음
        endcase
    endmodule

 

3. Loop statement

<syntax>

for ( loop-index = first-expr; logical-expression; loop-index = next-expr)
	procedural-statement

<synthesizable for-loop>

for ( loop-index = first; loop-index <= last; loop<-index = loop-index + constant)
    procedural-statement

 

*synthesizable 하려면 반복되는 loop의 횟수가 정해져있어야한다.

참고 : https://electronics.stackexchange.com/questions/350078/question-about-synthesizable-for-loop-and-generate

 

 

<Other loop primitives(mostly unsynthesizable)>

 

 

4. Parameterized Modules

  • parameter로 input, output vector의 width 조절 가능
  • reuse 가능

<Module 선언>

module Maj(OUT, I0, I1, I2);
    parameter WID = 1;
    input [WID-1 : 0] I0, I1, I2;
    output [WID-1 : 0] OUT;
    
    assign OUT = I0 & I1 | I0 & I2 | I1 & I2;
    
endmodule

 

<Module 사용>

Maj #8 U1(.OUT(W), .I0(X), .I1(Y), .I2(Z); // module안에 parameter가 한 개일 경우만 가능
Maj #(.WID(8)) U1(.OUT(W), .I0(X), .I1(Y), .I2(Z);

//overide multiple parameters!
Maj #(.P2(8), .P1(4)) U1(.OUT(W), .I0(X), .I1(Y), .I2(Z);

 

 

5. Function

  • default result type은 single bit
  • returnfunction이름으로 output register 생성
  • global variable은 사용가능하나, task를 call하는 것은 불가
  • combinational logic처럼 synthesized됨

<syntax>

 

<Example>

function [7:0] sum;
    input [7:0] a, b;
    
    sum = a + b;  //function name인 sum으로 return(output register)
endfunction

 

 

6. Task

  • function과 비슷하지만 result를 return하지 않음.
  • global variable drive 가능
  • 거의 대부분 synthesizable하지 않으므로 module에서 사용하는 것을 피할 것..

 

7. Built-in System Tasks and Functions

  • $display : print
  • $monitor : listed signal일 바뀔 때 print
  • $fflush : standarad output 뿐만 아니라 file output을 출력
  • $time :  현재 simulated time 값을 출력한다.
  • $random : pseudo random 32-bit singed integer를 return한다.
    • Q. pseudo random 이란, random값들은 truly 하지 않고 사실 sequential 하게 정해져있음..!
  • $stop : simulation을 잠시 멈추고 제어권을 user에게 준다.

 

 

8. time scale

  • each statement에 time delay를 specify한다.
  • synthesize와 상관없고 단순히 로직을 테스트하기 위한 simulation과정에서 영향을 준다.
  • simulation할 때, 모든 베릴로그 파일에서 time scale은 동일해야 한다.

<time scale 선언>

  • 왼쪽 칸에 시간단위를 적고, 오른 칸에 시간 해상도를 적는다.
  • 해상도의 n배 까지는 허용하지만, 해상도 보다 작은 단위는 허용하지 않는다.

 

<example>

  • timescale 1 ns / 100ps 이므로 예를 들어 2, 2.5까지는 허용하고  2.56부터는 허용하지 않는다.

 

 

 

9. Test Bench

  • module을 검증하기 위해 HDL model에 synthetic input을 apply 하는 module

<example>

아래에서 Vrprimed module은 prime을 계산한다.

-> 10ns 마다 Num, 즉 UUT의 input이 바뀜

-> for loop을 사용하여 input의 모든 경우의 수로 module을 실행

 

**주의할 것 : module내부 코드는 병렬적으로 실행

      -> Num값이 바뀔 때 마다 UUT Module 실행

 

 

  • self-checking test bench
    • case문을 사용하여 모든 input에 대하여 output을 지정
    • module를 거쳐 나온 output이랑 미리 정해놓은 output(case문으로)을 비교하여 틀리면 error 출력

 

  • 알고리즘적으로 expected result를 generate하는 test bench

'전자과 과목 > 디지털시스템설계' 카테고리의 다른 글

Verilog 기초 문법 #1  (0) 2023.03.16