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

Subversion Repositories neo430

[/] [neo430/] [trunk/] [neo430/] [rtl/] [core/] [neo430_freq_gen.vhd] - Blame information for rev 198

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 198 zero_gravi
-- #################################################################################################
2
-- #  << NEO430 - Arbitrary Frequency Generator >>                                                 #
3
-- # ********************************************************************************************* #
4
-- # Number controlled oscillator-based frequency generator with three independent channels. Each  #
5
-- # channel has its enable flag, 16-bit tuning word register and prescaler selector. The phase    #
6
-- # accumulator of each channel is 17 bit wide.                                                   #
7
-- # f_out(x) = ((f_cpu / nco_prsc(x)) * tuning_word(x)) / 2^17   for channels x = 0,1,2           #
8
-- # ********************************************************************************************* #
9
-- # BSD 3-Clause License                                                                          #
10
-- #                                                                                               #
11
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
12
-- #                                                                                               #
13
-- # Redistribution and use in source and binary forms, with or without modification, are          #
14
-- # permitted provided that the following conditions are met:                                     #
15
-- #                                                                                               #
16
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
17
-- #    conditions and the following disclaimer.                                                   #
18
-- #                                                                                               #
19
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
20
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
21
-- #    provided with the distribution.                                                            #
22
-- #                                                                                               #
23
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
24
-- #    endorse or promote products derived from this software without specific prior written      #
25
-- #    permission.                                                                                #
26
-- #                                                                                               #
27
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
28
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
29
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
30
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
31
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
32
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
33
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
34
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
35
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
36
-- # ********************************************************************************************* #
37
-- # The NEO430 Processor - https://github.com/stnolting/neo430                                    #
38
-- #################################################################################################
39
 
40
library ieee;
41
use ieee.std_logic_1164.all;
42
use ieee.numeric_std.all;
43
 
44
library neo430;
45
use neo430.neo430_package.all;
46
 
47
entity neo430_freq_gen is
48
  port (
49
    -- host access --
50
    clk_i       : in  std_ulogic; -- global clock line
51
    rden_i      : in  std_ulogic; -- read enable
52
    wren_i      : in  std_ulogic; -- write enable
53
    addr_i      : in  std_ulogic_vector(15 downto 0); -- address
54
    data_i      : in  std_ulogic_vector(15 downto 0); -- data in
55
    data_o      : out std_ulogic_vector(15 downto 0); -- data out
56
    -- clock generator --
57
    clkgen_en_o : out std_ulogic; -- enable clock generator
58
    clkgen_i    : in  std_ulogic_vector(07 downto 0);
59
    -- frequency generator --
60
    freq_gen_o  : out std_ulogic_vector(02 downto 0)  -- programmable frequency output
61
  );
62
end neo430_freq_gen;
63
 
64
architecture neo430_freq_gen_rtl of neo430_freq_gen is
65
 
66
  -- IO space: module base address --
67
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
68
  constant lo_abb_c : natural := index_size_f(freq_gen_size_c); -- low address boundary bit
69
 
70
  -- control reg bits --
71
  constant ctrl_en_ch0_c    : natural :=  0; -- r/w: enable NCO channel 0
72
  constant ctrl_en_ch1_c    : natural :=  1; -- r/w: enable NCO channel 1
73
  constant ctrl_en_ch2_c    : natural :=  2; -- r/w: enable NCO channel 2
74
  constant ctrl_ch0_prsc0_c : natural :=  3; -- r/w: prescaler select bit 0 for channel 0
75
  constant ctrl_ch0_prsc1_c : natural :=  4; -- r/w: prescaler select bit 1 for channel 0
76
  constant ctrl_ch0_prsc2_c : natural :=  5; -- r/w: prescaler select bit 2 for channel 0
77
  constant ctrl_ch1_prsc0_c : natural :=  6; -- r/w: prescaler select bit 0 for channel 1
78
  constant ctrl_ch1_prsc1_c : natural :=  7; -- r/w: prescaler select bit 1 for channel 1
79
  constant ctrl_ch1_prsc2_c : natural :=  8; -- r/w: prescaler select bit 2 for channel 1
80
  constant ctrl_ch2_prsc0_c : natural :=  9; -- r/w: prescaler select bit 0 for channel 2
81
  constant ctrl_ch2_prsc1_c : natural := 10; -- r/w: prescaler select bit 1 for channel 2
82
  constant ctrl_ch2_prsc2_c : natural := 11; -- r/w: prescaler select bit 2 for channel 2
83
 
84
  -- access control --
85
  signal acc_en : std_ulogic; -- module access enable
86
  signal addr   : std_ulogic_vector(15 downto 0); -- access address
87
  signal wren   : std_ulogic; -- word write enable
88
  signal rden   : std_ulogic; -- word read enable
89
 
90
  -- accessible regs --
91
  signal ctrl        : std_ulogic_vector(11 downto 0); -- r/w: control register
92
  type tuning_word_t is array (0 to 2) of std_ulogic_vector(15 downto 0);
93
  signal tuning_word : tuning_word_t; -- -/w: tuning word channel 0,1,2
94
 
95
  -- nco core --
96
  type phase_accu_t is array (0 to 2) of std_ulogic_vector(16 downto 0);
97
  signal nco_phase_accu : phase_accu_t;
98
  signal nco_prsc_tick  : std_ulogic_vector(2 downto 0);
99
 
100
begin
101
 
102
  -- Access Control -----------------------------------------------------------
103
  -- -----------------------------------------------------------------------------
104
  acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = freq_gen_base_c(hi_abb_c downto lo_abb_c)) else '0';
105
  addr   <= freq_gen_base_c(15 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 1) & '0'; -- word aligned
