abcbus.sv 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. module abcbus (
  2. input rst_n,
  3. input sys_clk,
  4. input sdram_clk, // Assumed to be a multiple of sys_clk
  5. input stb_1mhz, // 1-2 MHz sys_clk strobe
  6. // CPU interface
  7. input abc_valid, // Control/status registers
  8. input map_valid, // Memory map
  9. input [31:0] cpu_addr,
  10. input [31:0] cpu_wdata,
  11. input [3:0] cpu_wstrb,
  12. output reg [31:0] cpu_rdata, // For the ABC-bus control
  13. output [31:0] cpu_rdata_map, // For the map RAM
  14. output reg irq,
  15. // ABC bus
  16. inout abc_clk,
  17. output abc_clk_s,
  18. inout [15:0] abc_a,
  19. inout [7:0] abc_d,
  20. output reg abc_d_oe,
  21. inout abc_rst_n,
  22. inout abc_cs_n,
  23. inout [4:0] abc_out_n,
  24. inout [1:0] abc_inp_n,
  25. inout abc_xmemfl_n,
  26. inout abc_xmemw800_n, // Memory write strobe (ABC800)
  27. inout abc_xmemw80_n, // Memory write strobe (ABC80)
  28. inout abc_xinpstb_n, // I/O read strobe (ABC800)
  29. inout abc_xoutpstb_n, // I/O write strobe (ABC80)
  30. // The following are inverted versus the bus IF
  31. // the corresponding MOSFETs are installed
  32. inout abc_rdy_x, // RDY = WAIT#
  33. inout abc_resin_x, // System reset request
  34. inout abc_int80_x, // System INT request (ABC80)
  35. inout abc_int800_x, // System INT request (ABC800)
  36. inout abc_nmi_x, // System NMI request (ABC800)
  37. inout abc_xm_x, // System memory override (ABC800)
  38. // Host/device control
  39. output abc_host, // 1 = host, 0 = device
  40. // ABC-bus extension header
  41. // (Note: cannot use an array here because HC and HH are
  42. // input only.)
  43. inout exth_ha,
  44. inout exth_hb,
  45. input exth_hc,
  46. inout exth_hd,
  47. inout exth_he,
  48. inout exth_hf,
  49. inout exth_hg,
  50. input exth_hh,
  51. // SDRAM interface
  52. output [24:0] sdram_addr,
  53. input [7:0] sdram_rd,
  54. output reg sdram_valid,
  55. input sdram_ready,
  56. output [7:0] sdram_wd,
  57. output reg sdram_wstrb
  58. );
  59. // SDRAM base address, used for reading back the map registers
  60. parameter [31:0] sdram_base_addr;
  61. // Set if MOSFETs Q1-Q6 are installed rather than the corresponding
  62. // resistors. BOTH CANNOT BE INSTALLED AT THE SAME TIME.
  63. parameter [6:1] mosfet_installed = 6'b111_111;
  64. // Are the auxiliary extension header bits reversed (this may be
  65. // programmable in the future depending on orientation of the header)?
  66. parameter [0:0] exth_reversed = 1'b0;
  67. // Synchronizer for ABC-bus input signals; also changes
  68. // the sense to positive logic where applicable
  69. wire [15:0] abc_a_s;
  70. wire [7:0] abc_di;
  71. wire abc_rst_s;
  72. wire abc_cs_s;
  73. wire [4:0] abc_out_s;
  74. wire [1:0] abc_inp_s;
  75. wire abc_xmemfl_s;
  76. wire abc_xmemw800_s;
  77. wire abc_xmemw80_s;
  78. wire abc_xinpstb_s;
  79. wire abc_xoutpstb_s;
  80. synchronizer #( .width(39) ) abc_synchro
  81. (
  82. .rst_n ( rst_n ),
  83. .clk ( sys_clk ),
  84. .d ( { abc_clk, abc_a, abc_d, ~abc_rst_n, ~abc_cs_n,
  85. ~abc_out_n, ~abc_inp_n, ~abc_xmemfl_n, ~abc_xmemw800_n,
  86. ~abc_xmemw80_n, ~abc_xinpstb_n, ~abc_xoutpstb_n } ),
  87. .q ( { abc_clk_s, abc_a_s, abc_di, abc_rst_s, abc_cs_s,
  88. abc_out_s, abc_inp_s, abc_xmemfl_s, abc_xmemw800_s,
  89. abc_xmemw80_s, abc_xinpstb_s, abc_xoutpstb_s } )
  90. );
  91. // Only support device mode for now (v2 cards could support host mode)
  92. assign abc_host = 1'b0;
  93. reg abc_clk_active;
  94. // On ABC800, only one of XINPSTB# or XOUTPSTB# will be active;
  95. // on ABC80 they will either be 00 or ZZ; in the latter case pulled
  96. // low by external resistors.
  97. wire abc80 = abc_xinpstb_s & abc_xoutpstb_s;
  98. wire abc800 = ~abc80;
  99. wire xinpstb_s = (abc800 & abc_xinpstb_s) | (|abc_inp_s);
  100. wire xoutpstb_s = (abc800 & abc_xinpstb_s) | (|abc_out_s);
  101. // Memory and I/O read/write strobes for ABC-bus
  102. reg abc_xmemrd;
  103. reg abc_xmemwr;
  104. reg abc_xinpstb;
  105. reg abc_xoutpstb;
  106. reg [1:0] abc_inp;
  107. reg [4:0] abc_out;
  108. reg abc_rst;
  109. reg abc_cs;
  110. reg [3:1] abc_stb; // Delayed strobes
  111. always @(negedge rst_n or posedge sdram_clk)
  112. if (~rst_n)
  113. begin
  114. abc_xmemrd <= 1'b0;
  115. abc_xmemwr <= 1'b0;
  116. abc_inp <= 2'b0;
  117. abc_out <= 5'b0;
  118. abc_xinpstb <= 1'b0;
  119. abc_xoutpstb <= 1'b0;
  120. abc_rst <= 1'b0;
  121. abc_cs <= 1'b0;
  122. abc_stb <= 'b0;
  123. end
  124. else
  125. begin
  126. abc_xmemrd <= abc_clk_active & abc_xmemfl_s;
  127. abc_xmemwr <= abc_clk_active &
  128. (abc800 ? abc_xmemw800_s : abc_xmemw80_s);
  129. abc_inp <= abc_inp_s & {2{abc_clk_active}};
  130. abc_out <= abc_out_s & {5{abc_clk_active}};
  131. abc_xinpstb <= xinpstb_s & abc_clk_active;
  132. abc_xoutpstb <= xoutpstb_s & abc_clk_active;
  133. abc_rst <= abc_rst_s & abc_clk_active;
  134. abc_cs <= abc_cs_s & abc_clk_active;
  135. abc_stb <= { abc_stb,
  136. abc_xinpstb|abc_xoutpstb|abc_xmemrd|abc_xmemwr };
  137. end
  138. reg [7:0] abc_do;
  139. assign abc_d = abc_d_oe ? abc_do : 8'hzz;
  140. reg [8:0] ioselx;
  141. wire iosel_en = ioselx[8];
  142. wire [5:0] iosel = ioselx[5:0];
  143. // ABC-bus I/O select
  144. always @(negedge rst_n or posedge sdram_clk)
  145. if (~rst_n)
  146. ioselx <= 9'b0;
  147. else if (abc_rst)
  148. ioselx <= 9'b0;
  149. else if (abc_cs)
  150. ioselx <= { 1'b1, abc_di };
  151. // Open drain signals with optional MOSFETs
  152. reg abc_wait = 1'b1; // Power up asserted; see below
  153. reg abc_int = 1'b0;
  154. reg abc_nmi = 1'b0;
  155. reg abc_resin = 1'b0;
  156. reg abc_xm = 1'b0;
  157. function reg opt_mosfet(input signal, input mosfet);
  158. if (mosfet)
  159. opt_mosfet = signal;
  160. else
  161. opt_mosfet = signal ? 1'b0 : 1'bz;
  162. endfunction // opt_mosfet
  163. assign abc_int80_x = opt_mosfet(abc_int & abc80, mosfet_installed[1]);
  164. assign abc_rdy_x = opt_mosfet(abc_wait, mosfet_installed[2]);
  165. assign abc_nmi_x = opt_mosfet(abc_nmi, mosfet_installed[3]);
  166. assign abc_resin_x = opt_mosfet(abc_resin, mosfet_installed[4]);
  167. assign abc_int800_x = opt_mosfet(abc_int & abc800, mosfet_installed[5]);
  168. assign abc_xm_x = opt_mosfet(abc_xm, mosfet_installed[6]);
  169. // Detect ABC-bus clock: need a minimum frequency of 84/64 MHz
  170. // to be considered live.
  171. reg [2:0] abc_clk_ctr;
  172. reg [1:0] abc_clk_q;
  173. always @(negedge rst_n or posedge sys_clk)
  174. if (~rst_n)
  175. begin
  176. abc_clk_q <= 2'b0;
  177. abc_clk_ctr <= 3'b0;
  178. abc_clk_active <= 1'b0;
  179. end
  180. else
  181. begin
  182. abc_clk_q <= { abc_clk_q[0], abc_clk_s };
  183. case ( { abc_clk_q == 2'b10, stb_1mhz } )
  184. 2'b10: begin
  185. if (abc_clk_ctr == 3'b111)
  186. abc_clk_active <= 1'b1;
  187. else
  188. abc_clk_ctr <= abc_clk_ctr + 1'b1;
  189. end
  190. 2'b01: begin
  191. if (abc_clk_ctr == 3'b000)
  192. abc_clk_active <= 1'b0;
  193. else
  194. abc_clk_ctr <= abc_clk_ctr - 1'b1;
  195. end
  196. default: begin
  197. // nothing
  198. end
  199. endcase // case ( {(abc_clk_q == 2'10), sys_clk_stb[6]} )
  200. end // else: !if(~rst_n)
  201. // ABC-bus extension header (exth_c and exth_h are input only)
  202. // The naming of pins is kind of nonsensical:
  203. //
  204. // +3V3 - 1 2 - +3V3
  205. // HA - 3 4 - HE
  206. // HB - 5 6 - HG
  207. // HC - 7 8 - HH
  208. // HD - 9 10 - HF
  209. // GND - 11 12 - GND
  210. //
  211. // This layout allows the header to be connected on either side
  212. // of the board. This logic assigns the following names to the pins;
  213. // if the ext_reversed is set to 1 then the left and right sides
  214. // are flipped.
  215. //
  216. // +3V3 - 1 2 - +3V3
  217. // exth[0] - 3 4 - exth[1]
  218. // exth[2] - 5 6 - exth[3]
  219. // exth[6] - 7 8 - exth[7]
  220. // exth[4] - 9 10 - exth[5]
  221. // GND - 11 12 - GND
  222. wire [7:0] exth_d; // Input data
  223. wire [5:0] exth_q; // Output data
  224. wire [5:0] exth_oe; // Output enable
  225. assign exth_d[0] = exth_reversed ? exth_he : exth_ha;
  226. assign exth_d[1] = exth_reversed ? exth_ha : exth_he;
  227. assign exth_d[2] = exth_reversed ? exth_hg : exth_hb;
  228. assign exth_d[3] = exth_reversed ? exth_hb : exth_hg;
  229. assign exth_d[4] = exth_reversed ? exth_hf : exth_hd;
  230. assign exth_d[5] = exth_reversed ? exth_hd : exth_hf;
  231. assign exth_d[6] = exth_reversed ? exth_hh : exth_hc;
  232. assign exth_d[7] = exth_reversed ? exth_hc : exth_hh;
  233. wire [2:0] erx = { 2'b00, exth_reversed };
  234. assign exth_ha = exth_oe[3'd0 ^ erx] ? exth_q[3'd0 ^ erx] : 1'bz;
  235. assign exth_he = exth_oe[3'd1 ^ erx] ? exth_q[3'd1 ^ erx] : 1'bz;
  236. assign exth_hb = exth_oe[3'd2 ^ erx] ? exth_q[3'd2 ^ erx] : 1'bz;
  237. assign exth_hg = exth_oe[3'd3 ^ erx] ? exth_q[3'd3 ^ erx] : 1'bz;
  238. assign exth_hd = exth_oe[3'd4 ^ erx] ? exth_q[3'd4 ^ erx] : 1'bz;
  239. assign exth_hf = exth_oe[3'd5 ^ erx] ? exth_q[3'd5 ^ erx] : 1'bz;
  240. assign exth_q = 6'b0;
  241. assign exth_oe = 6'b0;
  242. // ABC SDRAM interface
  243. //
  244. // Memory map for ABC-bus memory references.
  245. // 1024 byte granularity in two maps for memory (registers 0-127).
  246. //
  247. // For simplicity, the data bits from the CPU are reorganized
  248. // so the SDRAM address is the same as the CPU uses.
  249. //
  250. // bit [14:0] = SDRAM address [24:10] ( bits 24:10 from RV32 )
  251. // bit [15] = write enable ( bit 0 from RV32 )
  252. // bit [16] = read enable ( bit 1 from RV32 )
  253. // bit [17] = assert XM# ( bit 2 from RV32 )
  254. //
  255. // Accesses from RV32 supports 32-bit accesses only!
  256. //
  257. logic abc_a_map;
  258. wire [17:0] rdata_abcmemmap; // RV32 access port
  259. wire [17:0] abc_memmap_rd; // ABC-bus access port
  260. abcmapram
  261. (
  262. .aclr ( ~rst_n ),
  263. .clock ( sdram_clk ),
  264. .address_a ( { abc_a_map, abc_a_s[15:10] } ),
  265. .data_a ( 18'bx ),
  266. .wren_a ( 1'b0 ),
  267. .q_a ( abc_memmap_rd ),
  268. .address_b ( cpu_addr[8:2] ),
  269. .data_b ( { cpu_wdata[2:0], cpu_wdata[24:10] } ),
  270. .wren_b ( map_valid & cpu_wstrb[0] ),
  271. .q_b ( rdata_abcmemmap )
  272. );
  273. assign cpu_rdata_map = sdram_base_addr | // Fixed bits
  274. { 7'b0, rdata_abcmemmap[14:0], // Address
  275. 7'b0, rdata_abcmemmap[17:15] }; // Flags
  276. wire abc_xmen = abc_memmap_rd[17];
  277. wire abc_rden = abc_memmap_rd[16];
  278. wire abc_wren = abc_memmap_rd[15];
  279. wire [24:0] abc_memaddr = { abc_memmap_rd[14:0], abc_a_s[9:0] };
  280. reg abc_memrd_en;
  281. reg abc_memwr_en;
  282. reg abc_do_memrd;
  283. reg abc_do_memwr;
  284. always @(posedge sdram_clk or negedge rst_n)
  285. if (~rst_n)
  286. begin
  287. abc_memrd_en <= 1'b0;
  288. abc_memwr_en <= 1'b0;
  289. abc_do_memrd <= 1'b0;
  290. abc_do_memwr <= 1'b0;
  291. sdram_valid <= 1'b0;
  292. sdram_wstrb <= 1'b0;
  293. abc_xm <= 1'b0;
  294. end
  295. else
  296. begin
  297. // Careful with the registering here: need to make sure
  298. // abcmapram is caught up for I/O; for memory the address
  299. // will have been stable for some time
  300. abc_memwr_en <= abc_xmemwr;
  301. abc_memrd_en <= abc_xmemrd;
  302. abc_do_memrd <= abc_rden & abc_memrd_en;
  303. abc_do_memwr <= abc_wren & abc_memwr_en;
  304. sdram_valid <= abc_do_memrd | abc_do_memwr;
  305. sdram_wstrb <= abc_do_memwr;
  306. abc_xm <= abc_xmen;
  307. end // else: !if(~rst_n)
  308. assign sdram_addr = abc_memaddr;
  309. assign sdram_wd = abc_di;
  310. //
  311. // 4680 data registers; RST# is considered OUT 7 even through
  312. // it is an IN from the ABC point of view.
  313. //
  314. // OUT register, written from ABC: <addr 2:0> <data 7:0>
  315. // IN register, written from CPU: <enable 1:0> <status 7:0> <inp 7:0>
  316. // Busy register:
  317. //
  318. // [7:0] - busy OUT status (write-1-clear)
  319. // [9:8] - busy IN status (write-1-clear)
  320. // [15:12] - bus status change (write-1-clear)
  321. // same bit positions as the bus status register
  322. //
  323. // [23:16] - busy OUT mask
  324. // [25:24] - busy IN mask
  325. // [31:28] - bus status change IRQ enable
  326. //
  327. // Assert WAIT# (deassert RDY) if the masked busy status is nonzero
  328. // and an busy-unmasked I/O comes in.
  329. //
  330. // An IRQ is generated if the masked busy status is nonzero.
  331. //
  332. reg [9:0] busy_status;
  333. reg [9:0] busy_mask;
  334. reg [9:0] busy_io_q;
  335. reg [1:0] inp_en;
  336. reg [3:0] bus_change_status;
  337. reg [3:0] bus_change_mask;
  338. wire [9:0] is_io = { abc_inp[1:0], abc_rst, 1'b0,
  339. abc_out[4:1], abc_cs, abc_out[0] };
  340. wire [9:0] busy_io = is_io & busy_mask;
  341. wire is_busy = |(busy_status & busy_mask);
  342. wire [9:0] busy_valid = 10'b11_1011_1111;
  343. wire [9:0] set_busy = busy_io_q & ~busy_io;
  344. always @(posedge sys_clk or negedge rst_n)
  345. if (~rst_n)
  346. busy_io_q <= 10'b0;
  347. else
  348. busy_io_q <= busy_io;
  349. // WAIT# logic
  350. reg abc_wait_force = 1'b1; // Power up asserted; ignores rst_n
  351. always @(posedge sys_clk)
  352. abc_wait <= abc_wait_force | (rst_n & |set_busy & is_busy);
  353. //
  354. // 4680 bus data registers
  355. //
  356. reg [2:0] reg_out_addr;
  357. reg [7:0] reg_out_data;
  358. reg [7:0] reg_inp_data[0:1];
  359. // OUT logic
  360. always @(posedge sdram_clk)
  361. begin
  362. if (|busy_io[7:0])
  363. begin
  364. reg_out_data <= abc_di;
  365. case (busy_io[7:0])
  366. 8'b0000_0001: reg_out_addr <= 3'd0;
  367. 8'b0000_0010: reg_out_addr <= 3'd1;
  368. 8'b0000_0100: reg_out_addr <= 3'd2;
  369. 8'b0000_1000: reg_out_addr <= 3'd3;
  370. 8'b0001_0000: reg_out_addr <= 3'd4;
  371. 8'b0010_0000: reg_out_addr <= 3'd5;
  372. 8'b0100_0000: reg_out_addr <= 3'd6;
  373. 8'b1000_0000: reg_out_addr <= 3'd7;
  374. default: reg_out_addr <= 3'dx;
  375. endcase // case (busy_io)
  376. end // if (|busy_io[7:0])
  377. end // always @ (posedge sdram_clk)
  378. //
  379. // ABC800 non-4680 I/O ports
  380. //
  381. reg abc800mac_en = 1'b0;
  382. reg [7:1] abc800mac_iobase = 7'b0;
  383. reg [15:0] abc800mac_xmmask = 16'b0;
  384. wire abc800mac_avalid =
  385. abc800mac_en && abc_a_s[7:1] == abc800mac_iobase;
  386. wire cpu_xmmask = rst_n && abc_valid && cpu_addr[6:2] == 5'b01001;
  387. // XMMASK logic (abc800mac_en is handled with other CPU registers)
  388. always @(posedge sdram_clk)
  389. begin
  390. if (abc_xoutpstb & abc800mac_avalid & !abc_a_s[0])
  391. abc800mac_xmmask[7:0] <= abc_di;
  392. else if (cpu_xmmask & cpu_wstrb[0])
  393. abc800mac_xmmask[7:0] <= cpu_wdata[7:0];
  394. if (abc_xoutpstb & abc800mac_avalid & abc_a_s[1])
  395. abc800mac_xmmask[15:8] <= abc_di;
  396. else if (cpu_xmmask & cpu_wstrb[1])
  397. abc800mac_xmmask[15:8] <= cpu_wdata[15:8];
  398. end // always @ (posedge sdram_clk)
  399. //
  400. // "ROM hack" map control logic
  401. //
  402. reg romhack_en = 1'b0; // Enable
  403. reg [5:0] romhack_addr = 6'b0; // Address mask (1K granular)
  404. reg romhack_map = 1'b0; // Current map
  405. wire cpu_romhackmap = rst_n && abc_valid && cpu_addr[6:2] == 5'b01011;
  406. always @(posedge sdram_clk)
  407. begin
  408. if (abc_xmemwr & romhack_en & (abc_a_s[15:10] == romhack_addr))
  409. romhack_map <= abc_a_s[0];
  410. else if (cpu_romhackmap & cpu_wstrb[0])
  411. romhack_map <= cpu_wdata[0];
  412. end
  413. //
  414. // ABC data out (= ABC host read) logic
  415. //
  416. always @(negedge rst_n or posedge sdram_clk)
  417. if (~rst_n)
  418. begin
  419. abc_d_oe <= 1'b0;
  420. abc_do <= 8'bx;
  421. end
  422. else
  423. begin
  424. abc_d_oe <= 1'b0;
  425. abc_do <= sdram_rd;
  426. if (abc_do_memrd)
  427. begin
  428. // Drive the output bus even if sdram_rd doesn't yet have
  429. // valid data (i.e. sdram_ready = 0).
  430. // The propagation delay for OE#/DIR for 74HC245 is about
  431. // twice what it is for data.
  432. abc_d_oe <= 1'b1;
  433. abc_do <= sdram_rd;
  434. end
  435. else if (abc_inp[0] & inp_en[0])
  436. begin
  437. abc_d_oe <= 1'b1;
  438. abc_do <= reg_inp_data[0];
  439. end
  440. else if (abc_inp[1] & inp_en[1])
  441. begin
  442. abc_d_oe <= 1'b1;
  443. abc_do <= reg_inp_data[1];
  444. end
  445. else if (abc_xinpstb & abc800mac_avalid)
  446. begin
  447. abc_d_oe <= 1'b1;
  448. case (abc_a_s[0])
  449. 1'b0: abc_do <= abc800mac_xmmask[7:0];
  450. 1'b1: abc_do <= abc800mac_xmmask[15:8];
  451. endcase // case (abc_a_s[0])
  452. end
  453. end // else: !if(~rst_n)
  454. //
  455. // Memory map control logic
  456. //
  457. always_comb
  458. begin
  459. abc_a_map = 1'b0;
  460. if (abc800mac_en)
  461. abc_a_map |= abc800mac_xmmask[abc_a_s[15:12]];
  462. if (romhack_en)
  463. abc_a_map |= romhack_map;
  464. end
  465. // Memory read latency counter
  466. reg [7:0] memrd_latency_ctr = 'b0;
  467. reg [7:0] memrd_latency_max = 'b0;
  468. reg memrd_latency_err = 1'b0;
  469. wire [7:0] memrd_latency_ctr_next = memrd_latency_ctr + 1'b1;
  470. always @(posedge sdram_clk)
  471. begin
  472. if (abc_do_memrd & ~sdram_ready)
  473. begin
  474. memrd_latency_ctr <= memrd_latency_ctr_next;
  475. if (memrd_latency_max == memrd_latency_ctr)
  476. memrd_latency_max <= memrd_latency_ctr_next;
  477. // If abc_xmemrd goes away, then we missed our time
  478. // window... this is bad.
  479. if (~abc_xmemrd)
  480. memrd_latency_err <= 1'b1;
  481. end // else: !if(~abc_do_memrd)
  482. else if (~abc_do_memrd)
  483. begin
  484. memrd_latency_ctr <= 'b0;
  485. end
  486. end // always @ (posedge sdram_clk)
  487. // Bus status
  488. reg [3:0] abc_status[0:1];
  489. always @(posedge sys_clk)
  490. begin
  491. abc_status[0] <= { 1'b0, abc800, abc_rst_s, abc_clk_active };
  492. abc_status[1] <= abc_status[0];
  493. end
  494. wire [3:0] bus_change = (abc_status[0] ^ abc_status[1]) & bus_change_mask;
  495. wire [3:0] bus_change_valid = 4'b0111;
  496. //
  497. // Busy/IRQ status and CPU register writes
  498. //
  499. always @(posedge sys_clk or negedge rst_n)
  500. if (~rst_n)
  501. begin
  502. busy_status <= 10'b0;
  503. busy_mask <= 10'h082; // Enable hold on RST# and CS#
  504. inp_en <= 2'b00;
  505. bus_change_status <= 4'b0;
  506. bus_change_mask <= 4'b0;
  507. abc800mac_en <= 1'b0;
  508. romhack_en <= 1'b0;
  509. // abc_resin, nmi, int and force_wait are deliberately not affected
  510. // by an internal CPU reset. They are, however, inherently asserted
  511. // when the FPGA is configured, and initialized to fixed values
  512. // at configuration time (RESIN# asserted, the others deasserted.)
  513. end
  514. else
  515. begin
  516. busy_status <= busy_status | set_busy;
  517. bus_change_status <= bus_change_status | bus_change;
  518. if (abc_valid)
  519. begin
  520. casez (cpu_addr[6:2] )
  521. 5'b00010: begin
  522. if (cpu_wstrb[0])
  523. busy_status[7:0] <= set_busy[7:0] | (busy_status[7:0] & ~cpu_wdata[7:0]);
  524. if (cpu_wstrb[1])
  525. begin
  526. busy_status[9:8] <= set_busy[9:8] | (busy_status[9:8] & ~cpu_wdata[9:8]);
  527. bus_change_status <= bus_change | (bus_change_status & ~cpu_wdata[15:12]);
  528. end
  529. if (cpu_wstrb[2])
  530. busy_mask[7:0] <= cpu_wdata[23:16] & busy_valid[7:0];
  531. if (cpu_wstrb[3])
  532. begin
  533. busy_mask[9:8] <= cpu_wdata[25:24] & busy_valid[9:8];
  534. bus_change_mask <= cpu_wdata[31:28] & bus_change_valid;
  535. end
  536. end
  537. 5'b00011: begin
  538. if (cpu_wstrb[0])
  539. begin
  540. abc_resin <= cpu_wdata[3];
  541. abc_nmi <= cpu_wdata[2];
  542. abc_int <= cpu_wdata[1];
  543. abc_wait_force <= cpu_wdata[0];
  544. end
  545. end
  546. 5'b00101: begin
  547. if (cpu_wstrb[0])
  548. reg_inp_data[0] <= cpu_wdata[7:0];
  549. if (cpu_wstrb[1])
  550. reg_inp_data[1] <= cpu_wdata[15:8];
  551. if (cpu_wstrb[2])
  552. inp_en <= cpu_wdata[17:16];
  553. end
  554. 5'b01000: begin
  555. if (cpu_wstrb[0])
  556. abc800mac_iobase <= cpu_wdata[7:1];
  557. if (cpu_wstrb[3])
  558. abc800mac_en <= cpu_wdata[31];
  559. end
  560. // 5'b01001: abc800mac_xmdata - handled elsewhere
  561. 5'b01010: begin
  562. if (cpu_wstrb[1])
  563. romhack_addr <= cpu_wdata[15:10];
  564. if (cpu_wstrb[3])
  565. romhack_en <= cpu_wdata[31];
  566. end
  567. // 5'b01011: romhack_map - handled elsewhere
  568. default:
  569. /* do nothing */ ;
  570. endcase // casez (cpu_addr[6:2])
  571. end // if (abc_valid & cpu_wstrb[0])
  572. end
  573. // Level triggered IRQ
  574. always @(posedge sys_clk)
  575. irq <= is_busy | |(bus_change_status & bus_change_mask);
  576. // Read MUX
  577. always_comb
  578. casez (cpu_addr[6:2])
  579. 5'b00000: cpu_rdata = { 28'b0, abc_status[0] };
  580. 5'b00001: cpu_rdata = { 23'b0, ~iosel_en, ioselx[7:0] };
  581. 5'b00010: cpu_rdata = { bus_change_mask, 2'b0, busy_mask,
  582. bus_change_status, 2'b0, busy_status };
  583. 5'b00011: cpu_rdata = { 28'b0, abc_resin, abc_nmi, abc_int, abc_wait };
  584. 5'b00100: cpu_rdata = { 21'b0, reg_out_addr, reg_out_data };
  585. 5'b00101: cpu_rdata = { 14'b0, inp_en, reg_inp_data[1], reg_inp_data[0] };
  586. 5'b00111: cpu_rdata = { 23'b0, memrd_latency_err, memrd_latency_max };
  587. 5'b01000: cpu_rdata = { abc800mac_en, 23'b0, abc800mac_iobase, 1'b0 };
  588. 5'b01001: cpu_rdata = { 16'b0, abc800mac_xmmask };
  589. 5'b01010: cpu_rdata = { romhack_en, 15'b0, romhack_addr, 10'b0 };
  590. 5'b01011: cpu_rdata = { 31'b0, romhack_map };
  591. default: cpu_rdata = 32'bx;
  592. endcase // casez (cpu_addr[6:2])
  593. endmodule // abcbus