abcbus.sv 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. output reg abc_a_oe,
  41. // ABC-bus extension header
  42. // (Note: cannot use an array here because HC and HH are
  43. // input only.)
  44. inout exth_ha,
  45. inout exth_hb,
  46. input exth_hc,
  47. inout exth_hd,
  48. inout exth_he,
  49. inout exth_hf,
  50. inout exth_hg,
  51. input exth_hh,
  52. // SDRAM interface
  53. output [24:0] sdram_addr,
  54. input [7:0] sdram_rd,
  55. output reg sdram_valid,
  56. input sdram_ready,
  57. output [7:0] sdram_wd,
  58. output reg sdram_wstrb
  59. );
  60. // Set if MOSFETs Q1-Q6 are installed rather than the corresponding
  61. // resistors. BOTH CANNOT BE INSTALLED AT THE SAME TIME.
  62. parameter [6:1] mosfet_installed = 6'b111_111;
  63. parameter [0:0] exth_reversed = 1'b0;
  64. // Synchronizer for ABC-bus input signals; also changes
  65. // the sense to positive logic where applicable
  66. wire [15:0] abc_a_s;
  67. wire [7:0] abc_di;
  68. wire abc_rst_s;
  69. wire abc_cs_s;
  70. wire [4:0] abc_out_s;
  71. wire [1:0] abc_inp_s;
  72. wire abc_xmemfl_s;
  73. wire abc_xmemw800_s;
  74. wire abc_xmemw80_s;
  75. wire abc_xinpstb_s;
  76. wire abc_xoutpstb_s;
  77. synchronizer #( .width(39) ) abc_synchro
  78. (
  79. .rst_n ( rst_n ),
  80. .clk ( sys_clk ),
  81. .d ( { abc_clk, abc_a, abc_d, ~abc_rst_n, ~abc_cs_n,
  82. ~abc_out_n, ~abc_inp_n, ~abc_xmemfl_n, ~abc_xmemw800_n,
  83. ~abc_xmemw80_n, ~abc_xinpstb_n, ~abc_xoutpstb_n } ),
  84. .q ( { abc_clk_s, abc_a_s, abc_di, abc_rst_s, abc_cs_s,
  85. abc_out_s, abc_inp_s, abc_xmemfl_s, abc_xmemw800_s,
  86. abc_xmemw80_s, abc_xinpstb_s, abc_xoutpstb_s } )
  87. );
  88. // Only support device mode for now (v2 cards could support host mode)
  89. assign abc_host = 1'b0;
  90. assign abc_a_oe = 1'b0;
  91. reg abc_clk_active;
  92. // On ABC800, only one of XINPSTB# or XOUTPSTB# will be active;
  93. // on ABC80 they will either be 00 or ZZ; in the latter case pulled
  94. // low by external resistors.
  95. wire abc80 = abc_xinpstb_s & abc_xoutpstb_s;
  96. wire abc800 = ~abc80;
  97. // Memory and I/O read/write strobes for ABC-bus
  98. reg abc_xmemrd;
  99. reg abc_xmemwr;
  100. reg [1:0] abc_inp;
  101. reg [4:0] abc_out;
  102. reg abc_rst;
  103. reg abc_cs;
  104. reg [3:1] abc_stb; // Delayed strobes
  105. always @(posedge sdram_clk)
  106. begin
  107. abc_xmemrd <= abc_clk_active & abc_xmemfl_s;
  108. abc_xmemwr <= abc_clk_active &
  109. (abc800 ? abc_xmemw800_s : abc_xmemw80_s);
  110. abc_inp <= abc_inp_s & {2{abc_clk_active}};
  111. abc_out <= abc_out_s & {5{abc_clk_active}};
  112. abc_rst <= abc_rst_s & abc_clk_active;
  113. abc_cs <= abc_cs_s & abc_clk_active;
  114. abc_stb <= { abc_stb, |{abc_inp, abc_out, abc_rst,
  115. abc_cs, abc_xmemrd, abc_xmemwr} };
  116. end
  117. reg [7:0] abc_do;
  118. assign abc_d = abc_d_oe ? abc_do : 8'hzz;
  119. reg [8:0] ioselx;
  120. wire iosel_en = ioselx[8];
  121. wire [5:0] iosel = ioselx[5:0];
  122. // ABC-bus I/O select
  123. always @(negedge rst_n or posedge sdram_clk)
  124. if (~rst_n)
  125. ioselx <= 9'b0;
  126. else if (abc_rst)
  127. ioselx <= 9'b0;
  128. else if (abc_cs)
  129. ioselx <= { 1'b1, abc_di };
  130. // Open drain signals with optional MOSFETs
  131. reg abc_wait = 1'b1; // Power up asserted; see below
  132. reg abc_int = 1'b0;
  133. reg abc_nmi = 1'b0;
  134. reg abc_resin = 1'b0;
  135. reg abc_xm = 1'b0;
  136. function reg opt_mosfet(input signal, input mosfet);
  137. if (mosfet)
  138. opt_mosfet = signal;
  139. else
  140. opt_mosfet = signal ? 1'b0 : 1'bz;
  141. endfunction // opt_mosfet
  142. assign abc_int80_x = opt_mosfet(abc_int & abc80, mosfet_installed[1]);
  143. assign abc_rdy_x = opt_mosfet(abc_wait, mosfet_installed[2]);
  144. assign abc_nmi_x = opt_mosfet(abc_nmi, mosfet_installed[3]);
  145. assign abc_resin_x = opt_mosfet(abc_resin, mosfet_installed[4]);
  146. assign abc_int800_x = opt_mosfet(abc_int & abc800, mosfet_installed[5]);
  147. assign abc_xm_x = opt_mosfet(abc_xm, mosfet_installed[6]);
  148. // Detect ABC-bus clock: need a minimum frequency of 84/64 MHz
  149. // to be considered live.
  150. reg [2:0] abc_clk_ctr;
  151. reg [1:0] abc_clk_q;
  152. always @(negedge rst_n or posedge sys_clk)
  153. if (~rst_n)
  154. begin
  155. abc_clk_q <= 2'b0;
  156. abc_clk_ctr <= 3'b0;
  157. abc_clk_active <= 1'b0;
  158. end
  159. else
  160. begin
  161. abc_clk_q <= { abc_clk_q[0], abc_clk_s };
  162. case ( { abc_clk_q == 2'b10, stb_1mhz } )
  163. 5'b10: begin
  164. if (abc_clk_ctr == 3'b111)
  165. abc_clk_active <= 1'b1;
  166. else
  167. abc_clk_ctr <= abc_clk_ctr + 1'b1;
  168. end
  169. 5'b01: begin
  170. if (abc_clk_ctr == 3'b000)
  171. abc_clk_active <= 1'b0;
  172. else
  173. abc_clk_ctr <= abc_clk_ctr - 1'b1;
  174. end
  175. default: begin
  176. // nothing
  177. end
  178. endcase // case ( {(abc_clk_q == 2'10), sys_clk_stb[6]} )
  179. end // else: !if(~rst_n)
  180. // ABC-bus extension header (exth_c and exth_h are input only)
  181. // The naming of pins is kind of nonsensical:
  182. //
  183. // +3V3 - 1 2 - +3V3
  184. // HA - 3 4 - HE
  185. // HB - 5 6 - HG
  186. // HC - 7 8 - HH
  187. // HD - 9 10 - HF
  188. // GND - 11 12 - GND
  189. //
  190. // This layout allows the header to be connected on either side
  191. // of the board. This logic assigns the following names to the pins;
  192. // if the ext_reversed is set to 1 then the left and right sides
  193. // are flipped.
  194. //
  195. // +3V3 - 1 2 - +3V3
  196. // exth[0] - 3 4 - exth[1]
  197. // exth[2] - 5 6 - exth[3]
  198. // exth[6] - 7 8 - exth[7]
  199. // exth[4] - 9 10 - exth[5]
  200. // GND - 11 12 - GND
  201. wire [7:0] exth_d; // Input data
  202. wire [5:0] exth_q; // Output data
  203. wire [5:0] exth_oe; // Output enable
  204. assign exth_d[0] = exth_reversed ? exth_he : exth_ha;
  205. assign exth_d[1] = exth_reversed ? exth_ha : exth_he;
  206. assign exth_d[2] = exth_reversed ? exth_hg : exth_hb;
  207. assign exth_d[3] = exth_reversed ? exth_hb : exth_hg;
  208. assign exth_d[4] = exth_reversed ? exth_hf : exth_hd;
  209. assign exth_d[5] = exth_reversed ? exth_hd : exth_hf;
  210. assign exth_d[6] = exth_reversed ? exth_hh : exth_hc;
  211. assign exth_d[7] = exth_reversed ? exth_hc : exth_hh;
  212. wire [2:0] erx = { 2'b00, exth_reversed };
  213. assign exth_ha = exth_oe[3'd0 ^ erx] ? exth_q[3'd0 ^ erx] : 1'bz;
  214. assign exth_he = exth_oe[3'd1 ^ erx] ? exth_q[3'd1 ^ erx] : 1'bz;
  215. assign exth_hb = exth_oe[3'd2 ^ erx] ? exth_q[3'd2 ^ erx] : 1'bz;
  216. assign exth_hg = exth_oe[3'd3 ^ erx] ? exth_q[3'd3 ^ erx] : 1'bz;
  217. assign exth_hd = exth_oe[3'd4 ^ erx] ? exth_q[3'd4 ^ erx] : 1'bz;
  218. assign exth_hf = exth_oe[3'd5 ^ erx] ? exth_q[3'd5 ^ erx] : 1'bz;
  219. assign exth_q = 6'b0;
  220. assign exth_oe = 6'b0;
  221. // ABC SDRAM interface
  222. //
  223. // Memory map for ABC-bus memory references.
  224. // 512 byte granularity for memory (registers 0-127),
  225. // one input and one output queue per select code for I/O (128-255).
  226. //
  227. // bit [15:0] = SDRAM address [24:9] ( bits 24:9 from CPU )
  228. // bit [16] = write enable ( bit 30 from CPU )
  229. // bit [17] = read enable ( bit 31 from CPU )
  230. //
  231. // Accesses from the internal CPU supports 32-bit accesses only!
  232. //
  233. wire [17:0] rdata_abcmemmap;
  234. wire [17:0] abc_memmap_rd;
  235. abcmapram abcmapram (
  236. .aclr ( ~rst_n ),
  237. .clock ( sdram_clk ),
  238. .address_a ( abc_a_s[15:9] ),
  239. .data_a ( 18'bx ),
  240. .wren_a ( 1'b0 ),
  241. .q_a ( abc_memmap_rd ),
  242. .address_b ( cpu_addr[8:2] ),
  243. .data_b ( { cpu_wdata[31:30], cpu_wdata[24:9] } ),
  244. .wren_b ( map_valid & cpu_wstrb[0] ),
  245. .q_b ( rdata_abcmemmap )
  246. );
  247. assign cpu_rdata_map = { rdata_abcmemmap[17:16], 5'b0,
  248. rdata_abcmemmap[15:0], 9'b0 };
  249. wire abc_rden = abc_memmap_rd[17];
  250. wire abc_wren = abc_memmap_rd[16];
  251. wire [24:0] abc_memaddr = { abc_memmap_rd[15:0], abc_a_s[8:0] };
  252. reg abc_memrd_en;
  253. reg abc_memwr_en;
  254. reg abc_do_memrd;
  255. reg abc_do_memwr;
  256. always @(posedge sdram_clk or negedge rst_n)
  257. if (~rst_n)
  258. begin
  259. abc_memrd_en <= 1'b0;
  260. abc_memwr_en <= 1'b0;
  261. abc_do_memrd <= 1'b0;
  262. abc_do_memwr <= 1'b0;
  263. sdram_valid <= 1'b0;
  264. sdram_wstrb <= 1'b0;
  265. end
  266. else
  267. begin
  268. // Careful with the registering here: need to make sure
  269. // abcmapram is caught up for I/O; for memory the address
  270. // will have been stable for some time
  271. abc_memwr_en <= abc_xmemwr;
  272. abc_memrd_en <= abc_xmemrd;
  273. abc_do_memrd <= abc_rden & abc_memrd_en;
  274. abc_do_memwr <= abc_wren & abc_memwr_en;
  275. sdram_valid <= abc_do_memrd | abc_do_memwr;
  276. sdram_wstrb <= abc_do_memwr;
  277. end // else: !if(~rst_n)
  278. assign sdram_addr = abc_memaddr;
  279. assign sdram_wd = abc_di;
  280. // I/O data registers; RST# is considered OUT 7 even through
  281. // it is an IN from the ABC point of view.
  282. //
  283. // OUT register, written from ABC: <addr 2:0> <data 7:0>
  284. // IN register, written from CPU: <enable 1:0> <status 7:0> <inp 7:0>
  285. // Busy register:
  286. //
  287. // [7:0] - busy OUT status (write-1-clear)
  288. // [9:8] - busy IN status (write-1-clear)
  289. // [15:12] - bus status change (write-1-clear)
  290. // same bit positions as the bus status register
  291. //
  292. // [23:16] - busy OUT mask
  293. // [25:24] - busy IN mask
  294. // [31:28] - bus status change IRQ enable
  295. //
  296. // Assert WAIT# (deassert RDY) if the masked busy status is nonzero
  297. // and an busy-unmasked I/O comes in.
  298. //
  299. // An IRQ is generated if the masked busy status is nonzero.
  300. //
  301. reg [9:0] busy_status;
  302. reg [9:0] busy_mask;
  303. reg [9:0] busy_io_q;
  304. reg [1:0] inp_en;
  305. reg [3:0] bus_change_status;
  306. reg [3:0] bus_change_mask;
  307. wire [9:0] is_io = { abc_inp[1:0], abc_rst, 1'b0,
  308. abc_out[4:1], abc_cs, abc_out[0] };
  309. wire [9:0] busy_io = is_io & busy_mask;
  310. wire is_busy = |(busy_status & busy_mask);
  311. wire [9:0] busy_valid = 10'b11_1011_1111;
  312. wire [9:0] set_busy = busy_io_q & ~busy_io;
  313. always @(posedge sys_clk or negedge rst_n)
  314. if (~rst_n)
  315. busy_io_q <= 10'b0;
  316. else
  317. busy_io_q <= busy_io;
  318. // WAIT# logic
  319. reg abc_wait_force = 1'b1; // Power up asserted; ignores rst_n
  320. always @(posedge sys_clk)
  321. abc_wait <= abc_wait_force | (rst_n & |set_busy & is_busy);
  322. //
  323. // I/O data registers
  324. //
  325. reg [2:0] reg_out_addr;
  326. reg [7:0] reg_out_data;
  327. reg [7:0] reg_inp_data[0:1];
  328. // OUT logic
  329. always @(posedge sdram_clk)
  330. begin
  331. if (|busy_io[7:0])
  332. begin
  333. reg_out_data <= abc_di;
  334. case (busy_io[7:0])
  335. 8'b0000_0001: reg_out_addr <= 3'd0;
  336. 8'b0000_0010: reg_out_addr <= 3'd1;
  337. 8'b0000_0100: reg_out_addr <= 3'd2;
  338. 8'b0000_1000: reg_out_addr <= 3'd3;
  339. 8'b0001_0000: reg_out_addr <= 3'd4;
  340. 8'b0010_0000: reg_out_addr <= 3'd5;
  341. 8'b0100_0000: reg_out_addr <= 3'd6;
  342. 8'b1000_0000: reg_out_addr <= 3'd7;
  343. default: reg_out_addr <= 3'dx;
  344. endcase // case (busy_io)
  345. end // if (|busy_io[7:0])
  346. end // always @ (posedge sdram_clk)
  347. //
  348. // ABC data out (= ABC host read) logic
  349. //
  350. always @(negedge rst_n or posedge sdram_clk)
  351. if (~rst_n)
  352. begin
  353. abc_d_oe <= 1'b0;
  354. abc_do <= 8'bx;
  355. end
  356. else
  357. begin
  358. abc_d_oe <= 1'b0;
  359. abc_do <= 8'bx;
  360. if (abc_do_memrd & sdram_ready)
  361. begin
  362. abc_d_oe <= 1'b1;
  363. abc_do <= sdram_rd;
  364. end
  365. else if (abc_inp[0] & inp_en[0])
  366. begin
  367. abc_d_oe <= 1'b1;
  368. abc_do <= reg_inp_data[0];
  369. end
  370. else if (abc_inp[1] & inp_en[1])
  371. begin
  372. abc_d_oe <= 1'b1;
  373. abc_do <= reg_inp_data[1];
  374. end
  375. end // else: !if(~rst_n)
  376. // Bus status
  377. reg [3:0] abc_status[0:1];
  378. always @(posedge sys_clk)
  379. begin
  380. abc_status[0] <= { 1'b0, abc800, abc_rst_s, abc_clk_active };
  381. abc_status[1] <= abc_status[0];
  382. end
  383. wire [3:0] bus_change = (abc_status[0] ^ abc_status[1]) & bus_change_mask;
  384. wire [3:0] bus_change_valid = 4'b0111;
  385. //
  386. // Busy/IRQ status and CPU register writes
  387. //
  388. always @(posedge sys_clk or negedge rst_n)
  389. if (~rst_n)
  390. begin
  391. busy_status <= 10'b0;
  392. busy_mask <= 10'h082; // Enable hold on RST# and CS#
  393. inp_en <= 2'b00;
  394. bus_change_status <= 4'b0;
  395. bus_change_mask <= 4'b0;
  396. // abc_resin, nmi, int and force_wait are deliberately not affected
  397. // by an internal CPU reset. They are, however, inherently asserted
  398. // when the FPGA is configured, and initialized to fixed values
  399. // at configuration time (RESIN# asserted, the others deasserted.)
  400. end
  401. else
  402. begin
  403. busy_status <= busy_status | set_busy;
  404. bus_change_status <= bus_change_status | bus_change;
  405. if (abc_valid)
  406. begin
  407. casez (cpu_addr[5:2] )
  408. 5'b??010: begin
  409. if (cpu_wstrb[0])
  410. busy_status[7:0] <= set_busy[7:0] | (busy_status[7:0] & ~cpu_wdata[7:0]);
  411. if (cpu_wstrb[1])
  412. begin
  413. busy_status[9:8] <= set_busy[9:8] | (busy_status[9:8] & ~cpu_wdata[9:8]);
  414. bus_change_status <= bus_change | (bus_change_status & ~cpu_wdata[15:12]);
  415. end
  416. if (cpu_wstrb[2])
  417. busy_mask[7:0] <= cpu_wdata[23:16] & busy_valid[7:0];
  418. if (cpu_wstrb[3])
  419. begin
  420. busy_mask[9:8] <= cpu_wdata[25:24] & busy_valid[9:8];
  421. bus_change_mask <= cpu_wdata[31:28] & bus_change_valid;
  422. end
  423. end
  424. 5'b??011: begin
  425. if (cpu_wstrb[0])
  426. begin
  427. abc_resin <= cpu_wdata[3];
  428. abc_nmi <= cpu_wdata[2];
  429. abc_int <= cpu_wdata[1];
  430. abc_wait_force <= cpu_wdata[0];
  431. end
  432. end
  433. 5'b??101: begin
  434. if (cpu_wstrb[0])
  435. reg_inp_data[0] <= cpu_wdata[7:0];
  436. if (cpu_wstrb[1])
  437. reg_inp_data[1] <= cpu_wdata[15:8];
  438. if (cpu_wstrb[2])
  439. inp_en <= cpu_wdata[17:16];
  440. end
  441. default:
  442. /* do nothing */ ;
  443. endcase // casez (cpu_addr[5:2])
  444. end // if (abc_valid & cpu_wstrb[0])
  445. end
  446. // Level triggered IRQ
  447. always @(posedge sys_clk)
  448. irq <= is_busy | (bus_change_status & bus_change_mask);
  449. // Read MUX
  450. always_comb
  451. casez (cpu_addr[5:2])
  452. 5'b00000: cpu_rdata = { 28'b0, abc_status[0] };
  453. 5'b00001: cpu_rdata = { 23'b0, ~iosel_en, ioselx[7:0] };
  454. 5'b00010: cpu_rdata = { bus_change_mask, 2'b0, busy_mask,
  455. bus_change_status, 2'b0, busy_status };
  456. 5'b00011: cpu_rdata = { 28'b0, abc_resin, abc_nmi, abc_int, abc_wait };
  457. 5'b00100: cpu_rdata = { 21'b0, reg_out_addr, reg_out_data };
  458. 5'b00101: cpu_rdata = { 14'b0, inp_en, reg_inp_data[1], reg_inp_data[0] };
  459. default: cpu_rdata = 32'bx;
  460. endcase // casez (cpu_addr[5:2])
  461. endmodule // abcbus