abcbus.sv 21 KB

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