nco.vhd

    1  -- title:              Numeric Controlled Oscillator
    2  -- author:             Sebastian Weiss
    3  -- last change:        22.10.13
    4 
    5 library IEEE;
    6 use IEEE.std_logic_1164.all;
    7 use IEEE.numeric_std.all;
    8 
    9 entity nco is
   10         generic
   11         (
   12                 A       : natural := 16;                        -- amplitude resolution
   13                 F       : natural := 24;                        -- frequency resolution
   14                 P       : natural := 24;                        -- phase resolution
   15                 N       : natural := 14                         -- number of stages
   16         );
   17 
   18         port
   19         (
   20                 clk     : in std_logic;                         -- signal processing clock
   21                 fcw     : in unsigned(F-1 downto 0);
   22                 sin     : out signed(A-1 downto 0);             -- sine output
   23                 cos     : out signed(A-1 downto 0)              -- cosine output
   24         );
   25 end entity;
   26 
   27 architecture behavioral of nco is
   28 type q_vector is array(natural range <>) of std_logic_vector(1 downto 0);
   29 signal cordic_phi       : unsigned(P-3 downto 0);
   30 signal cordic_sin       : signed(A-1 downto 0);
   31 signal cordic_cos       : signed(A-1 downto 0);
   32 signal phi              : unsigned(P-1 downto 0) := (others => '0');
   33 signal phase            : unsigned(P-2 downto 0);
   34 signal q                : q_vector(N+1 downto 0) := (others => (others => '0'));
   35 begin
   36 
   37         cordic : entity work.cordic
   38                 generic map(
   39                         A       => A,
   40                         P       => P,
   41                         N       => N
   42                 )
   43                 port map(
   44                         clk     => clk,
   45                         phi     => cordic_phi,
   46                         sin     => cordic_sin,
   47                         cos     => cordic_cos
   48                 );
   49 
   50         process
   51         begin
   52                 wait until rising_edge(clk);
   53                 phi <= phi + fcw;
   54 
   55                 if phi(P-2) = '1' then
   56                         phase <= 0 - phi(P-2 downto 0);
   57                 else
   58                         phase <= phi(P-2 downto 0);
   59                 end if;
   60 
   61                 q(0) <= phi(P-1) & phi(P-2);
   62                 for i in 1 to N+1 loop
   63                         q(i) <= q(i-1);
   64                 end loop;
   65 
   66                 if q(N+1) = "00" then
   67                         sin <= cordic_sin;
   68                         cos <= cordic_cos;
   69                 elsif q(N+1) = "01" then
   70                         sin <= cordic_sin;
   71                         cos <= -cordic_cos;
   72                 elsif q(N+1) = "10" then
   73                         sin <= -cordic_sin;
   74                         cos <= -cordic_cos;
   75                 elsif q(N+1) = "11" then
   76                         sin <= -cordic_sin;
   77                         cos <= cordic_cos;
   78                 end if;
   79         end process;
   80 
   81                 cordic_phi <= phase(P-3 downto 0);
   82 end behavioral;
   83 

This page was generated using GHDL 0.29 (20100109) [Sokcho edition], a program written by Tristan Gingold