top_level_de2.vhd 18 KB

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