Case문

SungchulCHA·2024년 6월 26일

Verilog

목록 보기
2/8

if statement

always @(A, B, Use_B)
  begin
    if (Use_B)
      begin
        D_Out = B;
      end
    else
      begin
        D_Out = A;
      end
  end
always @(A, B, Use_B)
  begin
    D_Out = A;
    if (Use_B)
      begin
        D_Out = B;
      end
  end
D_Out = Use_B ? B : A;

Mux로 생성될 수도 있고, And, Not, Or gate들을 이용한 logic gate들이 나올 수도 있음.


Don't Care Comparision Caveat

if (In_A[7:0] == 8'b00xx11xx)
  begin
    Data_Out = 1'b1;
  end
else
  begin
    Data_Out = 1'b0;
  end

simulation 입장에서 1'bx는 항상 false다.

if ((In_A[7:6] == 2'b00) && (In_A[3:2] == 2'b11))
  begin
    Data_Out = 1'b1;
  end
else
  begin
    Data_Out = 1'b0;
  end

don't care를 써야 한다면 이렇게 나눠서 쓰자.


Priority

  • if statements 에서는 bottom 부터 top 순서로 priority가 높음

  • if else if statements 에서는 top 부터 bottom 순서로 priority가 높음

  • case expression에서는 top 부터 bottom 순서로 priority가 높음


Directive

  • full_case
case (Sel) //synopsys full_case
  2'b00: D_Out = A;
  2'b01: D_Out = B;
  2'b10: D_Out = C;
endcase

Latch가 생성 안됨.
pre-synthesis와 post-synthesis 사이에 mismatch 발생

  • parallel_case
case (1'b1) // synopsys parallel_case
  Sel_A   : Data_Out = In_A;
  Sel_B   : Data_Out = In_B;
  Sel_C   : Data_Out = In_C;
  default : Data_Out = In_D;
endcase

이것 또한 pre-synthesis와 post-synthesis 사이에 mismatch 발생

  • infer_mux
case (Sel) // synopsys infer_mux
  3'b000: Data_Out = Data_In[0];
  3'b001: Data_Out = Data_In[1];
  3'b010: Data_Out = Data_In[2];
  3'b011: Data_Out = Data_In[3];
  3'b100: Data_Out = Data_In[4];
  3'b101: Data_Out = Data_In[5];
  3'b110: Data_Out = Data_In[6];
  3'b111: Data_Out = Data_In[7];
endcase

synthesis algorithms 이 multiplexers 선택하게 지시

// synopsys infer_mux "My_Block_Label"
always @ (*)
begin: My_Block_Label
  case (Sel_1)
    ...
  endcase
  
  case (Sel_2)
    ...
  endcase
  
end

label이 되어 있는 always block 내의 모든 case statements에 대해 mux 생성
After the case expression, but before the first case item
On the line preceding ad always statement when the block labeling method is used
회로 상 mux가 더 안좋을 수 있으니 남용은 지양하자.
if statements에서도 사용 가능하다.


  • casex와 casez 는 합성 불가능

  • for loop

input [3:0] In_A;
output [3:0] Out_B;

integer K;

always @ (In_A)
begin

  for (K = 3; K >= 0; K = K - 1)
    begin
      Out_B[K] = In_A[3 - K];
    end
    
end

loop parameter, K 가 각 for loop의 iteration에 대해 상수이므로, subtracters 합성 안됨
default value for hdlin_while_loop_iteration : 1000
elaborate synthesis 전에 바꿔야함.

begin: LOOP_BLOCK
  for (k = 0; K < 115; K = K + 1)
    begin
      if (In_A[K])
        begin
          // loop body
        end
      else
        begin
          disable LOOP_BLOCK;
        end
    end
end
  • for loop 안하는 technique
    2-to-4 decoder
input [1:0] In_2;
output reg [3:0] Out_4;
integer K;

always @(In_2)
begin

  for (K = 0; K <= 3; K = K + 1)
    begin
    
      if (In_2 == K)
        begin
          Out_4[K] = 1'b1;
        end
      else
        begin
          Out_4[K] = 1'b0;
        end
    end
end
input [1:0] In_2;
output reg [3:0] Out_4;

always @(In_2)
begin

  Out_4 = 4'b0;
  Out_4[In_2] = 1'b1;
  
end
  • functions and tasks
module Bits_Reversed_Module(input [127:0] In_A,
                            output [127:0] Out_B);

  function [127:0] Bits_Reversed (input [127:0] Func_Input);
    integer K;
    begin
      for (K = 127; K >= 0; K = K - 1)
        begin
          Bits_Reversed[K] = Func_Input[127 - K];
        end
    end
  endfunction
  
  assign Out_B = Bits_Reversed(In_A);
                            
endmodule
  • tristate gate
assign D_OUt = (Use_B) ? B : 1'bz;
always @(B, Use_B)
begin
  if (Use_B)
    begin
      D_Out = B;
    end
  else
    begin
      D_Out = 1'bz;
    end
end

Conditional assignment of 1'bz implies the synthesis of a tristate gate.

  • multiple tristate gates
assign D_Out = (Use_B) ? B : 1'bz;
assign D_Out = (!Use_A_N) ? A : 1'bz;

inout pin 만들 때,
concurrent error 안남
always statements는 사용하면 안됨
simulation/synthesis mismatch 발생

  • bidirectional ports
module Bidi_Module (Bidi_Port, Direction_In, Direction_Out,
                    Data_Received, Data_To_Send);

  inout Bidi_Port;
  input Direction_In, Direction_Out, Data_To_Send;
  output Data_Received;
  
  assign Bidi_Port = Direction_Out ? Data_To_Send : 1'bz;
  
  assign Data_Received = Direction_In ? Bidi_Port : 1'bz;
  
endmodule

tristate buffer가 2개

module Bidi_Module (Bidi_Port, Direction_Out,
                    Data_Received, Data_To_Send);

  inout Bidi_Port;
  input Direction_Out, Data_To_Send;
  output Data_Received;
  
  assign Bidi_Port = Direction_Out ? Data_To_Send : 1'bz;
  
  assign Data_Received = Bidi_Port;
  
endmodule

tristate buffer가 1개

profile
Myongji UNIV. B.S. in Electronic Engineering

0개의 댓글