forked from Kitrinx/Atari7800_MiSTer
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Atari7800.sv
1398 lines (1208 loc) · 39.1 KB
/
Atari7800.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
`default_nettype none
// k7800 (c) by Jamie Blanks
// k7800 is licensed under a
// Creative Commons Attribution-NonCommercial 4.0 International License.
// You should have received a copy of the license along with this
// work. If not, see http://creativecommons.org/licenses/by-nc/4.0/.
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 BUTTONS = 0;
assign {UART_RTS, UART_TXD, UART_DTR} = 0;
assign AUDIO_S = 0;
assign AUDIO_MIX = status[3:2];
assign LED_USER = cart_download | bk_state | bk_pending;
assign LED_DISK = 0;
assign LED_POWER = 0;
assign VGA_SCALER = 0;
assign VGA_DISABLE = 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;
/////////////////////// CLOCK/RESET ///////////////////////////////////
wire clock_locked;
wire clk_vid;
wire clk_sys;
wire clk_tia;
pll pll
(
.refclk(CLK_50M),
.rst(0),
.outclk_0(clk_sys),
.outclk_1(clk_vid),
.outclk_2(clk_tia),
.locked(clock_locked)
);
// 7.1590909 = NTSC pixel clock
// 14.318189 = NTSC Master clock
// 7.093788 = PAL Pixel Clock // Skip every 109 cycles?
// 14.187576 = PAL Master Clock
logic reset;
logic use_tape;
always @(posedge clk_vid) begin
if (status[48])
use_tape <= 1;
if (cart_download)
use_tape <= 0;
reset <= RESET | buttons[1] | status[0] | cart_download | bios_download || status[48];
end
wire cart_download = ioctl_download & (ioctl_index[5:0] == 6'd1);
wire bios_download = ioctl_download & ((ioctl_index[5:0] == 6'd0) && (ioctl_index[7:6] == 0)) || (ioctl_index[5:0] == 2);
wire pal_download = ioctl_download & (ioctl_index[5:0] == 6'd3);
reg old_cart_download;
//////////////////////////// HPS I/O //////////////////////////////////
// Status Bit Map:
// 0 1 2 3 4 5 6
// 01234567890123456789012345678901 23456789012345678901234567890123
// 0123456789ABCDEFGHIJKLMNOPQRSTUV 0123456789ABCDEFGHIJKLMNOPQRSTUV
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXX XXXXXXXX
`include "build_id.v"
parameter CONF_STR = {
"ATARI7800;;",
"FS1,A78A26BIN;",
"FC2,ROMBIN,Load BIOS;",
"-;",
"OFG,Region,Auto,NTSC,PAL;",
"OC,Difficulty Right,A,B;",
"OD,Difficulty Left,A,B;",
"-;",
"P1,Audio & Video;",
"P1-;",
"P1O23,Stereo Mix,None,25%,50%,100%;",
"P1O4,Stereo TIA,No,Yes;",
"d0P1OM,Vertical Crop,Disabled,216p(5x);",
"d0P1ONQ,Crop Offset,0,2,4,8,10,12,-12,-10,-8,-6,-4,-2;",
"P1ORS,Scale,Normal,V-Integer,Narrower HV-Integer,Wider HV-Integer;",
"P1-;",
"P1oIJ,Aspect ratio,Original,Full Screen,[ARC1],[ARC2];",
"P1O9B,Scandoubler Fx,None,HQ2x,CRT 25%,CRT 50%,CRT 75%;",
"P1OT,Show Overscan,No,Yes;",
"P1OE,Show Border,Yes,No;",
"P1OK,Composite Blending,No,Yes;",
"P1OUV,Temperature Colors,Warm,Cool,Hot,Custom;",
"H3P1FC3,PAL,Load Palette;",
"P2,Peripherals;",
"P2OIJ,High Score Cart,Auto,On,Off;",
"P2O7,Swap Joysticks,No,Yes;",
"P2-;",
"P2o69,Port1 Input,Auto,None,Joystick,Lightgun,Paddle,Trakball,Keypad,Driving,STMouse,AmigaMouse,BoosterGrip,Robotron,SaveKey,SNAC;",
"P2oAD,Port2 Input,Auto,None,Joystick,Lightgun,Paddle,Trakball,Keypad,Driving,STMouse,AmigaMouse,BoosterGrip,Robotron,SaveKey,SNAC;",
"h1P2O5,SNAC Analog,Yes,No;",
"P2oK,Allow Multi Paddles,No,Yes;",
"h1P2O6,Sega Phaser Mode,No,Yes;",
"P2oH,Swap Paddle A<->B,No,Yes;",
"P2oE,Quadtari,Off,On;",
"P2O[72:70],Gun X Tuning,0,1,2,3,4,5,6,7;",
"P2-;",
"P2o01,Gun Control,Joy1,Joy2,Mouse;",
"P2o2,Gun Fire,Joy,Mouse;",
"P2o45,Cross,Small,Medium,Big,None;",
"D2P4,Atari2600;",
"D2P4oT,Flickerblend,Off,On;",
"D2P4oV,Stabilize Video,On,Off;",
"D2P4oF,De-comb,Off,On;",
"D2P4oU,Black & White,Off,On;",
"D2P4oOS,Bankswitching,Auto,F8,F6,FE,E0,3F,F4,P2,FA,CV,2K,UA,E7,F0,32,AR,3E,SB,WD,EF;",
"D2P4-;",
"D2P4rG,Load Tape From ADC;",
"P3,Advanced;",
"P3OH,Bypass Bios,Yes,No;",
"P3O1,Clear Memory,Zero,Random;",
"P3O8,Pokey IRQ Enabled,No,Yes;",
"D2P3OL,CPU Driver,TIA,Maria;",
"-;",
"o3,Pause Core on OSD,Off,On;",
"-;",
"R0,Hard Reset;",
"J,Fire1,Fire2,Pause/B&W,Select,Reset(Start),Paddle Button,Diff L,Diff R,Halt;",
"jn,A,B,R,Select,Start,X|P;",
"jp,B,Y,R,Select,Start,X|P;",
"I,",
"Paddle 1A Assigned,",
"Paddle 1B Assigned,",
"Paddle 2A Assigned,",
"Paddle 2B Assigned,",
"Difficulty Right: A,",
"Difficulty Right: B,",
"Difficulty Left: A,",
"Difficulty Left: B,",
"Black and White: Off,",
"Black and White: On,",
"Paddle Mode Enabled,",
"Joystick Mode Enabled;",
"V,v",`BUILD_DATE
};
wire [1:0] buttons;
logic [127:0] status, status_in;
wire status_set;
wire forced_scandoubler;
wire img_mounted;
wire img_readonly;
wire [63:0] img_size;
wire ioctl_download;
wire [24:0] ioctl_addr;
wire [7:0] ioctl_dout;
wire ioctl_wr;
wire [7:0] ioctl_index;
wire [21:0] gamma_bus;
wire [15:0] joy0,joy1,joy2,joy3;
wire [15:0] joya_0,joya_1,joya_2,joya_3,joyar_0,joyar_1,joyar_2,joyar_3;
wire [7:0] pd_0,pd_1,pd_2,pd_3;
wire ioctl_wait;
reg [31:0] sd_lba[1];
reg sd_rd;
reg sd_wr;
wire sd_ack;
wire [8:0] sd_buff_addr;
wire [7:0] sd_buff_dout;
wire [7:0] sd_buff_din[1];
wire sd_buff_wr;
reg en216p;
wire [24:0] ps2_mouse;
logic [10:0] ps2_key;
logic [15:0] ps2_mouse_ext;
logic is_snac0, is_snac1;
logic info_req;
logic [7:0] info;
logic [1:0] last_paddle;
logic pad0_assigned, pad1_assigned, pad2_assigned, pad3_assigned;
logic old_auto_paddle, auto_paddle;
hps_io #(.CONF_STR(CONF_STR)) hps_io
(
.clk_sys (clk_sys),
.HPS_BUS (HPS_BUS),
.buttons (buttons),
.forced_scandoubler (forced_scandoubler),
.gamma_bus (gamma_bus),
.joystick_0 (joy0),
.joystick_1 (joy1),
.joystick_2 (joy2),
.joystick_3 (joy3),
.joystick_l_analog_0 (joya_0),
.joystick_l_analog_1 (joya_1),
.joystick_l_analog_2 (joya_2),
.joystick_l_analog_3 (joya_3),
.joystick_r_analog_0 (joyar_0),
.joystick_r_analog_1 (joyar_1),
.joystick_r_analog_2 (joyar_2),
.joystick_r_analog_3 (joyar_3),
.paddle_0 (pd_0),
.paddle_1 (pd_1),
.paddle_2 (pd_2),
.paddle_3 (pd_3),
.info_req (info_req),
.info (info),
.ps2_mouse (ps2_mouse),
.ps2_mouse_ext (ps2_mouse_ext),
.ps2_key (ps2_key),
.status (status),
.status_set (status_set),
.status_in (status_in),
.status_menumask ({status[31:30] != 3,~tia_en,(is_snac0 | is_snac1),en216p}),
.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),
.img_mounted (img_mounted),
.img_readonly (img_readonly),
.img_size (img_size)
);
//////////////////////////// SYSTEM ///////////////////////////////////
logic tia_en;
logic [3:0] idump;
logic [1:0] ilatch;
logic [7:0] PAin, PBin, PAout, PBout;
wire [7:0] R,G,B;
wire HSync;
wire VSync;
wire HBlank;
wire VBlank, VBlank_orig;
wire [15:0] bios_addr;
reg [7:0] cart_data, bios_data;
reg [7:0] joy0_type, joy1_type, cart_region, cart_save;
logic [15:0] cart_flags;
logic [39:0] cart_header;
logic [31:0] cart_size;
logic [24:0] cart_addr;
logic [7:0] cart_xm;
logic cart_busy;
logic cart_read;
logic [7:0] cart_data_sd;
reg cart_loaded = 0;
logic RW;
logic cart_is_7800;
logic [7:0] hsc_ram_dout, din;
logic hsc_ram_cs;
logic tia_mode;
logic ce_pix_raw;
logic [7:0] cart_din;
logic cart_rw;
wire [4:0] force_bs;
wire sc;
logic [15:0] rnd;
wire PAread;
lfsr #(.N(15)) random(rnd);
wire region_select = ~|status[16:15] ? (tia_en ? tia_pal : cart_region[0]) : status[16];
logic [3:0] iout;
logic tia_hsync;
logic tia_pal, tia_f1;
logic [7:0] rval;
always @(posedge clk_sys) begin
rval <= rnd[7:0];
old_cart_download <= cart_download;
end
logic tape_adc;
logic tape_adc_act;
ltc2308_tape #(.CLK_RATE(14318182)) ltc2308_tape
(
.clk(clk_sys),
.reset(reset),
.ADC_BUS(ADC_BUS),
.dout(tape_adc),
.active(tape_adc_act)
);
wire [17:0] cartram_addr;
wire cartram_wr;
wire [7:0] cartram_wrdata;
wire cartram_rd;
wire [7:0] cartram_data;
wire cartram_busy;
logic [3:0] i_read;
logic halted;
logic use_sk;
logic core_paused;
logic freeze_sync;
logic cpu_ce;
always_ff @(posedge clk_sys) begin :core_sync
reg old_sync;
old_sync <= freeze_sync;
if (old_sync ^ freeze_sync)
core_paused <= (status[35] && OSD_STATUS) || halted;
end
assign HDMI_FREEZE = core_paused;
Atari7800 main
(
.clk_sys (clk_sys),
.reset (reset),
.loading (cart_download || bios_download),
.pause (core_paused),
// Video
.RED (R),
.GREEN (G),
.BLUE (B),
.HSync (HSync),
.VSync (VSync),
.HBlank (HBlank),
.VBlank (VBlank),
.VBlank_orig (VBlank_orig),
.ce_pix (ce_pix_raw),
.show_border (~status[14]),
.show_overscan(status[29]),
.PAL (region_select),
.pal_temp (status[31:30]),
.tia_mode (tia_mode && ~status[17]),
.bypass_bios (~status[17]),
.pokey_irq (status[8]),
.hsc_en (~use_sk && (~|status[19:18] && (|cart_save || cart_xm[0]) ? 1'b1 : status[18])),
.hsc_ram_dout (hsc_ram_dout),
.hsc_ram_cs (hsc_ram_cs),
// Audio
.AUDIO_R (AUDIO_R), // 16 bit
.AUDIO_L (AUDIO_L), // 16 bit
// Cart Interface
.cart_out (cart_download ? ioctl_dout[7:0] : (cart_loaded ? cart_data_sd : cart_data)),
.cart_read (cart_read),
.cart_size (cart_size),
.cart_addr_out(cart_addr),
.cart_flags (cart_is_7800 ? cart_flags[15:0] : 16'd0),
.cart_save (cart_save),
.cart_din (cart_din),
.cart_xm (cart_is_7800 ? cart_xm : 8'h0),
.ps2_key (ps2_key),
.cartram_addr (cartram_addr),
.cartram_wr (cartram_wr),
.cartram_rd (cartram_rd),
.cartram_wrdata (cartram_wrdata),
.cartram_data (cartram_data),
// BIOS
.bios_out (bios_data),
.AB (bios_addr), // Address
.RW (RW), // inverted write
.dout (din),
// Tia
.idump (idump), // Paddle {A0, B0, A1, B1}
.ilatch (ilatch), // Buttons {FireB, FireA}
.i_out (iout),
.tia_en (tia_en),
.tia_hsync (tia_hsync),
.use_stereo (status[4]),
.cpu_driver (~status[21]),
.tia_f1 (tia_f1),
.tia_pal (tia_pal),
.tia_stab (status[63]),
// RIOT
.PAin (PAin), // Direction {RA, LA, DA, UA, RB, LB, DB, UB}
.PBin (PBin), // Port B input
.PAout (PAout), // Port A output
.PBout (PBout), // Peanut butter
.PAread (PAread),
// 2600 Cart Flags from detect2600
.force_bs (use_tape ? BANKAR : force_bs),
.sc (sc),
.clearval (status[1] ? rval : 8'h00),
.random (rval),
.decomb (status[47]),
.mapper (status[60:56]),
.tape_in ({use_tape, tape_adc}),
// Palette loading
.pal_load (pal_download),
.pal_addr (ioctl_addr[9:0]),
.pal_wr (ioctl_wr),
.pal_data (ioctl_dout[7:0]),
.blend (status[61]),
.i_read (i_read)
);
//////////////////////////// MEMORY ///////////////////////////////////
logic [14:0] bios_mask;
detect2600 detect2600
(
.clk (clk_sys),
.addr (ioctl_addr[15:0]),
.reset (~old_cart_download && cart_download),
.cart_size (cart_size),
.enable (ioctl_wr & cart_download),
.data (ioctl_dout),
.force_bs (force_bs),
.sc (sc)
);
initial begin
cart_header = "ATARI";
cart_size = 32'h00008000;
cart_flags = 0;
cart_region = 0;
bios_mask = 0;
cart_save = 0;
cart_xm = 0;
tia_mode = 0;
end
always_ff @(posedge clk_sys) begin
cart_is_7800 <= (cart_header == "ATARI");
if (bios_download && ioctl_wr) // This assumes bootrom is always power of two
bios_mask <= ioctl_addr[14:0];
if (cart_download && ioctl_wr)
cart_size <= (ioctl_addr - (cart_is_7800 ? 8'd128 : 1'b0)) + 1'd1; // 32 bit 1
if (cart_download) begin
tia_mode <= ioctl_index[7:6] != 0;
cart_loaded <= 1;
if (!tia_mode) begin
case (ioctl_addr)
'd01: cart_header[39:32] <= ioctl_dout;
'd02: cart_header[31:24] <= ioctl_dout;
'd03: cart_header[23:16] <= ioctl_dout;
'd04: cart_header[15:8] <= ioctl_dout;
'd05: cart_header[7:0] <= ioctl_dout;
// 'd49: hcart_size[31:24] <= ioctl_dout;
// 'd50: hcart_size[23:16] <= ioctl_dout;
// 'd51: hcart_size[15:8] <= ioctl_dout;
// 'd52: hcart_size[7:0] <= ioctl_dout;
'd53: cart_flags[15:8] <= ioctl_dout;
'd54: cart_flags[7:0] <= ioctl_dout;
'd55: joy0_type <= ioctl_dout; // 0=none, 1=joystick, 2=lightgun
'd56: joy1_type <= ioctl_dout;
'd57: cart_region <= ioctl_dout; // 0=ntsc, 1=pal
'd58: cart_save <= ioctl_dout; // 0=none, 1=high score cart, 2=savekey
'd63: cart_xm <= ioctl_dout; // 1 = Has XM
endcase
if (!cart_is_7800) begin
joy0_type <= 8'd1;
joy1_type <= 8'd1;
end
end else begin
joy0_type <= 8'd1;
joy1_type <= 8'd1;
cart_header <= '0;
cart_flags <= 0;
cart_region <= 0;
cart_save <= 0;
cart_xm <= 0;
end
end
end
logic [24:0] cart_write_addr, fixed_addr;
assign cart_write_addr = (ioctl_addr >= 8'd128) && cart_is_7800 ? (ioctl_addr[24:0] - 8'd128) : ioctl_addr[24:0];
spram #(
.addr_width(14),
.mem_name("Cart"),
.mem_init_file("mem0.mif")
) cart
(
.address (cart_addr),
.clock (clk_sys),
.data (),
.wren (),
.q (cart_data)
);
spram #(.addr_width(15), .mem_name("BIOS")) bios
(
.address (bios_download ? ioctl_addr : (bios_addr[14:0] & bios_mask)),
.clock (clk_sys),
.data (ioctl_dout),
.wren (ioctl_wr & bios_download),
.q (bios_data)
);
always @(posedge clk_vid)
ioctl_wait <= cart_download && cart_busy;
sdram sdram
(
.*,
// system interface
.clk ( clk_vid ),
.init ( !clock_locked ),
// cpu/chipset interface
.ch0_addr (cart_download ? cart_write_addr : cart_addr),
.ch0_wr (cart_download ? (ioctl_wr & cart_download) : 1'b0),
.ch0_din (cart_download ? ioctl_dout : cart_din),
.ch0_rd (cart_read & ~cart_download & ~reset),
.ch0_dout (cart_data_sd),
.ch0_busy (cart_busy)
);
////////////////////////////// IO /////////////////////////////////////
// RIOT Ports:
// 4 bits of PA are used for first stick, other 4 bits for second stick.
// 2600: Bits PB 0,1,3,6,7 are used for reset, select, b/w, left diff, right diff
// 7800: Bits PB 0,1,3,6,7 are used for reset, select, pause, left diff, right diff
// 7800: Bits PB 2 & 4 are used for output to select 2 button mode.
assign info_req = pad0_assigned | pad1_assigned | pad2_assigned |
pad3_assigned | toggle_bw | toggle_ldiff | toggle_rdiff | toggle_paddle;
always_comb begin
info = 8'd0;
if (pad0_assigned)
info = 8'd1;
if (pad1_assigned)
info = 8'd2;
if (pad2_assigned)
info = 8'd3;
if (pad3_assigned)
info = 8'd4;
if (toggle_rdiff)
info = status[12] ? 8'd5: 8'd6;
if (toggle_ldiff)
info = status[13] ? 8'd7: 8'd8;
if (toggle_bw)
info = status[62] ? 8'd9: 8'd10;
if (toggle_paddle)
info = (auto_paddle ? 8'd11 : 8'd12);
end
assign PBin[7] = ~status[12]; // Right diff
assign PBin[6] = ~status[13]; // Left diff
assign PBin[5] = PBout[5]; // Unused (Not connected)
assign PBin[4] = PBout[4]; // Unused (used for 2 button sensing)
assign PBin[3] = tia_en ? ~status[62] : (~joya[6] & ~joyb[6]); // Pause/B&W
assign PBin[2] = PBout[2]; // Unused (used for 2 button sensing)
assign PBin[1] = (~joya[7] & ~joyb[7] & ~keyselect); // Select
assign PBin[0] = (~joya[8] & ~joyb[8] & ~keystart); // Start/Reset
wire [7:0] porta_type, portb_type;
wire [1:0] gun_mode = status[33:32];
wire gun_btn_mode = status[34];
wire gun_port = (portb_type == 2) ? 1'b1 : 1'b0;
wire gun_en = (porta_type == 2 || portb_type == 2);
wire gun_target;
wire gun_sensor;
wire gun_trigger;
lightgun lightgun
(
.CLK(clk_sys),
.RESET(reset),
.MOUSE(ps2_mouse),
.MOUSE_XY(gun_mode[1]),
.LIGHT (|R[7:4] || |G[7:4] || |B[7:4]),
.H_WIDTH(tia_en ? 9'd160 : 9'd372),
.JOY_X(~|gun_mode ? joya_0[7:0] : joya_1[7:0]),
.JOY_Y(~|gun_mode ? joya_0[15:8] : joya_1[15:8]),
.JOY_TRIG(~|gun_mode ? (joya[4] || joya[5]) :(joyb[4] || joyb[5])),
.HDE(~HBlank),
.VDE(~VBlank_orig),
.CE_PIX(ce_pix_raw),
.BTN_MODE(gun_btn_mode),
.SIZE(status[37:36]),
.SENSOR_DELAY((tia_en ? 8'd20 : 8'd48) + {status[72:70], 1'b0}),
.LINE_DELAY((tia_en ? 8'd1 : 8'd20)),
.TARGET(gun_target),
.SENSOR(gun_sensor),
.TRIGGER(gun_trigger)
);
logic [3:0] pad_b;
logic [7:0] pad_ax[4];
logic [3:0] pad_wire;
logic [15:0] joya_a, joya_b, joya_c, joya_d;
logic [7:0] pd_a, pd_b, pd_c, pd_d;
logic sb_a, sb_b, sb_c, sb_d;
logic pdb_a, pdb_b, pdb_c, pdb_d;
wire [3:0] paddle_mask = {(portb_type == 2'd3 ? 2'b11 : 2'b00), (porta_type == 2'd3 ? 2'b11 : 2'b00)};
paddle_chooser paddles
(
.clk (clk_sys),
.reset (reset),
.mask (paddle_mask),
.enable0 (1'b1),
.enable1 (1'b1),
.use_multi (status[52]),
.mouse (ps2_mouse),
.analog ({joya_3, joya_2, joya_1, joya_0}),
.paddle ({pd_3, pd_2, pd_1, pd_0}),
.buttons_in ({joy3[9], joy2[9], joy1[9], joy0[9]}),
.alt_b_in ({joy3[5], joy2[5], joy1[5], joy0[5]}),
.assigned ({pad3_assigned, pad2_assigned, pad1_assigned, pad0_assigned}),
.pd_out ({pad_ax[3], pad_ax[2], pad_ax[1], pad_ax[0]}),
.paddle_but (pad_b)
);
logic [31:0] difference0, difference1, difference2, difference3;
wire [3:0] pread_mux1 = status[49] ? {i_read[2], i_read[3], i_read[0], i_read[1]} : i_read;
wire [3:0] pread_mux2 = status[7] ? {pread_mux1[1:0], pread_mux1[3:2]} : pread_mux1;
paddle_timer pt0 (clk_sys, 1, reset || ~paddle_mask[0], {1'b0, pad_ax[0][7:0]}, ~iout[1], pread_mux2[0], pad_wire[0], difference0);
paddle_timer pt1 (clk_sys, 1, reset || ~paddle_mask[1], {1'b0, pad_ax[1][7:0]}, ~iout[1], pread_mux2[1], pad_wire[1], difference1);
paddle_timer pt2 (clk_sys, 1, reset || ~paddle_mask[2], {1'b0, pad_ax[2][7:0]}, ~iout[1], pread_mux2[2], pad_wire[2], difference2);
paddle_timer pt3 (clk_sys, 1, reset || ~paddle_mask[3], {1'b0, pad_ax[3][7:0]}, ~iout[1], pread_mux2[3], pad_wire[3], difference3);
logic [7:0] mouse_x, mouse_y;
logic dir_x, dir_y;
logic ep_do;
logic [3:0] trackball;
logic trackball_button;
wire pada_0, pada_1, padb_0, padb_1;
wire is_rdiff = joya[11] | joyb[11] | keyrdiff;
wire is_ldiff = joya[10] | joyb[10] | keyldiff;
wire is_halt = joya[12] | joyb[12] | keyhalt;
wire is_bw = (joya[6] | joyb[6] | keybw) && tia_en;
logic old_rdiff, old_ldiff, old_bw, old_halt;
wire toggle_rdiff = ~old_rdiff && is_rdiff;
wire toggle_ldiff = ~old_ldiff && is_ldiff;
wire toggle_bw = ~old_bw && is_bw;
wire toggle_paddle = old_auto_paddle != auto_paddle;
wire toggle_halt = old_halt && ~is_halt;
assign status_set = toggle_rdiff || toggle_ldiff || toggle_bw;
always_comb begin
status_in = status;
status_in[62] = tia_en && toggle_bw ? ~status[62] : status[62];
status_in[12] = toggle_rdiff ? ~status[12] : status[12];
status_in[13] = toggle_ldiff ? ~status[13] : status[13];
end
always @(posedge clk_sys) begin
logic ps2_old;
logic xtog, ytog;
old_rdiff <= is_rdiff;
old_ldiff <= is_ldiff;
old_bw <= is_bw;
old_halt <= is_halt;
old_auto_paddle <= auto_paddle;
if (toggle_halt)
halted <= ~halted;
if (joya[4])
auto_paddle <= 0;
if (joya[9] && ~|status[41:38] && tia_en)
auto_paddle <= 1;
if (~tia_en || reset)
auto_paddle <= 0;
ps2_old <= ps2_mouse[24];
// Feed out the trackball movement one pixel per cpu cycle.
if (PAread) begin
if (mouse_y > 0) begin
ytog <= ~ytog;
mouse_y <= mouse_y - 1'd1;
end
if (mouse_x > 0) begin
xtog <= ~xtog;
mouse_x <= mouse_x - 1'd1;
end
trackball <= {ytog, dir_y, xtog, dir_x};
end
if (ps2_old != ps2_mouse[24]) begin
trackball_button <= ps2_mouse[0] | ps2_mouse[1]; // Allow either right or left button to trigger
mouse_x <= ps2_mouse[4] ? ~ps2_mouse[15:8] : ps2_mouse[15:8]; // Record the absolute values of the 2's complement numbers
mouse_y <= ps2_mouse[5] ? ~ps2_mouse[23:16] : ps2_mouse[23:16];
dir_x <= ~ps2_mouse[4]; // Record the directions (x is inverted for this trackball)
dir_y <= ps2_mouse[5];
end
end
logic [6:0] st_mouse;
logic [3:0] st_m_cnt;
always @(posedge clk_sys)
st_m_cnt <= st_m_cnt + 1'd1;
ps2 mouse_st (
.clk (clk_sys),
.ce (!st_m_cnt),
.reset (reset),
.ps2_key (ps2_key),
.ps2_mouse (ps2_mouse),
.ps2_mouse_ext (ps2_mouse_ext),
.mouse_atari (st_mouse)
);
wire joya_b2 = ~PBout[2] && ~tia_en && joy0_type != 5;
wire joyb_b2 = ~PBout[4] && ~tia_en && joy1_type != 5;
logic [15:0] joya, joyb;
assign joya = (status[46] && ~iout[0]) ? joy2 : (status[7] ? joy1 : joy0);
assign joyb = (status[46] && ~iout[0]) ? joy3 : (status[7] ? joy0 : joy1);
// Col0 Col1 Col2
logic key3, key2, key1; // Row 0
logic key6, key5, key4; // Row 1
logic key9, key8, key7; // Row 2
logic keyh, key0, keya; // Row 3
logic keystart, keyselect, keyhalt, keyrdiff, keyldiff, keybw;
// Follows the format of il, id[1:0], pa[3:0]
logic [6:0] keypad0, keypad1;
wire [3:0] kp_out0 = PAout[7:4];
wire [3:0] kp_out1 = PAout[3:0];
logic [3:0] robor, robol;
// 1,2,3 ---> 1,2,3
// 4,5,6 ---> Q,W,E
// 7,8,9 ---> A,S,D
// *,0,# ---> Z,X,C
// Matrix-ify the keypad
always @(posedge clk_sys) begin
logic last_ps2key10;
if(last_ps2key10 != ps2_key[10]) begin
last_ps2key10 <= ps2_key[10];
case (ps2_key[8:0])
9'h16: key1 <= ps2_key[9]; // 1
9'h1E: key2 <= ps2_key[9]; // 2
9'h26: key3 <= ps2_key[9]; // 3
// Number row
9'h25: key4 <= ps2_key[9]; // 4
9'h2E: key5 <= ps2_key[9]; // 5
9'h36: key6 <= ps2_key[9]; // 6
9'h3D: key7 <= ps2_key[9]; // 7
9'h3E: key8 <= ps2_key[9]; // 8
9'h46: key9 <= ps2_key[9]; // 9
9'h45: key0 <= ps2_key[9]; // 0
9'h4E: keyh <= ps2_key[9]; // -
9'h55: keya <= ps2_key[9]; // =
// Numpad Layout
9'h15: key4 <= ps2_key[9]; // Q
9'h1d: key5 <= ps2_key[9]; // W
9'h24: key6 <= ps2_key[9]; // E
9'h1c: key7 <= ps2_key[9]; // A
9'h1b: key8 <= ps2_key[9]; // S
9'h23: key9 <= ps2_key[9]; // D
9'h1a: keya <= ps2_key[9]; // Z
9'h22: key0 <= ps2_key[9]; // X
9'h21: keyh <= ps2_key[9]; // C
// Numeric Keypad Layout
9'h77: key1 <= ps2_key[9]; // Numlock
9'h4A: key2 <= ps2_key[9]; // Divide
9'h7C: key3 <= ps2_key[9]; // Times
9'h6C: key4 <= ps2_key[9]; // Num 7
9'h75: key5 <= ps2_key[9]; // Num 8
9'h7D: key6 <= ps2_key[9]; // Num 9
9'h6B: key7 <= ps2_key[9]; // Num 4
9'h73: key8 <= ps2_key[9]; // Num 5
9'h74: key9 <= ps2_key[9]; // Num 6
9'h69: keya <= ps2_key[9]; // Num 1
9'h72: key0 <= ps2_key[9]; // Num 2
9'h7A: keyh <= ps2_key[9]; // Num 3
9'h05: keyselect <= ps2_key[9]; // F1
9'h06: keystart <= ps2_key[9]; // F2
9'h04: keybw <= ps2_key[9]; // F3
9'h0C: keyldiff <= ps2_key[9]; // F4
9'h03: keyrdiff <= ps2_key[9]; // F5
9'h0B: keyhalt <= ps2_key[9]; // F6
endcase
end
if (reset)
{key1, key2, key3, key4, key5, key6, key7, key8, key9, key0, keyh, keya, keystart, keyselect, keybw, keyldiff, keyrdiff, keyhalt} <= '0;
// These have pull-ups in an undisturbed state
keypad0 <= '1;
keypad1 <= '1;
// Row 0
if (key3) begin keypad0[6] <= kp_out0[0]; keypad1[6] <= kp_out1[0]; end
if (key2) begin keypad0[5] <= kp_out0[0]; keypad1[5] <= kp_out1[0]; end
if (key1) begin keypad0[4] <= kp_out0[0]; keypad1[4] <= kp_out1[0]; end
// Row 1
if (key6) begin keypad0[6] <= kp_out0[1]; keypad1[6] <= kp_out1[1]; end
if (key5) begin keypad0[5] <= kp_out0[1]; keypad1[5] <= kp_out1[1]; end
if (key4) begin keypad0[4] <= kp_out0[1]; keypad1[4] <= kp_out1[1]; end
// Row 2
if (key9) begin keypad0[6] <= kp_out0[2]; keypad1[6] <= kp_out1[2]; end