MemoryUnit.v 25 KB

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