This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathx86cpu.js
4415 lines (4161 loc) · 160 KB
/
x86cpu.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
/**
* @fileoverview Implements PCjs 8086/8088 CPU logic.
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @version 1.0
* Created 2012-Sep-05
*
* Copyright © 2012-2016 Jeff Parsons <[email protected]>
*
* This file is part of PCjs, which is part of the JavaScript Machines Project (aka JSMachines)
* at <http://jsmachines.net/> and <http://pcjs.org/>.
*
* PCjs 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 3
* of the License, or (at your option) any later version.
*
* PCjs 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 PCjs. If not,
* see <http://www.gnu.org/licenses/gpl.html>.
*
* You are required to include the above copyright notice in every source code file of every
* copy or modified version of this work, and to display that copyright notice on every screen
* that loads or runs any version of this software (see Computer.COPYRIGHT).
*
* Some PCjs files also attempt to load external resource files, such as character-image files,
* ROM files, and disk image files. Those external resource files are not considered part of the
* PCjs program for purposes of the GNU General Public License, and the author does not claim
* any copyright as to their contents.
*/
"use strict";
if (NODE) {
var str = require("../../shared/lib/strlib");
var web = require("../../shared/lib/weblib");
var Component = require("../../shared/lib/component");
var Messages = require("./messages");
var Memory = require("./memory");
var State = require("./state");
var CPU = require("./cpu");
var X86 = require("./x86");
var X86Seg = require("./x86seg");
}
/**
* X86CPU(parmsCPU)
*
* The X86CPU class uses the following (parmsCPU) properties:
*
* model: a number (eg, 8088) that should match one of the X86.MODEL values (default is 8088)
* stepping: a string (eg, "B1") that should match one of the X86.STEPPING values (default is "")
*
* This extends the CPU class and passes any remaining parmsCPU properties to the CPU class
* constructor, along with a default speed (cycles per second) based on the specified (or default)
* CPU model number.
*
* The X86CPU class was initially written to simulate a 8086/8088 microprocessor, although over time
* it has evolved to support later microprocessors (eg, the 80186/80188 and the 80286, including
* protected-mode support).
*
* This is a logical simulation, not a physical simulation, and performance is critical, second only
* to the accuracy of the simulation when running real-world x86 software. Consequently, it takes a
* few liberties with the operation of the simulated hardware, especially with regard to timings,
* little-used features, etc. We do make an effort to maintain approximate instruction cycle counts,
* but there are many other obstacles (eg, prefetch queue, wait states) to achieving accurate timings.
*
* For example, our 8237 DMA controller performs all DMA transfers immediately, since internally
* they are all memory-to-memory, and attempting to interleave DMA cycles with instruction execution
* cycles would hurt overall performance. Similarly, 8254 timer counters are updated only on-demand.
*
* The 8237 and 8254, along with the 8259 interrupt controller and several other "chips", are combined
* into a single ChipSet component, to keep the number of components we juggle to a minimum.
*
* All that being said, this does not change the overall goal: to produce as accurate a simulation as
* possible, within the limits of what JavaScript allows and how precisely/predictably it behaves.
*
* @constructor
* @extends CPU
* @param {Object} parmsCPU
*/
function X86CPU(parmsCPU)
{
this.model = parmsCPU['model'] || X86.MODEL_8088;
/*
* We take the 'stepping' value, convert it to a hex value, and then add that to the model to provide
* a single value that's unique for any given CPU stepping. If no stepping is provided, then stepping
* is equal to model.
*/
var stepping = parmsCPU['stepping'];
this.stepping = this.model + (stepping? str.parseInt(stepping, 16) : 0);
var nCyclesDefault = 0;
switch(this.model) {
case X86.MODEL_8088:
default:
nCyclesDefault = 4772727;
break;
case X86.MODEL_80286:
nCyclesDefault = 6000000;
break;
case X86.MODEL_80386:
nCyclesDefault = 16000000;
break;
}
CPU.call(this, parmsCPU, nCyclesDefault);
/*
* Initialize processor operation to match the requested model
*/
this.initProcessor();
/*
* List of software interrupt notification functions: aIntNotify is an array, indexed by
* interrupt number, where each element contains:
*
* registered function to call for every software interrupt
*
* The registered function is called with the linear address (LIP) following the software interrupt;
* if any function returns false, the software interrupt will be skipped (presumed to be emulated),
* and no further notification functions will be called.
*
* NOTE: Registered functions are called only for INT N instructions -- *not* INT 0x03 or INTO or the
* INT 0x00 generated by a divide-by-zero or any other kind of interrupt (nor any interrupt simulated
* with PUSHF/CALLF).
*
* aIntReturn is a hash of return address notifications set up by software interrupt notification
* functions that want to receive return notifications. A software interrupt function must call
* cpu.addIntReturn(fn).
*
* WARNING: There's no mechanism in place to insure that software interrupt return notifications don't
* get "orphaned" if an interrupt handler bypasses the normal return path (INT 0x24 is one example of an
* "evil" software interrupt).
*/
this.aIntNotify = [];
this.aIntReturn = [];
/*
* Since aReturnNotify is a "sparse array", this global count gives the CPU a quick way of knowing whether
* or not RETF or IRET instructions need to bother calling checkIntReturn().
*/
this.cIntReturn = 0;
/*
* A variety of stepCPU() state variables that don't strictly need to be initialized before the first
* stepCPU() call, but it's good form to do so.
*/
this.resetCycles();
this.flags.fComplete = this.flags.fDebugCheck = false;
/*
* If there are no live registers to display, then updateStatus() can skip a bit....
*/
this.cLiveRegs = 0;
/*
* We're just declaring aMemBlocks and associated Bus parameters here; they'll be initialized by initMemory()
* when the Bus is initialized.
*/
this.aBusBlocks = this.aMemBlocks = [];
this.nBusMask = this.nMemMask = 0;
this.nBlockShift = this.nBlockSize = this.nBlockLimit = this.nBlockTotal = this.nBlockMask = 0;
if (PREFETCH) {
this.cbPrefetch = 0;
this.adwPrefetch = null;
}
/*
* This initial resetRegs() call is important to create all the registers (eg, the X86Seg registers),
* so that if/when we call restore(), it will have something to fill in.
*/
this.resetRegs();
}
Component.subclass(X86CPU, CPU);
if (PREFETCH) {
/*
* NOTE: X86CPU.PFINFO.LENGTH must be set to a power of two, so that LENGTH - 1 will form a mask
* (IP_MASK) we can use to create a sliding prefetch window of LENGTH bytes. We also zero the low
* 2 bits of IP_MASK so that the sliding window always starts on a 32-bit (long) boundary. Finally,
* instead breaking breaking all the longs we prefetch into bytes, we simply store the longs as-is
* into every 4th element of the queue (the queue is sparse array).
*/
X86CPU.PFINFO = {
LENGTH: 16 // 16 generates a 16-byte prefetch queue consisting of 4 32-bit entries
};
X86CPU.PFINFO.IP_MASK = ((X86CPU.PFINFO.LENGTH - 1) & ~0x3);
}
X86CPU.PAGEBLOCKS_CACHE = 512; // TODO: This seems adequate for 4Mb of RAM, but it should be dynamically reconfigured
/**
* initMemory(aMemBlocks, nBlockShift)
*
* Notification from Bus.initMemory(), giving us direct access to the entire memory space
* (aMemBlocks). Since the CPU must perform additional layers of address decoding depending
* on the mode (real-mode, protected-mode, paging), it's best if the CPU can avoid going
* through the Bus component for every memory access.
*
* We also initialize a 32-bit prefetch queue, containing dword-aligned values; the queue is
* an array of dwords indexed by a masked regLIP; for example, a queue of 4 dwords is indexed
* by "regLIP & 0xC"; we use a sparse array to avoid right-shifting the index, like so:
*
* 0: [dword]
* 4: [dword]
* 8: [dword]
* 12: [dword]
*
* The actual regLIP mask is X86CPU.PFINFO.IP_MASK; ie, (X86CPU.PFINFO.LENGTH - 1) & ~0x3.
*
* On refilling, the queue is always filled to capacity, and cbPrefetch is set to its maximum
* value (eg, a value from 16 to 13, depending on whether "regLIP & 0x3" is 0, 1, 2 or 3).
*
* When a byte is requested from the queue, the dword is extracted from index "regLIP & 0xC"
* and then shifted by 0, 8, 16, or 24, depending on whether "regLIP & 0x3" is 0, 1, 2 or 3
* (ie, "(regLIP & 0x3) << 3").
*
* TODO: Consider how/whether to simulate an effective prefetch queue size of 4 bytes for an 8088,
* 6 bytes for an 8086, 12 for an 80386, etc.
*
* @this {X86CPU}
* @param {Array} aMemBlocks
* @param {number} nBlockShift
*/
X86CPU.prototype.initMemory = function(aMemBlocks, nBlockShift)
{
/*
* aBusBlocks preserves the Bus block array for the life of the machine, whereas aMemBlocks
* will be altered if/when the CPU enables paging. PAGEBLOCKS must be true when using Memory
* blocks to simulate paging, ensuring that physical blocks and pages have the same size (4Kb).
*/
this.aBusBlocks = this.aMemBlocks = aMemBlocks;
this.nBlockShift = nBlockShift;
this.nBlockSize = 1 << this.nBlockShift;
this.nBlockLimit = this.nBlockSize - 1;
this.nBlockTotal = aMemBlocks.length;
this.nBlockMask = this.nBlockTotal - 1;
if (PREFETCH) {
// this.nBusCycles = 0;
this.adwPrefetch = new Array(X86CPU.PFINFO.LENGTH);
}
};
/**
* setAddressMask(nBusMask)
*
* Notification from Bus.initMemory() and Bus.setA20(); the latter calls us whenever the physical
* A20 line changes (note that on a 20-bit bus machine, address lines A20 and higher are always zero).
*
* For 32-bit bus machines (eg, 80386), nBusMask is never changed after the initial call, because A20
* wrap-around is simulated by changing the physical memory map rather than altering the A20 bit in nBusMask.
*
* We maintain nMemMask separate from nBusMask, because when paging is enabled on the 80386, the CPU memory
* functions are now dealing with linear addresses rather than physical addresses, so it would be incorrect
* to apply nBusMask to those addresses; nMemMask must remain 0xffffffff (-1) for the duration. If we change
* how A20 is simulated on the 80386, then enablePageBlocks() and disablePageBlocks() will need to override
* nMemMask appropriately.
*
* TODO: Ideally, we would eliminate masking altogether of 32-bit addresses, but that would require different
* sets of memory access functions for different machines (ie, those with 20-bit or 24-bit buses).
*
* @this {X86CPU}
* @param {number} nBusMask
*/
X86CPU.prototype.setAddressMask = function(nBusMask)
{
this.nBusMask = this.nMemMask = nBusMask;
};
/**
* addMemBreak(addr, fWrite, fPhysical)
*
* NOTE: addMemBreak() could be merged with addMemCheck(), but the new merged interface would
* have to provide one additional parameter indicating whether the Debugger or the CPU is the client.
*
* For now, this is simply a DEBUGGER-only interface.
*
* @this {X86CPU}
* @param {number} addr
* @param {boolean} fWrite is true for a memory write breakpoint, false for a memory read breakpoint
* @param {boolean} [fPhysical] (true for physical breakpoint, false for linear)
*/
X86CPU.prototype.addMemBreak = function(addr, fWrite, fPhysical)
{
if (DEBUGGER) {
var iBlock = addr >>> this.nBlockShift;
var aBlocks = (fPhysical? this.aBusBlocks : this.aMemBlocks);
aBlocks[iBlock].addBreakpoint(addr & this.nBlockLimit, fWrite);
/*
* When a physical memory breakpoint is added, a fresh setPhysBlock() call is REQUIRED for any
* linear mappings to that address. This is a bit of a sledgehammer solution, but at least it's a solution.
*/
if (fPhysical) this.flushPageBlocks();
}
};
/**
* removeMemBreak(addr, fWrite, fPhysical)
*
* NOTE: removeMemBreak() could be merged with removeMemCheck(), but the new merged interface would
* have to provide one additional parameter indicating whether the Debugger or the CPU is the client.
*
* For now, this is simply a DEBUGGER-only interface.
*
* @this {X86CPU}
* @param {number} addr
* @param {boolean} fWrite is true for a memory write breakpoint, false for a memory read breakpoint
* @param {boolean} [fPhysical] (true for physical breakpoint, false for linear)
*/
X86CPU.prototype.removeMemBreak = function(addr, fWrite, fPhysical)
{
if (DEBUGGER) {
var iBlock = addr >>> this.nBlockShift;
var aBlocks = (fPhysical? this.aBusBlocks : this.aMemBlocks);
aBlocks[iBlock].removeBreakpoint(addr & this.nBlockLimit, fWrite);
/*
* When a physical memory breakpoint is removed, a fresh setPhysBlock() call is RECOMMENDED for any
* linear mappings to that address. This is a bit of a sledgehammer solution, but at least it's a solution.
*/
if (fPhysical) this.flushPageBlocks();
}
};
/**
* addMemCheck(addr, fWrite)
*
* These functions provide Debug register functionality to the CPU by leveraging the same Memory block-based
* breakpoint support originally created for our built-in Debugger. Only minimal changes were required to the
* Memory component, by adding additional checkMemoryException() call-outs from the "checked" Memory access
* functions.
*
* Note that those call-outs occur only AFTER our own Debugger (if present) has checked the address and has
* passed on it, because we want our own Debugger's breakpoints to take precedence over any breakpoints that
* the emulated machine may have enabled.
*
* @this {X86CPU}
* @param {number} addr
* @param {boolean} fWrite is true for a memory write check, false for a memory read check
*/
X86CPU.prototype.addMemCheck = function(addr, fWrite)
{
var iBlock = addr >>> this.nBlockShift;
this.aMemBlocks[iBlock].addBreakpoint(addr & this.nBlockLimit, fWrite, this);
};
/**
* removeMemCheck(addr, fWrite)
*
* @this {X86CPU}
* @param {number} addr
* @param {boolean} fWrite is true for a memory write check, false for a memory read check
*/
X86CPU.prototype.removeMemCheck = function(addr, fWrite)
{
var iBlock = addr >>> this.nBlockShift;
this.aMemBlocks[iBlock].removeBreakpoint(addr & this.nBlockLimit, fWrite);
};
/**
* enablePageBlocks()
*
* Whenever the CPU turns on paging and/or updates CR3, this function is called to update our copy
* of the Bus block array, to simulate paging. Whenever the CPU turns paging off, disablePageBlocks()
* must be called to restore our copy of the Bus block array to its original (physical) mapping.
*
* This also requires PAGEBLOCKS be enabled, to ensure the Bus is configured with a 4Kb block size.
*
* The first time this function is called, aMemBlocks and aBusBlocks are identical, so aMemBlocks is
* reinitialized with special UNPAGED Memory blocks that know how to perform page directory/page table
* lookup and replace themselves with special PAGED Memory blocks that reference memory from the
* appropriate block in aBusBlocks. A parallel array, aBlocksPaged, keeps track (by block number) of
* which blocks have been PAGED, so that whenever CR3 is updated, those blocks can be quickly UNPAGED.
*
* @this {X86CPU}
*/
X86CPU.prototype.enablePageBlocks = function()
{
if (!PAGEBLOCKS) {
this.setError("PAGEBLOCKS support required");
return;
}
var iBlock;
if (this.aMemBlocks === this.aBusBlocks) {
this.aMemBlocks = new Array(this.nBlockTotal);
/*
* TODO: Currently we allocate only one UNPAGED block for the entire linear address space;
* only when a block is touched and becomes PAGED do we allocate a dedicated Memory block
* for that slot. One potential downside to using a single UNPAGED block, however, is that
* it will accumulate all breakpoints for all UNPAGED blocks, requiring copyBreakpoints() to
* do extra work to figure out which breakpoints should be copied (ie, removed) from the
* outgoing block -- which it can't currently do, because blocks only keep track of the total
* number of breakpoints, not the actual breakpoint addresses.
*
* So, Memory blocks either need to start maintaining their own breakpoint address lists,
* or we need to allocate separate (empty) UNPAGED blocks for every slot. I've not tackled
* this yet, because it's largely just a debugging issue.
*
* Notice that when we call copyBreakpoints() here, it's merely to initialize the new block;
* we make no attempt to copy any breakpoints from physical blocks to linear blocks, although
* perhaps we should. The plan for our Debugger is to maintain separate physical and linear
* breakpoint address lists, but what about CPU Debug registers? If the CPU sets the Debug
* registers, then enables paging, do all the previous Debug register addresses automatically
* become linear addresses? I'm guessing they do.
*/
this.blockUnpaged = new Memory(null, 0, 0, Memory.TYPE.UNPAGED, null, this);
this.blockUnpaged.copyBreakpoints(this.dbg);
for (iBlock = 0; iBlock < this.nBlockTotal; iBlock++) {
this.aMemBlocks[iBlock] = this.blockUnpaged;
}
/*
* We also use a special "empty" Memory block that mapPageBlock() can pass back to callers
* whenever a valid block cannot be found for an UNPAGED block. Under normal conditions,
* an invalid block will trigger a fault, so memEmpty will never actually be returned, but
* if the Debugger is suppressing faults or calling probeAddr(), returning memEmpty is helpful.
*/
this.memEmpty = new Memory();
/*
* Initialize our PAGEBLOCKS cache (see acquirePageBlock() and releasePageBlock()).
*/
this.aCacheBlocks = new Array(X86CPU.PAGEBLOCKS_CACHE);
this.iCacheBlocks = 0;
} else {
/*
* Our equivalent of a TLB flush. NOTE: We do not attempt to simulate an actual TLB; our
* aMemBlocks array will "cache" as many pages (ie, allow as many PAGED block) as there are
* entries in the array. I'm assuming we won't run into any system software that relies on
* a constrained TLB -- at least not from the 80386 era, which is all we're emulating.
*/
for (var i = 0; i < this.aBlocksPaged.length; i++) {
iBlock = this.aBlocksPaged[i];
this.releasePageBlock(this.aMemBlocks[iBlock]);
this.aMemBlocks[iBlock] = this.blockUnpaged;
}
}
this.aBlocksPaged = [];
};
/**
* flushPageBlocks()
*
* @this {X86CPU}
*/
X86CPU.prototype.flushPageBlocks = function()
{
if (this.regCR0 & X86.CR0.PG) this.enablePageBlocks();
};
/**
* acquirePageBlock(addr)
*
* This implements a simple paged memory block cache. Candidates for caching must be released via
* releasePageBlock().
*
* After acquiring a block from this cache, the caller MUST use setPhysBlock() to properly reinitialize
* it for the new given linear address.
*
* @this {X86CPU}
* @param {number} addr
* @return {Memory}
*/
X86CPU.prototype.acquirePageBlock = function(addr)
{
var block;
if (this.iCacheBlocks > 0) {
block = this.aCacheBlocks[--this.iCacheBlocks];
/*
* Paged memory blocks are all very generic and contain no memory of their own, so the fact
* that we're not calling the Memory constructor to reinitialize it is OK. setPhysBlock() is
* what's critical, and the caller will take care of that. However, to avoid any confusion,
* especially when debugging, there are a few properties we should reinitialize, hence init().
*/
block.init(addr);
} else {
block = new Memory(addr, 0, 0, Memory.TYPE.PAGED);
}
return block;
};
/**
* releasePageBlock(block)
*
* Instead of simply tossing Memory blocks onto the garbage collector's heap, we'll retain a maximum
* number (X86CPU.PAGEBLOCKS_CACHE) in aCacheBlocks, with iCacheBlocks pointing to the next free element.
*
* @this {X86CPU}
* @param {Memory} block
*/
X86CPU.prototype.releasePageBlock = function(block)
{
this.assert(block && block.type === Memory.TYPE.PAGED);
if (this.iCacheBlocks < X86CPU.PAGEBLOCKS_CACHE) {
this.aCacheBlocks[this.iCacheBlocks++] = block;
}
};
/**
* mapPageBlock(addr, fWrite, fSuppress)
*
* Locate the corresponding physical PDE, PTE and memory blocks for the given linear address, and then
* upgrade the block from an UNPAGED Memory block to a new PAGED Memory block; all future accesses to
* the current page will go directly to that block, instead of coming here through the UNPAGED block
* handlers.
*
* Note that since the incoming address (addr) is a linear address, we never need to mask it with nBusMask,
* but all the intermediate (PDE, PTE) and final physical addresses we calculate should still be masked.
*
* Granted, nBusMask on a 32-bit bus is generally going to be 0xffffffff (-1), so masking might seem like
* a waste of time; however, if we decide to once again rely on nBusMask for emulating A20 wrap-around
* (instead of changing the physical memory map to alias the 2nd Mb to the 1st Mb), then performing
* consistent masking will be important.
*
* Also, addrPDE, addrPTE and addrPhys do not need any offsets added to them, because we immediately shift
* the offset portion of those addresses out. But for now, at least for debugging and documentation purposes,
* my preference is to include the offset in the address calculations.
*
* Besides, this should not be a performance-critical function; it's normally called only once per UNPAGED
* page. Obviously, if CR3 is constantly being updated, that will trigger repeated calls to enablePageBlocks(),
* which will perform our equivalent of a TLB flush (ie, resetting all PAGED blocks back to UNPAGED blocks).
* That would hurt our performance, but it would hurt performance on a real machine as well, so presumably
* CR3 updates will be minimal.
*
* @this {X86CPU}
* @param {number} addr is a linear address
* @param {boolean} fWrite (true if called for a write, false if for a read)
* @param {boolean} [fSuppress] (true if any faults, remapping, etc should be suppressed)
* @return {Memory}
*/
X86CPU.prototype.mapPageBlock = function(addr, fWrite, fSuppress)
{
var offPDE = (addr & X86.LADDR.PDE.MASK) >>> X86.LADDR.PDE.SHIFT;
var addrPDE = this.regCR3 + offPDE;
/*
* bus.getLong(addrPDE) would be simpler, but setPhysBlock() needs to know blockPDE and offPDE, too.
* TODO: Since we're immediately shifting addrPDE by nBlockShift, then we could also skip adding offPDE.
*/
var blockPDE = this.aBusBlocks[(addrPDE & this.nBusMask) >>> this.nBlockShift];
var pde = blockPDE.readLong(offPDE);
if (!(pde & X86.PTE.PRESENT)) {
if (!fSuppress) X86.helpPageFault.call(this, addr, false, fWrite);
return this.memEmpty;
}
if (!(pde & X86.PTE.USER) && this.nCPL == 3) {
if (!fSuppress) X86.helpPageFault.call(this, addr, true, fWrite);
return this.memEmpty;
}
var offPTE = (addr & X86.LADDR.PTE.MASK) >>> X86.LADDR.PTE.SHIFT;
var addrPTE = (pde & X86.PTE.FRAME) + offPTE;
/*
* bus.getLong(addrPTE) would be simpler, but setPhysBlock() needs to know blockPTE and offPTE, too.
* TODO: Since we're immediately shifting addrPDE by nBlockShift, then we could also skip adding offPTE.
*/
var blockPTE = this.aBusBlocks[(addrPTE & this.nBusMask) >>> this.nBlockShift];
var pte = blockPTE.readLong(offPTE);
if (!(pte & X86.PTE.PRESENT)) {
if (!fSuppress) X86.helpPageFault.call(this, addr, false, fWrite);
return this.memEmpty;
}
if (!(pte & X86.PTE.USER) && this.nCPL == 3) {
if (!fSuppress) X86.helpPageFault.call(this, addr, true, fWrite);
return this.memEmpty;
}
var addrPhys = (pte & X86.PTE.FRAME) + (addr & X86.LADDR.OFFSET);
/*
* TODO: Since we're immediately shifting addrPhys by nBlockShift, we could also skip adding the addr's offset.
*/
var blockPhys = this.aBusBlocks[(addrPhys & this.nBusMask) >>> this.nBlockShift];
if (fSuppress) return blockPhys;
var iBlock = addr >>> this.nBlockShift;
var block = this.aMemBlocks[iBlock];
/*
* So we have the block containing the physical memory corresponding to the given linear address.
*
* Now we can create a new PAGED Memory block and record the physical block info using setPhysBlock().
*/
var blockPage = this.acquirePageBlock(addr & ~X86.LADDR.OFFSET);
blockPage.setPhysBlock(blockPhys, blockPDE, offPDE, blockPTE, offPTE);
blockPage.copyBreakpoints(this.dbg, block);
this.aMemBlocks[iBlock] = blockPage;
this.aBlocksPaged.push(iBlock);
return blockPage;
};
/**
* disablePageBlocks()
*
* Whenever the CPU turns off paging, this function restores the CPU's original aMemBlocks.
*
* @this {X86CPU}
*/
X86CPU.prototype.disablePageBlocks = function()
{
if (this.aMemBlocks !== this.aBusBlocks) {
this.aMemBlocks = this.aBusBlocks;
this.blockUnpaged = null;
this.aBlocksPaged = null;
this.memEmpty = null;
}
};
/**
* isPagingEnabled()
*
* @this {X86CPU}
* @return {boolean}
*/
X86CPU.prototype.isPagingEnabled = function()
{
var fPaging = !!(this.regCR0 & X86.CR0.PG);
this.assert((this.aMemBlocks !== this.aBusBlocks) === fPaging);
return fPaging;
};
/**
* initProcessor()
*
* This isolates 80186/80188/80286/80386 support, so that it can be selectively enabled/tested.
*
* Here's a summary of 80186/80188 differences according to "AP-186: Introduction to the 80186
* Microprocessor, March 1983" (pp.55-56). "The iAPX 86,88 and iAPX 186,188 User's Manual Programmer's
* Reference", p.3-38, apparently contains the same information, but I've not seen that document.
*
* Undefined [Invalid] Opcodes:
*
* When the opcodes 63H, 64H, 65H, 66H, 67H, F1H, FEH/xx111xxxB and FFH/xx111xxxB are executed,
* the 80186 will execute an illegal [invalid] instruction exception, interrupt 0x06.
* The 8086 will ignore the opcode.
*
* 0FH opcode:
*
* When the opcode 0FH is encountered, the 8086 will execute a POP CS, while the 80186 will
* execute an illegal [invalid] instruction exception, interrupt 0x06.
*
* Word Write at Offset FFFFH:
*
* When a word write is performed at offset FFFFH in a segment, the 8086 will write one byte
* at offset FFFFH, and the other at offset 0, while the 80186 will write one byte at offset
* FFFFH, and the other at offset 10000H (one byte beyond the end of the segment). One byte segment
* underflow will also occur (on the 80186) if a stack PUSH is executed and the Stack Pointer
* contains the value 1.
*
* Shift/Rotate by Value Greater Then [sic] 31:
*
* Before the 80186 performs a shift or rotate by a value (either in the CL register, or by an
* immediate value) it ANDs the value with 1FH, limiting the number of bits rotated to less than 32.
* The 8086 does not do this.
*
* LOCK prefix:
*
* The 8086 activates its LOCK signal immediately after executing the LOCK prefix. The 80186 does
* not activate the LOCK signal until the processor is ready to begin the data cycles associated
* with the LOCKed instruction.
*
* Interrupted String Move Instructions:
*
* If an 8086 is interrupted during the execution of a repeated string move instruction, the return
* value it will push on the stack will point to the last prefix instruction before the string move
* instruction. If the instruction had more than one prefix (e.g., a segment override prefix in
* addition to the repeat prefix), it will not be re-executed upon returning from the interrupt.
* The 80186 will push the value of the first prefix to the repeated instruction, so long as prefixes
* are not repeated, allowing the string instruction to properly resume.
*
* Conditions causing divide error with an integer divide:
*
* The 8086 will cause a divide error whenever the absolute value of the quotient is greater then
* [sic] 7FFFH (for word operations) or if the absolute value of the quotient is greater than 7FH
* (for byte operations). The 80186 has expanded the range of negative numbers allowed as a quotient
* by 1 to include 8000H and 80H. These numbers represent the most negative numbers representable
* using 2's complement arithmetic (equaling -32768 and -128 in decimal, respectively).
*
* ESC Opcode:
*
* The 80186 may be programmed to cause an interrupt type 7 whenever an ESCape instruction (used for
* co-processors like the 8087) is executed. The 8086 has no such provision. Before the 80186 performs
* this trap, it must be programmed to do so. [The details of this "programming" are not included.]
*
* Here's a summary of 80286 differences according to "80286 and 80287 Programmer's Reference Manual",
* Appendix C, p.C-1 (p.329):
*
* 1. Add Six Interrupt Vectors
*
* The 80286 adds six interrupts which arise only if the 8086 program has a hidden bug. These interrupts
* occur only for instructions which were undefined on the 8086/8088 or if a segment wraparound is attempted.
* It is recommended that you add an interrupt handler to the 8086 software that is to be run on the 80286,
* which will treat these interrupts as invalid operations.
*
* This additional software does not significantly effect [sic] the existing 8086 software because the interrupts
* do not normally occur and should not already have been used since they are in the interrupt group reserved
* by Intel. [NOTE: IBM ignored Intel's admonishments.]
*
* 2. Do not Rely on 8086/8088 Instruction Clock Counts
*
* The 80286 takes fewer clocks for most instructions than the 8086/8088. The areas to look into are delays
* between I/0 operations, and assumed delays in 8086/8088 operating in parallel with an 8087.
*
* 3. Divide Exceptions Point at the DIV Instruction
*
* Any interrupt on the 80286 will always leave the saved CS:IP value pointing at the beginning of the
* instruction that failed (including prefixes). On the 8086, the CS:IP value saved for a divide exception
* points at the next instruction.
*
* 4. Use Interrupt 16 (0x10) for Numeric Exceptions
*
* Any 80287 system must use interrupt vector 16 for the numeric error interrupt. If an 8086/8087 or 8088/8087
* system uses another vector for the 8087 interrupt, both vectors should point at the numeric error interrupt
* handler.
*
* 5. Numeric Exception Handlers Should allow Prefixes
*
* The saved CS:IP value in the NPX environment save area will point at any leading prefixes before an ESC
* instruction. On 8086/8088 systems, this value points only at the ESC instruction.
*
* 6. Do Not Attempt Undefined 8086/8088 Operations
*
* Instructions like POP CS or MOV CS,op will either cause exception 6 (undefined [invalid] opcode) or perform
* a protection setup operation like LIDT on the 80286. Undefined bit encodings for bits 5-3 of the second byte
* of POP MEM or PUSH MEM will cause exception 13 on the 80286.
*
* 7. Place a Far JMP Instruction at FFFF0H
*
* After reset, CS:IP = F000:FFF0 on the 80286 (versus FFFF:0000 on the 8086/8088). This change was made to allow
* sufficient code space to enter protected mode without reloading CS. Placing a far JMP instruction at FFFF0H
* will avoid this difference. Note that the BOOTSTRAP option of LOC86 will automatically generate this jump
* instruction.
*
* 8. Do not Rely on the Value Written by PUSH SP
*
* The 80286 will push a different value on the stack for PUSH SP than the 8086/8088. If the value pushed is
* important [and when would it NOT be?], replace PUSH SP instructions with the following three instructions:
*
* PUSH BP
* MOV BP,SP
* XCHG BP,[BP]
*
* This code functions as the 8086/8088 PUSH SP instruction on the 80286.
*
* 9. Do not Shift or Rotate by More than 31 Bits
*
* The 80286 masks all shift/rotate counts to the low 5 bits. This MOD 32 operation limits the count to a maximum
* of 31 bits. With this change, the longest shift/rotate instruction is 39 clocks. Without this change, the longest
* shift/rotate instruction would be 264 clocks, which delays interrupt response until the instruction completes
* execution.
*
* 10. Do not Duplicate Prefixes
*
* The 80286 sets an instruction length limit of 10 bytes. The only way to violate this limit is by duplicating
* a prefix two or more times before an instruction. Exception 6 occurs if the instruction length limit is violated.
* The 8086/8088 has no instruction length limit.
*
* 11. Do not Rely on Odd 8086/8088 LOCK Characteristics
*
* The LOCK prefix and its corresponding output signal should only be used to prevent other bus masters from
* interrupting a data movement operation. The 80286 will always assert LOCK during an XCHG instruction with memory
* (even if the LOCK prefix was not used). LOCK should only be used with the XCHG, MOV, MOVS, INS, and OUTS instructions.
*
* The 80286 LOCK signal will not go active during an instruction prefetch.
*
* 12. Do not Single Step External Interrupt Handlers
*
* The priority of the 80286 single step interrupt is different from that of the 8086/8088. This change was made
* to prevent an external interrupt from being single-stepped if it occurs while single stepping through a program.
* The 80286 single step interrupt has higher priority than any external interrupt.
*
* The 80286 will still single step through an interrupt handler invoked by INT instructions or an instruction
* exception.
*
* 13. Do not Rely on IDIV Exceptions for Quotients of 80H or 8000H
*
* The 80286 can generate the largest negative number as a quotient for IDIV instructions. The 8086 will instead
* cause exception O.
*
* 14. Do not Rely on NMI Interrupting NMI Handlers
*
* After an NMI is recognized, the NMI input and processor extension limit error interrupt is masked until the
* first IRET instruction is executed.
*
* 15. The NPX error signal does not pass through an interrupt controller (an 8087 INT signal does). Any interrupt
* controller-oriented instructions for the 8087 may have to be deleted.
*
* 16. If any real-mode program relies on address space wrap-around (e.g., FFF0:0400=0000:0300), then external hardware
* should be used to force the upper 4 addresses to zero during real mode.
*
* 17. Do not use I/O ports 00F8-00FFH. These are reserved for controlling 80287 and future processor extensions.
*
* @this {X86CPU}
*/
X86CPU.prototype.initProcessor = function()
{
this.PS_SET = X86.PS_SET_8086;
this.PS_DIRECT = X86.PS_DIRECT_8086;
this.PS_CLEAR_RM = X86.PS.IOPL.MASK | X86.PS.NT;
this.OPFLAG_NOINTR_8086 = X86.OPFLAG.NOINTR;
this.nShiftCountMask = 0xff; // on an 8086/8088, all shift counts are used as-is
this.cycleCounts = (this.model >= X86.MODEL_80286? X86.CYCLES_80286 : X86.CYCLES_8088);
this.aOps = X86.aOps;
this.aOpGrp4b = X86.aOpGrp4b;
this.aOpGrp4w = X86.aOpGrp4w;
this.aOpGrp6 = X86.aOpGrp6Real; // setProtMode() will ensure that aOpGrp6 is switched
if (this.model >= X86.MODEL_80186) {
/*
* I don't go out of my way to make 80186/80188 cycle times accurate, since I'm not aware of any
* IBM PC models that used those processors; beyond the 8086, my next priorities are the 80286 and
* 80386, but I might revisit the 80186 someday.
*
* Instruction handlers that contain "hard-coded" 80286 cycle times include: opINSb, opINSw, opOUTSb,
* opOUTSw, opENTER, and opLEAVE.
*/
this.aOps = X86.aOps.slice(); // make copies of opcode tables before modifying
this.aOpGrp4b = X86.aOpGrp4b.slice();
this.aOpGrp4w = X86.aOpGrp4w.slice();
this.nShiftCountMask = 0x1f; // on newer processors, all shift counts are MOD 32
this.aOps[0x0F] = X86.opInvalid;
this.aOps[X86.OPCODE.PUSHA] = X86.opPUSHA; // 0x60
this.aOps[X86.OPCODE.POPA] = X86.opPOPA; // 0x61
this.aOps[X86.OPCODE.BOUND] = X86.opBOUND; // 0x62
this.aOps[X86.OPCODE.ARPL] = X86.opInvalid; // 0x63
this.aOps[X86.OPCODE.FS] = X86.opInvalid; // 0x64
this.aOps[X86.OPCODE.GS] = X86.opInvalid; // 0x65
this.aOps[X86.OPCODE.OS] = X86.opInvalid; // 0x66
this.aOps[X86.OPCODE.AS] = X86.opInvalid; // 0x67
this.aOps[X86.OPCODE.PUSHN] = X86.opPUSHn; // 0x68
this.aOps[X86.OPCODE.IMULN] = X86.opIMULn; // 0x69
this.aOps[X86.OPCODE.PUSH8] = X86.opPUSH8; // 0x6A
this.aOps[X86.OPCODE.IMUL8] = X86.opIMUL8; // 0x6B
this.aOps[X86.OPCODE.INSB] = X86.opINSb; // 0x6C
this.aOps[X86.OPCODE.INSW] = X86.opINSw; // 0x6D
this.aOps[X86.OPCODE.OUTSB] = X86.opOUTSb; // 0x6E
this.aOps[X86.OPCODE.OUTSW] = X86.opOUTSw; // 0x6F
this.aOps[0xC0] = X86.opGRP2bn; // 0xC0
this.aOps[0xC1] = X86.opGRP2wn; // 0xC1
this.aOps[X86.OPCODE.ENTER] = X86.opENTER; // 0xC8
this.aOps[X86.OPCODE.LEAVE] = X86.opLEAVE; // 0xC9
this.aOps[0xF1] = X86.opINT1; // 0xF1
this.aOpGrp4b[0x07] = X86.fnGRPInvalid;
this.aOpGrp4w[0x07] = X86.fnGRPInvalid;
if (this.model >= X86.MODEL_80286) {
this.PS_SET = X86.PS.BIT1; // on the 80286, only BIT1 of Processor Status (flags) is always set
this.PS_DIRECT |= X86.PS.IOPL.MASK | X86.PS.NT;
this.OPFLAG_NOINTR_8086 = 0; // for instructions that do *not* set NOINTR on an 80286 (eg, non-SS segment loads)
this.aOps[0x0F] = X86.op0F;
this.aOps0F = X86.aOps0F.slice();
for (var i = 0; i < this.aOps0F.length; i++) {
if (!this.aOps0F[i]) this.aOps0F[i] = X86.opUndefined;
}
this.aOps[X86.OPCODE.PUSHSP] = X86.opPUSHSP; // 0x54
this.aOps[X86.OPCODE.ARPL] = X86.opARPL; // 0x63
if (I386 && this.model >= X86.MODEL_80386) {
var bOpcode;
this.PS_CLEAR_RM = 0; // NOTE: This allows the 80386 to modify X86.PS.NT in real-mode (which is presumably OK)
this.PS_DIRECT |= X86.PS.RF | X86.PS.VM;
this.aOps[X86.OPCODE.FS] = X86.opFS; // 0x64
this.aOps[X86.OPCODE.GS] = X86.opGS; // 0x65
this.aOps[X86.OPCODE.OS] = X86.opOS; // 0x66
this.aOps[X86.OPCODE.AS] = X86.opAS; // 0x67
for (bOpcode in X86.aOps0F386) {
this.aOps0F[+bOpcode] = X86.aOps0F386[bOpcode];
}
if (this.stepping >= X86.STEPPING_80386_A0 && this.stepping <= X86.STEPPING_80386_B0) {
this.aOps0F[0xA6] = X86.opXBTS;
this.aOps0F[0xA7] = X86.opIBTS;
}
}
}
}
};
/**
* reset()
*
* @this {X86CPU}
*/
X86CPU.prototype.reset = function()
{
if (this.flags.fRunning) this.stopCPU();
this.resetRegs();
this.resetCycles();
this.clearError(); // clear any fatal error/exception that setError() may have flagged
};
/**
* getReg(i)
*
* @this {X86CPU}
* @param {number} i (0-7)
* @return {number}
*/
X86CPU.prototype.getReg = function(i)
{
var reg;
switch(i) {
case 0x0:
reg = this.regEAX;
break;
case 0x1:
reg = this.regECX;
break;
case 0x2:
reg = this.regEDX;
break;
case 0x3:
reg = this.regEBX;
break;
case 0x4:
reg = this.getSP();
break;
case 0x5:
reg = this.regEBP;
break;
case 0x6:
reg = this.regESI;
break;
case 0x7:
reg = this.regEDI;
break;
}
return reg;
};
/**
* setReg(i, reg)
*
* @this {X86CPU}
* @param {number} i (0-7)
* @param {number} reg
*/
X86CPU.prototype.setReg = function(i, reg)
{
switch(i) {
case 0x0:
this.regEAX = reg;
break;
case 0x1:
this.regECX = reg;
break;
case 0x2:
this.regEDX = reg;
break;
case 0x3:
this.regEBX = reg;
break;
case 0x4:
this.setSP(reg);
break;
case 0x5:
this.regEBP = reg;
break;
case 0x6:
this.regESI = reg;
break;
case 0x7:
this.regEDI = reg;
break;
}
};
/**
* resetRegs()
*
* According to "The 8086 Book", p.7-5, a RESET signal initializes the following registers:
*
* PS = 0x0000 (which has the important side-effect of disabling interrupts and traps)
* IP = 0x0000
* CS = 0xFFFF
* DS/ES/SS = 0x0000
*
* It is silent as to whether the remaining registers are initialized to any particular values.
*
* According to the "80286 and 80287 Programmer's Reference Manual", these 80286 registers are reset:
*
* PS = 0x0002
* MSW = 0xFFF0
* IP = 0xFFF0
* CS Selector = 0xF000 DS/ES/SS Selector = 0x0000