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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [rtl/] [wbpwmaudio.v] - Blame information for rev 46

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dgisselq
///////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    wbpwmaudio.v
4
//              
5
// Project:     A Wishbone Controlled PWM (audio) controller
6
//
7
// Purpose:     This PWM controller was designed with audio in mind, although
8
//              it should be sufficient for many other purposes.  Specifically,
9
//      it creates a pulse-width modulated output, where the amount of time
10
//      the output is 'high' is determined by the pulse width data given to
11
//      it.  Further, the 'high' time is spread out in bit reversed order.
12
//      In this fashion, a halfway point will alternate between high and low,
13
//      rather than the normal fashion of being high for half the time and then
14
//      low.  This approach was chosen to move the PWM artifacts to higher,
15
//      inaudible frequencies and hence improve the sound quality.
16
//
17
//      The interface supports two addresses:
18
//
19
//      Addr[0] is the data register.  Writes to this register will set
20
//              a 16-bit sample value to be produced by the PWM logic.
21
//              Reads will also produce, in the 17th bit, whether the interrupt
22
//              is set or not.  (If set, it's time to write a new data value
23
//              ...)
24
//
25
//      Addr[1] is a timer reload value, used to determine how often the 
26
//              PWM logic needs its next value.  This number should be set
27
//              to the number of clock cycles between reload values.  So,
28
//              for example, an 80 MHz clock can generate a 44.1 kHz audio
29
//              stream by reading in a new sample every (80e6/44.1e3 = 1814)
30
//              samples.  After loading a sample, the device is immediately
31
//              ready to load a second.  Once the first sample completes,
32
//              the second sample will start going to the output, and an
33
//              interrupt will be generated indicating that the device is
34
//              now ready for the third sample.  (The one sample buffer
35
//              allows some flexibility in getting the new sample there fast
36
//              enough ...)
37
//
38
//
39
//      If you read through the code below, you'll notice that you can also
40
//      set the timer reload value to an immutable constant by changing the
41
//      VARIABLE_RATE parameter to 0.  When VARIABLE_RATE is set to zero,
42
//      both addresses become the same, Addr[0] or the data register, and the
43
//      reload value can no longer be changed--forcing the sample rate to
44
//      stay constant.
45
//
46
//
47
//      Of course, if you don't want to deal with the interrupts or sample
48
//      rates, you can still get a pseudo analog output by just setting the
49
//      value to the analog output you would like and then not updating
50
//      it.  In this case, you could also shut the interrupt down at the
51
//      controller, to keep that from bothering you as well.
52
//
53
// Creator:     Dan Gisselquist, Ph.D.
54
//              Gisselquist Technology, LLC
55
//
56
///////////////////////////////////////////////////////////////////////////
57
//
58
// Copyright (C) 2015, Gisselquist Technology, LLC
59
//
60
// This program is free software (firmware): you can redistribute it and/or
61
// modify it under the terms of  the GNU General Public License as published
62
// by the Free Software Foundation, either version 3 of the License, or (at
63
// your option) any later version.
64
//
65
// This program is distributed in the hope that it will be useful, but WITHOUT
66
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
67
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
68
// for more details.
69
//
70
// You should have received a copy of the GNU General Public License along
71
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
72
// target there if the PDF file isn't present.)  If not, see
73
// <http://www.gnu.org/licenses/> for a copy.
74
//
75
// License:     GPL, v3, as defined and found on www.gnu.org,
76
//              http://www.gnu.org/licenses/gpl.html
77
//
78
//
79
///////////////////////////////////////////////////////////////////////////
80
module  wbpwmaudio(i_clk,
81
                // Wishbone interface
82
                i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
83
                        o_wb_ack, o_wb_stall, o_wb_data,
84 4 dgisselq
                o_pwm, o_aux, o_int);
85 46 dgisselq
        parameter       DEFAULT_RELOAD = 16'd1814, // about 44.1 kHz @  80MHz
86
                        //DEFAULT_RELOAD = 16'd2268,//about 44.1 kHz @ 100MHz
87 2 dgisselq
                        NAUX=2, // Dev control values
88 46 dgisselq
                        VARIABLE_RATE=0,
89
                        TIMING_BITS=16;
90
        localparam [0:0]  BITREVERSE=1;
91 2 dgisselq
        input   i_clk;
92
        input   i_wb_cyc, i_wb_stb, i_wb_we;
93
        input           i_wb_addr;
94
        input   [31:0]   i_wb_data;
95
        output  reg             o_wb_ack;
96
        output  wire            o_wb_stall;
97
        output  wire    [31:0]   o_wb_data;
98
        output  reg             o_pwm;
99
        output  reg     [(NAUX-1):0]     o_aux;
100
        output  reg             o_int;
101
 
