forked from MiSTer-devel/NeoGeo_MiSTer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
NeoGeo.sv
2146 lines (1838 loc) · 58 KB
/
NeoGeo.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//============================================================================
// SNK NeoGeo for MiSTer
//
// Copyright (C) 2018 Sean 'Furrtek' Gonsalves
//
// 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.
//============================================================================
// Current status:
// Neo CD CD check ok but crashes when loading. No more video glitches.
// How about not using a cache at all and DMAing directly from the data fed by the HPS ?
// The "sector ready" IRQ could be triggered just when a sector is requested, and the actual transfer
// could be done when the system ROM starts up the DMA transfer ?
// Neo CD times 12/05/2019:
// SECTOR_READY goes high at SECTOR_TIMER == 430631
// So it takes 600000-430631=169369 ticks to get a sector from HPS
// 169369/120M=1.41ms (1451 kB/s)
// DMA copy: 215 ticks for 10 bytes
// So 215/120M/10=179.17ns per byte (5450 kB/s)
// Neo CD times 11/05/2019:
// DMA copy: 616 ticks for 10 bytes
// So 215/120M/10=513.3ns per byte (1902 kB/s)
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 [48: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.
//if VIDEO_ARX[12] or VIDEO_ARY[12] is set then [11:0] contains scaled size instead of aspect ratio.
output [12:0] VIDEO_ARX,
output [12: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,
output VGA_SCALER, // Force VGA scaler
input [11:0] HDMI_WIDTH,
input [11:0] HDMI_HEIGHT,
output HDMI_FREEZE,
`ifdef MISTER_FB
// Use framebuffer in 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)
//
// FB_STRIDE either 0 (rounded to 256 bytes) or multiple of pixel size (in bytes)
output FB_EN,
output [4:0] FB_FORMAT,
output [11:0] FB_WIDTH,
output [11:0] FB_HEIGHT,
output [31:0] FB_BASE,
output [13:0] FB_STRIDE,
input FB_VBL,
input FB_LL,
output FB_FORCE_BLANK,
`ifdef MISTER_FB_PALETTE
// Palette control for 8bit modes.
// Ignored for other video modes.
output FB_PAL_CLK,
output [7:0] FB_PAL_ADDR,
output [23:0] FB_PAL_DOUT,
input [23:0] FB_PAL_DIN,
output FB_PAL_WR,
`endif
`endif
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,
// I/O board button press simulation (active high)
// b[1]: user button
// b[0]: osd button
output [1:0] BUTTONS,
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
output [1:0] AUDIO_MIX, // 0 - no mix, 1 - 25%, 2 - 50%, 3 - 100% (mono)
//ADC
inout [3:0] ADC_BUS,
//SD-SPI
output SD_SCK,
output SD_MOSI,
input SD_MISO,
output SD_CS,
input SD_CD,
//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,
//SDRAM interface with lower latency
output SDRAM_CLK,
output SDRAM_CKE,
output [12:0] SDRAM_A,
output [1:0] SDRAM_BA,
inout [15:0] SDRAM_DQ,
output SDRAM_DQML,
output SDRAM_DQMH,
output SDRAM_nCS,
output SDRAM_nCAS,
output SDRAM_nRAS,
output SDRAM_nWE,
`ifdef MISTER_DUAL_SDRAM
//Secondary SDRAM
//Set all output SDRAM_* signals to Z ASAP if SDRAM2_EN is 0
input SDRAM2_EN,
output SDRAM2_CLK,
output [12:0] SDRAM2_A,
output [1:0] SDRAM2_BA,
inout [15:0] SDRAM2_DQ,
output SDRAM2_nCS,
output SDRAM2_nCAS,
output SDRAM2_nRAS,
output SDRAM2_nWE,
`endif
input UART_CTS,
output UART_RTS,
input UART_RXD,
output UART_TXD,
output UART_DTR,
input UART_DSR,
// 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,
input OSD_STATUS
);
assign ADC_BUS = 'Z;
assign USER_OUT = '1;
assign {UART_RTS, UART_TXD, UART_DTR} = 0;
assign {SD_SCK, SD_MOSI, SD_CS} = 'Z;
assign AUDIO_S = 1; // Signed
assign AUDIO_MIX = status[5:4];
assign AUDIO_L = snd_left;
assign AUDIO_R = snd_right;
assign LED_USER = status[0] | bk_pending;
assign LED_DISK = 0;
assign LED_POWER = 0;
assign BUTTONS = osd_btn;
assign VGA_SCALER= 0;
assign HDMI_FREEZE = 0;
wire [1:0] ar = status[33:32];
wire vcrop_en = status[34];
wire [3:0] vcopt = status[38:35];
reg en216p;
reg [4:0] voff;
always @(posedge CLK_VIDEO) begin
en216p <= ((HDMI_WIDTH == 1920) && (HDMI_HEIGHT == 1080) && !forced_scandoubler && !scale);
voff <= (vcopt < 6) ? {vcopt,1'b0} : ({vcopt,1'b0} - 5'd24);
end
wire vga_de;
video_freak video_freak
(
.*,
.VGA_DE_IN(vga_de),
.ARX((!ar) ? 12'd10 : (ar - 1'd1)),
.ARY((!ar) ? 12'd7 : 12'd0),
.CROP_SIZE((en216p & vcrop_en) ? 10'd216 : 10'd0),
.CROP_OFF(voff),
.SCALE(status[40:39])
);
// status bit definition:
// 31 23 15 7
// --AA-PSS -------- L--CGGDD DEEMVTTR
// : status[0] System Reset, used by the HPS, keep it there
// T: status[2:1] System type, 0=Console, 1=Arcade, 2=CD, 3=CDZ
// V: status[3] Video mode
// M: status[4] Memory card presence
// E: status[6:5] Stereo mix
// D: status[9:7] DIP switches
// G: status[11:10] Neo CD region
// C: status[12] Save memory card & backup RAM
// L: status[12] CD lid state (DEBUG)
// : status[14] Manual Reset
// : status[20:15] OSD options
// : status[21] bk_load
// : status[22] BIOS 0=original 1=universe
// : status[24] wire bk_autosave = status[24];
// : status[28:25] sound debug
// : status[33:32] ar
// : status[34] vcrop_en
// : status[38:35] vcopt
// : status[40:39] scale
// : status[47:44] H-sync adjust
// : status[51:48] V-sync adjust
// : status[57:62] sound debug (ADPCMA channels)
// 0123456789 ABCDEFGHIJKLMNO
// Conditional modification of the CONF strings chaining according to chosen system type
// Con Arc CD CDz
// 00 01 10 11
// F F + + ~SYSTEM_CDx;
// + + S S SYSTEM_CDx;
// O O + + ~SYSTEM_CDx;
// + O + + SYSTEM_MVS;
// + + O O SYSTEM_CDx;
// Status Bit Map:
// Upper Lower
// 0 1 2 3 4 5 6
// 01234567890123456789012345678901 23456789012345678901234567890123
// 0123456789ABCDEFGHIJKLMNOPQRSTUV 0123456789ABCDEFGHIJKLMNOPQRSTUV
// XXXXXXXXXXXXX XXX XXXXX XXXXX XXXXXXXXXXX XXXXXX
`include "build_id.v"
localparam CONF_STR = {
"NEOGEO;;",
"-;",
`ifndef MVS_ARCADE_LOAD
"H0FS1,*,Load ROM;",
`ifdef NGCD_SUPPORT
"H1S1,ISOBIN,Load CD Image;",
`endif
"-;",
`endif
"D6H2O9,Pause (Stop Mode),Off,On;",
"H2-;",
`ifdef NGCD_SUPPORT
"O12,System Type,Console(AES),Arcade(MVS),CD,CDZ;",
`elsif MVS_ARCADE_LOAD
`else
"O12,System Type,Console(AES),Arcade(MVS);"
`endif
// Page 1 - System Settings
`ifndef MVS_ARCADE_LOAD
"P1,System Settings;",
"P1-;",
`ifdef NGCD_SUPPORT
"P1O12,Select System,AES,MVS,CD,CDZ;",
"H1P1OAB,Region,US,EU,JP,AS;",
"H1P1OF,CD lid,Opened,Closed;",
`else
"P1O12,Select System,AES,MVS;",
`endif
"P1OM,Select BIOS,UniBIOS,Original;",
"P1-;",
"H0P1O4,Memory Card,Plugged,Unplugged;",
"P1RL,Reload Memory Card;",
"D4P1RC,Save Memory Card;",
"P1OO,Autosave,OFF,ON;",
"P1-;",
"P1RE,Reset & Apply;", // decouple manual reset from system reset
`endif
// Page 2 - HARD DIPS
"H2P2,HARD DIP Settings;",
"H2P2-;",
"H2P2O7,Setting Mode,Off,On;",
"H2P2O8,Free Play,Off,On;",
"H2P2O9,Stop Mode,Off,On;",
// Page 3 - Video & Audio Settings
"P3,Video & Audio Settings;",
"P3-;",
`ifndef MVS_ARCADE_LOAD
"P3O3,Video Mode,NTSC,PAL;",
`endif
"P3OG,Width,320px,304px;",
"P3OIK,Scandoubler Fx,None,N/A,CRT 25%,CRT 50%,CRT 75%;",
"P3-;",
"P3oCF,H-sync Adjust,0,1,2,3,4,5,6,7,-8,-7,-6,-5,-4,-3,-2,-1;",
"P3oGJ,V-sync Adjust,0,1,2,3,4,5,6,7,-8,-7,-6,-5,-4,-3,-2,-1;",
"P3-;",
"P3o01,Aspect Ratio,Original,Full Screen,[ARC1],[ARC2];",
"d5P3o2,Vertical Crop,Disabled,216p(5x);",
"d5P3o36,Crop Offset,0,2,4,8,10,12,-12,-10,-8,-6,-4,-2;",
"P3o78,Scale,Normal,V-Integer,Narrower HV-Integer,Wider HV-Integer;",
"P3-;",
"P3O56,Stereo Mix,none,25%,50%,100%;",
"P3-;",
`ifdef SOUND_DEBUG
// Page 4 - Hidden sound debug menu
"P4H3,Sound Debug;",
"P4H3OP,FM,ON,OFF;",
"P4H3OQ,ADPCMA,ON,OFF;",
"P4H3OR,ADPCMB,ON,OFF;",
"P4H3OS,PSG,ON,OFF;",
"P4H3oP,ADPCMA CH 1,ON,OFF;",
"P4H3oQ,ADPCMA CH 2,ON,OFF;",
"P4H3oR,ADPCMA CH 3,ON,OFF;",
"P4H3oS,ADPCMA CH 4,ON,OFF;",
"P4H3oT,ADPCMA CH 5,ON,OFF;",
"P4H3oU,ADPCMA CH 6,ON,OFF;",
"P4H3-;",
`endif
"-;",
"o9A,Input,Joystick or Spinner,Joystick,Spinner,Mouse(Irr.Maze);",
"H2oB,Pause Mode,OSD,DIP;",
"-;",
"RE,Reset & Apply;", // decouple manual reset from system reset
"J1,A,B,C,D,Start,Select,Coin,ABC;", // ABC is a special key to press A+B+C at once, useful for keyboards that don't allow more than 2 keypresses at once and to access UniBIOS
"jn,A,B,X,Y,Start,Select,L,R;", // name mapping
"jp,B,A,D,C,Start,Select,L,R;", // positional mapping consistent with NeoGeoCD controller
"V,v",`BUILD_DATE
};
//////////////////// CLOCKS ///////////////////
wire locked;
wire clk_sys;
wire CLK_24M = counter_p[1];
// 50MHz in, 4*24=96MHz out
// CAS latency = 2 (20.8ns)
pll pll(
.refclk(CLK_50M),
.rst(0),
.outclk_0(clk_sys),
.outclk_1(CLK_VIDEO),
.reconfig_to_pll(reconfig_to_pll),
.reconfig_from_pll(reconfig_from_pll),
.locked(locked)
);
wire [63:0] reconfig_to_pll;
wire [63:0] reconfig_from_pll;
wire cfg_waitrequest;
reg cfg_write;
reg [5:0] cfg_address;
reg [31:0] cfg_data;
pll_cfg pll_cfg
(
.mgmt_clk(CLK_50M),
.mgmt_reset(0),
.mgmt_waitrequest(cfg_waitrequest),
.mgmt_read(0),
.mgmt_readdata(),
.mgmt_write(cfg_write),
.mgmt_address(cfg_address),
.mgmt_writedata(cfg_data),
.reconfig_to_pll(reconfig_to_pll),
.reconfig_from_pll(reconfig_from_pll)
);
always @(posedge CLK_50M) begin
reg sys_mvs = 0, sys_mvs2 = 0;
reg [2:0] state = 0;
reg sys_mvs_r;
sys_mvs <= SYSTEM_MVS;
sys_mvs2 <= sys_mvs;
cfg_write <= 0;
if(sys_mvs2 == sys_mvs && sys_mvs2 != sys_mvs_r) begin
state <= 1;
sys_mvs_r <= sys_mvs2;
end
if(!cfg_waitrequest) begin
if(state) state<=state+1'd1;
case(state)
1: begin
cfg_address <= 0;
cfg_data <= 0;
cfg_write <= 1;
end
5: begin
cfg_address <= 7;
cfg_data <= sys_mvs_r ? 2576980378 : 2865308404;
cfg_write <= 1;
end
7: begin
cfg_address <= 2;
cfg_data <= 0;
cfg_write <= 1;
end
endcase
end
end
// The watchdog should output nRESET but it makes video sync stop for a moment, so the
// MiSTer OSD jumps around. Provide an indication for devs that a watchdog reset happened ?
reg [14:0] TRASH_ADDR;
reg [1:0] SYSTEM_TYPE;
reg nRESET;
always @(posedge CLK_24M) begin
nRESET <= &TRASH_ADDR;
if (~&TRASH_ADDR) TRASH_ADDR <= TRASH_ADDR + 1'b1;
if (ioctl_download | status[0] | status[14] | buttons[1] | bk_loading | RESET | key_reset) begin
`ifndef MVS_ARCADE_LOAD
TRASH_ADDR <= 0;
SYSTEM_TYPE <= status[2:1]; // Latch the system type on reset
`else
SYSTEM_TYPE <= 'd1;
// Ugly but delays reset in MRA mitigating issue with sound initialization
// at the start on some titles (like twinspri, mslugx etc.)
TRASH_ADDR <= 15'h7f00;
`endif
end
end
reg [1:0] counter_p = 0;
always @(posedge clk_sys) counter_p <= counter_p + 1'd1;
reg osd_btn = 0;
`ifndef MVS_ARCADE_LOAD
always @(posedge CLK_24M) begin
integer timeout = 0;
reg last_rst = 0;
if (RESET)
last_rst = 0;
if (status[0])
last_rst = 1;
if (last_rst & ~status[0]) begin
osd_btn <= 0;
if(timeout < 24000000) begin
timeout <= timeout + 1;
osd_btn <= 1;
end
end
end
`endif
////////////////// HPS I/O ///////////////////
// VD 0: Save file
// VD 1: CD bin file
// VD 2: CD cue file, let MiSTer binary take care of it, do not touch !
wire [1:0] img_mounted;
wire sd_buff_wr, img_readonly;
wire [7:0] sd_buff_addr; // Address inside 256-word sector
wire [15:0] sd_buff_dout;
wire [15:0] sd_buff_din[2];
wire [15:0] sd_req_type;
wire [63:0] img_size;
wire [31:0] sd_lba[2];
wire [1:0] sd_wr;
wire [1:0] sd_rd;
wire [1:0] sd_ack;
wire [15:0] joystick_0; // ----HNLS DCBAUDLR
wire [15:0] joystick_1;
wire [15:0] joy0 = joystick_0 | {key_select1, key_start1, key_p1_d, key_p1_c, key_p1_b, key_p1_a, key_p1_up, key_p1_down, key_p1_left, key_p1_right};
wire [15:0] joy1 = joystick_1 | {key_select2, key_start2, key_p2_d, key_p2_c, key_p2_b, key_p2_a, key_p2_up, key_p2_down, key_p2_left, key_p2_right};
wire [8:0] spinner_0, spinner_1;
wire [1:0] buttons;
wire [10:0] ps2_key;
wire [24:0] ps2_mouse;
wire forced_scandoubler;
wire [63:0] status;
wire [64:0] rtc;
wire ioctl_wr;
wire [26:0] ioctl_addr;
wire [15:0] ioctl_dout;
wire ioctl_download;
wire [7:0] ioctl_index;
wire SYSTEM_MVS = (SYSTEM_TYPE == 2'd1);
wire SYSTEM_CDx = SYSTEM_TYPE[1];
wire [15:0] sdram_sz;
wire [21:0] gamma_bus;
// 8-bit ioctl mode, used only for FIX ROMs in MVS Arcade load
`ifdef MVS_ARCADE_LOAD
localparam WIDE_IOCTL = 0;
`else
localparam WIDE_IOCTL = 1;
`endif
hps_io #(.CONF_STR(CONF_STR), .WIDE(WIDE_IOCTL), .VDNUM(2)) hps_io
(
.clk_sys(clk_sys),
.HPS_BUS(HPS_BUS),
.EXT_BUS(),
.forced_scandoubler(forced_scandoubler),
.joystick_0(joystick_0), .joystick_1(joystick_1),
.spinner_0(spinner_0), .spinner_1(spinner_1),
.ps2_mouse(ps2_mouse),
.buttons(buttons),
.ps2_key(ps2_key),
.status(status), // status read (64 bits)
.status_menumask({status[22], 8'd0, pause_mode, en216p, bk_autosave | ~bk_pending, ~dbg_menu,~SYSTEM_MVS,~SYSTEM_CDx,SYSTEM_CDx}),
.RTC(rtc),
.sdram_sz(sdram_sz),
.gamma_bus(gamma_bus),
// Loading signals
.ioctl_wr(ioctl_wr),
.ioctl_addr(ioctl_addr),
.ioctl_dout(ioctl_dout),
.ioctl_download(ioctl_download),
.ioctl_index(ioctl_index),
.ioctl_wait(ddram_wait | memcp_wait),
.sd_lba(sd_lba),
.sd_rd(sd_rd),
.sd_wr(sd_wr),
.sd_ack(sd_ack),
.sd_buff_addr(sd_buff_addr),
.sd_buff_dout(sd_buff_dout),
.sd_buff_din(sd_buff_din),
.sd_buff_wr(sd_buff_wr),
.img_mounted(img_mounted),
.img_readonly(img_readonly),
.img_size(img_size)
);
wire key_start1, key_start2, key_select1, key_select2;
wire key_coin1, key_coin2, key_coin3, key_coin4;
wire key_test, key_reset, key_service;
wire key_p1_up, key_p1_left, key_p1_down, key_p1_right, key_p1_a, key_p1_b, key_p1_c, key_p1_d;
wire key_p2_up, key_p2_left, key_p2_down, key_p2_right, key_p2_a, key_p2_b, key_p2_c, key_p2_d;
wire pressed = ps2_key[9];
reg dbg_menu = 0;
always @(posedge clk_sys) begin
reg old_stb;
reg enter = 0;
reg esc = 0;
old_stb <= ps2_key[10];
if(old_stb ^ ps2_key[10]) begin
casex(ps2_key[8:0])
'h05A: enter <= pressed;
'h076: esc <= pressed;
'h016: key_start1 <= pressed; // 1
'h01e: key_start2 <= pressed; // 2
'h02E: begin // 5
key_coin1 <= pressed & SYSTEM_MVS;
key_select1 <= pressed & ~SYSTEM_MVS;
end
'h036: begin // 6
key_coin2 <= pressed & SYSTEM_MVS;
key_select2 <= pressed & ~SYSTEM_MVS;
end
'h03d: key_coin3 <= pressed; // 7
'h03e: key_coin4 <= pressed; // 8
'h006: key_test <= pressed; // F2
'h004: key_reset <= pressed; // F3
'h046: key_service <= pressed; // 9
'hX75: key_p1_up <= pressed; // up
'hX6b: key_p1_left <= pressed; // left
'hX72: key_p1_down <= pressed; // down
'hX74: key_p1_right <= pressed; // right
'h014: key_p1_a <= pressed; // lctrl
'h011: key_p1_b <= pressed; // lalt
'h029: key_p1_c <= pressed; // space
'h012: key_p1_d <= pressed; // lshift
'h02d: key_p2_up <= pressed; // r
'h023: key_p2_left <= pressed; // d
'h02b: key_p2_down <= pressed; // f
'h034: key_p2_right <= pressed; // g
'h01c: key_p2_a <= pressed; // a
'h01b: key_p2_b <= pressed; // s
'h015: key_p2_c <= pressed; // q
'h01d: key_p2_d <= pressed; // d
endcase
end
if(enter & esc) begin
dbg_menu <= ~dbg_menu;
enter <= 0;
esc <= 0;
end
end
////////////////// Her Majesty ///////////////////
reg [31:0] cfg = 0;
wire [15:0] snd_right;
wire [15:0] snd_left;
wire nRESETP, nSYSTEM, CARD_WE, SHADOW, nVEC, nREGEN, nSRAMWEN, PALBNK;
wire CD_nRESET_Z80;
// Clocks
wire CLK_12M, CLK_68KCLK, CLK_68KCLKB, CLK_8M, CLK_6MB, CLK_4M, CLK_4MB, CLK_1HB;
// 68k stuff
wire [15:0] M68K_DATA;
wire [23:1] M68K_ADDR;
wire A22Z, A23Z;
wire M68K_RW, nAS, nLDS, nUDS, nDTACK, nHALT, nBR, nBG, nBGACK;
wire [15:0] M68K_DATA_BYTE_MASK;
wire [15:0] FX68K_DATAIN;
wire [15:0] FX68K_DATAOUT;
wire IPL0, IPL1;
wire FC0, FC1, FC2;
reg [3:0] P_BANK;
// RTC stuff
wire RTC_DOUT, RTC_DIN, RTC_CLK, RTC_STROBE, RTC_TP;
// OEs and WEs
wire nSROMOEL, nSROMOEU, nSROMOE;
wire nROMOEL, nROMOEU;
wire nPORTOEL, nPORTOEU, nPORTWEL, nPORTWEU, nPORTADRS;
wire nSRAMOEL, nSRAMOEU, nSRAMWEL, nSRAMWEU;
wire nWRL, nWRU, nWWL, nWWU;
wire nLSPOE, nLSPWE;
wire nPAL, nPAL_WE;
wire nBITW0, nBITW1, nBITWD0, nDIPRD0, nDIPRD1;
wire nSDROE, nSDPOE;
// RAM outputs
wire [7:0] WRAML_OUT;
wire [7:0] WRAMU_OUT;
wire [15:0] SRAM_OUT;
// Memory card stuff
wire [23:0] CDA;
wire [2:0] BNK;
wire [7:0] CDD;
wire nCD1, nCD2;
wire nCRDO, nCRDW, nCRDC;
wire nCARDWEN, CARDWENB;
// Z80 stuff
wire [7:0] SDD_IN;
wire [7:0] SDD_OUT;
wire [7:0] SDD_RD_C1;
wire [15:0] SDA;
wire nSDRD, nSDWR, nMREQ, nIORQ;
wire nZ80INT, nZ80NMI, nSDW, nSDZ80R, nSDZ80W, nSDZ80CLR;
wire nSDROM, nSDMRD, nSDMWR, SDRD0, SDRD1, nZRAMCS;
wire n2610CS, n2610RD, n2610WR;
// Graphics stuff
wire [23:0] PBUS;
wire [7:0] LO_ROM_DATA;
wire nPBUS_OUT_EN;
wire [19:0] C_LATCH;
reg [3:0] C_LATCH_EXT;
wire [63:0] CR_DOUBLE;
wire [26:0] CROM_ADDR;
wire [1:0] FIX_BANK;
wire [15:0] S_LATCH;
wire [7:0] FIXD;
wire [10:0] FIXMAP_ADDR;
wire CWE, BWE, BOE;
wire [14:0] SLOW_VRAM_ADDR;
reg [15:0] SLOW_VRAM_DATA_IN;
wire [15:0] SLOW_VRAM_DATA_OUT;
wire [10:0] FAST_VRAM_ADDR;
wire [15:0] FAST_VRAM_DATA_IN;
wire [15:0] FAST_VRAM_DATA_OUT;
wire [11:0] PAL_RAM_ADDR;
wire [15:0] PAL_RAM_DATA;
reg [15:0] PAL_RAM_REG;
wire PCK1, PCK2, EVEN1, EVEN2, LOAD, H;
wire DOTA, DOTB;
wire CA4, S1H1, S2H1;
wire CHBL, nBNKB, VCS;
wire CHG, LD1, LD2, SS1, SS2;
wire [3:0] GAD;
wire [3:0] GBD;
wire [3:0] WE;
wire [3:0] CK;
wire CD_VIDEO_EN, CD_FIX_EN, CD_SPR_EN;
// SDRAM multiplexing stuff
wire [15:0] SROM_DATA;
wire [15:0] PROM_DATA;
parameter INDEX_SPROM = 0;
parameter INDEX_LOROM = 1;
parameter INDEX_SFIXROM = 2;
parameter INDEX_P1ROM_A = 4;
parameter INDEX_P1ROM_B = 5;
parameter INDEX_P2ROM = 6;
parameter INDEX_S1ROM = 8;
parameter INDEX_M1ROM = 9;
parameter INDEX_MEMCP = 10;
parameter INDEX_CROM0 = 15;
parameter INDEX_VROMS = 16;
parameter INDEX_CROMS = 64;
wire video_mode = status[3];
wire [3:0] cart_pchip = cfg[22:20];
wire use_pcm = cfg[23];
wire [1:0] cart_chip = cfg[25:24]; // legacy option: 0 - none, 1 - PRO-CT0, 2 - Link MCU
wire [1:0] cmc_chip = cfg[27:26]; // type 1/2
// Memory card and backup ram image save/load
assign sd_rd[0] = bk_rd;
assign sd_wr[0] = bk_wr;
assign sd_lba[0] = bk_lba;
assign sd_buff_din[0] = bk_dout;
wire bk_ack = sd_ack[0];
wire downloading = status[0];
reg bk_rd, bk_wr;
reg bk_ena = 0;
reg bk_pending = 0;
reg [31:0] bk_lba;
wire bk_autosave = status[24];
// Memory write flag for backup memory & memory card
// (~nBWL | ~nBWU) : [AES] Unibios (set to MVS) softdip settings, [MVS] cab settings, dates, timer, high scores, saves, & bookkeeeping
// CARD_WE : [AES/MVS] game saves and high scores
wire bk_change = sram_slot_we | CARD_WE;
wire memcard_change;
reg sram_slot_we;
always @(posedge clk_sys) begin
sram_slot_we <= 0;
if(~nBWL | ~nBWU) begin
sram_slot_we <= (M68K_ADDR[15:1] >= 'h190 && M68K_ADDR[15:1] < 'h4190);
end
end
always @(posedge clk_sys) begin
reg old_downloading = 0;
old_downloading <= downloading;
if(~old_downloading & downloading) bk_ena <= 0;
// Save file always mounted in the end of downloading state.
if(downloading && img_mounted[0] && !img_readonly) bk_ena <= 1;
// Determine whether file needs to be written
if (bk_change) bk_pending <= 1;
else if (bk_state) bk_pending <= 0;
end
wire bk_load = status[21];
wire bk_save = status[12] | (bk_autosave & OSD_STATUS);
reg bk_loading = 0;
reg bk_state = 0;
always @(posedge clk_sys) begin
reg old_downloading = 0;
reg old_load = 0, old_save = 0, old_ack;
old_downloading <= downloading;
old_load <= bk_load;
old_save <= bk_save;
old_ack <= bk_ack;
if(~old_ack & bk_ack) {bk_rd, bk_wr} <= 0;
if(!bk_state) begin
if(bk_ena & ((~old_load & bk_load) | (~old_save & bk_save & bk_pending))) begin
bk_state <= 1;
bk_loading <= bk_load;
bk_lba <= 0;
bk_rd <= bk_load;
bk_wr <= ~bk_load;
end
// always load backup so it will erase the memory if doesn't exist
if(old_downloading & ~downloading & bk_ena) begin
bk_state <= 1;
bk_loading <= 1;
bk_lba <= 0;
bk_rd <= 1;
bk_wr <= 0;
end
end else begin
if(old_ack & ~bk_ack) begin
if (bk_lba >= 'h8F) begin // 64KB + 8KB regardless the selected system
bk_loading <= 0;
bk_state <= 0;
end else begin
bk_lba <= bk_lba + 1'd1;
bk_rd <= bk_loading;
bk_wr <= ~bk_loading;
end
end
end
end
wire [1:0] CD_REGION;
always @(status[11:10])
begin
// CD Region code remap:
// status[11:10] remap
// 00 10 US
// 01 01 EU
// 10 11 JP
// 11 00 ASIA
case(status[11:10])
2'b00: CD_REGION = 2'b10;
2'b01: CD_REGION = 2'b01;
2'b10: CD_REGION = 2'b11;
2'b11: CD_REGION = 2'b00;
endcase
end
wire [2:0] CD_TR_AREA; // Transfer area code
wire [15:0] CD_TR_WR_DATA;
wire [19:1] CD_TR_WR_ADDR;
wire [1:0] CD_BANK_SPR;
wire CD_TR_WR_SPR, CD_TR_WR_PCM, CD_TR_WR_Z80, CD_TR_WR_FIX;
wire CD_BANK_PCM;
wire CD_IRQ;
wire DMA_RUNNING, DMA_WR_OUT, DMA_RD_OUT;
wire [15:0] DMA_DATA_OUT;
wire [23:0] DMA_ADDR_IN;
wire [23:0] DMA_ADDR_OUT;
wire DMA_SDRAM_BUSY;
wire PROM_DATA_READY;
assign sd_wr[1] = 0;
assign sd_buff_din[1] = 0;
cd_sys cdsystem(
.nRESET(nRESET),
.clk_sys(clk_sys), .CLK_68KCLK(CLK_68KCLK),
.M68K_ADDR(M68K_ADDR), .M68K_DATA(M68K_DATA), .A22Z(A22Z), .A23Z(A23Z),
.nLDS(nLDS), .nUDS(nUDS), .M68K_RW(M68K_RW), .nAS(nAS), .nDTACK(nDTACK_ADJ),
.nBR(nBR), .nBG(nBG), .nBGACK(nBGACK),
.SYSTEM_TYPE(SYSTEM_TYPE),
.CD_REGION(CD_REGION),
.CD_LID(status[15]), // CD lid state (DEBUG)
.CD_VIDEO_EN(CD_VIDEO_EN), .CD_FIX_EN(CD_FIX_EN), .CD_SPR_EN(CD_SPR_EN),
.CD_nRESET_Z80(CD_nRESET_Z80),
.CD_TR_WR_SPR(CD_TR_WR_SPR), .CD_TR_WR_PCM(CD_TR_WR_PCM),
.CD_TR_WR_Z80(CD_TR_WR_Z80), .CD_TR_WR_FIX(CD_TR_WR_FIX),
.CD_TR_AREA(CD_TR_AREA),
.CD_BANK_SPR(CD_BANK_SPR), .CD_BANK_PCM(CD_BANK_PCM),
.CD_TR_WR_DATA(CD_TR_WR_DATA), .CD_TR_WR_ADDR(CD_TR_WR_ADDR),
.CD_IRQ(CD_IRQ), .IACK(IACK),
.sd_req_type(sd_req_type),
.sd_rd(sd_rd[1]), .sd_ack(sd_ack[1]), .sd_buff_wr(sd_buff_wr),
.sd_buff_dout(sd_buff_dout), .sd_lba(sd_lba[1]),
.DMA_RUNNING(DMA_RUNNING),
.DMA_DATA_IN(PROM_DATA), .DMA_DATA_OUT(DMA_DATA_OUT),
.DMA_WR_OUT(DMA_WR_OUT), .DMA_RD_OUT(DMA_RD_OUT),
.DMA_ADDR_IN(DMA_ADDR_IN), // Used for reading
.DMA_ADDR_OUT(DMA_ADDR_OUT), // Used for writing
.DMA_SDRAM_BUSY(DMA_SDRAM_BUSY)
);
// The P1 zone is writable on the Neo CD
// Is there a write enable register for it ?
wire CD_EXT_WR = DMA_RUNNING ? (SYSTEM_CDx & (DMA_ADDR_OUT[23:21] == 3'd0) & DMA_WR_OUT) : // DMA writes to $000000~$1FFFFF
(SYSTEM_CDx & ~|{A23Z, A22Z, M68K_ADDR[21]} & ~M68K_RW & ~nAS); // CPU writes to $000000~$1FFFFF
wire CD_WR_SDRAM_SIG = SYSTEM_CDx & |{CD_TR_WR_SPR, CD_TR_WR_FIX, CD_EXT_WR};
wire nROMOE = nROMOEL & nROMOEU;
wire nPORTOE = nPORTOEL & nPORTOEU;
// CD system work ram is in SDRAM
wire CD_EXT_RD = DMA_RUNNING ? (SYSTEM_CDx & (DMA_ADDR_IN[23:21] == 3'd0) & DMA_RD_OUT) : // DMA reads from $000000~$1FFFFF
(SYSTEM_CDx & (~nWRL | ~nWRU)); // CPU reads from $100000~$1FFFFF
wire sdram_ready;
wire [26:1] sdram_addr;
wire [63:0] sdram_dout;
wire [15:0] sdram_din;
// ioctl_download is used to load the system ROM on CD systems, we need it !
wire ioctl_en = SYSTEM_CDx ? (ioctl_index == INDEX_SPROM) :
(ioctl_index != INDEX_LOROM && ioctl_index != INDEX_M1ROM && ioctl_index != INDEX_MEMCP && (ioctl_index < INDEX_VROMS || ioctl_index >= INDEX_CROMS));
wire [26:0] CROM_LOAD_ADDR = ({ioctl_addr[25:0], 1'b0} + {ioctl_index[7:1]-INDEX_CROMS[7:1], 18'h00000, ioctl_index[0], 1'b0});
wire [26:0] VROM_LOAD_ADDR = ({1'b0, ioctl_addr[25:0]} + {ioctl_index[7:0]-INDEX_VROMS[7:0], 19'h00000});
localparam SPROM_OFFSET = 27'h0000000; // System ROM: $0000000~$007FFFF
localparam SFIXROM_OFFSET = 27'h0020000; // SFIX: $0020000~$003FFFF (in sys ROM)
localparam S1ROM_OFFSET = 27'h0080000; // S1: $0080000~$00FFFFF
localparam P1ROM_A_OFFSET = 27'h0200000; // P1+: $0200000~...
localparam P1ROM_B_OFFSET = 27'h0280000; // P1: $0280000~$02FFFFF (secondary 512KB)
localparam P2ROM_OFFSET = 27'h0300000; // P2+: $0300000~...
// FIX ROMs reordering to optimize burst reads
// forward 16,24,0,8...
// inverted 2,6,10,14,...
// In: FEDCBA9876543210
// Out: FEDCBA9876510432
`ifdef MVS_ARCADE_LOAD
wire [18:0] SROM_LOAD_ADDR = {ioctl_addr[18:5],ioctl_addr[2:0],~ioctl_addr[4],ioctl_addr[3]};
`else
wire [18:0] SROM_LOAD_ADDR = ioctl_addr[18:0];
`endif
wire [26:0] ioctl_addr_offset =
(ioctl_index == INDEX_SPROM) ? {SPROM_OFFSET[26:19] , ioctl_addr[18:0]} : // System ROM: $0000000~$007FFFF
(ioctl_index == INDEX_SFIXROM) ? {SFIXROM_OFFSET[26:17], SROM_LOAD_ADDR[16:0]} : // SFIX: $0020000~$003FFFF (in sys ROM)
(ioctl_index == INDEX_S1ROM) ? {S1ROM_OFFSET[26:19] , SROM_LOAD_ADDR} : // S1: $0080000~$00FFFFF
(ioctl_index == INDEX_P1ROM_A) ? P1ROM_A_OFFSET + ioctl_addr : // P1+: $0200000~...
(ioctl_index == INDEX_P1ROM_B) ? {P1ROM_B_OFFSET[26:19], ioctl_addr[18:0]} : // P1: $0280000~$02FFFFF (secondary 512KB)
(ioctl_index == INDEX_P2ROM) ? P2ROM_OFFSET + ioctl_addr : // P2+: $0300000~...
(ioctl_index == INDEX_CROM0) ? {CROM_START, 20'd0} + ioctl_addr : // C: end of P, consolidated CROM
(ioctl_index >= INDEX_CROMS) ? {CROM_START, 20'd0} + CROM_LOAD_ADDR : // C*: end of P
27'h0100000; // undefined case, use work RAM address to make sure no ROM gets overwritten
//wire [26:0] c0rom_load_addr = {ioctl_addr[26:7], ioctl_addr[5:2], ~ioctl_addr[6], ~ioctl_addr[1], 1'b0};
//(ioctl_index == INDEX_CROM0) ? {CROM_START, 20'd0} + c0rom_load_addr : // C: end of P, consolidated CROM
reg [6:0] CROM_START;
reg [26:0] P2ROM_MASK, CROM_MASK, V1ROM_MASK, V2ROM_MASK, MROM_MASK;
always_ff @(posedge clk_sys) begin
reg old_rst;
reg old_download;
old_rst <= status[0];
if(status[0]) begin
P2ROM_MASK <= P2ROM_MASK | P2ROM_MASK[26:1];
CROM_MASK <= CROM_MASK | CROM_MASK[26:1];