Merge pull request #183 from shawnanastasio/addpcis
[microwatt.git] / soc.vhdl
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4 use ieee.math_real.all;
5 use std.textio.all;
6 use std.env.stop;
7
8 library work;
9 use work.common.all;
10 use work.wishbone_types.all;
11
12
13 -- Memory map. *** Keep include/microwatt_soc.h updated on changes ***
14 --
15 -- Main bus:
16 -- 0x00000000: Block RAM (MEMORY_SIZE) or DRAM depending on syscon
17 -- 0x40000000: DRAM (when present)
18 -- 0x80000000: Block RAM (aliased & repeated)
19
20 -- IO Bus:
21 -- 0xc0000000: SYSCON
22 -- 0xc0002000: UART0
23 -- 0xc0004000: XICS ICP
24 -- 0xc0100000: LiteDRAM control (CSRs)
25 -- 0xf0000000: DRAM init code (if any)
26
27 entity soc is
28 generic (
29 MEMORY_SIZE : positive;
30 RAM_INIT_FILE : string;
31 RESET_LOW : boolean;
32 CLK_FREQ : positive;
33 SIM : boolean;
34 DISABLE_FLATTEN_CORE : boolean := false;
35 HAS_DRAM : boolean := false;
36 DRAM_SIZE : integer := 0
37 );
38 port(
39 rst : in std_ulogic;
40 system_clk : in std_ulogic;
41
42 -- DRAM controller signals
43 wb_dram_in : out wishbone_master_out;
44 wb_dram_out : in wishbone_slave_out;
45 wb_dram_ctrl_in : out wb_io_master_out;
46 wb_dram_ctrl_out : in wb_io_slave_out;
47 wb_dram_is_csr : out std_ulogic;
48 wb_dram_is_init : out std_ulogic;
49
50 -- UART0 signals:
51 uart0_txd : out std_ulogic;
52 uart0_rxd : in std_ulogic;
53
54 -- DRAM controller signals
55 alt_reset : in std_ulogic
56 );
57 end entity soc;
58
59 architecture behaviour of soc is
60
61 -- Wishbone master signals:
62 signal wishbone_dcore_in : wishbone_slave_out;
63 signal wishbone_dcore_out : wishbone_master_out;
64 signal wishbone_icore_in : wishbone_slave_out;
65 signal wishbone_icore_out : wishbone_master_out;
66 signal wishbone_debug_in : wishbone_slave_out;
67 signal wishbone_debug_out : wishbone_master_out;
68
69 -- Arbiter array (ghdl doesnt' support assigning the array
70 -- elements in the entity instantiation)
71 constant NUM_WB_MASTERS : positive := 3;
72 signal wb_masters_out : wishbone_master_out_vector(0 to NUM_WB_MASTERS-1);
73 signal wb_masters_in : wishbone_slave_out_vector(0 to NUM_WB_MASTERS-1);
74
75 -- Wishbone master (output of arbiter):
76 signal wb_master_in : wishbone_slave_out;
77 signal wb_master_out : wishbone_master_out;
78
79 -- Main "IO" bus, from main slave decoder to the latch
80 signal wb_io_in : wishbone_master_out;
81 signal wb_io_out : wishbone_slave_out;
82
83 -- Secondary (smaller) IO bus after the IO bus latch
84 signal wb_sio_out : wb_io_master_out;
85 signal wb_sio_in : wb_io_slave_out;
86
87 -- Syscon signals
88 signal dram_at_0 : std_ulogic;
89 signal do_core_reset : std_ulogic;
90 signal wb_syscon_in : wb_io_master_out;
91 signal wb_syscon_out : wb_io_slave_out;
92
93 -- UART0 signals:
94 signal wb_uart0_in : wb_io_master_out;
95 signal wb_uart0_out : wb_io_slave_out;
96 signal uart_dat8 : std_ulogic_vector(7 downto 0);
97
98 -- XICS0 signals:
99 signal wb_xics0_in : wb_io_master_out;
100 signal wb_xics0_out : wb_io_slave_out;
101 signal int_level_in : std_ulogic_vector(15 downto 0);
102
103 signal core_ext_irq : std_ulogic;
104
105 -- Main memory signals:
106 signal wb_bram_in : wishbone_master_out;
107 signal wb_bram_out : wishbone_slave_out;
108 constant mem_adr_bits : positive := positive(ceil(log2(real(MEMORY_SIZE))));
109
110 -- DMI debug bus signals
111 signal dmi_addr : std_ulogic_vector(7 downto 0);
112 signal dmi_din : std_ulogic_vector(63 downto 0);
113 signal dmi_dout : std_ulogic_vector(63 downto 0);
114 signal dmi_req : std_ulogic;
115 signal dmi_wr : std_ulogic;
116 signal dmi_ack : std_ulogic;
117
118 -- Per slave DMI signals
119 signal dmi_wb_dout : std_ulogic_vector(63 downto 0);
120 signal dmi_wb_req : std_ulogic;
121 signal dmi_wb_ack : std_ulogic;
122 signal dmi_core_dout : std_ulogic_vector(63 downto 0);
123 signal dmi_core_req : std_ulogic;
124 signal dmi_core_ack : std_ulogic;
125
126 -- Delayed/latched resets and alt_reset
127 signal rst_core : std_ulogic := '1';
128 signal rst_uart : std_ulogic := '1';
129 signal rst_xics : std_ulogic := '1';
130 signal rst_bram : std_ulogic := '1';
131 signal rst_dtm : std_ulogic := '1';
132 signal rst_wbar : std_ulogic := '1';
133 signal rst_wbdb : std_ulogic := '1';
134 signal alt_reset_d : std_ulogic;
135
136 begin
137
138 resets: process(system_clk)
139 begin
140 if rising_edge(system_clk) then
141 rst_core <= rst or do_core_reset;
142 rst_uart <= rst;
143 rst_xics <= rst;
144 rst_bram <= rst;
145 rst_dtm <= rst;
146 rst_wbar <= rst;
147 rst_wbdb <= rst;
148 alt_reset_d <= alt_reset;
149 end if;
150 end process;
151
152 -- Processor core
153 processor: entity work.core
154 generic map(
155 SIM => SIM,
156 DISABLE_FLATTEN => DISABLE_FLATTEN_CORE,
157 ALT_RESET_ADDRESS => (27 downto 0 => '0', others => '1')
158 )
159 port map(
160 clk => system_clk,
161 rst => rst_core,
162 alt_reset => alt_reset_d,
163 wishbone_insn_in => wishbone_icore_in,
164 wishbone_insn_out => wishbone_icore_out,
165 wishbone_data_in => wishbone_dcore_in,
166 wishbone_data_out => wishbone_dcore_out,
167 dmi_addr => dmi_addr(3 downto 0),
168 dmi_dout => dmi_core_dout,
169 dmi_din => dmi_dout,
170 dmi_wr => dmi_wr,
171 dmi_ack => dmi_core_ack,
172 dmi_req => dmi_core_req,
173 ext_irq => core_ext_irq
174 );
175
176 -- Wishbone bus master arbiter & mux
177 wb_masters_out <= (0 => wishbone_dcore_out,
178 1 => wishbone_icore_out,
179 2 => wishbone_debug_out);
180 wishbone_dcore_in <= wb_masters_in(0);
181 wishbone_icore_in <= wb_masters_in(1);
182 wishbone_debug_in <= wb_masters_in(2);
183 wishbone_arbiter_0: entity work.wishbone_arbiter
184 generic map(
185 NUM_MASTERS => NUM_WB_MASTERS
186 )
187 port map(
188 clk => system_clk,
189 rst => rst_wbar,
190 wb_masters_in => wb_masters_out,
191 wb_masters_out => wb_masters_in,
192 wb_slave_out => wb_master_out,
193 wb_slave_in => wb_master_in
194 );
195
196 -- Top level Wishbone slaves address decoder & mux
197 --
198 -- From CPU to BRAM, DRAM, IO, selected on top 3 bits and dram_at_0
199 -- 0000 - BRAM
200 -- 0001 - DRAM
201 -- 01xx - DRAM
202 -- 10xx - BRAM
203 -- 11xx - IO
204 --
205 slave_top_intercon: process(wb_master_out, wb_bram_out, wb_dram_out, wb_io_out, dram_at_0)
206 type slave_top_type is (SLAVE_TOP_BRAM,
207 SLAVE_TOP_DRAM,
208 SLAVE_TOP_IO);
209 variable slave_top : slave_top_type;
210 variable top_decode : std_ulogic_vector(3 downto 0);
211 begin
212 -- Top-level address decoder
213 top_decode := wb_master_out.adr(31 downto 29) & dram_at_0;
214 slave_top := SLAVE_TOP_BRAM;
215 if std_match(top_decode, "0000") then
216 slave_top := SLAVE_TOP_BRAM;
217 elsif std_match(top_decode, "0001") then
218 slave_top := SLAVE_TOP_DRAM;
219 elsif std_match(top_decode, "01--") then
220 slave_top := SLAVE_TOP_DRAM;
221 elsif std_match(top_decode, "10--") then
222 slave_top := SLAVE_TOP_BRAM;
223 elsif std_match(top_decode, "11--") then
224 slave_top := SLAVE_TOP_IO;
225 end if;
226
227 -- Top level wishbone muxing.
228 wb_bram_in <= wb_master_out;
229 wb_bram_in.cyc <= '0';
230 wb_dram_in <= wb_master_out;
231 wb_dram_in.cyc <= '0';
232 wb_io_in <= wb_master_out;
233 wb_io_in.cyc <= '0';
234 case slave_top is
235 when SLAVE_TOP_BRAM =>
236 wb_bram_in.cyc <= wb_master_out.cyc;
237 wb_master_in <= wb_bram_out;
238 when SLAVE_TOP_DRAM =>
239 wb_dram_in.cyc <= wb_master_out.cyc;
240 wb_master_in <= wb_dram_out;
241 when SLAVE_TOP_IO =>
242 wb_io_in.cyc <= wb_master_out.cyc;
243 wb_master_in <= wb_io_out;
244 end case;
245 end process slave_top_intercon;
246
247 -- IO wishbone slave 64->32 bits converter
248 --
249 -- For timing reasons, this adds a one cycle latch on the way both
250 -- in and out. This relaxes timing and routing pressure on the "main"
251 -- memory bus by moving all simple IOs to a slower 32-bit bus.
252 --
253 -- This implementation is rather dumb at the moment, no stash buffer,
254 -- so we stall whenever that latch is busy. This can be improved.
255 --
256 slave_io_latch: process(system_clk)
257 -- State
258 type state_t is (IDLE, WAIT_ACK_BOT, WAIT_ACK_TOP);
259 variable state : state_t;
260
261 -- Misc
262 variable has_top : boolean;
263 variable has_bot : boolean;
264 begin
265 if rising_edge(system_clk) then
266 if (rst) then
267 state := IDLE;
268 wb_io_out.ack <= '0';
269 wb_io_out.stall <= '0';
270 wb_sio_out.cyc <= '0';
271 wb_sio_out.stb <= '0';
272 has_top := false;
273 has_bot := false;
274 else
275 case state is
276 when IDLE =>
277 -- Clear ACK in case it was set
278 wb_io_out.ack <= '0';
279
280 -- Do we have a cycle ?
281 if wb_io_in.cyc = '1' and wb_io_in.stb = '1' then
282 -- Stall master until we are done, we are't (yet) pipelining
283 -- this, it's all slow IOs.
284 wb_io_out.stall <= '1';
285
286 -- Start cycle downstream
287 wb_sio_out.cyc <= '1';
288 wb_sio_out.stb <= '1';
289
290 -- Copy write enable to IO out, copy address as well
291 wb_sio_out.we <= wb_io_in.we;
292 wb_sio_out.adr <= wb_io_in.adr(wb_sio_out.adr'left downto 3) & "000";
293
294 -- Do we have a top word and/or a bottom word ?
295 has_top := wb_io_in.sel(7 downto 4) /= "0000";
296 has_bot := wb_io_in.sel(3 downto 0) /= "0000";
297
298 -- If we have a bottom word, handle it first, otherwise
299 -- send the top word down. XXX Split the actual mux out
300 -- and only generate a control signal.
301 if has_bot then
302 if wb_io_in.we = '1' then
303 wb_sio_out.dat <= wb_io_in.dat(31 downto 0);
304 end if;
305 wb_sio_out.sel <= wb_io_in.sel(3 downto 0);
306
307 -- Wait for ack
308 state := WAIT_ACK_BOT;
309 else
310 if wb_io_in.we = '1' then
311 wb_sio_out.dat <= wb_io_in.dat(63 downto 32);
312 end if;
313 wb_sio_out.sel <= wb_io_in.sel(7 downto 4);
314
315 -- Bump address
316 wb_sio_out.adr(2) <= '1';
317
318 -- Wait for ack
319 state := WAIT_ACK_TOP;
320 end if;
321 end if;
322 when WAIT_ACK_BOT =>
323 -- If we aren't stalled by the device, clear stb
324 if wb_sio_in.stall = '0' then
325 wb_sio_out.stb <= '0';
326 end if;
327
328 -- Handle ack
329 if wb_sio_in.ack = '1' then
330 -- If it's a read, latch the data
331 if wb_sio_out.we = '0' then
332 wb_io_out.dat(31 downto 0) <= wb_sio_in.dat;
333 end if;
334
335 -- Do we have a "top" part as well ?
336 if has_top then
337 -- Latch data & sel
338 if wb_io_in.we = '1' then
339 wb_sio_out.dat <= wb_io_in.dat(63 downto 32);
340 end if;
341 wb_sio_out.sel <= wb_io_in.sel(7 downto 4);
342
343 -- Bump address and set STB
344 wb_sio_out.adr(2) <= '1';
345 wb_sio_out.stb <= '1';
346
347 -- Wait for new ack
348 state := WAIT_ACK_TOP;
349 else
350 -- We are done, ack up, clear cyc downstram
351 wb_sio_out.cyc <= '0';
352
353 -- And ack & unstall upstream
354 wb_io_out.ack <= '1';
355 wb_io_out.stall <= '0';
356
357 -- Wait for next one
358 state := IDLE;
359 end if;
360 end if;
361 when WAIT_ACK_TOP =>
362 -- If we aren't stalled by the device, clear stb
363 if wb_sio_in.stall = '0' then
364 wb_sio_out.stb <= '0';
365 end if;
366
367 -- Handle ack
368 if wb_sio_in.ack = '1' then
369 -- If it's a read, latch the data
370 if wb_sio_out.we = '0' then
371 wb_io_out.dat(63 downto 32) <= wb_sio_in.dat;
372 end if;
373
374 -- We are done, ack up, clear cyc downstram
375 wb_sio_out.cyc <= '0';
376
377 -- And ack & unstall upstream
378 wb_io_out.ack <= '1';
379 wb_io_out.stall <= '0';
380
381 -- Wait for next one
382 state := IDLE;
383 end if;
384 end case;
385 end if;
386 end if;
387 end process;
388
389 -- IO wishbone slave intercon.
390 --
391 slave_io_intercon: process(wb_sio_out, wb_syscon_out, wb_uart0_out,
392 wb_dram_ctrl_out, wb_xics0_out)
393 -- IO branch split:
394 type slave_io_type is (SLAVE_IO_SYSCON,
395 SLAVE_IO_UART,
396 SLAVE_IO_DRAM_INIT,
397 SLAVE_IO_DRAM_CSR,
398 SLAVE_IO_ICP_0,
399 SLAVE_IO_NONE);
400 variable slave_io : slave_io_type;
401
402 variable match : std_ulogic_vector(31 downto 12);
403 begin
404
405 -- Simple address decoder.
406 slave_io := SLAVE_IO_NONE;
407 match := "11" & wb_sio_out.adr(29 downto 12);
408 if std_match(match, x"F----") then
409 slave_io := SLAVE_IO_DRAM_INIT;
410 elsif std_match(match, x"C0000") then
411 slave_io := SLAVE_IO_SYSCON;
412 elsif std_match(match, x"C0002") then
413 slave_io := SLAVE_IO_UART;
414 elsif std_match(match, x"C01--") then
415 slave_io := SLAVE_IO_DRAM_CSR;
416 elsif std_match(match, x"C0004") then
417 slave_io := SLAVE_IO_ICP_0;
418 end if;
419 wb_uart0_in <= wb_sio_out;
420 wb_uart0_in.cyc <= '0';
421
422 -- Only give xics 8 bits of wb addr
423 wb_xics0_in <= wb_sio_out;
424 wb_xics0_in.adr <= (others => '0');
425 wb_xics0_in.adr(7 downto 0) <= wb_sio_out.adr(7 downto 0);
426 wb_xics0_in.cyc <= '0';
427
428 wb_dram_ctrl_in <= wb_sio_out;
429 wb_dram_ctrl_in.cyc <= '0';
430 wb_dram_is_csr <= '0';
431 wb_dram_is_init <= '0';
432
433 wb_syscon_in <= wb_sio_out;
434 wb_syscon_in.cyc <= '0';
435
436 case slave_io is
437 when SLAVE_IO_DRAM_INIT =>
438 wb_dram_ctrl_in.cyc <= wb_sio_out.cyc;
439 wb_sio_in <= wb_dram_ctrl_out;
440 wb_dram_is_init <= '1';
441 when SLAVE_IO_DRAM_CSR =>
442 wb_dram_ctrl_in.cyc <= wb_sio_out.cyc;
443 wb_sio_in <= wb_dram_ctrl_out;
444 wb_dram_is_csr <= '1';
445 when SLAVE_IO_SYSCON =>
446 wb_syscon_in.cyc <= wb_sio_out.cyc;
447 wb_sio_in <= wb_syscon_out;
448 when SLAVE_IO_UART =>
449 wb_uart0_in.cyc <= wb_sio_out.cyc;
450 wb_sio_in <= wb_uart0_out;
451 when SLAVE_IO_ICP_0 =>
452 wb_xics0_in.cyc <= wb_sio_out.cyc;
453 wb_sio_in <= wb_xics0_out;
454 when others =>
455 wb_sio_in.dat <= (others => '1');
456 wb_sio_in.ack <= wb_sio_out.stb and wb_sio_out.cyc;
457 wb_sio_in.stall <= '0';
458 end case;
459
460 end process;
461
462 -- Syscon slave
463 syscon0: entity work.syscon
464 generic map(
465 HAS_UART => true,
466 HAS_DRAM => HAS_DRAM,
467 BRAM_SIZE => MEMORY_SIZE,
468 DRAM_SIZE => DRAM_SIZE,
469 CLK_FREQ => CLK_FREQ
470 )
471 port map(
472 clk => system_clk,
473 rst => rst,
474 wishbone_in => wb_syscon_in,
475 wishbone_out => wb_syscon_out,
476 dram_at_0 => dram_at_0,
477 core_reset => do_core_reset,
478 soc_reset => open -- XXX TODO
479 );
480
481 -- Simulated memory and UART
482
483 -- UART0 wishbone slave
484 uart0: entity work.pp_soc_uart
485 generic map(
486 FIFO_DEPTH => 32
487 )
488 port map(
489 clk => system_clk,
490 reset => rst_uart,
491 txd => uart0_txd,
492 rxd => uart0_rxd,
493 irq => int_level_in(0),
494 wb_adr_in => wb_uart0_in.adr(11 downto 0),
495 wb_dat_in => wb_uart0_in.dat(7 downto 0),
496 wb_dat_out => uart_dat8,
497 wb_cyc_in => wb_uart0_in.cyc,
498 wb_stb_in => wb_uart0_in.stb,
499 wb_we_in => wb_uart0_in.we,
500 wb_ack_out => wb_uart0_out.ack
501 );
502 wb_uart0_out.dat <= x"000000" & uart_dat8;
503 wb_uart0_out.stall <= '0' when wb_uart0_in.cyc = '0' else not wb_uart0_out.ack;
504
505 xics0: entity work.xics
506 generic map(
507 LEVEL_NUM => 16
508 )
509 port map(
510 clk => system_clk,
511 rst => rst_xics,
512 wb_in => wb_xics0_in,
513 wb_out => wb_xics0_out,
514 int_level_in => int_level_in,
515 core_irq_out => core_ext_irq
516 );
517
518 -- BRAM Memory slave
519 bram0: entity work.wishbone_bram_wrapper
520 generic map(
521 MEMORY_SIZE => MEMORY_SIZE,
522 RAM_INIT_FILE => RAM_INIT_FILE
523 )
524 port map(
525 clk => system_clk,
526 rst => rst_bram,
527 wishbone_in => wb_bram_in,
528 wishbone_out => wb_bram_out
529 );
530
531 -- DMI(debug bus) <-> JTAG bridge
532 dtm: entity work.dmi_dtm
533 generic map(
534 ABITS => 8,
535 DBITS => 64
536 )
537 port map(
538 sys_clk => system_clk,
539 sys_reset => rst_dtm,
540 dmi_addr => dmi_addr,
541 dmi_din => dmi_din,
542 dmi_dout => dmi_dout,
543 dmi_req => dmi_req,
544 dmi_wr => dmi_wr,
545 dmi_ack => dmi_ack
546 );
547
548 -- DMI interconnect
549 dmi_intercon: process(dmi_addr, dmi_req,
550 dmi_wb_ack, dmi_wb_dout,
551 dmi_core_ack, dmi_core_dout)
552
553 -- DMI address map (each address is a full 64-bit register)
554 --
555 -- Offset: Size: Slave:
556 -- 0 4 Wishbone
557 -- 10 16 Core
558
559 type slave_type is (SLAVE_WB,
560 SLAVE_CORE,
561 SLAVE_NONE);
562 variable slave : slave_type;
563 begin
564 -- Simple address decoder
565 slave := SLAVE_NONE;
566 if std_match(dmi_addr, "000000--") then
567 slave := SLAVE_WB;
568 elsif std_match(dmi_addr, "0001----") then
569 slave := SLAVE_CORE;
570 end if;
571
572 -- DMI muxing
573 dmi_wb_req <= '0';
574 dmi_core_req <= '0';
575 case slave is
576 when SLAVE_WB =>
577 dmi_wb_req <= dmi_req;
578 dmi_ack <= dmi_wb_ack;
579 dmi_din <= dmi_wb_dout;
580 when SLAVE_CORE =>
581 dmi_core_req <= dmi_req;
582 dmi_ack <= dmi_core_ack;
583 dmi_din <= dmi_core_dout;
584 when others =>
585 dmi_ack <= dmi_req;
586 dmi_din <= (others => '1');
587 end case;
588
589 -- SIM magic exit
590 if SIM and dmi_req = '1' and dmi_addr = "11111111" and dmi_wr = '1' then
591 stop;
592 end if;
593 end process;
594
595 -- Wishbone debug master (TODO: Add a DMI address decoder)
596 wishbone_debug: entity work.wishbone_debug_master
597 port map(clk => system_clk,
598 rst => rst_wbdb,
599 dmi_addr => dmi_addr(1 downto 0),
600 dmi_dout => dmi_wb_dout,
601 dmi_din => dmi_dout,
602 dmi_wr => dmi_wr,
603 dmi_ack => dmi_wb_ack,
604 dmi_req => dmi_wb_req,
605 wb_in => wishbone_debug_in,
606 wb_out => wishbone_debug_out);
607
608
609 end architecture behaviour;