| 1 |
167 |
jshamlet |
-- Copyright (c)2013 Jeremy Seth Henry
|
| 2 |
|
|
-- 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 |
|
|
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
| 22 |
|
|
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 23 |
|
|
--
|
| 24 |
|
|
-- VHDL Units : o8_vdsm8
|
| 25 |
|
|
-- Description: 8-bit variable delta-sigma modulator. Requires Open8_pkg.vhd
|
| 26 |
|
|
|
| 27 |
|
|
library ieee;
|
| 28 |
|
|
use ieee.std_logic_1164.all;
|
| 29 |
|
|
use ieee.std_logic_unsigned.all;
|
| 30 |
|
|
use ieee.std_logic_arith.all;
|
| 31 |
|
|
|
| 32 |
|
|
library work;
|
| 33 |
|
|
use work.open8_pkg.all;
|
| 34 |
|
|
|
| 35 |
|
|
entity o8_vdsm8 is
|
| 36 |
|
|
generic(
|
| 37 |
|
|
Reset_Level : std_logic;
|
| 38 |
|
|
Address : ADDRESS_TYPE
|
| 39 |
|
|
);
|
| 40 |
|
|
port(
|
| 41 |
|
|
Clock : in std_logic;
|
| 42 |
|
|
Reset : in std_logic;
|
| 43 |
|
|
--
|
| 44 |
|
|
Bus_Address : in ADDRESS_TYPE;
|
| 45 |
|
|
Wr_Enable : in std_logic;
|
| 46 |
|
|
Wr_Data : in DATA_TYPE;
|
| 47 |
|
|
Rd_Enable : in std_logic;
|
| 48 |
|
|
Rd_Data : out DATA_TYPE;
|
| 49 |
|
|
--
|
| 50 |
|
|
DACout : out std_logic
|
| 51 |
|
|
);
|
| 52 |
|
|
end entity;
|
| 53 |
|
|
|
| 54 |
|
|
architecture behave of o8_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 User_Addr : std_logic_vector(15 downto 0) := Address;
|
| 67 |
|
|
alias Comp_Addr is Bus_Address(15 downto 0);
|
| 68 |
|
|
signal Addr_Match : std_logic;
|
| 69 |
|
|
signal Wr_En : std_logic;
|
| 70 |
|
|
signal Wr_Data_q : DATA_TYPE;
|
| 71 |
|
|
signal Rd_En : std_logic;
|
| 72 |
|
|
signal DACin : DATA_TYPE;
|
| 73 |
|
|
|
| 74 |
|
|
-- DAC WIDTH = 8 is fixed, with all constants normalized
|
| 75 |
|
|
-- against 256 (the MAX PERIOD)
|
| 76 |
|
|
|
| 77 |
|
|
constant DAC_WIDTH : integer := 8;
|
| 78 |
|
|
|
| 79 |
|
|
constant DELTA_1_I : integer := 1;
|
| 80 |
|
|
constant DELTA_2_I : integer := 5;
|
| 81 |
|
|
constant DELTA_3_I : integer := 25;
|
| 82 |
|
|
constant DELTA_4_I : integer := 75;
|
| 83 |
|
|
constant DELTA_5_I : integer := 125;
|
| 84 |
|
|
constant DELTA_6_I : integer := 195;
|
| 85 |
|
|
|
| 86 |
|
|
constant DELTA_1 : std_logic_vector(DAC_WIDTH - 1 downto 0) :=
|
| 87 |
|
|
conv_std_logic_vector(DELTA_1_I, DAC_WIDTH);
|
| 88 |
|
|
constant DELTA_2 : std_logic_vector(DAC_WIDTH - 1 downto 0) :=
|
| 89 |
|
|
conv_std_logic_vector(DELTA_2_I, DAC_WIDTH);
|
| 90 |
|
|
constant DELTA_3 : std_logic_vector(DAC_WIDTH - 1 downto 0) :=
|
| 91 |
|
|
conv_std_logic_vector(DELTA_3_I, DAC_WIDTH);
|
| 92 |
|
|
constant DELTA_4 : std_logic_vector(DAC_WIDTH - 1 downto 0) :=
|
| 93 |
|
|
conv_std_logic_vector(DELTA_4_I, DAC_WIDTH);
|
| 94 |
|
|
constant DELTA_5 : std_logic_vector(DAC_WIDTH - 1 downto 0) :=
|
| 95 |
|
|
conv_std_logic_vector(DELTA_5_I, DAC_WIDTH);
|
| 96 |
|
|
constant DELTA_6 : std_logic_vector(DAC_WIDTH - 1 downto 0) :=
|
| 97 |
|
|
conv_std_logic_vector(DELTA_6_I, DAC_WIDTH);
|
| 98 |
|
|
|
| 99 |
|
|
constant MAX_PERIOD : integer := 2**DAC_WIDTH;
|
| 100 |
|
|
constant DIV_WIDTH : integer := 2 * DAC_WIDTH;
|
| 101 |
|
|
|
| 102 |
|
|
constant PADJ_1_I : integer := DELTA_1_I * MAX_PERIOD;
|
| 103 |
|
|
constant PADJ_2_I : integer := DELTA_2_I * MAX_PERIOD;
|
| 104 |
|
|
constant PADJ_3_I : integer := DELTA_3_I * MAX_PERIOD;
|
| 105 |
|
|
constant PADJ_4_I : integer := DELTA_4_I * MAX_PERIOD;
|
| 106 |
|
|
constant PADJ_5_I : integer := DELTA_5_I * MAX_PERIOD;
|
| 107 |
|
|
constant PADJ_6_I : integer := DELTA_6_I * MAX_PERIOD;
|
| 108 |
|
|
|
| 109 |
|
|
constant PADJ_1 : std_logic_vector(DIV_WIDTH-1 downto 0) :=
|
| 110 |
|
|
conv_std_logic_vector(PADJ_1_I,DIV_WIDTH);
|
| 111 |
|
|
constant PADJ_2 : std_logic_vector(DIV_WIDTH-1 downto 0) :=
|
| 112 |
|
|
conv_std_logic_vector(PADJ_2_I,DIV_WIDTH);
|
| 113 |
|
|
constant PADJ_3 : std_logic_vector(DIV_WIDTH-1 downto 0) :=
|
| 114 |
|
|
conv_std_logic_vector(PADJ_3_I,DIV_WIDTH);
|
| 115 |
|
|
constant PADJ_4 : std_logic_vector(DIV_WIDTH-1 downto 0) :=
|
| 116 |
|
|
conv_std_logic_vector(PADJ_4_I,DIV_WIDTH);
|
| 117 |
|
|
constant PADJ_5 : std_logic_vector(DIV_WIDTH-1 downto 0) :=
|
| 118 |
|
|
conv_std_logic_vector(PADJ_5_I,DIV_WIDTH);
|
| 119 |
|
|
constant PADJ_6 : std_logic_vector(DIV_WIDTH-1 downto 0) :=
|
| 120 |
|
|
conv_std_logic_vector(PADJ_6_I,DIV_WIDTH);
|
| 121 |
|
|
|
| 122 |
|
|
signal DACin_q : DATA_TYPE;
|
| 123 |
|
|
|
| 124 |
|
|
signal Divisor : std_logic_vector(DIV_WIDTH-1 downto 0);
|
| 125 |
|
|
signal Dividend : std_logic_vector(DIV_WIDTH-1 downto 0);
|
| 126 |
|
|
|
| 127 |
|
|
signal q : std_logic_vector(DIV_WIDTH*2-1 downto 0);
|
| 128 |
|
|
signal diff : std_logic_vector(DIV_WIDTH downto 0);
|
| 129 |
|
|
|
| 130 |
|
|
constant CB : integer := ceil_log2(DIV_WIDTH);
|
| 131 |
|
|
signal count : std_logic_vector(CB-1 downto 0);
|
| 132 |
|
|
|
| 133 |
|
|
signal Next_Width : DATA_TYPE;
|
| 134 |
|
|
signal Next_Period : DATA_TYPE;
|
| 135 |
|
|
|
| 136 |
|
|
signal PWM_Width : DATA_TYPE;
|
| 137 |
|
|
signal PWM_Period : DATA_TYPE;
|
| 138 |
|
|
|
| 139 |
|
|
signal Width_Ctr : DATA_TYPE;
|
| 140 |
|
|
signal Period_Ctr : DATA_TYPE;
|
| 141 |
|
|
|
| 142 |
|
|
begin
|
| 143 |
|
|
|
| 144 |
|
|
Addr_Match <= '1' when Comp_Addr = User_Addr else '0';
|
| 145 |
|
|
|
| 146 |
|
|
io_reg: process( Clock, Reset )
|
| 147 |
|
|
begin
|
| 148 |
|
|
if( Reset = Reset_Level )then
|
| 149 |
|
|
Wr_En <= '0';
|
| 150 |
|
|
Wr_Data_q <= x"00";
|
| 151 |
|
|
Rd_En <= '0';
|
| 152 |
|
|
Rd_Data <= x"00";
|
| 153 |
|
|
DACin <= x"00";
|
| 154 |
|
|
elsif( rising_edge( Clock ) )then
|
| 155 |
|
|
Wr_En <= Addr_Match and Wr_Enable;
|
| 156 |
|
|
Wr_Data_q <= Wr_Data;
|
| 157 |
|
|
if( Wr_En = '1' )then
|
| 158 |
|
|
DACin <= Wr_Data_q;
|
| 159 |
|
|
end if;
|
| 160 |
|
|
|
| 161 |
|
|
Rd_Data <= (others => '0');
|
| 162 |
|
|
Rd_En <= Addr_Match and Rd_Enable;
|
| 163 |
|
|
if( Rd_En = '1' )then
|
| 164 |
|
|
Rd_Data <= DACin;
|
| 165 |
|
|
end if;
|
| 166 |
|
|
end if;
|
| 167 |
|
|
end process;
|
| 168 |
|
|
|
| 169 |
|
|
diff <= ('0' & q(DIV_WIDTH*2-2 downto DIV_WIDTH-1)) -
|
| 170 |
|
|
('0' & Divisor);
|
| 171 |
|
|
|
| 172 |
|
|
Dividend <= PADJ_2 when DACin_q >= DELTA_2_I and DACin_q < DELTA_3_I else
|
| 173 |
|
|
PADJ_3 when DACin_q >= DELTA_3_I and DACin_q < DELTA_4_I else
|
| 174 |
|
|
PADJ_4 when DACin_q >= DELTA_4_I and DACin_q < DELTA_5_I else
|
| 175 |
|
|
PADJ_5 when DACin_q >= DELTA_5_I and DACin_q < DELTA_6_I else
|
| 176 |
|
|
PADJ_6 when DACin_q >= DELTA_6_I else
|
| 177 |
|
|
PADJ_1;
|
| 178 |
|
|
|
| 179 |
|
|
Next_Width <= DELTA_1 when DACin_q >= DELTA_1_I and DACin_q < DELTA_2_I else
|
| 180 |
|
|
DELTA_2 when DACin_q >= DELTA_2_I and DACin_q < DELTA_3_I else
|
| 181 |
|
|
DELTA_3 when DACin_q >= DELTA_3_I and DACin_q < DELTA_4_I else
|
| 182 |
|
|
DELTA_4 when DACin_q >= DELTA_4_I and DACin_q < DELTA_5_I else
|
| 183 |
|
|
DELTA_5 when DACin_q >= DELTA_5_I and DACin_q < DELTA_6_I else
|
| 184 |
|
|
DELTA_6 when DACin_q >= DELTA_6_I else
|
| 185 |
|
|
(others => '0');
|
| 186 |
|
|
|
| 187 |
|
|
Next_Period <= q(7 downto 0) - 1;
|
| 188 |
|
|
|
| 189 |
|
|
vDSM_proc: process( Clock, Reset )
|
| 190 |
|
|
begin
|
| 191 |
|
|
if( Reset = Reset_Level )then
|
| 192 |
|
|
q <= (others => '0');
|
| 193 |
|
|
count <= (others => '1');
|
| 194 |
|
|
Divisor <= (others => '0');
|
| 195 |
|
|
DACin_q <= (others => '0');
|
| 196 |
|
|
PWM_Width <= (others => '0');
|
| 197 |
|
|
PWM_Period <= (others => '0');
|
| 198 |
|
|
Period_Ctr <= (others => '0');
|
| 199 |
|
|
Width_Ctr <= (others => '0');
|
| 200 |
|
|
DACout <= '0';
|
| 201 |
|
|
elsif( rising_edge(Clock) )then
|
| 202 |
|
|
q <= diff(DIV_WIDTH-1 downto 0) &
|
| 203 |
|
|
q(DIV_WIDTH-2 downto 0) & '1';
|
| 204 |
|
|
if( diff(DIV_WIDTH) = '1' )then
|
| 205 |
|
|
q <= q(DIV_WIDTH*2-2 downto 0) & '0';
|
| 206 |
|
|
end if;
|
| 207 |
|
|
|
| 208 |
|
|
count <= count + 1;
|
| 209 |
|
|
if( count = DIV_WIDTH )then
|
| 210 |
|
|
PWM_Width <= Next_Width;
|
| 211 |
|
|
PWM_Period <= Next_Period;
|
| 212 |
|
|
DACin_q <= DACin;
|
| 213 |
|
|
Divisor <= (others => '0');
|
| 214 |
|
|
Divisor(7 downto 0) <= DACin_q;
|
| 215 |
|
|
q <= conv_std_logic_vector(0,DIV_WIDTH) & Dividend;
|
| 216 |
|
|
count <= (others => '0');
|
| 217 |
|
|
end if;
|
| 218 |
|
|
|
| 219 |
|
|
Period_Ctr <= Period_Ctr - 1;
|
| 220 |
|
|
Width_Ctr <= Width_Ctr - 1;
|
| 221 |
|
|
|
| 222 |
|
|
DACout <= '1';
|
| 223 |
|
|
if( Width_Ctr = 0 )then
|
| 224 |
|
|
DACout <= '0';
|
| 225 |
|
|
Width_Ctr <= (others => '0');
|
| 226 |
|
|
end if;
|
| 227 |
|
|
|
| 228 |
|
|
if( Period_Ctr = 0 )then
|
| 229 |
|
|
Period_Ctr <= PWM_Period;
|
| 230 |
|
|
Width_Ctr <= PWM_Width;
|
| 231 |
|
|
end if;
|
| 232 |
|
|
|
| 233 |
|
|
end if;
|
| 234 |
|
|
end process;
|
| 235 |
|
|
|
| 236 |
|
|
end architecture;
|