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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [vdsm8.vhd] - Blame information for rev 333

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 219 jshamlet
-- Copyright (c)2018, 2020 Jeremy Seth Henry
2 218 jshamlet
-- All rights reserved.
3
--
4
-- Redistribution and use in source and binary forms, with or without
5
-- modification, are permitted provided that the following conditions are met:
6
--     * Redistributions of source code must retain the above copyright
7
--       notice, this list of conditions and the following disclaimer.
8
--     * Redistributions in binary form must reproduce the above copyright
9
--       notice, this list of conditions and the following disclaimer in the
10
--       documentation and/or other materials provided with the distribution,
11
--       where applicable (as part of a user interface, debugging port, etc.)
12
--
13
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
14
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
17
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 220 jshamlet
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22
-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 218 jshamlet
--
24
-- VHDL Units :  vdsm8
25
-- Description:  8-bit variable delta-sigma modulator single-bit DAC
26 219 jshamlet
--
27
-- Revision History
28
-- Author          Date     Change
29
------------------ -------- ---------------------------------------------------
30
-- Seth Henry      04/25/18 Initial design
31
-- Seth Henry      04/14/20 Code cleanup and revision section added
32 218 jshamlet
 
33
library ieee;
34
use ieee.std_logic_1164.all;
35
use ieee.std_logic_unsigned.all;
36
use ieee.std_logic_arith.all;
37
 
38
entity vdsm8 is
39
generic(
40
  Reset_Level           : std_logic := '1';
41
  -- Do not adjust alone! DELTA constants must be
42
  --  changed as well.
43
  DAC_Width             : integer := 8
44
);
45
port(
46
  Clock                 : in  std_logic;
47
  Reset                 : in  std_logic;
48
  DACin                 : in  std_logic_vector(DAC_Width-1 downto 0);
49 310 jshamlet
  DACadv                : in  std_logic := '1';
50 218 jshamlet
  DACout                : out std_logic
51
);
52
end entity;
53
 
54
architecture behave of vdsm8 is
55
 
56
  function ceil_log2 (x : in natural) return natural is
57
    variable retval     : natural;
58
  begin
59
    retval              := 1;
60
    while ((2**retval) - 1) < x loop
61
      retval            := retval + 1;
62
    end loop;
63
    return retval;
64
  end function;
65
 
66
  constant DELTA_1_I    : integer := 1;
67
  constant DELTA_2_I    : integer := 5;
68
  constant DELTA_3_I    : integer := 25;
69
  constant DELTA_4_I    : integer := 75;
70
  constant DELTA_5_I    : integer := 125;
71
  constant DELTA_6_I    : integer := 195;
72
 
73
  constant DELTA_1      : std_logic_vector(DAC_Width-1 downto 0) :=
74
                           conv_std_logic_vector(DELTA_1_I, DAC_Width);
75
  constant DELTA_2      : std_logic_vector(DAC_Width-1 downto 0) :=
76
                           conv_std_logic_vector(DELTA_2_I, DAC_Width);
77
  constant DELTA_3      : std_logic_vector(DAC_Width-1 downto 0) :=
78
                           conv_std_logic_vector(DELTA_3_I, DAC_Width);
79
  constant DELTA_4      : std_logic_vector(DAC_Width-1 downto 0) :=
80
                           conv_std_logic_vector(DELTA_4_I, DAC_Width);
81
  constant DELTA_5      : std_logic_vector(DAC_Width-1 downto 0) :=
82
                           conv_std_logic_vector(DELTA_5_I, DAC_Width);
83
  constant DELTA_6      : std_logic_vector(DAC_Width-1 downto 0) :=
84
                           conv_std_logic_vector(DELTA_6_I, DAC_Width);
85
 
86
  constant MAX_PERIOD   : integer := 2**DAC_Width;
87
  constant DIV_WIDTH    : integer := DAC_Width * 2;
88
 
89
  constant PADJ_1_I     : integer := DELTA_1_I * MAX_PERIOD;
90
  constant PADJ_2_I     : integer := DELTA_2_I * MAX_PERIOD;
91
  constant PADJ_3_I     : integer := DELTA_3_I * MAX_PERIOD;
92
  constant PADJ_4_I     : integer := DELTA_4_I * MAX_PERIOD;
93
  constant PADJ_5_I     : integer := DELTA_5_I * MAX_PERIOD;
94
  constant PADJ_6_I     : integer := DELTA_6_I * MAX_PERIOD;
95
 
96
  constant PADJ_1       : std_logic_vector(DIV_WIDTH-1 downto 0) :=
97
                           conv_std_logic_vector(PADJ_1_I,DIV_WIDTH);
98
  constant PADJ_2       : std_logic_vector(DIV_WIDTH-1 downto 0) :=
99
                           conv_std_logic_vector(PADJ_2_I,DIV_WIDTH);
100
  constant PADJ_3       : std_logic_vector(DIV_WIDTH-1 downto 0) :=
101
                           conv_std_logic_vector(PADJ_3_I,DIV_WIDTH);
102
  constant PADJ_4       : std_logic_vector(DIV_WIDTH-1 downto 0) :=
103
                           conv_std_logic_vector(PADJ_4_I,DIV_WIDTH);
