audio_pcm.v 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * audio_pcm.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2020 Sylvain Munaut <tnt@246tNt.com>
  7. * SPDX-License-Identifier: CERN-OHL-P-2.0
  8. */
  9. `default_nettype none
  10. module audio_pcm (
  11. // Audio output
  12. output wire [1:0] audio,
  13. // Wishbone slave
  14. input wire [ 1:0] wb_addr,
  15. output reg [31:0] wb_rdata,
  16. input wire [31:0] wb_wdata,
  17. input wire wb_we,
  18. input wire wb_cyc,
  19. output wire wb_ack,
  20. // USB
  21. input wire usb_sof,
  22. // Clock / Reset
  23. input wire clk,
  24. input wire rst
  25. );
  26. // Signals
  27. // -------
  28. // Wishbone
  29. reg b_ack;
  30. reg b_we_csr;
  31. reg b_we_volume;
  32. reg b_we_fifo;
  33. wire b_rd_rst;
  34. reg run;
  35. reg [15:0] volume[0:1];
  36. // FSM
  37. localparam
  38. ST_IDLE = 0,
  39. ST_RUN = 1,
  40. ST_FLUSH = 2;
  41. reg [ 1:0] state;
  42. reg [ 1:0] state_nxt;
  43. wire running;
  44. // Timebase
  45. wire tick;
  46. reg [ 9:0] tick_cnt;
  47. reg [15:0] tpf_cnt;
  48. reg [15:0] tpf_cap;
  49. // FIFO
  50. wire [31:0] fw_data;
  51. wire fw_ena;
  52. wire fw_full;
  53. wire [31:0] fr_data;
  54. wire fr_ena;
  55. wire fr_empty;
  56. reg [ 9:0] f_lvl;
  57. wire [ 9:0] f_mod;
  58. // Audio pipeline
  59. reg [15:0] av_volume [0:1];
  60. reg [15:0] av_sample [0:1];
  61. reg [31:0] av_scaled [0:1];
  62. wire [15:0] av_out[0:1];
  63. // Wishbone interface
  64. // ------------------
  65. // Ack
  66. always @(posedge clk)
  67. b_ack <= wb_cyc & ~b_ack;
  68. assign wb_ack = b_ack;
  69. // Write
  70. always @(posedge clk)
  71. begin
  72. if (b_ack) begin
  73. b_we_csr <= 1'b0;
  74. b_we_volume <= 1'b0;
  75. b_we_fifo <= 1'b0;
  76. end else begin
  77. b_we_csr <= wb_cyc & wb_we & (wb_addr == 2'b00);
  78. b_we_volume <= wb_cyc & wb_we & (wb_addr == 2'b01);
  79. b_we_fifo <= wb_cyc & wb_we & (wb_addr == 2'b10);
  80. end
  81. end
  82. always @(posedge clk)
  83. if (rst)
  84. run <= 1'b0;
  85. else if (b_we_csr)
  86. run <= wb_wdata[0];
  87. always @(posedge clk or posedge rst)
  88. if (rst)
  89. { volume[1], volume[0] } <= 32'h00000000;
  90. else if (b_we_volume)
  91. { volume[1], volume[0] } <= wb_wdata;
  92. assign fw_data = wb_wdata;
  93. assign fw_ena = b_we_fifo & ~fw_full;
  94. // Read
  95. assign b_rd_rst = ~wb_cyc | b_ack;
  96. always @(posedge clk)
  97. if (b_rd_rst)
  98. wb_rdata <= 32'h00000000;
  99. else
  100. wb_rdata <= { tpf_cap, 2'b00, f_lvl, 2'b00, running, run };
  101. // FSM
  102. // ---
  103. // State register
  104. always @(posedge clk)
  105. if (rst)
  106. state <= ST_IDLE;
  107. else
  108. state <= state_nxt;
  109. // Next state
  110. always @(*)
  111. begin
  112. // Default is to stay
  113. state_nxt = state;
  114. // Transitions
  115. case (state)
  116. ST_IDLE:
  117. if (run)
  118. state_nxt = ST_RUN;
  119. ST_RUN:
  120. if (~run)
  121. state_nxt = ST_FLUSH;
  122. ST_FLUSH:
  123. if (fr_empty)
  124. state_nxt = ST_IDLE;
  125. endcase
  126. end
  127. // Misc
  128. assign running = (state == ST_RUN) | (state == ST_FLUSH);
  129. // Timebase
  130. // --------
  131. // Tick counter
  132. always @(posedge clk or posedge rst)
  133. if (rst)
  134. tick_cnt <= 0;
  135. else
  136. tick_cnt <= tick ? 10'd498 : (tick_cnt - 1);
  137. assign tick = tick_cnt[9];
  138. // Tick-per-usb frame counter
  139. always @(posedge clk or posedge rst)
  140. if (rst)
  141. tpf_cnt <= 16'h0000;
  142. else
  143. tpf_cnt <= tpf_cnt + tick;
  144. always @(posedge clk or posedge rst)
  145. if (rst)
  146. tpf_cap <= 16'h0000;
  147. else if (usb_sof)
  148. tpf_cap <= tpf_cnt;
  149. // FIFO
  150. // ----
  151. // Instance
  152. fifo_sync_ram #(
  153. .DEPTH(512),
  154. .WIDTH(32)
  155. ) fifo_I (
  156. .wr_data (fw_data),
  157. .wr_ena (fw_ena),
  158. .wr_full (fw_full),
  159. .rd_data (fr_data),
  160. .rd_ena (fr_ena),
  161. .rd_empty (fr_empty),
  162. .clk (clk),
  163. .rst (rst)
  164. );
  165. // Read
  166. assign fr_ena = ~fr_empty & tick & running;
  167. // Level counter
  168. always @(posedge clk)
  169. if (rst)
  170. f_lvl <= 0;
  171. else
  172. f_lvl <= f_lvl + f_mod;
  173. assign f_mod = { {9{fr_ena & ~fw_ena}}, fr_ena ^ fw_ena };
  174. // Volume & Mute
  175. // -------------
  176. always @(posedge clk)
  177. begin : outpipe
  178. integer i;
  179. for (i=0; i<2; i=i+1)
  180. begin
  181. av_volume[i] <= volume[i];
  182. av_sample[i] <= fr_empty ? 0 : fr_data[16*i+:16];
  183. av_scaled[i] <= $signed(av_volume[i]) * $signed(av_sample[i]);
  184. end
  185. end
  186. // PDM output
  187. // ----------
  188. assign av_out[0] = av_scaled[0][30:15] ^ 16'h8000;
  189. assign av_out[1] = av_scaled[1][30:15] ^ 16'h8000;
  190. pdm #(
  191. .WIDTH(16),
  192. .DITHER("NO"),
  193. .PHY("ICE40")
  194. ) pdm_I[1:0] (
  195. .pdm (audio),
  196. .cfg_val ({av_out[1], av_out[0]}),
  197. .cfg_oe (1'b1),
  198. .clk (clk),
  199. .rst (rst)
  200. );
  201. endmodule // audio_pcm