forked from somhi/PCXT_SoCkit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PCXT.sv
1693 lines (1462 loc) · 51.3 KB
/
PCXT.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
//============================================================================
//
// 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,
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
);
///////// Default values for ports not used in this core /////////
assign ADC_BUS = 'Z;
//assign USER_OUT = '1;
//assign {UART_RTS, UART_TXD, UART_DTR} = 0;
assign {SD_SCK, SD_MOSI, SD_CS} = 'Z;
//assign {SDRAM_DQ, SDRAM_A, SDRAM_BA, SDRAM_CLK, SDRAM_CKE, SDRAM_DQML, SDRAM_DQMH, SDRAM_nWE, SDRAM_nCAS, SDRAM_nRAS, SDRAM_nCS} = 'Z;
assign SDRAM_CLK = clk_chipset;
assign {DDRAM_CLK, DDRAM_BURSTCNT, DDRAM_ADDR, DDRAM_DIN, DDRAM_BE, DDRAM_RD, DDRAM_WE} = '0;
assign VGA_F1 = 0;
assign VGA_SCALER = 0;
assign VGA_DISABLE = 0;
assign HDMI_FREEZE = 0;
assign LED_DISK = 0;
assign LED_POWER = 0;
assign BUTTONS = 0;
assign LED_USER = 0;
//led fdd_led(clk_cpu, |mgmt_req[7:6], LED_USER);
//////////////////////////////////////////////////////////////////
// Status Bit Map:
// Upper Lower
// 0 1 2 3 4 5 6
// 01234567890123456789012345678901 23456789012345678901234567890123
// 0123456789ABCDEFGHIJKLMNOPQRSTUV 0123456789ABCDEFGHIJKLMNOPQRSTUV
// XXXXX XXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXX
`include "build_id.v"
localparam CONF_STR = {
"PCXT;UART115200:115200;",
"S0,IMGIMAVFD,Floppy A:;",
"S1,IMGIMAVFD,Floppy B:;",
"OJK,Write Protect,None,A:,B:,A: & B:;",
"-;",
"S2,VHD,IDE 0-0;",
"S3,VHD,IDE 0-1;",
"-;",
"OHI,CPU Speed,4.77MHz,7.16MHz,9.54MHz,PC/AT 3.5MHz;",
"-;",
"P1,System & BIOS;",
"P1-;",
"P1O3,Model,IBM PCXT,Tandy 1000;",
"P1-;",
"P1oC,PCXT CGA Graphics,Yes,No;",
"P1oD,PCXT Hercules Graphics,Yes,No;",
"P1O4,PCXT 1st Video,CGA,Hercules;",
"P1-;",
"P1O7,Boot Splash Screen,Yes,No;",
"P1-;",
"P1FC0,ROM,PCXT BIOS:;",
"P1FC1,ROM,Tandy BIOS:;",
"P1-;",
"P1FC2,ROM,EC00 BIOS:;",
"P1-;",
"P1OUV,BIOS Writable,None,EC00,PCXT/Tandy,All;",
"P1-;",
"P2,Audio & Video;",
"P2-;",
"P2OA,C/MS Audio,Enabled,Disabled;",
"P2oAB,OPL2,Adlib 388h,SB FM 388h/228h, Disabled;",
"P2o01,Speaker Volume,1,2,3,4;",
"P2o23,Tandy Volume,1,2,3,4;",
"P2o45,Audio Boost,No,2x,4x;",
"P2o67,Stereo Mix,none,25%,50%,100%;",
"P2-;",
"P2O12,Scandoubler Fx,None,HQ2x,CRT 25%,CRT 50%;",
"P2O89,Aspect ratio,Original,Full Screen,[ARC1],[ARC2];",
"P2OT,Border,No,Yes;",
"P2o8,Composite video,Off,On;",
"P2OEG,Display,Full Color,Green,Amber,B&W,Red,Blue,Fuchsia,Purple;",
"P2-;",
"P3,Hardware;",
"P3-;",
"P3OB,Lo-tech 2MB EMS,Enabled,Disabled;",
"P3OCD,EMS Frame,C000,D000,E000;",
"P3-;",
"P3o9,A000 UMB,Enabled,Disabled;",
"P3-;",
"P3ONO,Joystick 1, Analog, Digital, Disabled;",
"P3OPQ,Joystick 2, Analog, Digital, Disabled;",
"P3OR,Sync Joy to CPU Speed,No,Yes;",
"P3OS,Swap Joysticks,No,Yes;",
"P3-;",
"-;",
"R0,Reset & apply settings;",
"J,Fire 1,Fire 2;",
"V,v",`BUILD_DATE
};
wire forced_scandoubler;
wire [1:0] buttons;
wire [63:0] status;
wire [7:0] xtctl;
//VHD
// wire[ 0:0] usdRd = { vsdRd };
// wire[ 0:0] usdWr = { vsdWr };
// wire usdAck;
// wire[31:0] usdLba[1] = '{ vsdLba };
// wire usdBuffWr;
// wire[ 8:0] usdBuffA;
// wire[ 7:0] usdBuffD[1] = '{ vsdBuffD };
// wire[ 7:0] usdBuffQ;
// wire[63:0] usdImgSz;
// wire[ 0:0] usdImgMtd;
//Keyboard Ps2
wire ps2_kbd_clk_out;
wire ps2_kbd_data_out;
wire ps2_kbd_clk_in;
wire ps2_kbd_data_in;
//Mouse PS2
wire ps2_mouse_clk_out;
wire ps2_mouse_data_out;
wire ps2_mouse_clk_in;
wire ps2_mouse_data_in;
wire ioctl_download;
wire [7:0] ioctl_index;
wire ioctl_wr;
wire [24:0] ioctl_addr;
wire [15:0] ioctl_data;
reg ioctl_wait;
wire [21:0] gamma_bus;
wire [13:0] joy0, joy1;
wire [15:0] joya0, joya1;
wire [4:0] joy_opts = status[27:23];
wire composite = status[40] | xtctl[0];
wire [1:0] scale = status[2:1];
wire [2:0] screen_mode = status[16:14];
wire [1:0] ar = status[9:8];
wire border = status[29] | xtctl[1];
wire a000h = ~status[41] & ~xtctl[6];
reg [1:0] scale_video_ff;
reg hgc_mode_video_ff;
reg [2:0] screen_mode_video_ff;
reg border_video_ff;
wire VGA_VBlank_border;
wire std_hsyncwidth;
wire pause_core;
wire swap_video;
always @(posedge CLK_VIDEO)
begin
scale_video_ff <= scale;
hgc_mode_video_ff <= hgc_mode & ~tandy_mode;
screen_mode_video_ff <= screen_mode;
border_video_ff <= border;
VIDEO_ARX <= (!ar) ? 12'd4 : (ar - 1'd1);
VIDEO_ARY <= (!ar) ? 12'd3 : 12'd0;
end
hps_io #(.CONF_STR(CONF_STR), .PS2DIV(2000), .PS2WE(1), .WIDE(1)) hps_io
(
.clk_sys(clk_chipset),
.HPS_BUS(HPS_BUS),
.EXT_BUS(EXT_BUS),
.gamma_bus(gamma_bus),
.forced_scandoubler(forced_scandoubler),
.buttons(buttons),
.status(status),
.status_menumask({status[5]}),
//VHD
// .sd_rd (usdRd),
// .sd_wr (usdWr),
// .sd_ack (usdAck),
// .sd_lba (usdLba),
// .sd_buff_wr (usdBuffWr),
// .sd_buff_addr (usdBuffA),
// .sd_buff_din (usdBuffD),
// .sd_buff_dout (usdBuffQ),
// .img_mounted (usdImgMtd),
// .img_size (usdImgSz),
.ps2_kbd_clk_in (ps2_kbd_clk_out),
.ps2_kbd_data_in (ps2_kbd_data_out),
.ps2_kbd_clk_out (ps2_kbd_clk_in),
.ps2_kbd_data_out (ps2_kbd_data_in),
.ps2_mouse_clk_out (ps2_mouse_clk_out),
.ps2_mouse_data_out (ps2_mouse_data_out),
.ps2_mouse_clk_in (ps2_mouse_clk_in),
.ps2_mouse_data_in (ps2_mouse_data_in),
.joystick_0(joy0),
.joystick_1(joy1),
.joystick_l_analog_0(joya0),
.joystick_l_analog_1(joya1),
//ioctl
.ioctl_download(ioctl_download),
.ioctl_index(ioctl_index),
.ioctl_wr(ioctl_wr),
.ioctl_addr(ioctl_addr),
.ioctl_dout(ioctl_data),
.ioctl_wait(ioctl_wait)
);
wire [15:0] mgmt_din;
wire [15:0] mgmt_dout;
wire [15:0] mgmt_addr;
wire mgmt_rd;
wire mgmt_wr;
wire [7:0] mgmt_req;
assign mgmt_req[5:3] = 3'b000;
wire [35:0] EXT_BUS;
hps_ext hps_ext
(
.clk_sys(clk_chipset),
.EXT_BUS(EXT_BUS),
.ext_din(mgmt_din),
.ext_dout(mgmt_dout),
.ext_addr(mgmt_addr),
.ext_rd(mgmt_rd),
.ext_wr(mgmt_wr),
.ext_req(mgmt_req),
.ext_hotswap(2'b00)
);
//
/////////////////////// CLOCKS /////////////////////////////
//
wire clk_sys;
wire pll_locked;
wire clk_100;
wire clk_28_636;
wire clk_56_875;
wire clk_113_750;
reg clk_25 = 1'b0;
reg clk_14_318 = 1'b0;
reg clk_9_54 = 1'b0;
reg clk_7_16 = 1'b0;
wire clk_4_77;
wire clk_cpu;
wire pclk;
wire clk_opl2;
wire clk_chipset;
wire peripheral_clock;
wire clk_uart;
pll pll
(
.refclk(CLK_50M),
.rst(0),
.outclk_0(clk_100),
.outclk_1(clk_56_875),
.outclk_2(clk_28_636),
.outclk_3(clk_uart),
.outclk_4(clk_opl2),
.outclk_5(clk_chipset),
.outclk_6(clk_113_750),
.locked(pll_locked)
);
wire reset_wire = RESET | status[0] | buttons[1] | !pll_locked | splashscreen;
wire reset_sdram_wire = RESET | !pll_locked;
//////////////////////////////////////////////////////////////////
always @(posedge clk_28_636)
begin
HBlank_del <= {HBlank_del[13], HBlank_del[12], HBlank_del[11], HBlank_del[10], HBlank_del[9],
HBlank_del[8], HBlank_del[7], HBlank_del[6], HBlank_del[5], HBlank_del[4],
HBlank_del[3], HBlank_del[2], HBlank_del[1], HBlank_del[0], HBlank};
clk_14_318 <= ~clk_14_318; // 14.318Mhz
ce_pixel_cga <= clk_14_318; //if outside always block appears an overscan column in CGA mode
end
reg [4:0] clk_9_54_cnt = 1'b0;
always @(posedge clk_chipset)
if (4'd0 == clk_9_54_cnt) begin
if (clk_9_54)
clk_9_54_cnt <= 4'd3 - 4'd1;
else
clk_9_54_cnt <= 4'd2 - 4'd1;
clk_9_54 <= ~clk_9_54;
end
else begin
clk_9_54_cnt <= clk_9_54_cnt - 4'd1;
clk_9_54 <= clk_9_54;
end
always @(posedge clk_chipset)
clk_25 <= ~clk_25;
always @(posedge clk_14_318)
clk_7_16 <= ~clk_7_16; // 7.16Mhz
clk_div3 clk_normal // 4.77MHz
(
.clk(clk_14_318),
.clk_out(clk_4_77)
);
always @(posedge clk_4_77)
peripheral_clock <= ~peripheral_clock; // 2.385Mhz
reg [27:0] cur_rate;
always @(posedge CLK_50M) cur_rate <= 30000000;
//////////////////////////////////////////////////////////////////
logic biu_done;
logic [7:0] clock_cycle_counter_division_ratio;
logic [7:0] clock_cycle_counter_decrement_value;
logic shift_read_timing;
logic [1:0] ram_read_wait_cycle;
logic [1:0] ram_write_wait_cycle;
logic cycle_accrate;
logic [1:0] clk_select;
always @(posedge clk_chipset, posedge reset)
begin
if (reset)
clk_select <= 2'b00;
else if (biu_done)
clk_select <= (xtctl[3:2] == 2'b00 & ~xtctl[7]) ? status[18:17] : xtctl[7] ? 2'b11 : xtctl[3:2] - 2'b01;
else
clk_select <= clk_select;
end
logic clk_cpu_ff_1;
logic clk_cpu_ff_2;
logic pclk_ff_1;
logic pclk_ff_2;
always @(posedge clk_chipset, posedge reset)
begin
if (reset)
begin
clk_cpu_ff_1 <= 1'b0;
clk_cpu_ff_2 <= 1'b0;
clk_cpu <= 1'b0;
pclk_ff_1 <= 1'b0;
pclk_ff_2 <= 1'b0;
pclk <= 1'b0;
cycle_accrate <= 1'b1;
clock_cycle_counter_division_ratio <= 8'd1 - 8'd1;
clock_cycle_counter_decrement_value <= 8'd1;
shift_read_timing <= 1'b0;
ram_read_wait_cycle <= 2'd0;
ram_write_wait_cycle <= 2'd0;
end
else
begin
clk_cpu_ff_2 <= clk_cpu_ff_1;
clk_cpu <= clk_cpu_ff_2;
pclk_ff_1 <= peripheral_clock;
pclk_ff_2 <= pclk_ff_1;
pclk <= pclk_ff_2;
casez (clk_select)
2'b00: begin
clk_cpu_ff_1 <= clk_4_77;
clock_cycle_counter_division_ratio <= 8'd1 - 8'd1;
clock_cycle_counter_decrement_value <= 8'd1;
shift_read_timing <= 1'b0;
ram_read_wait_cycle <= 2'd0;
ram_write_wait_cycle <= 2'd0;
cycle_accrate <= 1'b1;
end
2'b01: begin
clk_cpu_ff_1 <= clk_7_16;
clock_cycle_counter_division_ratio <= 8'd2 - 8'd1;
clock_cycle_counter_decrement_value <= 8'd3;
shift_read_timing <= 1'b0;
ram_read_wait_cycle <= 2'd0;
ram_write_wait_cycle <= 2'd0;
cycle_accrate <= 1'b1;
end
2'b10: begin
clk_cpu_ff_1 <= clk_9_54;
clock_cycle_counter_division_ratio <= 8'd10 - 8'd1;
clock_cycle_counter_decrement_value <= 8'd21;
shift_read_timing <= 1'b0;
ram_read_wait_cycle <= 2'd0;
ram_write_wait_cycle <= 2'd0;
cycle_accrate <= 1'b1;
end
2'b11: begin
clk_cpu_ff_1 <= clk_25;
clock_cycle_counter_division_ratio <= 8'd1 - 8'd1;
clock_cycle_counter_decrement_value <= 8'd5;
shift_read_timing <= 1'b1;
ram_read_wait_cycle <= 2'd1;
ram_write_wait_cycle <= 2'd0;
cycle_accrate <= 1'b0;
end
endcase
end
end
logic clk_opl2_ff_1;
logic clk_opl2_ff_2;
logic clk_opl2_ff_3;
logic cen_opl2;
always @(posedge clk_chipset)
begin
clk_opl2_ff_1 <= clk_opl2;
clk_opl2_ff_2 <= clk_opl2_ff_1;
clk_opl2_ff_3 <= clk_opl2_ff_2;
cen_opl2 <= clk_opl2_ff_2 & ~clk_opl2_ff_3;
end
//////////////////////////////////////////////////////////////////
logic reset = 1'b1;
logic [15:0] reset_count = 16'h0000;
logic reset_sdram = 1'b1;
logic [15:0] reset_sdram_count = 16'h0000;
always @(posedge CLK_50M, posedge reset_wire)
begin
if (reset_wire)
begin
reset <= 1'b1;
reset_count <= 16'h0000;
end
else if (reset)
begin
if (reset_count != 16'hffff)
begin
reset <= 1'b1;
reset_count <= reset_count + 16'h0001;
end
else
begin
reset <= 1'b0;
reset_count <= reset_count;
end
end
else
begin
reset <= 1'b0;
reset_count <= reset_count;
end
end
logic reset_cpu_ff = 1'b1;
logic reset_cpu = 1'b1;
logic [15:0] reset_cpu_count = 16'h0000;
always @(negedge clk_chipset, posedge reset)
begin
if (reset)
reset_cpu_ff <= 1'b1;
else
reset_cpu_ff <= reset;
end
reg tandy_mode = 0;
reg hgc_mode = 0;
always @(negedge clk_chipset, posedge reset)
begin
if (reset)
begin
tandy_mode <= status[3];
hgc_mode <= status[4];
reset_cpu <= 1'b1;
reset_cpu_count <= 16'h0000;
end
else if (reset_cpu)
begin
reset_cpu <= reset_cpu_ff;
reset_cpu_count <= 16'h0000;
end
else
begin
if (reset_cpu_count != 16'h002A)
begin
reset_cpu <= reset_cpu_ff;
reset_cpu_count <= reset_cpu_count + 16'h0001;
end
else
begin
reset_cpu <= 1'b0;
reset_cpu_count <= reset_cpu_count;
end
end
end
always @(posedge CLK_50M, posedge reset_sdram_wire)
begin
if (reset_sdram_wire)
begin
reset_sdram <= 1'b1;
reset_sdram_count <= 16'h0000;
end
else if (reset_sdram)
begin
if (reset_sdram_count != 16'hffff)
begin
reset_sdram <= 1'b1;
reset_sdram_count <= reset_sdram_count + 16'h0001;
end
else
begin
reset_sdram <= 1'b0;
reset_sdram_count <= reset_sdram_count;
end
end
else
begin
reset_sdram <= 1'b0;
reset_sdram_count <= reset_sdram_count;
end
end
//
/////////////////////// BIOS LOADER ////////////////////////////
//
reg [4:0] bios_load_state = 4'h0;
reg [1:0] bios_protect_flag;
reg bios_access_request;
reg [19:0] bios_access_address;
reg [15:0] bios_write_data;
reg bios_write_n;
reg [7:0] bios_write_wait_cnt;
reg bios_write_byte_cnt;
reg tandy_bios_write;
wire select_pcxt = (ioctl_index[5:0] == 0) && (ioctl_addr[24:16] == 9'b000000000);
wire select_tandy = (ioctl_index[5:0] == 1) && (ioctl_addr[24:16] == 9'b000000000);
wire select_xtide = ioctl_index == 2;
wire [19:0] bios_access_address_wire = select_pcxt ? { 4'b1111, ioctl_addr[15:0]} :
select_tandy ? { 4'b1111, ioctl_addr[15:0]} :
select_xtide ? { 6'b111011, ioctl_addr[13:0]} :
20'hFFFFF;
wire bios_load_n = ~(ioctl_download & (select_pcxt | select_tandy | select_xtide));
always @(posedge clk_chipset, posedge reset_sdram)
begin
if (reset_sdram)
begin
bios_protect_flag <= 2'b11;
bios_access_request <= 1'b0;
bios_access_address <= 20'hFFFFF;
bios_write_data <= 16'hFFFF;
bios_write_n <= 1'b1;
bios_write_wait_cnt <= 'h0;
bios_write_byte_cnt <= 1'h0;
tandy_bios_write <= 1'b0;
ioctl_wait <= 1'b1;
bios_load_state <= 4'h00;
end
else if (~initilized_sdram)
begin
bios_protect_flag <= 2'b11;
bios_access_request <= 1'b0;
bios_access_address <= 20'hFFFFF;
bios_write_data <= 16'hFFFF;
bios_write_n <= 1'b1;
bios_write_wait_cnt <= 'h0;
bios_write_byte_cnt <= 1'h0;
ioctl_wait <= 1'b1;
bios_load_state <= 4'h00;
end
else
begin
casez (bios_load_state)
4'h00:
begin
bios_protect_flag <= ~status[31:30]; // bios_writable
bios_access_address <= 20'hFFFFF;
bios_write_data <= 16'hFFFF;
bios_write_n <= 1'b1;
bios_write_wait_cnt <= 'h0;
bios_write_byte_cnt <= 1'h0;
tandy_bios_write <= 1'b0;
if (~ioctl_download)
begin
bios_access_request <= 1'b0;
ioctl_wait <= 1'b0;
end
else
begin
bios_access_request <= 1'b1;
ioctl_wait <= 1'b1;
end
if ((ioctl_download) && (~processor_ready) && (address_direction))
bios_load_state <= 4'h01;
else
bios_load_state <= 4'h00;
end
4'h01:
begin
bios_protect_flag <= 2'b00;
bios_access_request <= 1'b1;
bios_write_byte_cnt <= 1'h0;
tandy_bios_write <= select_tandy;
if (~ioctl_download)
begin
bios_access_address <= 20'hFFFFF;
bios_write_data <= 16'hFFFF;
bios_write_n <= 1'b1;
bios_write_wait_cnt <= 'h0;
ioctl_wait <= 1'b0;
bios_load_state <= 4'h00;
end
else if ((~ioctl_wr) || (bios_load_n))
begin
bios_access_address <= 20'hFFFFF;
bios_write_data <= 16'hFFFF;
bios_write_n <= 1'b1;
bios_write_wait_cnt <= 'h0;
ioctl_wait <= 1'b0;
bios_load_state <= 4'h01;
end
else
begin
bios_access_address <= bios_access_address_wire;
bios_write_data <= ioctl_data;
bios_write_n <= 1'b1;
bios_write_wait_cnt <= 'h0;
ioctl_wait <= 1'b1;
bios_load_state <= 4'h02;
end
end
4'h02:
begin
bios_protect_flag <= 2'b00;
bios_access_request <= 1'b1;
bios_access_address <= bios_access_address;
bios_write_data <= bios_write_data;
bios_write_byte_cnt <= bios_write_byte_cnt;
tandy_bios_write <= select_tandy;
ioctl_wait <= 1'b1;
bios_write_wait_cnt <= bios_write_wait_cnt + 'h1;
if (bios_write_wait_cnt != 'd20)
begin
bios_write_n <= 1'b0;
bios_load_state <= 4'h02;
end
else
begin
bios_write_n <= 1'b1;
bios_load_state <= 4'h03;
end
end
4'h03:
begin
bios_protect_flag <= 2'b00;
bios_access_request <= 1'b1;
bios_access_address <= bios_access_address;
bios_write_data <= bios_write_data;
bios_write_n <= 1'b1;
bios_write_byte_cnt <= bios_write_byte_cnt;
tandy_bios_write <= 1'b0;
ioctl_wait <= 1'b1;
bios_write_wait_cnt <= bios_write_wait_cnt + 'h1;
if (bios_write_wait_cnt != 'h40)
bios_load_state <= 4'h03;
else
bios_load_state <= 4'h04;
end
4'h04:
begin
bios_protect_flag <= 2'b00;
bios_access_request <= 1'b1;
bios_access_address <= bios_access_address + 'h1;
bios_write_data <= {8'hFF, bios_write_data[15:8]};
bios_write_n <= 1'b1;
bios_write_wait_cnt <= 'h0;
bios_write_byte_cnt <= ~bios_write_byte_cnt;
tandy_bios_write <= 1'b0;
ioctl_wait <= 1'b1;
if (bios_write_byte_cnt == 1'b0)
bios_load_state <= 4'h02;
else
bios_load_state <= 4'h01;
end
default:
begin
bios_protect_flag <= 2'b11;
bios_access_request <= 1'b0;
bios_access_address <= 20'hFFFFF;
bios_write_data <= 16'hFFFF;
bios_write_n <= 1'b1;
bios_write_wait_cnt <= 'h0;
bios_write_byte_cnt <= 1'h0;
tandy_bios_write <= 1'b0;
ioctl_wait <= 1'b0;
bios_load_state <= 4'h00;
end
endcase
end
end
//////////////////////////////////////////////////////////////////
//
// Splash screen
//
reg splash_off;
reg [24:0] splash_cnt = 0;
reg [3:0] splash_cnt2 = 0;
reg splashscreen = 1;
always @ (posedge clk_14_318)
begin
splash_off <= status[7];
if (splashscreen)
begin
if (splash_off)
splashscreen <= 0;
else if(splash_cnt2 == 5) // 5 seconds delay
splashscreen <= 0;
else if (splash_cnt == 14318000)
begin // 1 second at 14.318Mhz
splash_cnt2 <= splash_cnt2 + 1;
splash_cnt <= 0;
end
else
splash_cnt <= splash_cnt + 1;
end
end
//
// Input F/F PS2_CLK
//
logic device_clock_ff;
logic device_clock;
always_ff @(negedge clk_chipset, posedge reset)
begin
if (reset)
begin
device_clock_ff <= 1'b0;
device_clock <= 1'b0;
end
else
begin
device_clock_ff <= ps2_kbd_clk_in;
device_clock <= device_clock_ff ;
end
end
//
// Input F/F PS2_DAT
//
logic device_data_ff;
logic device_data;
always_ff @(negedge clk_chipset, posedge reset)
begin
if (reset)
begin
device_data_ff <= 1'b0;
device_data <= 1'b0;
end
else
begin
device_data_ff <= ps2_kbd_data_in;
device_data <= device_data_ff;
end
end
wire [7:0] data_bus;
wire INTA_n;
wire [19:0] cpu_ad_out;
reg [19:0] cpu_address;
wire [7:0] cpu_data_bus;
wire processor_ready;
wire interrupt_to_cpu;
wire address_latch_enable;
wire address_direction;
wire lock_n;
wire [2:0]processor_status;
wire [3:0] dma_acknowledge_n;
logic [7:0] port_b_out;
logic [7:0] port_c_in;
reg [7:0] sw;
assign sw = hgc_mode ? 8'b00111101 : 8'b00101101; // PCXT DIP Switches (HGC or CGA 80)
assign port_c_in[3:0] = port_b_out[3] ? sw[7:4] : sw[3:0];
wire tandy_bios_flag = bios_write_n ? tandy_mode : tandy_bios_write;
always @(posedge clk_chipset)
begin
if (address_latch_enable)
cpu_address <= cpu_ad_out;
else
cpu_address <= cpu_address;
end