-
Notifications
You must be signed in to change notification settings - Fork 57
/
TurboGrafx16.sv
1373 lines (1146 loc) · 33.8 KB
/
TurboGrafx16.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
//============================================================================
// TurboGrafx16 / PC Engine
//
// Port to MiSTer
// Copyright (C) 2017-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
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
);
//`define DEBUG_BUILD
//`define DEBUG_PALETTES
`ifdef DEBUG_BUILD
localparam LITE = 1;
`else
localparam LITE = 0;
`endif
assign ADC_BUS = 'Z;
assign VGA_F1 = 0;
assign {UART_RTS, UART_TXD, UART_DTR} = 0;
assign {SD_SCK, SD_MOSI, SD_CS} = 'Z;
assign LED_USER = cart_download | bk_state | bk_pending;
assign LED_DISK = 0;
assign LED_POWER = 0;
assign BUTTONS = osd_btn;
assign VGA_SCALER= 0;
assign VGA_DISABLE = 0;
assign HDMI_FREEZE = 0;
wire [1:0] ar = status[25:24];
wire vcrop_en = status[32];
wire [3:0] vcopt = status[36:33];
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) ? (overscan ? 8'd4 : 8'd47) : (ar - 1'd1)),
.ARY((!ar) ? (overscan ? 8'd3 : 8'd37) : 12'd0),
.CROP_SIZE((en216p & vcrop_en) ? 10'd216 : 10'd0),
.CROP_OFF(voff),
.SCALE(status[38:37])
);
// Status Bit Map:
// 0 1 2 3 4 5 6
// 01234567890123456789012345678901 23456789012345678901234567890123
// 0123456789ABCDEFGHIJKLMNOPQRSTUV 0123456789ABCDEFGHIJKLMNOPQRSTUV
// XXXX XXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXX
`include "build_id.v"
parameter CONF_STR = {
"TGFX16;;",
"FS0,PCEBIN,Load TurboGrafx;",
`ifndef DEBUG_BUILD
"FS1,SGX,Load SuperGrafx;",
`endif
"-;",
`ifdef DEBUG_PALETTES
"F3,TGP,Load Palette;",
"-;",
`endif
"S0,CUECHD,Insert CD;",
"-;",
"C,Cheats;",
"H1O5,Cheats enabled,ON,OFF;",
"-;",
"D0RG,Load Backup RAM;",
"D0R7,Save Backup RAM;",
"D0ON,Autosave,OFF,ON;",
"-;",
"P1,Audio & Video;",
"P1-;",
"P1OOP,Aspect ratio,Original,Full Screen,[ARC1],[ARC2];",
"P1O8A,Scandoubler Fx,None,HQ2x,CRT 25%,CRT 50%,CRT 75%;",
"P1-;",
"d6P1o0,Vertical Crop,Disabled,216p(5x);",
"d6P1o14,Crop Offset,0,2,4,8,10,12,-12,-10,-8,-6,-4,-2;",
"P1o56,Scale,Normal,V-Integer,Narrower HV-Integer,Wider HV-Integer;",
"P1-;",
"P1OS,Colors,Original,Raw RGB;",
"P1OH,Overscan,Hidden,Visible;",
"P1OF,Border Color,Original,Black;",
"P1OB,Sprites per line,Normal,Extra;",
"P1-;",
"P1OK,CD Audio Boost,No,2x;",
"P1OM,ADPCM Audio Boost,No,Yes;",
"P1OIJ,Master Audio Boost,No,2x,4x;",
"P2,Hardware;",
"P2-;",
"P2O3,ROM Data Swap,No,Yes;",
`ifdef DEBUG_BUILD
"P2O6,ROM Storage,SDRAM,DDR3;",
`else
"D4H2P2O6,ROM Storage,SDRAM,SDRAM;",
"D4H3P2O6,ROM Storage,DDR3,DDR3;",
`endif
"P2-;",
"P2OE,Arcade Card,Disabled,Enabled;",
"P2O1,CD Seek,Normal,Fast;",
"P2-;",
"P2OD,USER I/O,Off,SNAC;",
"H5P2OL,MB128,Disabled,Enabled;",
"P2-;",
"D0P2RC,Format Backup RAM;",
"-;",
"H5O2,Turbo Tap,Disabled,Enabled;",
"H5OTU,Controller,2 Buttons,2 Turbo,6 Buttons;",
"H5OQR,Special,None,Mouse,Pachinko,XE-1AP;",
"H5-;",
"R0,Reset;",
"J1,Button I,Button II,Select,Run,Button III,Button IV,Button V,Button VI;",
"jn,A,B,Select,Start,X,Y,L,R;",
"jp,A,B,Select,Start,L,R,Y,X;",
"V,v",`BUILD_DATE
};
reg osd_btn = 0;
always @(posedge clk_sys) begin
integer timeout = 0;
reg has_bootrom = 0;
reg last_rst = 0;
if (reset) last_rst = 0;
if (status[0]) last_rst = 1;
if (cart_download & ioctl_wr & status[0]) has_bootrom <= 1;
if (last_rst & ~status[0]) begin
osd_btn <= 0;
if (timeout < 24000000) begin
timeout <= timeout + 1;
osd_btn <= ~has_bootrom;
end
end
end
//////////////////// CLOCKS ///////////////////
wire clk_sys, clk_ram;
wire pll_locked;
pll pll
(
.refclk(CLK_50M),
.rst(0),
.outclk_0(clk_ram),
.outclk_1(clk_sys),
.locked(pll_locked)
);
///////////////////////////////////////////////////
wire [63:0] status;
wire [1:0] buttons;
wire [11:0] joy_0, joy_1, joy_2, joy_3, joy_4;
wire [15:0] joy_a, joy_b;
wire [7:0] pd_0;
wire ioctl_download;
wire [7:0] ioctl_index;
wire ioctl_wr;
wire [24:0] ioctl_addr;
wire [15:0] ioctl_dout;
reg ioctl_wait;
wire forced_scandoubler;
reg [31:0] sd_lba;
reg sd_rd = 0;
reg sd_wr = 0;
wire sd_ack;
wire [7:0] sd_buff_addr;
wire [15:0] sd_buff_dout;
wire [15:0] sd_buff_din;
wire sd_buff_wr;
wire img_mounted;
wire img_readonly;
wire [63:0] img_size;
wire [21:0] gamma_bus;
wire [15:0] sdram_sz;
wire [10:0] ps2_key;
wire [24:0] ps2_mouse;
wire [15:0] ps2_mouse_ext;
hps_io #(.CONF_STR(CONF_STR), .WIDE(1)) hps_io
(
.clk_sys(clk_sys),
.HPS_BUS(HPS_BUS),
.buttons(buttons),
.status(status),
.status_menumask({en216p, snac, 1'd1, use_sdr, ~use_sdr, ~gg_avail,~bk_ena}),
.forced_scandoubler(forced_scandoubler),
.sdram_sz(sdram_sz),
.new_vmode(0),
.gamma_bus(gamma_bus),
.ioctl_download(ioctl_download),
.ioctl_index(ioctl_index),
.ioctl_wr(ioctl_wr),
.ioctl_addr(ioctl_addr),
.ioctl_dout(ioctl_dout),
.ioctl_wait(ioctl_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),
.joystick_0(joy_0),
.joystick_1(joy_1),
.joystick_2(joy_2),
.joystick_3(joy_3),
.joystick_4(joy_4),
.joystick_l_analog_0(joy_a),
.joystick_r_analog_0(joy_b),
.paddle_0(pd_0),
.ps2_key(ps2_key),
.ps2_mouse(ps2_mouse),
.ps2_mouse_ext(ps2_mouse_ext),
.EXT_BUS(EXT_BUS)
);
wire reset = (RESET | status[0] | buttons[1] | bk_loading);
`ifdef DEBUG_PALETTES
wire palette_download = ioctl_download & (ioctl_index[5:0] == 6'h03 || (ioctl_index[7:6] == 1 && ~|ioctl_index));
`endif
wire code_index = &ioctl_index;
wire code_download = ioctl_download & code_index;
wire cart_download = ioctl_download & (ioctl_index[5:0] <= 6'h01);
wire cd_dat_download = ioctl_download & (ioctl_index[5:0] == 6'h02);
wire overscan = ~status[17];
wire [95:0] cd_comm;
wire cd_comm_send;
reg [15:0] cd_stat;
reg cd_stat_rec;
reg cd_dataout_req;
wire [79:0] cd_dataout;
wire cd_dataout_send;
wire cd_reset_req;
reg cd_region;
wire [21:0] cd_ram_a;
wire cd_ram_rd, cd_ram_wr;
wire [7:0] cd_ram_do;
wire ce_rom;
wire [15:0] cdda_sl, cdda_sr, adpcm_s, psg_sl, psg_sr;
pce_top #(LITE) pce_top
(
.RESET(reset|cart_download),
.COLD_RESET(cart_download),
.CLK(clk_sys),
.ROM_RD(rom_rd),
.ROM_RDY(rom_sdrdy & rom_ddrdy & ram_ddrdy),
.ROM_A(rom_rdaddr),
.ROM_DO(use_sdr ? rom_sdata : rom_ddata),
.ROM_SZ(romwr_a[23:12]),
.ROM_POP(populous[romwr_a[9]]),
.ROM_CLKEN(ce_rom),
.BRM_A(bram_addr),
.BRM_DO(bram_q),
.BRM_DI(bram_data),
.BRM_WE(bram_wr),
.GG_EN(status[5]),
.GG_CODE(gg_code),
.GG_RESET((cart_download | code_download) & ioctl_wr & !ioctl_addr),
.GG_AVAIL(gg_avail),
.SP64(status[11]),
.SGX(sgx && !LITE),
.JOY_OUT(joy_out),
.JOY_IN(joy_in),
.CD_EN(cd_en),
.AC_EN(status[14]),
.CD_RAM_A(cd_ram_a),
.CD_RAM_DO(cd_ram_do),
.CD_RAM_DI(use_sdr ? rom_sdata : rom_ddata),
.CD_RAM_RD(cd_ram_rd),
.CD_RAM_WR(cd_ram_wr),
.CD_STAT(cd_stat[7:0]),
.CD_MSG(cd_stat[15:8]),
.CD_STAT_GET(cd_stat_rec),
.CD_COMM(cd_comm),
.CD_COMM_SEND(cd_comm_send),
.CD_DOUT_REQ(cd_dataout_req),
.CD_DOUT(cd_dataout),
.CD_DOUT_SEND(cd_dataout_send),
.CD_REGION(cd_region),
.CD_RESET(cd_reset_req),
.CD_DATA(!cd_dat_byte ? cd_dat[7:0] : cd_dat[15:8]),
.CD_WR(cd_wr),
.CD_DATA_END(cd_dat_req),
.CD_DM(cd_dm),
.CDDA_SL(cdda_sl),
.CDDA_SR(cdda_sr),
.ADPCM_S(adpcm_s),
.PSG_SL(psg_sl),
.PSG_SR(psg_sr),
.BG_EN(VDC_BG_EN),
.SPR_EN(VDC_SPR_EN),
.GRID_EN(VDC_GRID_EN),
.CPU_PAUSE_EN(CPU_PAUSE_EN),
.ReducedVBL(~overscan),
.BORDER_EN(~status[15]),
.VIDEO_R(r),
.VIDEO_G(g),
.VIDEO_B(b),
.VIDEO_BW(bw),
//.VIDEO_CE(ce_vid),
.VIDEO_CE_FS(ce_vid),
.VIDEO_VS(vs),
.VIDEO_HS(hs),
.VIDEO_HBL(hbl),
.VIDEO_VBL(vbl)
);
//CD communication
wire [35:0] EXT_BUS;
reg [112:0] cd_in = 0;
wire [112:0] cd_out;
hps_ext hps_ext
(
.clk_sys(clk_sys),
.EXT_BUS(EXT_BUS),
.cd_in(cd_in),
.cd_out(cd_out)
);
reg cd_en = 0;
always @(posedge clk_sys) begin
if(img_mounted && img_size) cd_en <= 1;
if(cart_download) cd_en <= 0;
end
reg cd_dat_req;
always @(posedge clk_sys) begin
reg cd_out112_last = 1;
reg cd_comm_send_old = 0, cd_dataout_send_old = 0, cd_dat_req_old = 0, cd_reset_req_old = 0;
cd_stat_rec <= 0;
cd_dataout_req <= 0;
if (reset || cart_download) begin
cd_region <= 0;
end
else begin
if (cd_out[112] != cd_out112_last) begin
cd_out112_last <= cd_out[112];
cd_stat <= cd_out[15:0];
cd_stat_rec <= ~cd_out[16];
cd_dataout_req <= cd_out[16];
cd_region <= cd_out[17];
end
cd_comm_send_old <= cd_comm_send;
cd_dataout_send_old <= cd_dataout_send;
cd_dat_req_old <= cd_dat_req;
cd_reset_req_old <= cd_reset_req;
if (cd_comm_send && !cd_comm_send_old) begin
cd_in[95:0] <= cd_comm;
cd_in[111:96] <= {status[1],15'd0};
cd_in[112] <= ~cd_in[112];
end
else if (cd_dataout_send && !cd_dataout_send_old) begin
cd_in[79:0] <= cd_dataout;
cd_in[111:96] <= 16'h0001;
cd_in[112] <= ~cd_in[112];
end
else if (cd_dat_req && !cd_dat_req_old) begin
cd_in[111:96] <= 16'h0002;
cd_in[112] <= ~cd_in[112];
end
else if (cd_reset_req && !cd_reset_req_old) begin
cd_in[111:96] <= 16'h00FF;
cd_in[112] <= ~cd_in[112];
end
end
end
reg [15:0] cd_dat;
reg cd_wr;
reg cd_dat_byte;
reg cd_dm;
always @(posedge clk_sys) begin
reg old_download;
reg head_pos, cd_dat_write;
reg [14:0] cd_dat_len, cd_dat_cnt;
old_download <= cd_dat_download;
if ((~old_download && cd_dat_download) || reset) begin
head_pos <= 0;
cd_dat_len <= 0;
cd_dat_cnt <= 0;
end
else if (ioctl_wr && cd_dat_download) begin
if (!head_pos) begin
{cd_dm,cd_dat_len} <= ioctl_dout;
cd_dat_cnt <= 0;
head_pos <= 1;
end
else if (cd_dat_cnt < cd_dat_len) begin
cd_dat_write <= 1;
cd_dat_byte <= 0;
cd_dat <= ioctl_dout;
end
end
if (cd_dat_write) begin
if (!cd_wr) begin
cd_wr <= 1;
end
else begin
cd_wr <= 0;
cd_dat_byte <= ~cd_dat_byte;
cd_dat_cnt <= cd_dat_cnt + 15'd1;
if (cd_dat_byte || cd_dat_cnt >= cd_dat_len-1) begin
cd_dat_write <= 0;
end
end
end
end
//////////////////////////// VIDEO ///////////////////////////////////
wire [2:0] r,g,b;
wire hs,vs;
wire hbl,vbl;
wire bw;
wire ce_vid;
assign CLK_VIDEO = clk_ram;
reg ce_pix;
always @(posedge CLK_VIDEO) begin
reg old_ce;
old_ce <= ce_vid;
ce_pix <= ~old_ce & ce_vid;
end
`ifdef DEBUG_PALETTES
logic [23:0] read_color;
logic [8:0] ioctl_addr_1;
logic [1:0] pal_cnt;
always @(posedge clk_sys) begin
if (ioctl_wr & palette_download) begin
pal_cnt <= pal_cnt + 2'd1;
if (pal_cnt == 2) begin
pal_cnt <= 0;
ioctl_addr_1 <= ioctl_addr_1 + 1'd1;
end
end
if (palette_download)
read_color[{2'd2 - pal_cnt, 3'b000}+:8] <= ioctl_dout[7:0];
else begin
pal_cnt <= 0;
ioctl_addr_1 <= 0;
end
end
`endif
logic [23:0] pal_color;
dpram #(
.addr_width(9),
.data_width(24),
.mem_init_file("palette.mif")
) palette_ram (
.clock(CLK_VIDEO),
.address_a({g,r,b}),
.q_a(pal_color)
`ifdef DEBUG_PALETTES
,
.address_b(ioctl_addr_1),
.enable_b(palette_download),
.data_b({read_color[23:8], ioctl_dout[7:0]}),
.wren_b(ioctl_wr)
`endif
);
logic [7:0] r1, b1, g1;
assign {r1, g1, b1} = status[28] ? {{r,r,r[2:1]}, {g,g,g[2:1]}, {b,b,b[2:1]}} : pal_color;
color_mix color_mix
(
.clk_vid(CLK_VIDEO),
.ce_pix(ce_pix),
.mix(bw ? 3'd5 : 0),
.R_in(r1),
.G_in(g1),
.B_in(b1),
.HSync_in(hs),
.VSync_in(vs),
.HBlank_in(hbl),
.VBlank_in(vbl),
.R_out(R),
.G_out(G),
.B_out(B),
.HSync_out(HS),
.VSync_out(VS),
.HBlank_out(HBlank),
.VBlank_out(VBlank)
);
wire [7:0] R,G,B;
wire HS,VS;
wire HBlank,VBlank;
wire [2:0] scale = status[10:8];
wire [2:0] sl = scale ? scale - 1'd1 : 3'd0;
assign VGA_SL = sl[1:0];
reg VSync, HSync;
always @(posedge CLK_VIDEO) begin
HSync <= HS;
if(~HSync & HS) VSync <= VS;
end
video_mixer #(.LINE_LENGTH(560), .GAMMA(1)) video_mixer
(
.*,
.VGA_DE(vga_de),
.freeze_sync(),
.scandoubler(scale || forced_scandoubler),
.hq2x(scale==1)
);
//////////////////////////// AUDIO ///////////////////////////////////
localparam [3:0] comp_f1 = 4;
localparam [3:0] comp_a1 = 2;
localparam comp_x1 = ((32767 * (comp_f1 - 1)) / ((comp_f1 * comp_a1) - 1)) + 1; // +1 to make sure it won't overflow
localparam comp_b1 = comp_x1 * comp_a1;
localparam [3:0] comp_f2 = 8;
localparam [3:0] comp_a2 = 4;
localparam comp_x2 = ((32767 * (comp_f2 - 1)) / ((comp_f2 * comp_a2) - 1)) + 1; // +1 to make sure it won't overflow
localparam comp_b2 = comp_x2 * comp_a2;
function [15:0] compr; input [15:0] inp;
reg [15:0] v, v1, v2;
begin
v = inp[15] ? (~inp) + 1'd1 : inp;
v1 = (v < comp_x1[15:0]) ? (v * comp_a1) : (((v - comp_x1[15:0])/comp_f1) + comp_b1[15:0]);
v2 = (v < comp_x2[15:0]) ? (v * comp_a2) : (((v - comp_x2[15:0])/comp_f2) + comp_b2[15:0]);
v = status[19] ? v2 : v1;
compr = inp[15] ? ~(v-1'd1) : v;
end
endfunction
reg [17:0] audio_l, audio_r;
reg [15:0] cmp_l, cmp_r;
logic [4:0] div_audio;
logic adpcm_ce, psg_ce;
logic [15:0] adpcm_filt, psg_l_filt, psg_r_filt;
always @(posedge clk_sys) begin
// 2684650 and 1342323
div_audio <= div_audio + 1'd1;
adpcm_ce <= &div_audio[4:0];
psg_ce <= &div_audio[3:0];
end
IIR_filter #(
.coeff_x (0.00200339512841342642),
.coeff_x0 (2),
.coeff_x1 (1),
.coeff_x2 (0),
.coeff_y0 (-1.95511712863912712201),
.coeff_y1 (0.95667938324280066276),
.coeff_y2 (0),
.stereo (1)
) psg_filter (
.clk (clk_sys),
.ce (psg_ce), // (1342323 * 2)
.sample_ce (1),
.input_l (psg_sl),
.input_r (psg_sr),
.output_l (psg_l_filt),
.output_r (psg_r_filt)
);
IIR_filter #(
.coeff_x (0.00002488367092441635),
.coeff_x0 (3),
.coeff_x1 (3),
.coeff_x2 (1),
.coeff_y0 (-2.94383188882174362533),
.coeff_y1 (2.88923013608993572987),
.coeff_y2 (-0.94537670406128904155),
.stereo (0)
) adpcm_filter (
.clk (clk_sys),
.ce (adpcm_ce), // 1342323
.sample_ce (1),
.input_l (adpcm_s),
.output_l (adpcm_filt)
);
always @(posedge clk_sys) begin
reg [17:0] pre_l, pre_r;
reg signed [16:0] adpcm_boost;
adpcm_boost <= $signed({adpcm_filt[15], adpcm_filt}) + $signed((status[22] ? {{3{adpcm_filt[15]}}, adpcm_filt[15:2]} : 17'd0));
pre_l <= ( CDDA_EN ? {{2{cdda_sl[15]}}, cdda_sl} : 18'd0)
+ ((CDDA_EN && status[20]) ? {{2{cdda_sl[15]}}, cdda_sl} : 18'd0)
+ ( PSG_EN ? {{2{psg_l_filt[15]}}, psg_l_filt} : 18'd0)
+ ( ADPCM_EN ? {adpcm_boost[16], adpcm_boost} : 18'd0);
pre_r <= ( CDDA_EN ? {{2{cdda_sr[15]}}, cdda_sr} : 18'd0)
+ ((CDDA_EN && status[20]) ? {{2{cdda_sr[15]}}, cdda_sr} : 18'd0)
+ ( PSG_EN ? {{2{psg_r_filt[15]}}, psg_r_filt} : 18'd0)
+ ( ADPCM_EN ? {adpcm_boost[16], adpcm_boost} : 18'd0);
if(~status[20]) begin
// 3/4 + 1/4 to cover the whole range.
audio_l <= $signed(pre_l) + ($signed(pre_l)>>>2);
audio_r <= $signed(pre_r) + ($signed(pre_r)>>>2);
end
else begin
audio_l <= pre_l;
audio_r <= pre_r;
end
cmp_l <= compr(audio_l[17:2]);
cmp_r <= compr(audio_r[17:2]);
end
assign AUDIO_L = status[19:18] ? cmp_l : audio_l[17:2];
assign AUDIO_R = status[19:18] ? cmp_r : audio_r[17:2];
assign AUDIO_S = 1;
assign AUDIO_MIX = 0;
//////////////////////////// MEMORY //////////////////////////////////
reg use_sdr = 0;
always @(posedge clk_ram) if(~rom_rd) use_sdr <= LITE ? ~status[6] : |sdram_sz[14:0];
wire [21:0] rom_rdaddr;
wire [7:0] rom_ddata, rom_sdata;
wire rom_rd, rom_sdrdy, rom_ddrdy, ram_ddrdy;
assign DDRAM_CLK = clk_ram;
ddram ddram
(
.*,
.clkref(ce_rom),
.wraddr(cart_download ? romwr_a : {3'b001,cd_ram_a}),
.din(cart_download ? romwr_d : {cd_ram_do,cd_ram_do}),
.we(~cart_download & ~use_sdr & cd_ram_wr & ce_rom),
.we_req(rom_wr),
.we_ack(dd_wrack),
.we_rdy(ram_ddrdy),
.rdaddr(rom_rd ? {3'b000,(rom_rdaddr + (romwr_a[9] ? 22'h200 : 22'h0))} : {3'b001,cd_ram_a}),
.rd(~use_sdr & (rom_rd | cd_ram_rd) & ce_rom),
.rd_rdy(rom_ddrdy),
.dout(rom_ddata)
);
sdram sdram
(
.*,
.init(~pll_locked),
.clk(clk_ram),
.clkref(ce_rom),
.waddr(cart_download ? romwr_a : {3'b001,cd_ram_a}),
.din(cart_download ? romwr_d : {cd_ram_do,cd_ram_do}),
.we(~cart_download & use_sdr & cd_ram_wr & ce_rom),
.we_req(rom_wr),
.we_ack(sd_wrack),
.raddr(rom_rd ? {3'b000,(rom_rdaddr + (romwr_a[9] ? 22'h200 : 22'h0))} : {3'b001,cd_ram_a}),
.rd(use_sdr & (rom_rd | cd_ram_rd) & ce_rom),
.rd_rdy(rom_sdrdy),
.dout(rom_sdata)
);
wire romwr_ack;
reg [23:0] romwr_a;
wire [15:0] romwr_d = status[3] ?
{ ioctl_dout[8], ioctl_dout[9], ioctl_dout[10],ioctl_dout[11],ioctl_dout[12],ioctl_dout[13],ioctl_dout[14],ioctl_dout[15],
ioctl_dout[0], ioctl_dout[1], ioctl_dout[2], ioctl_dout[3], ioctl_dout[4], ioctl_dout[5], ioctl_dout[6], ioctl_dout[7] }
: ioctl_dout;
reg rom_wr = 0;
wire sd_wrack, dd_wrack;
reg [1:0] populous;
reg sgx;
always @(posedge clk_sys) begin
reg old_download, old_reset;
old_download <= cart_download;
old_reset <= reset;
if(~old_reset && reset) ioctl_wait <= 0;
if(~old_download && cart_download) begin
romwr_a <= 0;
populous <= 2'b11;
sgx <= ioctl_index[0];
end
else begin
if(ioctl_wr & cart_download) begin
ioctl_wait <= 1;
rom_wr <= ~rom_wr;
if((romwr_a[23:4] == 'h212) || (romwr_a[23:4] == 'h1f2)) begin
case(romwr_a[3:0])
6: if(romwr_d != 'h4F50) populous[romwr_a[13]] <= 0;
8: if(romwr_d != 'h5550) populous[romwr_a[13]] <= 0;
10: if(romwr_d != 'h4F4C) populous[romwr_a[13]] <= 0;
12: if(romwr_d != 'h5355) populous[romwr_a[13]] <= 0;
endcase
end
end else if(ioctl_wait && (rom_wr == dd_wrack) && (rom_wr == sd_wrack)) begin
ioctl_wait <= 0;
romwr_a <= romwr_a + 2'd2;
end
end
end
//////////////////////////// CODES ///////////////////////////////////
reg [128:0] gg_code;
wire gg_avail;
// Code layout:
// {clock bit, code flags, 32'b address, 32'b compare, 32'b replace}
// 128 127:96 95:64 63:32 31:0
// Integer values are in BIG endian byte order, so it up to the loader
// or generator of the code to re-arrange them correctly.
always_ff @(posedge clk_sys) begin
gg_code[128] <= 1'b0;
if (code_download & ioctl_wr) begin
case (ioctl_addr[3:0])
0: gg_code[111:96] <= ioctl_dout; // Flags Bottom Word
2: gg_code[127:112] <= ioctl_dout; // Flags Top Word
4: gg_code[79:64] <= ioctl_dout; // Address Bottom Word
6: gg_code[95:80] <= ioctl_dout; // Address Top Word
8: gg_code[47:32] <= ioctl_dout; // Compare Bottom Word
10: gg_code[63:48] <= ioctl_dout; // Compare top Word
12: gg_code[15:0] <= ioctl_dout; // Replace Bottom Word
14: begin
gg_code[31:16] <= ioctl_dout; // Replace Top Word
gg_code[128] <= 1'b1; // Clock it in
end
endcase
end
end
//////////////////////////// INPUT ///////////////////////////////////
wire [15:0] joy_data;
always_comb begin
case (joy_port)
0: joy_data = (status[27:26] == 2'b01) ? {mouse_data, mouse_data} :
(status[27:26] == 2'b11) ? {xe1_data[2], xe1_data[1], xe1_data[3], xe1_data[0], xe1_runbtn, xe1_selbtn, xe1_trg2, xe1_trg1,
xe1_data[2], xe1_data[1], xe1_data[3], xe1_data[0], xe1_runbtn, xe1_selbtn, xe1_trg2, xe1_trg1} :
~{4'hF, joy_0[11:8], joy_0[1], joy_0[2], joy_0[0], joy_0[3], joy_0[7:6], (status[30:29] == 2'b01) ? joyrept_0[1:0] : joy_0[5:4]};
1: joy_data = (status[27:26] == 2'b10) ? pachinko : ~{4'hF, joy_1[11:8], joy_1[1], joy_1[2], joy_1[0], joy_1[3], joy_1[7:6], (status[30:29] == 2'b01) ? joyrept_1[1:0] : joy_1[5:4]};
2: joy_data = ~{4'hF, joy_2[11:8], joy_2[1], joy_2[2], joy_2[0], joy_2[3], joy_2[7:6], (status[30:29] == 2'b01) ? joyrept_2[1:0] : joy_2[5:4]};