102
 
103
        // How often shall we create an interrupt?  Every reload_value clocks!
104
        // If VARIABLE_RATE==0, this value will never change and will be kept
105 12 dgisselq
        // at the default reload rate (defined up top)
106 4 dgisselq
        wire    [(TIMING_BITS-1):0]      w_reload_value;
107 2 dgisselq
        generate
108
        if (VARIABLE_RATE != 0)
109
        begin
110 4 dgisselq
                reg     [(TIMING_BITS-1):0]      r_reload_value;
111 2 dgisselq
                initial r_reload_value = DEFAULT_RELOAD;
112
                always @(posedge i_clk) // Data write
113 46 dgisselq
                        if ((i_wb_stb)&&(i_wb_addr)&&(i_wb_we))
114 4 dgisselq
                                r_reload_value <= i_wb_data[(TIMING_BITS-1):0];
115 2 dgisselq
                assign  w_reload_value = r_reload_value;
116
        end else begin
117
                assign  w_reload_value = DEFAULT_RELOAD;
118
        end endgenerate
119
 
120 46 dgisselq
        reg                             ztimer;
121 4 dgisselq
        reg     [(TIMING_BITS-1):0]      timer;
122 2 dgisselq
        initial timer = DEFAULT_RELOAD;
123 46 dgisselq
        initial ztimer= 1'b0;
124 2 dgisselq
        always @(posedge i_clk)
125 46 dgisselq
                ztimer <= (timer == { {(TIMING_BITS-1){1'b0}}, 1'b1 });
126
        always @(posedge i_clk)
127
                if (ztimer)
128 8 dgisselq
                        timer <= w_reload_value;
129 2 dgisselq
                else
130 4 dgisselq
                        timer <= timer - {{(TIMING_BITS-1){1'b0}},1'b1};
131 2 dgisselq
 
132
        reg     [15:0]   sample_out;
133
        always @(posedge i_clk)
134 46 dgisselq
                if (ztimer)
135 2 dgisselq
                        sample_out <= next_sample;
136
 
137
 
138
        reg     [15:0]   next_sample;
139
        reg             next_valid;
140
        initial next_valid = 1'b1;
141
        initial next_sample = 16'h8000;
142
        always @(posedge i_clk) // Data write
143 46 dgisselq
                if ((i_wb_stb)&&(i_wb_we)
144 2 dgisselq
                                &&((~i_wb_addr)||(VARIABLE_RATE==0)))
145
                begin
146
                        // Write with two's complement data, convert it
147
                        // internally to binary offset
148
                        next_sample <= { ~i_wb_data[15], i_wb_data[14:0] };
149
                        next_valid <= 1'b1;
150
                        if (i_wb_data[16])
151
                                o_aux <= i_wb_data[(NAUX+20-1):20];
152 46 dgisselq
                end else if (ztimer)
153 2 dgisselq
                        next_valid <= 1'b0;
154
 
155
        initial o_int = 1'b0;
156
        always @(posedge i_clk)
157
                o_int <= (~next_valid);
158
 
159
        reg     [15:0]   pwm_counter;
160
        initial pwm_counter = 16'h00;
161
        always @(posedge i_clk)
162 12 dgisselq
                pwm_counter <= pwm_counter + 16'h01;
163 2 dgisselq
 
164
        wire    [15:0]   br_counter;
165
        genvar  k;
166
        generate for(k=0; k<16; k=k+1)
167
        begin : bit_reversal_loop
168 46 dgisselq
                assign br_counter[k] = (BITREVERSE)?pwm_counter[15-k]:pwm_counter[k];
169 2 dgisselq
        end endgenerate
170
 
171
        always @(posedge i_clk)
172
                o_pwm <= (sample_out >= br_counter);
173
 
174
        generate
175
        if (VARIABLE_RATE == 0)
176
        begin
177
                assign o_wb_data = { {(12-NAUX){1'b0}}, o_aux,
178
                                        3'h0, o_int, sample_out };
179
        end else begin
180
                reg     [31:0]   r_wb_data;
181
                always @(posedge i_clk)
182
                        if (i_wb_addr)
183 4 dgisselq
                                r_wb_data <= w_reload_value;
184 2 dgisselq
                        else
185
                                r_wb_data <= { {(12-NAUX){1'b0}}, o_aux,
186
                                                3'h0, o_int, sample_out };
187
                assign  o_wb_data = r_wb_data;
188
        end endgenerate
189
 
190
        initial o_wb_ack = 1'b0;
191
        always @(posedge i_clk)
192 46 dgisselq
                o_wb_ack <= (i_wb_stb);
193 2 dgisselq
        assign  o_wb_stall = 1'b0;
194
 
195
endmodule

powered by: WebSVN 2.1.0

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