sine wave in vhdl

Guest #5872213
1
library ieee;
2
use ieee.std_logic_1164.all;
3
use ieee.numeric_std.all;
4

5
entity simple_sine_gen is
6
    port
7
    (
8
        clk     : in  std_ulogic;
9
        --
10
        sine    : out signed( 7 downto 0)
11
    );
12
end entity simple_sine_gen;
13

14
architecture rtl of simple_sine_gen is
15

16
    constant amplitude  : natural   := 15; -- max. 15
17
    constant frequency  : natural   :=  1; -- 1..16(?)
18
    --
19
    constant value      : natural   := 512;
20

21
    type reg_t is record
22
        x   : signed( 15 downto 0);
23
        xt  : signed( 15 downto 0);
24
        xa  : signed( 15 downto 0);
25
        y   : signed( 15 downto 0);
26
        yt  : signed( 15 downto 0);
27
        ya  : signed( 15 downto 0);
28
    end record;
29
    constant default_reg_c : reg_t := 
30
    (
31
        x   => to_signed(                 0, 16),
32
        xt  => ( others => '0'),
33
        xa  => ( others => '0'),
34
        y   => to_signed( amplitude * value, 16),
35
        yt  => ( others => '0'),
36
        ya  => ( others => '0')
37
    );
38

39
    signal r    : reg_t := default_reg_c;
40
    signal r_in : reg_t := default_reg_c;
41

42
begin
43

44

45
    comb: process( r)
46
        variable v : reg_t;
47
    begin
48
        v   := r;
49

50
        -- output
51
        sine <= resize( shift_right( v.x, 6), 8);
52

53
        -- calculation
54
        v.xa    := v.x + v.xt;
55
        v.xt    := resize( frequency * v.ya / value, 16);
56
        v.x     := v.x + v.xt; 
57

58
        v.ya    := v.y - v.yt;
59
        v.yt    := resize( frequency * v.xa / value, 16);
60
        v.y     := v.y - v.yt;
61

62
        r_in <= v;
63
    end process;
64

65

66
    seq : process
67
    begin
68
        wait until rising_edge(clk);
69
        r <= r_in;
70
    end process;
71

72
end architecture rtl;

Can you explain how it works?
Guest #5906510
Thanks to Duke, you now know about the way to realise 2 stage diff 
equations in VHDL. Anyway you should have been able to figure that out 
yourself.

Reply

A question of your own? This thread is over 7 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [vhdl]VHDL-Code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.