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
44 architecture Behavioral of lx_dad_top is
47 signal reset_s : std_logic;
48 signal init_s : std_logic;
49 -- Peripherals on the memory buses
51 signal example_out_s : std_logic_vector(31 downto 0);
52 signal example_ce_s : std_logic;
53 -- Measurement (Master)
54 signal meas_out_s : std_logic_vector(31 downto 0);
55 signal meas_ce_s : std_logic;
56 -- Signals for external bus transmission
57 signal data_i_s : std_logic_vector(31 downto 0);
58 signal data_o_s : std_logic_vector(31 downto 0);
59 -- Signals for internal transaction
60 signal last_address_s : std_logic_vector(15 downto 0);
61 signal next_last_address_s : std_logic_vector(15 downto 0);
62 signal next_address_hold_s : std_logic;
63 signal address_hold_s : std_logic;
64 signal last_rd_s : std_logic;
65 signal next_last_rd_s : std_logic;
66 signal last_bls_s : std_logic_vector(3 downto 0); -- prev bls_f_s (active 1)
67 signal next_last_bls_s : std_logic_vector(3 downto 0);
69 -- Reading logic for Master CPU:
70 -- Broadcast rd only till ta (transaction acknowledge)
71 -- is received, then latch the data till the state of
72 -- rd or address changes
74 -- Data latching is synchronous - it's purpose is to
75 -- provide stable data for CPU on the bus
76 signal cs0_xc_f_s : std_logic;
77 signal rd_f_s : std_logic; -- Filtered RD
78 signal i_rd_s : std_logic; -- Internal bus RD (active 1)
79 -- signal next_i_rd_s : std_logic;
80 signal last_i_rd_s : std_logic; -- Delayed RD bus, used for latching
81 signal next_last_i_rd_s : std_logic;
82 signal i_rd_cycle2_s : std_logic; -- Some internal subsystems provide
83 signal next_i_rd_cycle2_s : std_logic; -- data only after 2 cycles
85 signal address_f_s : std_logic_vector(15 downto 0); -- Filtered address
87 signal data_f_s : std_logic_vector(31 downto 0); -- Filterred input data
89 signal data_read_s : std_logic_vector(31 downto 0); -- Latched read data
90 signal next_data_read_s : std_logic_vector(31 downto 0);
93 signal bls_f_s : std_logic_vector(3 downto 0); -- Filtered BLS (active 1)
94 signal i_bls_s : std_logic_vector(3 downto 0); -- Internal BLS (active 1)
95 signal next_i_bls_s : std_logic_vector(3 downto 0);
97 signal data_write_s : std_logic_vector(31 downto 0); -- Data broadcasted to write
98 signal next_data_write_s : std_logic_vector(31 downto 0);
100 -- signal s0 : std_logic;
101 -- signal s1 : std_logic;
102 -- signal s2 : std_logic;
105 attribute REGISTER_DUPLICATION : string;
106 attribute REGISTER_DUPLICATION of rd : signal is "NO";
107 attribute REGISTER_DUPLICATION of rd_f_s : signal is "NO";
108 attribute REGISTER_DUPLICATION of bls : signal is "NO";
109 attribute REGISTER_DUPLICATION of bls_f_s : signal is "NO";
110 attribute REGISTER_DUPLICATION of address : signal is "NO";
111 attribute REGISTER_DUPLICATION of address_f_s : signal is "NO";
112 attribute REGISTER_DUPLICATION of cs0_xc : signal is "NO";
113 attribute REGISTER_DUPLICATION of cs0_xc_f_s : signal is "NO";
117 -- Example connection
118 memory_bus_example: bus_example
123 ce_i => example_ce_s,
125 address_i => address_f_s(11 downto 0),
127 data_o => example_out_s
130 -- additional externally connected signals goes there
134 memory_bus_measurement: bus_measurement
140 address_i => address_f_s(1 downto 0),
159 data_i_s <= data_write_s;
166 process(cs0_xc_f_s, rd_f_s, last_rd_s, i_rd_cycle2_s, last_i_rd_s,
167 bls_f_s, last_bls_s, data_f_s, data_write_s,
168 data_o_s, data_read_s, last_address_s, address_f_s)
171 next_i_rd_cycle2_s <= '0';
172 next_address_hold_s <= '0';
174 -- Check if we have chip select
175 if cs0_xc_f_s = '1' then
180 if last_rd_s = '0' or (last_address_s /= address_f_s) then
182 next_i_rd_cycle2_s <= '1';
183 next_last_i_rd_s <= '1';
184 elsif i_rd_cycle2_s = '1' then -- FIXME it seems that some internal
185 i_rd_s <= '1'; -- peripherals demands 2 cycles to read
186 next_last_i_rd_s <= '1';
189 next_last_i_rd_s <= '0';
192 if last_i_rd_s = '1' then
193 -- Latch data we just read - they are valid in this cycle
194 next_data_read_s <= data_o_s;
196 next_data_read_s <= data_read_s;
199 -- -- Not reading, anything goes
200 -- data_read_s <= (others => 'X');
201 next_data_read_s <= data_read_s;
203 next_last_i_rd_s <= '0';
206 next_last_rd_s <= rd_f_s;
208 -- Data for write are captured only when BLS signals are stable
209 if bls_f_s /= "0000" then
210 next_data_write_s <= data_f_s;
211 next_address_hold_s <= '1';
213 next_data_write_s <= data_write_s;
216 if (bls_f_s /= "0000") or (rd_f_s = '1') then
217 next_last_address_s <= address_f_s;
219 next_last_address_s <= last_address_s;
222 next_last_rd_s <= '0';
224 next_last_i_rd_s <= '0';
226 next_i_bls_s <= "0000";
227 next_data_write_s <= data_write_s;
228 next_data_read_s <= data_read_s;
229 next_last_address_s <= last_address_s;
232 -- Data for write are captured at/before BLS signals are negated
233 -- and actual write cycle takes place exacly after BLS negation
234 if ((last_bls_s and not bls_f_s) /= "0000") or
235 ((last_bls_s /= "0000") and (cs0_xc_f_s = '0')) then
236 next_i_bls_s <= last_bls_s;
237 next_last_bls_s <= "0000";
238 next_address_hold_s <= '1';
240 next_i_bls_s <= "0000";
241 if cs0_xc_f_s = '1' then
242 next_last_bls_s <= bls_f_s;
244 next_last_bls_s <= "0000" ;
255 wait until clk_50m = '1' and clk_50m'event;
257 address_hold_s <= next_address_hold_s;
259 -- Synchronized external signals with main clock domain
260 cs0_xc_f_s <= not cs0_xc;
264 if address_hold_s = '0' then
265 address_f_s <= address;
267 address_f_s <= next_last_address_s;
270 -- Synchronoust state andvance to next period
271 last_bls_s <= next_last_bls_s;
272 last_rd_s <= next_last_rd_s;
273 i_bls_s <= next_i_bls_s;
274 -- i_rd_s <= next_i_rd_s;
275 i_rd_cycle2_s <= next_i_rd_cycle2_s;
276 last_i_rd_s <= next_last_i_rd_s;
277 data_write_s <= next_data_write_s;
278 last_address_s <= next_last_address_s;
279 data_read_s <= next_data_read_s;
283 -- Do the actual wiring here
285 process(cs0_xc_f_s, i_bls_s, address_f_s, example_out_s, meas_out_s)
288 -- Inactive by default
291 data_o_s <= (others => '0');
293 if cs0_xc_f_s = '1' or i_bls_s /= "0000" then
295 -- Memory Map (16-bit address @ 32-bit each)
297 -- Each address is seen as 32-bit entry now
298 -- 0x0000 - 0x0FFF: Example memory
299 -- 0x1FFC - 0x1FFF: Measurement
300 -- 0x2000 - 0x8FFF: Free space
302 if address_f_s < "0001000000000000" then -- Tumbl
304 data_o_s <= example_out_s;
305 elsif address_f_s(15 downto 2) = "00011111111111" then -- Measurement
307 data_o_s <= meas_out_s;
314 -- If RD and BLS is not high, we must keep DATA at high impedance
315 -- or the FPGA collides with SDRAM (damaging each other)
317 process(cs0_xc, rd, data_read_s)
320 -- CS0 / RD / BLS are active LOW
321 if cs0_xc = '0' and rd = '0' then
322 -- Don't risk flipping (between data_o_s and latched data_read_s, it's better to wait)
323 -- Maybe check this later.
324 -- if last_i_rd_s = '1' then
331 data <= (others => 'Z');