forked from MiSTer-devel/NES_MiSTer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NES.sv
1270 lines (1115 loc) · 34.2 KB
/
NES.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
// Copyright (c) 2012-2013 Ludvig Strigeus
// This program is GPL Licensed. See COPYING for the full license.
//
// MiSTer port: Copyright (C) 2017,2018 Sorgelig
module emu
(
//Master input clock
input CLK_50M,
//Async reset from top-level module.
//Can be used as initial reset.
input RESET,
//Must be passed to hps_io module
inout [45:0] HPS_BUS,
//Base video clock. Usually equals to CLK_SYS.
output CLK_VIDEO,
//Multiple resolutions are supported using different CE_PIXEL rates.
//Must be based on CLK_VIDEO
output CE_PIXEL,
//Video aspect ratio for HDMI. Most retro systems have ratio 4:3.
output [7:0] VIDEO_ARX,
output [7:0] VIDEO_ARY,
output [7:0] VGA_R,
output [7:0] VGA_G,
output [7:0] VGA_B,
output VGA_HS,
output VGA_VS,
output VGA_DE, // = ~(VBlank | HBlank)
output VGA_F1,
output [1:0] VGA_SL,
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,
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,
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 AUDIO_S = 1'b1;
assign AUDIO_L = |mute_cnt ? 16'd0 : sample_signed[15:0];
assign AUDIO_R = AUDIO_L;
assign AUDIO_MIX = 0;
assign LED_USER = downloading | (loader_fail & led_blink) | (bk_state != S_IDLE) | (bk_pending & status[17]);
assign LED_DISK = 0;
assign LED_POWER = 0;
assign BUTTONS = 0;
assign VIDEO_ARX = status[8] ? 8'd16 : (hide_overscan ? 8'd64 : 8'd128);
assign VIDEO_ARY = status[8] ? 8'd9 : (hide_overscan ? 8'd49 : 8'd105);
assign VGA_F1 = 0;
//assign {UART_RTS, UART_TXD, UART_DTR} = 0;
assign {DDRAM_CLK, DDRAM_BURSTCNT, DDRAM_ADDR, DDRAM_DIN, DDRAM_BE, DDRAM_RD, DDRAM_WE} = 0;
assign {SD_SCK, SD_MOSI, SD_CS} = 'Z;
`define DEBUG_AUDIO
// Status Bit Map:
// 0 1 2 3
// 01234567890123456789012345678901
// 0123456789ABCDEFGHIJKLMNOPQRSTUV
// XXXXXXXXXXX XXXXXXXXXXXXXXXXX XX
`include "build_id.v"
parameter CONF_STR = {
"NES;;",
"-;",
"FS,NESFDSNSF;",
"H1F,BIN,Load FDS BIOS;",
"-;",
"ONO,System Type,NTSC,PAL,Dendy;",
"-;",
"OG,Disk Swap ("
};
parameter CONF_STR2 = {
"),Auto,FDS button;",
"O5,Invert Mirroring,Off,On;",
"-;",
"C,Cheats;",
"H2OK,Cheats Enabled,On,Off;",
"-;",
"D0R6,Load Backup RAM;",
"D0R7,Save Backup RAM;",
"D0OH,Autosave,Off,On;",
"-;",
"O8,Aspect Ratio,4:3,16:9;",
"O13,Scandoubler Fx,None,HQ2x,CRT 25%,CRT 50%,CRT 75%;",
"O4,Hide Overscan,Off,On;",
"ORS,Mask Edges,Off,Left,Both,Auto;",
"OP,Extra Sprites,Off,On;",
"OCF,Palette,Smooth,Unsat.,FCEUX,NES Classic,Composite,PC-10,PVM,Wavebeam,Real,Sony CXA,YUV,Greyscale,Rockman9,Nintendulator;",
"-;",
"O9,Swap Joysticks,No,Yes;",
"OIJ,Peripheral,Powerpad,Zapper(Mouse),Zapper(Joy1),Zapper(Joy2);",
"OL,Zapper Trigger,Mouse,Joystick;",
"OM,Crosshairs,On,Off;",
"OA,Multitap,Disabled,Enabled;",
"OQ,Serial Mode,None,SNAC;",
`ifdef DEBUG_AUDIO
"-;",
"OUV,Audio Enable,Both,Internal,Cart Expansion,None;",
`endif
"-;",
"R0,Reset;",
"J1,A,B,Select,Start,FDS,Trigger,Mic,PP 1,PP 2,PP 3,PP 4,PP 5,PP 6,PP 7,PP 8,PP 9,PP 10,PP 11,PP 12;",
"jn,A,B,Select,Start,L,R,Y,X;", // name-based layout, also add 3 extra buttons for FDS, Trigger, and PP1
"jp,B,Y,Select,Start,L,R,X,A;", // positional layout using Nintendo's layout for NES remakes (e.g. Mario All Starts in SNES)
"V,v",`BUILD_DATE
};
wire [22:0] joyA,joyB,joyC,joyD;
wire [1:0] buttons;
wire [31:0] status;
wire arm_reset = status[0];
wire mirroring_osd = status[5];
wire pal_video = |status[24:23];
wire hide_overscan = status[4] && ~pal_video;
wire [3:0] palette2_osd = status[15:12];
wire joy_swap = status[9] ^ (raw_serial || piano); // Controller on port 2 for Miracle Piano/SNAC
wire fds_swap_invert = status[16];
`ifdef DEBUG_AUDIO
wire ext_audio = ~status[30];
wire int_audio = ~status[31];
`else
wire ext_audio = 1;
wire int_audio = 1;
`endif
// Remove DC offset and convert to signed
// At this CE rate, it also slightly lowers the bass to
// better imitate the real high pass of the system.
jt49_dcrm2 #(.sw(16)) dc_filter (
.clk (clk),
.cen (apu_ce & &filter_cnt),
.rst (reset_nes),
.din (sample),
.dout (sample_signed)
);
wire apu_ce;
wire signed [15:0] sample_signed;
reg [20:0] mute_cnt = 21'h1FFFFF;
// Pause audio to avoid loud "POP"
always_ff @(posedge clk) begin
if (reset_nes)
mute_cnt <= 21'h1FFFFF;
else if (|mute_cnt)
mute_cnt <= mute_cnt - 1'b1;
end
// Filter CE impacts frequency response
reg [2:0] filter_cnt;
always_ff @(posedge clk) begin
if (apu_ce)
filter_cnt<= filter_cnt + 1'b1;
end
reg [31:0] sd_lba;
reg sd_rd = 0;
reg sd_wr = 0;
wire sd_ack;
wire [8:0] sd_buff_addr;
wire [7:0] sd_buff_dout;
wire [7:0] sd_buff_din;
wire sd_buff_wr;
wire img_mounted;
wire img_readonly;
wire [63:0] img_size;
wire [7:0] filetype;
wire ioctl_download;
wire [24:0] ioctl_addr;
reg ioctl_wait;
wire [24:0] ps2_mouse;
wire [15:0] joy_analog0, joy_analog1;
wire forced_scandoubler;
hps_io #(.STRLEN(($size(CONF_STR)>>3) + ($size(CONF_STR2)>>3) + 1)) hps_io
(
.clk_sys(clk),
.HPS_BUS(HPS_BUS),
.conf_str({CONF_STR, diskside==3?"3":diskside==2?"2":diskside==1?"1":"0", CONF_STR2}),
.buttons(buttons),
.forced_scandoubler(forced_scandoubler),
.joystick_0(joyA),
.joystick_1(joyB),
.joystick_2(joyC),
.joystick_3(joyD),
.joystick_analog_0(joy_analog0),
.joystick_analog_1(joy_analog1),
.status(status),
.status_menumask({~gg_avail, bios_loaded, ~bk_ena}),
.ioctl_download(ioctl_download),
.ioctl_addr(ioctl_addr),
.ioctl_wr(loader_clk),
.ioctl_dout(file_input),
.ioctl_wait(ioctl_wait | save_wait),
.ioctl_index(filetype),
.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),
.ps2_kbd_led_use(0),
.ps2_kbd_led_status(0),
.ps2_mouse(ps2_mouse),
.uart_mode(16'b000_11111_000_11111)
);
wire clock_locked;
wire clk85;
wire clk;
assign SDRAM_CLK = ~clk85;
pll pll
(
.refclk(CLK_50M),
.rst(0),
.outclk_0(clk85),
.outclk_1(CLK_VIDEO),
.outclk_2(clk),
.reconfig_to_pll(reconfig_to_pll),
.reconfig_from_pll(reconfig_from_pll),
.locked(clock_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 pald = 0, pald2 = 0;
reg [2:0] state = 0;
pald <= status[23];
pald2 <= pald;
cfg_write <= 0;
if(pald2 != pald) 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 <= 7;
cfg_data <= pald2 ? 2201376898 : 2537933971;
cfg_write <= 1;
end
5: begin
cfg_address <= 2;
cfg_data <= 0;
cfg_write <= 1;
end
endcase
end
end
// reset after download
reg [7:0] download_reset_cnt;
wire download_reset = download_reset_cnt != 0;
always @(posedge clk) begin
if(downloading) download_reset_cnt <= 8'hFF;
else if(!loader_busy && download_reset_cnt) download_reset_cnt <= download_reset_cnt - 1'd1;
end
// hold machine in reset until first download starts
reg init_reset_n = 0;
always @(posedge clk) if(downloading) init_reset_n <= 1;
wire [8:0] cycle;
wire [8:0] scanline;
wire [15:0] sample;
wire [5:0] color;
wire joypad_strobe;
wire [1:0] joypad_clock;
reg [23:0] joypad_bits, joypad_bits2;
reg [7:0] powerpad_d3, powerpad_d4;
reg [1:0] last_joypad_clock;
wire [11:0] powerpad = joyA[22:11] | joyB[22:11] | joyC[22:11] | joyD[22:11];
wire [7:0] nes_joy_A = { joyA[0], joyA[1], joyA[2], joyA[3], joyA[7], joyA[6], joyA[5], joyA[4] };
wire [7:0] nes_joy_B = { joyB[0], joyB[1], joyB[2], joyB[3], joyB[7], joyB[6], joyB[5], joyB[4] };
wire [7:0] nes_joy_C = { joyC[0], joyC[1], joyC[2], joyC[3], joyC[7], joyC[6], joyC[5], joyC[4] };
wire [7:0] nes_joy_D = { joyD[0], joyD[1], joyD[2], joyD[3], joyD[7], joyD[6], joyD[5], joyD[4] };
wire mic_button = joyA[10] | joyB[10];
wire fds_btn = joyA[8] | joyB[8];
reg [22:0] clkcount;
always@(posedge clk) begin
if (nes_ce == 3) begin
clkcount<=clkcount+1'd1;
end
end
wire fds_eject = swap_delay[2] | fds_swap_invert ? fds_btn : (clkcount[21] | fds_btn);
reg [1:0] nes_ce;
wire raw_serial = status[26];
// Indexes:
// 0 = D+
// 1 = D-
// 2 = TX-
// 3 = GND_d
// 4 = RX+
// 5 = RX-
assign USER_OUT[2] = 1'b1;
assign USER_OUT[3] = 1'b1;
assign USER_OUT[4] = 1'b1;
assign USER_OUT[5] = 1'b1;
assign USER_OUT[6] = 1'b1;
always_comb begin
if (raw_serial) begin
USER_OUT[0] = joypad_strobe;
USER_OUT[1] = ~joy_swap ? ~joypad_clock[1] : ~joypad_clock[0];
joy_data = {~USER_IN[4], ~USER_IN[2], ~joy_swap ? ~USER_IN[5] : joypad_bits2[0], ~joy_swap ? joypad_bits[0] : ~USER_IN[5]};
end else begin
USER_OUT[0] = 1'b1;
USER_OUT[1] = 1'b1;
joy_data = {lightgun_en ? trigger : powerpad_d4[0],lightgun_en ? light : powerpad_d3[0],joypad_bits2[0],joypad_bits[0]};
end
end
wire [3:0] joy_data;
reg [7:0] mic_cnt;
wire mic = (mic_cnt < 8'd215) && mic_button;
always @(posedge clk)
mic_cnt <= (mic_cnt == 8'd250) ? 8'd0 : mic_cnt + 1'b1;
assign {UART_RTS, UART_DTR} = 1;
wire [15:0] uart_data;
miraclepiano miracle(
.clk(clk),
.reset(reset_nes || !piano),
.strobe(joypad_strobe),
.joypad_o(),
.joypad_clock(joypad_clock[0]),
.data_o(uart_data),
.txd(UART_TXD),
.rxd(UART_RXD)
);
zapper zap (
.clk(clk),
.reset(reset_nes | ~lightgun_en),
.mode(status[19]),
.trigger_mode(status[21]),
.ps2_mouse(ps2_mouse),
.analog(~status[18] ? joy_analog0 : joy_analog1),
.analog_trigger(~status[18] ? joyA[9] : joyB[9]),
.cycle(cycle),
.scanline(scanline),
.color(color),
.reticle(reticle),
.light(light),
.trigger(trigger)
);
always @(posedge clk) begin
if (reset_nes) begin
joypad_bits <= 0;
joypad_bits2 <= 0;
powerpad_d3 <= 0;
powerpad_d4 <= 0;
last_joypad_clock <= 0;
end else begin
if (joypad_strobe) begin
joypad_bits <= piano ? {15'h0000, uart_data[8:0]}
: {status[10] ? {8'h08, nes_joy_C} : 16'hFFFF, joy_swap ? nes_joy_B : nes_joy_A};
joypad_bits2 <= {status[10] ? {8'h04, nes_joy_D} : 16'hFFFF, joy_swap ? nes_joy_A : nes_joy_B};
powerpad_d4 <= {4'b0000, powerpad[7], powerpad[11], powerpad[2], powerpad[3]};
powerpad_d3 <= {powerpad[6], powerpad[10], powerpad[9], powerpad[5], powerpad[8], powerpad[4], powerpad[0], powerpad[1]};
end
if (!joypad_clock[0] && last_joypad_clock[0]) begin
joypad_bits <= {1'b0, joypad_bits[23:1]};
end
if (!joypad_clock[1] && last_joypad_clock[1]) begin
joypad_bits2 <= {1'b0, joypad_bits2[23:1]};
powerpad_d4 <= {1'b0, powerpad_d4[7:1]};
powerpad_d3 <= {1'b0, powerpad_d3[7:1]};
end
last_joypad_clock <= joypad_clock;
end
end
// Loader
wire [7:0] file_input;
wire [7:0] loader_input = (loader_busy && !downloading) ? !nsf ? bios_data : nsf_data : file_input;
wire loader_clk;
wire [21:0] loader_addr;
wire [7:0] loader_write_data;
reg [7:0] old_filetype;
wire loader_reset = !download_reset || ((old_filetype != filetype) && |filetype && ~type_gg); //loader_conf[0];
wire loader_write;
wire [31:0] loader_flags;
reg [31:0] mapper_flags;
wire fds = (mapper_flags[7:0] == 8'h14);
wire nsf = (loader_flags[7:0] == 8'h1F);
wire piano = (mapper_flags[30]);
wire loader_busy, loader_done, loader_fail;
wire bios_download;
GameLoader loader
(
.clk ( clk ),
.reset ( loader_reset ),
.downloading ( downloading ),
.filetype ( filetype ),
.indata ( loader_input ),
.indata_clk ( loader_clk ),
.invert_mirroring ( mirroring_osd ),
.mem_addr ( loader_addr ),
.mem_data ( loader_write_data ),
.mem_write ( loader_write ),
.bios_download ( bios_download ),
.mapper_flags ( loader_flags ),
.busy ( loader_busy ),
.done ( loader_done ),
.error ( loader_fail )
);
reg [24:0] rom_sz;
always @(posedge clk) begin
reg done = 0;
done <= loader_done;
if(~done & loader_done) rom_sz <= ioctl_addr - 1'd1;
if (loader_done) mapper_flags <= loader_flags;
old_filetype <= filetype;
end
reg led_blink;
always @(posedge clk) begin
int cnt = 0;
cnt <= cnt + 1;
if(cnt == 10000000) begin
cnt <= 0;
led_blink <= ~led_blink;
end;
end
wire reset_nes =
~init_reset_n ||
buttons[1] ||
arm_reset ||
download_reset ||
loader_fail ||
bk_loading ||
bk_loading_req ||
hold_reset ||
(old_sys_type != status[24:23]);
reg [1:0] old_sys_type;
always @(posedge clk) old_sys_type <= status[24:23];
wire [17:0] bram_addr;
wire [7:0] bram_din;
wire [7:0] bram_dout;
wire bram_write;
wire bram_en;
wire trigger;
wire light;
wire [1:0] diskside_req;
reg [1:0] diskside;
wire lightgun_en = |status[19:18];
NES nes (
.clk (clk),
.reset_nes (reset_nes),
.sys_type (status[24:23]),
.nes_div (nes_ce),
.mapper_flags (downloading ? 32'd0 : mapper_flags),
.gg (status[20]),
.gg_code (gg_code),
.gg_reset (ioctl_download && loader_clk && !ioctl_addr),
.gg_avail (gg_avail),
// Audio
.sample (sample),
.audio_channels (5'b11111),
.int_audio (int_audio),
.ext_audio (ext_audio),
.apu_ce (apu_ce),
// Video
.ex_sprites (status[25]),
.color (color),
.emphasis (emphasis),
.cycle (cycle),
.scanline (scanline),
.mask (status[28:27]),
// User Input
.joypad_strobe (joypad_strobe),
.joypad_clock (joypad_clock),
.joypad_data (joy_data),
.mic (mic),
.diskside_req (diskside_req),
.diskside (diskside),
.fds_busy (fds_busy),
.fds_eject (fds_eject),
// Memory transactions
.cpumem_addr (cpu_addr ),
.cpumem_read (cpu_read ),
.cpumem_write (cpu_write),
.cpumem_dout (cpu_dout ),
.cpumem_din (cpu_din ),
.ppumem_addr (ppu_addr ),
.ppumem_read (ppu_read ),
.ppumem_write (ppu_write),
.ppumem_dout (ppu_dout ),
.ppumem_din (ppu_din ),
.bram_addr (bram_addr),
.bram_din (bram_din),
.bram_dout (bram_dout),
.bram_write (bram_write),
.bram_override (bram_en),
.save_written (save_written)
);
wire [21:0] cpu_addr, ppu_addr;
wire cpu_read, cpu_write, ppu_read, ppu_write;
wire [7:0] cpu_dout, cpu_din, ppu_dout, ppu_din;
wire [2:0] emphasis;
wire [7:0] xor_data;
wire [7:0] bios_data;
wire bios_write = (loader_write && bios_download && ~bios_loaded);
reg bios_loaded = 0; // Only load bios once
reg last_bios_download = 0;
always @(posedge clk) begin
last_bios_download <= bios_download;
if(last_bios_download && ~bios_download) begin
bios_loaded = 1;
end
end
dpram #("fdspatch.mif", 13) biospatch
(
.clock_a(clk),
.address_a(ioctl_addr[12:0]),
.wren_a(bios_write),
.data_a(bios_data ^ loader_write_data),
.q_a(xor_data),
.clock_b(clk),
.address_b(loader_addr[12:0]),
.q_b(bios_data)
);
wire [7:0] nsf_data;
spram #(12, 8, "loopy_NSF.mif") nsfplayrom
(
.clock(clk),
.address(loader_addr[11:0]),
.q(nsf_data)
);
// loader_write -> clock when data available
reg loader_write_mem;
reg [7:0] loader_write_data_mem;
reg [21:0] loader_addr_mem;
reg loader_write_triggered;
always @(posedge clk) begin
if(loader_write) begin
loader_write_triggered <= 1'b1;
loader_addr_mem <= loader_addr;
loader_write_data_mem <= bios_download ? loader_write_data ^ xor_data : loader_write_data;
ioctl_wait <= 1;
end
if(nes_ce == 3) begin
loader_write_mem <= loader_write_triggered;
if(loader_write_triggered) begin
loader_write_triggered <= 1'b0;
end else if(ioctl_wait) begin
ioctl_wait <= 0;
end
end
end
sdram sdram
(
.SDRAM_DQ ( SDRAM_DQ ),
.SDRAM_A ( SDRAM_A ),
.SDRAM_DQML ( SDRAM_DQML ),
.SDRAM_DQMH ( SDRAM_DQMH ),
.SDRAM_BA ( SDRAM_BA ),
.SDRAM_nCS ( SDRAM_nCS ),
.SDRAM_nWE ( SDRAM_nWE ),
.SDRAM_nRAS ( SDRAM_nRAS ),
.SDRAM_nCAS ( SDRAM_nCAS ),
.SDRAM_CKE ( SDRAM_CKE ),
// system interface
.clk ( clk85 ),
.init ( !clock_locked ),
// cpu/chipset interface
.ch0_addr ( (downloading | loader_busy) ? loader_addr_mem : ppu_addr ),
.ch0_wr ( loader_write_mem | ppu_write ),
.ch0_din ( (downloading | loader_busy) ? loader_write_data_mem : ppu_dout ),
.ch0_rd ( ~(downloading | loader_busy) & ppu_read ),
.ch0_dout ( ppu_din ),
.ch1_addr ( cpu_addr ),
.ch1_wr ( cpu_write ),
.ch1_din ( cpu_dout ),
.ch1_rd ( cpu_read ),
.ch1_dout ( cpu_din ),
// reserved for backup ram save/load
.ch2_addr ( {4'b1111, save_addr} ),
.ch2_wr ( save_wr ),
.ch2_din ( sd_buff_dout ),
.ch2_rd ( save_rd ),
.ch2_dout ( save_dout ),
.ch2_busy ( save_busy )
);
wire [7:0] save_dout;
assign sd_buff_din = bram_en ? eeprom_dout : save_dout;
wire [7:0] eeprom_dout;
dpram #(" ", 11) eeprom
(
.clock_a(clk85),
.address_a(bram_addr),
.data_a(bram_dout),
.wren_a(bram_write),
.q_a(bram_din),
.clock_b(clk),
.address_b({sd_lba[1:0],sd_buff_addr}),
.data_b(sd_buff_dout),
.wren_b(sd_buff_wr & sd_ack),
.q_b(eeprom_dout)
);
wire save_busy;
reg save_rd, save_wr;
reg save_wait;
reg [17:0] save_addr;
always @(posedge clk) begin
if(~save_busy & ~save_rd & ~save_wr) save_wait <= 0;
if(~bk_busy) begin
save_addr <= '1;
save_wait <= 0;
end
else if(sd_ack & ~save_busy ) begin
if(~bk_loading && (save_addr != {sd_lba[8:0], sd_buff_addr})) begin
save_rd <= 1;
save_addr <= {sd_lba[8:0], sd_buff_addr};
save_wait <= 1;
end
if(bk_loading && sd_buff_wr) begin
save_wr <= 1;
save_addr <= {sd_lba[8:0], sd_buff_addr};
save_wait <= 1;
end
end
if(~bk_busy | save_busy | bram_en) {save_rd, save_wr} <= 0;
end
reg bk_pending;
wire save_written;
always @(posedge clk) begin
if ((mapper_flags[25] || fds) && ~OSD_STATUS && save_written)
bk_pending <= 1'b1;
else if (bk_state)
bk_pending <= 1'b0;
end
wire downloading = ioctl_download && ~type_gg;
wire type_gg = &filetype;
///////////////////////////////////////////////////
wire hit_x = (9'h027 >= cycle && 9'h020 <= cycle);
wire hit_y = (9'h0D7 >= scanline && 9'hD0 <= scanline);
reg displayp;
reg [1:0] disksidepixel;
always @(posedge clk) begin
if (reset_nes) begin
disksidepixel <= 0;
displayp <= 0;
end else begin
if (swap_delay == {1'b0, ~clkcount[22:21]})
displayp = 1'b0;
if (swap_delay[2] || (fds_eject && fds_swap_invert))
displayp = 1'b1;
if (hit_x && hit_y && displayp)
disksidepixel[0] <= 1'b1;
else
disksidepixel[0] <= 1'b0;
disksidepixel[1] <= ((cycle[0] == 1'b1) && (cycle[2:1] <= diskside));
end
end
///////////////////////////////////////////////////
wire [2:0] scale = status[3:1];
wire [2:0] sl = scale ? scale - 1'd1 : 3'd0;
assign VGA_SL = sl[1:0];
wire [1:0] reticle;
wire hold_reset;
wire ce_pix;
video video
(
.*,
.clk(clk),
.reset(reset_nes),
.cnt(nes_ce),
.hold_reset(hold_reset),
.count_v(scanline),
.count_h(cycle),
.forced_scandoubler(forced_scandoubler),
.scale(scale),
.hide_overscan(hide_overscan),
.palette(palette2_osd),
.emphasis(emphasis),
.reticle(displayp ? disksidepixel : ~status[22] ? reticle : 2'b00),
.pal_video(pal_video),
.ce_pix(ce_pix)
);
reg ce_out;
always @(posedge CLK_VIDEO) begin
reg old_clk;
old_clk <= clk;
ce_out <= 0;
if(old_clk & ~clk) ce_out <= ce_pix;
end
assign CE_PIXEL = ce_out;
//////////////////////////// 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) begin
gg_code[128] <= 1'b0;
if (ioctl_download & type_gg & loader_clk) begin
case (ioctl_addr[3:0])
0: gg_code[111:96] <= file_input; // Flags Bottom Word
1: gg_code[119:112] <= file_input; // Flags Bottom Word
2: gg_code[127:120] <= file_input; // Flags Top Word
3: gg_code[127:112] <= file_input; // Flags Top Word
4: gg_code[71:64] <= file_input; // Address Bottom Word
5: gg_code[79:72] <= file_input; // Address Bottom Word
6: gg_code[87:80] <= file_input; // Address Top Word
7: gg_code[95:88] <= file_input; // Address Top Word
8: gg_code[39:32] <= file_input; // Compare Bottom Word
9: gg_code[47:40] <= file_input; // Compare Bottom Word
10: gg_code[55:48] <= file_input; // Compare top Word
11: gg_code[63:56] <= file_input; // Compare top Word
12: gg_code[7:0] <= file_input; // Replace Bottom Word
13: gg_code[15:8] <= file_input; // Replace Bottom Word
14: gg_code[23:16] <= file_input; // Replace Top Word
15: begin
gg_code[31:24] <= file_input; // Replace Top Word
gg_code[128] <= 1'b1; // Clock it in
end
endcase
end
end
///////////////////////// STATE SAVE/LOAD /////////////////////////////
reg bk_ena = 0;
reg old_downloading = 0;
reg [1:0] last_diskside = 2'd3;
always @(posedge clk) begin
old_downloading <= downloading;
if(~old_downloading & downloading) bk_ena <= 0;
//Save file always mounted in the end of downloading state.
if(downloading && img_mounted && !img_readonly) bk_ena <= 1;
if(~bk_ena && loader_write_triggered) last_diskside <= loader_addr_mem[17:16];
end
wire bk_load = status[6];
wire bk_save = status[7] | (bk_pending & OSD_STATUS && status[17]);
reg bk_loading = 0;
reg bk_loading_req = 0;
reg bk_request = 0;
wire bk_busy = (bk_state == S_COPY);
reg fds_busy;
reg old_fds_btn;
reg [2:0] swap_delay;
reg [1:0] diskside_btn;
wire [8:0] save_sz = fds ? rom_sz[17:9] : bram_en ? 9'd3 : 9'd63;
wire [1:0] diskside_req_use = fds_swap_invert ? diskside_btn : diskside_req;
wire [1:0] next_btn_diskside = (last_diskside == diskside_btn) ? 2'd0 : diskside_btn + 2'd1;
typedef enum bit [1:0] { S_IDLE, S_COPY } mystate;
mystate bk_state = S_IDLE;
always @(posedge clk) begin
reg old_load = 0, old_save = 0, old_ack;
reg old_downloading = 0;
old_downloading <= downloading;
old_load <= bk_load & bk_ena;
old_save <= bk_save & bk_ena;
old_ack <= sd_ack;
fds_busy <= (bk_state != S_IDLE) || bk_request;
old_fds_btn <= fds_btn;
if(~old_ack & sd_ack) {sd_rd, sd_wr} <= 0;
if (swap_delay == {1'b1, clkcount[22:21]}) begin
swap_delay[2] <= 0;
end
if(~old_fds_btn & fds_btn & ~fds_busy & ~swap_delay[2]) diskside_btn <= next_btn_diskside;
if (downloading) begin
diskside <= 2'd0;
bk_state <= S_IDLE;
bk_request <= 0;
diskside_btn <= 2'd0;
end else if(bk_state == S_IDLE) begin
if((~old_load & bk_load) | (~old_save & bk_save)) begin
bk_loading <= bk_load;
bk_request <= 1;
end else if((diskside_req_use != diskside) && ~downloading && ~bk_request && fds) begin
diskside <= diskside_req_use;
swap_delay <= {1'b1, ~clkcount[22:21]};
end
if(old_downloading & ~downloading & |img_size & bk_ena) begin
bk_loading <= 1;
bk_request <= 1;
end
if (bk_request && !loader_busy) begin
bk_request <= 0;
bk_state <= S_COPY;
sd_lba <= 0;
sd_rd <= bk_loading;
sd_wr <= ~bk_loading;
end
end else begin
if(old_ack & ~sd_ack) begin
if(sd_lba[8:0] == save_sz) begin
bk_loading <= 0;
bk_state <= S_IDLE;
end else begin
sd_lba <= sd_lba + 1'd1;
sd_rd <= bk_loading;
sd_wr <= ~bk_loading;
end
end
end
end