forked from MiSTer-devel/GBA_MiSTer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGBA.sv
1463 lines (1244 loc) · 47.1 KB
/
GBA.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
//============================================================================
// GBA
// 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
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 ADC_BUS = 'Z;
assign {UART_RTS, UART_TXD, UART_DTR} = 0;
assign USER_OUT = '1;
assign AUDIO_S = 1;
assign AUDIO_MIX = status[8:7];
assign LED_USER = cart_download | bk_pending;
assign LED_DISK = 0;
assign LED_POWER = 0;
assign BUTTONS = 0;
assign VGA_SCALER = 0;
assign VGA_DISABLE = 0;
assign HDMI_FREEZE = 0;
assign {SD_SCK, SD_MOSI, SD_CS} = 'Z;
assign FB_EN = status[21] || status[22];
assign FB_FORMAT = 5'b00110;
assign FB_WIDTH = 12'd480;
assign FB_HEIGHT = 12'd320;
assign FB_STRIDE = 0;
assign FB_FORCE_BLANK = 0;
/////////////////////// CLOCK/RESET ///////////////////////////////////
wire pll_locked;
wire clk_sys;
pll pll
(
.refclk(CLK_50M),
.rst(0),
.outclk_0(clk_sys),
.outclk_1(CLK_VIDEO),
.locked(pll_locked)
);
wire reset = RESET | buttons[1] | status[0] | cart_download | bk_loading | hold_reset;
//////////////////////////// HPS I/O //////////////////////////////////
// Status Bit Map: (0..31 => "O", 32..63 => "o")
// 0 1 2 3 4 5 6
// 01234567890123456789012345678901 23456789012345678901234567890123
// 0123456789ABCDEFGHIJKLMNOPQRSTUV 0123456789ABCDEFGHIJKLMNOPQRSTUV
// X XXXXXXXXXRXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXX
`include "build_id.v"
parameter CONF_STR = {
"GBA;SS3E000000:80000;",
"FS,GBA,Load,300C0000;",
"-;",
"C,Cheats;",
"H1O[6],Cheats Enabled,Yes,No;",
"-;",
"D0R[12],Reload Backup RAM;",
"D0R[13],Save Backup RAM;",
"D0O[23],Autosave,Off,On;",
"D0-;",
"O[36],Savestates to SDCard,On,Off;",
"O[43],Autoincrement Slot,Off,On;",
"O[38:37],Savestate Slot,1,2,3,4;",
"h4H3R[17],Save state (Alt-F1);",
"h4H3R[18],Restore state (F1);",
"-;",
"P1,Video & Audio;",
"P1-;",
"P1O[33:32],Aspect ratio,Original,Full Screen,[ARC1],[ARC2];",
"P1O[4:2],Scandoubler Fx,None,HQ2x,CRT 25%,CRT 50%,CRT 75%;",
"P1O[35:34],Scale,Normal,V-Integer,Narrower HV-Integer,Wider HV-Integer;",
"P1-;",
"P1O[26:24],Modify Colors,Off,GBA 2.2,GBA 1.6,NDS 1.6,VBA 1.4,75%,50%,25%;",
"P1-;",
"P1O[39],Sync core to video,On,Off;",
"P1O[10:9],Flickerblend,Off,Blend,30Hz;",
"P1O[22:21],2XResolution,Off,Background,Sprites,Both;",
"P1O[20],Spritelimit,Off,On;",
"P1-;",
"P1O[8:7],Stereo Mix,None,25%,50%,100%;",
"P1O[19],Fast Forward Sound,On,Off;",
"P2,Hardware;",
"P2-;",
"H6P2O[31:29],Solar Sensor,0%,15%,30%,42%,55%,70%,85%,100%;",
"H2P2O[16],Turbo,Off,On;",
"P2O[28],Homebrew BIOS(Reset!),Off,On;",
"P3,Miscellaneous;",
"P3-;",
"P3O[15:14],Storage,Auto,SDRAM,DDR3;",
"D5P3O[5],Pause when OSD is open,Off,On;",
"P3O[27],Rewind Capture,Off,On;",
"P3-;",
"P3-,Only Romhacks or Crash!;",
"P3O[40],GPIO HACK(RTC+Rumble),Off,On;",
"P3O[42:41],Underclock CPU,0,1,2,3;",
"- ;",
"R0,Reset;",
"J1,A,B,L,R,Select,Start,FastForward,Rewind,Savestates;",
"jn,A,B,L,R,Select,Start,X,X;",
"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...;",
"V,v",`BUILD_DATE
};
wire [1:0] buttons;
wire [63:0] status;
wire [15:0] status_menumask = {~solar_quirk, status[27], cart_loaded, |cart_type, force_turbo, ~gg_active, ~bk_ena};
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 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 [15:0] joy_rumble;
wire [15:0] joy;
wire [15:0] joy_unmod;
wire [10:0] ps2_key;
wire [21:0] gamma_bus;
wire [15:0] sdram_sz;
wire [15:0] joystick_analog_0;
wire [32:0] RTC_time;
wire [63:0] status_in = cart_download ? {status[63:39],ss_slot,status[36:19],3'b000,status[15:0]} : {status[63:39],ss_slot,status[36:19],2'b00,status[16:0]};
hps_io #(.CONF_STR(CONF_STR), .WIDE(1)) hps_io
(
.clk_sys(clk_sys),
.HPS_BUS(HPS_BUS),
.buttons(buttons),
.forced_scandoubler(forced_scandoubler),
.joystick_0(joy_unmod),
.joystick_0_rumble(joy_rumble),
.ps2_key(ps2_key),
.status(status),
.status_in(status_in),
.status_set(cart_download | statusUpdate),
.status_menumask(status_menumask),
.info_req(ss_info_req),
.info(ss_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_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),
.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_0)
);
assign joy = joy_unmod[12] ? 16'b0 : joy_unmod;
////////////////////////// ROM DETECT /////////////////////////////////
reg code_download, bios_download, cart_download;
always @(posedge clk_sys) begin
code_download <= ioctl_download & &ioctl_index;
bios_download <= ioctl_download & !ioctl_index;
cart_download <= ioctl_download & ~&ioctl_index & |ioctl_index;
end
reg [26:0] last_addr;
reg flash_1m;
reg [1:0] cart_type;
reg cart_loaded = 0;
always @(posedge clk_sys) begin
reg [63:0] str;
reg old_download;
old_download <= cart_download;
if (old_download & ~cart_download) last_addr <= ioctl_addr;
if (~ioctl_download && ioctl_download_1 && ioctl_index == 1) begin
flash_1m <= 0;
cart_type <= ioctl_index[7:6];
cart_loaded <= 1;
end
if(rom_wr) begin
if({str, rom_dout[7:0]} == "FLASH1M_V") flash_1m <= 1;
if({str[55:0], rom_dout[7:0], rom_dout[15:8]} == "FLASH1M_V") flash_1m <= 1;
str <= {str[47:0], rom_dout[7:0], rom_dout[15:8]};
end
end
wire force_turbo = |cart_type;
reg [11:0] bios_wraddr;
reg [31:0] bios_wrdata;
reg bios_wr;
always @(posedge clk_sys) begin
bios_wr <= 0;
if(bios_download & ioctl_wr & ~status[28]) begin
if(~ioctl_addr[1]) bios_wrdata[15:0] <= ioctl_dout;
else begin
bios_wrdata[31:16] <= ioctl_dout;
bios_wraddr <= ioctl_addr[13:2];
bios_wr <= 1;
end
end
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_sys ),
.ps2_key (ps2_key[10:0] ),
.allow_ss (cart_loaded ),
.joySS (joy_unmod[12] ),
.joyRight (joy_unmod[0] ),
.joyLeft (joy_unmod[1] ),
.joyDown (joy_unmod[2] ),
.joyUp (joy_unmod[3] ),
.joyStart (joy_unmod[9] ),
.joyRewind (joy_unmod[11] ),
.rewindEnable (status[27] ),
.status_slot (status[38:37] ),
.autoincslot (status[43] ),
.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 = 27;
//////////////////////////// SYSTEM ///////////////////////////////////
wire save_eeprom, save_sram, save_flash, ss_loaded;
reg fast_forward, pause, cpu_turbo;
reg ff_latch;
always @(posedge clk_sys) begin : ffwd
reg last_ffw;
reg ff_was_held;
longint ff_count;
last_ffw <= joy[10];
if (joy[10])
ff_count <= ff_count + 1;
if (~last_ffw & joy[10]) begin
ff_latch <= 0;
ff_count <= 0;
end
if ((last_ffw & ~joy[10])) begin // 100mhz clock, 0.2 seconds
ff_was_held <= 0;
if (ff_count < 10000000 && ~ff_was_held) begin
ff_was_held <= 1;
ff_latch <= 1;
end
end
fast_forward <= (joy[10] | ff_latch) & ~force_turbo;
pause <= force_pause | (status[5] & OSD_STATUS & ~status[27]); // pause from "sync to core" or "pause in osd", but not if rewind capture is on
cpu_turbo <= ((status[16] & ~fast_forward) | force_turbo) & ~pause;
end
reg [79:0] time_dout = 41'd0;
wire [79:0] time_din;
assign time_din[42 + 32 +: 80 - (42 + 32)] = '0;
wire has_rtc;
wire cart_rumble;
reg RTC_load = 0;
reg [7:0] rumble_reg = 0;
always @(posedge clk_sys) begin
rumble_reg <= (cart_rumble ? 8'd128 : 8'd0);
end
assign joy_rumble = {8'd0, rumble_reg};
wire [15:0] GBA_AUDIO_L;
wire [15:0] GBA_AUDIO_R;
gba_top
#(
// assume: cart may have either flash or eeprom, not both! (need to verify)
.Softmap_GBA_FLASH_ADDR (0), // 131072 (8bit) -- 128 Kbyte Data for GBA Flash
.Softmap_GBA_EEPROM_ADDR (0), // 8192 (8bit) -- 8 Kbyte Data for GBA EEProm
.Softmap_GBA_WRam_ADDR (131072), // 65536 (32bit) -- 256 Kbyte Data for GBA WRam Large
.Softmap_GBA_Gamerom_ADDR(65536+131072), // 32MB of ROM
.Softmap_SaveState_ADDR (58720256), // 65536 (64bit) -- ~512kbyte Data for SaveState (separate memory)
.Softmap_Rewind_ADDR (33554432), // 65536 qwords*64 -- 64*512 Kbyte Data for Savestates
.turbosound('1) // sound buffer to play sound in turbo mode without sound pitched up
)
gba
(
.clk100(clk_sys),
.GBA_on(~reset && ~romcopy_active), // switching from off to on = reset
.GBA_lockspeed(~fast_forward), // 1 = 100% speed, 0 = max speed
.GBA_cputurbo(cpu_turbo),
.GBA_flash_1m(flash_1m), // 1 when string "FLASH1M_V" is anywhere in gamepak
.CyclePrecalc(pause ? 16'd0 : 16'd100), // 100 seems to be ok to keep fullspeed for all games
.Underclock(status[42:41]),
.MaxPakAddr(last_addr[26:2]), // max byte address that will contain data, required for buggy games that read behind their own memory, e.g. zelda minish cap
.CyclesMissing(), // debug only for speed measurement, keep open
.CyclesVsyncSpeed(), // debug only for speed measurement, keep open
.SramFlashEnable(~sram_quirk),
.memory_remap(memory_remap_quirk),
.increaseSSHeaderCount(!status[36]),
.save_state(ss_save),
.load_state(ss_load),
.interframe_blend(status[10:9]),
.maxpixels(status[20] | sprite_quirk),
.hdmode2x_bg(status[21]),
.hdmode2x_obj(status[22]),
.shade_mode(shadercolors),
.specialmodule(gpio_quirk | status[40]),
.solar_in(status[31:29]),
.tilt(tilt_quirk),
.rewind_on(status[27]),
.rewind_active(status[27] & joy[11]),
.savestate_number(ss_slot),
.RTC_timestampNew(RTC_time[32]),
.RTC_timestampIn(RTC_time[31:0]),
.RTC_timestampSaved(time_dout[42 +: 32]),
.RTC_savedtimeIn(time_dout[0 +: 42]),
.RTC_saveLoaded(RTC_load),
.RTC_timestampOut(time_din[42 +: 32]),
.RTC_savedtimeOut(time_din[0 +: 42]),
.RTC_inuse(has_rtc),
.cheat_clear(gg_reset),
.cheats_enabled(~status[6]),
.cheat_on(gg_valid),
.cheat_in(gg_code),
.cheats_active(gg_active),
.sdram_read_ena(sdram_req), // triggered once for read request
.sdram_read_done(sdram_ack), // must be triggered once when sdram_read_data is valid after last read
.sdram_read_addr(sdram_addr), // all addresses are DWORD addresses!
.sdram_read_data(sdram_dout1), // data from last request, valid when done = 1
.sdram_second_dword(sdram_dout2), // second dword to be read for buffering/prefetch. Must be valid 1 cycle after done = 1
.bus_out_Din(bus_din), // data read from WRam Large, SRAM/Flash/EEPROM
.bus_out_Dout(bus_dout), // data written to WRam Large, SRAM/Flash/EEPROM
.bus_out_Adr(bus_addr), // all addresses are DWORD addresses!
.bus_out_rnw(bus_rd), // read = 1, write = 0
.bus_out_ena(bus_req), // one cycle high for each action
.bus_out_done(bus_ack), // should be one cycle high when write is done or read value is valid
.SAVE_out_Din(ss_din), // data read from savestate
.SAVE_out_Dout(ss_dout), // data written to savestate
.SAVE_out_Adr(ss_addr), // all addresses are DWORD addresses!
.SAVE_out_rnw(ss_rnw), // read = 1, write = 0
.SAVE_out_ena(ss_req), // one cycle high for each action
.SAVE_out_be(ss_be),
.SAVE_out_done(ss_ack), // should be one cycle high when write is done or read value is valid
.save_eeprom(save_eeprom),
.save_sram(save_sram),
.save_flash(save_flash),
.load_done(ss_loaded),
.bios_wraddr(bios_wraddr),
.bios_wrdata(bios_wrdata),
.bios_wr(bios_wr),
.KeyA(joy[4]),
.KeyB(joy[5]),
.KeySelect(joy[8]),
.KeyStart(joy[9]),
.KeyRight(joy[0]),
.KeyLeft(joy[1]),
.KeyUp(joy[3]),
.KeyDown(joy[2]),
.KeyR(joy[7]),
.KeyL(joy[6]),
.AnalogTiltX(joystick_analog_0[7:0]),
.AnalogTiltY(joystick_analog_0[15:8]),
.Rumble(cart_rumble),
.pixel_out_addr(pixel_addr), // integer range 0 to 38399; -- address for framebuffer
.pixel_out_data(pixel_data), // RGB data for framebuffer
.pixel_out_we(pixel_we), // new pixel for framebuffer
.largeimg_out_base(FB_BASE),
.largeimg_out_addr(fb_addr),
.largeimg_out_data(fb_din),
.largeimg_out_req(fb_req),
.largeimg_out_done(fb_ack),
.largeimg_newframe(FB_VBL),
.largeimg_singlebuf(FB_LL),
.sound_out_left(GBA_AUDIO_L),
.sound_out_right(GBA_AUDIO_R)
);
assign AUDIO_L = (fast_forward && status[19]) ? 16'd0 : GBA_AUDIO_L;
assign AUDIO_R = (fast_forward && status[19]) ? 16'd0 : GBA_AUDIO_R;
//////////////////////////// QUIRKS //////////////////////////////////
reg sram_quirk = 0; // game tries to use SRAM as emulation detection. This bit forces to pretent we have no SRAM
reg memory_remap_quirk = 0; // game uses memory mirroring, e.g. access 4Mbyte but is only 1 Mbyte.
reg gpio_quirk = 0; // game exchanges some addresses to be GPIO lines for e.g. Solar or RTC
reg tilt_quirk = 0; // game exchanges some addresses to be Tilt module
reg solar_quirk = 0; // game has solar module
reg sprite_quirk = 0; // game depends on using max sprite pixels per line, otherwise glitches appear
always @(posedge clk_sys) begin
reg [31:0] cart_id;
if (~ioctl_download && ioctl_download_1 && ioctl_index == 1) begin
sram_quirk <= 0;
memory_remap_quirk <= 0;
gpio_quirk <= 0;
tilt_quirk <= 0;
solar_quirk <= 0;
sprite_quirk <= 0;
end
if(rom_wr) begin
if(rom_addr[26:4] == 'hA) begin
if(rom_addr[3:0] >= 12) cart_id[{4'd14 - rom_addr[3:0], 3'd0} +:16] <= {rom_dout[7:0],rom_dout[15:8]};
end
if(rom_addr == 'hB0) begin
if(cart_id[31:8] == "AR8" ) begin sram_quirk <= 1; end // Rocky US
if(cart_id[31:8] == "ARO" ) begin sram_quirk <= 1; end // Rocky EU
if(cart_id[31:8] == "ALG" ) begin sram_quirk <= 1; end // Dragon Ball Z - The Legacy of Goku
if(cart_id[31:8] == "ALF" ) begin sram_quirk <= 1; end // Dragon Ball Z - The Legacy of Goku II
if(cart_id[31:8] == "BLF" ) begin sram_quirk <= 1; end // 2 Games in 1 - Dragon Ball Z - The Legacy of Goku I & II
if(cart_id[31:8] == "BDB" ) begin sram_quirk <= 1; end // Dragon Ball Z - Taiketsu
if(cart_id[31:8] == "BG3" ) begin sram_quirk <= 1; end // Dragon Ball Z - Buu's Fury
if(cart_id[31:8] == "BDV" ) begin sram_quirk <= 1; end // Dragon Ball Z - Advanced Adventure
if(cart_id[31:8] == "A2Y" ) begin sram_quirk <= 1; end // Top Gun - Combat Zones
if(cart_id[31:8] == "AI2" ) begin sram_quirk <= 1; end // Iridion II
if(cart_id[31:8] == "BPE" ) begin gpio_quirk <= 1; end // POKEMON Emerald
if(cart_id[31:8] == "AXV" ) begin gpio_quirk <= 1; end // POKEMON Ruby
if(cart_id[31:8] == "AXP" ) begin gpio_quirk <= 1; end // POKEMON Sapphire
if(cart_id[31:8] == "RZW" ) begin gpio_quirk <= 1; end // WarioWare Twisted
if(cart_id[31:8] == "BKA" ) begin gpio_quirk <= 1; end // Sennen Kazoku
if(cart_id[31:8] == "BR4" ) begin gpio_quirk <= 1; end // Rockman EXE 4.5
if(cart_id[31:8] == "V49" ) begin gpio_quirk <= 1; end // Drill Dozer
if(cart_id[31:8] == "2GB" ) begin gpio_quirk <= 1; end // Goodboy Galaxy
if(cart_id[31:8] == "BHG" ) begin sprite_quirk <= 1; end // Gunstar Super Heroes
if(cart_id[31:8] == "BGX" ) begin sprite_quirk <= 1; end // Gunstar Super Heroes
if(cart_id[31:8] == "KHP" ) begin tilt_quirk <= 1; end // Koro Koro Puzzle JP
if(cart_id[31:8] == "KYG" ) begin tilt_quirk <= 1; end // Yoshi's Topsy-Turvy
if(cart_id[31:8] == "U3I" ) begin gpio_quirk <= 1; solar_quirk <= 1; end // Boktai 1
if(cart_id[31:8] == "U32" ) begin gpio_quirk <= 1; solar_quirk <= 1; end // Boktai 2
if(cart_id[31:8] == "U33" ) begin gpio_quirk <= 1; solar_quirk <= 1; end // Boktai 3
if(cart_id == "FBME") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Classic NES Series Bomberman
if(cart_id == "FADE") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Classic NES Series Castlevania
if(cart_id == "FDKE") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Classic NES Series Donkey Kong
if(cart_id == "FDME") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Classic NES Series DR. MARIO
if(cart_id == "FEBE") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Classic NES Series EXCITEBIKE
if(cart_id == "FICE") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Classic NES Series ICE CLIMBER
if(cart_id == "FMRE") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Classic NES Series NES METROID
if(cart_id == "FP7E") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Classic NES Series PAC-MAN
if(cart_id == "FSME") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Classic NES Series SUPER MARIO Bros
if(cart_id == "FZLE") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Classic NES Series The Legend of Zelda
if(cart_id == "FXVE") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Classic NES Series XEVIOUS
if(cart_id == "FLBE") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Classic NES Series Zelda II - The Adventure of Link
if(cart_id == "FSRJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini - Dai-2-ji Super Robot Taisen (Japan) (Promo)
if(cart_id == "FGZJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini - Kidou Senshi Z Gundam - Hot Scramble (Japan) (Promo)
if(cart_id == "FSDJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; sprite_quirk <= 1; end // Famicom Mini 30 - SD Gundam World - Gachapon Senshi Scramble Wars
if(cart_id == "FADJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; sprite_quirk <= 1; end // Famicom Mini 29 - Akumajou Dracula
if(cart_id == "FTUJ") begin sram_quirk <= 1; sprite_quirk <= 1; end // Famicom Mini 28 - Famicom Tantei Club Part II - Ushiro ni Tatsu Shoujo - Zen, Kouhen
if(cart_id == "FTKJ") begin sram_quirk <= 1; sprite_quirk <= 1; end // Famicom Mini 27 - Famicom Tantei Club - Kieta Koukeisha - Zen, Kouhen
if(cart_id == "FFMJ") begin sram_quirk <= 1; sprite_quirk <= 1; end // Famicom Mini 26 - Famicom Mukashibanashi - Shin Onigashima - Zen, Kouhen
if(cart_id == "FLBJ") begin sram_quirk <= 1; sprite_quirk <= 1; end // Famicom Mini 25 - The Legend of Zelda 2 - Link no Bouken
if(cart_id == "FPTJ") begin sram_quirk <= 1; sprite_quirk <= 1; end // Famicom Mini 24 - Hikari Shinwa - Palthena no Kagami
if(cart_id == "FMRJ") begin sram_quirk <= 1; sprite_quirk <= 1; end // Famicom Mini 23 - Metroid
if(cart_id == "FNMJ") begin sram_quirk <= 1; sprite_quirk <= 1; end // Famicom Mini 22 - Nazo no Murasame Jou
if(cart_id == "FM2J") begin sram_quirk <= 1; memory_remap_quirk <= 1; sprite_quirk <= 1; end // Famicom Mini 21 - Super Mario Bros. 2
if(cart_id == "FGGJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 20 - Ganbare Goemon! - Karakuri Douchuu
if(cart_id == "FTWJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 19 - Twin Bee
if(cart_id == "FMKJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 18 - Makaimura
if(cart_id == "FTBJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 17 - Takahashi Meijin no Bouken-jima
if(cart_id == "FDDJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 16 - Dig Dug
if(cart_id == "FDMJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 15 - Dr. Mario
if(cart_id == "FWCJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 14 - Wrecking Crew
if(cart_id == "FVFJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 13 - Balloon Fight
if(cart_id == "FCLJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 12 - Clu Clu Land
if(cart_id == "FMBJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 11 - Mario Bros.
if(cart_id == "FSOJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 10 - Star Soldier
if(cart_id == "FBMJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 09 - Bomber Man
if(cart_id == "FMPJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 08 - Mappy
if(cart_id == "FXVJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 07 - Xevious
if(cart_id == "FPMJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 06 - Pac-Man
if(cart_id == "FZLJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 05 - Zelda no Densetsu 1 - The Hyrule Fantasy
if(cart_id == "FEBJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 04 - Excitebike
if(cart_id == "FICJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 03 - Ice Climber
if(cart_id == "FDKJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 02 - Donkey Kong
if(cart_id == "FSMJ") begin sram_quirk <= 1; memory_remap_quirk <= 1; end // Famicom Mini 01 - Super Mario Bros.
end
end
end
//////////////////////////// MEMORY ///////////////////////////////////
localparam ROM_START = (65536+131072)*4;
reg sdram_en;
always @(posedge clk_sys) begin
if(reset) sdram_en <= (!status[15:14]) ? |sdram_sz[2:0] : status[14];
if (sdram_sz[1:0] == 2'b01 && last_addr[25]) sdram_en <= 1'b0; // use ddr3 if game is 32mbyte in size
end
wire [25:2] sdram_addr;
wire [31:0] sdram_dout1 = sdram_en ? sdr_sdram_dout1 : ddr_sdram_dout1;
wire [31:0] sdram_dout2 = sdram_en ? sdr_sdram_dout2 : ddr_sdram_dout2;
wire sdram_ack = sdram_en ? sdr_sdram_ack : ddr_sdram_ack;
wire sdram_req;
wire [25:2] bus_addr;
wire [31:0] bus_din;
wire [31:0] bus_dout = sdram_en ? sdr_bus_dout : ddr_bus_dout;
wire bus_ack = sdram_en ? sdr_bus_ack : ddr_bus_ack;
wire bus_rd, bus_req;
wire [31:0] sdr_sdram_dout1, sdr_sdram_dout2, sdr_bus_dout;
wire [15:0] sdr_bram_din;
wire sdr_sdram_ack, sdr_bus_ack, sdr_bram_ack;
wire [27:2] fb_addr;
wire [63:0] fb_din;
wire fb_req;
wire fb_ack;
sdram sdram
(
.*,
.init(~pll_locked),
.clk(clk_sys),
.ch1_addr({sdram_addr, 1'b0}),
.ch1_din(16'b0),
.ch1_dout({sdr_sdram_dout2, sdr_sdram_dout1}),
.ch1_req(sdram_req & sdram_en),
.ch1_rnw(cart_download ? 1'b0 : 1'b1 ),
.ch1_ready(sdr_sdram_ack),
.ch2_addr(romcopysd_active ? romcopy_writepos[26:1]+ROM_START[26:1] : {bus_addr, 1'b0}),
.ch2_din(romcopysd_active ? romcopy_data : bus_din),
.ch2_dout(sdr_bus_dout),
.ch2_req(romcopysd_active ? romcopysd_req : ~cart_download & bus_req & sdram_en),
.ch2_rnw(romcopysd_active ? 1'b0 : bus_rd),
.ch2_ready(sdr_bus_ack),
.ch3_addr({sd_lba[7:0],bram_addr}),
.ch3_din(bram_dout),
.ch3_dout(sdr_bram_din),
.ch3_req(bram_req & sdram_en),
.ch3_rnw(~bk_loading || extra_data_addr),
.ch3_ready(sdr_bram_ack)
);
always @(posedge clk_sys) begin
if(cart_download) begin
if(ioctl_wr) ioctl_wait <= 1;
if(sdram_ack) ioctl_wait <= 0;
end
else ioctl_wait <= 0;
end
wire [31:0] ddr_sdram_dout1, ddr_sdram_dout2, ddr_bus_dout;
wire [15:0] ddr_bram_din;
wire ddr_sdram_ack, ddr_bus_ack, ddr_bram_ack;
wire [63:0] ss_dout, ss_din;
wire [27:2] ss_addr;
wire [7:0] ss_be;
wire ss_rnw, ss_req, ss_ack;
assign DDRAM_CLK = clk_sys;
ddram ddram
(
.*,
.ch1_addr(romcopy_active ? romcopy_readpos[26:1]+ROM_START[26:1] : {sdram_addr, 1'b0}),
.ch1_din(16'b0),
.ch1_dout({ddr_sdram_dout2, ddr_sdram_dout1}),
.ch1_req(romcopy_active ? romcopy_ddrreq : sdram_req & ~sdram_en),
.ch1_rnw(1'b1),
.ch1_ready(ddr_sdram_ack),
.ch2_addr({bus_addr, 1'b0}),
.ch2_din(bus_din),
.ch2_dout(ddr_bus_dout),
.ch2_req(~cart_download & bus_req & ~sdram_en),
.ch2_rnw(bus_rd),
.ch2_ready(ddr_bus_ack),
.ch3_addr({sd_lba[7:0],bram_addr}),
.ch3_din(bram_dout),
.ch3_dout(ddr_bram_din),
.ch3_req(bram_req & ~sdram_en),
.ch3_rnw(~bk_loading || extra_data_addr),
.ch3_ready(ddr_bram_ack),
.ch4_addr({ss_addr, 1'b0}),
.ch4_din(ss_din),
.ch4_dout(ss_dout),
.ch4_req(ss_req),
.ch4_rnw(ss_rnw),
.ch4_be(ss_be),
.ch4_ready(ss_ack),
.ch5_addr({fb_addr, 1'b0}),
.ch5_din(gamma_en ? fb_gamma_din : fb_din),
.ch5_req(fb_ddr3_req),
.ch5_ready(fb_ack)
);
// gamma for 2x rendering
(* ramstyle="no_rw_check" *) reg [7:0] gamma_curve_r1[256];
(* ramstyle="no_rw_check" *) reg [7:0] gamma_curve_g1[256];
(* ramstyle="no_rw_check" *) reg [7:0] gamma_curve_b1[256];
(* ramstyle="no_rw_check" *) reg [7:0] gamma_curve_r2[256];
(* ramstyle="no_rw_check" *) reg [7:0] gamma_curve_g2[256];
(* ramstyle="no_rw_check" *) reg [7:0] gamma_curve_b2[256];
wire gamma_en = gamma_bus[19];
wire gamma_wr = gamma_bus[18];
wire [9:0] gamma_wr_addr = gamma_bus[17:8];
wire [7:0] gamma_value = gamma_bus[7:0];
reg [63:0] fb_gamma_din;
reg fb_ddr3_req;
wire [7:0] gamma_index_r1 = fb_din[ 7: 0];
wire [7:0] gamma_index_g1 = fb_din[15: 8];
wire [7:0] gamma_index_b1 = fb_din[23:16];
wire [7:0] gamma_index_r2 = fb_din[39:32];
wire [7:0] gamma_index_g2 = fb_din[47:40];
wire [7:0] gamma_index_b2 = fb_din[55:48];
always @(posedge clk_sys) begin
if (gamma_wr) begin
case(gamma_wr_addr[9:8])
0: begin gamma_curve_r1[gamma_wr_addr[7:0]] <= gamma_value; gamma_curve_r2[gamma_wr_addr[7:0]] <= gamma_value; end
1: begin gamma_curve_g1[gamma_wr_addr[7:0]] <= gamma_value; gamma_curve_g2[gamma_wr_addr[7:0]] <= gamma_value; end
2: begin gamma_curve_b1[gamma_wr_addr[7:0]] <= gamma_value; gamma_curve_b2[gamma_wr_addr[7:0]] <= gamma_value; end
endcase
end
fb_gamma_din[ 7: 0] <= gamma_curve_r1[gamma_index_r1];
fb_gamma_din[15: 8] <= gamma_curve_g1[gamma_index_g1];
fb_gamma_din[23:16] <= gamma_curve_b1[gamma_index_b1];
fb_gamma_din[31:24] <= 8'd0;
fb_gamma_din[39:32] <= gamma_curve_r2[gamma_index_r2];
fb_gamma_din[47:40] <= gamma_curve_g2[gamma_index_g2];
fb_gamma_din[55:48] <= gamma_curve_b2[gamma_index_b2];
fb_gamma_din[63:56] <= 8'd0;
fb_ddr3_req <= fb_req;
end
///////////////// copy rom data from ddrram to sdram
localparam STATE_ROMCOPY_IDLE = 0;
localparam STATE_ROMCOPY_READ = 1;
localparam STATE_ROMCOPY_WAIT = 2;
localparam STATE_ROMCOPY_ACK = 3;
reg[1:0] romcopy_state = 0;
reg romcopy_active = 0;
reg[26:0] romcopy_size;
reg[26:0] romcopy_readpos;
reg romcopy_ddrreq = 0;
always @(posedge clk_sys) begin
romcopy_ddrreq <= 0;
case (romcopy_state)
STATE_ROMCOPY_IDLE : begin
if (~ioctl_download && ioctl_download_1 && ioctl_index == 1) begin
romcopy_state <= STATE_ROMCOPY_READ;
romcopy_size <= ioctl_addr;
romcopy_readpos <= 0;
romcopy_active <= 1;
end
end
STATE_ROMCOPY_READ : begin
if (romcopy_readpos >= romcopy_size) begin
romcopy_state <= STATE_ROMCOPY_IDLE;
romcopy_active <= 0;
end else begin
romcopy_ddrreq <= 1;
romcopy_state <= STATE_ROMCOPY_WAIT;
end
end
STATE_ROMCOPY_WAIT : begin
if (ddr_sdram_ack) begin
romcopy_readpos <= romcopy_readpos + 4'd8;
romcopy_state <= STATE_ROMCOPY_ACK;
end
end
STATE_ROMCOPY_ACK : begin
if (romcopysd_state == STATE_ROMCOPYSD_IDLE) begin
romcopy_state <= STATE_ROMCOPY_READ;
end
end
endcase
end
localparam STATE_ROMCOPYSD_IDLE = 0;
localparam STATE_ROMCOPYSD_WAIT1 = 1;
localparam STATE_ROMCOPYSD_WAIT2 = 2;
reg[1:0] romcopysd_state = 0;
reg[26:0] romcopy_writepos;
reg romcopysd_active = 0;
reg romcopysd_req = 0;
reg[31:0] romcopy_data;
reg[31:0] romcopy_datanext;
reg [2:0] rom_state;
reg [26:0] rom_addr;
reg [26:0] rom_addrnext;
reg [15:0] rom_dout;
reg [63:0] rom_data;
reg rom_wr = 0;
always @(posedge clk_sys) begin
romcopysd_req <= 0;
case (romcopysd_state)
STATE_ROMCOPYSD_IDLE : begin
if (ioctl_download && ~ioctl_download_1 && ioctl_index == 1) begin
romcopy_writepos <= 0;
end
if (romcopy_state == STATE_ROMCOPY_ACK) begin
romcopysd_state <= STATE_ROMCOPYSD_WAIT1;
romcopysd_active <= 1;
romcopysd_req <= sdram_en;
romcopy_data <= ddr_sdram_dout1;
romcopy_datanext <= ddr_sdram_dout2;
rom_state <= 4;
rom_data <= {ddr_sdram_dout2, ddr_sdram_dout1};
rom_addrnext <= romcopy_writepos;
end
end
STATE_ROMCOPYSD_WAIT1 : begin
if (sdr_bus_ack || ~sdram_en) begin
romcopy_writepos <= romcopy_writepos + 3'd4;
romcopysd_state <= STATE_ROMCOPYSD_WAIT2;
romcopysd_req <= sdram_en;
romcopy_data <= romcopy_datanext;
end