-
Notifications
You must be signed in to change notification settings - Fork 4
/
MegaSys1_A.sv
2817 lines (2428 loc) · 87.1 KB
/
MegaSys1_A.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
//============================================================================
//
// 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.
//
//============================================================================
`default_nettype none
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 [2:0] VGA_SL,
output VGA_SCALER, // Force VGA scaler
output VGA_DISABLE, // Analog out is off
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,
//Audio
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,
`ifdef MISTER_ENABLE_YC
output [39:0] CHROMA_PHASE_INC,
output YC_EN,
output PALFLAG,
`endif
// 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
);
///////// Default values for ports not used in this core /////////
assign ADC_BUS = 'Z;
assign USER_OUT = 0;
assign {UART_RTS, UART_TXD, UART_DTR} = 0;
assign {SD_SCK, SD_MOSI, SD_CS} = 'Z;
//assign {SDRAM_DQ, SDRAM_A, SDRAM_BA, SDRAM_CLK, SDRAM_CKE, SDRAM_DQML, SDRAM_DQMH, SDRAM_nWE, SDRAM_nCAS, SDRAM_nRAS, SDRAM_nCS} = 'Z;
//assign {DDRAM_CLK, DDRAM_BURSTCNT, DDRAM_ADDR, DDRAM_DIN, DDRAM_BE, DDRAM_RD, DDRAM_WE} = '0;
assign VGA_F1 = 0;
assign VGA_SCALER = 0;
assign HDMI_FREEZE = 0;
assign AUDIO_MIX = 0;
assign LED_USER = |{ m68kp_a[0], m68ks_a[0], max_count, scroll0_x, scroll0_y, oki0_sample_clk, oki1_sample_clk};
assign LED_DISK = 0;
assign LED_POWER = 0;
assign BUTTONS = 0;
//reg [7:0] max_count;
//reg [31:0] req_count;
//reg [31:0] wait_count;
//reg [31:0] max_wait;
//reg prev_cs;
// Status Bit Map:
// Upper Case Lower Case
// 0 1 2 3 4 5 6
// 01234567890123456789012345678901 23456789012345678901234567890123
// 0123456789ABCDEFGHIJKLMNOPQRSTUV 0123456789ABCDEFGHIJKLMNOPQRSTUV
// X XXXXXXXXXXX X X XXXXXXXXX XXXXXXXXXXXXXXXXXX XXXXXXXX
wire [1:0] aspect_ratio = status[9:8];
wire orientation = ~status[3];
wire [2:0] scan_lines = status[6:4];
reg refresh_mod;
reg new_vmode;
always @(posedge clk_sys) begin
if (refresh_mod != status[19]) begin
refresh_mod <= status[19];
new_vmode <= ~new_vmode;
end
end
wire [3:0] hs_offset = status[27:24];
wire [3:0] vs_offset = status[31:28];
wire [3:0] hs_width = status[59:56];
wire [3:0] vs_width = status[63:60];
assign VIDEO_ARX = (!aspect_ratio) ? (orientation ? 8'd8 : 8'd7) : (aspect_ratio - 1'd1);
assign VIDEO_ARY = (!aspect_ratio) ? (orientation ? 8'd7 : 8'd8) : 12'd0;
`include "build_id.v"
localparam CONF_STR = {
"MegaSys1_A;;",
"-;",
"P1,Video Settings;",
"P1-;",
"P1O89,Aspect Ratio,Original,Full Screen,[ARC1],[ARC2];",
"P1O3,Orientation,Horz,Vert;",
"P1-;",
"P1O46,Scandoubler Fx,None,HQ2x,CRT 25%,CRT 50%,CRT 75%,CRT 100%;",
"P1OA,Force Scandoubler,Off,On;",
"P1-;",
"P1O7,Video Mode,NTSC,PAL;",
"P1OM,Video Signal,RGBS/YPbPr,Y/C;",
"P1OJ,Refresh Rate,Native,NTSC;",
"P1-;",
"P1OOR,H-sync Pos Adj,0,1,2,3,4,5,6,7,-8,-7,-6,-5,-4,-3,-2,-1;",
"P1OSV,V-sync Pos Adj,0,1,2,3,4,5,6,7,-8,-7,-6,-5,-4,-3,-2,-1;",
"P1-;",
"P1oOR,H-sync Width Adj,0,1,2,3,4,5,6,7,-8,-7,-6,-5,-4,-3,-2,-1;",
"P1oSV,V-sync Width Adj,0,1,2,3,4,5,6,7,-8,-7,-6,-5,-4,-3,-2,-1;",
"P1-;",
"P2,Audio Settings;",
"P2-;",
"P2OC,Audio Mix,Mono,Stereo;",
"P2-;",
//"P2OB,OPM/ADPCM Audio,On,Off;",
//"P2-;",
"P2oBC,ADPCM-0 Volume,Default,50%,75%,Off;",
"P2oDE,ADPCM-1 Volume,Default,50%,75%,Off;",
"P2oFG,OPM Volume,Default,50%,25%,Off;",
"P2-;",
"-;",
"P3,Core Options;",
"P3-;",
"P3oH,Swap P1/P2 Joystick,Off,On;",
"P3-;",
"P3OF,68k Freq.,6Mhz,7.2MHz;",
"P3-;",
"P3-;",
"P3-;",
"P3o0,P1 Reserve 1,Off,On;",
"P3o1,P1 Reserve 2,On,Off;",
"P3o2,P1 Reserve 3,Off,On;",
"P3o3,P1 Reserve 4,Off,On;",
"P3-;",
"P3o4,P2 Reserve 1,On,Off;",
"P3o5,P2 Reserve 2,On,Off;",
"P3o6,P2 Reserve 3,Off,On;",
"P3o7,P2 Reserve 4,On,Off;",
"P3-;",
"P3o8,Unknown,Off,On;",
"P3o9,Unknown,Off,On;",
"P3oA,Unknown,Off,On;",
"P3-;",
"DIP;",
"-;",
"OK,Pause OSD,Off,When Open;",
"OL,Dim Video,Off,10s;",
"-;",
"R0,Reset;",
"V,v",`BUILD_DATE
};
wire hps_forced_scandoubler;
wire forced_scandoubler = hps_forced_scandoubler | status[10];
wire [1:0] buttons;
wire [63:0] status;
wire [10:0] ps2_key;
wire [15:0] joy0, joy1;
hps_io #(.CONF_STR(CONF_STR)) hps_io
(
.clk_sys(clk_sys),
.HPS_BUS(HPS_BUS),
.buttons(buttons),
.ps2_key(ps2_key),
.status(status),
.status_menumask(direct_video),
.forced_scandoubler(hps_forced_scandoubler),
.gamma_bus(gamma_bus),
.new_vmode(new_vmode),
.direct_video(direct_video),
.video_rotated(video_rotated),
.ioctl_download(ioctl_download),
.ioctl_upload(ioctl_upload),
.ioctl_wr(ioctl_wr),
.ioctl_addr(ioctl_addr),
.ioctl_dout(ioctl_dout),
.ioctl_din(ioctl_din),
.ioctl_index(ioctl_index),
.ioctl_wait(ioctl_wait),
.joystick_0(joy0),
.joystick_1(joy1)
);
// INPUT
// 8 dip switches of 8 bits
reg [7:0] sw[8];
always @(posedge clk_sys) begin
if (ioctl_wr && (ioctl_index==254) && !ioctl_addr[24:3]) begin
sw[ioctl_addr[2:0]] <= ioctl_dout;
end
end
localparam P47 = 0;
localparam PHANTASM = 2;
localparam RODLAND = 3;
localparam RODLANDJ = 4;
localparam SHINGEN = 5;
localparam ASTYANAX = 6;
localparam SOLDAM = 7;
localparam SOLDAMJ = 8;
localparam EDFP = 9;
localparam INYOURF = 10;
localparam STDRAGON = 11;
localparam STDRAGONA = 12;
localparam HACHOO = 13;
localparam PLUSALPH = 14;
localparam IGANINJU = 15;
localparam JITSUPRO = 16;
reg [23:0] prom [0:15];
reg [7:0] irq1_scanline;
always @(posedge clk_sys) begin
if (ioctl_wr && ioctl_index==1) begin
pcb <= ioctl_dout;
end
// priority table
if (ioctl_wr && ioctl_index==2) begin
if ( ioctl_addr[1:0] > 0 ) begin
prom[ioctl_addr[5:2]][ { ~ioctl_addr[1:0], 3'b111 } -: 8] <= ioctl_dout;
end
end
if (ioctl_wr && ioctl_index==3) begin
irq1_scanline <= ioctl_dout;
end
end
wire direct_video;
wire ioctl_download;
wire ioctl_upload;
wire ioctl_upload_req;
wire ioctl_wait;
wire ioctl_wr;
wire [7:0] ioctl_index;
wire [26:0] ioctl_addr;
wire [7:0] ioctl_dout;
wire [7:0] ioctl_din;
reg [4:0] pcb;
reg [7:0] cfg;
wire [21:0] gamma_bus;
//<buttons names="Fire,Jump,Start,Coin,Pause" default="A,B,R,L,Start" />
reg [15:0] p1;
reg [15:0] p2;
reg [15:0] dsw;
reg [15:0] system;
always @ (posedge clk_sys ) begin
p1 <= ~{ 8'h00, p1_buttons[3:0], p1_up, p1_down, p1_left, p1_right };
p2 <= ~{ reserve_2p_4, reserve_2p_3, reserve_2p_2, reserve_2p_1, reserve_1p_4, reserve_1p_3, reserve_1p_2, reserve_1p_1, p2_buttons[3:0], p2_up, p2_down, p2_left, p2_right };
dsw <= { sw[0], sw[1] };
system <= ~{ 8'h00, coin_b, coin_a, service, unknown_2, unknown_1, unknown_0, start2, start1 };
end
reg p1_swap;
reg p1_right;
reg p1_left;
reg p1_down;
reg p1_up;
reg [3:0] p1_buttons;
reg p2_right;
reg p2_left;
reg p2_down;
reg p2_up;
reg [3:0] p2_buttons;
reg start1;
reg start2;
reg coin_a;
reg coin_b;
reg b_pause;
reg service;
// Purpose unknown, set like PCB
reg reserve_1p_1;
reg reserve_1p_2;
reg reserve_1p_3;
reg reserve_1p_4;
reg reserve_2p_1;
reg reserve_2p_2;
reg reserve_2p_3;
reg reserve_2p_4;
// Purpose unknown, unknown_1 registers as coinup on PCB
reg unknown_0;
reg unknown_1;
reg unknown_2;
always @ (posedge clk_sys) begin
p1_swap <= status[49];
if ( status[49] == 0 ) begin
p1_right <= joy0[0] | key_p1_right;
p1_left <= joy0[1] | key_p1_left;
p1_down <= joy0[2] | key_p1_down;
p1_up <= joy0[3] | key_p1_up;
p1_buttons <= joy0[7:4] | { key_p1_d, key_p1_c, key_p1_b, key_p1_a };
start1 <= joy0[8] | joy1[8] | key_start_1p;
coin_a <= joy0[10] | joy1[10] | key_coin_a;
p2_right <= joy1[0] | key_p2_right;
p2_left <= joy1[1] | key_p2_left;
p2_down <= joy1[2] | key_p2_down;
p2_up <= joy1[3] | key_p2_up;
p2_buttons <= joy1[7:4] | { key_p2_d, key_p2_c, key_p2_b, key_p2_a };
start2 <= joy0[9] | joy1[9] | key_start_2p;
coin_b <= joy0[11] | joy1[11] | key_coin_b;
end else begin
p2_right <= joy0[0] | key_p1_right;
p2_left <= joy0[1] | key_p1_left;
p2_down <= joy0[2] | key_p1_down;
p2_up <= joy0[3] | key_p1_up;
p2_buttons <= joy0[7:4] | { key_p1_d, key_p1_c, key_p1_b, key_p1_a };
start2 <= joy0[8] | joy1[8] | key_start_1p;
coin_b <= joy0[10] | joy1[10] | key_coin_a;
p1_right <= joy1[0] | key_p2_right;
p1_left <= joy1[1] | key_p2_left;
p1_down <= joy1[2] | key_p2_down;
p1_up <= joy1[3] | key_p2_up;
p1_buttons <= joy1[7:4] | { key_p2_d, key_p2_c, key_p2_b, key_p2_a };
start1 <= joy1[9] | joy0[9] | key_start_2p;
coin_a <= joy0[11] | joy1[11] | key_coin_b;
end
end
always @ (posedge clk_sys) begin
service <= key_service;
b_pause <= joy0[12] | joy1[12] | key_pause;
reserve_1p_1 <= status[32];
reserve_1p_2 <= ~status[33];
reserve_1p_3 <= status[34];
reserve_1p_4 <= status[35];
reserve_2p_1 <= ~status[36];
reserve_2p_2 <= ~status[37];
reserve_2p_3 <= status[38];
reserve_2p_4 <= ~status[39];
unknown_0 <= status[40];
unknown_1 <= status[41];
unknown_2 <= status[42];
end
// Keyboard handler
reg key_start_1p, key_start_2p, key_coin_a, key_coin_b;
reg key_tilt, key_test, key_reset, key_service, key_pause;
reg key_p1_up, key_p1_left, key_p1_down, key_p1_right, key_p1_a, key_p1_b, key_p1_c, key_p1_d;
reg 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];
always @(posedge clk_sys) begin
reg old_state;
old_state <= ps2_key[10];
if ( old_state ^ ps2_key[10] ) begin
casex ( ps2_key[8:0] )
'h016 : key_start_1p <= pressed; // 1
'h01E : key_start_2p <= pressed; // 2
'h02E : key_coin_a <= pressed; // 5
'h036 : key_coin_b <= pressed; // 6
'h006 : key_test <= key_test ^ pressed; // f2
'h004 : key_reset <= pressed; // f3
'h046 : key_service <= pressed; // 9
'h02C : key_tilt <= pressed; // t
'h04D : key_pause <= pressed; // p
'h175 : key_p1_up <= pressed; // up
'h172 : key_p1_down <= pressed; // down
'h16B : key_p1_left <= pressed; // left
'h174 : key_p1_right <= pressed; // right
'h014 : key_p1_a <= pressed; // lctrl
'h011 : key_p1_b <= pressed; // lalt
'h029 : key_p1_c <= pressed; // spacebar
'h012 : key_p1_d <= pressed; // lshift
'h02D : key_p2_up <= pressed; // r
'h02B : key_p2_down <= pressed; // f
'h023 : key_p2_left <= pressed; // d
'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; // w
endcase
end
end
wire pll_locked;
wire clk_sys;
wire turbo_68k = status[15];
reg clk_1_75M,clk_3_5M,clk_4M,clk_6M,clk_cpu_p,clk_cpu_s;
wire clk_72M;
pll pll
(
.refclk(CLK_50M),
.rst(0),
.outclk_0(clk_sys),
.outclk_1(clk_72M),
.locked(pll_locked)
);
assign SDRAM_CLK = clk_72M;
localparam CLKSYS=72;
reg [7:0] clk_cpu_count;
reg [7:0] clk12_count;
reg [7:0] clk6_count;
reg [7:0] clk4_count;
reg [7:0] clk3_5_count;
reg [7:0] clk1_75_count;
always @ (posedge clk_sys) begin
clk_4M <= ( clk4_count == 0 );
if ( clk4_count == 17 ) begin
clk4_count <= 0;
end else if ( pause_cpu == 0 ) begin
clk4_count <= clk4_count + 1;
end
clk_6M <= ( clk6_count == 0 );
if ( clk6_count == 11 ) begin // 11
clk6_count <= 0;
end else begin
clk6_count <= clk6_count + 1;
end
// 12 / 14.4 MHZ
clk_cpu_p <= ( clk12_count == 0 );
if ( clk12_count == ( turbo_68k == 1 ? 4 : 5 ) ) begin // 5
clk12_count <= 0;
end else if ( pause_cpu == 0 ) begin
clk12_count <= clk12_count + 1;
end
// clocks below change to fractional divider
// clk_cpu_s <= ( clk_cpu_count == 0 );
// if ( clk_cpu_count == 4 ) begin // 4
// clk_cpu_count <= 0;
// end else if ( pause_cpu == 0 ) begin
// clk_cpu_count <= clk_cpu_count + 1;
// end
// 14MHz
// M = 7 / N = 36
clk_cpu_s <= 0;
if ( clk_cpu_count > 35 ) begin
clk_cpu_s <= 1;
clk_cpu_count <= clk_cpu_count - 29;
end else begin
clk_cpu_count <= clk_cpu_count + 7;
end
// M = 7 / N = 144
// clk_3_5M <= 0;
// if ( clk3_5_count > 143 ) begin
// clk_3_5M <= 1;
// clk3_5_count <= clk3_5_count - 136;
// end else begin
// clk3_5_count <= clk3_5_count + 7;
// end
// clk_3_5M <= ( clk3_5_count == 0 );
// if ( clk3_5_count == 19 ) begin // 3.6
// clk3_5_count <= 0;
// end else if ( pause_cpu == 0 ) begin
// clk3_5_count <= clk3_5_count + 1;
// end
// M = 6 / N = 247
// clk_1_75M <= 0;
// if ( clk1_75_count > 246 ) begin
// clk_1_75M <= 1;
// clk1_75_count <= clk1_75_count - 240;
// end else begin
// clk1_75_count <= clk1_75_count + 6;
// end
clk_3_5M <= ( clk1_75_count == 0 || clk1_75_count == 20 );
clk_1_75M <= ( clk1_75_count == 0 );
if ( clk1_75_count == 39 ) begin // 1.8
clk1_75_count <= 0;
end else if ( pause_cpu == 0 ) begin
clk1_75_count <= clk1_75_count + 1;
end
end
wire reset;
assign reset = RESET | key_reset | status[0];
//////////////////////////////////////////////////////////////////
wire rotate_ccw = 0;
wire no_rotate = orientation | direct_video;
wire video_rotated;
reg flip_init;
reg [23:0] rgb;
wire hbl;
wire vbl;
wire [8:0] hc;
wire [8:0] vc;
wire hsync;
wire vsync;
reg hbl_delay, vbl_delay;
always @ ( posedge clk_6M ) begin
hbl_delay <= hbl;
vbl_delay <= vbl;
end
video_timing video_timing (
.clk(clk_sys),
.clk_pix(clk_6M),
.hc(hc),
.vc(vc),
.refresh_mod(refresh_mod),
.hs_offset(hs_offset),
.vs_offset(vs_offset),
.hs_width(hs_width),
.vs_width(vs_width),
.hbl(hbl),
.vbl(vbl),
.hsync(hsync),
.vsync(vsync)
);
// PAUSE SYSTEM
wire pause_cpu;
wire hs_pause;
// 8 bits per colour, 72MHz sys clk
pause #(8,8,8,72) pause
(
.clk_sys(clk_sys),
.reset(reset),
.user_button(b_pause),
.pause_request(hs_pause),
.options(status[21:20]),
.pause_cpu(pause_cpu),
.dim_video(dim_video),
.OSD_STATUS(OSD_STATUS),
.r(rgb[23:16]),
.g(rgb[15:8]),
.b(rgb[7:0]),
.rgb_out(rgb_pause_out)
);
wire [23:0] rgb_pause_out;
wire dim_video;
arcade_video #(256,24) arcade_video
(
.*,
.clk_video(clk_sys),
.ce_pix(clk_6M),
.RGB_in(rgb_pause_out),
.HBlank(hbl_delay),
.VBlank(vbl_delay),
.HSync(hsync),
.VSync(vsync),
.fx(scan_lines)
);
/*
Phase Accumulator Increments (Fractional Size 32, look up size 8 bit, total 40 bits)
Increment Calculation - (Output Clock * 2 ^ Word Size) / Reference Clock
Example
NTSC = 3.579545
PAL = 4.43361875
W = 40 ( 32 bit fraction, 8 bit look up reference)
Ref CLK = 42.954544 (This could us any clock)
NTSC_Inc = 3.579545333 * 2 ^ 40 / 96 = 40997413706
*/
// SET PAL and NTSC TIMING
`ifdef MISTER_ENABLE_YC
assign CHROMA_PHASE_INC = PALFLAG ? 40'd67705769010: 40'd54663037000;
assign YC_EN = status[22];
assign PALFLAG = status[7];
`endif
wire flip = 0;
screen_rotate screen_rotate
(
.CLK_VIDEO,
.CE_PIXEL,
.VGA_R,
.VGA_G,
.VGA_B,
.VGA_HS,
.VGA_VS,
.VGA_DE,
.rotate_ccw,
.no_rotate,
.flip,
.video_rotated,
.FB_EN,
.FB_FORMAT,
.FB_WIDTH,
.FB_HEIGHT,
.FB_BASE,
.FB_STRIDE,
.FB_VBL,
.FB_LL,
.DDRAM_CLK,
.DDRAM_BUSY,
.DDRAM_BURSTCNT,
.DDRAM_ADDR,
.DDRAM_DIN,
.DDRAM_BE,
.DDRAM_WE,
.DDRAM_RD
);
reg [7:0] hc_del;
////////
reg download_en;
reg [15:0] download_index;
reg [26:0] download_addr;
reg [7:0] download_data;
reg download_wr;
wire download_wait;
// region size aw b dw ofs
// --------------------------------------
// maincpu 80000 19 1 16 00000
// audiocpu 40000 18 1 16 80000
// oki1 100000 20 0 8 100000
// oki2 80000 19 0 8 200000
// scroll0 80000 19 3 64 280000
// scroll1 80000 19 3 64 300000
// scroll2 20000 17 3 64 380000
// sprites 100000 20 3 64 400000
// proms 200 9 0 8 500000
// mcu 2000 13 0 8
// define functions used to decrypt cpu program
// bit reordering based on address range
// phantasm
function [15:0] swap_00(input [15:0] d);
begin
swap_00 = { d[4'hd],d[4'he],d[4'hf],d[4'h0],d[4'h1],d[4'h8],d[4'h9],d[4'ha],d[4'hb],d[4'hc],d[4'h5],d[4'h6],d[4'h7],d[4'h2],d[4'h3],d[4'h4] };
end
endfunction
function [15:0] swap_01(input [15:0] d);
begin
swap_01 = { d[4'hf],d[4'hd],d[4'hb],d[4'h9],d[4'h7],d[4'h5],d[4'h3],d[4'h1],d[4'he],d[4'hc],d[4'ha],d[4'h8],d[4'h6],d[4'h4],d[4'h2],d[4'h0] };
end
endfunction
function [15:0] swap_02(input [15:0] d);
begin
swap_02 = { d[4'h0],d[4'h1],d[4'h2],d[4'h3],d[4'h4],d[4'h5],d[4'h6],d[4'h7],d[4'hb],d[4'ha],d[4'h9],d[4'h8],d[4'hf],d[4'he],d[4'hd],d[4'hc] };
end
endfunction
// astyanax
function [15:0] swap_10(input [15:0] d);
begin
swap_10 = { d[4'hd],d[4'he],d[4'hf],d[4'h0],d[4'ha],d[4'h9],d[4'h8],d[4'h1],d[4'h6],d[4'h5],d[4'hc],d[4'hb],d[4'h7],d[4'h2],d[4'h3],d[4'h4] };
end
endfunction
function [15:0] swap_11(input [15:0] d);
begin
swap_11 = { d[4'hf],d[4'hd],d[4'hb],d[4'h9],d[4'h7],d[4'h5],d[4'h3],d[4'h1],d[4'h8],d[4'ha],d[4'hc],d[4'he],d[4'h0],d[4'h2],d[4'h4],d[4'h6] };
end
endfunction
function [15:0] swap_12(input [15:0] d);
begin
swap_12 = { d[4'h4],d[4'h5],d[4'h6],d[4'h7],d[4'h0],d[4'h1],d[4'h2],d[4'h3],d[4'hb],d[4'ha],d[4'h9],d[4'h8],d[4'hf],d[4'he],d[4'hd],d[4'hc] };
end
endfunction
// rodland
function [15:0] swap_20(input [15:0] d);
begin
swap_20 = { d[4'hd],d[4'h0],d[4'ha],d[4'h9],d[4'h6],d[4'he],d[4'hb],d[4'hf],d[4'h5],d[4'hc],d[4'h7],d[4'h2],d[4'h3],d[4'h8],d[4'h1],d[4'h4] };
end
endfunction
function [15:0] swap_21(input [15:0] d);
begin
swap_21 = { d[4'h4],d[4'h5],d[4'h6],d[4'h7],d[4'h0],d[4'h1],d[4'h2],d[4'h3],d[4'hb],d[4'ha],d[4'h9],d[4'h8],d[4'hf],d[4'he],d[4'hd],d[4'hc] };
end
endfunction
function [15:0] swap_22(input [15:0] d);
begin
swap_22 = { d[4'hf],d[4'hd],d[4'hb],d[4'h9],d[4'hc],d[4'he],d[4'h0],d[4'h7],d[4'h5],d[4'h3],d[4'h1],d[4'h8],d[4'ha],d[4'h2],d[4'h4],d[4'h6] };
end
endfunction
function [15:0] swap_23(input [15:0] d);
begin
swap_23 = { d[4'h4],d[4'h5],d[4'h1],d[4'h2],d[4'he],d[4'hd],d[4'h3],d[4'hb],d[4'ha],d[4'h9],d[4'h6],d[4'h7],d[4'h0],d[4'h8],d[4'hf],d[4'hc] };
end
endfunction
//localparam P47 = 0; no encryption
//localparam PHANTASM = 2; phantasm
//localparam RODLAND = 3; rodland
//localparam RODLANDJ = 4; astyanax
//localparam SHINGEN = 5; phantasm
//localparam ASTYANAX = 6; astyanax
//localparam SOLDAM = 7; phantasm
//localparam SOLDAMJ = 8; astyanax
//localparam EDFP = 9; phantasm
//localparam INYOURF = 10; phantasm
//localparam STDRAGON = 11; phantasm
//localparam STDRAGONA = 12; phantasm
//localparam HACHOO = 13; astyanax
//localparam PLUSALPH = 14; astyanax
//localparam IGANINJU = 15; phantasm
//localparam JITSUPRO = 16; astyanax
function [15:0] cpu_decode(input [23:0] i, input [15:0] d);
begin
if ( pcb == 0 || pcb == 1 ) begin
// p47 & kickoff are not encrypted
cpu_decode = d;
end else if ( pcb == 2 || pcb == 5 || pcb == 7 || pcb == 9 || pcb == 10 || pcb == 11 || pcb == 12 || pcb == 15 ) begin
// phantasm
if ( i < 20'h04000 ) begin
cpu_decode = ( i[8] & i[5] & i[2] ) ? swap_01( d ) : swap_00( d );
end else if ( i < 20'h08000 ) begin
cpu_decode = swap_02( d );
end else if ( i < 20'h0c000 ) begin
cpu_decode = ( i[8] & i[5] & i[2] ) ? swap_01( d ) : swap_00( d );
end else if ( i < 20'h10000 ) begin
cpu_decode = swap_01( d );
end else if ( i < 20'h20000 ) begin
cpu_decode = swap_02( d );
end else begin
cpu_decode = d;
end
end else if ( pcb == 4 || pcb == 6 || pcb == 8 || pcb == 13 || pcb == 14 || pcb == 16 ) begin
// astyanax
if ( i < 20'h04000 ) begin
cpu_decode = ( i[8] & i[5] & i[2] ) ? swap_11( d ) : swap_10( d );
end else if ( i < 20'h08000 ) begin
cpu_decode = swap_12( d );
end else if ( i < 20'h0c000 ) begin
cpu_decode = ( i[8] & i[5] & i[2] ) ? swap_11( d ) : swap_10( d );
end else if ( i < 20'h10000 ) begin
cpu_decode = swap_11( d );
end else if ( i < 20'h20000 ) begin
cpu_decode = swap_12( d );
end else begin
cpu_decode = d;
end
end else if ( pcb == 3 ) begin
// rod land
if ( i < 20'h04000 ) begin
cpu_decode = ( i[8] & i[5] & i[2] ) ? swap_21( d ) : swap_20( d );
end else if ( i < 20'h08000 ) begin
cpu_decode = ( i[8] & i[5] & i[2] ) ? swap_23( d ) : swap_22( d );
end else if ( i < 20'h0c000 ) begin
cpu_decode = ( i[8] & i[5] & i[2] ) ? swap_21( d ) : swap_20( d );
end else if ( i < 20'h10000 ) begin
cpu_decode = swap_21( d );
end else if ( i < 20'h20000 ) begin
cpu_decode = swap_23( d );
end else begin
cpu_decode = d;
end
end
end
endfunction
function [19:0] spr_decode_1(input [19:0] a);
begin
//int a = bitswap<20>(i, 19, 18, 17, 16, 15, 14, 3, 12, 11, 13, 9, 10, 7, 6, 5, 4, 8, 2, 1, 0);
spr_decode_1 = { a[19:14], a[3], a[12:11], a[13], a[9], a[10], a[7:4], a[8], a[2:0] };
end
endfunction
function [19:0] spr_decode_2(input [19:0] a);
begin
//int a = bitswap<20>(i, 19, 18, 17, 16, 15, 14, 10, 12, 11, 8, 9, 3, 7, 6, 5, 4, 13, 2, 1, 0);
spr_decode_2 = { a[19:14], a[10], a[12:11], a[8], a[9], a[3], a[7:4], a[13], a[2:0] };
end
endfunction
function [19:0] spr_decode_3(input [19:0] a);
begin
//int a = bitswap<20>(i, 19, 18, 17, 16, 15, 14, 8, 12, 11, 3, 9, 13, 7, 6, 5, 4, 10, 2, 1, 0);
spr_decode_3 = { a[19:14], a[8], a[12:11], a[3], a[9], a[13], a[7:4], a[10], a[2:0] };
end
endfunction
// rearrange sprite data so 16 bit wide sprite is one 64 bit read
wire [26:0] sprite_ioctl_addr = { ioctl_addr[26:7], ioctl_addr[5:2], ioctl_addr[6], ioctl_addr[1:0] };
// address is decoded before it is rearranged for efficient 64 bit wide access
wire [19:0] decode_ioctl_addr = ( pcb == RODLAND || pcb == RODLANDJ ) ? spr_decode_1(ioctl_addr[19:0]) :
( pcb == STDRAGONA ) ? spr_decode_2(ioctl_addr[19:0]) :
spr_decode_3(ioctl_addr[19:0]);
wire [19:0] sprite_decode_ioctl_addr = { decode_ioctl_addr[19:7], decode_ioctl_addr[5:2], decode_ioctl_addr[6], decode_ioctl_addr[1:0] };
always @ (posedge clk_sys) begin
download_en <= ioctl_download;
download_index <= ioctl_index;
if ( ioctl_addr >= 26'h080000 && ioctl_addr < 26'h0A0000 ) begin