106
  wren   <= acc_en and wren_i;
107
  rden   <= acc_en and rden_i;
108
 
109
 
110
  -- Write access -------------------------------------------------------------
111
  -- -----------------------------------------------------------------------------
112
  wr_access: process(clk_i)
113
  begin
114
    if rising_edge(clk_i) then
115
      if (wren = '1') then
116
        if (addr = freq_gen_ctrl_addr_c) then
117
          ctrl(ctrl_en_ch0_c)    <= data_i(ctrl_en_ch0_c);
118
          ctrl(ctrl_en_ch1_c)    <= data_i(ctrl_en_ch1_c);
119
          ctrl(ctrl_en_ch2_c)    <= data_i(ctrl_en_ch2_c);
120
          ctrl(ctrl_ch0_prsc0_c) <= data_i(ctrl_ch0_prsc0_c);
121
          ctrl(ctrl_ch0_prsc1_c) <= data_i(ctrl_ch0_prsc1_c);
122
          ctrl(ctrl_ch0_prsc2_c) <= data_i(ctrl_ch0_prsc2_c);
123
          ctrl(ctrl_ch1_prsc0_c) <= data_i(ctrl_ch1_prsc0_c);
124
          ctrl(ctrl_ch1_prsc1_c) <= data_i(ctrl_ch1_prsc1_c);
125
          ctrl(ctrl_ch1_prsc2_c) <= data_i(ctrl_ch1_prsc2_c);
126
          ctrl(ctrl_ch2_prsc0_c) <= data_i(ctrl_ch2_prsc0_c);
127
          ctrl(ctrl_ch2_prsc1_c) <= data_i(ctrl_ch2_prsc1_c);
128
          ctrl(ctrl_ch2_prsc2_c) <= data_i(ctrl_ch2_prsc2_c);
129
        end if;
130
        if (addr = freq_gen_tw_ch0_addr_c) then
131
          tuning_word(0) <= data_i;
132
        end if;
133
        if (addr = freq_gen_tw_ch1_addr_c) then
134
          tuning_word(1) <= data_i;
135
        end if;
136
        if (addr = freq_gen_tw_ch2_addr_c) then
137
          tuning_word(2) <= data_i;
138
        end if;
139
      end if;
140
    end if;
141
  end process wr_access;
142
 
143
 
144
  -- NCO core (number controlled oscillator) ----------------------------------
145
  -- -----------------------------------------------------------------------------
146
  nco_core: process(clk_i)
147
  begin
148
    if rising_edge(clk_i) then
149
      -- enable external clock generator --
150
      clkgen_en_o <= ctrl(ctrl_en_ch0_c) or ctrl(ctrl_en_ch1_c) or ctrl(ctrl_en_ch2_c);
151
      -- NCOs --
152
      for i in 0 to 2 loop
153
        -- NCO clock enable --
154
        nco_prsc_tick(i) <= clkgen_i(to_integer(unsigned(ctrl(ctrl_ch0_prsc2_c + 3*i downto ctrl_ch0_prsc0_c + 3*i))));
155
        -- phase accu --
156
        if (ctrl(ctrl_en_ch0_c + i) = '0') then -- disabled
157
          nco_phase_accu(i) <= (others => '0');
158
        elsif (nco_prsc_tick(i) = '1') then -- enabled; wait for clock enable tick
159
          nco_phase_accu(i) <= std_ulogic_vector(unsigned(nco_phase_accu(i)) + unsigned('0' & tuning_word(i)));
160
        end if;
161
        -- output --
162
        freq_gen_o(i) <= nco_phase_accu(i)(16); -- MSB (carry_out) is output
163
      end loop; -- i - NCO channel
164
    end if;
165
  end process nco_core;
166
 
167
 
168
  -- Read access --------------------------------------------------------------
169
  -- -----------------------------------------------------------------------------
170
  rd_access: process(clk_i)
171
  begin
172
    if rising_edge(clk_i) then
173
      data_o <= (others => '0');
174
      if (rden = '1') then
175
--      if (addr = freq_gen_ctrl_addr_c) then
176
          data_o(ctrl_en_ch0_c)    <= ctrl(ctrl_en_ch0_c);
177
          data_o(ctrl_en_ch1_c)    <= ctrl(ctrl_en_ch1_c);
178
          data_o(ctrl_en_ch2_c)    <= ctrl(ctrl_en_ch2_c);
179
          data_o(ctrl_ch0_prsc0_c) <= ctrl(ctrl_ch0_prsc0_c);
180
          data_o(ctrl_ch0_prsc1_c) <= ctrl(ctrl_ch0_prsc1_c);
181
          data_o(ctrl_ch0_prsc2_c) <= ctrl(ctrl_ch0_prsc2_c);
182
          data_o(ctrl_ch1_prsc0_c) <= ctrl(ctrl_ch1_prsc0_c);
183
          data_o(ctrl_ch1_prsc1_c) <= ctrl(ctrl_ch1_prsc1_c);
184
          data_o(ctrl_ch1_prsc2_c) <= ctrl(ctrl_ch1_prsc2_c);
185
          data_o(ctrl_ch2_prsc0_c) <= ctrl(ctrl_ch2_prsc0_c);
186
          data_o(ctrl_ch2_prsc1_c) <= ctrl(ctrl_ch2_prsc1_c);
187
          data_o(ctrl_ch2_prsc2_c) <= ctrl(ctrl_ch2_prsc2_c);
188
--      end if;
189
      end if;
190
    end if;
191
  end process rd_access;
192
 
193
 
194
end neo430_freq_gen_rtl;

powered by: WebSVN 2.1.0

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