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

Subversion Repositories xulalx25soc

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

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
// Purpose:     
8
//              
9
//
10
//
11
// Creator:     Dan Gisselquist, Ph.D.
12
//              Gisselquist Technology, LLC
13
//
14
///////////////////////////////////////////////////////////////////////////
15
//
16
// Copyright (C) 2015, Gisselquist Technology, LLC
17
//
18
// This program is free software (firmware): you can redistribute it and/or
19
// modify it under the terms of  the GNU General Public License as published
20
// by the Free Software Foundation, either version 3 of the License, or (at
21
// your option) any later version.
22
//
23
// This program is distributed in the hope that it will be useful, but WITHOUT
24
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
25
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26
// for more details.
27
//
28
// You should have received a copy of the GNU General Public License along
29
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
30
// target there if the PDF file isn't present.)  If not, see
31
// <http://www.gnu.org/licenses/> for a copy.
32
//
33
// License:     GPL, v3, as defined and found on www.gnu.org,
34
//              http://www.gnu.org/licenses/gpl.html
35
//
36
//
37
///////////////////////////////////////////////////////////////////////////
38
module  wbpwmaudio(i_clk,
39
                // Wishbone interface
40
                i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
41
                        o_wb_ack, o_wb_stall, o_wb_data,
42
                o_pwm, o_int);
43
        parameter       DEFAULT_RELOAD = 32'd2268, // about 44.1 kHz @  80MHz
44
                        //DEFAULT_RELOAD = 32'd2268,//about 44.1 kHz @ 100MHz
45
                        VARIABLE_RATE=0;
46
        input   i_clk;
47
        input   i_wb_cyc, i_wb_stb, i_wb_we;
48
        input           i_wb_addr;
49
        input   [31:0]   i_wb_data;
50
        output  reg             o_wb_ack;
51
        output  wire            o_wb_stall;
52
        output  wire    [31:0]   o_wb_data;
53
        output  reg             o_pwm;
54
        output  reg             o_int;
55
 
56
 
57
        // How often shall we create an interrupt?  Every reload_value clocks!
58
        // If VARIABLE_RATE==0, this value will never change and will be kept
59
        // at the default reload rate (44.1 kHz, for a 100 MHz clock)
60
        generate
61
        if (VARIABLE_RATE != 0)
62
        begin
63
                reg     [31:0]   r_reload_value;
64
                initial r_reload_value = DEFAULT_RELOAD;
65
                always @(posedge i_clk) // Data write
66
                        if ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr))
67
                                reload_value <= i_wb_data;
68
                wire    [31:0]   w_reload_value;
69
                assign  w_reload_value = r_reload_value;
70
        end else begin
71
                wire    [31:0]   w_reload_value;
72
                assign  w_reload_value = DEFAULT_RELOAD;
73
        end endgenerate
74
 
75
        reg     [31:0]   reload_value, timer;
76
        initial reload_value = DEFAULT_RELOAD;
77
        always @(posedge i_clk)
78
                if (timer == 0)
79
                        timer <= reload_value;
80
                else
81
                        timer <= timer - 1;
82
 
83
        reg     [15:0]   sample_out;
84
        always @(posedge i_clk)
85
                if (timer == 0)
86
                        sample_out <= next_sample;
87
 
88
 
89
        reg     [15:0]   next_sample;
90
        reg             next_valid;
91
        initial next_valid = 1'b1;
92
        initial next_sample = 16'h8000;
93
        always @(posedge i_clk) // Data write
94
                if ((i_wb_cyc)&&(i_wb_stb)&&((~i_wb_addr)||(VARIABLE_RATE==0)))
95
                begin
96
                        // Write with two's complement data, convert it
97
                        // internally to binary offset
98
                        next_sample <= { ~i_wb_data[15], i_wb_data[14:0] };
99
                        next_valid <= 1'b1;
100
                end else if (timer == 0)
101
                        next_valid <= 1'b0;
102
 
103
        initial o_int = 1'b0;
104
        always @(posedge i_clk)
105
                o_int <= (~next_valid);
106
 
107
        reg     [15:0]   pwm_counter;
108
        initial pwm_counter = 16'h00;
109
        always @(posedge i_clk)
110
                pwm_counter <= pwm_counter + 1;
111
 
112
        wire    [15:0]   br_counter;
113
        genvar  k;
114
        generate for(k=0; k<16; k=k+1)
115
        begin
116
                assign br_counter[k] = pwm_counter[15-k];
117
        end endgenerate
118
 
119
        always @(posedge i_clk)
120
                o_pwm <= (sample_out < br_counter);
121
 
122
        generate
123
        if (VARIABLE_RATE == 0)
124
        begin
125
                assign o_wb_data = { 15'h00, o_int, sample_out };
126
        end else begin
127
                reg     [31:0]   r_wb_data;
128
                always @(posedge i_clk)
129
                        if (i_wb_addr)
130
                                r_wb_data <= reload_value;
131
                        else
132
                                r_wb_data <= { 15'h00, o_int, sample_out };
133
                assign  o_wb_data = r_wb_data;
134
        end endgenerate
135
 
136
        initial o_wb_ack = 1'b0;
137
        always @(posedge i_clk)
138
                o_wb_ack <= (i_wb_cyc)&&(i_wb_stb);
139
        assign  o_wb_stall = 1'b0;
140
 
141
endmodule

powered by: WebSVN 2.1.0

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