forked from westerndigitalcorporation/swerv-ISS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interactive.cpp
1524 lines (1313 loc) · 38.3 KB
/
Interactive.cpp
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/>.
//
#include <iostream>
#include <fstream>
#include <sstream>
#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
#include "Interactive.hpp"
#include "linenoise.h"
using namespace WdRiscv;
/// Return format string suitable for printing an integer of type URV
/// in hexadecimal form.
template <typename URV>
static
const char*
getHexForm()
{
if (sizeof(URV) == 4)
return "0x%08x";
if (sizeof(URV) == 8)
return "0x%016x";
if (sizeof(URV) == 16)
return "0x%032x";
return "0x%x";
}
/// Convert the command line string numberStr to a number using
/// strotull and a base of zero (prefixes 0 and 0x are
/// honored). Return true on success and false on failure (string does
/// not represent a number). TYPE is an integer type (e.g
/// uint32_t). Option is the command line option associated with the
/// string and is used for diagnostic messages.
template <typename TYPE>
static
bool
parseCmdLineNumber(const std::string& option,
const std::string& numberStr,
TYPE& number)
{
bool good = not numberStr.empty();
if (good)
{
char* end = nullptr;
if (sizeof(TYPE) == 4)
number = strtoul(numberStr.c_str(), &end, 0);
else if (sizeof(TYPE) == 8)
number = strtoull(numberStr.c_str(), &end, 0);
else
{
std::cerr << "parseCmdLineNumber: Only 32/64-bit RISCV cores supported\n";
return false;
}
if (end and *end)
good = false; // Part of the string are non parseable.
}
if (not good)
std::cerr << "Invalid command line " << option << " value: " << numberStr << '\n';
return good;
}
template <typename URV>
Interactive<URV>::Interactive(std::vector< Core<URV>* >& coreVec)
: cores_(coreVec)
{
}
template <typename URV>
bool
Interactive<URV>::untilCommand(Core<URV>& core, const std::string& line,
const std::vector<std::string>& tokens,
FILE* traceFile)
{
if (tokens.size() != 2)
{
std::cerr << "Invalid until command: " << line << '\n';
std::cerr << "Expecting: until address\n";
return false;
}
URV addr = 0;
if (not parseCmdLineNumber("address", tokens.at(1), addr))
return false;
return core.untilAddress(addr, traceFile);
}
template <typename URV>
bool
Interactive<URV>::stepCommand(Core<URV>& core, const std::string& /*line*/,
const std::vector<std::string>& tokens,
FILE* traceFile)
{
if (tokens.size() == 1)
{
core.singleStep(traceFile);
core.clearTraceData();
return true;
}
uint64_t count;
if (not parseCmdLineNumber("instruction-count", tokens.at(1), count))
return false;
if (count == 0)
return true;
for (uint64_t i = 0; i < count; ++i)
{
core.singleStep(traceFile);
core.clearTraceData();
}
return true;
}
template <typename URV>
static
void
peekAllFpRegs(Core<URV>& core)
{
for (size_t i = 0; i < core.fpRegCount(); ++i)
{
uint64_t val = 0;
if (core.peekFpReg(i, val))
{
std::cout << "f" << i << ": "
<< (boost::format("0x%016x") % val) << '\n';
}
}
}
template <typename URV>
static
void
peekAllIntRegs(Core<URV>& core)
{
bool abiNames = core.abiNames();
auto hexForm = getHexForm<URV>(); // Format string for printing a hex val
for (size_t i = 0; i < core.intRegCount(); ++i)
{
std::string name;
URV val = 0;
if (core.peekIntReg(i, val, name))
{
std::string tag = name;
if (abiNames)
tag += "(" + std::to_string(i) + ")";
tag += ":";
std::cout << (boost::format("%-9s") % tag)
<< (boost::format(hexForm) % val) << '\n';
}
}
}
template <typename URV>
static
void
peekAllCsrs(Core<URV>& core)
{
auto hexForm = getHexForm<URV>(); // Format string for printing a hex val
std::cout << (boost::format("%-23s") % "csr");
if (sizeof(URV) == 4)
std::cout << (boost::format("%-10s %-10s %-10s %-10s\n") % "value" %
"reset" % "mask" % "pokemask");
else
std::cout << (boost::format("%-18s %-18s %-18s %-10s\n") % "value" %
"reset" % "mask" % "pokemask");
for (size_t i = 0; i <= size_t(CsrNumber::MAX_CSR_); ++i)
{
CsrNumber csr = CsrNumber(i);
std::string name;
URV val = 0;
if (core.peekCsr(csr, val, name))
{
std::ostringstream oss;
oss << name << "(0x" << std::hex << i << "):";
std::cout << (boost::format("%-23s") % oss.str())
<< (boost::format(hexForm) % val);
URV reset = 0, writeMask = 0, pokeMask = 0;
if (core.peekCsr(csr, val, reset, writeMask, pokeMask))
{
std::cout << ' ' << (boost::format(hexForm) % reset);
std::cout << ' ' << (boost::format(hexForm) % writeMask);
std::cout << ' ' << (boost::format(hexForm) % pokeMask);
}
std::cout << '\n';
}
}
}
template <typename URV>
static
void
peekAllTriggers(Core<URV>& core)
{
auto hexForm = getHexForm<URV>(); // Format string for printing a hex val
std::cout << (boost::format("%-12s") % "trigger");
if (sizeof(URV) == 4)
std::cout << (boost::format("%-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s\n") %
"value1" % "value2" % "value3" %
"mask1" % "mask2" % "mask3" %
"poke-mask1" % "poke-mask2" % "poke-mask3");
else
std::cout << (boost::format("%-18s %-18s %-18s %-18s %-18s %-18s %-18s %-18s %-18s\n") %
"value1" % "value2" % "value3" %
"mask1" % "mask2" % "mask3" %
"poke-mask1" % "poke-mask2" % "poke-mask3");
// value/reset/write-mask/poke-mask
URV tselVal = 0, tselReset, tselWm = 0, tselPm = 0;
if (core.peekCsr(CsrNumber::TSELECT, tselVal, tselReset, tselWm, tselPm))
{
URV maxTrigger = tselWm;
for (URV trigger = 0; trigger <= maxTrigger; ++trigger)
{
URV v1(0), v2(0), v3(0), wm1(0), wm2(0), wm3(0);
URV pm1(0), pm2(0), pm3(0);
if (core.peekTrigger(trigger, v1, v2, v3, wm1, wm2, wm3,
pm1, pm2, pm3))
{
std::string name = "trigger" + std::to_string(trigger) + ":";
std::cout << (boost::format("%-11s") % name);
std::cout << ' ' << (boost::format(hexForm) % v1);
std::cout << ' ' << (boost::format(hexForm) % v2);
std::cout << ' ' << (boost::format(hexForm) % v3);
std::cout << ' ' << (boost::format(hexForm) % wm1);
std::cout << ' ' << (boost::format(hexForm) % wm2);
std::cout << ' ' << (boost::format(hexForm) % wm3);
std::cout << ' ' << (boost::format(hexForm) % pm1);
std::cout << ' ' << (boost::format(hexForm) % pm2);
std::cout << ' ' << (boost::format(hexForm) % pm3);
std::cout << '\n';
}
else
break;
}
}
}
template <typename URV>
bool
Interactive<URV>::peekCommand(Core<URV>& core, const std::string& line,
const std::vector<std::string>& tokens)
{
if (tokens.size() < 2)
{
std::cerr << "Invalid peek command: " << line << '\n';
std::cerr << "Expecting: peek <item> <addr> or peek pc or peek all\n";
std::cerr << " Item is one of r, f, c, t or m for integer, floating point,\n";
std::cerr << " CSR, trigger register or memory location respective\n";
std::cerr << " example: peek r x3\n";
std::cerr << " example: peek c mtval\n";
std::cerr << " example: peek m 0x4096\n";
std::cerr << " example: peek t 0\n";
std::cerr << " example: peek pc\n";
return false;
}
auto hexForm = getHexForm<URV>(); // Format string for printing a hex val
URV val = 0;
const std::string& resource = tokens.at(1);
if (resource == "all")
{
std::cout << "pc: " << (boost::format(hexForm) % core.peekPc()) << '\n';
std::cout << "\n";
peekAllIntRegs(core);
std::cout << "\n";
peekAllCsrs(core);
std::cout << "\n";
peekAllTriggers(core);
return true;
}
if (resource == "pc")
{
URV pc = core.peekPc();
std::cout << (boost::format(hexForm) % pc) << std::endl;
return true;
}
if (tokens.size() < 3)
{
std::cerr << "Invalid peek command: " << line << '\n';
std::cerr << "Expecting: peek <resource> <address>\n";
return false;
}
const std::string& addrStr = tokens.at(2);
if (resource == "m")
{
URV addr0 = 0;
if (not parseCmdLineNumber("memory-address", addrStr, addr0))
return false;
URV addr1 = addr0;
if (tokens.size() == 4)
if (not parseCmdLineNumber("memory-address", tokens.at(3), addr1))
return false;
uint32_t word = 0;
for (URV addr = addr0; addr <= addr1; addr += 4)
{
if (not core.peekMemory(addr, word))
{
std::cerr << "Memory address out of bounds: " << addrStr << '\n';
return false;
}
std::cout << (boost::format(hexForm) % addr) << ": ";
std::cout << (boost::format("0x%08x") % word) << std::endl;
}
return true;
}
if (resource == "r")
{
if (addrStr == "all")
{
peekAllIntRegs(core);
return true;
}
unsigned intReg = 0;
if (not core.findIntReg(addrStr, intReg))
{
std::cerr << "No such integer register: " << addrStr << '\n';
return false;
}
if (core.peekIntReg(intReg, val))
{
std::cout << (boost::format(hexForm) % val) << std::endl;
return true;
}
std::cerr << "Failed to read integer register: " << addrStr << '\n';
return false;
}
if (resource == "f")
{
if (not core.isRvf())
{
std::cerr << "Floating point extension is no enabled\n";
return false;
}
if (addrStr == "all")
{
peekAllFpRegs(core);
return true;
}
unsigned fpReg = 0;
if (not core.findFpReg(addrStr, fpReg))
{
std::cerr << "No such integer register: " << addrStr << '\n';
return false;
}
uint64_t fpVal = 0;
if (core.peekFpReg(fpReg, fpVal))
{
std::cout << (boost::format("0x%016x") % fpVal) << std::endl;
return true;
}
std::cerr << "Failed to read fp register: " << addrStr << '\n';
return false;
}
if (resource == "c")
{
if (addrStr == "all")
{
peekAllCsrs(core);
return true;
}
auto csr = core.findCsr(addrStr);
if (not csr)
{
std::cerr << "No such CSR: " << addrStr << '\n';
return false;
}
if (core.peekCsr(csr->getNumber(), val))
{
std::cout << (boost::format(hexForm) % val) << std::endl;
return true;
}
std::cerr << "Failed to read CSR: " << addrStr << '\n';
return false;
}
if (resource == "t")
{
if (addrStr == "all")
{
peekAllTriggers(core);
return true;
}
URV trigger = 0;
if (not parseCmdLineNumber("trigger-number", addrStr, trigger))
return false;
URV v1(0), v2(0), v3(0);
if (core.peekTrigger(trigger, v1, v2, v3))
{
std::cout << (boost::format(hexForm) % v1) << ' '
<< (boost::format(hexForm) % v2) << ' '
<< (boost::format(hexForm) % v3) << std::endl;
return true;
}
std::cerr << "Trigger number out of bounds: " << addrStr << '\n';
return false;
}
std::cerr << "No such resource: " << resource
<< " -- expecting r, m, c, t, or pc\n";
return false;
}
template <typename URV>
bool
Interactive<URV>::pokeCommand(Core<URV>& core, const std::string& line,
const std::vector<std::string>& tokens)
{
if (tokens.size() < 3)
{
std::cerr << "Invalid poke command: " << line << '\n';
std::cerr << " Expecting: poke pc <value>\n";
std::cerr << " or poke <resource> <address> <value>\n";
std::cerr << " or poke t <number> <value1> <value2> <value3>\n";
std::cerr << " where <resource> is one of r, f, c, t or m\n";
return false;
}
const std::string& resource = tokens.at(1);
URV value = 0;
if (resource == "pc")
{
if (not parseCmdLineNumber("value", tokens.at(2), value))
return false;
core.pokePc(value);
return true;
}
size_t count = tokens.size();
if ((resource == "t" and count != 6) or (resource != "t" and count != 4))
{
std::cerr << "Invalid poke command: " << line << '\n';
std::cerr << " Expecting: poke <resource> <address> <value>\n";
std::cerr << " or poke t <number> <value1> <value2> <value3>\n";
std::cerr << " where <resource> is one of r, c, or m\n";
return false;
}
const std::string& addrStr = tokens.at(2);
const std::string& valueStr = tokens.at(3);
if (not parseCmdLineNumber("value", valueStr, value))
return false;
if (resource == "r")
{
unsigned intReg = 0;
if (core.findIntReg(addrStr, intReg))
{
if (core.pokeIntReg(intReg, value))
return true;
std::cerr << "Failed to write integer register " << addrStr << '\n';
return false;
}
std::cerr << "No such integer register " << addrStr << '\n';
return false;
}
if (resource == "f")
{
unsigned fpReg = 0;
if (core.findFpReg(addrStr, fpReg))
{
if (core.pokeFpReg(fpReg, value))
return true;
std::cerr << "Failed to write FP register " << addrStr << '\n';
return false;
}
std::cerr << "No such FP register " << addrStr << '\n';
return false;
}
if (resource == "c")
{
auto csr = core.findCsr(addrStr);
if (csr)
{
if (core.pokeCsr(csr->getNumber(), value))
return true;
std::cerr << "Failed to write CSR " << addrStr << '\n';
return false;
}
std::cerr << "No such CSR " << addrStr << '\n';
return false;
}
if (resource == "t")
{
URV trigger = 0, v1 = 0, v2 = 0, v3 = 0;
if (not parseCmdLineNumber("trigger", addrStr, trigger))
return false;
if (not parseCmdLineNumber("value1", tokens.at(3), v1))
return false;
if (not parseCmdLineNumber("value2", tokens.at(4), v2))
return false;
if (not parseCmdLineNumber("value3", tokens.at(5), v3))
return false;
if (core.pokeTrigger(trigger, v1, v2, v3))
return true;
std::cerr << "Trigger out of bounds: " << addrStr << '\n';
return false;
}
if (resource == "m")
{
URV addr = 0;
if (not parseCmdLineNumber("address", addrStr, addr))
return false;
if (core.pokeMemory(addr, value))
return true;
std::cerr << "Address out of bounds: " << addrStr << '\n';
return false;
}
std::cerr << "No such resource: " << resource <<
" -- expecting r, c, m or pc\n";
return false;
}
template <typename URV>
bool
Interactive<URV>::disassCommand(Core<URV>& core, const std::string& line,
const std::vector<std::string>& tokens)
{
if (tokens.size() >= 2 and tokens.at(1) == "opcode")
{
for (size_t i = 2; i < tokens.size(); ++i)
{
uint32_t code = 0;
if (not parseCmdLineNumber("opcode", tokens[i], code))
return false;
std::string str;
core.disassembleInst(code, str);
std::cout << " " << tokens[i] << ": " << str << '\n';
}
return true;
}
auto hexForm = getHexForm<URV>(); // Format string for printing a hex val
if (tokens.size() == 3 and (tokens.at(1) == "func" or
tokens.at(1) == "function"))
{
std::string item = tokens.at(2);
std::string name; // item (if a symbol) or function name containing item
ElfSymbol symbol;
if (core.findElfSymbol(item, symbol))
name = item;
else
{
// See if address falls in a function, then disassemble function.
URV addr = 0;
if (not parseCmdLineNumber("address", item, addr))
return false;
// Find function containing address.
core.findElfFunction(addr, name, symbol);
}
if (name.empty())
{
std::cerr << "Not a function or an address withing a function: " << item
<< '\n';
return false;
}
std::cout << "disassemble function " << name << ":\n";
size_t start = symbol.addr_, end = symbol.addr_ + symbol.size_;
for (size_t addr = start; addr < end; )
{
uint32_t inst = 0;
if (not core.peekMemory(addr, inst))
{
std::cerr << "Address out of bounds: 0x" << std::hex << addr << '\n';
return false;
}
unsigned instSize = instructionSize(inst);
if (instSize == 2)
inst = (inst << 16) >> 16; // Clear top 16 bits.
std::string str;
core.disassembleInst(inst, str);
std::cout << " " << (boost::format(hexForm) % addr) << ' '
<< (boost::format(hexForm) % inst) << ' ' << str << '\n';
addr += instSize;
}
return true;
}
if (tokens.size() != 3)
{
std::cerr << "Invalid disass command: " << line << '\n';
std::cerr << "Expecting: disass opcode <number> ...\n";
std::cerr << " or: disass function <name>\n";
std::cerr << " or: disass function <addr>\n";
std::cerr << " or: disass <addr1> <addr2>\n";
return false;
}
URV addr1, addr2;
if (not parseCmdLineNumber("address", tokens[1], addr1))
return false;
if (not parseCmdLineNumber("address", tokens[2], addr2))
return false;
for (URV addr = addr1; addr <= addr2; )
{
uint32_t inst = 0;
if (not core.peekMemory(addr, inst))
{
std::cerr << "Address out of bounds: 0x" << std::hex << addr << '\n';
return false;
}
unsigned instSize = instructionSize(inst);
if (instSize == 2)
inst = (inst << 16) >> 16; // Clear top 16 bits.
std::string str;
core.disassembleInst(inst, str);
std::cout << (boost::format(hexForm) % addr) << ' '
<< (boost::format(hexForm) % inst) << ' '
<< str << '\n';
addr += instSize;
}
return true;
}
template<typename URV>
extern
bool
loadElfFile(Core<URV>& core, const std::string& filePath);
template <typename URV>
bool
Interactive<URV>::elfCommand(Core<URV>& core, const std::string& line,
const std::vector<std::string>& tokens)
{
if (tokens.size() != 2)
{
std::cerr << "Invalid elf command: " << line << '\n';
std::cerr << "Expecting: elf <file-name>\n";
return false;
}
std::string fileName = tokens.at(1);
return loadElfFile(core, fileName);
}
template <typename URV>
bool
Interactive<URV>::hexCommand(Core<URV>& core, const std::string& line,
const std::vector<std::string>& tokens)
{
if (tokens.size() != 2)
{
std::cerr << "Invalid hex command: " << line << '\n';
std::cerr << "Expecting: hex <file-name>\n";
return false;
}
std::string fileName = tokens.at(1);
if (not core.loadHexFile(fileName))
return false;
return true;
}
template <typename URV>
bool
Interactive<URV>::resetCommand(Core<URV>& core, const std::string& /*line*/,
const std::vector<std::string>& tokens)
{
if (tokens.size() == 1)
{
core.reset(resetMemoryMappedRegs_);
return true;
}
if (tokens.size() == 2)
{
URV resetPc = 0;
if (not parseCmdLineNumber("reset-pc", tokens[1], resetPc))
return false;
core.defineResetPc(resetPc);
core.reset(resetMemoryMappedRegs_);
return true;
}
std::cerr << "Invalid reset command (extra arguments)\n";
return false;
}
template <typename URV>
bool
Interactive<URV>::replayFileCommand(const std::string& line,
const std::vector<std::string>& tokens,
std::ifstream& stream)
{
if (tokens.size() != 2)
{
std::cerr << "Invalid replay_file command: " << line << '\n';
std::cerr << "Expecting: replay_file <file-name>\n";
return false;
}
std::string fileName = tokens.at(1);
stream.close();
stream.open(fileName.c_str());
if (not stream.good())
{
std::cerr << "Failed to open replay-file '" << fileName << "'\n";
return false;
}
return true;
}
template <typename URV>
bool
Interactive<URV>::exceptionCommand(Core<URV>& core, const std::string& line,
const std::vector<std::string>& tokens)
{
bool bad = false;
URV addr = 0;
if (tokens.size() < 2)
bad = true;
else
{
const std::string& tag = tokens.at(1);
if (tag == "inst")
{
if (tokens.size() == 2)
core.postInstAccessFault(0);
else if (tokens.size() == 3)
{
if (parseCmdLineNumber("exception inst offset", tokens.at(2), addr))
core.postInstAccessFault(addr);
else
bad = true;
}
else
bad = true;
}
else if (tag == "data")
{
if (tokens.size() == 2)
core.postDataAccessFault(0);
else if (tokens.size() == 3)
{
if (parseCmdLineNumber("exception data offset", tokens.at(2), addr))
core.postDataAccessFault(addr);
else
bad = true;
}
else
bad = true;
}
else if (tag == "store")
{
bad = tokens.size() != 3;
if (not bad)
{
bad = not parseCmdLineNumber("exception store address",
tokens.at(2), addr);
if (not bad)
{
unsigned matchCount = 0;
if (core.applyStoreException(addr, matchCount))
return true;
std::cerr << "Invalid exception store command: " << line << '\n';
if (matchCount == 0)
std::cerr << " No pending store or invalid address\n";
else
std::cerr << " Multiple matching addresses (unsupported)\n";
return false;
}
}
}
else if (tag == "load")
{
bad = tokens.size() != 3;
if (not bad)
{
bad = not parseCmdLineNumber("exception load address",
tokens.at(2), addr);
if (not bad)
{
unsigned matchCount = 0;
if (core.applyLoadException(addr, matchCount))
return true;
std::cerr << "Invalid exception load command: " << line << '\n';
if (matchCount == 0)
std::cerr << " No pending load or invalid address\n";
else
std::cerr << " Multiple matching addresses (unsupported)\n";
return false;
}
}
}
else if (tag == "nmi")
{
bad = tokens.size() != 3;
if (not bad)
{
bad = not parseCmdLineNumber("nmi", tokens.at(2), addr);
if (not bad)
{
core.setPendingNmi(NmiCause(addr));
return true;
}
}
}
else if (tag == "memory_data")
{
if (parseCmdLineNumber("memory_data", tokens.at(2), addr))
{
return true;
}
}
else if (tag == "memory_inst")
{
if (parseCmdLineNumber("memory_inst", tokens.at(2), addr))
{
return true;
}
}
else
bad = true;
}
if (bad)
{
std::cerr << "Invalid exception command: " << line << '\n';
std::cerr << " Expecting: exception inst [<offset>]\n";
std::cerr << " or: exception data [<offset>]\n";
std::cerr << " or: exception load <address>\n";
std::cerr << " or: exception store <address>\n";
std::cerr << " or: exception nmi <cause>\n";
return false;
}
return true;
}
template <typename URV>
bool
Interactive<URV>::loadFinishedCommand(Core<URV>& core, const std::string& line,
const std::vector<std::string>& tokens)
{
if (tokens.size() != 2)
{
std::cerr << "Invalid load_finished command: " << line << '\n';
std::cerr << " Expecting: load_finished address\n";
return false;
}
URV addr = 0;
if (not parseCmdLineNumber("address", tokens.at(1), addr))
return false;
unsigned matches = 0;
core.applyLoadFinished(addr, matches);
return true;
}
/// If tokens contain a string of the form hart=<id> then remove that
/// token from tokens and set hartId to <id> returning true. Return
/// false if no hart=<id> token is found or if there is an error (<id>
/// is not an integer value) in which case error is set to true.
static
bool
getCommandHartId(std::vector<std::string>& tokens, unsigned& hartId,
bool& error)
{
error = false;
if (tokens.empty())
return false;
bool hasHart = false;
// Remaining tokens after removal of hart=<id> tokens.
std::vector<std::string> rest;
for (const auto& token : tokens)
{
if (boost::starts_with(token, "hart="))
{
std::string value = token.substr(strlen("hart="));
try
{
hartId = boost::lexical_cast<unsigned>(value);
hasHart = true;
}
catch(...)
{
std::cerr << "Bad hart id: " << value << '\n';
error = true;
return false;
}
}
else
rest.push_back(token);
}