MemoryUnit.v 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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. A_DIVWA = 39,
  142. A_DIVSTART = 40;
  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. //UART0
  221. //------------
  222. wire UART0_r_Tx_DV, UART0_w_Tx_Done;
  223. wire [7:0] UART0_r_Tx_Byte;
  224. UARTtx UART0_tx(
  225. .i_Clock (clk),
  226. .reset (reset),
  227. .i_Tx_DV (UART0_r_Tx_DV),
  228. .i_Tx_Byte (UART0_r_Tx_Byte),
  229. .o_Tx_Active(),
  230. .o_Tx_Serial(UART0_out),
  231. .o_Tx_Done (UART0_w_Tx_Done)
  232. );
  233. wire [7:0] UART0_w_Rx_Byte;
  234. UARTrx UART0_rx(
  235. .i_Clock (clk),
  236. .reset (reset),
  237. .i_Rx_Serial(UART0_in),
  238. .o_Rx_DV (UART0_rx_interrupt),
  239. .o_Rx_Byte (UART0_w_Rx_Byte)
  240. );
  241. //------------
  242. //UART1
  243. //------------
  244. /*
  245. wire UART1_r_Tx_DV, UART1_w_Tx_Done;
  246. wire [7:0] UART1_r_Tx_Byte;
  247. UARTtx UART1_tx(
  248. .i_Clock (clk),
  249. .reset (reset),
  250. .i_Tx_DV (UART1_r_Tx_DV),
  251. .i_Tx_Byte (UART1_r_Tx_Byte),
  252. .o_Tx_Active(),
  253. .o_Tx_Serial(UART1_out),
  254. .o_Tx_Done (UART1_w_Tx_Done)
  255. );
  256. wire [7:0] UART1_w_Rx_Byte;
  257. UARTrx UART1_rx(
  258. .i_Clock (clk),
  259. .reset (reset),
  260. .i_Rx_Serial(UART1_in),
  261. .o_Rx_DV (UART1_rx_interrupt),
  262. .o_Rx_Byte (UART1_w_Rx_Byte)
  263. );
  264. */
  265. //------------
  266. //UART2
  267. //------------
  268. wire UART2_r_Tx_DV, UART2_w_Tx_Done;
  269. wire [7:0] UART2_r_Tx_Byte;
  270. UARTtx UART2_tx(
  271. .i_Clock (clk),
  272. .reset (reset),
  273. .i_Tx_DV (UART2_r_Tx_DV),
  274. .i_Tx_Byte (UART2_r_Tx_Byte),
  275. .o_Tx_Active(),
  276. .o_Tx_Serial(UART2_out),
  277. .o_Tx_Done (UART2_w_Tx_Done)
  278. );
  279. wire [7:0] UART2_w_Rx_Byte;
  280. UARTrx UART2_rx(
  281. .i_Clock (clk),
  282. .reset (reset),
  283. .i_Rx_Serial(UART2_in),
  284. .o_Rx_DV (UART2_rx_interrupt),
  285. .o_Rx_Byte (UART2_w_Rx_Byte)
  286. );
  287. //------------
  288. //SPI1 (CH376T bottom)
  289. //------------
  290. wire SPI1_start;
  291. wire [7:0] SPI1_in;
  292. wire [7:0] SPI1_out;
  293. wire SPI1_done;
  294. SimpleSPI #(
  295. .CLKS_PER_HALF_BIT(2))
  296. SPI1(
  297. .clk (clk),
  298. .reset (reset),
  299. .in_byte (SPI1_in),
  300. .start (SPI1_start),
  301. .done (SPI1_done),
  302. .out_byte (SPI1_out),
  303. .spi_clk (SPI1_clk),
  304. .miso (SPI1_miso),
  305. .mosi (SPI1_mosi)
  306. );
  307. //------------
  308. //SPI2 (CH376T top)
  309. //------------
  310. wire SPI2_start;
  311. wire [7:0] SPI2_in;
  312. wire [7:0] SPI2_out;
  313. wire SPI2_done;
  314. SimpleSPI #(
  315. .CLKS_PER_HALF_BIT(2))
  316. SPI2(
  317. .clk (clk),
  318. .reset (reset),
  319. .in_byte (SPI2_in),
  320. .start (SPI2_start),
  321. .done (SPI2_done),
  322. .out_byte (SPI2_out),
  323. .spi_clk (SPI2_clk),
  324. .miso (SPI2_miso),
  325. .mosi (SPI2_mosi)
  326. );
  327. //------------
  328. //SPI3 (W5500)
  329. //------------
  330. wire SPI3_start;
  331. wire [7:0] SPI3_in;
  332. wire [7:0] SPI3_out;
  333. wire SPI3_done;
  334. SimpleSPI #(
  335. .CLKS_PER_HALF_BIT(1))
  336. SPI3(
  337. .clk (clk),
  338. .reset (reset),
  339. .in_byte (SPI3_in),
  340. .start (SPI3_start),
  341. .done (SPI3_done),
  342. .out_byte (SPI3_out),
  343. .spi_clk (SPI3_clk),
  344. .miso (SPI3_miso),
  345. .mosi (SPI3_mosi)
  346. );
  347. //------------
  348. //SPI4 (EXT/GP)
  349. //------------
  350. wire SPI4_start;
  351. wire [7:0] SPI4_in;
  352. wire [7:0] SPI4_out;
  353. wire SPI4_done;
  354. SimpleSPI #(
  355. .CLKS_PER_HALF_BIT(2))
  356. SPI4(
  357. .clk (clk),
  358. .reset (reset),
  359. .in_byte (SPI4_in),
  360. .start (SPI4_start),
  361. .done (SPI4_done),
  362. .out_byte (SPI4_out),
  363. .spi_clk (SPI4_clk),
  364. .miso (SPI4_miso),
  365. .mosi (SPI4_mosi)
  366. );
  367. //------------
  368. //GPIO
  369. //------------
  370. // TODO: To be implemented
  371. //------------
  372. //OS timer 1
  373. //------------
  374. wire OST1_trigger, OST1_set;
  375. wire [31:0] OST1_value;
  376. OStimer OST1(
  377. .clk (clk),
  378. .reset (reset),
  379. .timerValue (OST1_value),
  380. .setValue (OST1_set),
  381. .trigger (OST1_trigger),
  382. .interrupt (OST1_int)
  383. );
  384. //------------
  385. //OS timer 2
  386. //------------
  387. wire OST2_trigger, OST2_set;
  388. wire [31:0] OST2_value;
  389. OStimer OST2(
  390. .clk (clk),
  391. .reset (reset),
  392. .timerValue (OST2_value),
  393. .setValue (OST2_set),
  394. .trigger (OST2_trigger),
  395. .interrupt (OST2_int)
  396. );
  397. //------------
  398. //OS timer 3
  399. //------------
  400. wire OST3_trigger, OST3_set;
  401. wire [31:0] OST3_value;
  402. OStimer OST3(
  403. .clk (clk),
  404. .reset (reset),
  405. .timerValue (OST3_value),
  406. .setValue (OST3_set),
  407. .trigger (OST3_trigger),
  408. .interrupt (OST3_int)
  409. );
  410. //------------
  411. //SNES controller
  412. //------------
  413. /*
  414. wire [15:0] SNES_state;
  415. wire SNES_done;
  416. wire SNES_start;
  417. NESpadReader npr (
  418. .clk(clk),
  419. .reset(reset),
  420. .nesc(SNES_clk),
  421. .nesl(SNES_latch),
  422. .nesd(SNES_data),
  423. .nesState(SNES_state),
  424. .frame(SNES_start),
  425. .done(SNES_done)
  426. );*/
  427. //------------
  428. //PS/2 keyboard
  429. //------------
  430. wire [7:0] PS2_scanCode;
  431. Keyboard PS2Keyboard (
  432. .clk (clk),
  433. .reset (reset),
  434. .ps2d (PS2_data),
  435. .ps2c (PS2_clk),
  436. .rx_done_tick (PS2_int),
  437. .rx_data (PS2_scanCode)
  438. );
  439. wire [31:0] div_input;
  440. wire div_write_a;
  441. wire div_busy;
  442. wire [31:0] div_val;
  443. Divider divider(
  444. .clk (clk),
  445. .rst (reset),
  446. .start (div_start), // start calculation
  447. .write_a (div_write_a),
  448. .busy (div_busy), // calculation in progress
  449. //.done (div_done), // calculation is complete (high for one tick)
  450. //.valid (valid), // result is valid
  451. //.dbz (dbz), // divide by zero
  452. //.ovf (ovf), // overflow
  453. .a_in (bus_data), // dividend (numerator)
  454. .b (bus_data), // divisor (denominator)
  455. .val (div_val) // result value: quotient
  456. );
  457. reg [31:0] bus_d_reg = 32'd0;
  458. //----
  459. //MEMORY
  460. //----
  461. //SPI FLASH MEMORY
  462. assign SPIflashReader_addr = bus_addr - 27'h800000;
  463. assign SPIflashReader_start = bus_addr >= 27'h800000 && bus_addr < 27'hC00000 && bus_start;
  464. //VRAM32
  465. assign VRAM32_cpu_addr = bus_addr - 27'hC00000;
  466. assign VRAM32_cpu_d = bus_d_reg;
  467. assign VRAM32_cpu_we = bus_addr >= 27'hC00000 && bus_addr < 27'hC00420 && bus_we;
  468. //VRAM8
  469. assign VRAM8_cpu_addr = bus_addr - 27'hC00420;
  470. assign VRAM8_cpu_d = bus_data;
  471. assign VRAM8_cpu_we = bus_addr >= 27'hC00420 && bus_addr < 27'hC02422 && bus_we;
  472. //VRAMspr
  473. assign VRAMspr_cpu_addr = bus_addr - 27'hC02422;
  474. assign VRAMspr_cpu_d = bus_data;
  475. assign VRAMspr_cpu_we = bus_addr >= 27'hC02422 && bus_addr < 27'hC02522 && bus_we;
  476. //VRAMpx
  477. assign VRAMpx_cpu_addr = bus_addr - 27'hD00000;
  478. assign VRAMpx_cpu_d = bus_data;
  479. assign VRAMpx_cpu_we = bus_addr >= 27'hD00000 && bus_addr < 27'hD12C00 && bus_we;
  480. //ROM
  481. assign ROM_addr = bus_addr - 27'hC02522;
  482. //----
  483. //I/O
  484. //----
  485. //UART
  486. assign UART0_r_Tx_DV = bus_addr == 27'hC02723 && bus_we && bus_start;
  487. assign UART0_r_Tx_Byte = bus_data;
  488. //assign UART1_r_Tx_DV = bus_addr == 27'hC02725 && bus_we && bus_start;
  489. //assign UART1_r_Tx_Byte = bus_data;
  490. assign UART2_r_Tx_DV = bus_addr == 27'hC02727 && bus_we && bus_start;
  491. assign UART2_r_Tx_Byte = bus_data;
  492. //SPI
  493. assign SPI0_in = bus_data;
  494. assign SPI0_start = bus_addr == 27'hC02728 && bus_we && bus_start;
  495. assign SPI1_in = bus_data;
  496. assign SPI1_start = bus_addr == 27'hC0272B && bus_we && bus_start;
  497. assign SPI2_in = bus_data;
  498. assign SPI2_start = bus_addr == 27'hC0272E && bus_we && bus_start;
  499. assign SPI3_in = bus_data;
  500. assign SPI3_start = bus_addr == 27'hC02731 && bus_we && bus_start;
  501. assign SPI4_in = bus_data;
  502. assign SPI4_start = bus_addr == 27'hC02734 && bus_we && bus_start;
  503. //OS Timers
  504. assign OST1_value = bus_data;
  505. assign OST1_set = (bus_addr == 27'hC02739 && bus_we);
  506. assign OST1_trigger = (bus_addr == 27'hC0273A && bus_we);
  507. assign OST2_value = bus_data;
  508. assign OST2_set = (bus_addr == 27'hC0273B && bus_we);
  509. assign OST2_trigger = (bus_addr == 27'hC0273C && bus_we);
  510. assign OST3_value = bus_data;
  511. assign OST3_set = (bus_addr == 27'hC0273D && bus_we);
  512. assign OST3_trigger = (bus_addr == 27'hC0273E && bus_we);
  513. //SNES
  514. //assign SNES_start = bus_addr == 27'hC0273F && bus_start;
  515. //Divider
  516. assign div_write_a = (bus_addr == 27'hC02742 && bus_we);
  517. assign div_start = (bus_addr == 27'hC02743 && bus_we);
  518. reg [5:0] a_sel;
  519. // Address selection
  520. always @(bus_addr)
  521. begin
  522. a_sel = 6'd0;
  523. if (bus_addr < 27'h800000) a_sel = A_SDRAM;
  524. if (bus_addr >= 27'h800000 && bus_addr < 27'hC00000) a_sel = A_FLASH;
  525. if (bus_addr >= 27'hC00000 && bus_addr < 27'hC00420) a_sel = A_VRAM32;
  526. if (bus_addr >= 27'hC00420 && bus_addr < 27'hC02422) a_sel = A_VRAM8;
  527. if (bus_addr >= 27'hC02422 && bus_addr < 27'hC02522) a_sel = A_VRAMSPR;
  528. if (bus_addr >= 27'hC02522 && bus_addr < 27'hC02722) a_sel = A_ROM;
  529. if (bus_addr == 27'hC02722) a_sel = A_UART0RX;
  530. if (bus_addr == 27'hC02723) a_sel = A_UART0TX;
  531. //if (bus_addr == 27'hC02724) a_sel = A_UART1RX;
  532. //if (bus_addr == 27'hC02725) a_sel = A_UART1TX;
  533. if (bus_addr == 27'hC02726) a_sel = A_UART2RX;
  534. if (bus_addr == 27'hC02727) a_sel = A_UART2TX;
  535. if (bus_addr == 27'hC02728) a_sel = A_SPI0;
  536. if (bus_addr == 27'hC02729) a_sel = A_SPI0CS;
  537. if (bus_addr == 27'hC0272A) a_sel = A_SPI0EN;
  538. if (bus_addr == 27'hC0272B) a_sel = A_SPI1;
  539. if (bus_addr == 27'hC0272C) a_sel = A_SPI1CS;
  540. if (bus_addr == 27'hC0272D) a_sel = A_SPI1NINT;
  541. if (bus_addr == 27'hC0272E) a_sel = A_SPI2;
  542. if (bus_addr == 27'hC0272F) a_sel = A_SPI2CS;
  543. if (bus_addr == 27'hC02730) a_sel = A_SPI2NINT;
  544. if (bus_addr == 27'hC02731) a_sel = A_SPI3;
  545. if (bus_addr == 27'hC02732) a_sel = A_SPI3CS;
  546. if (bus_addr == 27'hC02733) a_sel = A_SPI3INT;
  547. if (bus_addr == 27'hC02734) a_sel = A_SPI4;
  548. if (bus_addr == 27'hC02735) a_sel = A_SPI4CS;
  549. if (bus_addr == 27'hC02736) a_sel = A_SPI4GP;
  550. if (bus_addr == 27'hC02737) a_sel = A_GPIO;
  551. if (bus_addr == 27'hC02738) a_sel = A_GPIODIR;
  552. if (bus_addr == 27'hC02739) a_sel = A_TIMER1VAL;
  553. if (bus_addr == 27'hC0273A) a_sel = A_TIMER1CTRL;
  554. if (bus_addr == 27'hC0273B) a_sel = A_TIMER2VAL;
  555. if (bus_addr == 27'hC0273C) a_sel = A_TIMER2CTRL;
  556. if (bus_addr == 27'hC0273D) a_sel = A_TIMER3VAL;
  557. if (bus_addr == 27'hC0273E) a_sel = A_TIMER3CTRL;
  558. //if (bus_addr == 27'hC0273F) a_sel = A_SNESPAD;
  559. if (bus_addr == 27'hC02740) a_sel = A_PS2;
  560. if (bus_addr == 27'hC02741) a_sel = A_BOOTMODE;
  561. if (bus_addr == 27'hC02742) a_sel = A_DIVWA;
  562. if (bus_addr == 27'hC02743) a_sel = A_DIVSTART;
  563. if (bus_addr >= 27'hD00000 && bus_addr < 27'hD12C00) a_sel = A_VRAMPX;
  564. end
  565. reg [31:0] bus_q_wire;
  566. reg [31:0] bus_q_wire_reg = 32'd0;
  567. always @(*)
  568. begin
  569. case (a_sel)
  570. A_SDRAM: bus_q_wire = 32'd0; //sd_q; sdram is removed now!
  571. A_FLASH: bus_q_wire = SPIflashReader_q;
  572. A_VRAM32: bus_q_wire = VRAM32_cpu_q;
  573. A_VRAM8: bus_q_wire = VRAM8_cpu_q;
  574. A_VRAMSPR: bus_q_wire = VRAMspr_cpu_q;
  575. A_ROM: bus_q_wire = ROM_q;
  576. A_UART0RX: bus_q_wire = UART0_w_Rx_Byte;
  577. //A_UART0TX: bus_q_wire =
  578. //A_UART1RX: bus_q_wire = UART1_w_Rx_Byte;
  579. //A_UART1TX: bus_q_wire =
  580. A_UART2RX: bus_q_wire = UART2_w_Rx_Byte;
  581. //A_UART2TX: bus_q_wire =
  582. A_SPI0: bus_q_wire = SPI0_out;
  583. A_SPI0CS: bus_q_wire = SPI0_cs;
  584. A_SPI0EN: bus_q_wire = SPI0_enable;
  585. A_SPI1: bus_q_wire = SPI1_out;
  586. A_SPI1CS: bus_q_wire = SPI1_cs;
  587. A_SPI1NINT: bus_q_wire = SPI1_nint;
  588. A_SPI2: bus_q_wire = SPI2_out;
  589. A_SPI2CS: bus_q_wire = SPI2_cs;
  590. A_SPI2NINT: bus_q_wire = SPI2_nint;
  591. A_SPI3: bus_q_wire = SPI3_out;
  592. A_SPI3CS: bus_q_wire = SPI3_cs;
  593. A_SPI3INT: bus_q_wire = SPI3_int;
  594. A_SPI4: bus_q_wire = SPI4_out;
  595. A_SPI4CS: bus_q_wire = SPI4_cs;
  596. A_SPI4GP: bus_q_wire = SPI4_GP;
  597. A_GPIO: bus_q_wire = {24'd0, GPO, GPI};
  598. //A_GPIODIR: bus_q_wire =
  599. //A_TIMER1VAL: bus_q_wire =
  600. //A_TIMER1CTRL: bus_q_wire =
  601. //A_TIMER2VAL: bus_q_wire =
  602. //A_TIMER2CTRL: bus_q_wire =
  603. //A_TIMER3VAL: bus_q_wire =
  604. //A_TIMER3CTRL: bus_q_wire =
  605. //A_SNESPAD: bus_q_wire = {16'd0, SNES_state};
  606. A_PS2: bus_q_wire = {24'd0, PS2_scanCode};
  607. A_BOOTMODE: bus_q_wire = {31'd0, boot_mode};
  608. A_VRAMPX: bus_q_wire = VRAMpx_cpu_q;
  609. A_DIVSTART: bus_q_wire = div_val;
  610. default: bus_q_wire = 32'd0;
  611. endcase
  612. end
  613. always @(posedge clk)
  614. begin
  615. if (reset)
  616. begin
  617. bus_q_wire_reg <= 32'd0;
  618. bus_d_reg <= 32'd0;
  619. end
  620. else
  621. begin
  622. bus_d_reg <= bus_data; // latch for copy instructions to SRAM/regs
  623. // latch output
  624. if (bus_done || bus_done_next || SPIflashReader_recvDone) // TODO: Should probably add more ready statements here
  625. bus_q_wire_reg <= bus_q_wire;
  626. end
  627. end
  628. reg bus_done_next = 1'b0;
  629. assign bus_q = (a_sel == A_ROM) ? ROM_q: // safe because ROM cannot be the destination of a copy instruction
  630. bus_q_wire_reg;
  631. always @(posedge clk)
  632. begin
  633. if (reset)
  634. begin
  635. GPO <= 4'd0;
  636. SPI0_enable <= 1'b0;
  637. bus_done <= 1'b0;
  638. bus_done_next <= 1'b0;
  639. SPI0_cs <= 1'b1;
  640. SPI1_cs <= 1'b1;
  641. SPI2_cs <= 1'b1;
  642. SPI3_cs <= 1'b1;
  643. SPI4_cs <= 1'b1;
  644. //TODO: add reset
  645. end
  646. else
  647. begin
  648. if (bus_done_next)
  649. begin
  650. bus_done_next <= 1'b0;
  651. bus_done <= 1'b1;
  652. end
  653. else
  654. begin
  655. bus_done <= 1'b0;
  656. end
  657. if (bus_start)
  658. begin
  659. case (a_sel)
  660. A_SDRAM:
  661. begin
  662. bus_done <= 1'b0; // this is to make sure bus_done from MU will never be used when SDRAM access
  663. end
  664. A_FLASH:
  665. begin
  666. if (SPIflashReader_recvDone || SPI0_enable)
  667. bus_done <= 1'b1;
  668. end
  669. A_UART0TX:
  670. begin
  671. if (UART0_w_Tx_Done)
  672. bus_done <= 1'b1;
  673. end
  674. /*
  675. A_UART1TX:
  676. begin
  677. if (UART1_w_Tx_Done)
  678. bus_done <= 1'b1;
  679. end
  680. */
  681. A_UART2TX:
  682. begin
  683. if (UART2_w_Tx_Done)
  684. bus_done <= 1'b1;
  685. end
  686. A_SPI0:
  687. begin
  688. if (SPI0_done)
  689. if (!bus_done_next) bus_done_next <= 1'b1;
  690. end
  691. A_SPI0CS:
  692. begin
  693. if (bus_we)
  694. begin
  695. SPI0_cs <= bus_data[0];
  696. end
  697. if (!bus_done_next) bus_done_next <= 1'b1;
  698. end
  699. A_SPI0EN:
  700. begin
  701. if (bus_we)
  702. begin
  703. SPI0_enable <= bus_data[0];
  704. end
  705. if (!bus_done_next) bus_done_next <= 1'b1;
  706. end
  707. A_SPI1:
  708. begin
  709. if (SPI1_done)
  710. if (!bus_done_next) bus_done_next <= 1'b1;
  711. end
  712. A_SPI1CS:
  713. begin
  714. if (bus_we)
  715. begin
  716. SPI1_cs <= bus_data[0];
  717. end
  718. if (!bus_done_next) bus_done_next <= 1'b1;
  719. end
  720. A_SPI2:
  721. begin
  722. if (SPI2_done)
  723. if (!bus_done_next) bus_done_next <= 1'b1;
  724. end
  725. A_SPI2CS:
  726. begin
  727. if (bus_we)
  728. begin
  729. SPI2_cs <= bus_data[0];
  730. end
  731. if (!bus_done_next) bus_done_next <= 1'b1;
  732. end
  733. A_SPI3:
  734. begin
  735. if (SPI3_done)
  736. if (!bus_done_next) bus_done_next <= 1'b1;
  737. end
  738. A_SPI3CS:
  739. begin
  740. if (bus_we)
  741. begin
  742. SPI3_cs <= bus_data[0];
  743. end
  744. if (!bus_done_next) bus_done_next <= 1'b1;
  745. end
  746. A_SPI4:
  747. begin
  748. if (SPI4_done)
  749. if (!bus_done_next) bus_done_next <= 1'b1;
  750. end
  751. A_SPI4CS:
  752. begin
  753. if (bus_we)
  754. begin
  755. SPI4_cs <= bus_data[0];
  756. end
  757. if (!bus_done_next) bus_done_next <= 1'b1;
  758. end
  759. A_GPIO:
  760. begin
  761. if (bus_we)
  762. begin
  763. GPO <= bus_data[7:4];
  764. end
  765. if (!bus_done_next) bus_done_next <= 1'b1;
  766. end
  767. /*
  768. A_SNESPAD:
  769. begin
  770. if (SNES_done)
  771. bus_done <= 1'b1;
  772. end
  773. */
  774. A_VRAM8, A_VRAM32, A_VRAMSPR, A_VRAMPX:
  775. begin
  776. if (bus_we)
  777. bus_done <= 1'b1;
  778. else
  779. if (!bus_done_next) bus_done_next <= 1'b1;
  780. end
  781. A_ROM:
  782. begin
  783. bus_done <= 1'b1;
  784. end
  785. A_DIVSTART:
  786. begin
  787. if (!div_busy)
  788. if (!bus_done_next) bus_done_next <= 1'b1;
  789. end
  790. default:
  791. begin
  792. if (!bus_done_next) bus_done_next <= 1'b1;
  793. end
  794. endcase
  795. end
  796. end
  797. end
  798. endmodule