2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4 use ieee.math_real.all;
10 use work.wishbone_types.all;
13 -- Memory map. *** Keep include/microwatt_soc.h updated on changes ***
16 -- 0x00000000: Block RAM (MEMORY_SIZE) or DRAM depending on syscon
17 -- 0x40000000: DRAM (when present)
18 -- 0x80000000: Block RAM (aliased & repeated)
23 -- 0xc0004000: XICS ICP
24 -- 0xc0006000: SPI Flash controller
25 -- 0xc0100000: LiteDRAM control (CSRs)
26 -- 0xf0000000: Flash "ROM" mapping
27 -- 0xff000000: DRAM init code (if any) or flash ROM
31 MEMORY_SIZE : natural;
32 RAM_INIT_FILE : string;
36 DISABLE_FLATTEN_CORE : boolean := false;
37 HAS_DRAM : boolean := false;
38 DRAM_SIZE : integer := 0;
39 DRAM_INIT_SIZE : integer := 0;
40 HAS_SPI_FLASH : boolean := false;
41 SPI_FLASH_DLINES : positive := 1;
42 SPI_FLASH_OFFSET : integer := 0;
43 SPI_FLASH_DEF_CKDV : natural := 2;
44 SPI_FLASH_DEF_QUAD : boolean := false
48 system_clk : in std_ulogic;
50 -- DRAM controller signals
51 wb_dram_in : out wishbone_master_out;
52 wb_dram_out : in wishbone_slave_out;
53 wb_dram_ctrl_in : out wb_io_master_out;
54 wb_dram_ctrl_out : in wb_io_slave_out;
55 wb_dram_is_csr : out std_ulogic;
56 wb_dram_is_init : out std_ulogic;
59 uart0_txd : out std_ulogic;
60 uart0_rxd : in std_ulogic;
63 spi_flash_sck : out std_ulogic;
64 spi_flash_cs_n : out std_ulogic;
65 spi_flash_sdat_o : out std_ulogic_vector(SPI_FLASH_DLINES-1 downto 0);
66 spi_flash_sdat_oe : out std_ulogic_vector(SPI_FLASH_DLINES-1 downto 0);
67 spi_flash_sdat_i : in std_ulogic_vector(SPI_FLASH_DLINES-1 downto 0);
69 -- DRAM controller signals
70 alt_reset : in std_ulogic
74 architecture behaviour of soc is
76 -- Wishbone master signals:
77 signal wishbone_dcore_in : wishbone_slave_out;
78 signal wishbone_dcore_out : wishbone_master_out;
79 signal wishbone_icore_in : wishbone_slave_out;
80 signal wishbone_icore_out : wishbone_master_out;
81 signal wishbone_debug_in : wishbone_slave_out;
82 signal wishbone_debug_out : wishbone_master_out;
84 -- Arbiter array (ghdl doesnt' support assigning the array
85 -- elements in the entity instantiation)
86 constant NUM_WB_MASTERS : positive := 3;
87 signal wb_masters_out : wishbone_master_out_vector(0 to NUM_WB_MASTERS-1);
88 signal wb_masters_in : wishbone_slave_out_vector(0 to NUM_WB_MASTERS-1);
90 -- Wishbone master (output of arbiter):
91 signal wb_master_in : wishbone_slave_out;
92 signal wb_master_out : wishbone_master_out;
94 -- Main "IO" bus, from main slave decoder to the latch
95 signal wb_io_in : wishbone_master_out;
96 signal wb_io_out : wishbone_slave_out;
98 -- Secondary (smaller) IO bus after the IO bus latch
99 signal wb_sio_out : wb_io_master_out;
100 signal wb_sio_in : wb_io_slave_out;
103 signal dram_at_0 : std_ulogic;
104 signal do_core_reset : std_ulogic;
105 signal wb_syscon_in : wb_io_master_out;
106 signal wb_syscon_out : wb_io_slave_out;
109 signal wb_uart0_in : wb_io_master_out;
110 signal wb_uart0_out : wb_io_slave_out;
111 signal uart_dat8 : std_ulogic_vector(7 downto 0);
113 -- SPI Flash controller signals:
114 signal wb_spiflash_in : wb_io_master_out;
115 signal wb_spiflash_out : wb_io_slave_out;
116 signal wb_spiflash_is_reg : std_ulogic;
117 signal wb_spiflash_is_map : std_ulogic;
120 signal wb_xics0_in : wb_io_master_out;
121 signal wb_xics0_out : wb_io_slave_out;
122 signal int_level_in : std_ulogic_vector(15 downto 0);
124 signal core_ext_irq : std_ulogic;
126 -- Main memory signals:
127 signal wb_bram_in : wishbone_master_out;
128 signal wb_bram_out : wishbone_slave_out;
130 -- DMI debug bus signals
131 signal dmi_addr : std_ulogic_vector(7 downto 0);
132 signal dmi_din : std_ulogic_vector(63 downto 0);
133 signal dmi_dout : std_ulogic_vector(63 downto 0);
134 signal dmi_req : std_ulogic;
135 signal dmi_wr : std_ulogic;
136 signal dmi_ack : std_ulogic;
138 -- Per slave DMI signals
139 signal dmi_wb_dout : std_ulogic_vector(63 downto 0);
140 signal dmi_wb_req : std_ulogic;
141 signal dmi_wb_ack : std_ulogic;
142 signal dmi_core_dout : std_ulogic_vector(63 downto 0);
143 signal dmi_core_req : std_ulogic;
144 signal dmi_core_ack : std_ulogic;
146 -- Delayed/latched resets and alt_reset
147 signal rst_core : std_ulogic := '1';
148 signal rst_uart : std_ulogic := '1';
149 signal rst_xics : std_ulogic := '1';
150 signal rst_spi : std_ulogic := '1';
151 signal rst_bram : std_ulogic := '1';
152 signal rst_dtm : std_ulogic := '1';
153 signal rst_wbar : std_ulogic := '1';
154 signal rst_wbdb : std_ulogic := '1';
155 signal alt_reset_d : std_ulogic;
158 type slave_io_type is (SLAVE_IO_SYSCON,
163 SLAVE_IO_SPI_FLASH_REG,
164 SLAVE_IO_SPI_FLASH_MAP,
166 signal slave_io_dbg : slave_io_type;
169 resets: process(system_clk)
171 if rising_edge(system_clk) then
172 rst_core <= rst or do_core_reset;
180 alt_reset_d <= alt_reset;
185 processor: entity work.core
188 DISABLE_FLATTEN => DISABLE_FLATTEN_CORE,
189 ALT_RESET_ADDRESS => (23 downto 0 => '0', others => '1')
194 alt_reset => alt_reset_d,
195 wishbone_insn_in => wishbone_icore_in,
196 wishbone_insn_out => wishbone_icore_out,
197 wishbone_data_in => wishbone_dcore_in,
198 wishbone_data_out => wishbone_dcore_out,
199 dmi_addr => dmi_addr(3 downto 0),
200 dmi_dout => dmi_core_dout,
203 dmi_ack => dmi_core_ack,
204 dmi_req => dmi_core_req,
205 ext_irq => core_ext_irq
208 -- Wishbone bus master arbiter & mux
209 wb_masters_out <= (0 => wishbone_dcore_out,
210 1 => wishbone_icore_out,
211 2 => wishbone_debug_out);
212 wishbone_dcore_in <= wb_masters_in(0);
213 wishbone_icore_in <= wb_masters_in(1);
214 wishbone_debug_in <= wb_masters_in(2);
215 wishbone_arbiter_0: entity work.wishbone_arbiter
217 NUM_MASTERS => NUM_WB_MASTERS
222 wb_masters_in => wb_masters_out,
223 wb_masters_out => wb_masters_in,
224 wb_slave_out => wb_master_out,
225 wb_slave_in => wb_master_in
228 -- Top level Wishbone slaves address decoder & mux
230 -- From CPU to BRAM, DRAM, IO, selected on top 3 bits and dram_at_0
237 slave_top_intercon: process(wb_master_out, wb_bram_out, wb_dram_out, wb_io_out, dram_at_0)
238 type slave_top_type is (SLAVE_TOP_BRAM,
241 variable slave_top : slave_top_type;
242 variable top_decode : std_ulogic_vector(3 downto 0);
244 -- Top-level address decoder
245 top_decode := wb_master_out.adr(31 downto 29) & dram_at_0;
246 slave_top := SLAVE_TOP_BRAM;
247 if std_match(top_decode, "0000") then
248 slave_top := SLAVE_TOP_BRAM;
249 elsif std_match(top_decode, "0001") then
250 slave_top := SLAVE_TOP_DRAM;
251 elsif std_match(top_decode, "01--") then
252 slave_top := SLAVE_TOP_DRAM;
253 elsif std_match(top_decode, "10--") then
254 slave_top := SLAVE_TOP_BRAM;
255 elsif std_match(top_decode, "11--") then
256 slave_top := SLAVE_TOP_IO;
259 -- Top level wishbone muxing.
260 wb_bram_in <= wb_master_out;
261 wb_bram_in.cyc <= '0';
262 wb_dram_in <= wb_master_out;
263 wb_dram_in.cyc <= '0';
264 wb_io_in <= wb_master_out;
267 when SLAVE_TOP_BRAM =>
268 wb_bram_in.cyc <= wb_master_out.cyc;
269 wb_master_in <= wb_bram_out;
270 when SLAVE_TOP_DRAM =>
271 wb_dram_in.cyc <= wb_master_out.cyc;
272 wb_master_in <= wb_dram_out;
274 wb_io_in.cyc <= wb_master_out.cyc;
275 wb_master_in <= wb_io_out;
277 end process slave_top_intercon;
279 -- IO wishbone slave 64->32 bits converter
281 -- For timing reasons, this adds a one cycle latch on the way both
282 -- in and out. This relaxes timing and routing pressure on the "main"
283 -- memory bus by moving all simple IOs to a slower 32-bit bus.
285 -- This implementation is rather dumb at the moment, no stash buffer,
286 -- so we stall whenever that latch is busy. This can be improved.
288 slave_io_latch: process(system_clk)
290 type state_t is (IDLE, WAIT_ACK_BOT, WAIT_ACK_TOP);
291 variable state : state_t;
294 variable has_top : boolean;
295 variable has_bot : boolean;
297 if rising_edge(system_clk) then
300 wb_io_out.ack <= '0';
301 wb_io_out.stall <= '0';
302 wb_sio_out.cyc <= '0';
303 wb_sio_out.stb <= '0';
309 -- Clear ACK in case it was set
310 wb_io_out.ack <= '0';
312 -- Do we have a cycle ?
313 if wb_io_in.cyc = '1' and wb_io_in.stb = '1' then
314 -- Stall master until we are done, we are't (yet) pipelining
315 -- this, it's all slow IOs.
316 wb_io_out.stall <= '1';
318 -- Start cycle downstream
319 wb_sio_out.cyc <= '1';
320 wb_sio_out.stb <= '1';
322 -- Copy write enable to IO out, copy address as well
323 wb_sio_out.we <= wb_io_in.we;
324 wb_sio_out.adr <= wb_io_in.adr(wb_sio_out.adr'left downto 3) & "000";
326 -- Do we have a top word and/or a bottom word ?
327 has_top := wb_io_in.sel(7 downto 4) /= "0000";
328 has_bot := wb_io_in.sel(3 downto 0) /= "0000";
330 -- If we have a bottom word, handle it first, otherwise
331 -- send the top word down. XXX Split the actual mux out
332 -- and only generate a control signal.
334 if wb_io_in.we = '1' then
335 wb_sio_out.dat <= wb_io_in.dat(31 downto 0);
337 wb_sio_out.sel <= wb_io_in.sel(3 downto 0);
340 state := WAIT_ACK_BOT;
342 if wb_io_in.we = '1' then
343 wb_sio_out.dat <= wb_io_in.dat(63 downto 32);
345 wb_sio_out.sel <= wb_io_in.sel(7 downto 4);
348 wb_sio_out.adr(2) <= '1';
351 state := WAIT_ACK_TOP;
355 -- If we aren't stalled by the device, clear stb
356 if wb_sio_in.stall = '0' then
357 wb_sio_out.stb <= '0';
361 if wb_sio_in.ack = '1' then
362 -- If it's a read, latch the data
363 if wb_sio_out.we = '0' then
364 wb_io_out.dat(31 downto 0) <= wb_sio_in.dat;
367 -- Do we have a "top" part as well ?
370 if wb_io_in.we = '1' then
371 wb_sio_out.dat <= wb_io_in.dat(63 downto 32);
373 wb_sio_out.sel <= wb_io_in.sel(7 downto 4);
375 -- Bump address and set STB
376 wb_sio_out.adr(2) <= '1';
377 wb_sio_out.stb <= '1';
380 state := WAIT_ACK_TOP;
382 -- We are done, ack up, clear cyc downstram
383 wb_sio_out.cyc <= '0';
385 -- And ack & unstall upstream
386 wb_io_out.ack <= '1';
387 wb_io_out.stall <= '0';
394 -- If we aren't stalled by the device, clear stb
395 if wb_sio_in.stall = '0' then
396 wb_sio_out.stb <= '0';
400 if wb_sio_in.ack = '1' then
401 -- If it's a read, latch the data
402 if wb_sio_out.we = '0' then
403 wb_io_out.dat(63 downto 32) <= wb_sio_in.dat;
406 -- We are done, ack up, clear cyc downstram
407 wb_sio_out.cyc <= '0';
409 -- And ack & unstall upstream
410 wb_io_out.ack <= '1';
411 wb_io_out.stall <= '0';
421 -- IO wishbone slave intercon.
423 slave_io_intercon: process(wb_sio_out, wb_syscon_out, wb_uart0_out,
424 wb_dram_ctrl_out, wb_xics0_out, wb_spiflash_out)
425 variable slave_io : slave_io_type;
427 variable match : std_ulogic_vector(31 downto 12);
430 -- Simple address decoder.
431 slave_io := SLAVE_IO_NONE;
432 match := "11" & wb_sio_out.adr(29 downto 12);
433 if std_match(match, x"FF---") and HAS_DRAM then
434 slave_io := SLAVE_IO_DRAM_INIT;
435 elsif std_match(match, x"F----") then
436 slave_io := SLAVE_IO_SPI_FLASH_MAP;
437 elsif std_match(match, x"C0000") then
438 slave_io := SLAVE_IO_SYSCON;
439 elsif std_match(match, x"C0002") then
440 slave_io := SLAVE_IO_UART;
441 elsif std_match(match, x"C01--") then
442 slave_io := SLAVE_IO_DRAM_CSR;
443 elsif std_match(match, x"C0004") then
444 slave_io := SLAVE_IO_ICP_0;
445 elsif std_match(match, x"C0006") then
446 slave_io := SLAVE_IO_SPI_FLASH_REG;
448 slave_io_dbg <= slave_io;
449 wb_uart0_in <= wb_sio_out;
450 wb_uart0_in.cyc <= '0';
451 wb_spiflash_in <= wb_sio_out;
452 wb_spiflash_in.cyc <= '0';
453 wb_spiflash_is_reg <= '0';
454 wb_spiflash_is_map <= '0';
456 -- Only give xics 8 bits of wb addr
457 wb_xics0_in <= wb_sio_out;
458 wb_xics0_in.adr <= (others => '0');
459 wb_xics0_in.adr(7 downto 0) <= wb_sio_out.adr(7 downto 0);
460 wb_xics0_in.cyc <= '0';
462 wb_dram_ctrl_in <= wb_sio_out;
463 wb_dram_ctrl_in.cyc <= '0';
464 wb_dram_is_csr <= '0';
465 wb_dram_is_init <= '0';
467 wb_syscon_in <= wb_sio_out;
468 wb_syscon_in.cyc <= '0';
471 when SLAVE_IO_DRAM_INIT =>
472 wb_dram_ctrl_in.cyc <= wb_sio_out.cyc;
473 wb_sio_in <= wb_dram_ctrl_out;
474 wb_dram_is_init <= '1';
475 when SLAVE_IO_DRAM_CSR =>
476 wb_dram_ctrl_in.cyc <= wb_sio_out.cyc;
477 wb_sio_in <= wb_dram_ctrl_out;
478 wb_dram_is_csr <= '1';
479 when SLAVE_IO_SYSCON =>
480 wb_syscon_in.cyc <= wb_sio_out.cyc;
481 wb_sio_in <= wb_syscon_out;
482 when SLAVE_IO_UART =>
483 wb_uart0_in.cyc <= wb_sio_out.cyc;
484 wb_sio_in <= wb_uart0_out;
485 when SLAVE_IO_ICP_0 =>
486 wb_xics0_in.cyc <= wb_sio_out.cyc;
487 wb_sio_in <= wb_xics0_out;
488 when SLAVE_IO_SPI_FLASH_MAP =>
489 -- Clear top bits so they don't make their way to the
491 wb_spiflash_in.adr(29 downto 28) <= "00";
492 wb_spiflash_in.cyc <= wb_sio_out.cyc;
493 wb_sio_in <= wb_spiflash_out;
494 wb_spiflash_is_map <= '1';
495 when SLAVE_IO_SPI_FLASH_REG =>
496 wb_spiflash_in.cyc <= wb_sio_out.cyc;
497 wb_sio_in <= wb_spiflash_out;
498 wb_spiflash_is_reg <= '1';
500 wb_sio_in.dat <= (others => '1');
501 wb_sio_in.ack <= wb_sio_out.stb and wb_sio_out.cyc;
502 wb_sio_in.stall <= '0';
508 syscon0: entity work.syscon
511 HAS_DRAM => HAS_DRAM,
512 BRAM_SIZE => MEMORY_SIZE,
513 DRAM_SIZE => DRAM_SIZE,
514 DRAM_INIT_SIZE => DRAM_INIT_SIZE,
515 CLK_FREQ => CLK_FREQ,
516 HAS_SPI_FLASH => HAS_SPI_FLASH,
517 SPI_FLASH_OFFSET => SPI_FLASH_OFFSET
522 wishbone_in => wb_syscon_in,
523 wishbone_out => wb_syscon_out,
524 dram_at_0 => dram_at_0,
525 core_reset => do_core_reset,
526 soc_reset => open -- XXX TODO
529 -- Simulated memory and UART
531 -- UART0 wishbone slave
532 uart0: entity work.pp_soc_uart
541 irq => int_level_in(0),
542 wb_adr_in => wb_uart0_in.adr(11 downto 0),
543 wb_dat_in => wb_uart0_in.dat(7 downto 0),
544 wb_dat_out => uart_dat8,
545 wb_cyc_in => wb_uart0_in.cyc,
546 wb_stb_in => wb_uart0_in.stb,
547 wb_we_in => wb_uart0_in.we,
548 wb_ack_out => wb_uart0_out.ack
550 wb_uart0_out.dat <= x"000000" & uart_dat8;
551 wb_uart0_out.stall <= '0' when wb_uart0_in.cyc = '0' else not wb_uart0_out.ack;
553 spiflash_gen: if HAS_SPI_FLASH generate
554 spiflash: entity work.spi_flash_ctrl
556 DATA_LINES => SPI_FLASH_DLINES,
557 DEF_CLK_DIV => SPI_FLASH_DEF_CKDV,
558 DEF_QUAD_READ => SPI_FLASH_DEF_QUAD
563 wb_in => wb_spiflash_in,
564 wb_out => wb_spiflash_out,
565 wb_sel_reg => wb_spiflash_is_reg,
566 wb_sel_map => wb_spiflash_is_map,
567 sck => spi_flash_sck,
568 cs_n => spi_flash_cs_n,
569 sdat_o => spi_flash_sdat_o,
570 sdat_oe => spi_flash_sdat_oe,
571 sdat_i => spi_flash_sdat_i
575 no_spi0_gen: if not HAS_SPI_FLASH generate
576 wb_spiflash_out.dat <= (others => '1');
577 wb_spiflash_out.ack <= wb_spiflash_in.cyc and wb_spiflash_in.stb;
578 wb_spiflash_out.stall <= wb_spiflash_in.cyc and not wb_spiflash_out.ack;
581 xics0: entity work.xics
588 wb_in => wb_xics0_in,
589 wb_out => wb_xics0_out,
590 int_level_in => int_level_in,
591 core_irq_out => core_ext_irq
595 bram: if MEMORY_SIZE /= 0 generate
596 bram0: entity work.wishbone_bram_wrapper
598 MEMORY_SIZE => MEMORY_SIZE,
599 RAM_INIT_FILE => RAM_INIT_FILE
604 wishbone_in => wb_bram_in,
605 wishbone_out => wb_bram_out
609 no_bram: if MEMORY_SIZE = 0 generate
610 wb_bram_out.ack <= wb_bram_in.cyc and wb_bram_in.stb;
611 wb_bram_out.dat <= x"FFFFFFFFFFFFFFFF";
612 wb_bram_out.stall <= wb_bram_in.cyc and not wb_bram_out.ack;
615 -- DMI(debug bus) <-> JTAG bridge
616 dtm: entity work.dmi_dtm
622 sys_clk => system_clk,
623 sys_reset => rst_dtm,
624 dmi_addr => dmi_addr,
626 dmi_dout => dmi_dout,
633 dmi_intercon: process(dmi_addr, dmi_req,
634 dmi_wb_ack, dmi_wb_dout,
635 dmi_core_ack, dmi_core_dout)
637 -- DMI address map (each address is a full 64-bit register)
639 -- Offset: Size: Slave:
643 type slave_type is (SLAVE_WB,
646 variable slave : slave_type;
648 -- Simple address decoder
650 if std_match(dmi_addr, "000000--") then
652 elsif std_match(dmi_addr, "0001----") then
661 dmi_wb_req <= dmi_req;
662 dmi_ack <= dmi_wb_ack;
663 dmi_din <= dmi_wb_dout;
665 dmi_core_req <= dmi_req;
666 dmi_ack <= dmi_core_ack;
667 dmi_din <= dmi_core_dout;
670 dmi_din <= (others => '1');
674 if SIM and dmi_req = '1' and dmi_addr = "11111111" and dmi_wr = '1' then
679 -- Wishbone debug master (TODO: Add a DMI address decoder)
680 wishbone_debug: entity work.wishbone_debug_master
681 port map(clk => system_clk,
683 dmi_addr => dmi_addr(1 downto 0),
684 dmi_dout => dmi_wb_dout,
687 dmi_ack => dmi_wb_ack,
688 dmi_req => dmi_wb_req,
689 wb_in => wishbone_debug_in,
690 wb_out => wishbone_debug_out);
693 end architecture behaviour;