Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Mayukhmali Das
Published © Apache-2.0

Serial Communication using Verilog

Designing serial communication used by PS/2 mouse, UART, RS232 etc. using Verilog # Retrotech

IntermediateWork in progress1 hour488
Serial Communication using Verilog

Things used in this project

Story

Read more

Schematics

PS/2 Mouse Data packets

Code

PS2 mouse protocol parser with data path.v

Verilog
module top_module(
    input clk,
    input [7:0] in,
    input reset,    // Synchronous reset
    output [23:0] out_bytes,
    output done); //
    
    // FSM from fsm_ps2
    reg [2:0] state,next_state;
    reg [23:0] temp;
    parameter BYTE1=0,BYTE2=1,BYTE3=2,DONE=4;
    
    // State transition logic (combinational)
   always@(*)begin
        case(state)
            BYTE1: next_state=(in[3])? BYTE2: BYTE1;
            BYTE2: next_state= BYTE3;
            BYTE3: next_state= DONE;
            DONE:  next_state= (in[3])? BYTE2: BYTE1;
        endcase
    end         
    
    // State flip-flops (sequential)
    always@(posedge clk)begin
        if(reset)
            state<=BYTE1;
        else
            state<=next_state;
    end
    
    always@(posedge clk)begin
        case(state)
            BYTE1: temp[23:16]=in;
            BYTE2: temp[15:8]=in;
            BYTE3: temp[7:0]=in;
            DONE : temp[23:16]=in;
        endcase
    end
    
    // Output logic
    assign done =(state==DONE);
    
    // New: Datapath to store incoming bytes.
    assign out_bytes= temp & {24{done}};

endmodule

PS2 mouse protocol parser.v

Verilog
module top_module(
    input clk,
    input [7:0] in,
    input reset,    // Synchronous reset
    output done); //
    
    reg [2:0] state,next_state;
    parameter BYTE1=0,BYTE2=1,BYTE3=2,DONE=4;
    
    // State transition logic (combinational)
    always@(*)begin
        case(state)
            BYTE1: next_state=(in[3])? BYTE2: BYTE1;
            BYTE2: next_state= BYTE3;
            BYTE3: next_state= DONE;
            DONE:  next_state= (in[3])? BYTE2: BYTE1;
        endcase
    end       
    
    // State flip-flops (sequential)
    always@(posedge clk)begin
        if(reset)
            state<=BYTE1;
        else
            state<=next_state;
    end
    
    // Output logic
    assign done =(state==DONE);
    
endmodule

Serial Receiver with start stop and parity checking.v

Verilog
module top_module(
    input clk,
    input in,
    input reset,    
    output [7:0] out_byte,
    output done
); 
    reg [7:0] mayukh;
    reg [3:0] state,next_state; 

    parameter start = 4'h0;
    parameter data1 = 4'h1;
    parameter data2 = 4'h2;
    parameter data3= 4'h3;
    parameter data4= 4'h4;
    parameter data5= 4'h5;
    parameter data6= 4'h6;
    parameter data7= 4'h7;
    parameter data8 = 4'h8;
    parameter stopmiss = 4'd9;
    parameter faultydone = 4'hA;
    parameter stop = 4'hB;
    parameter yay = 4'hC;
    parameter parity =4'hD;
    parameter noyay = 4'hE;
    parameter parityfault =4'hF;
    
    
    always@(*)begin
        case(state)    
            
            start:     next_state=(in)?start:data1;
            data1:     next_state=data2;
            data2:     next_state=data3;
            data3:     next_state=data4;
            data4:     next_state=data5;
            data5:     next_state=data6;
            data6:     next_state=data7;
            data7:     next_state=data8;
            data8:     next_state=parity;

            parity:    next_state=((^mayukh)^in)?stop:parityfault;
            
            
            
            parityfault: next_state=(in)?noyay:stopmiss;
            stop:      next_state=(in)?yay:stopmiss;
            
            
            yay:         next_state=(in)?start:data1;
            noyay:       next_state=(in)?start:data1;
            

            stopmiss:  next_state=(in)?faultydone:stopmiss;
            faultydone:next_state=(in)?start:data1;
            
        endcase
    end
    
    always@(posedge clk)
        begin
            
            if(reset)
                
                state<=start;

            else
                state<=next_state;
        end
    
    assign  done= (state==yay);
    
    
    always@(posedge clk)begin
        if(reset || state==start)
            mayukh<=8'h0;
        else if(next_state!=stop &&state!=stop)
            mayukh<={in,mayukh[7:1]};
    end
    assign out_byte=mayukh &({8{done}});

endmodule

Credits

Mayukhmali Das
9 projects • 16 followers
Electronics, Communication, Artificial Intelligence, Optimization
Contact

Comments

Please log in or sign up to comment.