abcbus.sv 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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_master, // 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 ( 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_master = 1'b0; // Only device mode supported
  93. assign abc_d_ce_n = 1'b0; // Do not isolate busses
  94. reg abc_clk_active;
  95. // On ABC800, only one of XINPSTB# or XOUTPSTB# will be active;
  96. // on ABC80 they will either be 00 or ZZ; in the latter case pulled
  97. // low by external resistors.
  98. wire abc80 = abc_xinpstb_s & abc_xoutpstb_s;
  99. wire abc800 = ~abc80;
  100. // Memory read/write strobes
  101. wire abc_xmemrd = abc_clk_active & abc_xmemfl_s;
  102. wire abc_xmemwr = abc_clk_active &
  103. (abc800 ? abc_xmemw800_s : abc_xmemw80_s);
  104. // I/O read/write strobes for ABC-bus only
  105. wire [2:0] abc_inp = abc_inp_s & {3{abc_clk_active}} & {abc800, 2'b11};
  106. wire [4:0] abc_out = abc_out_s & {5{abc_clk_active}};
  107. wire abc_rst = abc_rst_s & abc_clk_active;
  108. wire abc_cs = abc_cs_s & abc_clk_active;
  109. reg [7:0] abc_do;
  110. assign abc_d = abc_d_oe ? abc_do : 8'hzz;
  111. reg [8:0] ioselx;
  112. wire iosel_en = ioselx[8];
  113. wire iosel = ioselx[5:0];
  114. // ABC-bus I/O select
  115. always @(negedge rst_n or posedge sdram_clk)
  116. if (~rst_n)
  117. ioselx <= 9'b0;
  118. else if (abc_rst)
  119. ioselx <= 9'b0;
  120. else if (abc_cs)
  121. ioselx <= { 1'b1, abc_di };
  122. // Open drain signals with optional MOSFETs
  123. wire abc_wait = 1'b0;
  124. wire abc_resin = ~rst_n;
  125. wire abc_int = 1'b0;
  126. wire abc_nmi = 1'b0;
  127. wire abc_xm = 1'b0;
  128. function reg opt_mosfet(input signal, input mosfet);
  129. if (mosfet)
  130. opt_mosfet = signal;
  131. else
  132. opt_mosfet = signal ? 1'b0 : 1'bz;
  133. endfunction // opt_mosfet
  134. assign abc_int80_x = opt_mosfet(abc_int & abc80, mosfet_installed[1]);
  135. assign abc_rdy_x = opt_mosfet(abc_wait, mosfet_installed[2]);
  136. assign abc_nmi_x = opt_mosfet(abc_nmi, mosfet_installed[3]);
  137. assign abc_resin_x = opt_mosfet(abc_resin, mosfet_installed[4]);
  138. assign abc_int800_x = opt_mosfet(abc_int & abc800, mosfet_installed[5]);
  139. assign abc_xm_x = opt_mosfet(abc_xm, mosfet_installed[6]);
  140. // Detect ABC-bus clock: need a minimum frequency of 84/64 MHz
  141. // to be considered live.
  142. reg [2:0] abc_clk_ctr;
  143. reg [1:0] abc_clk_q;
  144. always @(negedge rst_n or posedge sys_clk)
  145. if (~rst_n)
  146. begin
  147. abc_clk_q <= 2'b0;
  148. abc_clk_ctr <= 3'b0;
  149. abc_clk_active <= 1'b0;
  150. end
  151. else
  152. begin
  153. abc_clk_q <= { abc_clk_q[0], abc_clk_s };
  154. case ( { abc_clk_q == 2'b10, stb_1mhz } )
  155. 5'b10: begin
  156. if (abc_clk_ctr == 3'b111)
  157. abc_clk_active <= 1'b1;
  158. else
  159. abc_clk_ctr <= abc_clk_ctr + 1'b1;
  160. end
  161. 5'b01: begin
  162. if (abc_clk_ctr == 3'b000)
  163. abc_clk_active <= 1'b0;
  164. else
  165. abc_clk_ctr <= abc_clk_ctr - 1'b1;
  166. end
  167. default: begin
  168. // nothing
  169. end
  170. endcase // case ( {(abc_clk_q == 2'10), sys_clk_stb[6]} )
  171. end // else: !if(~rst_n)
  172. // ABC-bus extension header (exth_c and exth_h are input only)
  173. // The naming of pins is kind of nonsensical:
  174. //
  175. // +3V3 - 1 2 - +3V3
  176. // HA - 3 4 - HE
  177. // HB - 5 6 - HG
  178. // HC - 7 8 - HH
  179. // HD - 9 10 - HF
  180. // GND - 11 12 - GND
  181. //
  182. // This layout allows the header to be connected on either side
  183. // of the board. This logic assigns the following names to the pins;
  184. // if the ext_reversed is set to 1 then the left and right sides
  185. // are flipped.
  186. //
  187. // +3V3 - 1 2 - +3V3
  188. // exth[0] - 3 4 - exth[1]
  189. // exth[2] - 5 6 - exth[3]
  190. // exth[6] - 7 8 - exth[7]
  191. // exth[4] - 9 10 - exth[5]
  192. // GND - 11 12 - GND
  193. wire [7:0] exth_d; // Input data
  194. wire [5:0] exth_q; // Output data
  195. wire [5:0] exth_oe; // Output enable
  196. assign exth_d[0] = exth_reversed ? exth_he : exth_ha;
  197. assign exth_d[1] = exth_reversed ? exth_ha : exth_he;
  198. assign exth_d[2] = exth_reversed ? exth_hg : exth_hb;
  199. assign exth_d[3] = exth_reversed ? exth_hb : exth_hg;
  200. assign exth_d[4] = exth_reversed ? exth_hf : exth_hd;
  201. assign exth_d[5] = exth_reversed ? exth_hd : exth_hf;
  202. assign exth_d[6] = exth_reversed ? exth_hh : exth_hc;
  203. assign exth_d[7] = exth_reversed ? exth_hc : exth_hh;
  204. wire [2:0] erx = { 2'b00, exth_reversed };
  205. assign exth_ha = exth_oe[3'd0 ^ erx] ? exth_q[3'd0 ^ erx] : 1'bz;
  206. assign exth_he = exth_oe[3'd1 ^ erx] ? exth_q[3'd1 ^ erx] : 1'bz;
  207. assign exth_hb = exth_oe[3'd2 ^ erx] ? exth_q[3'd2 ^ erx] : 1'bz;
  208. assign exth_hg = exth_oe[3'd3 ^ erx] ? exth_q[3'd3 ^ erx] : 1'bz;
  209. assign exth_hd = exth_oe[3'd4 ^ erx] ? exth_q[3'd4 ^ erx] : 1'bz;
  210. assign exth_hf = exth_oe[3'd5 ^ erx] ? exth_q[3'd5 ^ erx] : 1'bz;
  211. assign exth_q = 6'b0;
  212. assign exth_oe = 6'b0;
  213. // ABC SDRAM interface
  214. //
  215. // Memory map for ABC-bus memory references.
  216. // 512 byte granularity for memory (registers 0-127),
  217. // one input and one output queue per select code for I/O (128-255).
  218. //
  219. // bit [24:0] = SDRAM address.
  220. // bit [25] = write enable ( bit 30 from CPU )
  221. // bit [26] = read enable ( bit 31 from CPU )
  222. // bit [35:27] = DMA count for I/O ( separate register 384-511 from CPU )
  223. //
  224. // Accesses from the internal CPU supports 32-bit accesses only!
  225. //
  226. // If the DMA counter is exhausted, or I/O operations other than port 0,
  227. // I/O is instead directed to a memory area pointed to by the iomem_base
  228. // register as:
  229. // bit [24:4] = iomem_base
  230. // bit [3] = read
  231. // bit [2:0] = port
  232. //
  233. // However, the rd and wr enable bits in the I/O map still apply.
  234. //
  235. wire [24:0] abc_memaddr;
  236. wire [7:0] abc_map_addr =
  237. abc_out_s[0] ? { 1'b1, iosel, 1'b0 } :
  238. abc_inp_s[0] ? { 1'b1, iosel, 1'b1 } :
  239. { 1'b0, abc_a_s[15:9] };
  240. wire [8:0] abc_dma_count;
  241. wire [35:0] rdata_abcmemmap;
  242. wire abc_rden;
  243. wire abc_wren;
  244. //
  245. // For I/O, don't allow the read/write enables to conflict with
  246. // the direction of the I/O.
  247. //
  248. wire [1:0] abcmap_masked_rdwr = cpu_wdata[31:30] &
  249. { ~cpu_addr[9] | ~cpu_addr[2],
  250. ~cpu_addr[9] | cpu_addr[2] };
  251. abcmapram abcmapram (
  252. .aclr ( ~rst_n ),
  253. .clock ( sdram_clk ),
  254. .address_a ( abc_map_addr ),
  255. .data_a ( { abc_dma_count - 1'b1,
  256. abc_rden, abc_wren,
  257. abc_memaddr + 1'b1 } ),
  258. .wren_a ( abc_dma_update ),
  259. .byteena_a ( 4'b1111 ),
  260. .q_a ( { abc_dma_count,
  261. abc_rden, abc_wren, abc_memaddr } ),
  262. .address_b ( cpu_addr[9:2] ),
  263. .data_b ( { cpu_wdata[8:0],
  264. abcmap_masked_rdwr,
  265. cpu_wdata[24:0] } ),
  266. .wren_b ( map_valid & cpu_wstrb[0] ),
  267. .byteena_b ( { cpu_addr[10],
  268. {3{~cpu_addr[10]}} } ),
  269. .q_b ( rdata_abcmemmap )
  270. );
  271. assign cpu_rdata_map = cpu_addr[10] ?
  272. { 23'b0, rdata_abcmemmap[35:27] } :
  273. { rdata_abcmemmap[26:25], 5'b0,
  274. rdata_abcmemmap[24:0] };
  275. reg [24:4] abc_iobase;
  276. reg abc_memrd_en;
  277. reg abc_memwr_en;
  278. reg abc_dma_en;
  279. reg abc_iowr_en;
  280. reg abc_iord_en;
  281. reg abc_do_memrd;
  282. reg abc_do_memwr;
  283. reg abc_racked;
  284. reg abc_wacked;
  285. wire abc_rack;
  286. wire abc_wack;
  287. wire abc_rready;
  288. // I/O status register: set on valid access, trigger IRQ.
  289. // The RST# (INP 7) and CS# (OUT 1) signals are always valid
  290. // regardless of the current select code; OUT 0/INP 0 only
  291. // set the bit once the DMA counter reaches zero, if applicable.
  292. reg [7:0] abc_inpflag;
  293. wire [7:0] clear_inpflag;
  294. reg [7:0] abc_outflag;
  295. wire [7:0] clear_outflag;
  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_dma_en <= 1'b0;
  302. abc_iord_en <= 1'b0;
  303. abc_iowr_en <= 1'b0;
  304. abc_do_memrd <= 1'b0;
  305. abc_do_memwr <= 1'b0;
  306. sdram_rrq <= 1'b0;
  307. sdram_wrq <= 1'b0;
  308. abc_racked <= 1'b0;
  309. abc_wacked <= 1'b0;
  310. abc_inpflag <= 8'b0;
  311. abc_outflag <= 8'b0;
  312. end
  313. else
  314. begin
  315. // Careful with the registering here; need to make sure
  316. // abcmapram is caught up
  317. abc_memwr_en <= abc_xmemwr;
  318. abc_memrd_en <= abc_xmemrd;
  319. abc_dma_en <= iosel_en & (abc_out[0] | abc_inp[0]);
  320. abc_iowr_en <= iosel_en & |abc_out;
  321. abc_iord_en <= iosel_en & |abc_inp;
  322. abc_do_memrd <= abc_rden & (abc_memrd_en | abc_iord_en);
  323. abc_do_memwr <= abc_wren & (abc_memwr_en | abc_iowr_en);
  324. abc_racked <= abc_do_memrd & (sdram_rack | abc_racked);
  325. abc_wacked <= abc_do_memwr & (sdram_wack | abc_wacked);
  326. sdram_rrq <= abc_do_memrd & ~abc_racked;
  327. sdram_wrq <= abc_do_memwr & ~abc_wacked;
  328. // This will be true for one cycle only, which is what we want
  329. abc_dma_update <= abc_dma_en &
  330. ((sdram_rrq & abc_racked) |
  331. (sdram_wrq & abc_wacked));
  332. abc_inpflag <= ~clear_inpflag &
  333. (abc_inpflag |
  334. { abc_rst, 4'b0000,
  335. {2{abc_iord_en}} & abc_inp[2:1],
  336. abc_iord_en & ~|abc_dma_count & abc_inp[0] });
  337. abc_outflag <= ~clear_outflag &
  338. (abc_outflag |
  339. { 2'b00, {4{abc_iowr_en}} & abc_out[4:1],
  340. abc_cs,
  341. abc_iowr_en & ~|abc_dma_count & abc_out[0] });
  342. end // else: !if(~rst_n)
  343. assign sdram_addr =
  344. (abc_dma_en & |abc_dma_count) ? abc_memaddr :
  345. (abc_iord_en|abc_iowr_en) ? { abc_iobase, |abc_inp_s, abc_a_s[2:0] } :
  346. { abc_memaddr[24:9], abc_a_s[8:0] };
  347. assign sdram_wd = abc_di;
  348. //
  349. // ABC-bus data bus handling
  350. //
  351. always @(posedge sdram_clk or negedge rst_n)
  352. if (~rst_n)
  353. begin
  354. abc_do <= 8'hxx;
  355. abc_d_oe <= 1'b0;
  356. end
  357. else if (abc_racked & sdram_rready)
  358. begin
  359. abc_do <= sdram_rd;
  360. abc_d_oe <= 1'b1;
  361. end
  362. else
  363. begin
  364. abc_do <= 8'hxx;
  365. abc_d_oe <= 1'b0;
  366. end
  367. //
  368. // ABC-bus control/status registers
  369. // All these registers are 32-bit access only except the I/O status
  370. // register (which is write-1-clear.)
  371. //
  372. assign clear_inpflag = {8{abc_valid & cpu_addr[5:2] == 5'b00011
  373. & cpu_wstrb[1]}} & cpu_wdata[15:8];
  374. assign clear_outflag = {8{abc_valid & cpu_addr[5:2] == 5'b00011
  375. & cpu_wstrb[0]}} & cpu_wdata[7:0];
  376. always @(posedge sys_clk or negedge rst_n)
  377. if (~rst_n)
  378. begin
  379. abc_iobase <= 20'bx;
  380. end
  381. else if (abc_valid & cpu_wstrb[0])
  382. begin
  383. casez (cpu_addr[5:2])
  384. 5'b???10:
  385. abc_iobase <= cpu_wdata[24:4];
  386. default:
  387. /* do nothing */ ;
  388. endcase
  389. end
  390. reg [31:0] abc_status[0:1];
  391. always @(posedge sys_clk)
  392. begin
  393. abc_status[0] <= { 29'b0, abc800, abc_rst_s, abc_clk_active };
  394. abc_status[1] <= abc_status[0];
  395. end
  396. wire [15:0] abc_iostatus = { abc_inpflag, abc_outflag };
  397. reg [15:0] abc_iostatus_q;
  398. always @(posedge sys_clk)
  399. abc_iostatus_q <= abc_iostatus;
  400. always_comb
  401. casez (cpu_addr[5:2])
  402. 5'b00000: cpu_rdata = abc_status[0];
  403. 5'b00001: cpu_rdata = ioselx[7:0];
  404. 5'b00010: cpu_rdata = abc_iobase;
  405. 5'b00011: cpu_rdata = { 16'b0, abc_iostatus };
  406. default: cpu_rdata = 32'bx;
  407. endcase // casez (cpu_addr[5:2])
  408. // irq is edge-triggered on either changes to abc_status
  409. // or bits set in iostatus.
  410. always @(negedge rst_n or posedge sys_clk)
  411. if (~rst_n)
  412. irq <= 1'b0;
  413. else
  414. irq <= (abc_status[1] != abc_status[0]) |
  415. (|(abc_iostatus & ~abc_iostatus_q));
  416. endmodule // abcbus