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_last_i_rd_s : std_logic;
80 signal last_i_rd_s : std_logic; -- Delayed RD bus, used for latching
82 signal address_f_s : std_logic_vector(15 downto 0); -- Filtered address
84 signal data_f_s : std_logic_vector(31 downto 0); -- Filterred input data
86 signal data_read_s : std_logic_vector(31 downto 0); -- Latched read data
87 signal next_data_read_s : std_logic_vector(31 downto 0);
90 signal bls_f_s : std_logic_vector(3 downto 0); -- Filtered BLS (active 1)
91 signal i_bls_s : std_logic_vector(3 downto 0); -- Internal BLS (active 1)
92 signal next_i_bls_s : std_logic_vector(3 downto 0);
94 signal data_write_s : std_logic_vector(31 downto 0); -- Data broadcasted to write
95 signal next_data_write_s : std_logic_vector(31 downto 0);
97 -- signal s0 : std_logic;
98 -- signal s1 : std_logic;
99 -- signal s2 : std_logic;
102 attribute REGISTER_DUPLICATION : string;
103 attribute REGISTER_DUPLICATION of rd : signal is "NO";
104 attribute REGISTER_DUPLICATION of rd_f_s : signal is "NO";
105 attribute REGISTER_DUPLICATION of bls : signal is "NO";
106 attribute REGISTER_DUPLICATION of bls_f_s : signal is "NO";
107 attribute REGISTER_DUPLICATION of address : signal is "NO";
108 attribute REGISTER_DUPLICATION of address_f_s : signal is "NO";
109 attribute REGISTER_DUPLICATION of cs0_xc : signal is "NO";
110 attribute REGISTER_DUPLICATION of cs0_xc_f_s : signal is "NO";
114 -- Example connection
115 memory_bus_example: bus_example
120 ce_i => example_ce_s,
122 address_i => address_f_s(11 downto 0),
124 data_o => example_out_s
127 -- additional externally connected signals goes there
131 memory_bus_measurement: bus_measurement
137 address_i => address_f_s(1 downto 0),
156 data_i_s <= data_write_s;
163 process(cs0_xc_f_s, rd_f_s, last_rd_s, last_i_rd_s,
164 bls_f_s, last_bls_s, data_f_s, data_write_s,
165 data_o_s, data_read_s, last_address_s, address_f_s)
168 next_address_hold_s <= '0';
170 -- Check if we have chip select
171 if cs0_xc_f_s = '1' then
176 if last_rd_s = '0' or (last_address_s /= address_f_s) then
178 next_last_i_rd_s <= '1';
181 next_last_i_rd_s <= '0';
184 if last_i_rd_s = '1' then
185 -- Latch data we just read - they are valid in this cycle
186 next_data_read_s <= data_o_s;
188 next_data_read_s <= data_read_s;
191 -- -- Not reading, anything goes
192 -- data_read_s <= (others => 'X');
193 next_data_read_s <= data_read_s;
195 next_last_i_rd_s <= '0';
198 next_last_rd_s <= rd_f_s;
200 -- Data for write are captured only when BLS signals are stable
201 if bls_f_s /= "0000" then
202 next_data_write_s <= data_f_s;
203 next_address_hold_s <= '1';
205 next_data_write_s <= data_write_s;
208 if (bls_f_s /= "0000") or (rd_f_s = '1') then
209 next_last_address_s <= address_f_s;
211 next_last_address_s <= last_address_s;
214 next_last_rd_s <= '0';
216 next_last_i_rd_s <= '0';
218 next_i_bls_s <= "0000";
219 next_data_write_s <= data_write_s;
220 next_data_read_s <= data_read_s;
221 next_last_address_s <= last_address_s;
224 -- Data for write are captured at/before BLS signals are negated
225 -- and actual write cycle takes place exacly after BLS negation
226 if ((last_bls_s and not bls_f_s) /= "0000") or
227 ((last_bls_s /= "0000") and (cs0_xc_f_s = '0')) then
228 next_i_bls_s <= last_bls_s;
229 next_last_bls_s <= "0000";
230 next_address_hold_s <= '1';
232 next_i_bls_s <= "0000";
233 if cs0_xc_f_s = '1' then
234 next_last_bls_s <= bls_f_s;
236 next_last_bls_s <= "0000" ;
247 wait until clk_50m = '1' and clk_50m'event;
249 address_hold_s <= next_address_hold_s;
251 -- Synchronized external signals with main clock domain
252 cs0_xc_f_s <= not cs0_xc;
256 if address_hold_s = '0' then
257 address_f_s <= address;
259 address_f_s <= next_last_address_s;
262 -- Synchronoust state andvance to next period
263 last_bls_s <= next_last_bls_s;
264 last_rd_s <= next_last_rd_s;
265 i_bls_s <= next_i_bls_s;
266 last_i_rd_s <= next_last_i_rd_s;
267 data_write_s <= next_data_write_s;
268 last_address_s <= next_last_address_s;
269 data_read_s <= next_data_read_s;
273 -- Do the actual wiring here
275 process(cs0_xc_f_s, i_bls_s, address_f_s, example_out_s, meas_out_s, i_rd_s, last_i_rd_s)
278 -- Inactive by default
281 data_o_s <= (others => '0');
283 if i_rd_s = '1' or i_bls_s /= "0000" then
285 -- Memory Map (16-bit address @ 32-bit each)
287 -- Each address is seen as 32-bit entry now
288 -- 0x0000 - 0x0FFF: Example memory
289 -- 0x1FFC - 0x1FFF: Measurement
290 -- 0x2000 - 0x8FFF: Free space
292 if address_f_s < "0001000000000000" then -- Tumbl
294 elsif address_f_s(15 downto 2) = "00011111111111" then -- Measurement
300 if last_i_rd_s = '1' then
301 if address_f_s < "0001000000000000" then -- Tumbl
302 data_o_s <= example_out_s;
303 elsif address_f_s(15 downto 2) = "00011111111111" then -- Measurement
304 data_o_s <= meas_out_s;
310 -- If RD and BLS is not high, we must keep DATA at high impedance
311 -- or the FPGA collides with SDRAM (damaging each other)
313 process(cs0_xc, rd, data_read_s)
316 -- CS0 / RD / BLS are active LOW
317 if cs0_xc = '0' and rd = '0' then
318 -- Don't risk flipping (between data_o_s and latched data_read_s, it's better to wait)
319 -- Maybe check this later.
320 -- if last_i_rd_s = '1' then
327 data <= (others => 'Z');