104
  constant PADJ_5       : std_logic_vector(DIV_WIDTH-1 downto 0) :=
105
                           conv_std_logic_vector(PADJ_5_I,DIV_WIDTH);
106
  constant PADJ_6       : std_logic_vector(DIV_WIDTH-1 downto 0) :=
107
                           conv_std_logic_vector(PADJ_6_I,DIV_WIDTH);
108
 
109
  signal DACin_q        : std_logic_vector(DAC_Width-1 downto 0);
110
 
111
  signal Divisor        : std_logic_vector(DIV_WIDTH-1 downto 0);
112
  signal Dividend       : std_logic_vector(DIV_WIDTH-1 downto 0);
113
 
114
  signal q              : std_logic_vector(DIV_WIDTH*2-1 downto 0);
115
  signal diff           : std_logic_vector(DIV_WIDTH downto 0);
116
 
117
  constant CB           : integer := ceil_log2(DIV_WIDTH);
118
  signal count          : std_logic_vector(CB-1 downto 0);
119
 
120
  signal Next_Width     : std_logic_vector(DAC_Width-1 downto 0);
121
  signal Next_Period    : std_logic_vector(DAC_Width-1 downto 0);
122
 
123
  signal PWM_Width      : std_logic_vector(DAC_Width-1 downto 0);
124
  signal PWM_Period     : std_logic_vector(DAC_Width-1 downto 0);
125
 
126
  signal Width_Ctr      : std_logic_vector(DAC_Width-1 downto 0);
127
  signal Period_Ctr     : std_logic_vector(DAC_Width-1 downto 0);
128
 
129
begin
130
 
131
  diff                  <= ('0' & q(DIV_WIDTH*2-2 downto DIV_WIDTH-1)) -
132
                           ('0' & Divisor);
133
 
134
  Dividend   <= PADJ_2 when DACin_q >= DELTA_2_I and DACin_q < DELTA_3_I else
135
                PADJ_3 when DACin_q >= DELTA_3_I and DACin_q < DELTA_4_I else
136
                PADJ_4 when DACin_q >= DELTA_4_I and DACin_q < DELTA_5_I else
137
                PADJ_5 when DACin_q >= DELTA_5_I and DACin_q < DELTA_6_I else
138
                PADJ_6 when DACin_q >= DELTA_6_I else
139
                PADJ_1;
140
 
141
  Next_Width <= DELTA_1 when DACin_q >= DELTA_1_I and DACin_q < DELTA_2_I else
142
                DELTA_2 when DACin_q >= DELTA_2_I and DACin_q < DELTA_3_I else
143
                DELTA_3 when DACin_q >= DELTA_3_I and DACin_q < DELTA_4_I else
144
                DELTA_4 when DACin_q >= DELTA_4_I and DACin_q < DELTA_5_I else
145
                DELTA_5 when DACin_q >= DELTA_5_I and DACin_q < DELTA_6_I else
146
                DELTA_6 when DACin_q >= DELTA_6_I else
147
                (others => '0');
148
 
149
  Next_Period           <= q(7 downto 0) - 1;
150
 
151
  vDSM_proc: process( Clock, Reset )
152
  begin
153
    if( Reset = Reset_Level )then
154
      q                 <= (others => '0');
155
      count             <= (others => '1');
156
      Divisor           <= (others => '0');
157 310 jshamlet
      DACin_q           <= (others => '0');
158 218 jshamlet
      PWM_Width         <= (others => '0');
159
      PWM_Period        <= (others => '0');
160
      Period_Ctr        <= (others => '0');
161
      Width_Ctr         <= (others => '0');
162
      DACout            <= '0';
163
    elsif( rising_edge(Clock) )then
164
      q                 <= diff(DIV_WIDTH-1 downto 0) &
165
                           q(DIV_WIDTH-2 downto 0) & '1';
166
      if( diff(DIV_WIDTH) = '1' )then
167
        q               <= q(DIV_WIDTH*2-2 downto 0) & '0';
168
      end if;
169
 
170
      count             <= count + 1;
171
      if( count = DIV_WIDTH )then
172
        PWM_Width       <= Next_Width;
173
        PWM_Period      <= Next_Period;
174 310 jshamlet
        DACin_q         <= DACin;
175 218 jshamlet
        Divisor         <= (others => '0');
176
        Divisor(DAC_Width-1 downto 0) <= DACin_q;
177
        q               <= conv_std_logic_vector(0,DIV_WIDTH) & Dividend;
178
        count           <= (others => '0');
179
      end if;
180
 
181 310 jshamlet
      Period_Ctr        <= Period_Ctr - DACadv;
182
      Width_Ctr         <= Width_Ctr - DACadv;
183 218 jshamlet
 
184
      DACout            <= '1';
185
      if( Width_Ctr = 0 )then
186
        DACout          <= '0';
187
        Width_Ctr       <= (others => '0');
188
      end if;
189
 
190
      if( Period_Ctr = 0 )then
191
        Period_Ctr      <= PWM_Period;
192
        Width_Ctr       <= PWM_Width;
193
      end if;
194
 
195
    end if;
196
  end process;
197
 
198 310 jshamlet
end architecture;

powered by: WebSVN 2.1.0

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