Skip to content

Commit

Permalink
Add audio high pass filter
Browse files Browse the repository at this point in the history
  • Loading branch information
paulb-nl committed Jun 5, 2020
1 parent f75bc91 commit 312b908
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
32 changes: 29 additions & 3 deletions Gameboy.sv
Expand Up @@ -538,7 +538,7 @@ wire [14:0] lcd_data;
wire [1:0] lcd_mode;
wire lcd_on;

assign AUDIO_S = 0;
assign AUDIO_S = 1'b1;

wire reset = (RESET | status[0] | buttons[1] | cart_download | bk_loading);
wire speed;
Expand All @@ -549,6 +549,32 @@ always @(posedge clk_sys) if(reset) begin
else if(cart_download) isGBC <= !filetype[7:4];
end

// Filter CE impacts frequency response
reg [4:0] filter_cnt;
always_ff @(posedge clk_sys) begin
if (ce_cpu2x)
filter_cnt<= filter_cnt + 1'b1;
end

wire [15:0] audio_l, audio_r;

// Remove DC offset and convert to signed
jt49_dcrm2 #(.sw(16)) dc_filter_l (
.clk (clk_sys),
.cen (ce_cpu2x & &filter_cnt),
.rst (reset),
.din (audio_l),
.dout (AUDIO_L)
);

jt49_dcrm2 #(.sw(16)) dc_filter_r(
.clk (clk_sys),
.cen (ce_cpu2x & &filter_cnt),
.rst (reset),
.din (audio_r),
.dout (AUDIO_R)
);

// the gameboy itself
gb gb (
.reset ( reset ),
Expand Down Expand Up @@ -577,8 +603,8 @@ gb gb (
.gbc_bios_do ( bios_do ),

// audio
.audio_l ( AUDIO_L ),
.audio_r ( AUDIO_R ),
.audio_l ( audio_l ),
.audio_r ( audio_r ),

// interface to the lcd
.lcd_clkena ( lcd_clkena ),
Expand Down
2 changes: 2 additions & 0 deletions files.qip
Expand Up @@ -17,3 +17,5 @@ set_global_assignment -name VERILOG_FILE rtl/link.v
set_global_assignment -name VERILOG_FILE rtl/sgb.v
set_global_assignment -name SDC_FILE Gameboy.sdc
set_global_assignment -name SYSTEMVERILOG_FILE Gameboy.sv
set_global_assignment -name VERILOG_FILE jt49_dcrm2.v

62 changes: 62 additions & 0 deletions jt49_dcrm2.v
@@ -0,0 +1,62 @@
/* This file is part of JT49.
JT49 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
JT49 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with JT49. If not, see <http://www.gnu.org/licenses/>.
Author: Jose Tejada Gomez. Twitter: @topapate
Version: 1.0
Date: 15-Jan-2019
*/

// DC removal filter
// input is unsigned
// output is signed

module jt49_dcrm2 #(parameter sw=8) (
input clk,
input cen,
input rst,
input [sw-1:0] din,
output signed [sw-1:0] dout
);

localparam dw=10; // widht of the decimal portion

reg signed [sw+dw:0] integ, exact, error;
//reg signed [2*(9+dw)-1:0] mult;
// wire signed [sw+dw:0] plus1 = { {sw+dw{1'b0}},1'b1};
reg signed [sw:0] pre_dout;
// reg signed [sw+dw:0] dout_ext;
reg signed [sw:0] q;

always @(*) begin
exact = integ+error;
q = exact[sw+dw:dw];
pre_dout = { 1'b0, din } - q;
//dout_ext = { pre_dout, {dw{1'b0}} };
//mult = dout_ext;
end

assign dout = pre_dout[sw-1:0];

always @(posedge clk)
if( rst ) begin
integ <= {sw+dw+1{1'b0}};
error <= {sw+dw+1{1'b0}};
end else if( cen ) begin
integ <= integ + pre_dout; //mult[sw+dw*2:dw];
error <= exact-{q, {dw{1'b0}}};
end

endmodule

0 comments on commit 312b908

Please sign in to comment.