vjtag_max80.sv 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. //
  2. // vjtag_max80.sv
  3. //
  4. // Access the SDRAM and allow the CPU to take interrupts and receive
  5. // a command opcode via virtual JTAG.
  6. //
  7. // The module supports the following command codes over virtual JTAG:
  8. //
  9. // These chains use a common 1-bit boolean register:
  10. //
  11. // 00000 - bypass
  12. // 00010 - trigger IRQ on update_IR
  13. // 00100 - trigger system (soft) reset on update_IR
  14. // 0011x - assert halt while bit set
  15. // 0100x - memory underrun status flag, clear on capture
  16. //
  17. // These chains use a common 32-bit shift register:
  18. //
  19. // 1001x - address register (only bits [24:2] settable)
  20. // 1010x - read data from memory
  21. // returns address before streaming 32-bit data words
  22. // 1011x - write data to memory
  23. // send VJTAG_WRITE_PREFIX before streaming 32-bit data words
  24. // 1100x - command register to CPU
  25. // 1101x - command register to CPU, trigger IRQ on update_DR
  26. // 1110x - info register from CPU
  27. // 1111x - status register from CPU, clear on capture
  28. //
  29. // Setting bit 0 suppresses all register updates (but not other
  30. // side effects), allowing for nondestructive reads.
  31. //
  32. // The CPU registers are:
  33. //
  34. // 0 - CPU command register (readonly)
  35. // 1 - CPU information register (rw)
  36. // 2 - CPU status register (rw)
  37. // 3 - CPU status register set bits (rw1)
  38. //
  39. module vjtag_max80
  40. #(
  41. parameter sram_bits,
  42. parameter [31:0] sdram_base_addr,
  43. parameter sdram_bits,
  44. parameter [31:0] VJTAG_WRITE_PREFIX = 32'hABC80FED
  45. ) (
  46. input rst_n,
  47. input sys_clk,
  48. output reset_cmd,
  49. input cpu_valid,
  50. input [6:2] cpu_addr,
  51. input [31:0] cpu_wdata,
  52. input [ 3:0] cpu_wstrb,
  53. output [31:0] cpu_rdata,
  54. output cpu_irq,
  55. output cpu_halt,
  56. dram_bus.dstr sdram,
  57. // SRAM interface
  58. output [sram_bits-1:2] sram_addr,
  59. input [31:0] sram_rdata,
  60. output [31:0] sram_wdata,
  61. output sram_read,
  62. output sram_write
  63. );
  64. wire v_tdi;
  65. reg v_tdo;
  66. wire [4:0] v_ir;
  67. wire v_tck;
  68. wire v_st_cdr;
  69. wire v_st_sdr;
  70. wire v_st_e1dr;
  71. wire v_st_pdr;
  72. wire v_st_e2dr;
  73. wire v_st_udr;
  74. wire v_st_cir;
  75. wire v_st_uir;
  76. vjtag vjtag (
  77. .tdi ( v_tdi ),
  78. .tdo ( v_tdo ),
  79. .tck ( v_tck ), // Really nothing virtual...
  80. .ir_in ( v_ir ),
  81. .ir_out ( ),
  82. .virtual_state_cdr ( v_st_cdr ),
  83. .virtual_state_e1dr ( v_st_e1dr ),
  84. .virtual_state_e2dr ( v_st_e2dr ),
  85. .virtual_state_pdr ( v_st_pdr ),
  86. .virtual_state_sdr ( v_st_sdr ),
  87. .virtual_state_udr ( v_st_udr ),
  88. .virtual_state_uir ( v_st_uir ),
  89. .virtual_state_cir ( v_st_cir )
  90. );
  91. localparam cmd_bypass = 4'b0000;
  92. localparam cmd_irq = 4'b0001;
  93. localparam cmd_reset = 4'b0010;
  94. localparam cmd_halt = 4'b0011;
  95. localparam cmd_memerr = 4'b0100;
  96. localparam cmd_mem0 = 4'b1000;
  97. localparam cmd_memaddr = 4'b1001;
  98. localparam cmd_memread = 4'b1010;
  99. localparam cmd_memwrite = 4'b1011;
  100. localparam cmd_memwr = 3'b101; // Common bits of read and write
  101. localparam cmd_cpucmd = 4'b1100;
  102. localparam cmd_cpucmd_irq = 4'b1101; // cpucmd but trigger IRQ
  103. localparam cmd_cpuinfo = 4'b1110;
  104. localparam cmd_cpustatus = 4'b1111;
  105. reg jtag_bypass;
  106. wire jtag_out;
  107. reg tdi_s;
  108. wire tdo_s;
  109. // Latched information for use in the synchronous state machine
  110. reg [3:0] ir_cmd; // Command part of IR
  111. reg ir_ro; // Readonly (update suppress)
  112. always @(posedge v_tck)
  113. begin
  114. jtag_bypass <= v_ir == cmd_bypass;
  115. tdi_s <= v_tdi;
  116. ir_cmd <= v_ir[4:1];
  117. ir_ro <= v_ir[0];
  118. end
  119. assign v_tdo = jtag_bypass ? tdi_s : tdo_s;
  120. // Sync incoming JTAG signals. Only tck needs an actual
  121. // synchronizer; the rest just need holding registers (see above)
  122. // as the delay of tck will guarantee the others are stable.
  123. wire tck_s;
  124. synchronizer #(.width(1), .stages(3)) tck_sync
  125. (
  126. .rst_n ( rst_n ),
  127. .clk ( sys_clk ),
  128. .d ( v_tck ),
  129. .q ( tck_s )
  130. );
  131. // Mask of memaddr bits that are not settable: the top bits
  132. // and the bottom two bits (byte within dword), and the
  133. // sram/dram select bit
  134. localparam [31:0] memaddr_mask = ((1'b1 << sdram_bits) - 3'b100)
  135. | sdram_base_addr;
  136. function logic [31:0] maskaddr (input [31:0] addr);
  137. maskaddr = addr & memaddr_mask;
  138. endfunction
  139. wire is_dram = |(mem_addr & sdram_base_addr); // Really just one bit
  140. reg jtag_cpu_irq = 1'b0;
  141. reg jtag_cpu_halt = 1'b0;
  142. reg jtag_reset_cmd = 1'b0;
  143. assign cpu_irq = jtag_cpu_irq;
  144. assign cpu_halt = jtag_cpu_halt;
  145. assign reset_cmd = jtag_reset_cmd;
  146. reg [31:0] jtag_memaddr = maskaddr(32'b0);
  147. reg [31:0] jtag_cpucmd;
  148. reg [31:0] jtag_cpuinfo;
  149. reg [ 7:0] jtag_cpustatus;
  150. reg mem_valid;
  151. reg mem_write;
  152. reg [31:0] mem_addr;
  153. wire [31:0] mem_addr_next = maskaddr(mem_addr + 3'h4);
  154. wire [31:0] sdram_rdata;
  155. reg [31:0] mem_wdata;
  156. wire sdram_ready;
  157. reg sram_ready;
  158. reg mem_done;
  159. reg mem_error; // Memory underrun
  160. reg advance_mem_addr;
  161. reg mem_header_done;
  162. reg mem_do_write;
  163. reg tck_q;
  164. reg tck_stb;
  165. always @(posedge sys_clk)
  166. begin
  167. tck_q <= tck_s;
  168. tck_stb <= tck_s & ~tck_q;
  169. end
  170. // Keep a counter to keep track of SDRAM data bit count; this is to
  171. // allow streaming of data to/from SDRAM without leaving the
  172. // SDR state.
  173. reg [4:0] sdr_ctr;
  174. // Main data shift register.
  175. reg v_st_sdr_q;
  176. reg [31:0] jtag_shr;
  177. wire [31:0] jtag_shr_in = ir_cmd[3]
  178. ? { tdi_s, jtag_shr[31:1] } :
  179. { 30'bx, tdi_s, jtag_shr[1] };
  180. assign tdo_s = jtag_shr[0];
  181. always @(posedge sys_clk)
  182. begin
  183. if ( ~rst_n )
  184. jtag_reset_cmd <= 1'b0;
  185. jtag_cpu_irq <= 1'b0;
  186. if ( tck_stb )
  187. begin
  188. v_st_sdr_q <= v_st_sdr;
  189. if ( v_st_sdr_q )
  190. jtag_shr <= jtag_shr_in;
  191. if ( v_st_cdr )
  192. case ( ir_cmd )
  193. cmd_halt: begin
  194. jtag_shr[0] <= jtag_cpu_halt;
  195. end
  196. cmd_memerr: begin
  197. jtag_shr[0] <= mem_error;
  198. if ( ~ir_ro ) mem_error <= 1'b0;
  199. end
  200. cmd_mem0, cmd_memaddr, cmd_memread, cmd_memwrite: begin
  201. jtag_shr <= jtag_memaddr;
  202. end
  203. cmd_cpucmd, cmd_cpucmd_irq: begin
  204. jtag_shr <= jtag_cpucmd;
  205. end
  206. cmd_cpuinfo: begin
  207. jtag_shr <= jtag_cpuinfo;
  208. end
  209. cmd_cpustatus: begin
  210. jtag_shr <= jtag_cpustatus;
  211. end
  212. default: ;
  213. endcase // case ( ir_cmd )
  214. // For performance, the SDRAM data can be streamed
  215. // without exiting the shift_DR state, so this is
  216. // based on a counter rather than going to the DR_update
  217. // state.
  218. //
  219. // Note: for cmd_memread, this will trigger an
  220. // unnecessary memory load at the end, but that is
  221. // completely harmless, and we cannot know until the clock
  222. // comes in if we need it or not, so we have to
  223. // suppress the update until we know that the user will
  224. // be reading the fetched data.
  225. mem_do_write <= 1'b0;
  226. if ( ir_cmd[3:1] == cmd_memwr )
  227. begin
  228. if ( v_st_cdr )
  229. begin
  230. mem_done <= 1'b0;
  231. mem_valid <= 1'b0;
  232. mem_write <= ir_cmd[0];
  233. advance_mem_addr <= 1'b0;
  234. mem_header_done <= 1'b0;
  235. mem_addr <= jtag_memaddr;
  236. sdr_ctr <= 5'b0;
  237. mem_do_write <= 1'b0;
  238. end
  239. if ( v_st_sdr )
  240. begin
  241. sdr_ctr <= sdr_ctr + 1'b1;
  242. if ( ~mem_header_done )
  243. begin
  244. if ( ~mem_write )
  245. begin
  246. // Read
  247. mem_header_done <= &sdr_ctr;
  248. end
  249. else
  250. begin
  251. // Write
  252. if ( jtag_shr_in == VJTAG_WRITE_PREFIX )
  253. mem_header_done <= 1'b1;
  254. else
  255. sdr_ctr <= 5'b0;
  256. end
  257. end
  258. // Memory access underrun?
  259. if ( sdr_ctr == 5'd31 )
  260. mem_error <= mem_error | mem_valid;
  261. if ( ~mem_write )
  262. begin
  263. // Read
  264. case ( sdr_ctr )
  265. 5'd2: begin
  266. // For a read, make sure we are committed
  267. // to reading the new word
  268. if ( ~ir_ro )
  269. jtag_memaddr <= mem_addr;
  270. advance_mem_addr <= mem_header_done;
  271. end
  272. 5'd3:
  273. begin
  274. // After mem_addr advanced
  275. mem_valid <= 1'b1;
  276. mem_done <= 1'b0;
  277. end
  278. 5'd31: begin
  279. jtag_shr <= is_dram ? sdram_rdata : sram_rdata;
  280. end
  281. default: ;
  282. endcase // case ( sdr_ctr )
  283. end // if ( ~mem_write )
  284. else
  285. begin
  286. // Write
  287. mem_do_write <= &sdr_ctr;
  288. end // else: !if( ~mem_write )
  289. end // if ( st_sdr_s )
  290. end // if ( ir_cmd[3:1] == cmd_memwr )
  291. if ( mem_do_write )
  292. begin
  293. mem_wdata <= jtag_shr_in;
  294. mem_valid <= 1'b1;
  295. mem_done <= 1'b0;
  296. advance_mem_addr <= 1'b1;
  297. if ( ~ir_ro )
  298. jtag_memaddr <= mem_addr_next;
  299. end
  300. if ( v_st_uir )
  301. jtag_cpu_irq <= ir_cmd == cmd_irq;
  302. else if ( v_st_udr )
  303. jtag_cpu_irq <= ir_cmd == cmd_cpucmd_irq;
  304. if ( v_st_uir )
  305. jtag_reset_cmd <= jtag_reset_cmd | (ir_cmd == cmd_reset);
  306. if ( v_st_udr & ~ir_ro )
  307. case ( ir_cmd )
  308. cmd_halt: begin
  309. jtag_cpu_halt <= jtag_shr[0];
  310. end
  311. cmd_memaddr: begin
  312. jtag_memaddr <= maskaddr(jtag_shr);
  313. end
  314. cmd_cpucmd, cmd_cpucmd_irq: begin
  315. jtag_cpucmd <= jtag_shr;
  316. end
  317. default: /* nothing */ ;
  318. endcase // case ( ir_cmd )
  319. end // if ( tck_stb )
  320. // Increment the temporary address register if applicable,
  321. // but only after the previous transaction is done...
  322. if ( mem_valid )
  323. begin
  324. if (is_dram ? sdram_ready : sram_ready)
  325. begin
  326. mem_valid <= 1'b0;
  327. mem_done <= 1'b1;
  328. end
  329. else
  330. sram_ready <= ~is_dram;
  331. end
  332. if ( advance_mem_addr & ~mem_valid )
  333. begin
  334. mem_addr <= mem_addr_next;
  335. advance_mem_addr <= 1'b0;
  336. end
  337. end
  338. dram_port #(32) mem
  339. (
  340. .bus ( sdram ),
  341. .prio ( 2'd2 ),
  342. .addr ( mem_addr ),
  343. .valid ( mem_valid & is_dram ),
  344. .wd ( mem_wdata ),
  345. .wstrb ( {4{mem_write}} ),
  346. .ready ( sdram_ready ),
  347. .rd ( sdram_rdata )
  348. );
  349. assign sram_addr = mem_addr[sram_bits-1:2];
  350. assign sram_wdata = mem_wdata;
  351. assign sram_read = mem_valid & ~is_dram & ~mem_write;
  352. assign sram_write = mem_valid & ~is_dram & mem_write;
  353. wire [7:0] cpustatus_new =
  354. ( tck_stb & v_st_cdr & ~ir_ro & (ir_cmd == cmd_cpustatus) )
  355. ? 'b0 : jtag_cpustatus;
  356. always @(negedge rst_n or posedge sys_clk)
  357. if (~rst_n)
  358. begin
  359. jtag_cpuinfo <= 'b0;
  360. jtag_cpustatus <= 'b0;
  361. end
  362. else
  363. begin
  364. jtag_cpustatus <= cpustatus_new;
  365. if ( cpu_valid )
  366. begin
  367. case ( cpu_addr )
  368. 5'b00001: begin
  369. if ( cpu_wstrb[0] ) jtag_cpuinfo[7:0] <= cpu_wdata[7:0];
  370. if ( cpu_wstrb[1] ) jtag_cpuinfo[15:8] <= cpu_wdata[15:8];
  371. if ( cpu_wstrb[2] ) jtag_cpuinfo[23:16] <= cpu_wdata[23:16];
  372. if ( cpu_wstrb[3] ) jtag_cpuinfo[31:24] <= cpu_wdata[31:24];
  373. end
  374. 5'b00010: begin
  375. if ( cpu_wstrb[0] ) jtag_cpustatus <= cpu_wdata[7:0];
  376. // Add more if the width of jtag_cpustatus is increased
  377. end
  378. 5'b00011: begin
  379. if ( cpu_wstrb[0] )
  380. jtag_cpustatus <= cpustatus_new | cpu_wdata[7:0];
  381. end
  382. default: /* nothing */;
  383. endcase // case ( cpu_addr[1:0] )
  384. end // if ( cpu_valid )
  385. end
  386. always @(*)
  387. casez ( cpu_addr )
  388. 5'b00000: cpu_rdata = jtag_cpucmd;
  389. 5'b00001: cpu_rdata = jtag_cpuinfo;
  390. 5'b0001?: cpu_rdata = jtag_cpustatus;
  391. default: cpu_rdata = 32'bx;
  392. endcase // casez ( cpu_addr )
  393. endmodule // vjtag_max80