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

Subversion Repositories spdif_transmitter

[/] [spdif_transmitter/] [trunk/] [rtl/] [spdif.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ultra_embe
//-----------------------------------------------------------------
2
//                        SPDIF Transmitter
3
//                              V0.1
4
//                        Ultra-Embedded.com
5
//                          Copyright 2012
6
//
7
//                 Email: admin@ultra-embedded.com
8
//
9
//                         License: GPL
10
// If you would like a version with a more permissive license for
11
// use in closed source commercial applications please contact me
12
// for details.
13
//-----------------------------------------------------------------
14
//
15
// This file is open source HDL; you can redistribute it and/or 
16
// modify it under the terms of the GNU General Public License as 
17
// published by the Free Software Foundation; either version 2 of 
18
// the License, or (at your option) any later version.
19
//
20
// This file is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied warranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
// GNU General Public License for more details.
24
//
25
// You should have received a copy of the GNU General Public 
26
// License along with this file; if not, write to the Free Software
27
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28
// USA
29
//-----------------------------------------------------------------
30
module spdif
31
 
32
//-----------------------------------------------------------------
33
// Params
34
//-----------------------------------------------------------------
35
#(
36
    parameter CLK_RATE_KHZ          = 50000,
37
    parameter AUDIO_RATE            = 44100,
38
    parameter AUDIO_CLK_SRC         = "EXTERNAL", // INTERNAL or EXTERNAL
39
 
40
    // Generated params
41
    parameter WHOLE_CYCLES          = (CLK_RATE_KHZ*1000) / (AUDIO_RATE*128),
42
    parameter ERROR_BASE            = 10000,
43
    parameter [63:0] ERRORS_PER_BIT = ((CLK_RATE_KHZ * 1000 * ERROR_BASE) / (AUDIO_RATE*128)) - (WHOLE_CYCLES * ERROR_BASE)
44
)
45
 
46
//-----------------------------------------------------------------
47
// Ports
48
//-----------------------------------------------------------------
49
(
50
    input           clk_i,
51
    input           rst_i,
52
 
53
    // Audio clock source (only used when AUDIO_CLK_SRC=EXTERNAL)
54
    input           audio_clk_i,
55
 
56
    // Output
57
    output          spdif_o,
58
 
59
    // Audio interface (16-bit x 2 = RL)
60
    input [31:0]    sample_i,
61
    output          sample_req_o
62
);
63
 
64
//-----------------------------------------------------------------
65
// External clock source
66
//-----------------------------------------------------------------
67
wire    bit_clock_w;
68
generate
69
if (AUDIO_CLK_SRC == "EXTERNAL")
70
begin
71
    // Toggling flop in audio_clk_i domain
72
    reg toggle_aud_clk_q;
73
 
74
    always @ (posedge rst_i or posedge audio_clk_i)
75
    if (rst_i)
76
        toggle_aud_clk_q <= 1'b0;
77
    else
78
        toggle_aud_clk_q <= ~toggle_aud_clk_q;
79
 
80
    // Resync toggle_aud_clk_q to clk_i domain
81
    reg resync_toggle_ms_q;
82
    reg resync_toggle_q;
83
 
84
    always @ (posedge rst_i or posedge clk_i)
85
        if (rst_i)
86
        begin
87
            resync_toggle_ms_q  <= 1'b0;
88
            resync_toggle_q     <= 1'b0;
89
        end
90
        else
91
        begin
92
            resync_toggle_ms_q  <= toggle_aud_clk_q;
93
            resync_toggle_q     <= resync_toggle_ms_q;
94
        end
95
 
96
    reg last_toggle_q;
97
    always @ (posedge rst_i or posedge clk_i)
98
    if (rst_i)
99
        last_toggle_q   <= 1'b0;
100
    else
101
        last_toggle_q   <= resync_toggle_q;
102
 
103
    // Single cycle pulse on every rising edge of audio_clk_i
104
    assign bit_clock_w = last_toggle_q ^ resync_toggle_q;
105
end
106
//-----------------------------------------------------------------
107
// Internal clock source
108
//-----------------------------------------------------------------
109
else
110
begin
111
    reg [31:0]  count_q;
112
    reg [31:0]  error_q;
113
    reg         bit_clk_q;
114
 
115
    // Clock pulse generator
116
    always @ (posedge rst_i or posedge clk_i)
117
    begin
118
       if (rst_i)
119
       begin
120
            count_q     <= 32'd0;
121
            error_q     <= 32'd0;
122
            bit_clk_q   <= 1'b1;
123
       end
124
       else
125
       begin
126
            case (count_q)
127
 
128
            begin
129
                bit_clk_q   <= 1'b1;
130
                count_q     <= count_q + 32'd1;
131
            end
132
 
133
            WHOLE_CYCLES-1:
134
            begin
135
                if (error_q < (ERROR_BASE - ERRORS_PER_BIT))
136
                begin
137
                    error_q <= error_q + ERRORS_PER_BIT;
138
                    count_q <= 32'd0;
139
                end
140
                else
141
                begin
142
                    error_q <= error_q + ERRORS_PER_BIT - ERROR_BASE;
143
                    count_q <= count_q + 32'd1;
144
                end
145
 
146
                bit_clk_q   <= 1'b0;
147
            end
148
 
149
            WHOLE_CYCLES:
150
            begin
151
                count_q     <= 32'd0;
152
                bit_clk_q   <= 1'b0;
153
            end
154
 
155
            default:
156
            begin
157
                count_q     <= count_q + 32'd1;
158
                bit_clk_q   <= 1'b0;
159
            end
160
            endcase
161
       end
162
    end
163
 
164
    assign bit_clock_w = bit_clk_q;
165
end
166
endgenerate
167
 
168
//-----------------------------------------------------------------
169
// Core SPDIF
170
//-----------------------------------------------------------------
171
spdif_core
172
u_core
173
(
174
    .clk_i(clk_i),
175
    .rst_i(rst_i),
176
 
177
    .bit_out_en_i(bit_clock_w),
178
 
179
    .spdif_o(spdif_o),
180
 
181
    .sample_i(sample_i),
182
    .sample_req_o(sample_req_o)
183
);
184
 
185
endmodule

powered by: WebSVN 2.1.0

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