-
Notifications
You must be signed in to change notification settings - Fork 33
/
ZX-Spectrum.sv
1473 lines (1236 loc) · 37.4 KB
/
ZX-Spectrum.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
//============================================================================
// Sinclair ZX Spectrum for MiSTer
// Copyright (C) 2017-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 USER_OUT = '1;
assign VGA_F1 = 0;
assign {UART_RTS, UART_TXD, UART_DTR} = 0;
assign AUDIO_S = 1;
assign AUDIO_MIX = status[3:2];
assign LED_USER = ioctl_download | tape_led | tape_adc_act | (sd_act & ~vsd_sel);
assign LED_DISK = 0;
assign LED_POWER = 0;
assign BUTTONS = 0;
assign VGA_SCALER= 0;
assign VGA_DISABLE = 0;
assign HDMI_FREEZE = 0;
localparam ARCH_ZX48 = 5'b011_00; // ZX 48
localparam ARCH_ZX128 = 5'b000_01; // ZX 128/+2
localparam ARCH_ZX3 = 5'b100_01; // ZX 128 +3
localparam ARCH_P48 = 5'b011_10; // Pentagon 48
localparam ARCH_P128 = 5'b000_10; // Pentagon 128
localparam ARCH_P1024 = 5'b001_10; // Pentagon 1024
localparam CONF_BDI = "(BDI)";
localparam CONF_PLUSD = "(+D) ";
localparam CONF_PLUS3 = "(+3) ";
// Status Bit Map:
// Upper Lower
// 0 1 2 3 4 5 6
// 01234567890123456789012345678901 23456789012345678901234567890123
// 0123456789ABCDEFGHIJKLMNOPQRSTUV 0123456789ABCDEFGHIJKLMNOPQRSTUV
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXX
`include "build_id.v"
localparam CONF_STR = {
"Spectrum;;",
"S0,TRDIMGDSKMGT,Load Disk;",
"F2,TAPCSWTZX,Load Tape;",
"F4,Z80SNA,Load Snapshot;",
"S1,VHD,Load DivMMC;",
"-;",
"P1,Audio & Video;",
"P1-;",
"P1O[5:4],Aspect Ratio,Original,Full Screen,[ARC1],[ARC2];",
"P1O[16:15],Scandoubler Fx,None,HQ2x,CRT 25%,CRT 50%;",
"P1-;",
"P1O[38],Narrow Border,No,Yes;",
"H2d1P1O[28],Vertical Crop,No,Yes;",
"h2d1P1O[29:28],Vertical Crop,No,270,216;",
"P1O[27:26],Scale,Normal,V-Integer,Narrower HV-Integer,Wider HV-Integer;",
"P1-;",
"P1O[21:20,General Sound,512KB,1MB,2MB,Disabled;",
"P1O[3:2],Stereo Mix,none,25%,50%,100%;",
"P1-;",
"P1O[39],PSG/FM,Enabled,Disabled;",
"P1O[40],PSG Stereo,ABC,ACB;",
"P1O[41],PSG Model,YM2149,AY8910;",
"P2,Hardware;",
"P2-;",
"P2O[7],Port #FE,Issue 2,Issue 3;",
"P2O[13],Port #FF,Timex,SAA1099;",
"P2O[14],ULA+,Enabled,Disabled;",
"D3P2OP,Snow Bug,Disabled,Enabled;",
"P2-;",
"P2O[9:8],Video Timings,ULA-48,ULA-128,Pentagon;",
"P2O[12:10],Memory,Spectrum 128K/+2,Pentagon 1024K,Profi 1024K,Spectrum 48K,Spectrum +2A/+3;",
"P2-;",
"P2O[33:32],MMC Mode,Auto(VHD),SD Card 14MHz,SD Card 28MHz;",
"P2O[31:30],MMC Version,DivMMC+ESXDOS,DivMMC,ZXMMC;",
"-;",
"O[37:36],Keyboard,Normal,Ghosting,Recreated ZX,Recr+Ghosting;",
"O[19:17],Joystick,Kempston,Sinclair I,Sinclair II,Sinclair I+II,Cursor;",
"O[35:34],Mouse,Disabled,Kempston L/R,Kempston R/L;",
"O[6],Fast Tape Load,On,Off;",
"O[1],Tape Sound,On,Off;",
"O[24:22],CPU Speed,Original,7MHz,14MHz,28MHz,56MHz;",
"-;",
"R[0],Reset & Apply;",
"J,Fire 1,Fire 2;",
"V,v",`BUILD_DATE
};
//////////////////// CLOCKS ///////////////////
wire locked;
wire clk_sys, clk_56;
wire clk_aud = clk_56;
pll pll
(
.refclk(CLK_50M),
.rst(0),
.outclk_0(clk_sys),
.outclk_1(clk_56),
.locked(locked)
);
reg ce_7mp;
reg ce_7mn;
reg pause;
reg cpu_en = 1;
reg ce_cpu_tp;
reg ce_cpu_tn;
reg ce_tape;
reg ce_wd1793;
reg ce_u765;
reg ce_spi;
wire ce_cpu_p = cpu_en & cpu_p;
wire ce_cpu_n = cpu_en & cpu_n;
wire cpu_p = ~&turbo ? ce_cpu_tp : ce_cpu_sp;
wire cpu_n = ~&turbo ? ce_cpu_tn : ce_cpu_sn;
always @(posedge clk_sys) begin
reg [5:0] counter = 0;
counter <= counter + 1'd1;
ce_7mp <= !counter[3] & !counter[2:0];
ce_7mn <= counter[3] & !counter[2:0];
// split ce for relaxed fitting
ce_cpu_tp <= !(counter & turbo);
ce_tape <= !(counter & turbo) & cpu_en;
ce_wd1793 <= !(counter & turbo) & cpu_en;
ce_u765 <= !(counter & turbo) & cpu_en;
ce_cpu_tn <= !((counter & turbo) ^ turbo ^ turbo[4:1]);
ce_spi <= vsd_sel | ((status[33] | !counter[1]) & !counter[0]);
end
wire [4:0] turbo_req;
always_comb begin
casex({tape_active & ~status[6], status[24:22]})
'b1XXX: turbo_req = 5'b00001;
'b0001: turbo_req = 5'b01111;
'b0010: turbo_req = 5'b00111;
'b0011: turbo_req = 5'b00011;
'b0100: turbo_req = 5'b00001;
default: turbo_req = 5'b11111;
endcase
end
reg [2:0] speed_req;
reg speed_set;
always @(posedge clk_sys) begin
reg [9:4] old_Fn;
old_Fn <= Fn[9:4];
if(reset) pause <= 0;
speed_set <= 0;
if(!mod) begin
if(~old_Fn[4] & Fn[4]) {speed_set,speed_req} <= 4'b1_000;
if(~old_Fn[5] & Fn[5]) {speed_set,speed_req} <= 4'b1_001;
if(~old_Fn[6] & Fn[6]) {speed_set,speed_req} <= 4'b1_010;
if(~old_Fn[7] & Fn[7]) {speed_set,speed_req} <= 4'b1_011;
if(~old_Fn[8] & Fn[8]) {speed_set,speed_req} <= 4'b1_100;
if(~old_Fn[9] & Fn[9]) pause <= ~pause;
end
end
reg [4:0] turbo = 5'b11111;
always @(posedge clk_sys) begin
reg [1:0] timeout;
if(cpu_n) begin
if(timeout) timeout <= timeout + 1'd1;
if(turbo != turbo_req) begin
cpu_en <= 0;
timeout <= 1;
turbo <= turbo_req;
end else if(!cpu_en & !timeout & ram_ready) begin
cpu_en <= ~pause;
end else if(!turbo[4:2] & !ram_ready) begin // SDRAM wait for 28MHz/56MHz turbo
cpu_en <= 0;
end else if(!turbo[4:3] & !ram_ready & tape_active) begin // SDRAM wait for TAPE load on 14MHz
cpu_en <= 0;
end else if(cpu_en & pause) begin
cpu_en <= 0;
end
end
end
////////////////// HPS I/O ///////////////////
wire [10:0] ps2_key;
wire [24:0] ps2_mouse;
wire [15:0] joy0;
wire [15:0] joy1;
wire [1:0] buttons;
wire forced_scandoubler;
wire [63:0] status;
wire sd_rd_plus3;
wire sd_wr_plus3;
wire [31:0] sd_lba_plus3;
wire [7:0] sd_buff_din_plus3;
wire sd_rd_wd;
wire sd_wr_wd;
wire [31:0] sd_lba_wd;
wire [7:0] sd_buff_din_wd;
wire sd_rd_mmc;
wire sd_wr_mmc;
wire [31:0] sd_lba_mmc;
wire [7:0] sd_buff_din_mmc;
wire [31:0] sd_lba[2] = '{plus3_fdd_ready ? sd_lba_plus3 : sd_lba_wd, sd_lba_mmc};
wire [1:0] sd_rd = {sd_rd_mmc, plus3_fdd_ready ? sd_rd_plus3 : sd_rd_wd};
wire [1:0] sd_wr = {sd_wr_mmc, plus3_fdd_ready ? sd_wr_plus3 : sd_wr_wd};
wire [1:0] sd_ack;
wire [8:0] sd_buff_addr;
wire [7:0] sd_buff_dout;
wire [7:0] sd_buff_din[2] = '{plus3_fdd_ready ? sd_buff_din_plus3 : sd_buff_din_wd, sd_buff_din_mmc};
wire sd_buff_wr;
wire [1:0] img_mounted;
wire [63:0] img_size;
wire img_readonly;
wire ioctl_wr;
wire [24:0] ioctl_addr;
wire [7:0] ioctl_dout;
wire ioctl_download;
wire [7:0] ioctl_index;
wire ioctl_wait;
wire [21:0] gamma_bus;
hps_io #(.CONF_STR(CONF_STR), .VDNUM(2)) hps_io
(
.clk_sys(clk_sys),
.HPS_BUS(HPS_BUS),
.ps2_key(ps2_key),
.ps2_mouse(ps2_mouse),
.joystick_0(joy0),
.joystick_1(joy1),
.buttons(buttons),
.forced_scandoubler(forced_scandoubler),
.new_vmode(new_vmode),
.status(status),
.status_menumask({|status[9:8],en1080p,|vcrop,~need_apply}),
.status_set(speed_set|arch_set|snap_hwset),
.status_in({status[63:25], speed_set ? speed_req : 3'b000, status[21:13], arch_set ? arch : snap_hwset ? snap_hw : status[12:8], status[7:0]}),
.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_size(img_size),
.img_readonly(img_readonly),
.ioctl_wr(ioctl_wr),
.ioctl_addr(ioctl_addr),
.ioctl_dout(ioctl_dout),
.ioctl_download(ioctl_download),
.ioctl_index(ioctl_index),
.ioctl_wait(ioctl_wait),
.gamma_bus(gamma_bus)
);
reg [2:0] cur_mode = 0;
wire need_apply = status[12:10] != cur_mode;
always @(posedge clk_sys) begin
if(reset) cur_mode <= status[12:10];
end
/////////////////// CPU ///////////////////
wire [15:0] addr;
wire [7:0] cpu_dout;
wire nM1;
wire nMREQ;
wire nIORQ;
wire nRD;
wire nWR;
wire nRFSH;
wire nBUSACK;
wire nINT;
wire nBUSRQ = ~ioctl_download;
wire reset = buttons[1] | status[0] | cold_reset | warm_reset | shdw_reset | Fn[10] | mmc_reset;
wire cold_reset =((mod[2:1] == 1) & Fn[11]) | init_reset | arch_reset | snap_reset | mmc_reset;
wire warm_reset = (mod[2:1] == 2) & Fn[11];
wire shdw_reset = (mod[2:1] == 3) & Fn[11] & ~plus3;
wire io_wr = ~nIORQ & ~nWR & nM1;
wire io_rd = ~nIORQ & ~nRD & nM1;
wire m1 = ~nM1 & ~nMREQ;
wire[211:0] cpu_reg; // IFF2, IFF1, IM, IY, HL', DE', BC', IX, HL, DE, BC, PC, SP, R, I, F', A', F, A
wire [15:0] reg_DE = cpu_reg[111:96];
wire [7:0] reg_A = cpu_reg[7:0];
T80pa cpu
(
.RESET_n(~reset),
.CLK(clk_sys),
.CEN_p(ce_cpu_p),
.CEN_n(ce_cpu_n),
.WAIT_n(mmc_ready),
.INT_n(nINT),
.NMI_n(~NMI),
.BUSRQ_n(nBUSRQ),
.M1_n(nM1),
.MREQ_n(nMREQ),
.IORQ_n(nIORQ),
.RD_n(nRD),
.WR_n(nWR),
.RFSH_n(nRFSH),
.HALT_n(1),
.BUSAK_n(nBUSACK),
.A(addr),
.DO(cpu_dout),
.DI(cpu_din),
.REG(cpu_reg),
.DIR(snap_REG),
.DIRSet(snap_REGSet)
);
wire [7:0] cpu_din =
~nMREQ ? (tape_dout_en ? tape_dout : ram_dout) :
~io_rd ? port_ff :
fdc_sel ? fdc_dout :
mf3_port ? (&addr[14:13] ? page_reg : page_reg_plus3) :
mmc_sel ? mmc_dout :
kemp_sel ? kemp_dout :
portBF ? {page_scr_copy, 7'b1111111} :
gs_sel ? gs_dout :
psg_rd ? psg_dout :
ulap_sel ? ulap_dout :
~addr[0] ? {1'b1, ula_tape_in, 1'b1, kbd_dout} :
port_ff;
reg init_reset = 1;
always @(posedge clk_sys) begin
reg old_rst = 0;
old_rst <= status[0];
if(old_rst & ~status[0]) init_reset <= 0;
end
reg NMI;
always @(posedge clk_sys) begin
reg old_F11;
old_F11 <= Fn[11];
if(reset | ~Fn[11] | (m1 & (addr == 'h66))) NMI <= 0;
else if(~old_F11 & Fn[11] & (mod[2:1] == 0)) NMI <= 1;
end
////////////////// MEMORY //////////////////
reg [24:0] ram_addr;
reg [7:0] ram_din;
reg ram_we;
reg ram_rd;
wire [7:0] ram_dout;
wire ram_ready;
reg [24:0] load_addr;
always @(posedge clk_sys) load_addr <= ioctl_addr + (ioctl_index[4:0] ? 25'h400000 : ioctl_index[7:6] ? 25'h14C000 : 25'h150000);
reg load;
always @(posedge clk_sys) load <= (reset | ~nBUSACK) & ~nBUSRQ;
always_comb begin
casex({snap_reset, load, tape_req, mmc_ram_en, page_special, addr[15:14]})
'b1XXX_X_XX: ram_addr = snap_addr;
'b01XX_X_XX: ram_addr = load_addr;
'b001X_X_XX: ram_addr = tape_addr;
'b0001_X_XX: ram_addr = { 4'b1000, mmc_ram_bank, addr[12:0]};
'b0000_0_00: ram_addr = { 3'b101, page_rom, addr[13:0]}; //ROM
'b0000_0_01: ram_addr = { 4'b0000, 3'd5, addr[13:0]}; //Non-special page modes
'b0000_0_10: ram_addr = { 4'b0000, 3'd2, addr[13:0]};
'b0000_0_11: ram_addr = { 1'b0, page_ram, addr[13:0]};
'b0000_1_00: ram_addr = { 4'b0000, |page_reg_plus3[2:1], 2'b00, addr[13:0]}; //Special page modes
'b0000_1_01: ram_addr = { 4'b0000, |page_reg_plus3[2:1], &page_reg_plus3[2:1], 1'b1, addr[13:0]};
'b0000_1_10: ram_addr = { 4'b0000, |page_reg_plus3[2:1], 2'b10, addr[13:0]};
'b0000_1_11: ram_addr = { 4'b0000, ~page_reg_plus3[2] & page_reg_plus3[1], 2'b11, addr[13:0]};
endcase
casex({snap_reset, load, tape_req})
'b1XX: ram_din = snap_data;
'b01X: ram_din = ioctl_dout;
'b001: ram_din = 0;
'b000: ram_din = cpu_dout;
endcase
casex({load, tape_req})
'b1X: ram_rd = 0;
'b01: ram_rd = ~nMREQ;
'b00: ram_rd = ~nMREQ & ~nRD;
endcase
casex({snap_reset, load, tape_req})
'b1XX: ram_we = snap_wr;
'b01X: ram_we = ioctl_wr;
'b001: ram_we = 0;
'b000: ram_we = (mmc_ram_en | page_special | addr[15] | addr[14] | ((plusd_mem | mf128_mem) & addr[13])) & ~nMREQ & ~nWR;
endcase
end
sdram ram
(
.*,
.init(~locked),
.clk(clk_sys),
.dout(ram_dout),
.din (ram_din),
.addr(ram_addr),
.we(ram_we),
.rd(ram_rd),
.ready(ram_ready)
);
wire vram_we = (ram_addr[24:16] == 1) & ram_addr[14];
dpram #(.ADDRWIDTH(15)) vram
(
.clock(clk_sys),
.address_a({ram_addr[15], ram_addr[13:0]}),
.data_a(ram_din),
.wren_a(ram_we & vram_we),
.address_b(vram_addr),
.q_b(vram_dout)
);
reg zx48;
reg p1024;
reg pf1024;
reg plus3;
reg page_scr_copy;
reg shadow_rom;
reg [7:0] page_reg;
reg [7:0] page_reg_plus3;
reg [7:0] page_reg_p1024;
wire page_disable = zx48 | (~p1024 & page_reg[5]) | (p1024 & page_reg_p1024[2] & page_reg[5]);
wire page_scr = page_reg[3];
wire [5:0] page_ram = {page_128k, page_reg[2:0]};
wire page_write = ~addr[15] & ~addr[1] & (addr[14] | ~plus3) & ~page_disable; //7ffd
wire page_write_plus3 = ~addr[1] & addr[12] & ~addr[13] & ~addr[14] & ~addr[15] & plus3 & ~page_disable; //1ffd
wire page_special = page_reg_plus3[0];
wire motor_plus3 = page_reg_plus3[3];
wire page_p1024 = addr[15] & addr[14] & addr[13] & ~addr[12] & ~addr[3]; //eff7
reg [2:0] page_128k;
reg [3:0] page_rom;
wire active_48_rom = zx48 | (page_reg[4] & ~plus3) | (plus3 & page_reg[4] & page_reg_plus3[2] & ~page_special);
always_comb begin
casex({mmc_rom_en, shadow_rom, trdos_en, plusd_mem, mf128_mem, plus3})
'b1XXXXX: page_rom <= 4'b0011; //esxdos
'b01XXXX: page_rom <= 4'b0100; //shadow
'b001XXX: page_rom <= 4'b0101; //trdos
'b0001XX: page_rom <= 4'b1100; //plusd
'b00001X: page_rom <= { 2'b11, plus3, ~plus3 }; //MF128/+3
'b000001: page_rom <= { 2'b10, page_reg_plus3[2], page_reg[4] }; //+3
'b000000: page_rom <= { zx48, 2'b11, zx48 | page_reg[4] }; //up to +2
endcase
end
always @(posedge clk_sys) begin
reg old_wr, old_m1, old_reset;
reg [2:0] rmod;
old_wr <= io_wr;
old_m1 <= m1;
old_reset <= reset;
if(~old_reset & reset) rmod <= mod;
if(reset) begin
page_scr_copy <= 0;
page_reg <= 0;
page_reg_plus3 <= 0;
page_reg_p1024 <= 0;
page_128k <= 0;
page_reg[4] <= Fn[10];
page_reg_plus3[2] <= Fn[10];
shadow_rom <= shdw_reset & ~plusd_en;
if(Fn[10] && (rmod == 1)) begin
p1024 <= 0;
pf1024 <= 0;
zx48 <= ~plus3;
end else begin
p1024 <= (status[12:10] == 1);
pf1024<= (status[12:10] == 2);
zx48 <= (status[12:10] == 3);
plus3 <= (status[12:10] == 4);
end
end else begin
if(snap_REGSet) begin
if((snap_hw == ARCH_ZX128) || (snap_hw == ARCH_P128) || (snap_hw == ARCH_ZX3)) page_reg <= snap_7ffd;
if(snap_hw == ARCH_ZX3) page_reg_plus3 <= snap_1ffd;
end
else begin
if(m1 && ~old_m1 && addr[15:14]) shadow_rom <= 0;
if(m1 && ~old_m1 && ~plusd_en && ~mod[0] && (addr == 'h66) && ~plus3) shadow_rom <= 1;
if(io_wr & ~old_wr) begin
if(page_write) begin
page_reg <= cpu_dout;
if(p1024 & ~page_reg_p1024[2]) page_128k[2:0] <= { cpu_dout[5], cpu_dout[7:6] };
if(~plusd_mem) page_scr_copy <= cpu_dout[3];
end else if (page_write_plus3) begin
page_reg_plus3 <= cpu_dout;
end
if(pf1024 & (addr == 'hDFFD)) page_128k <= cpu_dout[2:0];
if(p1024 & page_p1024) page_reg_p1024 <= cpu_dout;
end
end
end
end
//////////////////// ULA PORT ///////////////////
reg [2:0] border_color;
reg ear_out;
reg mic_out;
always @(posedge clk_sys) begin
if(reset) {ear_out, mic_out} <= 2'b00;
else if(io_wr & ~addr[0]) begin
border_color <= cpu_dout[2:0];
ear_out <= cpu_dout[4];
mic_out <= cpu_dout[3];
end
if(snap_REGSet) border_color <= snap_border;
end
//////////////////// AUDIO ///////////////////
wire [7:0] psg_dout;
wire [11:0] ts_l, ts_r;
wire psg_sel = /*addr[0] &*/ addr[15] & ~addr[1];
wire psg_we = psg_sel & ~nIORQ & ~nWR & nM1;
wire psg_rd = psg_sel & addr[14];
reg psg_reset;
reg psg_active;
wire aud_reset = reset | psg_reset;
reg ce_ym; //3.5MHz
always @(posedge clk_aud) begin
reg [3:0] counter = 0;
reg p1,p2,p3;
p1 <= pause;
p2 <= p1;
p3 <= p2;
counter <= counter + 1'd1;
ce_ym <= !counter & ~p3;
end
// Turbosound FM: dual YM2203 chips
turbosound turbosound
(
.RESET(aud_reset),
.CLK(clk_aud),
.CE(ce_ym),
.BDIR(psg_we),
.BC(addr[14]),
.DI(cpu_dout),
.DO(psg_dout),
.ENABLE(~status[39]),
.PSG_MIX(status[40]),
.PSG_TYPE(status[41]),
.CHANNEL_L(ts_l),
.CHANNEL_R(ts_r)
);
reg ce_saa; //8MHz
always @(posedge clk_aud) begin
reg [3:0] counter = 0;
counter <= counter + 1'd1;
if(counter == 6) counter <= 0;
ce_saa <= !counter;
end
wire [7:0] saa_l;
wire [7:0] saa_r;
saa1099 saa1099
(
.clk_sys(clk_aud),
.ce(ce_saa),
.rst_n(~aud_reset),
.cs_n((addr[7:0] != 255) | nIORQ | tmx_avail),
.a0(addr[8]),
.wr_n(nWR),
.din(cpu_dout),
.out_l(saa_l),
.out_r(saa_r)
);
wire [7:0] gs_dout;
wire [14:0] gs_l, gs_r;
reg ce_gs = 0;
always @(posedge clk_aud) ce_gs <= ~ce_gs;
gs #("rtl/gs105b.mif") gs
(
.RESET(aud_reset),
.CLK(clk_aud),
.CE(ce_gs),
.A(addr[3]),
.DI(cpu_dout),
.DO(gs_dout),
.CS_n(~nM1 | nIORQ | ~gs_sel),
.WR_n(nWR),
.RD_n(nRD),
.MEM_ADDR(gs_mem_addr),
.MEM_DI(gs_mem_din),
.MEM_DO(gs_mem_dout | gs_mem_mask),
.MEM_RD(gs_mem_rd),
.MEM_WR(gs_mem_wr),
.MEM_WAIT(~gs_mem_ready),
.OUTL(gs_l),
.OUTR(gs_r)
);
wire [20:0] gs_mem_addr;
wire [7:0] gs_mem_dout;
wire [7:0] gs_mem_din;
wire gs_mem_rd;
wire gs_mem_wr;
wire gs_mem_ready;
reg [7:0] gs_mem_mask;
always_comb begin
gs_mem_mask = 0;
case(status[21:20])
0: if(gs_mem_addr[20:19]) gs_mem_mask = 8'hFF;
1: if(gs_mem_addr[20]) gs_mem_mask = 8'hFF;
2,3: gs_mem_mask = 0;
endcase
end
assign DDRAM_CLK = clk_aud;
ddram ddram
(
.*,
.addr(gs_mem_addr),
.dout(gs_mem_dout),
.din(gs_mem_din),
.we(gs_mem_wr),
.rd(gs_mem_rd),
.ready(gs_mem_ready)
);
wire gs_sel = (addr[7:0] ==? 'b1011?011) & ~&status[21:20];
localparam [3:0] comp_f = 4;
localparam [3:0] comp_a = 2;
localparam comp_x = ((32767 * (comp_f - 1)) / ((comp_f * comp_a) - 1)) + 1; // +1 to make sure it won't overflow
localparam comp_b = comp_x * comp_a;
function [15:0] compr; input [15:0] inp;
reg [15:0] v, v2;
begin
v = inp[15] ? (~inp) + 1'd1 : inp;
v2 = (v < comp_x[15:0]) ? (v * comp_a) : (((v - comp_x[15:0])/comp_f) + comp_b[15:0]);
compr = inp[15] ? ~(v2-1'd1) : v2;
end
endfunction
reg [15:0] audio_l, audio_r;
always @(posedge clk_aud) begin
reg [15:0] pre_l, pre_r;
pre_l <= {ts_l,4'd0} + {{3{gs_l[14]}}, gs_l[13:1]} + {2'b00, saa_l, 6'd0} + {3'b000, ear_out, mic_out, tape_aud, 10'd0};
pre_r <= {ts_r,4'd0} + {{3{gs_r[14]}}, gs_r[13:1]} + {2'b00, saa_r, 6'd0} + {3'b000, ear_out, mic_out, tape_aud, 10'd0};
audio_l <= compr(pre_l);
audio_r <= compr(pre_r);
end
assign AUDIO_L = audio_l;
assign AUDIO_R = audio_r;
//////////////////// VIDEO ///////////////////
wire ce_cpu_sn;
wire ce_cpu_sp;
wire [14:0] vram_addr;
wire [7:0] vram_dout;
wire [7:0] port_ff;
wire ulap_sel;
wire [7:0] ulap_dout;
reg mZX, m128;
always @(posedge clk_sys) begin
case(status[9:8])
0: {mZX, m128} <= 2'b10;
1: {mZX, m128} <= 2'b11;
default: {mZX, m128} <= 2'b00;
endcase
end
wire [1:0] scale = status[16:15];
wire HSync, VSync, HBlank, VBlank;
wire ulap_ena, ulap_mono, mode512;
wire ulap_avail = ~status[14] & ~trdos_en;
wire tmx_avail = ~status[13] & ~trdos_en;
wire snow_ena = status[25] & &turbo & ~plus3;
wire I,R,G,B;
wire [7:0] ulap_color;
ULA ULA(.*, .din(cpu_dout), .page_ram(page_ram[2:0]));
wire ce_sys = ce_7mp | (mode512 & ce_7mn);
reg ce_sys1;
always @(posedge clk_sys) begin
reg [1:0] ce_sys2;
ce_sys2 <= {ce_sys2[0],ce_sys};
ce_sys1 <= |ce_sys2;
end
assign CLK_VIDEO = clk_56;
reg ce_pix;
always @(posedge CLK_VIDEO) begin
reg ce1;
ce1 <= ce_sys1;
ce_pix <= ce1;
end
reg [7:0] Rx, Gx, Bx;
reg hs,vs,hbl,vbl;
always @(posedge CLK_VIDEO) if (ce_pix) begin
hs <= HSync;
if(~hs & HSync) vs <= VSync;
hbl <= HBlank;
vbl <= VBlank;
casex({ulap_ena, ulap_mono})
'b0X: {Gx,Rx,Bx} <= {G, G, {6{I & G}}, R, R, {6{I & R}}, B, B, {6{I & B}}};
'b10: {Gx,Rx,Bx} <= {{2{ulap_color[7:5]}}, ulap_color[7:6], {2{ulap_color[4:2]}}, ulap_color[4:3], {4{ulap_color[1:0]}}};
'b11: {Gx,Rx,Bx} <= {3{ulap_color}};
endcase
end
reg [9:0] vcrop;
reg [1:0] wide;
always @(posedge CLK_VIDEO) begin
vcrop <= 0;
wide[0] <= 0;
wide[1] <= status[38] & ~vcrop_en;
if(HDMI_WIDTH >= (HDMI_HEIGHT + HDMI_HEIGHT[11:1]) && !forced_scandoubler && !scale) begin
if(HDMI_HEIGHT == 480) vcrop <= 240;
if(HDMI_HEIGHT == 600) begin vcrop <= 200; wide <= vcrop_en; end
if(HDMI_HEIGHT == 720) vcrop <= 240;
if(HDMI_HEIGHT == 768) vcrop <= 256;
if(HDMI_HEIGHT == 800) begin vcrop <= 200; wide <= vcrop_en; end
if(HDMI_HEIGHT == 1080) vcrop <= status[29] ? 10'd216 : 10'd270;
if(HDMI_HEIGHT == 1200) vcrop <= 240;
end
end
reg en1080p;
always @(posedge CLK_VIDEO) en1080p <= (HDMI_WIDTH == 1920) && (HDMI_HEIGHT == 1080);
wire [1:0] ar = status[5:4];
wire vcrop_en = en1080p ? |status[29:28] : status[28];
wire vga_de;
video_freak video_freak
(
.*,
.VGA_DE_IN(vga_de),
.ARX((!ar) ? (wide[0] ? 12'd2903 : 12'd3307) : (ar - 1'd1)),
.ARY((!ar) ? 12'd2588 : 12'd0),
.CROP_SIZE(vcrop_en ? vcrop : 10'd0),
.CROP_OFF(0),
.SCALE(status[27:26])
);
video_mixer #(.LINE_LENGTH(896), .GAMMA(1)) video_mixer
(
.*,
.hq2x(scale == 1),
.scandoubler(scale || forced_scandoubler),
.freeze_sync(),
.VGA_DE(vga_de),
.HSync(hs),
.VSync(vs),
.HBlank(hbl),
.VBlank(vbl),
.R(Rx),
.G(Gx),
.B(Bx)
);