abcbus.sv 16 KB

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