FPGA/HDLBits

Module shift8

장영현 2023. 6. 10. 17:56
728x90

Problem Statement

You are given a module my_dff8 with two inputs and one output (that implements a set of 8 D flip-flops). Instantiate three of them, then chain them together to make a 8-bit wide shift register of length 3. In addition, create a 4-to-1 multiplexer (not provided) that chooses what to output depending on sel[1:0]: The value at the input d, after the first, after the second, or after the third D flip-flop. (Essentially, sel selects how many cycles to delay the input, from zero to three clock cycles.)

module top_module ( 
    input clk, 
    input [7:0] d, 
    input [1:0] sel, 
    output [7:0] q 
);
    wire [7:0] wire1, wire2, wire3;
    my_dff8 dff1 (clk, d, wire1);
    my_dff8 dff2 (clk, wire1, wire2);
    my_dff8 dff3 (clk, wire2, wire3);
    
    reg[7:0] sel_out;
    always @ (*) begin // always(*)에서 *는 모든 입력이 포함된 것을 의미 -> 입력이 변경될 때 마다 항상 변경 
        case (sel)
            2'b00 : sel_out = d;
            2'b01 : sel_out = wire1;
            2'b10 : sel_out = wire2;
            2'b11 : sel_out = wire3;
        endcase
     	end
    	
    assign q = sel_out;
    
endmodule

 

 


  • D 플립플롭의 인스턴스(객체)를 생성할 때 인스턴스를 생성하기 위한 하위 모듈의 포트설정과 동일하게 포트들을 설정해줘야 한다. 

 

문제에서 제공하는 하위 모듈 = module my_dff8 ( input clk, input [7:0] d, output [7:0] q );

 

모듈 설정 

  1. my_dff8 인스턴스1 (clk, 입력 포트1, 출력포트1);
  2. my_dff8 인스턴스2 (clk, 출력포트1, 출력포트2);
  3. my_dff8 인스턴스3 (clk, 출력포트2, 출력포트3);

해당 인스턴스로 만들게 되면 각 포트들이 입, 출력으로 연결되어 있어 인스턴스1 -> 인스턴스2 -> 인스턴스3으로 이어지게 된다. 

 

  • 문제에서는 4x1 멀티플렉서를 사용하므로 선택 포트 sel 관련 코드를 적을 때, 4x1 멀티플렉서의 진리표를 보며 작성하면 된다.

4x1 멀티플렉서 진리표