forked from gaz68/Arcade-DonkeyKong3_MiSTer_WIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arcade-DonkeyKong3.sv
459 lines (376 loc) · 10.8 KB
/
Arcade-DonkeyKong3.sv
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
///----------------------------------------------------------------------------
//
// Arcade: Donkey kong 3 by gaz68 (https://github.com/gaz68)
//
// July 2020
//
// Based on the original Donkey Kong core by Katsumi Degawa.
//
// Original Donkey Kong port to MiSTer
// Copyright (C) 2017 Sorgelig
//
// This program 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 2 of the License, or (at your option)
// any later version.
//
// This program 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 this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
//----------------------------------------------------------------------------
module emu
(
//Master input clock
input CLK_50M,
//Async reset from top-level module.
//Can be used as initial reset.
input RESET,
//Must be passed to hps_io module
inout [45:0] HPS_BUS,
//Base video clock. Usually equals to CLK_SYS.
output CLK_VIDEO,
//Multiple resolutions are supported using different CE_PIXEL rates.
//Must be based on CLK_VIDEO
output CE_PIXEL,
//Video aspect ratio for HDMI. Most retro systems have ratio 4:3.
output [7:0] VIDEO_ARX,
output [7:0] VIDEO_ARY,
output [7:0] VGA_R,
output [7:0] VGA_G,
output [7:0] VGA_B,
output VGA_HS,
output VGA_VS,
output VGA_DE, // = ~(VBlank | HBlank)
output VGA_F1,
output [1:0] VGA_SL,
// Use framebuffer from DDRAM (USE_FB=1 in qsf)
// FB_FORMAT:
// [2:0] : 011=8bpp(palette) 100=16bpp 101=24bpp 110=32bpp
// [3] : 0=16bits 565 1=16bits 1555
// [4] : 0=RGB 1=BGR (for 16/24/32 modes)
//
// stride is modulo 256 of bytes
output FB_EN,
output [4:0] FB_FORMAT,
output [11:0] FB_WIDTH,
output [11:0] FB_HEIGHT,
output [31:0] FB_BASE,
input FB_VBL,
input FB_LL,
output LED_USER, // 1 - ON, 0 - OFF.
// b[1]: 0 - LED status is system status OR'd with b[0]
// 1 - LED status is controled solely by b[0]
// hint: supply 2'b00 to let the system control the LED.
output [1:0] LED_POWER,
output [1:0] LED_DISK,
input CLK_AUDIO, // 24.576 MHz
output [15:0] AUDIO_L,
output [15:0] AUDIO_R,
output AUDIO_S, // 1 - signed audio samples, 0 - unsigned
//High latency DDR3 RAM interface
//Use for non-critical time purposes
output DDRAM_CLK,
input DDRAM_BUSY,
output [7:0] DDRAM_BURSTCNT,
output [28:0] DDRAM_ADDR,
input [63:0] DDRAM_DOUT,
input DDRAM_DOUT_READY,
output DDRAM_RD,
output [63:0] DDRAM_DIN,
output [7:0] DDRAM_BE,
output DDRAM_WE,
// Open-drain User port.
// 0 - D+/RX
// 1 - D-/TX
// 2..6 - USR2..USR6
// Set USER_OUT to 1 to read from USER_IN.
input [6:0] USER_IN,
output [6:0] USER_OUT
);
assign VGA_F1 = 0;
assign USER_OUT = '1;
assign LED_USER = ioctl_download;
assign LED_DISK = 0;
assign LED_POWER = 0;
assign VIDEO_ARX = status[1] ? 8'd16 : status[2] ? 8'd4 : 8'd3;
assign VIDEO_ARY = status[1] ? 8'd9 : status[2] ? 8'd3 : 8'd4;
`include "build_id.v"
localparam CONF_STR = {
"A.DKONG3;;",
"-;",
"O1,Aspect Ratio,Original,Wide;",
"O2,Orientation,Vert,Horz;",
"O35,Scandoubler Fx,None,HQ2x,CRT 25%,CRT 50%,CRT 75%;",
"-;",
"DIP;",
//"O89,Lives,3,4,5,6;",
//"OAB,Extra Life,30K,40K,50K,None;",
//"OCD,Additional Life,30K,40K,50K,None;",
//"OEF,Difficulty,1 (Easy),2,3,4 (Hard);",
//"OGI,Coin/Credit,1/1,2/1,3/1,1/2,1/3,1/5,1/4,1/6;",
//"OJ,Test Mode,Off,On;",
//"OK,Cabinet,Upright,Cocktail;",
"-;",
"R0,Reset;",
"J1,Jump,Start 1P,Start 2P,Coin;",
"jn,A,Start,Select,R;",
"V,v",`BUILD_DATE
};
//////////////////// CLOCKS ///////////////////
wire clk_sys;
wire clk_main;
wire clk_sub;
pll pll
(
.refclk(CLK_50M),
.rst(0),
.outclk_0(clk_sys), // 24.576Mhz
.outclk_1(clk_sub), // 21.477Mhz
.outclk_2(clk_main) // 4Mhz
);
///////////////////////////////////////////////////
wire [31:0] status;
wire [1:0] buttons;
wire forced_scandoubler;
wire direct_video;
wire ioctl_download;
wire ioctl_wr;
wire [24:0] ioctl_addr;
wire [7:0] ioctl_dout;
wire [7:0] ioctl_index;
wire [10:0] ps2_key;
wire [15:0] joy_0, joy_1;
wire [21:0] gamma_bus;
hps_io #(.STRLEN($size(CONF_STR)>>3)) hps_io
(
.clk_sys(clk_sys),
.HPS_BUS(HPS_BUS),
.EXT_BUS(),
.conf_str(CONF_STR),
.buttons(buttons),
.status(status),
.forced_scandoubler(forced_scandoubler),
.gamma_bus(gamma_bus),
.direct_video(direct_video),
.status_menumask(direct_video),
.ioctl_download(ioctl_download),
.ioctl_wr(ioctl_wr),
.ioctl_addr(ioctl_addr),
.ioctl_dout(ioctl_dout),
.ioctl_index(ioctl_index),
.joystick_0(joy_0),
.joystick_1(joy_1),
.ps2_key(ps2_key)
);
wire pressed = ps2_key[9];
wire [8:0] code = ps2_key[8:0];
always @(posedge clk_sys) begin
reg old_state;
old_state <= ps2_key[10];
if(old_state != ps2_key[10]) begin
casex(code)
'hX75: btn_up <= pressed; // up
'hX72: btn_down <= pressed; // down
'hX6B: btn_left <= pressed; // left
'hX74: btn_right <= pressed; // right
'h029: btn_fire <= pressed; // space
'h014: btn_fire <= pressed; // ctrl
// JPAC/IPAC/MAME Style Codes
'h005: btn_one_player <= pressed; // F1
'h006: btn_two_players <= pressed; // F2
'h016: btn_start_1 <= pressed; // 1
'h01E: btn_start_2 <= pressed; // 2
'h02E: btn_coin_1 <= pressed; // 5
'h036: btn_coin_2 <= pressed; // 6
'h02D: btn_up_2 <= pressed; // R
'h02B: btn_down_2 <= pressed; // F
'h023: btn_left_2 <= pressed; // D
'h034: btn_right_2 <= pressed; // G
'h01C: btn_fire_2 <= pressed; // A
'h02C: btn_test <= pressed; // T
endcase
end
end
reg btn_up = 0;
reg btn_down = 0;
reg btn_right = 0;
reg btn_left = 0;
reg btn_fire = 0;
reg btn_one_player = 0;
reg btn_two_players = 0;
reg btn_start_1 = 0;
reg btn_start_2 = 0;
reg btn_coin_1 = 0;
reg btn_coin_2 = 0;
reg btn_up_2 = 0;
reg btn_down_2 = 0;
reg btn_left_2 = 0;
reg btn_right_2 = 0;
reg btn_fire_2 = 0;
reg btn_test = 0;
wire m_up,m_down,m_left,m_right;
joy8way joy1
(
clk_sys,
{
status[2] ? btn_left | joy_0[1] : btn_up | joy_0[3],
status[2] ? btn_right | joy_0[0] : btn_down | joy_0[2],
status[2] ? btn_down | joy_0[2] : btn_left | joy_0[1],
status[2] ? btn_up | joy_0[3] : btn_right | joy_0[0]
},
{m_up,m_down,m_left,m_right}
);
wire m_up_2,m_down_2,m_left_2,m_right_2;
joy8way joy2
(
clk_sys,
{
status[2] ? btn_left_2 | joy_1[1] : btn_up_2 | joy_1[3],
status[2] ? btn_right_2 | joy_1[0] : btn_down_2 | joy_1[2],
status[2] ? btn_down_2 | joy_1[2] : btn_left_2 | joy_1[1],
status[2] ? btn_up_2 | joy_1[3] : btn_right_2 | joy_1[0]
},
{m_up_2,m_down_2,m_left_2,m_right_2}
);
wire m_fire = btn_fire | joy_0[4];
wire m_fire_2 = btn_fire_2 | joy_1[4];
wire m_start1 = btn_one_player | joy_0[5] | joy_1[5];
wire m_start2 = btn_two_players | joy_0[6] | joy_1[6];
wire m_coin = joy_0[7] | joy_1[7];
wire coinpulse;
shortpulse coin
(
clk_sys,
m_coin|btn_coin_1|btn_coin_2,
coinpulse
);
wire [7:0]m_sw1={~btn_test,~{m_start2|btn_start_2},~{m_start1|btn_start_1},~m_fire,~m_down,~m_up,~m_left,~m_right};
wire [7:0]m_sw2={1'b1,1'b1,~coinpulse,~m_fire_2,~m_down_2,~m_up_2,~m_left_2,~m_right_2};
//wire [7:0]m_dip1 = status[15:8];
//wire [7:0]m_dip2 = {status[20:19],3'b000,status[18:16]};
reg [7:0] sw[8];
always @(posedge clk_sys) if (ioctl_wr && (ioctl_index==254) && !ioctl_addr[24:3]) sw[ioctl_addr[2:0]] <= ioctl_dout;
wire hblank, vblank;
wire hs, vs;
wire [3:0] r,g,b;
wire rotate_ccw = 0;
wire no_rotate = status[2] | direct_video ;
screen_rotate screen_rotate (.*);
arcade_video#(256,12) arcade_video
(
.*,
.clk_video(clk_sys),
.ce_pix(ce_vid),
.RGB_in({r,g,b}),
.HBlank(hblank),
.VBlank(vblank),
.HSync(~hs),
.VSync(~vs),
.fx(status[5:3])
);
assign AUDIO_S = 1'b1;
assign AUDIO_L = |mute_cnt ? 16'd0 : sample_signed[15:0];
assign AUDIO_R = AUDIO_L;
wire signed [15:0] sample_signed;
reg [20:0] mute_cnt = 21'h1FFFFF;
// Pause audio to avoid loud "POP"
always_ff @(posedge clk_sub) begin
if (res)
mute_cnt <= 21'h1FFFFF;
else if (|mute_cnt)
mute_cnt <= mute_cnt - 1'b1;
end
assign hblank = hbl[8];
reg ce_vid;
wire clk_pix;
wire hbl0;
reg [8:0] hbl;
always @(posedge clk_sys) begin
reg old_pix;
old_pix <= clk_pix;
ce_vid <= 0;
if(~old_pix & clk_pix) begin
ce_vid <= 1;
hbl <= (hbl<<1)|hbl0;
end
end
wire res = RESET | status[0] | buttons[1];
dkong3_top dkong3
(
.I_CLK_24M(clk_sys),
.I_CLK_4M(clk_main),
.I_SUBCLK(clk_sub),
.I_RESETn(~res),
.I_SW1(m_sw1),
.I_SW2(m_sw2),
//.I_DIP_SW1(m_dip1),
//.I_DIP_SW2(m_dip2),
.I_DIP_SW1(sw[0]),
.I_DIP_SW2(sw[1]),
.dn_addr(ioctl_addr),
.dn_data(ioctl_dout),
.dn_wr(ioctl_wr && ioctl_index==0),
.O_VGA_R(r),
.O_VGA_G(g),
.O_VGA_B(b),
.O_VGA_HSYNCn(hs),
.O_VGA_VSYNCn(vs),
.O_HBLANK(hbl0),
.O_VBLANK(vblank),
.O_PIX(clk_pix),
.O_SOUND_DAT(sample_signed)
);
endmodule
// Handle the case where Up and Down are pressed simultaneously
// and the same for Left and Right i.e. when using a keyboard.
module joy8way
(
input clk,
input [3:0] indir,
output [3:0] outdir
);
reg [3:0] out = 0;
reg [3:0] in1,in2;
wire [3:0] innew = in1 & ~in2;
reg [1:0] last_h,last_v;
assign outdir = out;
always @(posedge clk) begin
in1 <= indir;
in2 <= in1;
if(innew[0]) last_h <= 2'b01; // R
if(innew[1]) last_h <= 2'b10; // L
if(innew[2]) last_v <= 2'b01; // D
if(innew[3]) last_v <= 2'b10; // U
out[1:0] <= in1[1:0] == 2'b11 ? last_h : in1[1:0];
out[3:2] <= in1[3:2] == 2'b11 ? last_v : in1[3:2];
end
endmodule
// Generates a short pulse at the
// end of the input signal.
module shortpulse
(
input clk,
input inp,
output pulse
);
reg out;
reg [19:0] pulse_cnt = 0;
always_ff @(posedge clk) begin
reg old_inp;
out <= 1'b0;
if (|pulse_cnt) begin
pulse_cnt <= pulse_cnt - 1'b1;
out <= 1'b1;
end else begin
old_inp <= inp;
if (old_inp && !inp) pulse_cnt <= 20'hFFFFF;
end
end
assign pulse = out;
endmodule