거북이처럼 천천히

4 X 2 Encoder / 2 X 4 Decoder 본문

Verilog/Verilog 연습

4 X 2 Encoder / 2 X 4 Decoder

유로 청년 2024. 7. 1. 19:45

1. Encoder

  • Encoder는 2^n bit 크기를 갖는 데이터를 받아서 n bit 크기를 갖는 데이터를 출력으로 내보내는 논리 회로 및 장치이다.
  • Encoder는 주로 다음과 같은 작업에 사용된다.
    - 데이터 전송 효율을 높이기 위한 데이터 압축
    - 데이터 보안을 위한 데이터 암호화
    - 다양한 센서 신호들을 디지털 신호로 변환하는 데 사용
  • Encoder와 Decoder의 진리표는 다음과 같다.

(왼) Encoder의 진리표, (오른) Decoder의 진리표.

 

 

 

2. Decoder

  • Decoder는 부호화된 n bit 데이터를 입력받아 2^n bit 크기의 데이터를 출력으로 내보낸다.
  • Encoder와 Decoder는 한 쌍의 짝을 이루어 사전에 약속된 진리표에 의해 encoding과 decoding을 할 수 있는 것이다.

 

 

 

 

 


 

 

 

 

 

 

 

1.1. Behavioral Modeling of Encoder (case)

< Source >

module Encoder_4x2_Behavioral_Modeling_case(
    input [3:0] signal,
    output reg [1:0] data);
    
    always @(signal) begin
        case(signal) 
            4'b0001 : data = 2'b00;
            4'b0010 : data = 2'b01;
            4'b0100 : data = 2'b10;
            4'b1000 : data = 2'b11;
            default : data = 2'b00;
        endcase
    end
    
endmodule

 

< Simulation >

 

< RTL Analysis >

 

< synthesis >

 

 

 

 

 

 

 

1.2. Behavioral Modeling of Encoder (if - else)

< Source >

// Behavioral Modeling of 4 x 2 Encoder (if-else)
module Encoder_4x2_Behavioral_Modeling_if_ease(
    input [3:0] signal,
    output reg [1:0] data);
    
    always @(signal) begin
        if(signal == 4'b0001) data = 2'b00;
        else if(signal == 4'b0010) data = 2'b01;
        else if(signal == 4'b0100) data = 2'b10;
        else if(signal == 4'b1000) data = 2'b11;
        else data = 2'b00;
    end
    
endmodule

 

< Simulation >

 

< RTL Analysis >

 

 

< synthesis >

 

 

 

 

 

 

 

 

1.3. Structural Modeling of Encoder

Gate Circuit of 4 X 2 Encoder

 

 

< Source >

// Behavioral Modeling of or gate.
module or_gate (
    input a, b,
    output reg out);
    
    always @(a, b) begin
        case({a, b}) 
            2'b00 : out = 0;
            2'b01 : out = 1;
            2'b10 : out = 1;
            2'b11 : out = 1;
        endcase
    end
endmodule

// Structural Modeling of 4X2 Encoder
module Encoder_4x2_Structural_Modeling(
    input [3:0] signal,
    output [1:0] data);
    
    or_gate or0 (signal[1], signal[3], data[0]);
    or_gate or1 (signal[2], signal[3], data[1]);
    
endmodule

 

< Simulation >

 

< RTL Analysis >

 

 

< synthesis >

 

 

 

 

 

 

 

1.4. Dataflow Modeling of Encoder

< Source >

// Dataflow Modeling of Encoder
module Encoder_4x2_Dataflow_Modeling(
    input [3:0] signal,
    output [1:0] data );
    
    assign data = (signal == 4'b0001)?  2'b00 : 
                  ((signal == 4'b0010)? 2'b01 : 
                  ((signal == 4'b0100)? 2'b10 :
                  ((signal == 4'b1000)? 2'b11 : 2'b00)));
    
endmodule

 

< Simulation >

 

< RTL Analysis >

 

< synthesis >

 

 

 

 

 


 

 

 

 

 

2.1. Behavioral Modeling of Decoder (case)

 

< Source >

// Behavioral Modeling of 2 X 4 Decoder.
module Behavioral_Modeling_of_Decoder_case(
    input [1:0] data,
    output reg [3:0] signal );
    
    always @(data) begin
        case(data) 
            2'b00 : signal = 4'b0001;
            2'b01 : signal = 4'b0010;
            2'b10 : signal = 4'b0100;
            2'b11 : signal = 4'b1000;
        endcase
    end
    
endmodule

 

< Simulation >

 

 

< RTL Analysis >

 

 

< synthesis >

 

 

 

2.2. Behavioral Modeling of Decoder (if-else)

 

< Source >

// Behavioral Modeling of 2 X 4 Decoder.
module Behavioral_Modeling_of_Decoder_if_else(
    input [1:0] data,
    output reg [3:0] signal );
    
    always @(data) begin
        if(data == 2'b00) signal = 4'b0001;
        else if(data == 2'b01) signal = 4'b0010;
        else if(data == 2'b10) signal = 4'b0100;
        else if(data == 2'b11) signal = 4'b1000;
        else signal = 4'b0000;
    end
    
endmodule

 

< Simulation >

 

< RTL Analysis >

 

 

< synthesis >

 

 

 

 

 

 

 

2.3. Structural Modeling of Decoder

 

< Source >

// Behavioral Modeling of not gate
module not_gate (
    input a, 
    output reg out);
    
    always @(a) begin
        case(a) 
            1'b0 : out = 1;
            1'b1 : out = 0;
        endcase
    end
endmodule

// Behavioral Modeling of and gate
module and_gate (
    input a, b,
    output reg out );
    
    always @(a, b) begin
        case({a, b}) 
            2'b00 : out = 0;
            2'b01 : out = 0;
            2'b10 : out = 0;
            2'b11 : out = 1;
        endcase
    end    
endmodule

// Structural Modeling of 2 X 4 Decoder.
module Structural_Modeling_of_Decoder(
    input [1:0] data,
    output [3:0] signal );
    
    wire not_data0, not_data1;
    
    not_gate not0 (data[0], not_data0);
    not_gate not1 (data[1], not_data1);
    
    and_gate and0 (not_data0, not_data1, signal[0]);
    and_gate and1 (data[0], not_data1, signal[1]);
    and_gate and2 (not_data0, data[1], signal[2]);
    and_gate and3 (data[0], data[1], signal[3]);
    
endmodule

 

 

< Simulation >

 

< RTL Analysis >

 

 

< synthesis >

 

 

 

 

 

2.4. Dataflow Modeling of Decoder

< Source >

// Dataflow Modeling of Decoder.
module Dataflow_Modeling_of_Decoder(
    input [1:0] data,
    output [3:0] signal);
    
    assign signal = (data == 2'b00)? 4'b0001 : 
                        ((data == 2'b01)? 4'b0010 :
                        ((data == 2'b10)? 4'b0100 : 
                        ((data == 2'b11)? 4'b1000 : 4'b0000)));
    
endmodule

 

< Simulation >

 

< RTL Analysis >

 

 

< synthesis >

'Verilog > Verilog 연습' 카테고리의 다른 글

Module with parameters  (0) 2024.06.30
4 bit Comparator / 32 bit Comparator  (0) 2024.06.30
1 bit Comparator  (0) 2024.06.30
4 bit parallel adder / subtractor  (0) 2024.06.30
4 bit parallel adder  (0) 2024.06.30