1
0

MemoryUnit.v 27 KB

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