forked from antoniocgj/swerv-ISS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHart.hpp
1611 lines (1320 loc) · 63.2 KB
/
Hart.hpp
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
//
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2018 Western Digital Corporation or its affiliates.
//
// 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 3 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, see <https://www.gnu.org/licenses/>.
//
#pragma once
#include <cstdint>
#include <vector>
#include <iosfwd>
#include <type_traits>
#include "InstId.hpp"
#include "InstEntry.hpp"
#include "IntRegs.hpp"
#include "CsRegs.hpp"
#include "FpRegs.hpp"
#include "Memory.hpp"
#include "InstProfile.hpp"
#include "DecodedInst.hpp"
namespace WdRiscv
{
/// Thrown by the simulator when a stop (store to to-host) is seen
/// or when the target program reaches the exit system call.
class CoreException : public std::exception
{
public:
enum Type { Stop, Exit };
CoreException(Type type, const char* message = "", uint64_t address = 0,
uint64_t value = 0)
: type_(type), msg_(message), addr_(address), val_(value)
{ }
const char* what() const noexcept override
{ return msg_; }
Type type() const
{ return type_; }
uint64_t address() const
{ return addr_; }
uint64_t value() const
{ return val_; }
private:
Type type_ = Stop;
const char* msg_ = "";
uint64_t addr_ = 0;
uint64_t val_ = 0;
};
/// Changes made by the execution of one instruction. Useful for
/// test pattern generation.
struct ChangeRecord
{
void clear()
{ *this = ChangeRecord(); }
uint64_t newPc = 0; // Value of pc after instruction execution.
bool hasException = false; // True if instruction causes an exception.
bool hasIntReg = false; // True if there is an integer register change.
unsigned intRegIx = 0; // Number of changed integer register if any.
uint64_t intRegValue = 0; // Value of changed integer register if any.
bool hasFpReg = false; // True if there is an FP register change.
unsigned fpRegIx = 0; // Number of changed fp register if any.
uint64_t fpRegValue = 0; // Value of changed fp register if any.
unsigned memSize = 0; // Size of changed memory (0 if none).
size_t memAddr = 0; // Address of changed memory if any.
uint64_t memValue = 0; // Value of changed memory if any.
// An exception will result in changing multiple CSRs.
std::vector<CsrNumber> csrIx; // Numbers of changed CSRs if any.
std::vector<uint64_t> csrValue; // Values of changed CSRs if any.
};
/// Model a RISCV hart with integer registers of type URV (uint32_t
/// for 32-bit registers and uint64_t for 64-bit registers).
template <typename URV>
class Hart
{
public:
/// Signed register type corresponding to URV. For example, if URV
/// is uint32_t, then SRV will be int32_t.
typedef typename std::make_signed_t<URV> SRV;
/// Constructor: Define a hart with the given integer register
/// cont, the givel local hart id (id within core) and associate
/// it with the given memory.
Hart(unsigned localHartId, Memory& memory, unsigned intRegCount);
/// Destructor.
~Hart();
/// Return count of integer registers.
size_t intRegCount() const
{ return intRegs_.size(); }
/// Return the name of the given integer register. Return an
/// abi-name (e.g. sp) if abi names are enabled.
std::string intRegName(unsigned regIx) const
{ return intRegs_.regName(regIx, abiNames_); }
/// Return the name of the given floating point register. Return an
/// abi-name (e.g. fa0) if abi names are enabled.
std::string fpRegName(unsigned regIx) const
{ return fpRegs_.regName(regIx, abiNames_); }
/// Return the name (e.g. x1) or the abi-name (e.g. ra) of the
/// given integer register.
std::string intRegName(unsigned regIx, bool abiName) const
{ return intRegs_.regName(regIx, abiName); }
/// Return count of floating point registers. Return zero if
/// extension f is not enabled.
size_t fpRegCount() const
{ return isRvf()? fpRegs_.size() : 0; }
/// Return size of memory in bytes.
size_t memorySize() const
{ return memory_.size(); }
/// Return the value of the program counter.
URV peekPc() const;
/// Set the program counter to the given address.
void pokePc(URV address);
/// Set val to the value of integer register reg returning true on
/// success. Return false leaving val unmodified if reg is out of
/// bounds.
bool peekIntReg(unsigned reg, URV& val) const;
/// Set val to the value of integer register reg returning true on
/// success. Return false leaving val unmodified if reg is out of
/// bounds. If successful, set name to the register name.
bool peekIntReg(unsigned reg, URV& val, std::string& name) const;
/// Return to the value of integer register reg which must not be
/// out of bounds (otherwise we trigger an assert).
URV peekIntReg(unsigned reg) const;
/// Set the given integer register, reg, to the given value
/// returning true on success. Return false if reg is out of
/// bound.
bool pokeIntReg(unsigned reg, URV val);
/// Set val to the bit-pattern of the value of the floating point
/// register returning true on success. Return false leaving val
/// unmodified if reg is out of bounds of if no floating point
/// extension is enabled.
bool peekFpReg(unsigned reg, uint64_t& val) const;
/// Set the given FP register, reg, to the given value returning
/// true on success. Return false if reg is out of bound.
bool pokeFpReg(unsigned reg, uint64_t val);
/// Set val to the value of the control and status register csr
/// returning true on success. Return false leaving val unmodified
/// if csr is out of bounds.
bool peekCsr(CsrNumber csr, URV& val) const;
/// Set val, reset, writeMask, and pokeMask respectively to the
/// value, reset-value, write-mask and poke-mask of the control
/// and status register csr returning true on success. Return
/// false leaving parameters unmodified if csr is out of bounds.
bool peekCsr(CsrNumber csr, URV& val, URV& reset, URV& writeMask,
URV& pokeMask) const;
/// Set val/name to the value/name of the control and status
/// register csr returning true on success. Return false leaving
/// val/name unmodified if csr is out of bounds.
bool peekCsr(CsrNumber csr, URV& val, std::string& name) const;
/// Set the given control and status register, csr, to the given
/// value returning true on success. Return false if csr is out of
/// bound.
bool pokeCsr(CsrNumber csr, URV val);
/// Find the integer register with the given name (which may
/// represent an integer or a symbolic name). Set num to the
/// number of the corresponding register if found. Return true on
/// success and false if no such register.
bool findIntReg(const std::string& name, unsigned& num) const;
/// Find the floating point with the given name. Set num to the
/// number of the corresponding register if found. Return true on
/// success and false if no such register.
bool findFpReg(const std::string& name, unsigned& num) const;
/// Find the control and status register with the given name
/// (which may represent an integer or a symbolic name). Return
/// pointer to CSR on success and nullptr if no such register.
Csr<URV>* findCsr(const std::string& name);
/// Find the control and status register with the given number.
/// Return pointer to CSR on success and nullptr if no such
/// register.
const Csr<URV>* findCsr(CsrNumber number)
{ return csRegs_.findCsr(number); }
/// Configure given CSR. Return true on success and false if
/// no such CSR.
bool configCsr(const std::string& name, bool implemented,
URV resetValue, URV mask, URV pokeMask,
bool isDebug, bool shared);
/// Define a new CSR (beyond the standard CSRs defined by the
/// RISCV spec). Return true on success and false if name/number
/// already in use.
bool defineCsr(const std::string& name, CsrNumber number,
bool implemented, URV resetValue, URV mask,
URV pokeMask, bool isDebug);
/// Configure given trigger with given reset values, write and
/// poke masks. Return true on success and false on failure.
bool configTrigger(unsigned trigger, URV val1, URV val2, URV val3,
URV wm1, URV wm2, URV wm3,
URV pm1, URV pm2, URV pm3)
{
return csRegs_.configTrigger(trigger, val1, val2, val3,
wm1, wm2, wm3, pm1, pm2, pm3);
}
/// Restrict chaining only to pairs of consecutive (even-numbered followed
/// by odd) triggers.
void configEvenOddTriggerChaining(bool flag)
{ csRegs_.configEvenOddTriggerChaining(flag); }
/// Configure machine mode performance counters returning true on
/// success and false on failure. N consecutive counters starting
/// at MHPMCOUNTER3/MHPMCOUNTER3H are made read/write. The
/// remaining counters are made read only. For each counter that
/// is made read-write the corresponding MHPMEVENT is made
/// read-write.
bool configMachineModePerfCounters(unsigned n);
/// Set the maximum event id that can be written to the mhpmevent
/// registers. Larger values are replaced by this max-value before
/// being written to the mhpmevent registers. Return true on
/// success and false on failure.
void configMachineModeMaxPerfEvent(URV maxId)
{ csRegs_.setMaxEventId(maxId); }
/// Get the values of the three components of the given debug
/// trigger. Return true on success and false if trigger is out of
/// bounds.
bool peekTrigger(URV trigger, URV& data1, URV& data2, URV& data3) const
{ return csRegs_.peekTrigger(trigger, data1, data2, data3); }
/// Get the values of the three components of the given debug
/// trigger as well as the components write and poke masks. Return
/// true on success and false if trigger is out of bounds.
bool peekTrigger(URV trigger, URV& val1, URV& val2, URV& val3,
URV& wm1, URV& wm2, URV& wm3,
URV& pm1, URV& pm2, URV& pm3) const
{ return csRegs_.peekTrigger(trigger, val1, val2, val3, wm1, wm2, wm3,
pm1, pm2, pm3); }
/// Set the values of the three components of the given debug
/// trigger. Return true on success and false if trigger is out of
/// bounds.
bool pokeTrigger(URV trigger, URV data1, URV data2, URV data3)
{ return csRegs_.pokeTrigger(trigger, data1, data2, data3); }
/// Fill given vector (cleared on entry) with the numbers of
/// implemented CSRs.
void getImplementedCsrs(std::vector<CsrNumber>& vec) const;
/// Reset this hart. Reset all CSRs to their initial value. Reset all
/// integer registers to zero. Reset PC to the reset-pc as
/// defined by defineResetPc (default is zero).
void reset(bool resetMemoryMappedRegister = false);
/// Run fetch-decode-execute loop. If a stop address (see
/// setStopAddress) is defined, stop when the program counter
/// reaches that address. If a tohost address is defined (see
/// setToHostAdress), stop when a store instruction writes into
/// that address. If given file is non-null, then print to that
/// file a record for each executed instruction.
bool run(FILE* file = nullptr);
/// Run one instruction at the current program counter. Update
/// program counter. If file is non-null then print thereon
/// tracing information related to the executed instruction.
void singleStep(FILE* file = nullptr);
/// Determine the effect of instruction fetching and discarding n
/// bytes (where n is the instruction size of the given
/// instruction) from memory and then executing the given
/// instruction without actually changing the state of the hart or
/// the memory. Return true if the instruction would execute
/// without an exception. Return false otherwise. In either case
/// set the record fields corresponding to the resources that
/// would have been changed by the execution of the instruction.
bool whatIfSingleStep(URV programCounter, uint32_t inst,
ChangeRecord& record);
/// Similar to the above method but without fetching anything from
/// from instruction memory (in other words, this variant will
/// never cause an misaligned/instruction-access-fault
/// exception).
bool whatIfSingleStep(uint32_t inst, ChangeRecord& record);
/// Similar to the above but without fetching register operands.
/// Register operand values are obtained from the given decoded
/// instruction object.
bool whatIfSingStep(const DecodedInst& inst, ChangeRecord& record);
/// Run until the program counter reaches the given address. Do
/// execute the instruction at that address. If file is non-null
/// then print thereon tracing information after each executed
/// instruction. Similar to method run with respect to tohost.
bool runUntilAddress(URV address, FILE* file = nullptr);
/// Helper to runUntiAddress: Same as runUntilAddress but does not
/// print run-time and instructions per second.
bool untilAddress(URV address, FILE* file = nullptr);
/// Define the program counter value at which the run method will
/// stop.
void setStopAddress(URV address)
{ stopAddr_ = address; stopAddrValid_ = true; }
/// Undefine stop address (see setStopAddress).
void clearStopAddress()
{ stopAddrValid_ = false; }
/// Define the memory address corresponding to console io. Reading/writing
/// a byte (lb/sb) from/to that address reads/writes a byte to/from
/// the console.
void setConsoleIo(URV address)
{ conIo_ = address; conIoValid_ = true; }
/// Undefine console io address (see setConsoleIo).
void clearConsoleIo()
{ conIoValid_ = false; }
/// Console output gets directed to given file.
void setConsoleOutput(FILE* out)
{ consoleOut_ = out; }
/// If a console io memory mapped location is defined then put its
/// address in address and return true; otherwise, return false
/// leaving address unmodified.
bool getConsoleIo(URV& address) const
{ if (conIoValid_) address = conIo_; return conIoValid_; }
/// Disassemble given instruction putting results on the given
/// stream.
void disassembleInst(uint32_t inst, std::ostream&);
/// Disassemble given instruction putting results on the given
/// stream.
void disassembleInst(const DecodedInst& di, std::ostream&);
/// Disassemble given instruction putting results into the given
/// string.
void disassembleInst(uint32_t inst, std::string& str);
/// Disassemble given instruction putting results into the given
/// string.
void disassembleInst(const DecodedInst& di, std::string& str);
/// Decode given instruction returning a pointer to the
/// instruction information and filling op0, op1 and op2 with the
/// corresponding operand specifier values. For example, if inst
/// is the instruction code for "addi r3, r4, 77", then the
/// returned value would correspond to addi and op0, op1 and op2
/// will be set to 3, 4, and 77 respectively. If an instruction
/// has fewer than 3 operands then only a subset of op0, op1 and
/// op2 will be set. If inst is not a valid instruction , then we
/// return a reference to the illegal-instruction info.
const InstEntry& decode(uint32_t inst, uint32_t& op0, uint32_t& op1,
uint32_t& op2, uint32_t& op3);
/// Similar to the above decode method but with decoded data
/// placed in the given DecodedInst object.
void decode(URV address, uint32_t inst, DecodedInst& decodedInst);
/// Load the given hex file and set memory locations accordingly.
/// Return true on success. Return false if file does not exists,
/// cannot be opened or contains malformed data.
/// File format: A line either contains @address where address
/// is a hexadecimal memory address or one or more space separated
/// tokens each consisting of two hexadecimal digits.
bool loadHexFile(const std::string& file);
/// Load the given ELF file and place ints contents in memory.
/// Return true on success. Return false if file does not exists,
/// cannot be opened or contains malformed data. On success, set
/// entryPoint to the program entry-point of the loaded file. If
/// the to-host-address is not set then set it to the value
/// corresponding to the to-host-symbol if such that symbol is
/// found in the ELF file.
bool loadElfFile(const std::string& file, size_t& entryPoint);
/// Locate the given ELF symbol (symbols are collected for every
/// loaded ELF file) returning true if symbol is found and false
/// otherwise. Set value to the corresponding value if symbol is
/// found.
bool findElfSymbol(const std::string& symbol, ElfSymbol& value) const
{ return memory_.findElfSymbol(symbol, value); }
/// Locate the ELF function cotaining the give address returning true
/// on success and false on failure. If successful set name to the
/// corresponding function name and symbol to the corresponding symbol
/// value.
bool findElfFunction(URV addr, std::string& name, ElfSymbol& value) const
{ return memory_.findElfFunction(addr, name, value); }
/// Print the ELF symbols on the given stream. Output format:
/// <name> <value>
void printElfSymbols(std::ostream& out) const
{ memory_.printElfSymbols(out); }
/// Set val to the value of the memory byte at the given address
/// returning true on success and false if address is out of
/// bounds.
bool peekMemory(size_t address, uint8_t& val) const
{ return memory_.readByte(address, val); }
/// Set val to the value of the half-word at the given address
/// returning true on success and false if address is out of
/// bounds. Memory is little endian.
bool peekMemory(size_t address, uint16_t& val) const;
/// Set val to the value of the word at the given address
/// returning true on success and false if address is out of
/// bounds. Memory is little endian.
bool peekMemory(size_t address, uint32_t& val) const;
/// Set val to the value of the word at the given address
/// returning true on success and false if address is out of
/// bounds. Memory is little endian.
bool peekMemory(size_t address, uint64_t& val) const;
/// Set the memory byte at the given address to the given value.
/// Return true on success and false on failure (address out of
/// bounds, location not mapped, location not writable etc...)
bool pokeMemory(size_t address, uint8_t val);
/// Halt word version of the above.
bool pokeMemory(size_t address, uint16_t val);
/// Word version of the above.
bool pokeMemory(size_t address, uint32_t val);
/// Double word version of the above.
bool pokeMemory(size_t address, uint64_t val);
/// Define value of program counter after a reset.
void defineResetPc(URV addr)
{ resetPc_ = addr; }
/// Define value of program counter after a non-maskable interrupt.
void defineNmiPc(URV addr)
{ nmiPc_ = addr; }
/// Clear/set pending non-maskable-interrupt.
void setPendingNmi(NmiCause cause = NmiCause::UNKNOWN);
/// Clear pending non-maskable-interrupt.
void clearPendingNmi();
/// Define address to which a write will stop the simulator. An
/// sb, sh, or sw instruction will stop the simulator if the write
/// address of he instruction is identical to the given address.
void setToHostAddress(size_t address);
/// Special target program symbol writing to which stops the
/// simulated program.
void setTohostSymbol(const std::string& sym)
{ toHostSym_ = sym; }
/// Undefine address to which a write will stop the simulator
void clearToHostAddress();
/// Set address to the special address writing to which stops the
/// simulation. Return true on success and false on failure (no
/// such address defined).
bool getToHostAddress(size_t& address) const
{ if (toHostValid_) address = toHost_; return toHostValid_; }
/// Support for tracing: Return the pc of the last executed
/// instruction.
URV lastPc() const;
/// Support for tracing: Return the index of the integer register
/// written by the last executed instruction. Return -1 it no
/// integer register was written.
int lastIntReg() const;
/// Support for tracing: Return the index of the floating point
/// register written by the last executed instruction. Return -1
/// it no FP register was written.
int lastFpReg() const;
/// Support for tracing: Fill the csrs vector with the
/// register-numbers of the CSRs written by the execution of the
/// last instruction. CSRs modified as a side effect (e.g. mcycle
/// and minstret) are not included. Fill the triggers vector with
/// the number of the debug-trigger registers written by the
/// execution of the last instruction.
void lastCsr(std::vector<CsrNumber>& csrs,
std::vector<unsigned>& triggers) const;
/// Support for tracing: Fill the addresses and words vectors with
/// the addresses of the memory words modified by the last
/// executed instruction and their corresponding values.
void lastMemory(std::vector<size_t>& addresses,
std::vector<uint32_t>& words) const;
/// Return data address of last executed load instruction.
URV lastLoadAddress() const
{ return loadAddr_; }
/// Read instruction at given address. Return true on success and
/// false if address is out of memory bounds.
bool readInst(size_t address, uint32_t& instr);
/// Set instruction count limit: When running with tracing the
/// run and the runUntil methods will stop if the retired instruction
/// count (true count and not value of minstret) reaches or exceeds
/// the limit.
void setInstructionCountLimit(uint64_t limit)
{ instCountLim_ = limit; }
/// Reset executed instruction count.
void setInstructionCount(uint64_t count)
{ instCounter_ = count; }
/// Get executed instruction count.
uint64_t getInstructionCount() const
{ return instCounter_; }
/// Define instruction closed coupled memory (in core instruction memory).
bool defineIccm(size_t region, size_t offset, size_t size);
/// Define data closed coupled memory (in core data memory).
bool defineDccm(size_t region, size_t offset, size_t size);
/// Define a region for memory mapped registers.
bool defineMemoryMappedRegisterRegion(size_t region, size_t offset,
size_t size);
/// Define a memory mapped register. Region (as defined by region
/// and offset) must be already defined using
/// defineMemoryMappedRegisterRegion. The register address must not
/// fall outside the region
bool defineMemoryMappedRegisterWriteMask(size_t region,
size_t regionOffset,
size_t registerBlockOffset,
size_t registerIx,
uint32_t mask);
/// Called after memory is configured to refine memory access to
/// sections of regions containing ICCM, DCCM or PIC-registers.
void finishCcmConfig()
{ memory_.finishCcmConfig(); }
/// Turn off all fetch access (except in ICCM regions) then turn
/// it on only in the pages overlapping the given address windows.
/// Return true on success and false on failure (invalid window
/// entry). Do nothing returning true if the windows vector is
/// empty.
bool configMemoryFetch(const std::vector< std::pair<URV,URV> >& windows);
/// Turn off all data access (except in DCCM/PIC regions) then
/// turn it on only in the pages overlapping the given address
/// windows. Return true on success and false on failure (invalid
/// window entry). Do nothing returning true if the windows vector
/// is empty.
bool configMemoryDataAccess(const std::vector< std::pair<URV,URV> >& windows);
/// Direct this hart to take an instruction access fault exception
/// within the next singleStep invocation.
void postInstAccessFault(URV offset)
{ forceFetchFail_ = true; forceFetchFailOffset_ = offset; }
/// Direct this hart to take a data access fault exception within
/// the subsequent singleStep invocation executing a load/store
/// instruction or take an NMI (double-bit-ecc) within the
/// subsequent interrupt if fast-interrupt is enabled.
void postDataAccessFault(URV offset);
/// Enable printing of load-instruction data address in
/// instruction trace mode.
void setTraceLoad(bool flag)
{ traceLoad_ = flag; }
/// Return count of traps (exceptions or interrupts) seen by this
/// hart.
uint64_t getTrapCount() const
{ return exceptionCount_ + interruptCount_; }
/// Return count of exceptions seen by this hart.
uint64_t getExceptionCount() const
{ return exceptionCount_; }
/// Return count of interrupts seen by this hart.
uint64_t getInterruptCount() const
{ return interruptCount_; }
/// Set pre and post to the count of "before"/"after" triggers
/// that tripped by the last executed instruction.
void countTrippedTriggers(unsigned& pre, unsigned& post) const
{ csRegs_.countTrippedTriggers(pre, post); }
/// Apply an imprecise store exception at given address. Return
/// true if address is found exactly once in the store
/// queue. Return false otherwise. Save the given address in
/// mdseac. Set matchCount to the number of entries in the store
/// queue that match the given address.
bool applyStoreException(URV address, unsigned& matchCount);
/// Apply an imprecise load exception at given address. Return
/// true if address is found exactly once in the pending load
/// queue. Return false otherwise. Save the given address in
/// mdseac. Set matchCount to the number of entries in the store
/// queue that match the given address.
bool applyLoadException(URV address, unsigned tag, unsigned& matchCount);
/// This supports the test-bench. Mark load-queue entry matching
/// given address as completed and remove it from the queue. Set
/// match count to 1 if matching entry is found and zero
/// otherwise. Return true if matching entry found. The testbench
/// will invoke this only for loads where the destination register
/// is updated.
bool applyLoadFinished(URV address, unsigned tag, unsigned& matchCount);
/// Enable processing of imprecise load exceptions.
void enableLoadExceptions(bool flag)
{ loadQueueEnabled_ = flag; }
/// Set load queue size (used when load exceptions are enabled).
void setLoadQueueSize(unsigned size)
{ maxLoadQueueSize_ = size; }
/// Enable collection of instruction frequencies.
void enableInstructionFrequency(bool b);
/// Enable expedited dispatch of external interrupt handler: Instead of
/// setting pc to the external interrupt handler, we set it to the
/// specific entry associated with the external interrupt id.
void enableFastInterrupts(bool b)
{ fastInterrupts_ = b; }
/// Enable/disable the zbb (bit manipulation base) extension. When
/// disbaled all the instructions in zbb extension result in an
/// illegal instruction exception.
void enableRvzbb(bool flag)
{ rvzbb_ = flag; }
/// Enable/disable the zbs (bit manipulation single)
/// extension. When disbaled all the instructions in zbs extension
/// result in an illegal instruction exception.
void enableRvzbs(bool flag)
{ rvzbs_ = flag; }
/// Put this hart in debug mode setting the DCSR cause field to
/// the given cause.
void enterDebugMode(DebugModeCause cause, URV pc);
/// Put this hart in debug mode setting the DCSR cause field to
/// either DEBUGGER or SETP depending on the step bit of DCSR.
/// given cause.
void enterDebugMode(URV pc);
/// True if in debug mode.
bool inDebugMode() const
{ return debugMode_; }
/// True if in debug-step mode.
bool inDebugStepMode() const
{ return debugStepMode_; }
/// Take this hart out of debug mode.
void exitDebugMode();
/// Enable/disable imprecise store error rollback. This is useful
/// in test-bench server mode.
void enableStoreErrorRollback(bool flag)
{ storeErrorRollback_ = flag; }
/// Enable/disable imprecise load error rollback. This is useful
/// in test-bench server mode.
void enableLoadErrorRollback(bool flag)
{ loadErrorRollback_ = flag; }
/// Print collected instruction frequency to the given file.
void reportInstructionFrequency(FILE* file) const;
/// Reset trace data (items changed by the execution of an
/// instruction.)
void clearTraceData();
/// Enable debug-triggers. Without this, triggers will not trip
/// and will not cause exceptions.
void enableTriggers(bool flag)
{ enableTriggers_ = flag; }
/// Enable performance counters (count up for some enabled
/// performance counters when their events do occur).
void enablePerformanceCounters(bool flag)
{ enableCounters_ = flag; }
/// Enable gdb-mode.
void enableGdb(bool flag)
{ enableGdb_ = flag; }
/// Enable use of ABI register names (e.g. sp instead of x2) in
/// instruction disassembly.
void enableAbiNames(bool flag)
{ abiNames_ = flag; }
/// Return true if ABI register names are enabled.
bool abiNames() const
{ return abiNames_; }
/// Enable emulation of newlib system calls.
void enableNewlib(bool flag)
{ newlib_ = flag; }
/// Enable emulation of Linux system calls.
void enableLinux(bool flag)
{ linux_ = flag; }
/// For Linux emulation: Set initial target program break to the
/// RISCV page address larger than or equal to the given address.
void setTargetProgramBreak(URV addr);
/// For Linux emulation: Put the program arguments on the stack
/// suitable for calling the target program main from _start.
/// Return true on success and false on failure (not all stack
/// area required is writable).
bool setTargetProgramArgs(const std::vector<std::string>& args);
/// Return true if given address is in the data closed coupled
/// memory of this hart.
bool isAddressInDccm(size_t addr) const
{ return memory_.isAddrInDccm(addr); }
/// Return true if given data (ld/st) address is external to the hart.
bool isDataAddressExternal(size_t addr) const
{ return memory_.isDataAddrExternal(addr); }
/// Return true if rv32f (single precision floating point)
/// extension is enabled in this hart.
bool isRvf() const
{ return rvf_; }
/// Return true if rv64d (double precision floating point)
/// extension is enabled in this hart.
bool isRvd() const
{ return rvd_; }
/// Return true if rv64 (64-bit option) extension is enabled in
/// this hart.
bool isRv64() const
{ return rv64_; }
/// Return true if rvm (multiply/divide) extension is enabled in
/// this hart.
bool isRvm() const
{ return rvm_; }
/// Return true if rvc (compression) extension is enabled in this
/// hart.
bool isRvc() const
{ return rvc_; }
/// Return true if rva (atomic) extension is enabled in this hart.
bool isRva() const
{ return rva_; }
/// Return true if rvu (user-mode) extension is enabled in this
/// hart.
bool isRvs() const
{ return rvs_; }
/// Return true if rvu (user-mode) extension is enabled in this
/// hart.
bool isRvu() const
{ return rvu_; }
/// Return true if zbb extension is enabled in this hart.
bool isRvzbb() const
{ return rvzbb_; }
/// Return true if zbs extension is enabled in this hart.
bool isRvzbs() const
{ return rvzbs_; }
/// Return true if current program is considered finihsed (either
/// reached stop address or executed exit limit).
bool hasTargetProgramFinished() const
{ return targetProgFinished_; }
/// Mark target program as finished/non-finished based on flag.
void setTargetProgramFinished(bool flag)
{ targetProgFinished_ = flag; }
/// Make atomic memory operations illegal/legal outside of the DCCM
/// region based on the value of flag (true/false).
void setAmoIllegalOutsideDccm(bool flag)
{ amoIllegalOutsideDccm_ = flag; }
/// Make load/store instructions take an exception if the base
/// address (value in rs1) and the effective address refer to
/// regions of different types.
void setEaCompatibleWithBase(bool flag)
{ eaCompatWithBase_ = flag; }
size_t getMemorySize() const
{ return memory_.size(); }
/// Copy memory region configuration from other processor.
void copyMemRegionConfig(const Hart<URV>& other);
/// Return true if hart was put in run state after reset. Hart 0
/// is automatically in run state after reset. If mhartstart CSR
/// exists, then each remaining hart must be explicitly started by
/// hart 0 by writing to the corresponding bit in that CSR. This
/// is special for WD.
bool isStarted() const
{ return hartStarted_; }
/// Mark this hart as started.
void setStarted(bool flag)
{ hartStarted_ = flag; }
/// Return the local (within a core) hart-id of this hart. Local
/// hart ids are dense and start at zero.
unsigned localHartId()
{ return localHartId_; }
/// Tie the shared CSRs in this hart to the corresponding CSRs in
/// the target hart making them share the same location for their
/// value.
void tieSharedCsrsTo(Hart<URV>& target)
{ return csRegs_.tieSharedCsrsTo(target.csRegs_); }
/// Return true if non-maskable interrupts (NMIs) should be delivered
/// to this hart.
bool isNmiEnabled() const
{ return nmiEnabled_; }
/// Enable delivery of NMIs to this hart.
bool enableNmi(bool flag)
{ return nmiEnabled_ = flag; }
/// Record given CSR number for later reporting of CSRs modified by
/// an instruction.
void recordCsrWrite(CsrNumber csr)
{ csRegs_.recordWrite(csr); }
protected:
/// Helper to run method: Run until toHost is written or until
/// exit is called.
bool simpleRun();
/// Helper to decode. Used for compressed instructions.
const InstEntry& decode16(uint16_t inst, uint32_t& op0, uint32_t& op1,
uint32_t& op2);
/// Helper to whatIfSingleStep.
void collectAndUndoWhatIfChanges(URV prevPc, ChangeRecord& record);
/// Return the effective rounding mode for the currently executing
/// floating point instruction.
RoundingMode effectiveRoundingMode(RoundingMode instMode);
/// Update the accrued floating point bits in the FCSR register.
void updateAccruedFpBits();
/// Undo the effect of the last executed instruction given that
/// that a trigger has tripped.
void undoForTrigger();
/// Return true if the mie bit of the mstatus register is on.
bool isInterruptEnabled() const
{ return csRegs_.isInterruptEnabled(); }
/// Based on current trigger configurations, either take an
/// exception returning false or enter debug mode returning true.
bool takeTriggerAction(FILE* traceFile, URV epc, URV info,
uint64_t& counter, bool beforeTiming);
/// Helper to load/store.
bool misalignedAccessCausesException(URV addr, unsigned accessSize,
SecondaryCause& secCause) const;
/// Helper to load methods: Initiate an exception with the given
/// cause and data address.
void initiateLoadException(ExceptionCause cause, URV addr,
SecondaryCause secCause);
/// Helper to store methods: Initiate an exception with the given
/// cause and data address.
void initiateStoreException(ExceptionCause cause, URV addr,
SecondaryCause secCause);
/// Helper to load methods: Return true if base and effective
/// address fall in regions of different types (with respect to io
/// and cacheability).
bool effectiveAndBaseAddrMismatch(URV base, URV addr);
/// Helper to lb, lh, lw and ld. Load type should be int_8, int16_t
/// etc... for signed byte, halfword etc... and uint8_t, uint16_t
/// etc... for lbu, lhu, etc...
/// Return true if the load is successful. Return false if an exception
/// or a trigger is encoutered.
template<typename LOAD_TYPE>
bool load(uint32_t rd, uint32_t rs1, int32_t imm);
/// Helper to load method: Return possible load exception (wihtout
/// taking any exception).
ExceptionCause determineLoadException(unsigned rs1, URV base, URV addr,
unsigned ldSize,
SecondaryCause& secCause);
/// Helper to sb, sh, sw ... Sore type should be uint8_t, uint16_t
/// etc... for sb, sh, etc...
/// Return true if store is successful. Return false if an exception
/// or a trigger is encoutered.
template<typename STORE_TYPE>
bool store(unsigned rs1, URV base, URV addr, STORE_TYPE value);
/// Helper to store method: Return possible exception (wihtout
/// taking any exception). Update stored value by doing memory
/// mapped register masking.
template<typename STORE_TYPE>
ExceptionCause determineStoreException(unsigned rs1, URV base, URV addr,
STORE_TYPE& storeVal,
SecondaryCause& secCause);
/// Helper to execLr. Load type should be int32_t, or int64_t.
/// Return true if instruction is successful. Return false if an
/// exception occurs or a trigger is tripped.
template<typename LOAD_TYPE>
bool loadReserve(uint32_t rd, uint32_t rs1);
/// Helper to execSc. Store type should be uint32_t, or uint64_t.
/// Return true if store is successful. Return false otherwise
/// (exception or trigger or condition failed).
template<typename STORE_TYPE>
bool storeConditional(unsigned rs1, URV addr, STORE_TYPE value);
/// Do a 64-bit wide load in one transaction. This is swerv
/// specfic.
bool wideLoad(unsigned rd, URV addr, unsigned ldSize);
/// Do a 64-bit wide store in one transaction. This is swerv
/// specfic.
bool wideStore(URV addr, URV storeVal, unsigned storeSize);
/// Helper to load methods. Check loads performed with stack
/// pointer. Return true if referenced bytes are all between the
/// stack bottom and the stack pointer value excluding the stack
/// pointer value. Initiate an exception and return false
/// otherwise.
bool checkStackLoad(URV addr, unsigned loadSize);
/// Helper to store methods. Check stores performed with stack
/// pointer. Return true if referenced bytes are all between the
/// stack bottom and the stack top excluding the stack top and
/// false otherwise.
bool checkStackStore(URV addr, unsigned storeSize);
/// Helper to CSR instructions. Keep minstret and mcycle up to date.
void preCsrInstruction(CsrNumber csr);
/// Helper to CSR instructions: Write csr and integer register if csr
/// is writeable.
void doCsrWrite(CsrNumber csr, URV csrVal, unsigned intReg,
URV intRegVal);