forked from gregani/la16fw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainmodule.vhd
360 lines (334 loc) · 13.6 KB
/
mainmodule.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
--
-- This file is part of the la16fw project.
--
-- Copyright (C) 2014-2015 Gregor Anich
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--
----------------------------------------------------------------------------------
--
-- connect all stuff together
--
----------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity mainmodule is
generic(
-- spi addresses
ADDRESS_FPGA_VERSION : integer := 0;
ADDRESS_STATUS_CONTROL : integer := 1;
ADDRESS_CHANNEL_SELECT_LO : integer := 2;
ADDRESS_CHANNEL_SELECT_HI : integer := 3;
ADDRESS_SAMPLE_RATE_DIVISOR : integer := 4;
ADDRESS_LED_BRIGHTNESS : integer := 5;
ADDRESS_SAMPLE_CLOCK_CONTROL : integer := 10;
FPGA_VERSION : integer := 16;
-- other constants
tick_1M_div : integer := 48 -- divider to get 1MHz from 48MHz clk
);
port(
-- always used
clk_in : in std_logic; --external clock input
led : out std_logic;
spi_ss_n : in std_logic;
spi_sclk : in std_logic;
spi_mosi : in std_logic;
spi_miso : out std_logic;
-- parallel data bus to fx2 chip
fifo_clk : in std_logic; -- 48MHz clock for the fifo bus
fifo_empty : out std_logic; -- flag the fx2 whether the fifo is empty (fx2 RDY0 pin)
fifo_read_n : in std_logic; -- low while the fx2 reads data, high otherwise
fifo_data : out std_logic_vector(15 downto 0);
-- logic inputs
logic_data : in std_logic_vector(15 downto 0)
-- logic_data : out std_logic_vector(15 downto 0)
);
end mainmodule;
architecture behavioral of mainmodule is
-- reset
signal reset : std_logic := '1';
signal reset_count : unsigned(4 downto 0) := (others=>'1');
signal reset_dcm : std_logic;
-- clock
signal clk : std_logic; -- 48MHz clock signal from the selected DCM
signal clk_a, clk_b : std_logic;
signal clk_c, clk_d : std_logic;
signal clk_100M : std_logic; -- 100MHz clock from dcm1
signal clk_100M_locked : std_logic;
signal clk_160M : std_logic; -- 160MHz clock from dcm2
signal clk_160M_locked : std_logic;
signal clk_user : std_logic; -- user clock
signal clk_user_2x : std_logic; -- 2x user clock from dcm3
signal clk_user_2x_locked : std_logic;
signal clk_user_4x : std_logic; -- 4x user clock from dcm4
signal clk_user_4x_locked : std_logic;
signal tick_1M : std_logic := '0';
signal tick_1M_count : unsigned(5 downto 0) := (others=>'0');
-- internal data bus FIXME: spi
signal spi_enable_write : std_logic;
signal spi_enable_read : std_logic;
signal spi_addr : std_logic_vector(6 downto 0);
signal spi_data_out : std_logic_vector(7 downto 0);
signal spi_data_in : std_logic_vector(7 downto 0);
-- status/control
signal led_brightness : std_logic_vector(7 downto 0);
signal led_invert : std_logic;
signal sample_run : std_logic := '0'; -- set to '1' to sample data
signal status_bit6 : std_logic;
signal sample_rate_divisor : std_logic_vector(7 downto 0); -- sample rate is base clock / (rate_divisor + 1)
signal sample_clk_sel : unsigned(1 downto 0); -- 0: clk_100M, 1: clk_160M, 2: user 2x, 3: user 4x
signal sample_clk : std_logic; -- sample clock, 100 or 160MHz
signal selected_channels : std_logic_vector(15 downto 0);
-- fifo to buffer logic data (from the core generator)
signal fifo_reset : std_logic;
signal fifo_almost_empty : std_logic;
signal fifo_data_in : std_logic_vector(15 downto 0);
signal fifo_data_out : std_logic_vector(15 downto 0);
signal fifo_enable_read : std_logic;
signal fifo_enable_write : std_logic;
signal fifo_full : std_logic;
signal fifo_almost_full : std_logic;
-- debug
signal debug : std_logic_vector(15 downto 0);
begin
-- debug
--logic_data <= debug;
debug(0) <= sample_run;
--debug(2) <= fifo_almost_empty;
--debug(2) <= not fifo_read_n;
debug(2) <= '1' when (unsigned(fifo_data_out) = 165) else '0';
-- clock units: generates 100MHz and 160MHz from 48MHz input
clock_100M_inst : entity work.clock
generic map(
CLK_FAST_DIV => 12,
CLK_FAST_MUL => 25,
STARTUP_WAIT => true
)
port map(
clk_in => clk_in,
reset => reset_dcm,
clk => clk_a,
clk_fb => clk_a,
clk_fast => clk_100M,
locked => clk_100M_locked
);
clock_160M_inst : entity work.clock
generic map(
CLK_FAST_DIV => 3,
CLK_FAST_MUL => 10,
STARTUP_WAIT => true
)
port map(
clk_in => clk_in,
reset => reset_dcm,
clk => clk_b,
clk_fb => clk_b,
clk_fast => clk_160M,
locked => clk_160M_locked
);
-- clock_user1_inst : entity work.clock
-- generic map(
-- CLK_FAST_DIV => 1,
-- CLK_FAST_MUL => 2
-- )
-- port map(
-- clk_in => clk_user,
-- reset => reset_dcm,
-- clk => clk_c,
-- clk_fb => clk_c,
-- clk_fast => clk_user_2x,
-- locked => clk_user_2x_locked
-- );
-- clock_user2_inst : entity work.clock
-- generic map(
-- CLK_FAST_DIV => 1,
-- CLK_FAST_MUL => 4
-- )
-- port map(
-- clk_in => clk_user,
-- reset => reset_dcm,
-- clk => clk_d,
-- clk_fb => clk_d,
-- clk_fast => clk_user_4x,
-- locked => clk_user_4x_locked
-- );
clk_user <= logic_data(15);
clockmux_inst : entity work.clockmux
generic map(
n_log2 => 2
)
port map(
clk_ctl => clk,
clk_sel => std_logic_vector(sample_clk_sel),
clk_in(0) => clk_100M,
clk_in(1) => clk_160M,
clk_in(2) => '0',--clk_user_2x,
clk_in(3) => '0',--clk_user_4x,
clk_out => sample_clk
);
clk <= clk_in; --FIXME: which clock to use for logic?
-- FIXME: use a BUFGMUX or custom circuit to avoid glitches
--sample_clk <= clk_100M when (sample_clk_sel = '0') else clk_160M;
-- led unit: creates pwm signal for the led from 1MHz tick
led_inst : entity work.led
port map(
clk => clk,
reset => reset,
tick_1M => tick_1M,
brightness => led_brightness,
invert => led_invert,
led => led
);
-- spi unit: provides the control interface to the fx2 chip
spi_inst : entity work.spi
port map(
clk => clk,
reset => reset,
enable_write => spi_enable_write,
enable_read => spi_enable_read,
addr => spi_addr,
data_out => spi_data_out,
data_in => spi_data_in,
ss_n => spi_ss_n,
sclk => spi_sclk,
mosi => spi_mosi,
miso => spi_miso
);
-- fifo unit
-- used for buffering the data between logic input and fx2
-- output is connected to the fx2
-- input is connected to the sample unit
fifo_inst : entity work.fifo
port map(
reset => fifo_reset,
clk_write => sample_clk,
clk_read => fifo_clk,
data_in => fifo_data_in,
enable_write => fifo_enable_write,
enable_read => fifo_enable_read,
data_out => fifo_data_out,
full => fifo_full,
almost_full => fifo_almost_full,
almost_empty => fifo_almost_empty
);
-- for some reason the fx2 reads one word too much if empty is used
fifo_empty <= fifo_almost_empty or (not sample_run);
fifo_data <= fifo_data_out;
fifo_enable_read <= (not fifo_read_n);
-- sample logic inputs
sample_inst : entity work.sample
port map(
sample_clk => sample_clk,
sample_run => sample_run,
sample_rate_divisor => sample_rate_divisor,
channel_select => selected_channels,
logic_data => logic_data,
--logic_data => (others=>'0'),
fifo_data => fifo_data_in,
fifo_reset => fifo_reset,
fifo_write => fifo_enable_write,
fifo_full => fifo_full,
fifo_almost_full => fifo_almost_full
);
-- create internal reset signal from 48MHz input clock
process(clk_in)
begin
if rising_edge(clk_in) then
if (reset_count = 0) then
reset <= '0';
else
reset <= '1';
end if;
if (reset_count /= 0) and
(((clk_100M_locked = '1') and (clk_160M_locked = '1')) or
(reset_count /= 5)) then
reset_count <= reset_count - 1;
end if;
end if;
end process;
-- make sure dcm clock starts/runs while reset is still '1'
reset_dcm <= '1' when (reset_count > 2**reset_count'length-5) else '0';
-- create 1MHz tick from 48MHz clock
process(clk)
begin
if rising_edge(clk) then
-- create 1MHz tick
if (tick_1M_count = 0) then
tick_1M <= '1';
tick_1M_count <= to_unsigned(tick_1M_div - 1, tick_1M_count'length);
else
tick_1M <= '0';
tick_1M_count <= tick_1M_count - 1;
end if;
end if;
end process;
-- handle reset and spi
process(clk)
begin
if rising_edge(clk) then
if (reset = '1') then
sample_clk_sel <= (others=>'0');
spi_data_in <= (others=>'0');
-- init status/control
led_brightness <= (others=>'0');
led_invert <= '0';
sample_run <= '0';
status_bit6 <= '0';
selected_channels <= (others=>'1');
sample_rate_divisor <= (others=>'0');
else
-- handle spi
spi_data_in <= (others=>'0');
if (spi_enable_read = '1') then
if (unsigned(spi_addr) = ADDRESS_FPGA_VERSION) then
spi_data_in <= std_logic_vector(to_unsigned(FPGA_VERSION, spi_data_in'length));
elsif (unsigned(spi_addr) = ADDRESS_STATUS_CONTROL) then
spi_data_in <= "0" & status_bit6 & "00100" & sample_run;
elsif (unsigned(spi_addr) = ADDRESS_CHANNEL_SELECT_LO) then
spi_data_in <= selected_channels(7 downto 0);
elsif (unsigned(spi_addr) = ADDRESS_CHANNEL_SELECT_HI) then
spi_data_in <= selected_channels(15 downto 8);
elsif (unsigned(spi_addr) = ADDRESS_SAMPLE_RATE_DIVISOR) then
spi_data_in <= sample_rate_divisor;
elsif (unsigned(spi_addr) = ADDRESS_LED_BRIGHTNESS) then
spi_data_in <= led_brightness;
elsif (unsigned(spi_addr) = ADDRESS_SAMPLE_CLOCK_CONTROL) then
spi_data_in <= "0000000" & sample_clk_sel(0);
end if;
end if;
if (spi_enable_write = '1') then
if (unsigned(spi_addr) = ADDRESS_STATUS_CONTROL) then
sample_run <= spi_data_out(0);
status_bit6 <= spi_data_out(6);
led_invert <= spi_data_out(0);
elsif (unsigned(spi_addr) = ADDRESS_CHANNEL_SELECT_LO) then
selected_channels(7 downto 0) <= spi_data_out;
elsif (unsigned(spi_addr) = ADDRESS_CHANNEL_SELECT_HI) then
selected_channels(15 downto 8) <= spi_data_out;
elsif (unsigned(spi_addr) = ADDRESS_SAMPLE_RATE_DIVISOR) then
sample_rate_divisor <= spi_data_out;
elsif (unsigned(spi_addr) = ADDRESS_LED_BRIGHTNESS) then
led_brightness <= spi_data_out;
elsif (unsigned(spi_addr) = ADDRESS_SAMPLE_CLOCK_CONTROL) then
sample_clk_sel(0) <= spi_data_out(0);
elsif (unsigned(spi_addr) = 123) then--foo
sample_clk_sel(1) <= spi_data_out(0);--bogus
end if;
end if;
end if;
end if;
end process;
end behavioral;