본문 바로가기

Verilog/HDLbits

[HDLBits] Fsm serialdata

1. 문제 및 설명

https://idkihg.tistory.com/160

 

[HDLBits] Fsm serial

1. 문제 및 설명 A. 많은 직렬 통신 프로토콜에서는 데이터 바이트의 경계를 구분하기 위해 시작 비트(start bit)와 정지 비트(stop bit)를 함께 전송한다. B. 일반적인 프레임 구조는 다음과 같다.시작

idkihg.tistory.com

 

  위의 문제에서 비트 스트림에서 바이트가 올바르게 수신되었는지 판별할 수 있는 유한 상태 기계를 구현했으므로, 정상적으로 수신된 데이터 를 출력하는 데이터 패스를 추가하라. done 신호가 1일 때 out_byte 값은 유효해야 하며, 그 외의 경우에는 어떤 값이 출력되어도 상관없다. 직렬 프로토콜을 LSB임을 유의하라.

 

 

2. Waveform

 

 

3. 모듈 정의

module top_module(
    input clk,
    input in,
    input reset,    // Synchronous reset
    output [7:0] out_byte,
    output done
);

 

 

4. 답

module top_module(
    input clk,
    input in,
    input reset,    // Synchronous reset
    output [7:0] out_byte,
    output done
); //

    
    parameter idle = 3'b000, data = 3'b001, stop = 3'b010, start = 3'b011, Error = 3'b100;
    
    reg [2:0] state, next_state;
    reg [3:0] cnt;
    reg [7:0] out;
    
    assign out_byte = out;
    assign done = (state == stop);
    
    always@(*)
        begin
            case(state)
                idle: next_state = !in?start:idle;
                start: next_state = data;
                data: next_state = (cnt==8)?(in?stop:Error):data;
                Error: next_state = in?idle:Error;
                stop: next_state = !in?start:idle;
                default next_state = state;               
            endcase
        end
    
    always@(posedge clk)
        begin
            if(reset)
                begin
                	state <= idle;
                    cnt <= 4'd0;
                    out <= 8'd0;

                end
            else
                begin
                	state <= next_state;
                    if(next_state == data)
                        begin
                        	cnt <= cnt + 4'd1;
                            out <= {in, out[7:1]};
                        end
                    else
                        begin
                        	cnt <= 4'd0;
                        end

                end
        end

endmodule

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

HDLbits[HDLBits] Fsm hdlc  (0) 2026.05.10
[HDLBits] Fsm serialdp  (0) 2026.05.06
[HDLBits] Fsm serial  (0) 2026.04.26
[HDLBits] Fsm ps2data  (0) 2026.04.20
[HDLBits] Fsm ps2  (2) 2026.04.20