2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_arith.all;
4 use ieee.std_logic_unsigned.all;
5 use ieee.numeric_std.all;
8 use unisim.vcomponents.all;
10 use work.lx_dad_pkg.all;
12 -- lx_dad_top - wires the modules with the outside world
14 -- ======================================================
15 -- MASTER CPU EXTERNAL MEMORY BUS
16 -- ======================================================
18 -- Master cpu memory bus has the following wires:
20 -- - address[15..0] The address, used to mark chip enable
21 -- - data_in[31..0] The data coming to bus
22 -- - data_out[31..0] The data coming from bus, multiplexed
23 -- - bls[3..0] Write enable for respective bytes
29 --clk_cpu : in std_logic;
30 clk_50m : in std_logic;
32 cs0_xc : in std_logic;
35 bls : in std_logic_vector(3 downto 0);
36 address : in std_logic_vector(15 downto 0);
37 data : inout std_logic_vector(31 downto 0);
40 -- signal connected to external JK FF
41 event_jk_j : out std_logic
45 architecture Behavioral of lx_dad_top is
48 signal reset_s : std_logic;
49 signal init_s : std_logic;
50 -- Peripherals on the memory buses
52 signal example_out_s : std_logic_vector(31 downto 0);
53 signal example_ce_s : std_logic;
54 -- Measurement (Master)
55 signal meas_out_s : std_logic_vector(31 downto 0);
56 signal meas_ce_s : std_logic;
57 -- Signals for external bus transmission
58 signal data_i_s : std_logic_vector(31 downto 0);
59 signal data_o_s : std_logic_vector(31 downto 0);
60 -- Signals for internal transaction
61 signal last_address_s : std_logic_vector(15 downto 0);
62 signal next_last_address_s : std_logic_vector(15 downto 0);
63 signal next_address_hold_s : std_logic;
64 signal address_hold_s : std_logic;
65 signal last_rd_s : std_logic;
66 signal next_last_rd_s : std_logic;
67 signal last_bls_s : std_logic_vector(3 downto 0); -- prev bls_f_s (active 1)
68 signal next_last_bls_s : std_logic_vector(3 downto 0);
70 -- Reading logic for Master CPU:
71 -- Broadcast rd only till ta (transaction acknowledge)
72 -- is received, then latch the data till the state of
73 -- rd or address changes
75 -- Data latching is synchronous - it's purpose is to
76 -- provide stable data for CPU on the bus
77 signal cs0_xc_f_s : std_logic;
78 signal rd_f_s : std_logic; -- Filtered RD
79 signal i_rd_s : std_logic; -- Internal bus RD (active 1)
80 -- signal next_i_rd_s : std_logic;
81 signal last_i_rd_s : std_logic; -- Delayed RD bus, used for latching
82 signal next_last_i_rd_s : std_logic;
83 signal i_rd_cycle2_s : std_logic; -- Some internal subsystems provide
84 signal next_i_rd_cycle2_s : std_logic; -- data only after 2 cycles
86 signal address_f_s : std_logic_vector(15 downto 0); -- Filtered address
88 signal data_f_s : std_logic_vector(31 downto 0); -- Filterred input data
90 signal data_read_s : std_logic_vector(31 downto 0); -- Latched read data
91 signal next_data_read_s : std_logic_vector(31 downto 0);
94 signal bls_f_s : std_logic_vector(3 downto 0); -- Filtered BLS (active 1)
95 signal i_bls_s : std_logic_vector(3 downto 0); -- Internal BLS (active 1)
96 signal next_i_bls_s : std_logic_vector(3 downto 0);
98 signal data_write_s : std_logic_vector(31 downto 0); -- Data broadcasted to write
99 signal next_data_write_s : std_logic_vector(31 downto 0);
101 -- signal s0 : std_logic;
102 -- signal s1 : std_logic;
103 -- signal s2 : std_logic;
106 attribute REGISTER_DUPLICATION : string;
107 attribute REGISTER_DUPLICATION of rd : signal is "NO";
108 attribute REGISTER_DUPLICATION of rd_f_s : signal is "NO";
109 attribute REGISTER_DUPLICATION of bls : signal is "NO";
110 attribute REGISTER_DUPLICATION of bls_f_s : signal is "NO";
111 attribute REGISTER_DUPLICATION of address : signal is "NO";
112 attribute REGISTER_DUPLICATION of address_f_s : signal is "NO";
113 attribute REGISTER_DUPLICATION of cs0_xc : signal is "NO";
114 attribute REGISTER_DUPLICATION of cs0_xc_f_s : signal is "NO";
118 -- Example connection
119 memory_bus_example: bus_example
124 ce_i => example_ce_s,
126 address_i => address_f_s(11 downto 0),
128 data_o => example_out_s
131 -- additional externally connected signals goes there
135 memory_bus_measurement: bus_measurement
141 address_i => address_f_s(1 downto 0),
160 data_i_s <= data_write_s;
167 process(cs0_xc_f_s, rd_f_s, last_rd_s, i_rd_cycle2_s, last_i_rd_s,
168 bls_f_s, last_bls_s, data_f_s, data_write_s,
169 data_o_s, data_read_s, last_address_s, address_f_s)
172 next_i_rd_cycle2_s <= '0';
173 next_address_hold_s <= '0';
175 -- Check if we have chip select
176 if cs0_xc_f_s = '1' then
181 if last_rd_s = '0' or (last_address_s /= address_f_s) then
183 next_i_rd_cycle2_s <= '1';
184 next_last_i_rd_s <= '1';
185 elsif i_rd_cycle2_s = '1' then -- FIXME it seems that some internal
186 i_rd_s <= '1'; -- peripherals demands 2 cycles to read
187 next_last_i_rd_s <= '1';
190 next_last_i_rd_s <= '0';
193 if last_i_rd_s = '1' then
194 -- Latch data we just read - they are valid in this cycle
195 next_data_read_s <= data_o_s;
197 next_data_read_s <= data_read_s;
200 -- -- Not reading, anything goes
201 -- data_read_s <= (others => 'X');
202 next_data_read_s <= data_read_s;
204 next_last_i_rd_s <= '0';
207 next_last_rd_s <= rd_f_s;
209 -- Data for write are captured only when BLS signals are stable
210 if bls_f_s /= "0000" then
211 next_data_write_s <= data_f_s;
212 next_address_hold_s <= '1';
214 next_data_write_s <= data_write_s;
217 if (bls_f_s /= "0000") or (rd_f_s = '1') then
218 next_last_address_s <= address_f_s;
220 next_last_address_s <= last_address_s;
223 next_last_rd_s <= '0';
225 next_last_i_rd_s <= '0';
227 next_i_bls_s <= "0000";
228 next_data_write_s <= data_write_s;
229 next_data_read_s <= data_read_s;
230 next_last_address_s <= last_address_s;
233 -- Data for write are captured at/before BLS signals are negated
234 -- and actual write cycle takes place exacly after BLS negation
235 if ((last_bls_s and not bls_f_s) /= "0000") or
236 ((last_bls_s /= "0000") and (cs0_xc_f_s = '0')) then
237 next_i_bls_s <= last_bls_s;
238 next_last_bls_s <= "0000";
239 next_address_hold_s <= '1';
241 next_i_bls_s <= "0000";
242 if cs0_xc_f_s = '1' then
243 next_last_bls_s <= bls_f_s;
245 next_last_bls_s <= "0000" ;
256 wait until clk_50m = '1' and clk_50m'event;
258 address_hold_s <= next_address_hold_s;
260 -- Synchronized external signals with main clock domain
261 cs0_xc_f_s <= not cs0_xc;
265 if address_hold_s = '0' then
266 address_f_s <= address;
268 address_f_s <= next_last_address_s;
271 -- Synchronoust state andvance to next period
272 last_bls_s <= next_last_bls_s;
273 last_rd_s <= next_last_rd_s;
274 i_bls_s <= next_i_bls_s;
275 -- i_rd_s <= next_i_rd_s;
276 i_rd_cycle2_s <= next_i_rd_cycle2_s;
277 last_i_rd_s <= next_last_i_rd_s;
278 data_write_s <= next_data_write_s;
279 last_address_s <= next_last_address_s;
280 data_read_s <= next_data_read_s;
284 -- Do the actual wiring here
286 process(cs0_xc_f_s, i_bls_s, address_f_s, example_out_s, meas_out_s)
289 -- Inactive by default
292 data_o_s <= (others => '0');
294 if cs0_xc_f_s = '1' or i_bls_s /= "0000" then
296 -- Memory Map (16-bit address @ 32-bit each)
298 -- Each address is seen as 32-bit entry now
299 -- 0x0000 - 0x0FFF: Example memory
300 -- 0x1FFC - 0x1FFF: Measurement
301 -- 0x2000 - 0x8FFF: Free space
303 if address_f_s < "0001000000000000" then -- Tumbl
305 data_o_s <= example_out_s;
306 elsif address_f_s(15 downto 2) = "00011111111111" then -- Measurement
308 data_o_s <= meas_out_s;
315 -- If RD and BLS is not high, we must keep DATA at high impedance
316 -- or the FPGA collides with SDRAM (damaging each other)
318 process(cs0_xc, rd, data_read_s)
321 -- CS0 / RD / BLS are active LOW
322 if cs0_xc = '0' and rd = '0' then
323 -- Don't risk flipping (between data_o_s and latched data_read_s, it's better to wait)
324 -- Maybe check this later.
325 -- if last_i_rd_s = '1' then
332 data <= (others => 'Z');