abcbus.sv 17 KB

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