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

Subversion Repositories xulalx25soc

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

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

powered by: WebSVN 2.1.0

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