ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Verilog][기초문법] 조건문, 분기문
    개발/Verilog 2022. 1. 8. 22:28

    C 언어 처럼 if, case 구문 쓸 수 있다

    다만 베릴로그에서 데이터는 x, z 값이 있기때문에 조건문을 작성할때 이것을 유의해야한다.

    기본적으로 1 이 아닌 값은 모두 false로 판단한다.

     

    module tb();
      
      reg a;
      
      initial begin
        a = 1'bx;
        $display("a is %b",a);
        if (a) $display("x is true");
        else $display("a is false");
        
        
        a = 1'bz;
        $display("a is %b",a);
        if (a) $display("z is true");
        else $display("z is false");
      end
      
    endmodule

    위 코드를 실행하면 아래의 결과가 나온다.

     

    EDA playground 실행 결과

     

     

     

     

     

     

     

    if 문을 쓰기에는 분기가 너무 많으면 case 구문을 쓴다.

    case 문 안에 분기조건에 해당하는 item들을 기술하면 된다.

    module tb();
      
      reg a;
      
      initial begin
        
    	a = 1'b0;
        test(a);
        
        a = 1'b1;
        test(a);
        
        a = 1'bx;
        test(a);
        
        a = 1'bz;
        test(a);
        
        
      end
      
      
      function test(reg x);
      	case (x)
          1'b0 : $display("x is 0");
          1'b1 : $display("x is 1");
          1'bx : $display("x is x");
          1'bz : $display("x is z");
          default : $display("default");
        endcase
      endfunction
      
    endmodule

    코드를 실행하면 결과는 다음과 같다.

     

    a 값이 0, 1, x, z 값 모두 비교를 한다.

    케이스문은 맨 처음에 일치하는것이 실행된다. 일치하는 것이 없다면 default 구문이 실행된다.

    코딩을 할 때, default 문장은 넣는것이 좋다.

     

    예제에선 1비트 변수를 갖고 실행했다. 만약에 변수와 case 구문 수식의 비트폭이 일치하지 않으면 비트가 더 큰 쪽에 맞춰서 0으로 채워넣는다.

     

     

     

    module tb();
      
      reg a;
      
      initial begin
        
        a = 1'b0;
        test(a);
        
        a = 1'b1;
        test(a);
        
        a = 1'bx;
        test(a);
        
        a = 1'bz;
        test(a);
      end
      
      
      function test(reg x);
      	case (x)
          1'b0, 1'b1 : $display("x is 0 or 1");
          1'bx : $display("x is x");
          1'bz : $display("x is z");
          default : $display("default");
        endcase
      endfunction
      
    endmodule

    case item 여러개를 넣을 수 있다. item 끼리는 콤마로 구분한다.

     

     

     

    casex, casez 구문도 있다.

    casez 구문은 z 값을 don`t care 취급하고, x 값은 구분한다.

     

    module tb();
      
      reg [3:0] a;
      
      initial begin
        
        a = 4'b1zzz;
        test(a);
        
        a = 4'bz1zz;
        test(a);
        
        a = 4'bzz1z;
        test(a);
        
        a = 4'b000z;
        test(a);
        
        a = 4'b10zz;
        test(a);
        
        a = 4'b01z1;
        test(a);
        
        a = 4'b1zzx;
        test(a);
        
        a = 4'b0z1x;
        test(a);
      end
      
      
      function test(reg [3:0] x);
      	casez (x)
          4'b1zzz : $display("x is 1zzz");
          4'bz1zz : $display("x is z1zz");
          4'bzz1z : $display("x is zz1z");
          4'bzzz1 : $display("x is zzz1");
          default : $display("default");
        endcase
      endfunction
      
    endmodule

    위의 코드를 실행한 결과.

    결과를 비교해보면 이해하기 쉬울것이다

     

     

     

    casex 는 x, z 값 둘 다 don`t care 취급한다.

    module tb();
      
      reg [3:0] a;
      
      initial begin
        
        a = 4'b1zzz;
        test(a);
        
        a = 4'bz1zz;
        test(a);
        
        a = 4'bzz1z;
        test(a);
        
        a = 4'b000z;
        test(a);
        
        a = 4'b10zz;
        test(a);
        
        a = 4'b01z1;
        test(a);
        
        a = 4'b1zzx;
        test(a);
        
        a = 4'b0z1x;
        test(a);
      end
      
      
      function test(reg [3:0] x);
      	casex (x)
          4'b1xzz : $display("x is 1zzz");
          4'bx1zz : $display("x is z1zz");
          4'bzx1z : $display("x is zz1z");
          4'bzxx1 : $display("x is zzz1");
          default : $display("default");
        endcase
      endfunction
      
    endmodule

    코드 실행 결과

     

     

Designed by Tistory.