forked from va7deo/zerowing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zerowing.sv
1761 lines (1441 loc) · 48.3 KB
/
zerowing.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 2021 Darren Olafson
//
// MiSTer Copyright (C) 2017 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.
//
//----------------------------------------------------------------------------
`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 [47: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
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,
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 {UART_RTS, UART_TXD, UART_DTR} = 0;
assign VGA_F1 = 0;
assign VGA_SCALER= 0;
assign HDMI_FREEZE = 0;
assign USER_OUT = '1;
assign AUDIO_MIX = 0;
//assign LED_USER = ioctl_download ;
assign LED_DISK = 0;
assign LED_POWER = 0;
assign BUTTONS = 0;
wire [1:0] aspect_ratio = status[2:1];
wire orientation = ~status[3];
wire [2:0] scan_lines = status[6:4];
// Status Bit Map:
// Upper Lower
// 0 1 2 3 4 5 6
// 01234567890123456789012345678901 23456789012345678901234567890123
// 0123456789ABCDEFGHIJKLMNOPQRSTUV 0123456789ABCDEFGHIJKLMNOPQRSTUV
assign VIDEO_ARX = (!aspect_ratio) ? (orientation ? 8'd4 : 8'd3) : (aspect_ratio - 1'd1);
assign VIDEO_ARY = (!aspect_ratio) ? (orientation ? 8'd3 : 8'd4) : 12'd0;
`include "build_id.v"
localparam CONF_STR = {
"Zero Wing;;",
"-;",
"P1,Video Settings;",
"P1-;",
"P1O12,Aspect ratio,Original,Full Screen,[ARC1],[ARC2];",
"P1O3,Orientation,Horz,Vert;",
"P1-;",
"P1O46,Scandoubler Fx,None,HQ2x,CRT 25%,CRT 50%,CRT 75%;",
"P1OOR,H-sync Adjust,0,1,2,3,4,5,6,7,-8,-7,-6,-5,-4,-3,-2,-1;",
"P1OSV,V-sync Adjust,0,1,2,3,4,5,6,7,-8,-7,-6,-5,-4,-3,-2,-1;",
"P1-;",
//"P1OB,Flip Screen,Off,On;",
"DIP;",
"-;",
"R0,Reset;",
"J1,Button 1,Button 2,Button 3,Start,Coin,Pause;",
"jn,A,B,X,R,L,Start;", // name mapping
"V,v",`BUILD_DATE
};
// CLOCKS
wire pll_locked;
wire clk_sys;
wire clk_70M;
reg clk_7M;
reg clk_10M; // 20MHz. 68k core needs twice freq
reg clk_14M;
assign SDRAM_CLK = clk_70M;
pll pll
(
.refclk(CLK_50M),
.rst(0),
.outclk_0(clk_sys), // 70
.outclk_1(clk_70M),
.locked(pll_locked)
);
reg [3:0] clk10_count;
reg [3:0] clk7_count;
reg [3:0] clk14_count;
always @ (posedge clk_sys ) begin
clk_10M <= 0;
case (clk10_count)
1: clk_10M <= 1;
3: clk_10M <= 1;
endcase
if ( clk10_count == 6 ) begin
clk10_count <= 0;
end else begin
clk10_count <= clk10_count + 1;
end
clk_7M <= ( clk7_count == 0);
if ( clk7_count == 9 ) begin
clk7_count <= 0;
end else begin
clk7_count <= clk7_count + 1;
end
clk_14M <= ( clk14_count == 0);
if ( clk14_count == 4 ) begin
clk14_count <= 0;
end else begin
clk14_count <= clk14_count + 1;
end
end
// 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
// Status bits
wire [31:0] status;
// Video settings
wire forced_scandoubler;
wire direct_video;
wire [3:0] hs_offset = status[27:24];
wire [3:0] vs_offset = status[31:28];
wire [9:0] sprite_adj_x = 0;
wire [9:0] sprite_adj_y = 0;
// Controls
wire [1:0] buttons;
wire [15:0] joy0, joy1;
wire [10:0] ps2_key;
wire [21:0] gamma_bus;
wire p1_up = joy0[3] | key_p1_up;
wire p1_down = joy0[2] | key_p1_down;
wire p1_left = joy0[1] | key_p1_left;
wire p1_right = joy0[0] | key_p1_right;
wire [2:0] p1_buttons = joy0[6:4] | {key_p1_c, key_p1_b, key_p1_a};
wire p2_up = joy1[3] | key_p2_up;
wire p2_down = joy1[2] | key_p2_down;
wire p2_left = joy1[1] | key_p2_left;
wire p2_right = joy1[0] | key_p2_right;
wire [2:0] p2_buttons = joy1[6:4] | {key_p2_c, key_p2_b, key_p2_a};
wire p1_start = joy0[7] | key_p1_start;
wire p2_start = joy1[7] | key_p2_start;
wire p1_coin = joy0[8] | key_p1_coin;
wire p2_coin = joy1[8] | key_p2_coin;
wire b_pause = joy0[9] | joy1[9];
// Keyboard handler
wire key_p1_start, key_p2_start, key_p1_coin, key_p2_coin;
wire key_test, key_reset, key_service;
wire key_p1_up, key_p1_left, key_p1_down, key_p1_right, key_p1_a, key_p1_b, key_p1_c;
wire key_p2_up, key_p2_left, key_p2_down, key_p2_right, key_p2_a, key_p2_b, key_p2_c;
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_p1_start <= pressed; // 1
'h01e: key_p2_start <= pressed; // 2
'h02E: key_p1_coin <= pressed; // 5
'h036: key_p2_coin <= pressed; // 6
'h006: key_test <= pressed; // F2
'h004: key_reset <= pressed; // F3
'h046: key_service <= pressed; // 9
'hX75: key_p1_up <= pressed; // up
'hX72: key_p1_down <= pressed; // down
'hX6b: key_p1_left <= pressed; // left
'hX74: key_p1_right <= pressed; // right
'h014: key_p1_a <= pressed; // lctrl
'h011: key_p1_b <= pressed; // lalt
'h029: key_p1_c <= pressed; // space
'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
endcase
end
end
// PAUSE SYSTEM
reg pause; // Pause signal (active-high)
reg pause_toggle = 1'b0; // User paused (active-high)
reg [31:0] pause_timer; // Time since pause
reg [31:0] pause_timer_dim = 31'h11E1A300; // Time until screen dim (10 seconds @ 48Mhz)
reg dim_video = 1'b0; // Dim video output (active-high)
// Pause when highscore module requires access, user has pressed pause, or OSD is open and option is set
assign pause = pause_toggle | (OSD_STATUS && ~status[7]);
assign dim_video = (pause_timer >= pause_timer_dim);
reg [1:0] adj_layer ;
reg [15:0] scroll_adj_x [3:0];
reg [15:0] scroll_adj_y [3:0];
reg layer_en [3:0];
reg old_pause;
always @(posedge clk_sys) begin
old_pause <= b_pause;
if (~old_pause & b_pause) begin
pause_toggle <= ~pause_toggle;
end
if (pause_toggle) begin
if (pause_timer < pause_timer_dim)
begin
pause_timer <= pause_timer + 1'b1;
end
end else begin
pause_timer <= 1'b0;
end
end
wire ioctl_download;
wire ioctl_upload;
wire ioctl_wr;
wire [15:0] ioctl_index;
wire [26:0] ioctl_addr;
wire [15:0] ioctl_dout;
wire [15:0] ioctl_din;
wire ioctl_wait;
//
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(forced_scandoubler),
.gamma_bus(gamma_bus),
.direct_video(direct_video),
.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)
);
reg ce_pix;
wire hbl;
wire vbl;
wire hsync;
wire vsync;
wire [8:0] hc;
wire [8:0] vc;
wire no_rotate = orientation | direct_video;
wire rotate_ccw = 1;
wire flip = 0;
screen_rotate screen_rotate (.*);
arcade_video #(320,24) arcade_video
(
.*,
.clk_video(clk_sys),
.ce_pix(clk_7M),
.RGB_in(rgb_out[23:0]),
.HBlank(hbl),
.VBlank(vbl),
.HSync(hsync),
.VSync(vsync),
.fx(scan_lines)
);
wire reset;
assign reset = RESET | status[0] | (ioctl_download & !ioctl_index) | buttons[1] | key_reset;
wire vid_clk = clk_7M;
video_timing video_timing (
.clk( vid_clk ), // pixel clock
.reset(reset), // reset
.hs_offset(hs_offset),
.vs_offset(vs_offset),
.hc(hc),
.vc(vc),
.hbl_delay(hbl),
.vbl(vbl),
.hsync(hsync),
.vsync(vsync)
);
reg [23:0] rgb_out;
// ===============================================================
// 68000 CPU
// ===============================================================
// clock generation
reg fx68_phi1 = 0;
wire fx68_phi2 = !fx68_phi1;
// phases for 68k clock
always @(posedge clk_sys) begin
if ( clk_10M == 1 ) begin
fx68_phi1 <= ~fx68_phi1;
end
end
// CPU outputs
wire cpu_rw ; // Read = 1, Write = 0
wire cpu_as_n ; // Address strobe
wire cpu_lds_n ; // Lower byte strobe
wire cpu_uds_n ; // Upper byte strobe
wire cpu_E;
wire vma_n ; // Valid peripheral memory address
wire [2:0]cpu_fc ; // Processor state
wire cpu_reset_n_o ; // Reset output signal
wire cpu_halted_n ; // Halt output
wire bg_n ; // Bus grant
// CPU busses
wire [15:0] cpu_dout ;
wire [23:0] cpu_a ;
reg [15:0] cpu_din ;
// CPU inputs
wire berr_n = 1'b1; // Bus error (never error)
reg dtack_n ;// = !vpa_n; // Data transfer ack (always ready)
wire vpa_n; // Valid peripheral address detected
reg cpu_br_n = 1'b1; // Bus request
reg bgack_n = 1'b1; // Bus grant ack
reg ipl0_n = 1'b1 ; // Interrupt request signals
reg ipl1_n = 1'b1;
reg ipl2_n ;//= 1'b1;
assign cpu_a[0] = 0; // odd memory address should cause cpu exception
fx68k fx68k (
// input
.clk( clk_10M ),
.enPhi1(fx68_phi1),
.enPhi2(fx68_phi2),
.extReset(reset),
.pwrUp(reset),
// output
.eRWn(cpu_rw),
.ASn( cpu_as_n),
.LDSn(cpu_lds_n),
.UDSn(cpu_uds_n),
// .E(cpu_E),
.VMAn(vma_n),
.FC0(cpu_fc[0]),
.FC1(cpu_fc[1]),
.FC2(cpu_fc[2]),
.BGn(bg_n),
.oRESETn(cpu_reset_n_o),
.oHALTEDn(cpu_halted_n),
// input
.VPAn(cpu_as_n & ipl2_n), // autovector int ack needs VPA low and DTACK high
.DTACKn(dtack_n & ipl2_n), //
.BERRn(berr_n),
.BRn(cpu_br_n),
.BGACKn(bgack_n),
.IPL0n(ipl0_n),
.IPL1n(ipl1_n),
.IPL2n(ipl2_n),
// busses
.iEdb(cpu_din),
.oEdb(cpu_dout),
.eab(cpu_a[23:1])
);
always @ (posedge clk_sys) begin
// tell 68k to wait for valid data. 0=ready 1=wait
// always ack when it's not program rom
dtack_n <= prog_rom_cs ? !prog_rom_data_valid : 0;
// select cpu data input based on what is active
cpu_din <= prog_rom_cs ? prog_rom_data :
ram_cs ? ram_dout :
tile_palette_cs ? tile_palette_cpu_dout :
sprite_palette_cs ? sprite_palette_cpu_dout :
shared_ram_cs ? cpu_shared_dout :
tile_ofs_cs ? curr_tile_ofs :
sprite_ofs_cs ? curr_sprite_ofs :
tile_attr_cs ? cpu_tile_dout_attr :
tile_num_cs ? cpu_tile_dout_num :
sprite_0_cs ? sprite_0_dout :
sprite_1_cs ? sprite_1_dout :
sprite_2_cs ? sprite_2_dout :
sprite_3_cs ? sprite_3_dout :
sprite_size_cs ? sprite_size_cpu_dout :
frame_done_cs ? { 16 { vbl } } : // get vblank state
int_en_cs ? 16'hffff :
16'd0;
end
wire [15:0] cpu_shared_dout;
wire [7:0] z80_shared_dout;
reg [15:0] z80_a;
wire [15:0] z80_addr;
reg [7:0] z80_din;
wire [7:0] z80_dout;
wire z80_wr_n;
wire z80_rd_n;
wire IORQ_n;
wire MREQ_n;
wire z80_halt_n;
reg z80_wait_n;
reg z80_int;
always @ (posedge clk_sys) begin
if ( reset == 1 ) begin
z80_wait_n <= 0;
sound_wr <= 0 ;
end else if ( clk_7M == 1 ) begin
z80_wait_n <= 1;
if ( ioctl_download | ( z80_rd_n == 0 && sound_rom_1_data_valid == 0 && sound_rom_1_cs == 1 ) ) begin
// wait if rom is selected and data is not yet available
z80_wait_n <= 0;
end
if ( z80_rd_n == 0 ) begin
if ( sound_rom_1_cs ) begin
if ( sound_rom_1_data_valid ) begin
z80_din <= sound_rom_1_data;
end else begin
z80_wait_n <= 0;
end
end else if ( sound_ram_1_cs ) begin
z80_din <= z80_shared_dout;
end else if ( z80_p1_cs ) begin
z80_din <= { 1'b0, p1_buttons, p1_right, p1_left, p1_down, p1_up };
end else if ( z80_p2_cs ) begin
z80_din <= { 1'b0, p2_buttons, p2_right, p2_left, p2_down, p2_up };
end else if ( z80_dswa_cs ) begin
z80_din <= sw[0];
end else if ( z80_dswb_cs ) begin
z80_din <= sw[1];
end else if ( z80_tjump_cs ) begin
z80_din <= sw[3];
end else if ( z80_system_cs ) begin
z80_din <= { 1'b0, p2_start, p1_start, p2_coin, p1_coin, 1'b0, 1'b0, key_service };
end else if ( z80_sound0_cs ) begin
z80_din <= opl_dout;
end else begin
z80_din <= 8'h00;
end
end
sound_wr <= 0 ;
if ( z80_wr_n == 0 ) begin
if ( z80_sound0_cs | z80_sound1_cs ) begin
sound_data <= z80_dout;
sound_addr <= { 1'b0, z80_sound1_cs }; // opl3
// sound_addr <= z80_sound1_cs ; // opl2 is single bit address
sound_wr <= 1;
end
end
end
end
always @ (posedge clk_7M ) begin
z80_int <= ( vc[7] == 1 && hc[8:7] == 3 ) ;
end
reg [1:0] sound_addr ;
reg [7:0] sound_data ;
reg sound_wr;
wire [7:0] opl_dout;
wire opl_irq_n;
//wire [15:0] sample_from_opl_l;
//wire [15:0] sample_from_opl_r;
//reg [7:0] count_1us;
//always @ (posedge clk_sys) begin
// // every 70 clocks @ 70MHz
// if ( count_1us == 69 ) begin
// count_1us <= 0;
// end else begin
// count_1us <= count_1us + 1;
// end
//end
//
//wire ce_1us = ( count_1us == 0 );
assign AUDIO_S = 1'b1 ;
opl3_intf opl
(
.clk(clk_7M),
.clk_opl(clk_14M),
.rst_n(~reset),
.irq_n(opl_irq_n),
.addr(sound_addr),
.din(sound_data),
.dout(opl_dout),
.we(sound_wr),
.rd(z80_sound0_cs & ~z80_rd_n ),
.sample_l(AUDIO_L),
.sample_r(AUDIO_R)
);
T80pa u_cpu(
.RESET_n ( ~reset ),
.CLK ( clk_7M ),
.CEN_p ( 1'b1 ),
.CEN_n ( 1'b1 ),
.WAIT_n ( z80_wait_n ), // don't wait if data is valid or rom access isn't selected
.INT_n ( opl_irq_n ), // opl timer
.NMI_n ( 1'b1 ),
.BUSRQ_n ( 1'b1 ),
.RD_n ( z80_rd_n ),
.WR_n ( z80_wr_n ),
.A ( z80_addr ),
.DI ( z80_din ),
.DO ( z80_dout ),
// unused
.DIRSET ( 1'b0 ),
.DIR ( 212'b0 ),
.OUT0 ( 1'b0 ),
.RFSH_n (),
.IORQ_n ( IORQ_n ),
.M1_n (),
.BUSAK_n (),
.HALT_n ( z80_halt_n ),
.MREQ_n ( MREQ_n ),
.Stop (),
.REG ()
);
// Chip select mux
wire prog_rom_cs;
wire scroll_ofs_x_cs;
wire scroll_ofs_y_cs;
wire ram_cs;
wire vblank_cs;
wire int_en_cs;
wire tile_ofs_cs;
wire tile_attr_cs;
wire tile_num_cs;
wire scroll_cs;
wire shared_ram_cs;
wire frame_done_cs; // word
wire tile_palette_cs;
wire sprite_palette_cs ;
wire sprite_ofs_cs;
wire sprite_cs; // *** offset needs to be auto-incremented
wire sprite_size_cs; // *** offset needs to be auto-incremented
wire z80_p1_cs;
wire z80_p2_cs;
wire z80_dswa_cs;
wire z80_dswb_cs;
wire z80_system_cs;
wire z80_tjump_cs;
wire z80_sound0_cs;
wire z80_sound1_cs;
// Select PCB Title and set chip select lines
reg [7:0] pcb;
wire tile_priority_type;
wire [15:0] scroll_y_offset;
always @(posedge clk_sys)
if (ioctl_wr && (ioctl_index==1))
pcb <= ioctl_dout;
chip_select cs (.*);
wire sprite_0_cs = ( curr_sprite_ofs[1:0] == 2'b00 ) & sprite_cs ;
wire sprite_1_cs = ( curr_sprite_ofs[1:0] == 2'b01 ) & sprite_cs ;
wire sprite_2_cs = ( curr_sprite_ofs[1:0] == 2'b10 ) & sprite_cs ;
wire sprite_3_cs = ( curr_sprite_ofs[1:0] == 2'b11 ) & sprite_cs ;
wire sound_rom_1_cs = ( MREQ_n == 0 && z80_addr <= 16'h7fff ) ;
wire sound_ram_1_cs = ( MREQ_n == 0 && z80_addr >= 16'h8000 && z80_addr <= 16'h87ff ) ;
reg int_en ;
reg int_ack ;
reg [1:0] vbl_sr;
// vblank interrupt on rising vbl
always @ (posedge clk_sys ) begin
if ( reset == 1 ) begin
ipl2_n <= 1 ;
int_ack <= 0;
end else begin
vbl_sr <= { vbl_sr[0], vbl };
if ( clk_10M == 1 ) begin
int_ack <= ( cpu_as_n == 0 ) && ( cpu_fc == 3'b111 ); // cpu acknowledged the interrupt
end
if ( vbl_sr == 2'b01 ) begin // rising edge
ipl2_n <= ~int_en;
end else if ( int_ack == 1 || vbl_sr == 2'b10 ) begin
ipl2_n <= 1 ;
end
end
end
reg [15:0] scroll_x [3:0] ;
reg [15:0] scroll_y [3:0] ;
reg [15:0] scroll_x_latch [3:0] ;
reg [15:0] scroll_y_latch [3:0] ;
reg inc_sprite_ofs;
always @ (posedge clk_sys) begin
if ( reset == 1 ) begin
// scroll_x[ 0 ] = 16'h01ed ;
// scroll_x[ 1 ] = 16'h01ef ;
// scroll_x[ 2 ] = 16'h01f1 ;
// scroll_x[ 3 ] = 16'h01f3 ;
//
// scroll_y[ 0 ] = 16'h00ef ;
// scroll_y[ 1 ] = 16'h00ef ;
// scroll_y[ 2 ] = 16'h00ef ;
// scroll_y[ 3 ] = 16'h00ef ;
//
// scroll_ofs_x <= 16'h01b7;
// scroll_ofs_y <= 16'h0102;
int_en <= 0;
end else begin
// write asserted and rising cpu clock
if ( clk_10M == 1 && cpu_rw == 0 ) begin
if ( tile_ofs_cs ) begin
curr_tile_ofs <= cpu_dout;
end
if ( int_en_cs & !cpu_rw ) begin
int_en <= cpu_dout[0];
end
if ( sprite_ofs_cs ) begin
// mask out valid range
curr_sprite_ofs <= { 6'b0, cpu_dout[9:0] };
end
if ( scroll_ofs_x_cs ) begin
scroll_ofs_x <= cpu_dout;
end
if ( scroll_ofs_y_cs ) begin
scroll_ofs_y <= cpu_dout;
end
// x layer values are even addresses
if ( scroll_cs ) begin
if ( cpu_a[1] == 0 ) begin
scroll_x[ cpu_a[3:2] ] <= cpu_dout[15:7] ;
end else begin
scroll_y[ cpu_a[3:2] ] <= cpu_dout[15:7] ;
end
end
// offset needs to be auto-incremented
if ( ( sprite_cs | sprite_size_cs ) & !cpu_rw ) begin
inc_sprite_ofs <= 1;
// curr_sprite_ofs <= curr_sprite_ofs + 1;
end
end
// write lasts multiple cpu clocks so limit to one increment per write signal
if ( inc_sprite_ofs == 1 && cpu_rw == 1 ) begin
curr_sprite_ofs <= curr_sprite_ofs + 1;
inc_sprite_ofs <= 0;
end
end
end
reg [15:0] scroll_x_total [3:0];
reg [15:0] scroll_y_total [3:0];
wire [15:0] ram_dout;
wire [9:0] tile_palette_addr;
wire [15:0] tile_palette_cpu_dout;
wire [15:0] tile_palette_dout;
wire [9:0] sprite_palette_addr;
wire [15:0] sprite_palette_cpu_dout;
wire [15:0] sprite_palette_dout;
reg [15:0] curr_tile_ofs;
reg [15:0] curr_sprite_ofs;
reg [15:0] scroll_ofs_x;
reg [15:0] scroll_ofs_y;
wire [15:0] cpu_tile_dout_attr;
wire [15:0] cpu_tile_dout_num;
wire [15:0] sprite_0_dout;
wire [15:0] sprite_1_dout;
wire [15:0] sprite_2_dout;
wire [15:0] sprite_3_dout;
wire [15:0] sprite_size_dout;
wire [15:0] sprite_size_cpu_dout;
wire [31:0] tile_attr_dout;
wire [15:0] sprite_attr_0_dout;
wire [15:0] sprite_attr_1_dout;
wire [15:0] sprite_attr_2_dout;
wire [15:0] sprite_attr_3_dout;
wire [15:0] sprite_size_buf_dout;
wire [15:0] sprite_attr_0_buf_dout ;
wire [15:0] sprite_attr_1_buf_dout ;
wire [15:0] sprite_attr_2_buf_dout ;
wire [15:0] sprite_attr_3_buf_dout ;
reg [15:0] sprite_buf_din;
reg [14:0] tile ;
reg [7:0] sprite_num;
reg [7:0] sprite_num_copy;
reg [3:0] tile_draw_state;
reg [1:0] layer; // 4 layers
wire [14:0] tile_idx = tile_attr[14:0] ;
wire [3:0] tile_priority = tile_attr[31:28] ;
wire [5:0] tile_palette_idx = tile_attr[21:16] ;
wire tile_hidden = tile_attr[15] ;
reg [15:0] fb_dout;
wire [15:0] tile_fb_out;
wire [15:0] sprite_fb_out;
reg [15:0] fb_din;
reg [15:0] sprite_fb_din ;
reg tile_fb_w;
reg sprite_fb_w;
reg sprite_buf_w;
reg sprite_size_buf_w;
ram1kx16dp tile_line_buffer (
.clock_a ( clk_sys ),
.address_a ( tile_fb_addr_w ),
.wren_a ( tile_fb_w ),
.data_a ( fb_din ),
.q_a ( ),
.clock_b ( clk_sys ),
.address_b ( fb_addr_r ),
.wren_b ( 0 ),
// .data_b ( ),
.q_b ( tile_fb_out )
);
ram1kx16dp sprite_line_buffer (
.clock_a ( clk_sys ),
.address_a ( sprite_fb_addr_w ),
.wren_a ( sprite_fb_w ),