OpenCores
URL https://opencores.org/ocsvn/s80186/s80186/trunk

Subversion Repositories s80186

[/] [s80186/] [trunk/] [fpga/] [uart/] [Receiver.sv] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jamieiles
// Copyright Jamie Iles, 2017
2
//
3
// This file is part of s80x86.
4
//
5
// s80x86 is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// s80x86 is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with s80x86.  If not, see .
17
 
18
module Receiver(input logic clk,
19
                input logic reset,
20
                input logic clken,
21
                input logic rx,
22
                output logic rdy,
23
                input logic rdy_clr,
24
                output logic [7:0] data);
25
 
26
typedef enum logic [1:0] {
27
    RX_STATE_START,
28
    RX_STATE_DATA,
29
    RX_STATE_STOP
30
} state_t;
31
 
32
state_t state;
33
reg [3:0] sample;
34
reg [3:0] bitpos;
35
reg [7:0] scratch;
36
 
37
always_ff @(posedge clk or posedge reset) begin
38
    if (reset) begin
39
        sample <= 4'b0;
40
        bitpos <= 4'b0;
41
        scratch <= 8'b0;
42
        rdy <= 1'b0;
43
        state <= RX_STATE_START;
44
    end else begin
45
        if (rdy_clr)
46
            rdy <= 0;
47
 
48
        if (clken) begin
49
            case (state)
50
            RX_STATE_START: begin
51
                /*
52
                * Start counting from the first low sample, once we've
53
                * sampled a full bit, start collecting data bits.
54
                */
55
                if (!rx || sample != 0)
56
                    sample <= sample + 4'b1;
57
 
58
                if (sample == 15) begin
59
                    state <= RX_STATE_DATA;
60
                    bitpos <= 0;
61
                    sample <= 0;
62
                    scratch <= 0;
63
                end
64
            end
65
            RX_STATE_DATA: begin
66
                sample <= sample + 4'b1;
67
                if (sample == 4'h8) begin
68
                    scratch[bitpos[2:0]] <= rx;
69
                    bitpos <= bitpos + 4'b1;
70
                end
71
                if (bitpos == 8 && sample == 15)
72
                    state <= RX_STATE_STOP;
73
            end
74
            RX_STATE_STOP: begin
75
                /*
76
                * Our baud clock may not be running at exactly the
77
                * same rate as the transmitter.  If we thing that
78
                * we're at least half way into the stop bit, allow
79
                * transition into handling the next start bit.
80
                */
81
                if (sample == 15 || (sample >= 8 && !rx)) begin
82
                    state <= RX_STATE_START;
83
                    data <= scratch;
84
                    rdy <= 1'b1;
85
                    sample <= 0;
86
                end else begin
87
                    sample <= sample + 4'b1;
88
                end
89
            end
90
            default: begin
91
                state <= RX_STATE_START;
92
            end
93
            endcase
94
        end
95
    end
96
end
97
 
98
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.