-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathz80.v
4091 lines (3420 loc) · 71.8 KB
/
z80.v
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 (C) 2022-2023 nukeykt
*
* This file is part of Nuked-MD.
*
* 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.
*
* Z80 emulator
* Thanks:
* Antoine Bercovici:
* Z80 decap & die shot.
* Visual6502 team:
* VisualZ80 simulator.
* org, andkorzh, HardWareMan (emu-russia):
* help & support.
*/
// Z80(NMOS)
module z80cpu
(
input MCLK,
input CLK,
output [15:0] ADDRESS,
output ADDRESS_z,
input [7:0] DATA_i,
output [7:0] DATA_o,
output DATA_z,
output M1,
output MREQ,
output MREQ_z,
output IORQ,
output IORQ_z,
output RD,
output RD_z,
output WR,
output WR_z,
output RFSH,
output HALT,
input WAIT,
input INT,
input NMI,
input RESET,
input BUSRQ,
output BUSAK
);
wire clk = CLK;
wire w1;
wire w2;
wire w3;
wire w4, w4_i;
wire w5;
wire w6, w6_i;
wire w7;
wire w8, w8_i;
wire w9_n, w9_i;
reg w9 = 1'h0;
wire w10;
wire w11;
wire w12;
wire w13;
wire w14;
wire w15;
wire w16;
wire w18, w18_i;
wire w19, w19_i;
wire w21, w21_i;
wire w22, w22_i;
wire w23;
wire w24;
wire w25;
wire w26;
wire w27;
wire w28;
reg w30 = 1'h0;
wire w31, w31_i;
wire w32;
wire w33, w33_i;
wire w34, w34_i;
wire w35;
wire w36;
wire w37, w37_i; //
wire w38;
wire w39, w39_i;
reg w40 = 1'h0, w40_i = 1'h1;
wire w41;
wire w42;
wire w43;
wire w44, w44_n, w44_i;
wire w45;
wire w46;
wire w47;
wire w48, w48_i;
wire w49;
wire w50, w50_i;
wire w51, w51_i;
wire w52;
wire w53;
wire w54;
wire w55;
wire w56;
wire w57;
wire w58, w58_i;
wire w59;
wire w60;
wire w61, w61_i;
wire w62;
wire w63, w63_t;
wire w65;
wire w66, w66_i;
wire w67;
wire o_busak;
wire w68, w68_i;
wire w69;
wire w71;
reg w73 = 1'h0;
reg w74 = 1'h0;
wire w75;
wire w76;
wire w77;
reg w78_i = 1'h0;
wire w78;
wire w79;
reg w80 = 1'h0;
wire w81;
wire w82;
wire w83;
wire w84;
wire w85;
wire w86;
wire w87;
wire w88;
wire w89;
wire w90;
wire w91;
reg w92;
wire w93;
wire w94;
reg w95_i;
wire w95;
wire w96;
wire w97;
wire w98;
wire w99;
reg w100;
wire w101;
wire w102;
wire w103;
wire w104;
wire w105;
wire w106;
wire w107;
wire w109_i;
wire w109;
wire w110;
wire w111;
wire w112;
wire w113;
wire w114, w114_i;
wire w115, w115_i;
wire w116;
wire w117;
wire w118;
wire w119;
wire w120, w120_i;
wire w121, w121_i;
wire w122;
wire w123, w123_i;
wire w124;
wire w125;
wire w126;
wire w127, w127_i;
wire w128;
wire w129;
wire rfsh_rs, rfsh;
wire w130;
wire w131, w131_i;
wire w132;
wire w133;
wire w134;
wire w135;
wire w136;
wire w137;
wire w138;
wire w139;
wire w140;
wire w141;
wire w142;
wire w143;
wire w144;
reg [7:0] w145 = 8'h0;
reg [7:0] w146 = 8'h0; // bus 1
reg [7:0] w147_prev = 8'h0;
wire [7:0] w147;
wire w148;
wire w149;
wire w150;
wire w151;
wire w152;
wire w153;
wire w154;
wire w155;
wire w156;
wire w157;
wire w158;
wire w159;
wire w160;
wire w161;
wire w162;
wire w163;
wire w164;
wire w165;
wire w166;
wire w167;
wire w168;
wire w169;
wire w170;
wire w171;
wire w172;
wire w173;
wire w174;
wire w175;
wire w176;
wire w177;
wire w178;
wire w179;
wire w180;
wire w181;
wire w182;
wire w183;
wire w184;
wire w185;
wire w186;
wire w187;
wire w188;
wire w189;
wire w190;
wire w191;
wire w192;
wire w193;
wire w194;
wire w195;
wire w196;
wire w197;
wire w198;
wire w199;
wire w200;
wire w201;
wire w202;
wire w203;
wire w204;
wire w205;
wire w206;
wire w207;
wire w208;
wire w209;
wire w210, w210_i;
wire w211;
wire w212;
wire w213;
wire w214;
wire w215;
wire w216;
wire w217;
wire w218;
wire w219;
wire w220;
wire w221;
wire w222;
wire w223;
wire w224;
wire w225;
wire w226;
wire w227;
wire w228;
wire w229;
wire w230;
wire w231;
wire w232;
wire w233;
wire w234;
wire w235;
wire w236;
wire w237;
wire w238;
wire w239;
wire w240;
wire w241;
wire w242;
wire w243;
wire w244;
wire w245;
wire w246;
wire w247;
wire w248;
wire w249;
wire w250;
wire w251;
wire w252;
wire w253;
wire w254;
wire w255;
wire w256;
wire w257;
wire w258;
wire w259;
wire w260;
wire w261;
wire w262;
wire w263;
wire w264;
wire w265;
wire w266;
wire w267;
wire w268;
wire w269;
wire w270;
wire w271;
wire w272;
wire w273;
wire w274;
wire w275;
wire w276;
wire w277;
wire w278;
wire w279;
wire w280;
wire w281;
wire w282;
wire w283;
wire w284;
wire w285;
wire w286;
wire w287;
wire w288;
wire w289;
wire w290;
wire w291;
wire w292;
wire w293;
wire w294;
wire w295;
wire w296;
wire w297;
wire w298;
wire w299;
wire w300;
wire w301;
reg w302 = 1'h0;
wire w303;
wire w304;
reg w304_r = 1'h0;
wire w305;
wire w306;
wire w307;
wire w308;
wire w309;
wire w310;
wire w311;
wire w312;
wire w313;
wire w314;
wire w315;
wire w316;
wire w317;
wire w318;
wire w319;
reg w320 = 1'h0;
wire w321;
wire w322;
wire w323;
wire w324;
wire w325;
wire w326;
wire w327_n, w327_i;
reg w327 = 1'h0;
wire w328;
wire w329;
reg w329_r = 1'h0;
wire w330_n, w330_i;
wire w331;
reg w331_r = 1'h0;
wire w332_n, w332_i;
wire w333;
wire w334;
wire w335;
wire w336;
wire w337;
wire w338;
wire w339;
wire w340;
wire w341;
wire w342;
wire w343;
wire w344;
wire w345;
wire w346;
wire w347;
wire w348;
wire w349;
wire w350;
wire w351;
wire w352;
wire w353;
wire w354;
wire w355;
wire w356;
wire w357;
wire w358;
wire w359;
wire w360;
wire w361_n, w361_i;
wire w362;
wire w363;
wire w364;
wire w365;
wire w366;
wire w367;
wire w368;
wire w369;
wire w370;
wire w371;
wire w372;
wire w373;
wire w374;
wire w375;
wire w376;
wire w377;
wire w378_1, w378_2, w378;
wire w379_1, w379_2, w379;
wire w380, w380_i;
wire w381;
wire w382;
wire w383;
wire w384;
wire w385;
wire w386;
wire w387;
wire w388;
wire w389;
wire w390;
wire w391;
reg w392 = 1'h0;
wire w393;
wire w394;
wire w395;
wire w396;
wire w397;
wire w398;
wire w399;
wire w400, w400_v;
wire w401;
wire w402;
wire w403;
wire w404;
wire w405;
wire w406;
wire w407;
wire w408;
wire w409;
wire w410;
wire w411;
wire w412;
wire w413;
wire w414;
wire w415;
wire w416;
wire w417;
wire w418;
wire w419;
reg w420 = 1'h0;
wire w421;
wire w422;
wire w423;
wire w424;
reg w425 = 1'h0;
wire w426;
wire w427;
wire w428;
wire w429;
wire w430;
wire w431;
wire w432;
wire w433;
wire w434;
wire w435;
wire w436;
wire w437;
wire w438;
wire w439;
wire w440;
reg w441 = 1'h0;
wire w442, w442_i;
wire w443;
wire w444;
reg w445 = 1'h0;
wire w446;
wire w448;
wire w449;
reg w450 = 1'h0;
wire w452;
wire w453;
wire w454;
wire w455;
wire w456;
wire w457;
wire w458;
wire w459;
wire w460;
wire w461;
wire w462;
wire w463;
reg w464 = 1'h0;
wire w465;
wire w466;
wire w467;
wire w468;
wire w469;
wire w470;
wire w471;
wire w472;
reg w473 = 1'h0;
wire w474;
wire w475;
reg w476 = 1'h0;
wire w477;
wire w479;
wire w480;
wire w481;
wire w483;
reg [7:0] w484 = 8'h0; // bus 2
wire w485;
wire w486;
wire w487;
wire w490;
wire w491;
wire w492;
wire w493;
wire w494;
wire w495;
reg [7:0] w496 = 8'h0;
wire [7:0] w497;
reg [7:0] w498 = 8'h0;
reg [3:0] w499 = 4'h0;
wire [3:0] w500;
wire w501;
wire w502;
reg [3:0] w503 = 4'h0;
wire [3:0] w504;
wire w505;
wire w506;
wire w507;
wire w508;
reg [7:0] w510 = 8'h0;
reg [7:0] w511 = 8'h0;
wire [3:0] w512;
reg [7:0] w513 = 8'h0; // bus 3
wire [15:0] rpull1[1:0];
wire [15:0] rpull2[1:0];
wire [15:0] rpull1_comb[1:0];
wire [15:0] rpull2_comb[1:0];
wire [15:0] rpullup1[1:0];
wire [15:0] rpullup2[1:0];
wire [15:0] rpullup1_comb[1:0];
wire [15:0] rpullup2_comb[1:0];
reg [15:0] regs[11:0][1:0];
reg [15:0] regs2[1:0][1:0];
reg [15:0] w514 = 16'h0;
reg [15:0] w515 = 16'h0;
wire w516;
wire w517;
wire w518;
wire w519;
reg [15:0] w520 = 16'h0;
reg [15:0] w521 = 16'h0;
reg [15:0] w522 = 16'h0;
wire [15:0] w523;
reg w524 = 1'h0;
wire [14:0] w525;
reg [15:0] w526 = 16'h0;
reg [15:0] w527 = 16'h0;
wire [15:0] w528;
wire w530;
wire w531;
wire w532;
wire halt, halt_i;
wire m1;
wire l1;
wire l2;
wire l3;
wire l4;
wire l5;
wire l6;
wire l7;
wire l8;
wire l9;
wire l10;
wire l11;
wire l12;
wire l13;
wire l14;
wire l15;
wire l16;
wire l17;
wire l18;
wire l19;
wire l20;
wire l21;
wire l22;
wire l23;
wire l24;
wire l25;
wire l26;
wire l27;
wire l28;
wire l29;
wire l30;
wire l31;
wire l32;
wire l33;
wire l34;
wire l35;
wire l36;
wire l37;
wire l38;
wire l39;
wire l40;
wire l41;
wire l42;
wire l43;
wire l44;
wire l45;
wire l46, l46_i;
wire l47, l47_i;
wire l48, l48_i;
wire l49, l49_i;
wire l50;
wire l51;
wire l52;
wire l53;
wire l54;
wire l55;
wire l56;
wire l57;
wire l58;
wire l59;
wire l60;
wire l61;
wire l62;
wire l63;
wire l64;
wire l65;
wire l66;
wire l67;
wire l68;
wire l70;
wire l71;
wire l72;
wire l73;
wire l75;
wire l76;
wire l77;
wire l79;
wire l81;
wire l82;
wire l83;
wire l84;
// pla
wire [98:0] pla;
//
wire w1_i;
assign w1 = ~w1_i;
z80_rs_trig_nor rs1
(
.MCLK(MCLK),
.rst(clk & w3 & w41),
.set(w55 | (clk & (w114 | w201))),
.q(w1_i),
.nq()
);
z80_dlatch dl1
(
.MCLK(MCLK),
.en(clk),
.inp(w69),
.outp(l1)
);
z80_rs_trig_nor rs2
(
.MCLK(MCLK),
.rst((clk & w131 & w41) | (~clk & ~l1)),
.set(clk & w15),
.q(w2),
.nq()
);
assign w3 = ~(w201 | w202);
z80_rs_trig_nand rs4
(
.MCLK(MCLK),
.nset(clk | ~INT),
.nrst(clk | INT),
.q(w4),
.nq(w4_i)
);
z80_rs_trig_nor rs5
(
.MCLK(MCLK),
.rst(clk & w4_i),
.set(clk & w4),
.q(w5),
.nq()
);
z80_dlatch dl2
(
.MCLK(MCLK),
.en(clk),
.inp(~(w55 | w19)),
.outp(l2)
);
wire nmi = ~NMI;
z80_rs_trig_nor rs7
(
.MCLK(MCLK),
.rst(~l2),
.set(~nmi),
.q(w7),
.nq()
);
z80_rs_trig_nor rs6
(
.MCLK(MCLK),
.rst(~l2 | (nmi & ~w7)),
.set(nmi & w7),
.q(w6),
.nq(w6_i)
);
z80_rs_trig_nand rs8
(
.MCLK(MCLK),
.nset(clk | w6_i),
.nrst(clk | w6),
.q(w8),
.nq(w8_i)
);
z80_rs_trig_nor rs9
(
.MCLK(MCLK),
.rst(clk & w8_i),
.set(clk & w8),
.q(w9_n),
.nq(w9_i)
);
always @(posedge MCLK)
begin
if (w9_i)
w9 <= 1'h0;
else if (w9_n)
w9 <= 1'h1;
end
assign w10 = ~(w12 | w9 | w11);
assign w11 = ~(w12 | w9 | ~pla[3]);
z80_dlatch dl3
(
.MCLK(MCLK),
.en(clk),
.inp(~w73 | pla[1]),
.outp(l3)
);
assign w12 = ~(w5 | w9 | l3);
assign w13 = ~((w16 & ~w10) | w18 | w19 | halt);
assign w14 = ~(w13 | (w16 & w10));
assign w15 = ~(~w114 | w202 | w201);
z80_dlatch dl4
(
.MCLK(MCLK),
.en(clk),
.inp(~(w55 | ~w97 | ~w118 | w133)),
.outp(l4)
);
assign w16 = l4 & ~clk;
z80_rs_trig_nor rs18
(
.MCLK(MCLK),
.rst(w16 & w12),
.set((w16 & ~w12) | w55),
.q(w18_i),
.nq()
);
assign w18 = ~w18_i;
z80_rs_trig_nor rs19
(
.MCLK(MCLK),
.rst(w16 & w9),
.set((w16 & ~w9) | w55),
.q(w19_i),
.nq()
);
assign w19 = ~w19_i;
z80_rs_trig_nor rs21
(
.MCLK(MCLK),
.rst(w32 | w26),
.set(w24),
.q(w21),
.nq(w21_i)
);
//assign MREQ = ~w21_i ? 1'h0 : ((~w21 & ~w62) ? 1'h1 : 1'hz);
assign MREQ = w21_i;
assign MREQ_z = w21_i & w62;
z80_rs_trig_nor rs22
(
.MCLK(MCLK),
.rst(w26 | w32),
.set(w23 | (w36 & clk)),
.q(w22),
.nq(w22_i)
);
//assign IORQ = ~w22_i ? 1'h0 : ((~w22 & ~w62) ? 1'h1 : 1'hz);
assign IORQ = w22_i;
assign IORQ_z = w22_i & w62;
z80_dlatch dl5
(
.MCLK(MCLK),
.en(clk),
.inp(w35),
.outp(l5)
);
assign w23 = ~clk & ~l5;
z80_dlatch dl6
(
.MCLK(MCLK),
.en(clk),
.inp(w27),
.outp(l6)
);
assign w24 = ~clk & ~w202 & ~l6;
assign w25 = ~(w24 | w23 | (w36 & clk));
assign w26 = w131 & w41 & clk;
assign w27 = !((w110 & w93) | (w131 & (w41 | (w110 & ~w18))));
z80_dlatch dl7
(
.MCLK(MCLK),
.en(clk),
.inp(w57),
.outp(l7)
);
assign w28 = ~(halt | (w18 & w80) | w55 | w19 | ~(w18 | l7));
always @(posedge MCLK)
begin
if (w55)
w30 <= 1'h1;
else if (clk)
w30 <= w30;
else if (w103)
w30 <= ~w28;
end
z80_dlatch dl8
(
.MCLK(MCLK),
.en(clk),
.inp(w101),
.outp(l8)
);
z80_rs_trig_nor rs31
(
.MCLK(MCLK),
.rst(w26 | w32),
.set(~w25 & l8),
.q(w31),
.nq(w31_i)
);
//assign RD = ~w31_i ? 1'h0 : ((~w31 & ~w62) ? 1'h1 : 1'hz);
assign RD = w31_i;
assign RD_z = w31_i & w62;
z80_dlatch dl9
(
.MCLK(MCLK),
.en(clk),
.inp(w94),
.outp(l9)
);
assign w32 = ~clk & l9;
z80_rs_trig_nor rs33
(
.MCLK(MCLK),
.rst(~clk & ~l10),
.set(~l11 | (clk & w106 & w114 & w201)),
.q(w33),
.nq(w33_i)
);
//assign WR = ~w33_i ? 1'h0 : ((~w33 & ~w62) ? 1'h1 : 1'hz);
assign WR = w33_i;
assign WR_z = w33_i & w62;
z80_dlatch dl10
(
.MCLK(MCLK),
.en(clk),
.inp(~w41 & ~w55),
.outp(l10)
);
z80_dlatch dl11
(
.MCLK(MCLK),
.en(clk),
.inp(~(w114 & w201)),
.outp(l11)
);
wire w34_v = ~(l12 & w112);
z80_rs_trig_nand rs34
(
.MCLK(MCLK),
.nset(clk | ~w34_v),
.nrst(clk | w34_v),
.q(w34),
.nq(w34_i)
);
z80_dlatch dl12
(