Skip to content

Commit

Permalink
Merge pull request #60 from pvanschendel/master
Browse files Browse the repository at this point in the history
Fix sound not working and add option to do analog emulation
  • Loading branch information
sorgelig authored Jan 28, 2024
2 parents 53fee7f + e30b060 commit 44aa33f
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 228 deletions.
11 changes: 10 additions & 1 deletion Arcade-DonkeyKong.sv
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ wire [1:0] ar = status[20:19];
assign VIDEO_ARX = (!ar) ? ((status[2]|mod_pestplace) ? 8'd8 : 8'd7) : (ar - 1'd1);
assign VIDEO_ARY = (!ar) ? ((status[2]|mod_pestplace) ? 8'd7 : 8'd8) : 12'd0;

// Status Bit Map:
// Upper Lower
// 0 1 2 3 4 5 6
// 01234567890123456789012345678901 23456789012345678901234567890123
// 0123456789ABCDEFGHIJKLMNOPQRSTUV 0123456789ABCDEFGHIJKLMNOPQRSTUV
// XXXXXX X XXXXXXXXXXXXX

`include "build_id.v"
localparam CONF_STR = {
"A.DKONG;;",
Expand All @@ -202,7 +209,8 @@ localparam CONF_STR = {
"O35,Scandoubler Fx,None,HQ2x,CRT 25%,CRT 50%,CRT 75%;",
"H1O7,Flip Screen,Off,On;",
"OOS,Analog Video H-Pos,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31;",
"OTV,Analog Video V-Pos,0,1,2,3,4,5,6,7;",
"OTV,Analog Video V-Pos,0,1,2,3,4,5,6,7;",
"O6,Sound Effects,Sampled,Emulated;",
"H2ON,Autosave Hiscores,Off,On;",
"P1,Pause options;",
"P1OL,Pause when OSD is open,On,Off;",
Expand Down Expand Up @@ -464,6 +472,7 @@ dkong_top dkong(
.O_PIX(clk_pix),

.flip_screen(status[7]),
.use_emulated_sfx(status[6]),
.H_OFFSET(status[28:24]),
.V_OFFSET(status[31:29]),

Expand Down
2 changes: 1 addition & 1 deletion files.qip
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ set_global_assignment -name VERILOG_FILE rtl/dkong_top.v
set_global_assignment -name VERILOG_FILE rtl/dkong_dma.v
set_global_assignment -name VHDL_FILE rtl/dpram.vhd
set_global_assignment -name VERILOG_FILE rtl/i8035ip.v
set_global_assignment -name VERILOG_FILE rtl/dkong_wav_sound.v
set_global_assignment -name SYSTEMVERILOG_FILE rtl/dkong_wav_sound.sv
set_global_assignment -name SYSTEMVERILOG_FILE rtl/dkong_soundboard.sv
set_global_assignment -name VERILOG_FILE rtl/dkong_vram.v
set_global_assignment -name VERILOG_FILE rtl/dkong_sound.v
Expand Down
Binary file added releases/Arcade-DonkeyKong_20240122.rbf
Binary file not shown.
48 changes: 27 additions & 21 deletions rtl/dkong_soundboard.sv
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module dkong_soundboard #(
) (
input W_CLK_24576M,
input W_RESETn,
input use_emulated_sfx,
input I_DKJR, /// 1 = Emulate Donkey Kong JR, 3 or PestPlace (async not a problem)
input W_W0_WE,
input W_W1_WE,
Expand Down Expand Up @@ -36,14 +37,14 @@ wire I8035_T1;
wire I8035_RSTn;

// emulate 6 MHz crystal oscillor
localparam increment_width = 17; // ceil(RATE_decimal_precision * 3.32192)
localparam increment_width = 17; // increment_width = ceil(RATE_decimal_precision * 3.32192)
reg [increment_width:0] count; // one longer for overflow bit.
localparam X1_RATE = 6000000;
localparam [increment_width:0] increment = (X1_RATE / W_CLK_24576M_RATE) * 2**increment_width;
localparam int fraction_mutliplier = (1<<<increment_width);
// This somehow refuses to work:
localparam I8035_CLK_FRACTION = 6000000.0 / W_CLK_24576M_RATE;

always @(posedge W_CLK_24576M) begin
count[increment_width] <= 1'b0; // keep overflow bit for just one cycle
count <= count + increment;
count <= {1'b0, count[increment_width - 1:0]} + (increment_width + 1)'(fraction_mutliplier * I8035_CLK_FRACTION);
end
assign I8035_CLK_EN = count[increment_width];

Expand Down Expand Up @@ -99,7 +100,7 @@ dkong_sound Digtal_sound
//---- DAC I/F ------------------------

localparam SAMPLE_RATE = 48000;
localparam [8:0] clocks_per_sample = 24000000 / 48000;
localparam [9:0] clocks_per_sample = 10'(W_CLK_24576M_RATE / SAMPLE_RATE);

wire signed[15:0] W_D_S_DATB;

Expand All @@ -118,7 +119,7 @@ iir_2nd_order filter
(
.clk(W_CLK_24576M),
.reset(~W_RESETn),
.div({3'd0, clocks_per_sample}),
.div(clocks_per_sample),
.A2(-18'sd26649),
.A3(18'sd11453),
.B1(18'sd215),
Expand All @@ -134,20 +135,20 @@ dkong_wav_sound #(
) Analog_sound (
.I_CLK(W_CLK_24576M),
.I_RSTn(W_RESETn),
.I_SW(I_DKJR ? 2'b00 : W_6H_Q[2:1]),
.I_SW(I_DKJR ? 3'b00 : {W_6H_Q[2:1],W_6H_Q[0] | use_emulated_sfx}),
.O_ROM_AB(WAV_ROM_A)
);

reg[8:0] audio_clk_counter;
reg[9:0] audio_clk_counter;
reg audio_clk_en;
always@(posedge W_CLK_24576M, negedge W_RESETn) begin
if(!W_RESETn)begin
audio_clk_en <= 0;
audio_clk_counter <= 0;
end else begin
if(audio_clk_counter != (clocks_per_sample - 9'd1))begin
if(audio_clk_counter != (clocks_per_sample - 1'd1)) begin
audio_clk_en <= 0;
audio_clk_counter <= audio_clk_counter + 9'd1;
audio_clk_counter <= audio_clk_counter + 1'd1;
end else begin
audio_clk_en <= 1;
audio_clk_counter <= 0;
Expand All @@ -156,25 +157,30 @@ always@(posedge W_CLK_24576M, negedge W_RESETn) begin
end

wire signed[15:0] walk_out;
dk_walk #(.CLOCK_RATE(W_CLK_24576M_RATE),.SAMPLE_RATE(SAMPLE_RATE)) walk (
dk_walk #(
.CLOCK_RATE(W_CLK_24576M_RATE),
.SAMPLE_RATE(SAMPLE_RATE)
) walk (
.clk(W_CLK_24576M),
.I_RSTn(W_RESETn),
.audio_clk_en(audio_clk_en),
.walk_en(~W_6H_Q[0]),
.walk_en(~W_6H_Q[0] & use_emulated_sfx),
.out(walk_out)
);

// SOUND MIXER (WAV + DIG ) -----------------------

wire signed[15:0] sound_mix =
(I_DKJR ? 16'd0 : {{3{~WAV_ROM_DO[7]}}, WAV_ROM_DO[6:0],6'b0}) +
{{3{W_D_S_DATC[15]}},W_D_S_DATC[14:2]} + {{5{W_D_S_DATC[15]}},W_D_S_DATC[14:4]} +
walk_out;
wire signed[16:0] sound_mix =
(I_DKJR ? 17'd0 : {{4{~WAV_ROM_DO[7]}}, WAV_ROM_DO[6:0],6'b0}) +
{{4{W_D_S_DATC[15]}},W_D_S_DATC[14:2]} + {{6{W_D_S_DATC[15]}},W_D_S_DATC[14:4]} +
{{2{walk_out[15]}},walk_out[14:0]};

always@(posedge W_CLK_24576M_RATE) begin
if (audio_clk_en) begin
O_SOUND_DAT <= sound_mix;
end

always@(posedge W_CLK_24576M) begin
O_SOUND_DAT <=
sound_mix[16:15] == 2'b01 ? 16'h7FFF :
sound_mix[16:15] == 2'b10 ? 16'h8000 :
sound_mix[15:0];
end

endmodule
22 changes: 20 additions & 2 deletions rtl/dkong_top.v
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,29 @@
// 2005- 2- 9 Data on the ROM are initialized at the time of the start.
// added device.
// changed module I/O.
//
// This description is largely based on the TKG4 schematics, with some extensions
// to support Radarscope and Donkey Kong Jr (although MiSter currently has a separate DKJR core).
//
// In the schematics, ICs are denoted by their position on the board, i.e. IC 6M
// is the IC in column 6, row M. Unfortunataly, this makes reverse lookup from
// Verilog to schematic hard, but at least ICs with similar coordinates are often cloase
// together in the schematic too.
// In the Verilog description, signals were orignally named by the source IC and pin
// name. Later changes did not always follow this convention.
// This naming can be confusing, because is not always unique:
// e.g. IC 4H occurs both on the VIDEO and CPU board.
//
// Contrary to Radarscope and TGK2, the TKG4 does not really have a sound board,
// so even in that code, numbering and naming of the TKG4 CPU board is largely
// followed.
//
//================================================================================

module dkong_top
(
// FPGA_USE
input I_CLK_24576M,
input I_CLK_24M,
input I_RESETn,
output O_PIX,

Expand All @@ -43,6 +59,7 @@ module dkong_top

// VGA (VIDEO) IF
input flip_screen,
input use_emulated_sfx,
input [8:0] H_OFFSET,
input [8:0] V_OFFSET,

Expand Down Expand Up @@ -112,7 +129,7 @@ wire W_SW2_OEn ;
wire W_SW3_OEn ;
wire W_DIP_OEn ;

wire [2:0]W_4H_Q;
wire [1:0]W_4H_Q;
wire [7:0]W_5H_Q;
wire [7:0]W_6H_Q;
wire [4:0]W_3D_Q;
Expand Down Expand Up @@ -568,6 +585,7 @@ dkong_col_pal cpal
dkong_soundboard dkong_soundboard(
.W_CLK_24576M(W_CLK_24576M & ~paused),
.W_RESETn(W_RESETn),
.use_emulated_sfx(use_emulated_sfx),
.I_DKJR(I_DKJR),
.O_SOUND_DAT(O_SOUND_DAT),
.O_SACK(W_SACK),
Expand Down
Loading

0 comments on commit 44aa33f

Please sign in to comment.