top_level_de0.vhd 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. --+-----------------------------------+-------------------------------------+--
  2. --| ___ ___ | (c) 2013-2014 William R Sowerbutts |--
  3. --| ___ ___ ___ ___( _ ) / _ \ | will@sowerbutts.com |--
  4. --| / __|/ _ \ / __|_ / _ \| | | | | |--
  5. --| \__ \ (_) | (__ / / (_) | |_| | | A Z80 FPGA computer, just for fun |--
  6. --| |___/\___/ \___/___\___/ \___/ | |--
  7. --| | http://sowerbutts.com/ |--
  8. --+-----------------------------------+-------------------------------------+--
  9. --| Top level module: connects modules to each other and the outside world |--
  10. --+-------------------------------------------------------------------------+--
  11. --
  12. -- See README.txt for more details
  13. --
  14. library IEEE;
  15. use IEEE.STD_LOGIC_1164.ALL;
  16. use IEEE.NUMERIC_STD.ALL;
  17. use work.T80_Pack.ALL;
  18. entity top_level is
  19. Port (
  20. CLOCK_50 : in std_logic;
  21. LEDG : out std_logic_vector(9 downto 0);
  22. BUTTON : in std_logic_vector(2 downto 0);
  23. SW : in std_logic_vector(9 downto 0);
  24. -- UART0 (to MAX3232 level shifter chip, hardware flow control)
  25. UART_RXD : in std_logic;
  26. UART_TXD : out std_logic;
  27. -- GPIOs
  28. GPIO1_D : inout std_logic_vector(31 downto 0);
  29. GPIO0_D : inout std_logic_vector(31 downto 0);
  30. -- 7-SEG Display
  31. HEX0_D: out std_logic_vector(6 downto 0);
  32. --HEX0_DP: out std_logic;
  33. HEX1_D: out std_logic_vector(6 downto 0);
  34. --HEX1_DP: out std_logic;
  35. HEX2_D: out std_logic_vector(6 downto 0);
  36. --HEX2_DP: out std_logic;
  37. HEX3_D: out std_logic_vector(6 downto 0);
  38. --sHEX3_DP: out std_logic
  39. -- SD card socket
  40. SD_CLK : out std_logic;
  41. SD_CMD : in std_logic;
  42. SD_DAT0 : out std_logic;
  43. SD_DAT3 : out std_logic;
  44. SD_WP_N : in std_logic;
  45. -- SDRAM chip
  46. DRAM_CLK : out std_logic;
  47. DRAM_CKE : out std_logic;
  48. DRAM_CS_N : out std_logic;
  49. DRAM_RAS_N : out std_logic;
  50. DRAM_CAS_N : out std_logic;
  51. DRAM_WE_N : out std_logic;
  52. DRAM_DQM : out std_logic_vector( 1 downto 0);
  53. DRAM_ADDR : out std_logic_vector (12 downto 0);
  54. DRAM_BA : out std_logic_vector( 1 downto 0);
  55. DRAM_DQ : inout std_logic_vector (15 downto 0)
  56. );
  57. end top_level;
  58. architecture Behavioral of top_level is
  59. constant clk_freq_mhz : natural := 50; -- this is the frequency which the PLL outputs, in MHz.
  60. -- SDRAM configuration
  61. constant sdram_address_width : natural := 24;
  62. constant sdram_column_bits : natural := 9;
  63. constant cycles_per_refresh : natural := (64000*clk_freq_mhz)/8192-1;
  64. -- For simulation, we don't need a long init stage. but for real DRAM we need approx 101us.
  65. -- The constant below has a different value when interpreted by the synthesis and simulator
  66. -- tools in order to achieve the desired timing in each.
  67. constant sdram_startup_cycles: natural := 101 * clk_freq_mhz
  68. -- pragma translate_off
  69. - 10000 -- reduce the value the simulator uses
  70. -- pragma translate_on
  71. ;
  72. -- signals for clocking
  73. signal clk_feedback : std_logic; -- PLL clock feedback
  74. signal clk_unbuffered : std_logic; -- unbuffered system clock
  75. signal clk : std_logic; -- buffered system clock (all logic should be clocked by this)
  76. -- console latch
  77. signal console_select_clk1 : std_logic;
  78. signal console_select_sync : std_logic;
  79. signal swap_uart01 : std_logic := '0';
  80. -- system reset signals
  81. signal power_on_reset : std_logic_vector(1 downto 0) := (others => '1');
  82. signal system_reset : std_logic;
  83. signal reset_button_clk1 : std_logic;
  84. signal reset_button_sync : std_logic; -- reset button signal, synchronised to our clock
  85. signal reset_request_uart : std_logic; -- reset request signal from FTDI UART (when you send "!~!~!~" to the UART, this line is asserted)
  86. -- CPU control
  87. signal coldboot : std_logic;
  88. signal cpu_clk_enable : std_logic;
  89. signal cpu_m1_cycle : std_logic;
  90. signal cpu_req_mem : std_logic;
  91. signal cpu_req_io : std_logic;
  92. signal req_mem : std_logic;
  93. signal req_io : std_logic;
  94. signal req_read : std_logic;
  95. signal req_write : std_logic;
  96. signal virtual_address : std_logic_vector(15 downto 0);
  97. signal physical_address : std_logic_vector(25 downto 0);
  98. signal mem_wait : std_logic;
  99. signal cpu_wait : std_logic;
  100. signal dram_wait : std_logic;
  101. signal mmu_wait : std_logic;
  102. signal spimaster0_wait : std_logic;
  103. signal spimaster1_wait : std_logic;
  104. -- chip selects
  105. signal mmu_cs : std_logic;
  106. signal rom_cs : std_logic;
  107. signal sram_cs : std_logic;
  108. signal dram_cs : std_logic;
  109. signal uartA_cs : std_logic;
  110. signal uartB_cs : std_logic;
  111. signal uart0_cs : std_logic;
  112. signal uart1_cs : std_logic;
  113. signal timer_cs : std_logic;
  114. signal spimaster0_cs : std_logic;
  115. signal spimaster1_cs : std_logic;
  116. signal clkscale_cs : std_logic;
  117. signal gpio_cs : std_logic;
  118. -- data bus
  119. signal cpu_data_in : std_logic_vector(7 downto 0);
  120. signal cpu_data_out : std_logic_vector(7 downto 0);
  121. signal rom_data_out : std_logic_vector(7 downto 0);
  122. signal sram_data_out : std_logic_vector(7 downto 0);
  123. signal dram_data_out : std_logic_vector(7 downto 0);
  124. signal uart0_data_out : std_logic_vector(7 downto 0);
  125. signal uart1_data_out : std_logic_vector(7 downto 0);
  126. signal timer_data_out : std_logic_vector(7 downto 0);
  127. signal spimaster0_data_out : std_logic_vector(7 downto 0);
  128. signal spimaster1_data_out : std_logic_vector(7 downto 0);
  129. signal mmu_data_out : std_logic_vector(7 downto 0);
  130. signal clkscale_out : std_logic_vector(7 downto 0);
  131. signal gpio_data_out : std_logic_vector(7 downto 0);
  132. -- GPIO
  133. signal gpio_input : std_logic_vector(7 downto 0);
  134. signal gpio_output : std_logic_vector(7 downto 0);
  135. signal gpio_bank0_input : std_logic_vector(31 downto 0);
  136. signal gpio_bank0_output : std_logic_vector(31 downto 0);
  137. signal gpio_bank1_input : std_logic_vector(31 downto 0);
  138. signal gpio_bank1_output : std_logic_vector(31 downto 0);
  139. -- Interrupts
  140. signal cpu_interrupt_in : std_logic;
  141. signal timer_interrupt : std_logic;
  142. signal uart0_interrupt : std_logic;
  143. signal uart1_interrupt : std_logic;
  144. begin
  145. -- Hold CPU reset high for 8 clock cycles on startup,
  146. -- and when the user presses their reset button.
  147. process(clk)
  148. begin
  149. if rising_edge(clk) then
  150. -- Xilinx advises using two flip-flops are used to bring external
  151. -- signals which feed control logic into our clock domain.
  152. reset_button_clk1 <= not BUTTON(0);
  153. reset_button_sync <= reset_button_clk1;
  154. console_select_clk1 <= SW(9);
  155. console_select_sync <= console_select_clk1;
  156. -- reset the system when requested
  157. if (power_on_reset(0) = '1' or reset_button_sync = '1' or reset_request_uart = '1') then
  158. system_reset <= '1';
  159. else
  160. system_reset <= '0';
  161. end if;
  162. -- shift 0s into the power_on_reset shift register from the MSB
  163. power_on_reset <= '0' & power_on_reset(power_on_reset'length-1 downto 1);
  164. -- During reset, latch the console select jumper. This is used to
  165. -- optionally swap over the UART roles and move the system console to
  166. -- the second serial port on the IO board.
  167. if system_reset = '1' then
  168. swap_uart01 <= console_select_sync;
  169. else
  170. swap_uart01 <= swap_uart01;
  171. end if;
  172. end if;
  173. end process;
  174. -- GPIO input signal routing
  175. gpio_input(0) <= SW(0);
  176. gpio_input(1) <= SW(1);
  177. gpio_input(2) <= SW(2);
  178. gpio_input(3) <= SW(3);
  179. gpio_input(4) <= SW(4);
  180. gpio_input(5) <= SW(5);
  181. gpio_input(6) <= swap_uart01;
  182. gpio_input(7) <= coldboot;
  183. -- GPIO output signal routing
  184. LEDG(0) <= gpio_output(0);
  185. LEDG(1) <= gpio_output(1);
  186. LEDG(2) <= gpio_output(2);
  187. LEDG(3) <= gpio_output(3);
  188. LEDG(4) <= gpio_output(4);
  189. LEDG(5) <= gpio_output(5);
  190. LEDG(6) <= gpio_output(6);
  191. LEDG(7) <= gpio_output(7);
  192. -- User LED (LED1) on Papilio Pro indicates when the CPU is being asked to wait (eg by the SDRAM cache)
  193. LEDG(9) <= cpu_wait;
  194. --
  195. LEDG(8) <= clk or not(BUTTON(2));
  196. -- Interrupt signal for the CPU
  197. cpu_interrupt_in <= (timer_interrupt or uart0_interrupt or uart1_interrupt);
  198. -- 7 Seg
  199. seg0: entity work.DE0_SEG7 port map(virtual_address(3 downto 0), HEX0_D);
  200. seg1: entity work.DE0_SEG7 port map(virtual_address(7 downto 4), HEX1_D);
  201. seg2: entity work.DE0_SEG7 port map(virtual_address(11 downto 8), HEX2_D);
  202. seg3: entity work.DE0_SEG7 port map(virtual_address(15 downto 12), HEX3_D);
  203. -- Z80 CPU core
  204. cpu: entity work.Z80cpu
  205. port map (
  206. reset => system_reset,
  207. clk => clk,
  208. clk_enable => cpu_clk_enable,
  209. m1_cycle => cpu_m1_cycle,
  210. interrupt => cpu_interrupt_in,
  211. nmi => '0',
  212. req_mem => cpu_req_mem,
  213. req_io => cpu_req_io,
  214. req_read => req_read,
  215. req_write => req_write,
  216. mem_wait => cpu_wait,
  217. address => virtual_address,
  218. data_in => cpu_data_in,
  219. data_out => cpu_data_out
  220. );
  221. -- Memory management unit
  222. mmu: entity work.MMU
  223. port map (
  224. reset => system_reset,
  225. clk => clk,
  226. address_in => virtual_address,
  227. address_out => physical_address,
  228. cpu_data_in => cpu_data_out,
  229. cpu_data_out => mmu_data_out,
  230. req_mem_in => cpu_req_mem,
  231. req_io_in => cpu_req_io,
  232. req_mem_out => req_mem,
  233. req_io_out => req_io,
  234. req_read => req_read,
  235. req_write => req_write,
  236. io_cs => mmu_cs,
  237. cpu_wait => mmu_wait,
  238. access_violated => open -- for now!!
  239. );
  240. -- This process determines which IO or memory device the CPU is addressing
  241. -- and asserts the appropriate chip select signals.
  242. cs_process: process(req_mem, req_io, physical_address, virtual_address, uartA_cs, uartB_cs, swap_uart01)
  243. begin
  244. -- memory chip selects: default to unselected
  245. rom_cs <= '0';
  246. sram_cs <= '0';
  247. dram_cs <= '0';
  248. -- io chip selects: default to unselected
  249. uartA_cs <= '0';
  250. uartB_cs <= '0';
  251. mmu_cs <= '0';
  252. timer_cs <= '0';
  253. spimaster0_cs <= '0';
  254. spimaster1_cs <= '0';
  255. clkscale_cs <= '0';
  256. gpio_cs <= '0';
  257. -- memory address decoding
  258. -- address space is organised as:
  259. -- 0x0 000 000 - 0x0 FFF FFF 16MB DRAM (cached) (mapped to 8MB DRAM twice)
  260. -- 0x1 000 000 - 0x1 FFF FFF 16MB DRAM (uncached) (mapped to 8MB DRAM twice)
  261. -- 0x2 000 000 - 0x2 000 FFF 4KB monitor ROM (FPGA block RAM)
  262. -- 0x2 001 000 - 0x2 001 FFF 4KB SRAM (FPGA block RAM)
  263. -- 0x2 002 000 - 0x3 FFF FFF unused space for future expansion
  264. if physical_address(25) = '0' then
  265. -- bottom 32MB: DRAM handles this
  266. dram_cs <= req_mem;
  267. else
  268. -- top 32MB: other memory devices
  269. case physical_address(24 downto 12) is
  270. when "0000000000000" => rom_cs <= req_mem;
  271. when "0000000000001" => sram_cs <= req_mem;
  272. when others => -- undecoded memory space
  273. end case;
  274. end if;
  275. -- IO address decoding
  276. case virtual_address(7 downto 3) is
  277. when "00000" => uartA_cs <= req_io; -- 00 ... 07
  278. when "00010" => timer_cs <= req_io; -- 10 ... 17
  279. when "00011" => spimaster0_cs <= req_io; -- 18 ... 1F
  280. when "00100" => gpio_cs <= req_io; -- 20 ... 27
  281. when "00101" => uartB_cs <= req_io; -- 28 ... 2F
  282. when "00110" => spimaster1_cs <= req_io; -- 30 ... 37
  283. -- unused ports
  284. when "11110" => clkscale_cs <= req_io; -- F0 ... F7
  285. when "11111" => mmu_cs <= req_io; -- F8 ... FF
  286. when others =>
  287. end case;
  288. -- send the UART chip select to the appropriate UART depending
  289. -- on whether they have been swapped over or not.
  290. if swap_uart01 = '0' then
  291. uart0_cs <= uartB_cs;
  292. uart1_cs <= uartA_cs;
  293. else
  294. uart0_cs <= uartA_cs;
  295. uart1_cs <= uartB_cs;
  296. end if;
  297. end process;
  298. -- the selected memory device can request the CPU to wait
  299. mem_wait <=
  300. dram_wait when dram_cs='1' else
  301. spimaster0_wait when spimaster0_cs='1' else
  302. spimaster1_wait when spimaster1_cs='1' else
  303. '0';
  304. -- the MMU can, at any time, request the CPU wait (this is used when
  305. -- translating IO to memory requests, to implement a wait state for
  306. -- the "17th page")
  307. cpu_wait <= (mem_wait or mmu_wait);
  308. -- input mux for CPU data bus
  309. cpu_data_in <=
  310. rom_data_out when rom_cs='1' else
  311. dram_data_out when dram_cs='1' else
  312. sram_data_out when sram_cs='1' else
  313. uart0_data_out when uart0_cs='1' else
  314. uart1_data_out when uart1_cs='1' else
  315. timer_data_out when timer_cs='1' else
  316. mmu_data_out when mmu_cs='1' else
  317. spimaster0_data_out when spimaster0_cs='1' else
  318. spimaster1_data_out when spimaster1_cs='1' else
  319. clkscale_out when clkscale_cs='1' else
  320. gpio_data_out when gpio_cs='1' else
  321. rom_data_out; -- default case
  322. dram: entity work.DRAM
  323. generic map(
  324. sdram_address_width => sdram_address_width,
  325. sdram_column_bits => sdram_column_bits,
  326. sdram_startup_cycles=> sdram_startup_cycles,
  327. cycles_per_refresh => cycles_per_refresh
  328. )
  329. port map(
  330. clk => clk,
  331. reset => '0', -- important to note that we DO NOT reset the SDRAM controller on reset (it would stop refreshing, which would be bad)
  332. -- interface to synthetic CPU
  333. cs => dram_cs,
  334. req_read => req_read,
  335. req_write => req_write,
  336. mem_address => physical_address(24 downto 0),
  337. mem_wait => dram_wait,
  338. data_in => cpu_data_out,
  339. data_out => dram_data_out,
  340. coldboot => coldboot,
  341. -- interface to hardware SDRAM chip
  342. SDRAM_CLK => open,
  343. SDRAM_CKE => DRAM_CKE,
  344. SDRAM_CS => DRAM_CS_N,
  345. SDRAM_nCAS => DRAM_CAS_N,
  346. SDRAM_nRAS => DRAM_RAS_N,
  347. SDRAM_nWE => DRAM_WE_N,
  348. SDRAM_DQM => DRAM_DQM,
  349. SDRAM_BA => DRAM_BA,
  350. SDRAM_ADDR => DRAM_ADDR,
  351. SDRAM_DQ => DRAM_DQ
  352. );
  353. -- 4KB system ROM implemented in block RAM
  354. rom: entity work.MonZ80
  355. port map(
  356. clk => clk,
  357. A => physical_address(11 downto 0),
  358. D => rom_data_out
  359. );
  360. -- 4KB SRAM memory implemented in block RAM
  361. sram: entity work.SSRAM
  362. generic map(
  363. AddrWidth => 12
  364. )
  365. port map(
  366. clk => clk,
  367. ce => sram_cs,
  368. we => req_write,
  369. A => physical_address(11 downto 0),
  370. DIn => cpu_data_out,
  371. DOut => sram_data_out
  372. );
  373. -- UART connected to FTDI USB UART
  374. uart0: entity work.uart_interface
  375. generic map ( watch_for_reset => 1, clk_frequency => (clk_freq_mhz * 1000000) )
  376. port map(
  377. clk => clk,
  378. reset => system_reset,
  379. reset_out => reset_request_uart, -- result of watching for reset sequence on the input
  380. serial_in => UART_RXD,
  381. serial_out => UART_TXD,
  382. serial_rts => open,
  383. serial_cts => '0',
  384. cpu_address => virtual_address(2 downto 0),
  385. cpu_data_in => cpu_data_out,
  386. cpu_data_out => uart0_data_out,
  387. enable => uart0_cs,
  388. interrupt => uart0_interrupt,
  389. req_read => req_read,
  390. req_write => req_write
  391. );
  392. -- UART connected to two GPIOs (1(30), 1(31))
  393. uart1: entity work.uart_interface
  394. generic map ( watch_for_reset => 1, clk_frequency => (clk_freq_mhz * 1000000) )
  395. port map(
  396. clk => clk,
  397. reset => system_reset,
  398. reset_out => open,
  399. serial_in => GPIO1_D(30),
  400. serial_out => GPIO1_D(31),
  401. serial_rts => open,
  402. serial_cts => '0',
  403. cpu_address => virtual_address(2 downto 0),
  404. cpu_data_in => cpu_data_out,
  405. cpu_data_out => uart1_data_out,
  406. enable => uart1_cs,
  407. interrupt => uart1_interrupt,
  408. req_read => req_read,
  409. req_write => req_write
  410. );
  411. -- Timer device (internally scales the clock to 1MHz)
  412. timer: entity work.timer
  413. generic map ( clk_frequency => (clk_freq_mhz * 1000000) )
  414. port map(
  415. clk => clk,
  416. reset => system_reset,
  417. cpu_address => virtual_address(2 downto 0),
  418. data_in => cpu_data_out,
  419. data_out => timer_data_out,
  420. enable => timer_cs,
  421. req_read => req_read,
  422. req_write => req_write,
  423. interrupt => timer_interrupt
  424. );
  425. -- GPIO to FPGA pins and/or internal signals
  426. gpio: entity work.gpio
  427. port map(
  428. clk => clk,
  429. reset => system_reset,
  430. cpu_address => virtual_address(2 downto 0),
  431. data_in => cpu_data_out,
  432. data_out => gpio_data_out,
  433. enable => gpio_cs,
  434. read_notwrite => req_read,
  435. input_pins => gpio_input,
  436. output_pins => gpio_output
  437. );
  438. -- SPI master device connected to SD card socket on the IO board
  439. spimaster1: entity work.spimaster
  440. port map(
  441. clk => clk,
  442. reset => system_reset,
  443. cpu_address => virtual_address(2 downto 0),
  444. cpu_wait => spimaster1_wait,
  445. data_in => cpu_data_out,
  446. data_out => spimaster1_data_out,
  447. enable => spimaster1_cs,
  448. req_read => req_read,
  449. req_write => req_write,
  450. slave_cs => SD_DAT3,
  451. slave_clk => SD_CLK,
  452. slave_mosi => SD_DAT0,
  453. slave_miso => SD_CMD
  454. );
  455. -- An attempt to allow the CPU clock to be scaled back to run
  456. -- at slower speeds without affecting the clock signal sent to
  457. -- IO devices. Basically this was an attempt to make CP/M games
  458. -- playable :) Very limited success. Might be simpler to remove
  459. -- this entirely.
  460. clkscale: entity work.clkscale
  461. port map (
  462. clk => clk,
  463. reset => system_reset,
  464. cpu_address => virtual_address(2 downto 0),
  465. data_in => cpu_data_out,
  466. data_out => clkscale_out,
  467. enable => clkscale_cs,
  468. read_notwrite => req_read,
  469. clk_enable => cpu_clk_enable
  470. );
  471. pll: entity work.pll
  472. port map (
  473. areset => open,
  474. inclk0 => CLOCK_50,
  475. c0 => DRAM_CLK, -- 100 Mhz - 180 deg
  476. c1 => clk, -- 100 Mhz
  477. locked => open
  478. );
  479. end Behavioral;