-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathCPU.js
3300 lines (2893 loc) · 119 KB
/
CPU.js
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 2015 by Paulo Augusto Peccin. See license.txt distributed with this file.
// This implementation fetches the base opcode at the FIRST clock cycle
// Then fetches operands and executes all operations of the instruction at the LAST clock cycle
// NMI is not supported. All IM modes supported, but data coming from device in bus will always be FFh (MSX). IFF2 is always the same as IFF1
// Original Z80 base clock: 3579545 Hz. Rectified to NTSC 60Hz: 3584160 Hz (228 clocks/line * 262 lines * 60 frames/sec)
// R800 clock is double Z80 clock, and processing pauses for ~4us each ~31us for memory refresh (~12.9% of the processing time)
// Thus it stops approx. twice each NTSC scanline, for about approx. 29 clocks
wmsx.CPU = function() {
"use strict";
var self = this;
var r800Timing = WMSX.R800_TIMING;
function init() {
defineZ80InstructionSet();
}
this.connectBus = function(aBus) {
bus = aBus;
};
this.setMachineType = function(machineType) {
r800Present = machineType >= wmsx.Machine.MACHINE_TYPE.MSXTR;
updateR800Present();
};
this.powerOn = function() {
setINT(0xff);
this.reset();
toAF(0xfffd); toBC(0xffff); DE = 0xffff; HL = 0xffff;
AF2 = 0xfffd; BC2 = 0xffff; DE2 = 0xffff; HL2 = 0xffff;
toIX(0xffff); toIY(0xffff); SP = 0xffff;
writeState(modeBackState);
};
this.powerOff = function() {
};
this.reset = function() {
r800 = false;
cpuCycles = 0; busCycles = 0; cpuToBusCycles = 0;
ackINT = false; prefix = 0;
T = 0; W = 0;
opcode = 0; instruction = undefined;
PC = 0; I = 0; R = 0; R7 = 0; IFF1 = 0; IM = 0;
extCurrRunning = null; extExtraIter = 0;
fetchForceNextBreak();
writeState(modeBackState);
updateInstructionSet();
};
this.setR800Mode = function(state) {
// console.log("Set R800 mode: " + state);
if (r800 === state) return;
r800 = state;
swapModeState();
updateInstructionSet();
updateClockMulti();
};
this.setZ80BUSRQ = function(pause) { // true = low = active
z80BUSRQ = pause;
};
function updateR800Present() {
if (r800Present) {
defineR800InstructionSet();
self.busClockPulses = self.busClockPulsesBoth; // VDP has to update this reference as well
} else
self.busClockPulses = self.busClockPulsesZ80;
}
function updateInstructionSet() {
if (r800) {
instructionsNoPrefix = instructionsNoPrefixR800;
instructionsByPrefix = instructionsByPrefixR800;
} else {
instructionsNoPrefix = instructionsNoPrefixZ80;
instructionsByPrefix = instructionsByPrefixZ80;
}
}
function defineZ80InstructionSet() {
if (!instructionsNoPrefixZ80[0]) defineInstructionSet(instructionsByPrefixZ80, false, false);
}
function defineR800InstructionSet() {
if (!instructionsNoPrefixR800[0]) defineInstructionSet(instructionsByPrefixR800, true, true);
}
function updateClockMulti() {
self.getBUSCycles(); // update before changing multi
clockMulti = r800 ? r800ClockMulti : z80ClockMulti;
}
this.busClockPulsesZ80 = function(busPulses) {
if (z80BUSRQ) return;
var toCycle = cpuCycles + ((busPulses * clockMulti) | 0);
for (; cpuCycles < toCycle; ++cpuCycles) {
if (--T > 0) continue;
if (T < 0) {
if (ackINT) acknowledgeINT();
else fetchNextInstruction();
} else
instruction.operation();
}
};
this.busClockPulsesBoth = function(busPulses) {
if (z80BUSRQ && !r800) return;
var toCycle = cpuCycles + ((busPulses * clockMulti) | 0);
for (; cpuCycles < toCycle; ++cpuCycles) {
if (--T > 0) continue;
if (T < 0) {
if (W > 0) { --W; continue; }
if (ackINT) acknowledgeINT();
else {
fetchNextInstruction();
if (T === 0) instruction.operation();
}
} else
instruction.operation();
}
};
this.busClockPulses = this.busClockPulsesZ80;
// this.busClockPulsesOld = function(busPulses) {
// var toCycle = cpuCycles + ((busPulses * clockMulti) | 0);
// for (; cpuCycles < toCycle; ++cpuCycles) {
// if (--T > 1) continue;
// if (T === 1) instruction.operation();
// else {
// if (W > 0) { --W; continue; }
// if (z80BUSRQ && !r800) continue;
// if (ackINT) acknowledgeINT();
// else {
// fetchNextInstruction();
// if (T === 1) instruction.operation();
// }
// }
// }
// };
// Called once every 228 clocks. 28 / 228 = ~4us refresh time each ~31.8us (~12.3% of processing time)
this.r800MemoryRefresh = function() {
if (r800) {
++R;
W += 28;
}
};
this.setINTChannel = function(chan, state) {
var val = state ? INT | (1 << chan) : INT & ~(1 << chan);
setINT(val);
};
function setINT(val) {
if (INT !== val) {
INT = val;
ackINT = val !== 0xff && IFF1 && prefix === 0;
}
}
this.getBUSCycles = function() {
busCycles += ((cpuCycles - cpuToBusCycles) / clockMulti) | 0;
cpuToBusCycles = cpuCycles;
return busCycles;
};
this.setZ80ClockMulti = function(multi) {
// console.log("Z80 CLOCK MULTI:" + multi);
z80ClockMulti = multi <= 0 ? 1 : multi > 8 ? 8 : multi; // (0..8]
if (!r800) updateClockMulti();
};
this.getZ80ClockMulti = function() {
return z80ClockMulti;
};
this.setR800ClockMulti = function(multi) {
// console.log("R800 CLOCK MULTI:" + multi);
r800ClockMulti = 2 * (multi <= 0 ? 1 : multi > 2 ? 2 : multi); // 2 * (0..2]
if (r800) updateClockMulti();
};
this.getR800ClockMulti = function() {
return r800ClockMulti / 2;
};
this.getClockFreqDesc = function(multi) {
return "" + (3.58 * multi).toFixed(2) + " MHz";
// switch (z80ClockMulti) {
// case 1: return "3.58 MHz";
// case 2: return "7.16 MHz";
// case 3: return "10.7 MHz";
// case 4: return "14.3 MHz";
// case 5: return "17.9 MHz";
// case 6: return "21.5 MHz";
// case 7: return "25.1 MHz";
// case 8: return "28.6 MHz";
// }
};
// Main processor mode
var r800 = false;
var r800Present = false;
var modeBackState = {}, modeFrontState = {};
var z80BUSRQ = false; // used to Halt Z80 when CPU Pause Key is ON
// Speed Control
var z80ClockMulti = 1, r800ClockMulti = 2; // relative to BUS clock
var clockMulti = z80ClockMulti; // active CPU multi
var cpuCycles = 0, busCycles = 0, cpuToBusCycles = 0;
// Extension Handling
var extCurrRunning = null;
var extExtraIter = 0;
// Interfaces
var bus;
var INT = 0xff; // 8 parallel INT channels. OR behavior (any bit set to 0 triggers INT)
// Registers
var PC = 0; // 16 bits
var SP = 0; // 16 bits
var A = 0;
var F = 0;
var B = 0;
var C = 0;
var DE = 0; // 16 bits
var HL = 0; // 16 bits
var IX = 0; // 16 bits
var IY = 0; // 16 bits
var AF2 = 0; // 16 bits
var BC2 = 0; // 16 bits
var DE2 = 0; // 16 bits
var HL2 = 0; // 16 bits
var I = 0;
var R = 0; // bits 6-0 of R, incremented
var R7 = 0; // bit 7 of R when set manually
// Interrupt flags and mode
var IFF1 = 0; // No IFF2 supported as NMI is not supported. Always the same as IFF1
var IM = 0;
// Status Bits references
var bS = 0x80, nS = 7;
var bZ = 0x40, nZ = 6;
var bF5 = 0x20, nF5 = 5;
var bH = 0x10, nH = 4;
var bF3 = 0x08, nF3 = 3;
var bPV = 0x04, nPV = 2;
var bN = 0x02, nN = 1;
var bC = 0x01, nC = 0;
// Fetch and Instruction control
var T = 0; // Clocks remaining in the current instruction
var W = 0; // Clocks remaining of additional wait states (besides M1)
var opcode = 0;
var prefix = 0;
var instruction;
var ackINT = false;
var fetchLastAddress = 0;
var instructionsNoPrefixZ80 = new Array(280);
var instructionsByPrefixZ80 = [
instructionsNoPrefixZ80, // 0
new Array(280), // 1 ED
new Array(280), // 2 CB
new Array(280), // 3 DD
new Array(280), // 4 FD
new Array(280), // 5 DDCB
new Array(280), // 6 FDCB
instructionsNoPrefixZ80 // 7 After EI
];
var instructionsNoPrefixR800 = new Array(280);
var instructionsByPrefixR800 = [
instructionsNoPrefixR800, // 0
new Array(280), // 1 ED
new Array(280), // 2 CB
new Array(280), // 3 DD
new Array(280), // 4 FD
new Array(280), // 5 DDCB
new Array(280), // 6 FDCB
instructionsNoPrefixR800 // 7 After EI
];
var instructionsAll = [];
var instructionsAllOld = [];
var instructionsNoPrefix, instructionsByPrefix;
var instrWait;
// Internal operations
function fetchNextInstruction() {
// if (DEBUG_PC_LOCATIONS[PC]) console.log("LOCATION: " + DEBUG_PC_LOCATIONS[PC]);
++R;
opcode = r800 ? fetchN_R800() : fetchN();
selectInstruction();
T = instruction.remainCycles;
}
// if (self.trace) self.breakpoint("TRACE");
// if (DEBUG_LOOP) {
// var pc = PC- 1;
// console.log((pc).toString(16) + ":", instruction.opcodeString);
//
// if (pc > 0x2200 && DEBUG_LOOP_PCS[pc] >= 2) DEBUG_LOOP = 0;
// else DEBUG_LOOP_PCS[pc] = (DEBUG_LOOP_PCS[pc] | 0) + 1;
// }
function acknowledgeINT() {
++R;
IFF1 = 0;
ackINT = false;
if (instruction.operation === HALT) pcInc(); // To "escape" from HALT, and continue in the next instruction after ISR
instruction = IM < 2 ? instructionsNoPrefix[258] : instructionsNoPrefix[259];
T = instruction.remainCycles;
}
function selectInstruction() {
if (prefix === 0) {
instruction = instructionsNoPrefix[opcode]; // always found
} else {
instruction = instructionsByPrefix[prefix][opcode] || instructionsNoPrefix[opcode]; // if nothing found, ignore prefix
if (INT !== 0xff && IFF1) ackINT = true;
prefix = 0;
}
}
function swapModeState() {
// console.log("SWAP STATE");
writeState(modeFrontState);
var back = modeBackState; modeBackState = modeFrontState; modeFrontState = back;
readState(modeFrontState);
}
function writeState(to) {
// to.busCycles = busCycles;
to.ackINT = ackINT;
to.prefix = prefix;
to.T = T; to.W = W; to.opcode = opcode;
to.PC = PC; to.SP = SP; to.I = I; to.R = R; to.IFF1 = IFF1; to.IM = IM;
to.AF = fromAF(); to.BC = fromBC(); to.DE = DE; to.HL = HL; to.IX = fromIX(); to.IY = fromIY();
to.AF2 = AF2; to.BC2 = BC2; to.DE2 = DE2; to.HL2 = HL2;
to.extCurrRunning = extCurrRunning; to.extExtraIters = extExtraIter;
}
function readState(from) {
// busCycles = from.busCycles;
ackINT = from.ackINT;
prefix = from.prefix;
T = from.T; W = from.W; opcode = from.opcode; instruction = instrWait;
PC = from.PC; SP = from.SP; I = from.I; R = from.R; IFF1 = from.IFF1; IM = from.IM;
toAF(from.AF); toBC(from.BC); DE = from.DE; HL = from.HL; toIX(from.IX); toIY(from.IY);
AF2 = from.AF2; BC2 = from.BC2; DE2 = from.DE2; HL2 = from.HL2;
extCurrRunning = from.extCurrRunning; extExtraIter = from.extExtraIters;
}
function fetchForceNextBreak() {
fetchLastAddress = 0x1ffff;
}
function busRead(addr) {
return bus.read(addr);
}
function busWrite(addr, val) {
bus.write(addr, val);
}
function fetchN() {
return busRead(pcInc());
}
function fetchNN() {
return fetchN() | (fetchN() << 8);
}
function memRead(addr) {
return busRead(addr);
}
function memRead16(addr) {
return busRead(addr) | (busRead((addr + 1) & 0xffff) << 8);
}
function memWrite(addr, val) {
busWrite(addr, val);
}
function memWrite16(addr, val) {
busWrite(addr, val & 255); busWrite((addr + 1) & 0xffff, val >>> 8);
}
function memWrite16Rev(addr, val) {
busWrite((addr + 1) & 0xffff, val >>> 8); busWrite(addr, val & 255);
}
function busInput(port) {
return bus.input(port);
}
function busOutput(port, val) {
bus.output(port, val);
}
function busRead_R800(addr) {
W += bus.getAccessWait(addr); // Add slot waits
return bus.read(addr);
}
function busWrite_R800(addr, val) {
W += bus.getAccessWait(addr); // Add slot waits
bus.write(addr, val);
}
function fetchN_R800() {
var addr = pcInc();
W += bus.getBreakWait(addr, fetchLastAddress); // Add page break
fetchLastAddress = addr;
return busRead_R800(addr);
}
function fetchNN_R800() {
return fetchN_R800() | (fetchN_R800() << 8);
}
function memRead_R800(addr) {
// Forced break for first memory read already at instruction T cycles
fetchForceNextBreak();
return busRead_R800(addr);
}
function memRead16_R800(addr) {
// Forced break for first memory read already at instruction T cycles
W += bus.getBreakWait(addr, addr + 1); // Add second read page break
fetchForceNextBreak();
return busRead_R800(addr) | (busRead_R800((addr + 1) & 0xffff) << 8);
}
function memWrite_R800(addr, val) {
// Forced break for first memory write already at instruction T cycles
fetchForceNextBreak();
busWrite_R800(addr, val);
}
function memWrite16_R800(addr, val) {
// Forced break for first memory write already at instruction T cycles
W += bus.getBreakWait(addr, addr + 1); // Add second write page break
fetchForceNextBreak();
busWrite_R800(addr, val & 255); busWrite_R800((addr + 1) & 0xffff, val >>> 8);
}
function memWrite16Rev_R800(addr, val) {
// Forced break for first memory write already at instruction T cycles
W += bus.getBreakWait(addr, addr + 1); // Add second write page break
fetchForceNextBreak();
busWrite_R800((addr + 1) & 0xffff, val >>> 8); busWrite_R800(addr, val & 255);
}
function busInput_R800(port) {
W += bus.getIOWait(port, clockMulti);
return bus.input(port);
}
function busOutput_R800(port, val) {
W += bus.getIOWait(port, clockMulti);
bus.output(port, val);
}
if (r800Timing !== 1) {
busRead_R800 = busRead;
busWrite_R800 = busWrite;
fetchN_R800 = fetchN;
fetchNN_R800 = fetchNN;
memRead_R800 = memRead;
memRead16_R800 = memRead16;
memWrite_R800 = memWrite;
memWrite16_R800 = memWrite16;
memWrite16Rev_R800 = memWrite16Rev;
}
if (r800Timing === 0) {
busInput_R800 = busInput;
busOutput_R800 = busOutput;
}
function pcInc() {
var old = PC;
PC = (PC + 1) & 0xffff;
return old;
}
function dec2PC() {
return PC = (PC - 2) & 0xffff;
}
function fromA() {
return A;
}
function fromB() {
return B;
}
function fromC() {
return C;
}
function fromD() {
return DE >>> 8;
}
function fromE() {
return DE & 0xff;
}
function fromH() {
return HL >>> 8;
}
function fromL() {
return HL & 0xff;
}
function fromIXh() {
return IX >>> 8;
}
function fromIXl() {
return IX & 0xff;
}
function fromIYh() {
return IY >>> 8;
}
function fromIYl() {
return IY & 0xff;
}
function toA(val) {
A = val;
}
function toB(val) {
B = val;
}
function toC(val) {
C = val;
}
function toD(val) {
DE = (DE & 0xff) | (val << 8);
}
function toE(val) {
DE = (DE & 0xff00) | val;
}
function toH(val) {
HL = (HL & 0xff) | (val << 8);
}
function toL(val) {
HL = (HL & 0xff00) | val;
}
function toIXh(val) {
IX = (IX & 0xff) | (val << 8);
}
function toIXl(val) {
IX = (IX & 0xff00) | val;
}
function toIYh(val) {
IY = (IY & 0xff) | (val << 8);
}
function toIYl(val) {
IY = (IY & 0xff00) | val;
}
function fromAF() {
return (A << 8) | F;
}
function fromBC() {
return (B << 8) | C;
}
function fromDE() {
return DE;
}
function fromHL() {
return HL;
}
function fromSP() {
return SP;
}
function fromIX () {
return IX;
}
function fromIY () {
return IY;
}
function toAF(val) {
A = val >>> 8; F = val & 0xff;
}
function toBC(val) {
B = val >>> 8; C = val & 0xff;
}
function toDE(val) {
DE = val;
}
function toHL(val) {
HL = val;
}
function toSP(val) {
SP = val;
}
function toIX(val) {
IX = val;
}
function toIY(val) {
IY = val;
}
function from_BC_8() {
return memRead(fromBC());
}
function from_BC_8_R800() {
return memRead_R800(fromBC());
}
function from_DE_8() {
return memRead(DE);
}
function from_DE_8_R800() {
return memRead_R800(DE);
}
function from_HL_8() {
return memRead(HL);
}
function from_HL_8_R800() {
return memRead_R800(HL);
}
function from_SP_16() { // bits
return memRead16(SP);
}
function from_SP_16_R800() { // bits
return memRead16_R800(SP);
}
function to_BC_8(val) {
memWrite(fromBC(), val);
}
function to_BC_8_R800(val) {
memWrite_R800(fromBC(), val);
}
function to_DE_8(val) {
memWrite(DE, val);
}
function to_DE_8_R800(val) {
memWrite_R800(DE, val);
}
function to_HL_8(val) {
memWrite(HL, val);
}
function to_HL_8_R800(val) {
memWrite_R800(HL, val);
}
function to_SP_16(val) {
memWrite16(SP, val);
}
function to_SP_16_R800(val) {
memWrite16_R800(SP, val);
}
var preReadIXYdOffset = 0;
function preReadIXYd() {
preReadIXYdOffset = fetchN();
}
function preReadIXYd_R800() {
preReadIXYdOffset = fetchN_R800();
}
function from_IXd_8() {
return memRead(sum16Signed(IX, fetchN()));
}
from_IXd_8.fromPreReadAddr = function() {
return memRead(sum16Signed(IX, preReadIXYdOffset));
};
function from_IXd_8_R800() {
return memRead_R800(sum16Signed(IX, fetchN_R800()));
}
from_IXd_8_R800.fromPreReadAddr = function() {
return memRead_R800(sum16Signed(IX, preReadIXYdOffset));
};
function from_IYd_8() {
return memRead(sum16Signed(IY, fetchN()));
}
from_IYd_8.fromPreReadAddr = function() {
return memRead(sum16Signed(IY, preReadIXYdOffset));
};
function from_IYd_8_R800() {
return memRead_R800(sum16Signed(IY, fetchN_R800()));
}
from_IYd_8_R800.fromPreReadAddr = function() {
return memRead_R800(sum16Signed(IY, preReadIXYdOffset));
};
function to_IXd_8(val) {
memWrite(sum16Signed(IX, fetchN()), val);
}
to_IXd_8.toPreReadAddr = function(val) {
memWrite(sum16Signed(IX, preReadIXYdOffset), val);
};
function to_IXd_8_R800(val) {
memWrite_R800(sum16Signed(IX, fetchN_R800()), val);
}
to_IXd_8_R800.toPreReadAddr = function(val) {
memWrite_R800(sum16Signed(IX, preReadIXYdOffset), val);
};
function to_IYd_8(val) {
memWrite(sum16Signed(IY, fetchN()), val);
}
to_IYd_8.toPreReadAddr = function(val) {
memWrite(sum16Signed(IY, preReadIXYdOffset), val);
};
function to_IYd_8_R800(val) {
memWrite_R800(sum16Signed(IY, fetchN_R800()), val);
}
to_IYd_8_R800.toPreReadAddr = function(val) {
memWrite_R800(sum16Signed(IY, preReadIXYdOffset), val);
};
function from_NN_8() {
return memRead(fetchNN());
}
function from_NN_8_R800() {
return memRead_R800(fetchNN_R800());
}
function to_NN_8(val) {
memWrite(fetchNN(), val);
}
function to_NN_8_R800(val) {
memWrite_R800(fetchNN_R800(), val);
}
function from_NN_16() {
return memRead16(fetchNN());
}
function from_NN_16_R800() {
return memRead16_R800(fetchNN_R800());
}
function to_NN_16(val) {
memWrite16(fetchNN(), val);
}
function to_NN_16_R800(val) {
memWrite16_R800(fetchNN_R800(), val);
}
function push16(val) {
SP = (SP - 2) & 0xffff;
memWrite16Rev(SP, val); // Write High first, then Low
}
function push16_R800(val) {
SP = (SP - 2) & 0xffff;
memWrite16Rev_R800(SP, val); // Write High first, then Low
}
function pop16() {
var res = memRead16(SP);
SP = (SP + 2) & 0xffff;
return res;
}
function pop16_R800() {
var res = memRead16_R800(SP);
SP = (SP + 2) & 0xffff;
return res;
}
function sum16Signed(a, b) {
return (a + (b > 127 ? (-256 + b) : b)) & 0xffff;
}
var parities = [ // 0b00000100 ready for P flag
4,0,0,4,0,4,4,0,0,4,4,0,4,0,0,4,0,4,4,0,4,0,0,4,4,0,0,4,0,4,4,0,0,4,4,0,4,0,0,4,4,0,0,4,0,4,4,0,
4,0,0,4,0,4,4,0,0,4,4,0,4,0,0,4,0,4,4,0,4,0,0,4,4,0,0,4,0,4,4,0,4,0,0,4,0,4,4,0,0,4,4,0,4,0,0,4,
4,0,0,4,0,4,4,0,0,4,4,0,4,0,0,4,0,4,4,0,4,0,0,4,4,0,0,4,0,4,4,0,0,4,4,0,4,0,0,4,4,0,0,4,0,4,4,0,
4,0,0,4,0,4,4,0,0,4,4,0,4,0,0,4,4,0,0,4,0,4,4,0,0,4,4,0,4,0,0,4,0,4,4,0,4,0,0,4,4,0,0,4,0,4,4,0,
4,0,0,4,0,4,4,0,0,4,4,0,4,0,0,4,0,4,4,0,4,0,0,4,4,0,0,4,0,4,4,0,0,4,4,0,4,0,0,4,4,0,0,4,0,4,4,0,
4,0,0,4,0,4,4,0,0,4,4,0,4,0,0,4
];
// Instruction Cores ---------------------------------------------------------------
function NOP() {
}
function HALT() {
//Util.log("HALT!");
//self.breakpoint("HALT");
--PC; // Keep repeating HALT instruction until an INT or RESET. Performance trade-off: does not check for PC underflow
}
function newLD(to, from) {
return function LD() {
to(from());
};
}
function LDAI() {
A = I;
// Flags
F = (F & 0x01) // H = 0; N = 0; C = C
| (A & 0xA8) // S = A is negative; f5, f3 copied from A
| ((A === 0) << nZ) // Z = A is 0
| (IFF1 << nPV); // PV = IFF2 (same as IFF1)
}
function LDAR() {
A = R7 | (R & 0x7f);
// Flags
F = (F & 0x01) // H = 0; N = 0; C = C
| (A & 0xA8) // S = A is negative; f5, f3 copied from A
| ((A === 0) << nZ) // Z = A is 0
| (IFF1 << nPV); // PV = IFF2 (same as IFF1)
}
function LDIA() {
I = A;
}
function LDRA() {
R = A;
R7 = A & 0x80; // R can have bit 7 = 1 if set manually. Store bit 7
}
function newPUSH(from, r8) {
return r8
? function PUSH_R800() {
push16_R800(from());
}
: function PUSH() {
push16(from());
};
}
function newPOP(to, r8) {
return r8
? function POP_R800() {
to(pop16_R800());
}
: function POP() {
to(pop16());
};
}
function EXDEHL() {
var temp = DE;
DE = HL;
HL = temp;
}
function EXX() {
var temp = fromBC();
toBC(BC2);
BC2 = temp;
temp = DE;
DE = DE2;
DE2 = temp;
temp = HL;
HL = HL2;
HL2 = temp;
}
function EXAFAF2() {
var temp = fromAF();
toAF(AF2);
AF2 = temp;
}
function newLDI(r8) {
var from = r8 ? from_HL_8_R800 : from_HL_8;
var to = r8 ? to_DE_8_R800 : to_DE_8;
return function LDI() {
to(from());
DE = (DE + 1) & 0xffff;
HL = (HL + 1) & 0xffff;
if (--C < 0) { C = 0xff; B = (B - 1) & 0xff; } // BC--
// Flags
F = (F & 0xc1) // S = S; Z = Z; f5 = ?; H = 0; f3 = ?; N = 0; C = C;
| ((B + C !== 0) << nPV); // PV = BC != 0
// Verify: Undocumented f5/f3 behavior for all LD block instructions, not implemented. Left 0
}
}
function newLDIR(ldi, w8) {
var w = w8 ? 1 : 5;
return function LDIR() {
ldi();
if (F & bPV) {
dec2PC(); // Repeat this instruction
T += w; instruction = instrWait;
}
}
}
function newLDD(r8) {
var from = r8 ? from_HL_8_R800 : from_HL_8;
var to = r8 ? to_DE_8_R800 : to_DE_8;
return function LDD() {
to(from());
DE = (DE - 1) & 0xffff;
HL = (HL - 1) & 0xffff;
if (--C < 0) { C = 0xff; B = (B - 1) & 0xff; } // BC--
// Flags
F = (F & 0xc1) // S = S; Z = Z; f5 = ?; H = 0; f3 = ?; N = 0; C = C;
| ((B + C !== 0) << nPV); // PV = BC != 0
}
}
function newLDDR(ldd, w8) {
var w = w8 ? 1 : 5;
return function LDDR() {
ldd();
if (F & bPV) {
dec2PC(); // Repeat this instruction
T += w; instruction = instrWait;
}
}
}
function newCPI(r8) {
var from = r8 ? from_HL_8_R800 : from_HL_8;
return function CPI() {
var val = from();
if (--C < 0) { C = 0xff; B = (B - 1) & 0xff; } // BC--
HL = (HL + 1) & 0xffff;
// Flags
var res = A - val;
var compare = A ^ val ^ res;
F = (F & bC) | bN // N = 1; C = C
| (res & 0xa8) // S = res is negative; f5, f3 copied from res
| ((res === 0) << nZ) // Z = res is 0
| (compare & bH) // H = borrow from bit 4
| ((B + C !== 0) << nPV); // PV = BC != 0
}
}
function newCPIR(cpi, w8) {
var w = w8 ? 1 : 5; // r800 VERIFY
return function CPIR() {
cpi();
if ((F & bPV) && !(F & bZ)) {
dec2PC(); // Repeat this instruction
T += w; instruction = instrWait;
}
}
}
function newCPD(r8) {
var from = r8 ? from_HL_8_R800 : from_HL_8;
return function CPD() {
var val = from();
if (--C < 0) { C = 0xff; B = (B - 1) & 0xff; } // BC--
HL = (HL - 1) & 0xffff;
// Flags
var res = A - val;
var compare = A ^ val ^ res;
F = (F & bC) | bN // N = 1; C = C
| (res & 0xa8) // S = res is negative; f5, f3 copied from res
| ((res === 0) << nZ) // Z = res is 0
| (compare & bH) // H = borrow from bit 4
| ((B + C !== 0) << nPV); // PV = BC != 0
}
}
function newCPDR(cpd, w8) {
var w = w8 ? 1 : 5;