OpenCores
URL https://opencores.org/ocsvn/systemverilog-uart16550/systemverilog-uart16550/trunk

Subversion Repositories systemverilog-uart16550

[/] [systemverilog-uart16550/] [trunk/] [rtl/] [uart_baud.sv] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 hiroshi
/* *****************************************************************************
2
   * title:         uart_16550_rll module                                      *
3
   * description:   RS232 Protocol 16550D uart (mostly supported)              *
4
   * languages:     systemVerilog                                              *
5
   *                                                                           *
6
   * Copyright (C) 2010 miyagi.hiroshi                                         *
7
   *                                                                           *
8
   * This library is free software; you can redistribute it and/or             *
9
   * modify it under the terms of the GNU Lesser General Public                *
10
   * License as published by the Free Software Foundation; either              *
11
   * version 2.1 of the License, or (at your option) any later version.        *
12
   *                                                                           *
13
   * This library is distributed in the hope that it will be useful,           *
14
   * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
15
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
16
   * Lesser General Public License for more details.                           *
17
   *                                                                           *
18
   * You should have received a copy of the GNU Lesser General Public          *
19
   * License along with this library; if not, write to the Free Software       *
20
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111*1307  USA *
21
   *                                                                           *
22
   *         ***  GNU LESSER GENERAL PUBLIC LICENSE  ***                       *
23
   *           from http://www.gnu.org/licenses/lgpl.txt                       *
24
   *****************************************************************************
25
   *                            redleaflogic,ltd                               *
26
   *                    miyagi.hiroshi@redleaflogic.biz                        *
27
   *          $Id: uart_baud.sv 108 2010-03-30 02:56:26Z hiroshi $         *
28
   ***************************************************************************** */
29
 
30
`ifdef SYN
31
/* empty */
32
`else
33
timeunit      1ps ;
34
timeprecision 1ps ;
35
`endif
36
import uart_package:: * ;
37
module uart_baud
38
  (
39
   input wire clk_i,
40
   input wire nrst_i,
41
   input u_reg_t u_reg,
42
   input codec_state_t  rec_next_state,
43
   input u_codec_t  trans_codec,
44
   input wire  rxd_clean,
45
   output wire rec_clk_en,
46
   output wire rec_sample_pulse,
47
   output wire leading_edge,
48
   output wire timeout_signal,
49
   output wire trans_clk_en
50
   ) ;
51
 
52
   // -------------------
53
   // -- receiver baud --
54
   // -------------------
55
   // -- leading edge generate --
56
   logic [3:0] rec_count ;
57
   logic [7:0] rec_divisor ;
58
   logic       rxd_l ;
59
   logic [5:0] timeout_c ;
60
 
61
   always_ff @(posedge clk_i, negedge nrst_i) begin
62
      if(nrst_i == 1'b0)
63
        rxd_l <= #1 1'b1 ;
64
      else
65
        rxd_l <= #1 rxd_clean ;
66
   end
67
 
68
   wire rec_count_enable = rec_count == 4'hf ;
69
   wire rec_count_sample = rec_count == 4'h8 ;
70
   assign leading_edge = (rxd_l & ~rxd_clean) && (rec_next_state == IDLE ||
71
                                                  rec_next_state == TIMEOUT ||
72
                                                  rec_next_state == STOP) ;
73
 
74
   always_ff @(posedge clk_i, negedge nrst_i) begin
75
      if(nrst_i == 1'b0)
76
        rec_count <= #1 4'h0 ;
77
      else if(rec_next_state == IDLE || rec_count_enable == 1'b1 || leading_edge == 1'b1)
78
        rec_count <= #1 4'h0 ;
79
      else
80
        rec_count <= #1 rec_count + 4'h1 ;
81
   end
82
   wire rec_count_end      = rec_divisor == u_reg.baud_reg && rec_count_enable == 1'b1 ;
83
   // sample pulse position -> baud_reg/2
84
   assign rec_sample_pulse = rec_divisor == {1'b0, u_reg.baud_reg[7:1]} && rec_count_sample == 1'b1 ;
85
   assign rec_clk_en       = rec_count_end ;
86
 
87
   always_ff @(posedge clk_i, negedge nrst_i) begin
88
      if(nrst_i == 1'b0)
89
        rec_divisor <= #1 8'h00 ;
90
      else if(rec_count_end == 1'b1 || rec_next_state == IDLE || leading_edge == 1'b1)
91
        rec_divisor <= #1 8'h00 ;
92
      else if(rec_count_enable == 1'b1)
93
        rec_divisor <= #1 rec_divisor + 8'h01 ;
94
      else
95
        rec_divisor <= #1 rec_divisor ;
96
   end
97
 
98
   // -- timeout counter --
99
   // about 4 character :: read for manual -> 4.3 Interrupt Identification Register (IIR)
100
   always_ff @(posedge clk_i, negedge nrst_i) begin
101
      if(nrst_i == 1'b0)
102
        timeout_c <= #1 6'h00 ;
103
      else if(timeout_signal == 1'b1 || rec_next_state == IDLE || leading_edge == 1'b1)
104
        timeout_c <= #1 6'h00 ;
105
      else if(rec_count_end == 1'b1)
106
        timeout_c <= #1 timeout_c + 6'h01 ;
107
      else
108
        timeout_c <= #1 timeout_c ;
109
   end
110
   assign timeout_signal = timeout_c == 6'h28 && rec_count_end == 1'b1 ;
111
 
112
   // ----------------------
113
   // -- transmitter baud --
114
   // ----------------------
115
   logic [3:0] trans_count ;
116
   logic [7:0] trans_divisor ;
117
 
118
   wire        trans_count_enable = trans_count == 4'hf ;
119
 
120
   always_ff @(posedge clk_i, negedge nrst_i) begin
121
      if(nrst_i == 1'b0)
122
        trans_count <= #1 4'h0 ;
123
      else if(trans_codec.state == IDLE || trans_count_enable == 1'b1)
124
        trans_count <= #1 4'h0 ;
125
      else
126
        trans_count <= trans_count + 4'h1 ;
127
   end
128
 
129
   wire trans_count_end = trans_divisor == u_reg.baud_reg && trans_count_enable == 1'b1 ;
130
   assign trans_clk_en = trans_count_end ;
131
 
132
   always_ff @(posedge clk_i, negedge nrst_i) begin
133
      if(nrst_i == 1'b0)
134
        trans_divisor <= #1 8'h00 ;
135
      else if(trans_count_end == 1'b1 || trans_codec.state == IDLE)
136
        trans_divisor <= #1 8'h00 ;
137
      else if(trans_count_enable == 1'b1)
138
        trans_divisor <= #1 trans_divisor + 8'h01 ;
139
      else
140
        trans_divisor <= #1 trans_divisor ;
141
   end
142
 
143
endmodule

powered by: WebSVN 2.1.0

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