2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
5 -- Disable next libraries for simulation in GHDL
7 --use unisim.vcomponents.all;
9 use work.lx_dad_pkg.all;
11 -- lx_dad_top - wires the modules with the outside world
13 -- ======================================================
14 -- MASTER CPU EXTERNAL MEMORY BUS
15 -- ======================================================
17 -- Master cpu memory bus has the following wires:
19 -- - address[15..0] The address, used to mark chip enable
20 -- - data_in[31..0] The data coming to bus
21 -- - data_out[31..0] The data coming from bus, multiplexed
22 -- - bls[3..0] Write enable for respective bytes
28 --clk_cpu : in std_logic;
29 clk_50m : in std_logic;
31 cs0_xc : in std_logic;
34 bls : in std_logic_vector(3 downto 0);
35 address : in std_logic_vector(15 downto 0);
36 data : inout std_logic_vector(31 downto 0);
39 -- signal connected to external JK FF
40 event_jk_j : out std_logic;
41 -- signals to image sensor
44 phi_rst : out std_logic;
45 LED_1 : out std_logic;
46 sck_o : out std_logic;
47 cnv_o : out std_logic;
48 phist : out std_logic;
55 architecture Behavioral of lx_dad_top is
58 signal reset_s : std_logic;
59 signal init_s : std_logic;
60 -- Peripherals on the memory buses
62 signal example_out_s : std_logic_vector(31 downto 0);
63 signal example_ce_s : std_logic;
64 -- Measurement (Master)
65 signal meas_out_s : std_logic_vector(31 downto 0);
66 signal meas_ce_s : std_logic;
68 signal sensor_ce_s : std_logic;
69 signal sensor_out_s : std_logic_vector(31 downto 0);
71 signal sens_mem_ce_s : std_logic;
72 signal sens_mem_out_s : std_logic_vector(31 downto 0);
74 signal sens_adr_s : std_logic_vector(10 downto 0);
75 signal sens_data_s : std_logic_vector(31 downto 0);
76 signal sens_i_bls_s : std_logic_vector(3 downto 0);
77 signal sens_mem_int_ce_s : std_logic;
79 -- Signals for external bus transmission
80 signal data_i_s : std_logic_vector(31 downto 0);
81 signal data_o_s : std_logic_vector(31 downto 0);
82 -- Signals for internal transaction
83 signal last_address_s : std_logic_vector(15 downto 0);
84 signal next_last_address_s : std_logic_vector(15 downto 0);
85 signal next_address_hold_s : std_logic;
86 signal address_hold_s : std_logic;
87 signal last_rd_s : std_logic;
88 signal next_last_rd_s : std_logic;
89 signal last_bls_s : std_logic_vector(3 downto 0); -- prev bls_f_s (active 1)
90 signal next_last_bls_s : std_logic_vector(3 downto 0);
92 -- Reading logic for Master CPU:
93 -- Broadcast rd only till ta (transaction acknowledge)
94 -- is received, then latch the data till the state of
95 -- rd or address changes
97 -- Data latching is synchronous - it's purpose is to
98 -- provide stable data for CPU on the bus
99 signal cs0_xc_f_s : std_logic;
100 signal rd_f_s : std_logic; -- Filtered RD
101 signal i_rd_s : std_logic; -- Internal bus RD (active 1)
102 signal next_last_i_rd_s : std_logic;
103 signal last_i_rd_s : std_logic; -- Delayed RD bus, used for latching
105 signal address_f_s : std_logic_vector(15 downto 0); -- Filtered address
107 signal data_f_s : std_logic_vector(31 downto 0); -- Filterred input data
109 signal data_read_s : std_logic_vector(31 downto 0); -- Latched read data
110 signal next_data_read_s : std_logic_vector(31 downto 0);
113 signal bls_f_s : std_logic_vector(3 downto 0); -- Filtered BLS (active 1)
114 signal i_bls_s : std_logic_vector(3 downto 0); -- Internal BLS (active 1)
115 signal next_i_bls_s : std_logic_vector(3 downto 0);
117 signal data_write_s : std_logic_vector(31 downto 0); -- Data broadcasted to write
118 signal next_data_write_s : std_logic_vector(31 downto 0);
120 -- signal s0 : std_logic;
121 -- signal s1 : std_logic;
122 -- signal s2 : std_logic;
125 attribute REGISTER_DUPLICATION : string;
126 attribute REGISTER_DUPLICATION of rd : signal is "NO";
127 attribute REGISTER_DUPLICATION of rd_f_s : signal is "NO";
128 attribute REGISTER_DUPLICATION of bls : signal is "NO";
129 attribute REGISTER_DUPLICATION of bls_f_s : signal is "NO";
130 attribute REGISTER_DUPLICATION of address : signal is "NO";
131 attribute REGISTER_DUPLICATION of address_f_s : signal is "NO";
132 attribute REGISTER_DUPLICATION of cs0_xc : signal is "NO";
133 attribute REGISTER_DUPLICATION of cs0_xc_f_s : signal is "NO";
137 -- Example connection
138 memory_bus_example: bus_example
143 ce_i => example_ce_s,
145 address_i => address_f_s(11 downto 0),
147 data_o => example_out_s
150 -- additional externally connected signals goes there
154 --Sensor clock generator
169 mem_o => sens_data_s,
170 addr_o => sens_adr_s,
171 bls_o => sens_i_bls_s,
172 ce_o => sens_mem_int_ce_s,
173 addr_i => address_f_s(1 downto 0),
177 data_o => sensor_out_s
181 --sensor memory connection
182 memory_bus_sensormem: bus_sensor
186 ce_i => sens_mem_ce_s,
189 address_i => address_f_s(10 downto 0),
191 data_o => sens_mem_out_s,
192 ce_a_i => sens_mem_int_ce_s,
193 adr_a_i => sens_adr_s,
194 bls_a_i => sens_i_bls_s,
195 dat_a_i => sens_data_s
199 memory_bus_measurement: bus_measurement
205 address_i => address_f_s(1 downto 0),
224 data_i_s <= data_write_s;
231 process(cs0_xc_f_s, rd_f_s, last_rd_s, last_i_rd_s,
232 bls_f_s, last_bls_s, data_f_s, data_write_s,
233 data_o_s, data_read_s, last_address_s, address_f_s)
236 next_address_hold_s <= '0';
238 -- Check if we have chip select
239 if cs0_xc_f_s = '1' then
244 if last_rd_s = '0' or (last_address_s /= address_f_s) then
246 next_last_i_rd_s <= '1';
249 next_last_i_rd_s <= '0';
252 if last_i_rd_s = '1' then
253 -- Latch data we just read - they are valid in this cycle
254 next_data_read_s <= data_o_s;
256 next_data_read_s <= data_read_s;
259 -- -- Not reading, anything goes
260 -- data_read_s <= (others => 'X');
261 next_data_read_s <= data_read_s;
263 next_last_i_rd_s <= '0';
266 next_last_rd_s <= rd_f_s;
268 -- Data for write are captured only when BLS signals are stable
269 if bls_f_s /= "0000" then
270 next_data_write_s <= data_f_s;
271 next_address_hold_s <= '1';
273 next_data_write_s <= data_write_s;
276 if (bls_f_s /= "0000") or (rd_f_s = '1') then
277 next_last_address_s <= address_f_s;
279 next_last_address_s <= last_address_s;
282 next_last_rd_s <= '0';
284 next_last_i_rd_s <= '0';
286 next_i_bls_s <= "0000";
287 next_data_write_s <= data_write_s;
288 next_data_read_s <= data_read_s;
289 next_last_address_s <= last_address_s;
292 -- Data for write are captured at/before BLS signals are negated
293 -- and actual write cycle takes place exacly after BLS negation
294 if ((last_bls_s and not bls_f_s) /= "0000") or
295 ((last_bls_s /= "0000") and (cs0_xc_f_s = '0')) then
296 next_i_bls_s <= last_bls_s;
297 next_last_bls_s <= "0000";
298 next_address_hold_s <= '1';
300 next_i_bls_s <= "0000";
301 if cs0_xc_f_s = '1' then
302 next_last_bls_s <= bls_f_s;
304 next_last_bls_s <= "0000" ;
315 wait until clk_50m = '1' and clk_50m'event;
317 address_hold_s <= next_address_hold_s;
319 -- Synchronized external signals with main clock domain
320 cs0_xc_f_s <= not cs0_xc;
324 if address_hold_s = '0' then
325 address_f_s <= address;
327 address_f_s <= next_last_address_s;
330 -- Synchronoust state andvance to next period
331 last_bls_s <= next_last_bls_s;
332 last_rd_s <= next_last_rd_s;
333 i_bls_s <= next_i_bls_s;
334 last_i_rd_s <= next_last_i_rd_s;
335 data_write_s <= next_data_write_s;
336 last_address_s <= next_last_address_s;
337 data_read_s <= next_data_read_s;
341 -- Do the actual wiring here
343 process(cs0_xc_f_s, i_bls_s, address_f_s, example_out_s, meas_out_s, sensor_out_s, sens_mem_out_s, i_rd_s, last_i_rd_s)
346 -- Inactive by default
350 sens_mem_ce_s <= '0';
351 data_o_s <= (others => '0');
353 if i_rd_s = '1' or i_bls_s /= "0000" then
355 -- Memory Map (16-bit address @ 32-bit each)
357 -- Each address is seen as 32-bit entry now
358 -- 0x0000 - 0x0FFF: Example memory
359 -- 0x1000 - 0x1003: Sensor timing
360 -- 0x1FFC - 0x1FFF: Measurement
361 -- 0x2000 - 0x3FFF: sensor memory
362 -- 0x4000 - 0x8FFF: Free space
364 if address_f_s < "0001000000000000" then -- Tumbl
366 elsif address_f_s(15 downto 2) = "00010000000000" then -- Sensor timing
368 elsif address_f_s(15 downto 2) = "00011111111111" then -- Measurement
370 elsif address_f_s > "0001111111111111" and address_f_s < "0100000000000000" then -- sensor data
371 sens_mem_ce_s <= '1';
376 if last_i_rd_s = '1' then
377 if address_f_s < "0001000000000000" then -- Tumbl
378 data_o_s <= example_out_s;
379 elsif address_f_s(15 downto 2) = "00011111111111" then -- Measurement
380 data_o_s <= meas_out_s;
381 elsif address_f_s(15 downto 2) = "00010000000000" then -- Sensor timing
382 data_o_s <= sensor_out_s;
383 elsif address_f_s > "0000111111111111" and address_f_s < "0100000000000000" then -- sensor data
384 data_o_s <= sens_mem_out_s;
390 -- If RD and BLS is not high, we must keep DATA at high impedance
391 -- or the FPGA collides with SDRAM (damaging each other)
393 process(cs0_xc, rd, data_read_s)
396 -- CS0 / RD / BLS are active LOW
397 if cs0_xc = '0' and rd = '0' then
398 -- Don't risk flipping (between data_o_s and latched data_read_s, it's better to wait)
399 -- Maybe check this later.
400 -- if last_i_rd_s = '1' then
407 data <= (others => 'Z');