analog_ip.v 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. `timescale 1ns/1ps
  2. module analog_ip (
  3. inout BAUD_RATE,
  4. inout TEST_SINGLE,
  5. inout UART1_RX,
  6. inout UART1_TX,
  7. inout so_io1,
  8. input csn_out_data,
  9. input csn_out_en,
  10. output tri0 rxd1_ip_in,
  11. input sck_out_data,
  12. input sck_out_en,
  13. output tri0 so_io1_in,
  14. input so_io1_out_data,
  15. input so_io1_out_en,
  16. input txd1_ip_out_data,
  17. input txd1_ip_out_en,
  18. input sys_clock,
  19. input bus_clock,
  20. input resetn,
  21. input stop,
  22. input [1:0] mem_ahb_htrans,
  23. input mem_ahb_hready,
  24. input mem_ahb_hwrite,
  25. input [31:0] mem_ahb_haddr,
  26. input [2:0] mem_ahb_hsize,
  27. input [2:0] mem_ahb_hburst,
  28. input [31:0] mem_ahb_hwdata,
  29. output tri1 mem_ahb_hreadyout,
  30. output tri0 mem_ahb_hresp,
  31. output tri0 [31:0] mem_ahb_hrdata,
  32. output tri0 slave_ahb_hsel,
  33. output tri1 slave_ahb_hready,
  34. input slave_ahb_hreadyout,
  35. output tri0 [1:0] slave_ahb_htrans,
  36. output tri0 [2:0] slave_ahb_hsize,
  37. output tri0 [2:0] slave_ahb_hburst,
  38. output tri0 slave_ahb_hwrite,
  39. output tri0 [31:0] slave_ahb_haddr,
  40. output tri0 [31:0] slave_ahb_hwdata,
  41. input slave_ahb_hresp,
  42. input [31:0] slave_ahb_hrdata,
  43. output tri0 [3:0] ext_dma_DMACBREQ,
  44. output tri0 [3:0] ext_dma_DMACLBREQ,
  45. output tri0 [3:0] ext_dma_DMACSREQ,
  46. output tri0 [3:0] ext_dma_DMACLSREQ,
  47. input [3:0] ext_dma_DMACCLR,
  48. input [3:0] ext_dma_DMACTC,
  49. output tri0 [3:0] local_int
  50. );
  51. assign UART1_TX = txd1_ip_out_en ? txd1_ip_out_data : 1'bz;
  52. assign rxd1_ip_in = UART1_RX;
  53. assign slave_ahb_hsel = 1'b0;
  54. assign slave_ahb_hready = 1'b1;
  55. assign slave_ahb_htrans = 2'b00;
  56. assign slave_ahb_hsize = 3'b000;
  57. assign slave_ahb_hburst = 3'b000;
  58. assign slave_ahb_hwrite = 1'b0;
  59. assign slave_ahb_haddr = 32'b0;
  60. assign slave_ahb_hwdata = 32'b0;
  61. assign ext_dma_DMACBREQ = 4'b0000; // 禁用DMA
  62. assign ext_dma_DMACLBREQ = 4'b0000;
  63. assign ext_dma_DMACSREQ = 4'b0000;
  64. assign ext_dma_DMACLSREQ = 4'b0000;
  65. assign local_int = 4'b0000; // 禁用中断
  66. // ==============================================
  67. // 核心RAM参数定义
  68. // ==============================================
  69. parameter RAM_SIZE = 2048; // 总容量:1024*16bit = 2048字节
  70. parameter RAM_DEPTH = 1024; // 数据深度:1024个
  71. parameter RAM_WIDTH = 16; // 数据位宽:16bit
  72. parameter ADDR_BITS = 16;
  73. parameter DATA_BITS = 32;
  74. parameter PER_BITS = 12;
  75. parameter PER_CNT = 4; // APB外设:ADC0(0)、DAC0(1),这里设置4是为了兼容3000地址的DAC0
  76. // 前四个地址用作说明 未实际使用对应变量
  77. // parameter ADC0_ADDR = 'h0000; // ADC0地址
  78. // parameter CFG_REG_ADDR = 'h1000; // 配置参数模块偏移地址
  79. // parameter TRIG_CTRL_ADDR= 'h2000; // 触发模块偏移地址
  80. // parameter DAC0_ADDR = 'h3000; // DAC0地址
  81. parameter RAM_BASE_ADDR = 'h6000; // 双口RAM APB地址 实际应用
  82. // ==============================================
  83. // AHB2APB 桥
  84. // ==============================================
  85. wire apb_psel;
  86. wire apb_penable;
  87. wire apb_pwrite;
  88. wire [ADDR_BITS-1:0] apb_paddr;
  89. wire [DATA_BITS-1:0] apb_pwdata;
  90. wire [3:0] apb_pstrb;
  91. wire [2:0] apb_pprot;
  92. wire apb_pready = 1'b1;
  93. wire apb_pslverr = 1'b0;
  94. reg [DATA_BITS-1:0] apb_prdata;
  95. wire apb_clock = bus_clock;
  96. ahb2apb #(ADDR_BITS, DATA_BITS) ahb2apb_inst(
  97. .reset (!resetn ),
  98. .ahb_clock (sys_clock ),
  99. .ahb_hmastlock(1'b0 ),
  100. .ahb_htrans (mem_ahb_htrans ),
  101. .ahb_hsel (1'b1 ),
  102. .ahb_hready (mem_ahb_hready ),
  103. .ahb_hwrite (mem_ahb_hwrite ),
  104. .ahb_haddr (mem_ahb_haddr[ADDR_BITS-1:0]),
  105. .ahb_hsize (mem_ahb_hsize ),
  106. .ahb_hburst (mem_ahb_hburst ),
  107. .ahb_hprot (4'b0011 ),
  108. .ahb_hwdata (mem_ahb_hwdata ),
  109. .ahb_hrdata (mem_ahb_hrdata ),
  110. .ahb_hreadyout(mem_ahb_hreadyout ),
  111. .ahb_hresp (mem_ahb_hresp ),
  112. .apb_clock (apb_clock ),
  113. .apb_psel (apb_psel ),
  114. .apb_penable (apb_penable ),
  115. .apb_pwrite (apb_pwrite ),
  116. .apb_paddr (apb_paddr ),
  117. .apb_pwdata (apb_pwdata ),
  118. .apb_pstrb (apb_pstrb ),
  119. .apb_pprot (apb_pprot ),
  120. .apb_pready (apb_pready ),
  121. .apb_pslverr (apb_pslverr ),
  122. .apb_prdata (apb_prdata )
  123. );
  124. // ==============================================
  125. // APB 外设选择
  126. // ==============================================
  127. wire [PER_CNT-1:0] select = 1 << (apb_paddr[ADDR_BITS-1:PER_BITS]);
  128. wire per_psel[PER_CNT];
  129. wire per_penable[PER_CNT];
  130. wire per_pwrite[PER_CNT];
  131. wire [PER_BITS-1:0] per_paddr[PER_CNT];
  132. wire [DATA_BITS-1:0] per_pwdata[PER_CNT];
  133. wire [DATA_BITS-1:0] per_prdata[PER_CNT];
  134. genvar i;
  135. generate
  136. for (i = 0; i < PER_CNT; i = i + 1) begin : gen_per
  137. assign per_psel[i] = apb_psel & select[i];
  138. assign per_penable[i] = apb_penable & select[i];
  139. assign per_pwrite[i] = apb_pwrite;
  140. assign per_paddr[i] = apb_paddr[PER_BITS-1:0];
  141. assign per_pwdata[i] = apb_pwdata;
  142. end
  143. endgenerate
  144. // ==============================================
  145. // 实例化 cfg_reg
  146. // ==============================================
  147. wire cfg_adc_en;
  148. wire [7:0] cfg_adc_clk_div;
  149. wire [3:0] cfg_adc_chnl_sel;
  150. wire [11:0] cfg_trig_threshold;
  151. wire [15:0] cfg_trig_pulse_width;
  152. wire [1:0] cfg_trig_edge;
  153. wire [1:0] cfg_trig_mode;
  154. wire [4:0] cfg_trig_time_slot;
  155. wire [15:0] cfg_trig_auto_timeout;
  156. wire cfg_adc_run;
  157. wire cfg_adc_restart;
  158. // 新增:DAC 配置信号
  159. wire cfg_dac_en;
  160. wire cfg_dac_run;
  161. wire [1:0] cfg_wave_type;
  162. wire [9:0] cfg_max_vol;
  163. wire [9:0] cfg_min_vol;
  164. wire [31:0] cfg_frequency;
  165. wire [7:0] cfg_duty_cycle;
  166. cfg_reg cfg_reg_inst(
  167. .pclk (apb_clock),
  168. .presetn (resetn),
  169. .paddr (per_paddr[1]),
  170. .pwrite (per_pwrite[1]),
  171. .pwdata (per_pwdata[1]),
  172. .prdata (per_prdata[1]),
  173. .psel (per_psel[1]),
  174. .penable (per_penable[1]),
  175. .adc_en (cfg_adc_en),
  176. .adc_clk_div (cfg_adc_clk_div),
  177. .adc_chnl_sel (cfg_adc_chnl_sel),
  178. .trig_threshold (cfg_trig_threshold),
  179. .trig_pulse_width (cfg_trig_pulse_width),
  180. .trig_edge (cfg_trig_edge),
  181. .trig_mode (cfg_trig_mode),
  182. .trig_time_slot (cfg_trig_time_slot),
  183. .trig_auto_timeout (cfg_trig_auto_timeout),
  184. .adc_run (cfg_adc_run),
  185. .adc_restart (cfg_adc_restart),
  186. // ********************** 新增:DAC 输出 **********************
  187. .dac_en (cfg_dac_en),
  188. .dac_run (cfg_dac_run),
  189. .wave_type (cfg_wave_type),
  190. .max_vol (cfg_max_vol),
  191. .min_vol (cfg_min_vol),
  192. .frequency (cfg_frequency),
  193. .duty_cycle (cfg_duty_cycle)
  194. );
  195. // ==============================================
  196. // 实例化 ADC0
  197. // ==============================================
  198. wire adc0_eoc;
  199. wire [11:0] adc0_data;
  200. apb_adc #(
  201. .SCLK_BIT(16)
  202. ) apb_adc0_inst(
  203. // APB接口