MemoryUnit.v 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*
  2. * Memory Unit
  3. */
  4. module MemoryUnit(
  5. // Clocks
  6. input clk,
  7. input reset,
  8. // Bus
  9. input [26:0] bus_addr,
  10. input [31:0] bus_data,
  11. input bus_we,
  12. input bus_start,
  13. output [31:0] bus_q,
  14. output reg bus_done = 1'b0,
  15. /********
  16. * MEMORY
  17. ********/
  18. //SPI Flash / SPI0
  19. inout SPIflash_data, SPIflash_q, SPIflash_wp, SPIflash_hold,
  20. output SPIflash_cs,
  21. output SPIflash_clk,
  22. //VRAM32 cpu port
  23. output [31:0] VRAM32_cpu_d,
  24. output [13:0] VRAM32_cpu_addr,
  25. output VRAM32_cpu_we,
  26. input [31:0] VRAM32_cpu_q,
  27. //VRAM8 cpu port
  28. output [7:0] VRAM8_cpu_d,
  29. output [13:0] VRAM8_cpu_addr,
  30. output VRAM8_cpu_we,
  31. input [7:0] VRAM8_cpu_q,
  32. //VRAMspr cpu port
  33. output [8:0] VRAMspr_cpu_d,
  34. output [13:0] VRAMspr_cpu_addr,
  35. output VRAMspr_cpu_we,
  36. input [8:0] VRAMspr_cpu_q,
  37. //VRAMpx cpu port
  38. output [7:0] VRAMpx_cpu_d,
  39. output [16:0] VRAMpx_cpu_addr,
  40. output VRAMpx_cpu_we,
  41. input [7:0] VRAMpx_cpu_q,
  42. //ROM
  43. output [8:0] ROM_addr,
  44. input [31:0] ROM_q,
  45. /********
  46. * I/O
  47. ********/
  48. //UART0 (Main USB)
  49. input UART0_in,
  50. output UART0_out,
  51. output UART0_rx_interrupt,
  52. //UART1 (APU) DEPRECATED
  53. //input UART1_in,
  54. //output UART1_out,
  55. //output UART1_rx_interrupt,
  56. //UART2 (GP)
  57. input UART2_in,
  58. output UART2_out,
  59. output UART2_rx_interrupt,
  60. //SPI0 (Flash)
  61. //declared under MEMORY
  62. output SPI0_QSPI,
  63. //SPI1 (USB0/CH376T)
  64. output SPI1_clk,
  65. output reg SPI1_cs = 1'b1,
  66. output SPI1_mosi,
  67. input SPI1_miso,
  68. input SPI1_nint,
  69. //SPI2 (USB1/CH376T)
  70. output SPI2_clk,
  71. output reg SPI2_cs = 1'b1,
  72. output SPI2_mosi,
  73. input SPI2_miso,
  74. input SPI2_nint,
  75. //SPI3 (W5500)
  76. output SPI3_clk,
  77. output reg SPI3_cs = 1'b1,
  78. output SPI3_mosi,
  79. input SPI3_miso,
  80. input SPI3_int,
  81. //SPI4 (EXT/GP)
  82. output SPI4_clk,
  83. output reg SPI4_cs = 1'b1,
  84. output SPI4_mosi,
  85. input SPI4_miso,
  86. input SPI4_GP,
  87. //GPIO (Separated GPI and GPO until GPIO module is implemented)
  88. input [3:0] GPI,
  89. output reg [3:0]GPO = 4'd0,
  90. //OStimers
  91. output OST1_int,
  92. output OST2_int,
  93. output OST3_int,
  94. //PS/2
  95. input PS2_clk, PS2_data,
  96. output PS2_int, //Scan code ready signal
  97. //Boot mode
  98. input boot_mode
  99. );
  100. // Address select parameters
  101. localparam
  102. A_SDRAM = 0,
  103. A_FLASH = 1,
  104. A_VRAM32 = 2,
  105. A_VRAM8 = 3,
  106. A_VRAMSPR = 4,
  107. A_ROM = 5,
  108. A_UART0RX = 6,
  109. A_UART0TX = 7,
  110. //A_UART1RX = 8,
  111. //A_UART1TX = 9,
  112. A_UART2RX = 10,
  113. A_UART2TX = 11,
  114. A_SPI0 = 12,
  115. A_SPI0CS = 13,
  116. A_SPI0EN = 14,
  117. A_SPI1 = 15,
  118. A_SPI1CS = 16,
  119. A_SPI1NINT = 17,
  120. A_SPI2 = 18,
  121. A_SPI2CS = 19,
  122. A_SPI2NINT = 20,
  123. A_SPI3 = 21,
  124. A_SPI3CS = 22,
  125. A_SPI3INT = 23,
  126. A_SPI4 = 24,
  127. A_SPI4CS = 25,
  128. A_SPI4GP = 26,
  129. A_GPIO = 27,
  130. A_GPIODIR = 28,
  131. A_TIMER1VAL = 29,
  132. A_TIMER1CTRL = 30,
  133. A_TIMER2VAL = 31,
  134. A_TIMER2CTRL = 32,
  135. A_TIMER3VAL = 33,
  136. A_TIMER3CTRL = 34,
  137. //A_SNESPAD = 35,
  138. A_PS2 = 36,
  139. A_BOOTMODE = 37,
  140. A_VRAMPX = 38;
  141. //------------
  142. //SPI0 (flash) TODO: move this to a separate module
  143. //------------
  144. //SPIreader
  145. wire [23:0] SPIflashReader_addr; //address of flash (32 bit)
  146. wire SPIflashReader_start; //start signal for SPIreader
  147. wire SPIflashReader_cs; //cs
  148. wire [31:0] SPIflashReader_q; //data out
  149. wire SPIflashReader_initDone; //initdone of SPIreader
  150. wire SPIflashReader_recvDone; //recvdone of SPIreader TODO might change this to busy
  151. wire SPIflashReader_reset; //reset SPIreader
  152. wire SPIflashReader_write; //output mode of inout pins (high when writing to SPI flash)
  153. wire SPIflashReader_clk; //clk for spi flash
  154. wire io0_out, io1_out, io2_out, io3_out; //d, q wp, hold output
  155. wire io0_in, io1_in, io2_in, io3_in; //d, q wp, hold input
  156. SPIreader sreader (
  157. .clk (clk),
  158. .reset (SPIflashReader_reset),
  159. .cs (SPIflashReader_cs),
  160. .address (SPIflashReader_addr),
  161. .instr (SPIflashReader_q),
  162. .start (SPIflashReader_start),
  163. .initDone (SPIflashReader_initDone),
  164. .recvDone (SPIflashReader_recvDone),
  165. .write (SPIflashReader_write),
  166. .spi_clk (SPIflashReader_clk),
  167. .io0_out (io0_out),
  168. .io1_out (io1_out),
  169. .io2_out (io2_out),
  170. .io3_out (io3_out),
  171. .io0_in (io0_in),
  172. .io1_in (io1_in),
  173. .io2_in (io2_in),
  174. .io3_in (io3_in)
  175. );
  176. //SPI0 (flash)
  177. wire SPI0_clk;
  178. wire SPI0_mosi;
  179. reg SPI0_cs = 1'b1;
  180. reg SPI0_enable = 1'b0; //high enables SPI0 and disables SPIreader
  181. wire SPI0_start;
  182. wire [7:0] SPI0_in;
  183. wire [7:0] SPI0_out;
  184. wire SPI0_done;
  185. assign SPI0_QSPI = ~SPI0_enable;
  186. SimpleSPI #(
  187. .CLKS_PER_HALF_BIT(1))
  188. SPI0(
  189. .clk (clk),
  190. .reset (reset),
  191. .in_byte (SPI0_in),
  192. .start (SPI0_start),
  193. .done (SPI0_done),
  194. .out_byte (SPI0_out),
  195. .spi_clk (SPI0_clk),
  196. .miso (SPIflash_q),
  197. .mosi (SPI0_mosi)
  198. );
  199. //Tri-state signals
  200. wire SPIcombined_d, SPIcombined_q, SPIcombined_wp, SPIcombined_hold, SPIcombined_OutputEnable;
  201. assign SPIflash_clk = (SPI0_enable) ? SPI0_clk : SPIflashReader_clk;
  202. assign SPIflash_cs = (SPI0_enable) ? SPI0_cs : SPIflashReader_cs;
  203. assign SPIflashReader_reset = (SPI0_enable) ? 1'b1 : reset;
  204. assign SPIcombined_d = (SPI0_enable) ? SPI0_mosi : io0_out;
  205. assign SPIcombined_q = (SPI0_enable) ? 1'bz : io1_out;
  206. assign SPIcombined_wp = (SPI0_enable) ? 1'b1 : io2_out;
  207. assign SPIcombined_hold = (SPI0_enable) ? 1'b1 : io3_out;
  208. assign SPIcombined_OutputEnable = (SPI0_enable) ? 1'b1 : SPIflashReader_write;
  209. assign SPIflash_data = (SPIcombined_OutputEnable) ? SPIcombined_d : 1'bz;
  210. assign SPIflash_q = (SPIcombined_OutputEnable) ? SPIcombined_q : 1'bz;
  211. assign SPIflash_wp = (SPIcombined_OutputEnable) ? SPIcombined_wp : 1'bz;
  212. assign SPIflash_hold = (SPIcombined_OutputEnable) ? SPIcombined_hold : 1'bz;
  213. assign io0_in = (~SPIcombined_OutputEnable) ? SPIflash_data : 1'bz;
  214. assign io1_in = (~SPIcombined_OutputEnable) ? SPIflash_q : 1'bz;
  215. assign io2_in = (~SPIcombined_OutputEnable) ? SPIflash_wp : 1'bz;
  216. assign io3_in = (~SPIcombined_OutputEnable) ? SPIflash_hold : 1'bz;
  217. //------------
  218. //UART0
  219. //------------
  220. wire UART0_r_Tx_DV, UART0_w_Tx_Done;
  221. wire [7:0] UART0_r_Tx_Byte;
  222. UARTtx UART0_tx(
  223. .i_Clock (clk),
  224. .reset (reset),
  225. .i_Tx_DV (UART0_r_Tx_DV),
  226. .i_Tx_Byte (UART0_r_Tx_Byte),
  227. .o_Tx_Active(),
  228. .o_Tx_Serial(UART0_out),
  229. .o_Tx_Done (UART0_w_Tx_Done)
  230. );
  231. wire [7:0] UART0_w_Rx_Byte;
  232. UARTrx UART0_rx(
  233. .i_Clock (clk),
  234. .reset (reset),
  235. .i_Rx_Serial(UART0_in),
  236. .o_Rx_DV (UART0_rx_interrupt),
  237. .o_Rx_Byte (UART0_w_Rx_Byte)
  238. );
  239. //------------
  240. //UART1
  241. //------------
  242. /*
  243. wire UART1_r_Tx_DV, UART1_w_Tx_Done;
  244. wire [7:0] UART1_r_Tx_Byte;
  245. UARTtx UART1_tx(
  246. .i_Clock (clk),
  247. .reset (reset),
  248. .i_Tx_DV (UART1_r_Tx_DV),
  249. .i_Tx_Byte (UART1_r_Tx_Byte),
  250. .o_Tx_Active(),
  251. .o_Tx_Serial(UART1_out),
  252. .o_Tx_Done (UART1_w_Tx_Done)
  253. );
  254. wire [7:0] UART1_w_Rx_Byte;
  255. UARTrx UART1_rx(
  256. .i_Clock (clk),
  257. .reset (reset),
  258. .i_Rx_Serial(UART1_in),
  259. .o_Rx_DV (UART1_rx_interrupt),
  260. .o_Rx_Byte (UART1_w_Rx_Byte)
  261. );
  262. */
  263. //------------
  264. //UART2
  265. //------------
  266. wire UART2_r_Tx_DV, UART2_w_Tx_Done;
  267. wire [7:0] UART2_r_Tx_Byte;
  268. UARTtx UART2_tx(
  269. .i_Clock (clk),
  270. .reset (reset),
  271. .i_Tx_DV (UART2_r_Tx_DV),
  272. .i_Tx_Byte (UART2_r_Tx_Byte),
  273. .o_Tx_Active(),
  274. .o_Tx_Serial(UART2_out),
  275. .o_Tx_Done (UART2_w_Tx_Done)
  276. );
  277. wire [7:0] UART2_w_Rx_Byte;
  278. UARTrx UART2_rx(
  279. .i_Clock (clk),
  280. .reset (reset),
  281. .i_Rx_Serial(UART2_in),
  282. .o_Rx_DV (UART2_rx_interrupt),
  283. .o_Rx_Byte (UART2_w_Rx_Byte)
  284. );
  285. //------------
  286. //SPI1 (CH376T bottom)
  287. //------------
  288. wire SPI1_start;
  289. wire [7:0] SPI1_in;
  290. wire [7:0] SPI1_out;
  291. wire SPI1_done;
  292. SimpleSPI #(
  293. .CLKS_PER_HALF_BIT(2))
  294. SPI1(
  295. .clk (clk),
  296. .reset (reset),
  297. .in_byte (SPI1_in),
  298. .start (SPI1_start),
  299. .done (SPI1_done),
  300. .out_byte (SPI1_out),
  301. .spi_clk (SPI1_clk),
  302. .miso (SPI1_miso),
  303. .mosi (SPI1_mosi)
  304. );
  305. //------------
  306. //SPI2 (CH376T top)
  307. //------------
  308. wire SPI2_start;
  309. wire [7:0] SPI2_in;
  310. wire [7:0] SPI2_out;
  311. wire SPI2_done;
  312. SimpleSPI #(
  313. .CLKS_PER_HALF_BIT(2))
  314. SPI2(
  315. .clk (clk),
  316. .reset (reset),
  317. .in_byte (SPI2_in),
  318. .start (SPI2_start),
  319. .done (SPI2_done),
  320. .out_byte (SPI2_out),
  321. .spi_clk (SPI2_clk),
  322. .miso (SPI2_miso),
  323. .mosi (SPI2_mosi)
  324. );
  325. //------------
  326. //SPI3 (W5500)
  327. //------------
  328. wire SPI3_start;
  329. wire [7:0] SPI3_in;
  330. wire [7:0] SPI3_out;
  331. wire SPI3_done;
  332. SimpleSPI #(
  333. .CLKS_PER_HALF_BIT(1))
  334. SPI3(
  335. .clk (clk),
  336. .reset (reset),
  337. .in_byte (SPI3_in),
  338. .start (SPI3_start),
  339. .done (SPI3_done),
  340. .out_byte (SPI3_out),
  341. .spi_clk (SPI3_clk),
  342. .miso (SPI3_miso),
  343. .mosi (SPI3_mosi)
  344. );
  345. //------------
  346. //SPI4 (EXT/GP)
  347. //------------
  348. wire SPI4_start;
  349. wire [7:0] SPI4_in;
  350. wire [7:0] SPI4_out;
  351. wire SPI4_done;
  352. SimpleSPI #(
  353. .CLKS_PER_HALF_BIT(2))
  354. SPI4(
  355. .clk (clk),
  356. .reset (reset),
  357. .in_byte (SPI4_in),
  358. .start (SPI4_start),
  359. .done (SPI4_done),
  360. .out_byte (SPI4_out),
  361. .spi_clk (SPI4_clk),
  362. .miso (SPI4_miso),
  363. .mosi (SPI4_mosi)
  364. );
  365. //------------
  366. //GPIO
  367. //------------
  368. // TODO: To be implemented
  369. //------------
  370. //OS timer 1
  371. //------------
  372. wire OST1_trigger, OST1_set;
  373. wire [31:0] OST1_value;
  374. OStimer OST1(
  375. .clk (clk),
  376. .reset (reset),
  377. .timerValue (OST1_value),
  378. .setValue (OST1_set),
  379. .trigger (OST1_trigger),
  380. .interrupt (OST1_int)
  381. );
  382. //------------
  383. //OS timer 2
  384. //------------
  385. wire OST2_trigger, OST2_set;
  386. wire [31:0] OST2_value;
  387. OStimer OST2(
  388. .clk (clk),
  389. .reset (reset),
  390. .timerValue (OST2_value),
  391. .setValue (OST2_set),
  392. .trigger (OST2_trigger),
  393. .interrupt (OST2_int)
  394. );
  395. //------------
  396. //OS timer 3
  397. //------------
  398. wire OST3_trigger, OST3_set;
  399. wire [31:0] OST3_value;
  400. OStimer OST3(
  401. .clk (clk),
  402. .reset (reset),
  403. .timerValue (OST3_value),
  404. .setValue (OST3_set),
  405. .trigger (OST3_trigger),
  406. .interrupt (OST3_int)
  407. );
  408. //------------
  409. //SNES controller
  410. //------------
  411. /*
  412. wire [15:0] SNES_state;
  413. wire SNES_done;
  414. wire SNES_start;
  415. NESpadReader npr (
  416. .clk(clk),
  417. .reset(reset),
  418. .nesc(SNES_clk),
  419. .nesl(SNES_latch),
  420. .nesd(SNES_data),
  421. .nesState(SNES_state),
  422. .frame(SNES_start),
  423. .done(SNES_done)
  424. );*/
  425. //------------
  426. //PS/2 keyboard
  427. //------------
  428. wire [7:0] PS2_scanCode;
  429. Keyboard PS2Keyboard (
  430. .clk (clk),
  431. .reset (reset),
  432. .ps2d (PS2_data),
  433. .ps2c (PS2_clk),
  434. .rx_done_tick (PS2_int),
  435. .rx_data (PS2_scanCode)
  436. );
  437. reg [31:0] bus_d_reg = 32'd0;
  438. //----
  439. //MEMORY
  440. //----
  441. //SPI FLASH MEMORY
  442. assign SPIflashReader_addr = bus_addr - 27'h800000;
  443. assign SPIflashReader_start = bus_addr >= 27'h800000 && bus_addr < 27'hC00000 && bus_start;
  444. //VRAM32
  445. assign VRAM32_cpu_addr = bus_addr - 27'hC00000;
  446. assign VRAM32_cpu_d = bus_d_reg;
  447. assign VRAM32_cpu_we = bus_addr >= 27'hC00000 && bus_addr < 27'hC00420 && bus_we;
  448. //VRAM8
  449. assign VRAM8_cpu_addr = bus_addr - 27'hC00420;
  450. assign VRAM8_cpu_d = bus_data;
  451. assign VRAM8_cpu_we = bus_addr >= 27'hC00420 && bus_addr < 27'hC02422 && bus_we;
  452. //VRAMspr
  453. assign VRAMspr_cpu_addr = bus_addr - 27'hC02422;
  454. assign VRAMspr_cpu_d = bus_data;
  455. assign VRAMspr_cpu_we = bus_addr >= 27'hC02422 && bus_addr < 27'hC02522 && bus_we;
  456. //VRAMpx
  457. assign VRAMpx_cpu_addr = bus_addr - 27'hD00000;
  458. assign VRAMpx_cpu_d = bus_data;
  459. assign VRAMpx_cpu_we = bus_addr >= 27'hD00000 && bus_addr < 27'hD12C00 && bus_we;
  460. //ROM
  461. assign ROM_addr = bus_addr - 27'hC02522;
  462. //----
  463. //I/O
  464. //----
  465. //UART
  466. assign UART0_r_Tx_DV = bus_addr == 27'hC02723 && bus_we && bus_start;
  467. assign UART0_r_Tx_Byte = bus_data;
  468. //assign UART1_r_Tx_DV = bus_addr == 27'hC02725 && bus_we && bus_start;
  469. //assign UART1_r_Tx_Byte = bus_data;
  470. assign UART2_r_Tx_DV = bus_addr == 27'hC02727 && bus_we && bus_start;
  471. assign UART2_r_Tx_Byte = bus_data;
  472. //SPI
  473. assign SPI0_in = bus_data;
  474. assign SPI0_start = bus_addr == 27'hC02728 && bus_we && bus_start;
  475. assign SPI1_in = bus_data;
  476. assign SPI1_start = bus_addr == 27'hC0272B && bus_we && bus_start;
  477. assign SPI2_in = bus_data;
  478. assign SPI2_start = bus_addr == 27'hC0272E && bus_we && bus_start;
  479. assign SPI3_in = bus_data;
  480. assign SPI3_start = bus_addr == 27'hC02731 && bus_we && bus_start;
  481. assign SPI4_in = bus_data;
  482. assign SPI4_start = bus_addr == 27'hC02734 && bus_we && bus_start;
  483. //OS Timers
  484. assign OST1_value = bus_data;
  485. assign OST1_set = (bus_addr == 27'hC02739 && bus_we);
  486. assign OST1_trigger = (bus_addr == 27'hC0273A && bus_we);
  487. assign OST2_value = bus_data;
  488. assign OST2_set = (bus_addr == 27'hC0273B && bus_we);
  489. assign OST2_trigger = (bus_addr == 27'hC0273C && bus_we);
  490. assign OST3_value = bus_data;
  491. assign OST3_set = (bus_addr == 27'hC0273D && bus_we);
  492. assign OST3_trigger = (bus_addr == 27'hC0273E && bus_we);
  493. //SNES
  494. //assign SNES_start = bus_addr == 27'hC0273F && bus_start;
  495. reg [5:0] a_sel;
  496. // Address selection
  497. always @(bus_addr)
  498. begin
  499. a_sel = 6'd0;
  500. if (bus_addr < 27'h800000) a_sel = A_SDRAM;
  501. if (bus_addr >= 27'h800000 && bus_addr < 27'hC00000) a_sel = A_FLASH;
  502. if (bus_addr >= 27'hC00000 && bus_addr < 27'hC00420) a_sel = A_VRAM32;
  503. if (bus_addr >= 27'hC00420 && bus_addr < 27'hC02422) a_sel = A_VRAM8;
  504. if (bus_addr >= 27'hC02422 && bus_addr < 27'hC02522) a_sel = A_VRAMSPR;
  505. if (bus_addr >= 27'hC02522 && bus_addr < 27'hC02722) a_sel = A_ROM;
  506. if (bus_addr == 27'hC02722) a_sel = A_UART0RX;
  507. if (bus_addr == 27'hC02723) a_sel = A_UART0TX;
  508. //if (bus_addr == 27'hC02724) a_sel = A_UART1RX;
  509. //if (bus_addr == 27'hC02725) a_sel = A_UART1TX;
  510. if (bus_addr == 27'hC02726) a_sel = A_UART2RX;
  511. if (bus_addr == 27'hC02727) a_sel = A_UART2TX;
  512. if (bus_addr == 27'hC02728) a_sel = A_SPI0;
  513. if (bus_addr == 27'hC02729) a_sel = A_SPI0CS;
  514. if (bus_addr == 27'hC0272A) a_sel = A_SPI0EN;
  515. if (bus_addr == 27'hC0272B) a_sel = A_SPI1;
  516. if (bus_addr == 27'hC0272C) a_sel = A_SPI1CS;
  517. if (bus_addr == 27'hC0272D) a_sel = A_SPI1NINT;
  518. if (bus_addr == 27'hC0272E) a_sel = A_SPI2;
  519. if (bus_addr == 27'hC0272F) a_sel = A_SPI2CS;
  520. if (bus_addr == 27'hC02730) a_sel = A_SPI2NINT;
  521. if (bus_addr == 27'hC02731) a_sel = A_SPI3;
  522. if (bus_addr == 27'hC02732) a_sel = A_SPI3CS;
  523. if (bus_addr == 27'hC02733) a_sel = A_SPI3INT;
  524. if (bus_addr == 27'hC02734) a_sel = A_SPI4;
  525. if (bus_addr == 27'hC02735) a_sel = A_SPI4CS;
  526. if (bus_addr == 27'hC02736) a_sel = A_SPI4GP;
  527. if (bus_addr == 27'hC02737) a_sel = A_GPIO;
  528. if (bus_addr == 27'hC02738) a_sel = A_GPIODIR;
  529. if (bus_addr == 27'hC02739) a_sel = A_TIMER1VAL;
  530. if (bus_addr == 27'hC0273A) a_sel = A_TIMER1CTRL;
  531. if (bus_addr == 27'hC0273B) a_sel = A_TIMER2VAL;
  532. if (bus_addr == 27'hC0273C) a_sel = A_TIMER2CTRL;
  533. if (bus_addr == 27'hC0273D) a_sel = A_TIMER3VAL;
  534. if (bus_addr == 27'hC0273E) a_sel = A_TIMER3CTRL;
  535. //if (bus_addr == 27'hC0273F) a_sel = A_SNESPAD;
  536. if (bus_addr == 27'hC02740) a_sel = A_PS2;
  537. if (bus_addr == 27'hC02741) a_sel = A_BOOTMODE;
  538. if (bus_addr >= 27'hD00000 && bus_addr < 27'hD12C00) a_sel = A_VRAMPX;
  539. end
  540. reg [31:0] bus_q_wire;
  541. reg [31:0] bus_q_wire_reg = 32'd0;
  542. always @(*)
  543. begin
  544. case (a_sel)
  545. A_SDRAM: bus_q_wire = 32'd0; //sd_q; sdram is removed now!
  546. A_FLASH: bus_q_wire = SPIflashReader_q;
  547. A_VRAM32: bus_q_wire = VRAM32_cpu_q;
  548. A_VRAM8: bus_q_wire = VRAM8_cpu_q;
  549. A_VRAMSPR: bus_q_wire = VRAMspr_cpu_q;
  550. A_ROM: bus_q_wire = ROM_q;
  551. A_UART0RX: bus_q_wire = UART0_w_Rx_Byte;
  552. //A_UART0TX: bus_q_wire =
  553. //A_UART1RX: bus_q_wire = UART1_w_Rx_Byte;
  554. //A_UART1TX: bus_q_wire =
  555. A_UART2RX: bus_q_wire = UART2_w_Rx_Byte;
  556. //A_UART2TX: bus_q_wire =
  557. A_SPI0: bus_q_wire = SPI0_out;
  558. A_SPI0CS: bus_q_wire = SPI0_cs;
  559. A_SPI0EN: bus_q_wire = SPI0_enable;
  560. A_SPI1: bus_q_wire = SPI1_out;
  561. A_SPI1CS: bus_q_wire = SPI1_cs;
  562. A_SPI1NINT: bus_q_wire = SPI1_nint;
  563. A_SPI2: bus_q_wire = SPI2_out;
  564. A_SPI2CS: bus_q_wire = SPI2_cs;
  565. A_SPI2NINT: bus_q_wire = SPI2_nint;
  566. A_SPI3: bus_q_wire = SPI3_out;
  567. A_SPI3CS: bus_q_wire = SPI3_cs;
  568. A_SPI3INT: bus_q_wire = SPI3_int;
  569. A_SPI4: bus_q_wire = SPI4_out;
  570. A_SPI4CS: bus_q_wire = SPI4_cs;
  571. A_SPI4GP: bus_q_wire = SPI4_GP;
  572. A_GPIO: bus_q_wire = {24'd0, GPO, GPI};
  573. //A_GPIODIR: bus_q_wire =
  574. //A_TIMER1VAL: bus_q_wire =
  575. //A_TIMER1CTRL: bus_q_wire =
  576. //A_TIMER2VAL: bus_q_wire =
  577. //A_TIMER2CTRL: bus_q_wire =
  578. //A_TIMER3VAL: bus_q_wire =
  579. //A_TIMER3CTRL: bus_q_wire =
  580. //A_SNESPAD: bus_q_wire = {16'd0, SNES_state};
  581. A_PS2: bus_q_wire = {24'd0, PS2_scanCode};
  582. A_BOOTMODE: bus_q_wire = {31'd0, boot_mode};
  583. A_VRAMPX: bus_q_wire = VRAMpx_cpu_q;
  584. default: bus_q_wire = 32'd0;
  585. endcase
  586. end
  587. always @(posedge clk)
  588. begin
  589. if (reset)
  590. begin
  591. bus_q_wire_reg <= 32'd0;
  592. bus_d_reg <= 32'd0;
  593. end
  594. else
  595. begin
  596. bus_d_reg <= bus_data; // latch for copy instructions to SRAM/regs
  597. // latch output
  598. if (bus_done || bus_done_next || SPIflashReader_recvDone) // TODO: Should probably add more ready statements here
  599. bus_q_wire_reg <= bus_q_wire;
  600. end
  601. end
  602. reg bus_done_next = 1'b0;
  603. assign bus_q = (a_sel == A_ROM) ? ROM_q: // safe because ROM cannot be the destination of a copy instruction
  604. bus_q_wire_reg;
  605. always @(posedge clk)
  606. begin
  607. if (reset)
  608. begin
  609. GPO <= 4'd0;
  610. SPI0_enable <= 1'b0;
  611. bus_done <= 1'b0;
  612. bus_done_next <= 1'b0;
  613. SPI0_cs <= 1'b1;
  614. SPI1_cs <= 1'b1;
  615. SPI2_cs <= 1'b1;
  616. SPI3_cs <= 1'b1;
  617. SPI4_cs <= 1'b1;
  618. //TODO: add reset
  619. end
  620. else
  621. begin
  622. if (bus_done_next)
  623. begin
  624. bus_done_next <= 1'b0;
  625. bus_done <= 1'b1;
  626. end
  627. else
  628. begin
  629. bus_done <= 1'b0;
  630. end
  631. if (bus_start)
  632. begin
  633. case (a_sel)
  634. A_SDRAM:
  635. begin
  636. bus_done <= 1'b0; // this is to make sure bus_done from MU will never be used when SDRAM access
  637. end
  638. A_FLASH:
  639. begin
  640. if (SPIflashReader_recvDone || SPI0_enable)
  641. bus_done <= 1'b1;
  642. end
  643. A_UART0TX:
  644. begin
  645. if (UART0_w_Tx_Done)
  646. bus_done <= 1'b1;
  647. end
  648. /*
  649. A_UART1TX:
  650. begin
  651. if (UART1_w_Tx_Done)
  652. bus_done <= 1'b1;
  653. end
  654. */
  655. A_UART2TX:
  656. begin
  657. if (UART2_w_Tx_Done)
  658. bus_done <= 1'b1;
  659. end
  660. A_SPI0:
  661. begin
  662. if (SPI0_done)
  663. if (!bus_done_next) bus_done_next <= 1'b1;
  664. end
  665. A_SPI0CS:
  666. begin
  667. if (bus_we)
  668. begin
  669. SPI0_cs <= bus_data[0];
  670. end
  671. if (!bus_done_next) bus_done_next <= 1'b1;
  672. end
  673. A_SPI0EN:
  674. begin
  675. if (bus_we)
  676. begin
  677. SPI0_enable <= bus_data[0];
  678. end
  679. if (!bus_done_next) bus_done_next <= 1'b1;
  680. end
  681. A_SPI1:
  682. begin
  683. if (SPI1_done)
  684. if (!bus_done_next) bus_done_next <= 1'b1;
  685. end
  686. A_SPI1CS:
  687. begin
  688. if (bus_we)
  689. begin
  690. SPI1_cs <= bus_data[0];
  691. end
  692. if (!bus_done_next) bus_done_next <= 1'b1;
  693. end
  694. A_SPI2:
  695. begin
  696. if (SPI2_done)
  697. if (!bus_done_next) bus_done_next <= 1'b1;
  698. end
  699. A_SPI2CS:
  700. begin
  701. if (bus_we)
  702. begin
  703. SPI2_cs <= bus_data[0];
  704. end
  705. if (!bus_done_next) bus_done_next <= 1'b1;
  706. end
  707. A_SPI3:
  708. begin
  709. if (SPI3_done)
  710. if (!bus_done_next) bus_done_next <= 1'b1;
  711. end
  712. A_SPI3CS:
  713. begin
  714. if (bus_we)
  715. begin
  716. SPI3_cs <= bus_data[0];
  717. end
  718. if (!bus_done_next) bus_done_next <= 1'b1;
  719. end
  720. A_SPI4:
  721. begin
  722. if (SPI4_done)
  723. if (!bus_done_next) bus_done_next <= 1'b1;
  724. end
  725. A_SPI4CS:
  726. begin
  727. if (bus_we)
  728. begin
  729. SPI4_cs <= bus_data[0];
  730. end
  731. if (!bus_done_next) bus_done_next <= 1'b1;
  732. end
  733. A_GPIO:
  734. begin
  735. if (bus_we)
  736. begin
  737. GPO <= bus_data[7:4];
  738. end
  739. if (!bus_done_next) bus_done_next <= 1'b1;
  740. end
  741. /*
  742. A_SNESPAD:
  743. begin
  744. if (SNES_done)
  745. bus_done <= 1'b1;
  746. end
  747. */
  748. A_VRAM8, A_VRAM32, A_VRAMSPR, A_VRAMPX:
  749. begin
  750. if (bus_we)
  751. bus_done <= 1'b1;
  752. else
  753. if (!bus_done_next) bus_done_next <= 1'b1;
  754. end
  755. A_ROM:
  756. begin
  757. bus_done <= 1'b1;
  758. end
  759. default:
  760. begin
  761. if (!bus_done_next) bus_done_next <= 1'b1;
  762. end
  763. endcase
  764. end
  765. end
  766. end
  767. endmodule