[베릴로그 개인 공부] 조합회로

2023. 6. 27. 16:01FPGA/Verilog

728x90
  • 조합회로

조합논리회로  : 입력 신호의 조합에 따라 출력신호가 결정되는 회로. 이 회로는 입력신호의 조합이 현재 상태에만 영향을 미치고, 이전 상태나 다음 상태와는 관련이 없다. 따라서 출력은 오로지 입력 신호의 조합에 의해 결정된다.

 

  • 조합회로 종류

 

  • 반가산기 (Half Adder) : 두 개의 입력 비트를 받아 합과 자리올림 값을 출력하는 회로.
module HalfAdder(A, B, Sum, Carry);
  input A, B;
  output Sum, Carry;
  
  assign Sum = A ^ B;  // XOR 연산: 입력 비트의 합을 구합니다.
  assign Carry = A & B;  // AND 연산: 자리올림 값을 구합니다.
endmodule

 

  • 전가산기 (Full Adder) : 두 개의 입력 비트와 자리올림 입력을 받아 합과 자리올림 값을 출력하는 회로.
module FullAdder(A, B, CarryIn, Sum, CarryOut);
  input A, B, CarryIn;
  output Sum, CarryOut;
  
  wire C1, C2;
  
  HalfAdder HA1(A, B, S1, C1);
  HalfAdder HA2(S1, CarryIn, Sum, C2);
  assign CarryOut = C1 | C2;  // OR 연산: 두 개의 반가산기로부터 얻은 자리올림 값을 합칩니다.
endmodule

module HalfAdder(A, B, Sum, Carry);
  input A, B;
  output Sum, Carry;
  
  assign Sum = A ^ B;  // XOR 연산: 입력 비트의 합을 구합니다.
  assign Carry = A & B;  // AND 연산: 자리올림 값을 구합니다.
endmodule

 

  • 디코더 (Decoder) : n의 입력을 받아 n^2출력을 하는 회로.(다수의 입력 신호를 받아 각 입력에 해당하는 출력을 생성하는 회로) / 밑의 코드는 2X4 디코더를 베릴로그 코드로 작성
//case문을 사용한 디코더
module Decoder_2x4_case(input [1:0] in, output reg [3:0] out);
  always @(*)
  begin
    case (in)
      2'b00: out = 4'b0001;
      2'b01: out = 4'b0010;
      2'b10: out = 4'b0100;
      2'b11: out = 4'b1000;
      default: out = 4'b0000;
    endcase
  end
endmodule
//if-else문을 사용한 디코더
module Decoder_2x4_if_else(input [1:0] in, output reg [3:0] out);
  always @(*)
  begin
    if (in == 2'b00)
      out = 4'b0001;
    else if (in == 2'b01)
      out = 4'b0010;
    else if (in == 2'b10)
      out = 4'b0100;
    else if (in == 2'b11)
      out = 4'b1000;
    else
      out = 4'b0000;
  end
endmodule
//for문을 사용한 디코더
verilog
Copy code
module Decoder_2x4_for(input [1:0] in, output reg [3:0] out);
  always @(*)
  begin
    reg [1:0] i;
    out = 4'b0000;
    for (i = 0; i < 4; i = i + 1)
    begin
      if (in == i)
        out[i] = 1'b1;
    end
  end
endmodule

 

  • 인코더 (Encoder) : 디코더와 반대 개념. n^2의 입력을 받아 n을 출력하는 회로. / 밑의 코드는 2X4 인코더를 베릴로그 코드로 작성
//case문을 사용한 인코더
module Encoder_4x2_case(input [3:0] in, output reg [1:0] out);
  always @(*)
  begin
    case (in)
      4'b0001: out = 2'b00;
      4'b0010: out = 2'b01;
      4'b0100: out = 2'b10;
      4'b1000: out = 2'b11;
      default: out = 2'b00;
    endcase
  end
endmodule
//if-else문을 사용한 인코더
module Encoder_4x2_if_else(input [3:0] in, output reg [1:0] out);
  always @(*)
  begin
    if (in == 4'b0001)
      out = 2'b00;
    else if (in == 4'b0010)
      out = 2'b01;
    else if (in == 4'b0100)
      out = 2'b10;
    else if (in == 4'b1000)
      out = 2'b11;
    else
      out = 2'b00;
  end
endmodule
//for문을 사용한 인코더
module Encoder_4x2_for(input [3:0] in, output reg [1:0] out);
  always @(*)
  begin
    reg [3:0] i;
    out = 2'b00;
    for (i = 0; i < 4; i = i + 1)
    begin
      if (in[i])
        out = i;
    end
  end
endmodule

 

  • 멀티플렉서 (Multiplexer)
module Mux_4to1(input [3:0] in, input [1:0] sel, output reg out);
  always @(*)
  begin
    case (sel)
      2'b00: out = in[0];
      2'b01: out = in[1];
      2'b10: out = in[2];
      2'b11: out = in[3];
      default: out = in[0];
    endcase
  end
endmodule

 

  • 디멀티플렉서(Demultiplexer)
module Demux_1to4(input in, input [1:0] sel, output reg [3:0] out);
  always @(*)
  begin
    case (sel)
      2'b00: out = 4'b0001;
      2'b01: out = 4'b0010;
      2'b10: out = 4'b0100;
      2'b11: out = 4'b1000;
      default: out = 4'b0000;
    endcase
  end
endmodule

 

'FPGA > Verilog' 카테고리의 다른 글

[베릴로그 개인 공부] generate문  (0) 2023.06.26
Finite State Machine (유한 상태 머신)  (0) 2023.06.14
Xilinx Board  (0) 2023.06.14
Verilog Simulation 2  (0) 2023.06.14
Verilog Simulation 1  (0) 2023.06.14