2
0

abcbus.sv 21 KB

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