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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [rtl/] [wbpwmaudio.v] - Blame information for rev 113

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 8 dgisselq
// 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 2 dgisselq
//
17 8 dgisselq
//      The interface supports two addresses:
18 2 dgisselq
//
19 8 dgisselq
//      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 2 dgisselq
// 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
                o_pwm, o_int);
85 63 dgisselq
        parameter       DEFAULT_RELOAD = 17'd1814, // about 44.1 kHz @  80MHz
86
                        //DEFAULT_RELOAD = 17'd2268,//about 44.1 kHz @ 100MHz
87 56 dgisselq
                        VARIABLE_RATE=0,
88
                        TIMING_BITS=17;
89 2 dgisselq
        input   i_clk;
90
        input   i_wb_cyc, i_wb_stb, i_wb_we;
91
        input           i_wb_addr;
92
        input   [31:0]   i_wb_data;
93
        output  reg             o_wb_ack;
94
        output  wire            o_wb_stall;
95
        output  wire    [31:0]   o_wb_data;
96
        output  reg             o_pwm;
97
        output  reg             o_int;
98
 
99
 
100
        // How often shall we create an interrupt?  Every reload_value clocks!
101
        // If VARIABLE_RATE==0, this value will never change and will be kept
102 56 dgisselq
        // at the default reload rate (defined up top)
103
        wire    [(TIMING_BITS-1):0]      w_reload_value;
104 2 dgisselq
        generate
105
        if (VARIABLE_RATE != 0)
106
        begin
107 56 dgisselq
                reg     [(TIMING_BITS-1):0]      r_reload_value;
108 2 dgisselq
                initial r_reload_value = DEFAULT_RELOAD;
109
                always @(posedge i_clk) // Data write
110 113 dgisselq
                        if ((i_wb_stb)&&(i_wb_addr)&&(i_wb_we))
111 56 dgisselq
                                r_reload_value <= i_wb_data[(TIMING_BITS-1):0];
112 2 dgisselq
                assign  w_reload_value = r_reload_value;
113
        end else begin
114
                assign  w_reload_value = DEFAULT_RELOAD;
115
        end endgenerate
116
 
117 63 dgisselq
        reg                             ztimer;
118 56 dgisselq
        reg     [(TIMING_BITS-1):0]      timer;
119 46 dgisselq
        initial timer = DEFAULT_RELOAD;
120 63 dgisselq
        initial ztimer= 1'b0;
121 2 dgisselq
        always @(posedge i_clk)
122 63 dgisselq
                ztimer <= (timer == { {(TIMING_BITS-1){1'b0}}, 1'b1 });
123
        always @(posedge i_clk)
124
                if (ztimer)
125 56 dgisselq
                        timer <= w_reload_value;
126 2 dgisselq
                else
127 56 dgisselq
                        timer <= timer - {{(TIMING_BITS-1){1'b0}},1'b1};
128 2 dgisselq
 
129
        reg     [15:0]   sample_out;
130
        always @(posedge i_clk)
131 63 dgisselq
                if (ztimer)
132 2 dgisselq
                        sample_out <= next_sample;
133
 
134
 
135
        reg     [15:0]   next_sample;
136
        reg             next_valid;
137
        initial next_valid = 1'b1;
138
        initial next_sample = 16'h8000;
139
        always @(posedge i_clk) // Data write
140 113 dgisselq
                if ((i_wb_stb)&&(i_wb_we)
141 9 dgisselq
                                &&((~i_wb_addr)||(VARIABLE_RATE==0)))
142 2 dgisselq
                begin
143
                        // Write with two's complement data, convert it
144
                        // internally to binary offset
145
                        next_sample <= { ~i_wb_data[15], i_wb_data[14:0] };
146
                        next_valid <= 1'b1;
147 63 dgisselq
                end else if (ztimer)
148 2 dgisselq
                        next_valid <= 1'b0;
149
 
150
        initial o_int = 1'b0;
151
        always @(posedge i_clk)
152
                o_int <= (~next_valid);
153
 
154
        reg     [15:0]   pwm_counter;
155
        initial pwm_counter = 16'h00;
156
        always @(posedge i_clk)
157 56 dgisselq
                pwm_counter <= pwm_counter + 16'h01;
158 2 dgisselq
 
159
        wire    [15:0]   br_counter;
160
        genvar  k;
161
        generate for(k=0; k<16; k=k+1)
162 9 dgisselq
        begin : bit_reversal_loop
163 2 dgisselq
                assign br_counter[k] = pwm_counter[15-k];
164
        end endgenerate
165
 
166
        always @(posedge i_clk)
167 34 dgisselq
                o_pwm <= (sample_out >= br_counter);
168 2 dgisselq
 
169
        generate
170
        if (VARIABLE_RATE == 0)
171
        begin
172
                assign o_wb_data = { 15'h00, o_int, sample_out };
173
        end else begin
174
                reg     [31:0]   r_wb_data;
175
                always @(posedge i_clk)
176
                        if (i_wb_addr)
177 113 dgisselq
                                r_wb_data <= { {(32-TIMING_BITS){1'b0}}, w_reload_value };
178 2 dgisselq
                        else
179
                                r_wb_data <= { 15'h00, o_int, sample_out };
180
                assign  o_wb_data = r_wb_data;
181
        end endgenerate
182
 
183
        initial o_wb_ack = 1'b0;
184
        always @(posedge i_clk)
185 113 dgisselq
                o_wb_ack <= (i_wb_stb);
186 2 dgisselq
        assign  o_wb_stall = 1'b0;
187
 
188
endmodule

powered by: WebSVN 2.1.0

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