forked from somhi/PSX_SoCkit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPSX.sv
1947 lines (1700 loc) · 55.6 KB
/
PSX.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
//============================================================================
// PSX
// Copyright (C) 2019 Robert Peip
//
// Port to MiSTer
// Copyright (C) 2019 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 [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
output VGA_DISABLE, // analog out is off
output resSwitchBlackout,
input [11:0] HDMI_WIDTH,
input [11:0] HDMI_HEIGHT,
output HDMI_FREEZE,
`ifdef MISTER_FB
// Use framebuffer in DDRAM
// 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 HDMI_FREEZE = 1'b0;
assign ADC_BUS = 'Z;
assign {UART_RTS, UART_TXD, UART_DTR} = 0;
assign AUDIO_S = 1;
assign AUDIO_MIX = status[8:7];
assign LED_USER = exe_download | bk_pending;
assign LED_DISK = 0;
assign LED_POWER = 0;
assign BUTTONS = 0;
assign VGA_SCALER= 0;
assign {SD_SCK, SD_MOSI, SD_CS} = 'Z;
wire [ 3:0] frameindex;
wire [11:0] DisplayWidth;
wire [11:0] DisplayHeight;
wire [ 9:0] DisplayOffsetX;
wire [ 8:0] DisplayOffsetY;
assign FB_BASE = status[11] ? 32'h30000000 : {8'h30, frameindex, DisplayOffsetY, DisplayOffsetX, 1'b0};
assign FB_EN = (status[14] || video_fbmode);
assign FB_FORMAT = (status[10] || video_fb24) ? 5'b00101 : 5'b01100;
assign FB_WIDTH = status[11] ? 12'd1024 : DisplayWidth;
assign FB_HEIGHT = status[11] ? 12'd512 : DisplayHeight;
assign FB_STRIDE = 14'd2048;
assign FB_FORCE_BLANK = 0;
/////////////////////// CLOCK/RESET ///////////////////////////////////
wire pll_locked;
wire clk_1x;
wire clk_2x;
wire clk_3x;
wire clk_vid;
pll pll
(
.refclk(CLK_50M),
.rst(0),
.outclk_0(clk_1x),
.outclk_1(clk_2x),
.outclk_2(clk_3x),
.locked(pll_locked)
);
pll2 pll2
(
.refclk(CLK_50M),
.rst(0),
.outclk_0(clk_vid),
.reconfig_to_pll(reconfig_to_pll),
.reconfig_from_pll(reconfig_from_pll)
);
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)
);
wire FFrequest = joy[17] && ~FB_LL && ~DIRECT_VIDEO;
wire syncVideoOut = 0; //status[57] && ~FB_LL && ~DIRECT_VIDEO;
wire syncVideoClock = 0; //status[56] && ~FB_LL && ~DIRECT_VIDEO;
always @(posedge CLK_50M) begin : cfg_block
reg pald = 0, pald2 = 0;
reg pdbg = 0, pdbg2 = 0;
reg pffw = 0, pffw2 = 0;
reg [3:0] state = 0;
pald <= isPal;
pald2 <= pald;
pdbg <= syncVideoClock;
pdbg2 <= pdbg;
pffw <= fast_forward;
pffw2 <= pffw;
cfg_write <= 0;
if(pald2 != pald || pdbg2 != pdbg || pffw2 != pffw) state <= 1;
if(!cfg_waitrequest) begin
if(state) state<=state+1'd1;
case(state)
1: begin
cfg_address <= 0;
cfg_data <= 0;
cfg_write <= 1;
end
3: begin
cfg_address <= 5;
cfg_data <= pffw2 ? 131842 : pdbg2 ? 771 : 1028;
cfg_write <= 1;
end
5: begin
cfg_address <= 7;
cfg_data <= pffw2 ? 2147483648 : pdbg2 ? 551954751 : pald2 ? 2201376898 : 2537930535;
cfg_write <= 1;
end
7: begin
cfg_address <= 2;
cfg_data <= 0;
cfg_write <= 1;
end
endcase
end
end
reg fast_forward;
reg ff_latch;
always @(posedge clk_1x) begin : ffwd
reg last_ffw;
reg ff_was_held;
longint ff_count;
last_ffw <= FFrequest;
if (FFrequest)
ff_count <= ff_count + 1;
if (~last_ffw & FFrequest) begin
ff_latch <= 0;
ff_count <= 0;
end
if ((last_ffw & ~FFrequest)) begin
ff_was_held <= 0;
if (ff_count < 10000000 && ~ff_was_held) begin
ff_was_held <= 1;
ff_latch <= 1;
end
end
fast_forward <= (FFrequest | ff_latch);
end
wire reset_or = RESET | buttons[1] | status[0] | bios_download | exe_download | cdDownloadReset;
//////////////////////////// HPS I/O //////////////////////////////////
// Status Bit Map: (0..31 => "O", 32..63 => "o")
// 0 1 2 3 4 5 6 7 8 9
// 01234567890123456789012345678901 23456789012345678901234567890123 45678901234567890123456789012345
// 0123456789ABCDEFGHIJKLMNOPQRSTUV 0123456789ABCDEFGHIJKLMNOPQRSTUV
// XXXX XXXXXX XXXXXX XXXXX XX XX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XX XXXXXXXXXXXXXXXXXXXXXXX
`include "build_id.v"
parameter CONF_STR = {
"PSX;SS3E000000:400000;",
"H7S1,CUECHD,Load CD;",
"h7-,Reload core for CD;",
"F1,EXE,Load Exe;",
"-;",
"d6C,Cheats;",
"h6O[6],Cheats Enabled,Yes,No;",
"-;",
"hA-,Memcard Status: not saved;",
"HB-,Memcard Status: saved;",
"hC-,Memcard Status: saving...;",
"RD,Save Memory Cards;",
"O[71],Save to SDCard,On Open OSD,Manual;",
"SC2,SAVMCD,Mount Memory Card 1;",
"SC3,SAVMCD,Mount Memory Card 2;",
"O[63],Automount Memory Card 1,Yes,No;",
"-;",
"O[36],Savestates to SDCard,On,Off;",
"O[68],Autoincrement Slot,Off,On;",
"O[38:37],Savestate Slot,1,2,3,4;",
"RH,Save state (Alt-F1);",
"RI,Restore state (F1);",
"-;",
"O[40:39],System Type,Auto,NTSC-U,NTSC-J,PAL;",
"-;",
"D8O[48:45],Pad1,Dualshock,Off,Digital,Analog,GunCon,NeGcon,Wheel-NegCon,Wheel-Analog,Mouse,Justifier,SNAC-port1,Analog Joystick;",
"D8O[52:49],Pad2,Dualshock,Off,Digital,Analog,GunCon,NeGcon,Wheel-NegCon,Wheel-Analog,Mouse,Justifier,SNAC-port2,Analog Joystick;",
"D8h2O[9],Show Crosshair,Off,On;",
"D8h4O[31],DS Mode,L3+R3+Up/Dn | Click,L1+L2+R1+R2+Up/Dn;",
"O[57:56],Multitap,Off,Port1: 4 x Digital,Port1: 4 x Analog;",
"-;",
"P1,Video & Audio;",
"P1-;",
"P1O[33:32],Aspect ratio,Original,Full Screen,[ARC1],[ARC2];",
"P1O[35:34],Scale,Normal,V-Integer,Narrower HV-Integer,Wider HV-Integer;",
"P1-;",
"DEP1O[62],Fixed HBlank,On,Off;",
"DEP1O[55],Fixed VBlank,Off,On;",
"d5P1O[4:3],Vertical Crop,Off,On(224/270),On(216/256);",
"P1O[67],Horizontal Crop,Off,On;",
"P1O[61],Black Transitions,On,Off;",
"P1O[41],Deinterlacing,Weave,Bob;",
"P1O[60],Sync 480i for HDMI,Off,On;",
"P1O[24],Rotate,Off,On;",
"P1-;",
"P1O[22],Dithering,On,Off;",
"DEP1O[84],Render 24 Bit,Off,On;",
"P1O[73],Dither 24 Bit for VGA,Off,On;",
"P1-;",
"P1O[89],480i to 480p Hack,Off,On;",
"P1O[54:53],Widescreen Hack,Off,3:2,5:3,16:9;",
"P1O[82:81],Texture Filter,Off,All Polygon,Dithered,Dith+Shaded;",
"hDP1O[87:86],Filter Strength,25%,50%,75%,100%;",
"hDP1O[83],Filter 2D Detect,Off,On;",
"P1-;",
"d1P1O[44],SPU RAM select,DDR3,SDRAM2;",
"P1O[8:7],Stereo Mix,None,25%,50%,100%;",
"P2,Miscellaneous;",
"P2-;",
"P2O[16],Fastboot,Off,On;",
"P2O[42],CD Lid,Closed,Open;",
"P2O[64],Pause when OSD open,On,Off;",
"P2-;",
"P2-,(U) = unsafe -> can crash;",
"P2O[80:79],Turbo(Cheats Off),Off,Low(U),Medium(U),High(U);",
"P2O[72],Pause when CD slow,On,Off(U);",
"P2O[15],PAL 60Hz Hack,Off,On(U);",
"P2O[21],CD Fast Seek,Off,On(U);",
"P2O[77:75],CD Speed,Original,Forced 1X(U),Forced 2X(U),Hack 4X(U),Hack 6X(U),Hack 8X(U);",
"P2O[78],Limit Max CD Speed,Off,On(U);",
"P2O[85],RAM(Homebrew),2 MByte,8 MByte(U);",
"P2-;",
"P2O[28],FPS Overlay,Off,On;",
"P2O[74],Error Overlay,Off,On;",
"P2O[59],CD Slow Overlay,Off,On;",
"h9P2O[70],CD Overlay,Read,Read+Seek;",
"h3-;",
"h3P3,Debug;",
"h3P3-;",
"h3P3O[14],DDR3 Framebuffer,Off,On;",
"h3P3O[10],DDR3 FB Color,16,24;",
"h3P3O[11],VRAMViewer,Off,On;",
"h3P3O[30],Sound,On,Off;",
"h3P3O[43],RepTimingSPUDMA,Off,On;",
"h3P3O[27],Textures,On,Off;",
"h3P3O[69],LBA Overlay,Off,On;",
"h3P3O[88],Fast CD DMA Timing,Off,On;",
"h3P3T1,Advance Pause;",
"h3P3T2,Sound IRQ Trigger;",
"- ;",
"R0,Reset;",
"J1,Triangle(NeGcon B),O(Gun Fire|NeGcon A),X(Gun B|NeGcon I),[](NeGcon II),Select,Start(Gun A),L1,R1,L2,R2,L3,R3,Savestates,Fastforward,Pause(Core),Toggle Dualshock;",
"jn,X,A,B,Y,Select,Start,L,R;",
"I,",
"Load=DPAD Up|Save=Down|Slot=L+R,",
"Active Slot 1,",
"Active Slot 2,",
"Active Slot 3,",
"Active Slot 4,",
"Save to state 1,",
"Restore state 1,",
"Save to state 2,",
"Restore state 2,",
"Save to state 3,",
"Restore state 3,",
"Save to state 4,",
"Restore state 4,",
"Rewinding...,",
"Slot 1 Analog,",
"Slot 1 Digital,",
"Slot 2 Analog,",
"Slot 2 Digital,",
"Region Unknown->US,",
"Region JP,",
"Region US,",
"Region EU,",
"Saving Memcard,",
"Unsafe option used!;",
"V,v",`BUILD_DATE
};
reg dbg_enabled = 0;
wire [1:0] buttons;
wire [127:0] status;
wire [15:0] status_menumask = {hack_480p, filter_on, saving_memcard, (bk_pending | saving_memcard), bk_pending, status[59], multitap, biosMod, ~TURBO_MEM, (status[55] && ~hack_480p), (PadPortDS1 | PadPortDS2), dbg_enabled, (PadPortGunCon1 | PadPortGunCon2 | PadPortJustif1 | PadPortJustif2), SDRAM2_EN, 1'b0};
wire forced_scandoubler;
reg [31:0] sd_lba0 = 0;
reg [31:0] sd_lba1;
reg [ 6:0] sd_lba2;
reg [ 6:0] sd_lba3;
reg [3:0] sd_rd;
reg [3:0] sd_wr;
wire [3:0] sd_ack;
wire [8:0] sd_buff_addr;
wire [15:0] sd_buff_dout;
wire [15:0] sd_buff_din2;
wire [15:0] sd_buff_din3;
wire sd_buff_wr;
wire [3:0] img_mounted;
wire img_readonly;
wire [63:0] img_size;
wire ioctl_download;
wire [26:0] ioctl_addr;
wire [15:0] ioctl_dout;
wire ioctl_wr;
wire [7:0] ioctl_index;
reg ioctl_wait = 0;
wire [19:0] joy;
wire [19:0] joy_unmod;
wire [19:0] joy2;
wire [19:0] joy3;
wire [19:0] joy4;
wire [10:0] ps2_key;
wire [21:0] gamma_bus;
wire [15:0] sdram_sz;
wire [15:0] joystick_analog_l0;
wire [15:0] joystick_analog_r0;
wire [15:0] joystick_analog_l1;
wire [15:0] joystick_analog_r1;
wire [15:0] joystick_analog_l2;
wire [15:0] joystick_analog_r2;
wire [15:0] joystick_analog_l3;
wire [15:0] joystick_analog_r3;
wire [24:0] mouse;
wire [15:0] joystick1_rumble;
wire [15:0] joystick2_rumble;
wire [15:0] joystick3_rumble;
wire [15:0] joystick4_rumble;
wire [32:0] RTC_time;
wire filter_on = (status[82:81] == 2'b00) ? 1'b0 : 1'b1;
assign resSwitchBlackout = ~status[61];
wire [127:0] status_in = {status[127:39],ss_slot,status[36:19], 2'b00, status[16:0]};
wire bk_pending;
wire DIRECT_VIDEO;
hps_io #(.CONF_STR(CONF_STR), .WIDE(1), .VDNUM(4), .BLKSZ(3)) hps_io
(
.clk_sys(clk_1x),
.HPS_BUS(HPS_BUS),
.EXT_BUS(EXT_BUS),
.buttons(buttons),
.forced_scandoubler(forced_scandoubler),
.joystick_0(joy_unmod),
.joystick_1(joy2),
.joystick_2(joy3),
.joystick_3(joy4),
.ps2_key(ps2_key),
.status(status),
.status_in(status_in),
.status_set(statusUpdate),
.status_menumask(status_menumask),
.info_req(psx_info_req),
.info(psx_info),
.ioctl_addr(ioctl_addr),
.ioctl_dout(ioctl_dout),
.ioctl_wr(ioctl_wr),
.ioctl_download(ioctl_download),
.ioctl_index(ioctl_index),
.ioctl_wait(ioctl_wait),
.sd_lba('{sd_lba0, sd_lba1, sd_lba2, sd_lba3}),
.sd_blk_cnt('{0,0, 0, 0}),
.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('{0, 0, sd_buff_din2, sd_buff_din3}),
.sd_buff_wr(sd_buff_wr),
.TIMESTAMP(RTC_time),
.img_mounted(img_mounted),
.img_readonly(img_readonly),
.img_size(img_size),
.sdram_sz(sdram_sz),
.gamma_bus(gamma_bus),
.joystick_l_analog_0(joystick_analog_l0),
.joystick_r_analog_0(joystick_analog_r0),
.joystick_l_analog_1(joystick_analog_l1),
.joystick_r_analog_1(joystick_analog_r1),
.joystick_l_analog_2(joystick_analog_l2),
.joystick_r_analog_2(joystick_analog_r2),
.joystick_l_analog_3(joystick_analog_l3),
.joystick_r_analog_3(joystick_analog_r3),
.ps2_mouse(mouse),
.joystick_0_rumble(paused ? 16'h0000 : joystick1_rumble),
.joystick_1_rumble(paused ? 16'h0000 : joystick2_rumble),
.direct_video(DIRECT_VIDEO)
);
assign joy = joy_unmod[16] ? 20'b0 : joy_unmod;
assign sd_rd[0] = 0;
assign sd_wr[0] = 0;
assign sd_wr[1] = 0;
wire [35:0] EXT_BUS;
wire heartbeat;
hps_ext hps_ext
(
.clk_sys(clk_1x),
.EXT_BUS(EXT_BUS),
.heartbeat(heartbeat)
);
////////////////////////// ROM DETECT /////////////////////////////////
reg bios_download, exe_download, cdinfo_download, code_download;
always @(posedge clk_1x) begin
bios_download <= ioctl_download & (ioctl_index[5:0] == 0);
exe_download <= ioctl_download & (ioctl_index == 1);
cdinfo_download <= ioctl_download & (ioctl_index == 251);
code_download <= ioctl_download & (ioctl_index == 255);
end
reg cart_loaded = 0;
always @(posedge clk_1x) begin
if (exe_download || img_mounted[1]) begin
cart_loaded <= 1;
end
end
localparam EXE_START = 16777216;
localparam BIOS_START = 8388608;
reg [26:0] ramdownload_wraddr;
reg [31:0] ramdownload_wrdata;
reg ramdownload_wr;
reg hasCD = 0;
reg exe_download_1 = 0;
reg cdinfo_download_1 = 0;
reg loadExe = 0;
reg sd_mounted2 = 0;
reg sd_mounted3 = 0;
reg memcard1_load = 0;
reg memcard2_load = 0;
reg memcard_save = 0;
wire saving_memcard;
reg memcard1_inserted = 0;
reg memcard2_inserted = 0;
reg [25:0] memcard1_cnt = 0;
reg [25:0] memcard2_cnt = 0;
wire bk_save = status[13];
wire bk_autosave = ~status[71];
reg old_save = 0;
reg old_save_a = 0;
wire bk_save_a = OSD_STATUS & bk_autosave;
reg cdbios = 0;
reg [1:0] region;
reg [1:0] biosregion;
wire [1:0] region_out;
reg isPal;
reg biosMod = 0;
reg [31:0] exe_initial_pc;
reg [31:0] exe_initial_gp;
reg [31:0] exe_load_address;
reg [31:0] exe_file_size;
reg [31:0] exe_stackpointer;
always @(posedge clk_1x) begin
ramdownload_wr <= 0;
if(exe_download | bios_download | cdinfo_download) begin
if (ioctl_wr) begin
if(~ioctl_addr[1]) begin
ramdownload_wrdata[15:0] <= ioctl_dout;
if (bios_download) ramdownload_wraddr <= {4'd1, 2'b00, ioctl_index[7:6], ioctl_addr[18:0]};
else if (exe_download) ramdownload_wraddr <= ioctl_addr[22:0] + EXE_START[26:0];
else if (cdinfo_download) ramdownload_wraddr <= ioctl_addr[26:0];
end else begin
ramdownload_wrdata[31:16] <= ioctl_dout;
ramdownload_wr <= 1;
ioctl_wait <= 1;
if (cdinfo_download) ioctl_wait <= 0;
end
end
if(sdramCh3_done) ioctl_wait <= 0;
end else begin
ioctl_wait <= 0;
end
exe_download_1 <= exe_download;
loadExe <= exe_download_1 & ~exe_download;
if (exe_download & ramdownload_wr) begin
if (ramdownload_wraddr[22:0] == 'h10) exe_initial_pc <= ramdownload_wrdata;
if (ramdownload_wraddr[22:0] == 'h14) exe_initial_gp <= ramdownload_wrdata;
if (ramdownload_wraddr[22:0] == 'h18) exe_load_address <= ramdownload_wrdata;
if (ramdownload_wraddr[22:0] == 'h1C) exe_file_size <= ramdownload_wrdata;
if (ramdownload_wraddr[22:0] == 'h30) exe_stackpointer <= ramdownload_wrdata;
if (ramdownload_wraddr[22:0] == 'h34) exe_stackpointer <= exe_stackpointer + ramdownload_wrdata;
end
if (loadExe) biosMod <= 1'b1;
if (img_mounted[1]) begin
if (img_size > 0) begin
hasCD <= 1;
end else begin
hasCD <= 0;
end
end
case(status[40:39])
0: begin
case(region_out)
0: begin region = 2'b00; isPal <= 0; end // unknown => default to NTSC
1: begin region = 2'b01; isPal <= 0; end // JP
2: begin region = 2'b00; isPal <= 0; end // US
3: begin region = 2'b10; isPal <= 1; end // EU
endcase
end
1: begin region = 2'b00; isPal <= 0; end
2: begin region = 2'b01; isPal <= 0; end
3: begin region = 2'b10; isPal <= 1; end
endcase
if (bios_download && ioctl_index[7:6] == 2'b11) cdbios <= 1'b1;
if (cdbios)
biosregion <= 2'b11;
else
biosregion <= region;
memcard1_load <= 0;
memcard2_load <= 0;
memcard_save <= 0;
// memcard 1
if (img_mounted[2]) begin
memcard1_inserted <= 0;
memcard1_cnt <= 26'd0;
if (img_size > 0) begin
sd_mounted2 <= 1;
memcard1_load <= 1;
end else begin
sd_mounted2 <= 0;
end
end
if (sd_mounted2) begin // delay memcard inserted for ~2 seconds on card change
if (memcard1_cnt[25]) begin
memcard1_inserted <= 1;
end else begin
memcard1_cnt <= memcard1_cnt + 1'd1;
end
end
// memcard 2
if (img_mounted[3]) begin
memcard2_inserted <= 0;
memcard2_cnt <= 26'd0;
if (img_size > 0) begin
sd_mounted3 <= 1;
memcard2_load <= 1;
end else begin
sd_mounted3 <= 0;
end
end
if (sd_mounted3) begin
if (memcard2_cnt[25]) begin
memcard2_inserted <= 1;
end else begin
memcard2_cnt <= memcard2_cnt + 1'd1;
end
end
old_save <= bk_save;
old_save_a <= bk_save_a;
if ((~old_save & bk_save) | (~old_save_a & bk_save_a)) memcard_save <= 1;
end
/////////////////////////// SAVESTATE /////////////////////////////////
wire [1:0] ss_slot;
wire [7:0] ss_info;
wire ss_save, ss_load, ss_info_req;
wire statusUpdate;
savestate_ui savestate_ui
(
.clk (clk_1x ),
.ps2_key (ps2_key[10:0] ),
.allow_ss (cart_loaded ),
.joySS (joy_unmod[16] ),
.joyRight (joy_unmod[0] ),
.joyLeft (joy_unmod[1] ),
.joyDown (joy_unmod[2] ),
.joyUp (joy_unmod[3] ),
.joyRewind (0 ),
.rewindEnable (0 ),
.status_slot (status[38:37] ),
.autoincslot (status[68] ),
.OSD_saveload (status[18:17] ),
.ss_save (ss_save ),
.ss_load (ss_load ),
.ss_info_req (ss_info_req ),
.ss_info (ss_info ),
.statusUpdate (statusUpdate ),
.selected_slot (ss_slot )
);
defparam savestate_ui.INFO_TIMEOUT_BITS = 25;
//////////////////////////// PAD ///////////////////////////////////
// 0000 -> DualShock
// 0001 -> off
// 0010 -> digital
// 0011 -> analog
// 0100 -> Namco GunCon lightgun
// 0101 -> Namco NeGcon
// 0110 -> Wheel Negcon
// 0111 -> Wheel Analog
// 1000 -> mouse
// 1001 -> Konami Justifier lightgun
// 1010 -> SNAC
// 1011 -> Analog Joystick
// 1100..1111 -> reserved
wire PadPortDS1 = (status[48:45] == 4'b0000);
wire PadPortEnable1 = (status[48:45] != 4'b0001);
wire PadPortDigital1 = (status[48:45] == 4'b0010);
wire PadPortAnalog1 = (status[48:45] == 4'b0011) || (status[48:45] == 4'b0111);
wire PadPortGunCon1 = (status[48:45] == 4'b0100);
wire PadPortNeGcon1 = (status[48:45] == 4'b0101) || (status[48:45] == 4'b0110);
wire PadPortWheel1 = (status[48:45] == 4'b0110) || (status[48:45] == 4'b0111);
wire PadPortMouse1 = (status[48:45] == 4'b1000);
wire PadPortJustif1 = (status[48:45] == 4'b1001);
wire snacPort1 = (status[48:45] == 4'b1010) && ~multitap;
wire PadPortStick1 = (status[48:45] == 4'b1011);
wire PadPortDS2 = (status[52:49] == 4'b0000);
wire PadPortEnable2 = (status[52:49] != 4'b0001) && ~multitap;
wire PadPortDigital2 = (status[52:49] == 4'b0010);
wire PadPortAnalog2 = (status[52:49] == 4'b0011) || (status[52:49] == 4'b0111);
wire PadPortGunCon2 = (status[52:49] == 4'b0100);
wire PadPortNeGcon2 = (status[52:49] == 4'b0101) || (status[52:49] == 4'b0110);
wire PadPortWheel2 = (status[52:49] == 4'b0110) || (status[52:49] == 4'b0111);
wire PadPortMouse2 = (status[52:49] == 4'b1000);
wire PadPortJustif2 = (status[52:49] == 4'b1001);
wire snacPort2 = (status[52:49] == 4'b1010) && ~multitap;
wire PadPortStick2 = (status[52:49] == 4'b1011);
// 00 -> multitap off
// 01 -> port1, 4 x digital
// 10 -> port1, 4 x analog
wire multitap = (status[57:56] != 2'b00);
wire multitapDigital = (status[57:56] == 2'b01);
wire multitapAnalog = (status[57:56] == 2'b10);
wire [1:0] padMode;
reg [1:0] padMode_1;
reg [7:0] psx_info;
reg psx_info_req;
wire resetFromCD;
reg cdDownloadReset = 0;
reg [3:0] ToggleDS = 0;
reg [3:0] joy19_1 = 0;
always @(posedge clk_1x) begin
psx_info_req <= 0;
padMode_1 <= padMode;
cdinfo_download_1 <= cdinfo_download;
if (ss_info_req) begin
psx_info_req <= 1;
psx_info <= ss_info;
end else if (saving_memcard) begin
psx_info_req <= 1;
psx_info <= 8'd23;
end else if (padMode_1[0] != padMode[0] && ~multitap) begin
psx_info_req <= 1;
if (padMode[0]) psx_info <= 8'd15;
if (!padMode[0]) psx_info <= 8'd16;
end else if (padMode_1[1] != padMode[1] && ~multitap) begin
psx_info_req <= 1;
if (padMode[1]) psx_info <= 8'd17;
if (!padMode[1]) psx_info <= 8'd18;
end else if (cdinfo_download_1 && ~cdinfo_download) begin
// warning for every unsafe option
if (status[89] || status[80:79] > 0 || status[72] || status[15] || status[21] || status[77:75] > 0 || status[78] || status[85]) begin
psx_info_req <= 1;
psx_info <= 8'd24;
end else if (status[40:39] == 2'b00) begin
psx_info_req <= 1;
case(region_out)
0: begin psx_info <= 8'd19; end // unknown => default to NTSC
1: begin psx_info <= 8'd20; end // JP
2: begin psx_info <= 8'd21; end // US
3: begin psx_info <= 8'd22; end // EU
endcase
end
end
cdDownloadReset <= 0;
if (cdinfo_download_1 && ~cdinfo_download && resetFromCD) begin
cdDownloadReset <= 1;
end
if (joy[14] && joy[15] && joy[8]) dbg_enabled <= 1; // L3+R3+Select
// DS toggle
joy19_1 <= {joy4[19] ,joy3[19] ,joy2[19] ,joy[19] };
ToggleDS[0] <= joy[19] & ~joy19_1[0];
ToggleDS[1] <= joy2[19] & ~joy19_1[1];
ToggleDS[2] <= joy3[19] & ~joy19_1[2];
ToggleDS[3] <= joy4[19] & ~joy19_1[3];
end
//////////////////////////// PAUSE and RESET ///////////////////////////
reg paused = 0;
reg [9:0] unpause = 0;
reg status1_1;
wire isPaused;
reg [20:0] aliveCnt = 0;
reg heartbeat_1 = 0;
reg hps_busy = 0;
reg reset = 0;
reg buttonpause_1 = 0;
reg button_paused = 0;
reg TURBO_MEM;
reg TURBO_COMP;
reg TURBO_CACHE;
reg TURBO_CACHE50;
always @(posedge clk_1x) begin
paused <= 0;
// pause from OSD open
if (~status[64] & OSD_STATUS & (unpause == 0)) begin
paused <= 1;
end
// pause from button
buttonpause_1 <= joy[18];
if (joy[18] & ~buttonpause_1) begin
button_paused <= ~button_paused;
end
if (button_paused) begin
paused <= 1;
end
// Advance Pause OSD trigger
status1_1 <= status[1];
if (status[1] & ~status1_1) begin
unpause <= 1023;
end else if (unpause > 0) begin
unpause <= unpause - 1'd1;
end
// pause from heartbeat -> only used for savestate
hps_busy <= 0;
heartbeat_1 <= heartbeat;
if (heartbeat == heartbeat_1) begin
if (aliveCnt[20] == 0) begin
aliveCnt <= aliveCnt + 1'b1;
end else begin
hps_busy <= 1;
end
end else begin
aliveCnt <= 0;
end