-
Notifications
You must be signed in to change notification settings - Fork 0
/
piano.vhd trial
254 lines (225 loc) · 8.71 KB
/
piano.vhd trial
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
--
-- piano.vhd - FPGA Piano
--
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
library UNISIM;
use UNISIM.VComponents.all;
entity piano is
port ( CLK_IN : in std_logic;
pb_in : in std_logic_vector(3 downto 0);
switch_in : in std_logic_vector(7 downto 0);
SPK_N : out std_logic;
SPK_P : out std_logic;
led_out : out std_logic_vector(7 downto 0);
digit_out : out std_logic_vector(3 downto 0);
seg_out : out std_logic_vector(7 downto 0)
);
end piano;
architecture Behavioral of piano is
-- Xilinx Native Components
component BUFG port ( I : in std_logic; O : out std_logic); end component;
component IBUFG port ( I : in std_logic; O : out std_logic); end component;
component IBUF port ( I : in std_logic; O : out std_logic); end component;
component OBUF port ( I : in std_logic; O : out std_logic); end component;
component MMCME2_BASE
generic( CLKFBOUT_MULT_F : real;
DIVCLK_DIVIDE : integer;
CLKOUT0_DIVIDE_F : real
);
port ( CLKIN1 : in std_logic;
CLKFBIN : in std_logic;
RST : in std_logic;
PWRDWN : in std_logic;
CLKOUT0 : out std_logic;
CLKOUT0B : out std_logic;
CLKOUT1 : out std_logic;
CLKOUT1B : out std_logic;
CLKOUT2 : out std_logic;
CLKOUT2B : out std_logic;
CLKOUT3 : out std_logic;
CLKOUT3B : out std_logic;
CLKOUT4 : out std_logic;
CLKOUT5 : out std_logic;
CLKOUT6 : out std_logic;
CLKFBOUT : out std_logic;
CLKFBOUTB : out std_logic;
LOCKED : out std_logic);
end component;
-- My Components:
-- Clock Divider
component clk_dvd
port (
CLK : in std_logic;
RST : in std_logic;
DIV : in std_logic_vector(15 downto 0);
EN : in std_logic;
CLK_OUT : out std_logic;
ONE_SHOT: out std_logic
);
end component;
-- Note decoder
component note_gen
port (
CLK : in std_logic;
RST : in std_logic;
NOTE_IN : in std_logic_vector(4 downto 0);
DIV : out std_logic_vector(15 downto 0)
);
end component;
-- 7-Segment Display for Notes
component seven_seg
port ( CLK : in std_logic;
RST : in std_logic;
NOTE_IN : in std_logic_vector(4 downto 0);
SCAN_EN : in std_logic;
DIGIT : out std_logic_vector(3 downto 0);
SEG : out std_logic_vector(7 downto 0)
);
end component;
-- Signals
signal CLK : std_logic; -- 50MHz clock after DCM and BUFG
signal CLK0 : std_logic; -- 50MHz clock from pad
signal CLK_BUF : std_logic; -- 50MHz clock after IBUF
signal GND : std_logic;
signal RST : std_logic;
signal PB : std_logic_vector(3 downto 0); -- Pushbuttons after ibufs
signal digit_l : std_logic_vector(3 downto 0); -- 7-seg digit MUX before obuf
signal switch : std_logic_vector(7 downto 0); -- Toggle switches after ibuf
signal led : std_logic_vector(7 downto 0); -- LEDs after ibufs
signal seg_l : std_logic_vector(7 downto 0); -- 7-seg segment select before obuf.
signal one_mhz : std_logic; -- 1MHz Clock
signal one_mhz_1 : std_logic; -- pulse with f=1 MHz created by divider
signal clk_10k_1 : std_logic; -- pulse with f=10kHz created by divider
signal div : std_logic_vector(15 downto 0); -- variable clock divider for loadable counter
signal note_in : std_logic_vector(4 downto 0); -- output of user interface. Current Note
signal note_next : std_logic_vector(4 downto 0); -- Buffer holding current Note
signal note_sel : std_logic_vector(3 downto 0); -- Encoding of switches.
signal div_1 : std_logic; -- 1MHz pulse
signal sound : std_logic; -- Output of Loadable Clock Divider. Sent to Speaker if note is playing.
signal SPK : std_logic; -- Output for Speaker fed to OBUF
signal cnt : std_logic_vector(30 downto 0);
begin
GND <= '0';
RST <= PB(0); -- push button one is the reset
led(1) <= RST; -- This is just to make sure our design is running.
-- Combinational logic to turn the sound on and off
process (div, sound) begin
if (div = x"0000") then
SPK <= GND;
else
SPK <= sound;
end if;
end process;
-- Speaker output
SPK_OBUF_INST : OBUF port map (I=>SPK, O=>SPK_N);
SPK_P <= GND;
-- Input/Output Buffers
loop0 : for i in 0 to 3 generate
pb_ibuf : IBUF port map(I => pb_in(i), O => PB(i));
dig_obuf : OBUF port map(I => digit_l(i), O => digit_out(i));
end generate ;
loop1 : for i in 0 to 7 generate
swt_obuf : IBUF port map(I => switch_in(i), O => switch(i));
led_obuf : OBUF port map(I => led(i), O => led_out(i));
seg_obuf : OBUF port map(I => seg_l(i), O => seg_out(i));
end generate ;
-- Global Clock Buffers
-- Pad -> DCM
CLKIN_IBUFG_INST : IBUFG
port map (I=>CLK_IN,
O=>CLK0);
-- DCM -> CLK
CLK0_BUFG_INST : BUFG
port map (I=>CLK_BUF,
O=>CLK);
-- MMCM for Clock deskew and frequency synthesis
MMCM_INST : MMCME2_BASE
generic map(
CLKFBOUT_MULT_F =>10.0,
DIVCLK_DIVIDE=>1,
CLKOUT0_DIVIDE_F =>10.0
)
port map (CLKIN1=>CLK0,
CLKFBIN=>CLK,
RST=>RST,
PWRDWN=>GND,
CLKOUT0=>CLK_BUF,
CLKOUT0B=>open,
CLKOUT1=>open,
CLKOUT1B=>open,
CLKOUT2=>open,
CLKOUT2B=>open,
CLKOUT3=>open,
CLKOUT3B=>open,
CLKOUT4=>open,
CLKOUT5=>open,
CLKOUT6=>open,
CLKFBOUT=>open,
CLKFBOUTB=>open,
LOCKED=>led(0)
);
-- Divide 100Mhz to 1Mhz clock
DIV_1M : clk_dvd
port map ( CLK => CLK,
RST => RST,
DIV => x"0032", -- 50
EN => '1',
CLK_OUT => one_mhz,
ONE_SHOT => one_mhz_1
);
-- Divide 1Mhz to Various frequencies for the notes.
DIV_NOTE : clk_dvd
port map ( CLK => CLK,
RST => RST,
DIV => div,
EN => one_mhz_1,
CLK_OUT => sound,
ONE_SHOT => div_1
);
-- Divide 1Mhz to 10k
DIV_10k : clk_dvd
port map ( CLK => CLK,
RST => RST,
DIV => x"0032", -- 50
EN => one_mhz_1,
CLK_OUT => open,
ONE_SHOT => clk_10k_1
);
-- Translate Encoded Note to clock divider for 1MHz clock.
note_gen_inst : note_gen
port map ( CLK => CLK,
RST => RST,
NOTE_IN => note_in,
DIV => div
);
-- Wire up seven-seg controller to display current note.
seven_seg_inst : seven_seg
port map ( CLK => CLK,
RST => RST,
NOTE_IN => note_in,
SCAN_EN => clk_10k_1,
DIGIT => digit_l,
SEG => seg_l
);
-- User Interface
note_in <= note_next;
process (CLK,RST)
begin
if (RST = '1') then
note_next <= (others => '0');
cnt <= x"00000000";
elsif (CLK'event and CLK = '1') then
if (switch(0) = '1') then
cnt <= cnt + 1;
end if;
if (cnt < x"00010000") then
note_next <= "10000";
elsif (cnt > x"00010010") and (cnt < x"00011000") then
note_next <= "00100";
end if;
end if;
end process;
end Behavioral;