-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2s.vhd
66 lines (57 loc) · 1.66 KB
/
i2s.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity i2s is
generic (
gClockHz : integer := 1e8; -- system clock frequency in Hz
gSamplingHz : integer := 44100 -- sampling rate in HZ
);
port (
iClock : in std_logic; -- system clock
iReset : in std_logic; -- assumed to be held high
-- I2S interface
oBitClock : out std_logic;
oWordClock : out std_logic;
oDataLine : out std_logic;
iLeftChannel : in signed(31 downto 0);
iRightChannel : in signed(31 downto 0)
);
end entity;
architecture etc of i2s is
constant CLOCK_TARGET : integer := gClockHz / (gSamplingHz * 32 * 2 * 2);
signal ClockCounter : integer := 0;
signal Index : integer := 0;
signal Phase : std_logic := '0';
signal Channel : std_logic := '0';
signal Message : signed(31 downto 0) := "00000000000000000000000000000000";
begin
oBitClock <= Phase;
oWordClock <= Channel;
oDataLine <= Message(Index);
process(iClock)
begin
if (rising_edge(iClock)) then
if (ClockCounter = CLOCK_TARGET) then
ClockCounter <= 0;
Phase <= not Phase;
if (Phase = '1') then
if (iReset = '0' or Index = 31) then
Index <= 0;
if (Channel = '0') then
Message <= iLeftChannel;
else
Message <= iRightChannel;
end if;
else
Index <= Index + 1;
if (Index = 30) then
Channel <= not Channel;
end if;
end if;
end if;
else
ClockCounter <= ClockCounter + 1;
end if;
end if;
end process;
end architecture;