Advertisement
Sidsh

WB_pwmaudiocontroller

Apr 10th, 2022
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Skip to content
  2. Search or jump to…
  3. Pull requests
  4. Issues
  5. Marketplace
  6. Explore
  7.  
  8. @Sidshx
  9. Sidshx
  10. /
  11. Integrating-peripheral-with-Core
  12. Private
  13. Code
  14. Issues
  15. Pull requests
  16. Actions
  17. Projects
  18. Security
  19. Insights
  20. Settings
  21. Integrating-peripheral-with-Core/wb_pwmaudio.v
  22. @Sidshx
  23. Sidshx Add files via upload
  24. Latest commit deb3169 9 days ago
  25.  History
  26.  1 contributor
  27. 122 lines (95 sloc)  3.19 KB
  28.    
  29. module wb_pwmaudio (i_clk, i_rst, // clock and reset
  30. i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, // Wishbone interface input signals
  31. o_wb_ack, o_wb_stall, o_wb_data, // Wishbone interface output signals
  32. o_pwm, o_aux, o_int) ;
  33.  
  34. parameter DEFAULT_RELOAD = 16'd2268, // For a 100MHz clk generating a 44.1KHz audio stream by reading in a new sample every (100e6/44.1e3) 2268 samples.
  35. NAUX=2, VARIABLE_RATE=0, TIMING_BITS=16; // Device control values
  36.              
  37. input   wire i_clk, i_rst;
  38. input   wire i_wb_cyc, i_wb_stb, i_wb_we;
  39. input   wire i_wb_addr;
  40. input   wire [31:0] i_wb_data;
  41. output reg o_wb_ack;
  42. output wire o_wb_stall;
  43. output wire [31:0] o_wb_data;
  44. output reg o_pwm;
  45. output reg [(NAUX-1):0] o_aux;
  46. output wire o_int;
  47.  
  48. wire [(TIMING_BITS-1):0] w_reload_value;  // To create interrupts every time reload_value clocks
  49. generate
  50. if (VARIABLE_RATE != 0)
  51. begin
  52.  
  53. reg [(TIMING_BITS-1):0] r_reload_value;
  54. initial r_reload_value = DEFAULT_RELOAD;
  55.  
  56. always @(posedge i_clk)     // data write
  57. if ((i_wb_stb)&&(i_wb_addr)&&(i_wb_we))    
  58. r_reload_value <= i_wb_data[(TIMING_BITS-1):0] - 1'b1;
  59.  
  60. assign  w_reload_value = r_reload_value;
  61. end else begin
  62. assign  w_reload_value = DEFAULT_RELOAD;
  63. end endgenerate
  64.  
  65. // to create a timer for indicating the next value
  66.  
  67. reg ztimer;   // zero timer
  68. reg [(TIMING_BITS-1):0] timer;
  69. initial timer = DEFAULT_RELOAD;
  70. initial ztimer =1'b0;
  71.  
  72. always@(posedge i_clk)
  73.     if (i_rst)
  74.         ztimer <= 1'b0;
  75.     else
  76.         ztimer <= (timer=={ {(TIMING_BITS-1){1'b0}},1'b1});
  77.        
  78. reg [15:0] sample_out;
  79. always@(posedge i_clk)  // for accepting the next sample when the ztimer runs out
  80.     if (ztimer==1)
  81.     begin
  82.         sample_out <= next_sample;
  83.     end
  84.        
  85. reg [15:0] next_sample;
  86. reg next_valid;
  87. initial next_valid=1'b1;
  88. initial next_sample=16'h8000;
  89.  
  90. always@(posedge i_clk)
  91.     if ((i_wb_stb)&&(i_wb_we)&&((!i_wb_addr)||(VARIABLE_RATE==0)))      // Data write
  92.         begin
  93.         next_sample <= { !i_wb_data[15], i_wb_data[14:0] };
  94.         next_valid <= 1'b1;
  95.         if (i_wb_data[16])
  96.             o_aux <= i_wb_data[(NAUX+20-1):20];
  97.         end else if (ztimer)
  98.             next_valid <= 1'b0;
  99.  
  100. assign o_int = (!next_valid);       // sends an interrupt to the processor, to know when to send a new sample
  101.  
  102. reg [15:0] pwm_counter;
  103. initial pwm_counter = 16'h00;
  104. always @(posedge i_clk)
  105.     if (i_rst)
  106.     pwm_counter <= 16'h0;
  107.     else
  108.     pwm_counter <= pwm_counter + 16'h01;
  109.    
  110.    
  111. wire [15:0] br_counter;
  112. genvar k;
  113. generate for(k=0; k<16; k=k+1)
  114. begin : bit_reversal_loop
  115. assign br_counter[k] = pwm_counter[15-k];
  116. end endgenerate
  117.  
  118. always@(posedge i_clk)
  119.     o_pwm <= (sample_out >= br_counter);
  120.    
  121. generate
  122. if (VARIABLE_RATE == 0)
  123. begin
  124. assign o_wb_data = { {(12-NAUX){1'b0}}, o_aux, 3'h0, o_int, sample_out };
  125.  
  126. end else
  127. begin
  128. reg [31:0] r_wb_data;
  129.  
  130. always @(posedge i_clk)
  131. if (i_wb_addr)
  132. r_wb_data <= { (32-TIMING_BITS),w_reload_value};
  133. else
  134. r_wb_data <= { {(12-NAUX){1'b0}}, o_aux, 3'h0, o_int, sample_out };
  135.  
  136. assign  o_wb_data = r_wb_data;
  137. end endgenerate
  138.  
  139. initial o_wb_ack = 1'b0;    // always ack on the clock following any request
  140. always @(posedge i_clk)
  141. o_wb_ack <= (i_wb_stb);
  142.  
  143. assign  o_wb_stall = 1'b0;  // does not stall the process
  144.  
  145. wire    [14:0] unused;
  146. assign  unused = { i_wb_cyc, i_wb_data[31:21], i_wb_data[19:17] };
  147.  
  148. endmodule
  149.  
  150.  
  151. © 2022 GitHub, Inc.
  152. Terms
  153. Privacy
  154. Security
  155. Status
  156. Docs
  157. Contact GitHub
  158. Pricing
  159. API
  160. Training
  161. Blog
  162. About
  163. Loading complete
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement