1*** Keywords ***
2Create Machine
3    Execute Command                             mach create
4    Execute Command                             machine LoadPlatformDescriptionFromString "cpu: CPU.RiscV64 @ sysbus { cpuType: \\"rv64imacv_zicsr\\"; timeProvider: empty }"
5    Execute Command                             machine LoadPlatformDescriptionFromString "mem: Memory.MappedMemory @ sysbus 0x0 { size: 0x1000 }"
6
7    Execute Command                             sysbus.cpu PC 0x0
8
9*** Test Cases ***
10Should Count Custom 16-bit Instruction
11    Create Machine
12
13    Execute Command                             sysbus.cpu InstallCustomInstructionHandlerFromString "1011001110001110" "cpu.DebugLog('custom instruction executed!')"
14    Execute Command                             sysbus.cpu EnableCustomOpcodesCounting
15
16    Execute Command                             sysbus WriteWord 0x0 0xb38e
17
18    Execute Command                             sysbus.cpu Step
19
20    PC Should Be Equal                          0x2
21    ${c}=  Execute Command                      sysbus.cpu GetOpcodeCounter "1011001110001110"
22    Should Be Equal As Numbers                  ${c}  1
23
24Should Count Custom 32-bit Instruction
25    Create Machine
26
27    Execute Command                             sysbus.cpu InstallCustomInstructionHandlerFromString "10110011100011110000111110000011" "cpu.DebugLog('custom instruction executed!')"
28    Execute Command                             sysbus.cpu EnableCustomOpcodesCounting
29
30    Execute Command                             sysbus WriteDoubleWord 0x0 0xb38f0f83
31
32    Execute Command                             sysbus.cpu Step
33
34    PC Should Be Equal                          0x4
35    ${c}=  Execute Command                      sysbus.cpu GetOpcodeCounter "10110011100011110000111110000011"
36    Should Be Equal As Numbers                  ${c}  1
37
38Should Count Custom 64-bit Instruction
39    Create Machine
40
41    Execute Command                             sysbus.cpu InstallCustomInstructionHandlerFromString "1011001110001111000011111000001010110011100011110000111110111111" "cpu.DebugLog('custom instruction executed!')"
42    Execute Command                             sysbus.cpu EnableCustomOpcodesCounting
43
44    Execute Command                             sysbus WriteDoubleWord 0x0 0xb38f0fbf
45    Execute Command                             sysbus WriteDoubleWord 0x4 0xb38f0f82
46
47    Execute Command                             sysbus.cpu Step
48
49    PC Should Be Equal                          0x8
50    ${c}=  Execute Command                      sysbus.cpu GetOpcodeCounter "1011001110001111000011111000001010110011100011110000111110111111"
51    Should Be Equal As Numbers                  ${c}  1
52
53Should Count Standard Opcode
54    Create Machine
55
56    Execute Command                             sysbus.cpu InstallOpcodeCounterPattern "nop" "0000000000010011"
57    Execute Command                             sysbus.cpu EnableOpcodesCounting true
58
59    Execute Command                             sysbus WriteDoubleWord 0x0 0x13
60    Execute Command                             sysbus WriteDoubleWord 0x4 0x13
61    Execute Command                             sysbus WriteDoubleWord 0x8 0x13
62
63    Execute Command                             sysbus.cpu Step 3
64
65    PC Should Be Equal                          0xC
66    ${c}=  Execute Command                      sysbus.cpu GetOpcodeCounter "nop"
67    Should Be Equal As Numbers                  ${c}  3
68
69    Create Machine
70    Create Log Tester                           1
71
72Should Count RVV Opcode
73    Create Machine
74
75    Execute Command                             sysbus.cpu EnableVectorOpcodesCounting
76
77    # vlm.v
78    Execute Command                             sysbus WriteDoubleWord 0x0 0x02b00007
79
80    Execute Command                             sysbus.cpu Step
81
82    PC Should Be Equal                          0x4
83    ${c}=  Execute Command                      sysbus.cpu GetOpcodeCounter "vlm.v"
84    Should Be Equal As Numbers                  ${c}  1
85
86Should Parse All Embedded RISC-V Opcodes
87    Create Machine
88
89    ${names}=  Execute Command                  sysbus.cpu GetRiscvOpcodesEmbeddedResourceNames
90    ${names}=  Remove String                    ${names}  [  ]  \n  ${SPACE}
91    # remove the dangling `,` produced by the Monitor
92    ${names}=  Evaluate                         '${names}'.rstrip(',')
93    @{names}=  Split String                     ${names}  ,
94
95    FOR  ${name}  IN  @{names}
96        Execute Command                         sysbus.cpu EnableRiscvOpcodesCountingFromEmbeddedResource "${name}"
97    END
98
99    ${r}=  Execute Command                      sysbus.cpu GetAllOpcodesCounters
100    Should Contain                              ${r}  \@custom0
101    Should Contain                              ${r}  wfi
102
103Should Count RISC-V Opcodes
104    Create Machine
105
106    # this should enable all opcodes supported by the simulated core
107    Execute Command                             sysbus.cpu EnableRiscvOpcodesCounting
108
109    # auipc
110    Execute Command                             sysbus WriteDoubleWord 0x0 0x00000297
111    # addi
112    Execute Command                             sysbus WriteDoubleWord 0x4 0x01028293
113    # csrw
114    Execute Command                             sysbus WriteDoubleWord 0x8 0x30529073
115    # j
116    Execute Command                             sysbus WriteDoubleWord 0xC 0x0000006f
117
118    Execute Command                             sysbus.cpu Step 4
119
120    ${c}=  Execute Command                      sysbus.cpu GetOpcodeCounter "jal"
121    Should Be Equal As Numbers                  ${c}  1
122    ${c}=  Execute Command                      sysbus.cpu GetOpcodeCounter "auipc"
123    Should Be Equal As Numbers                  ${c}  1
124    ${c}=  Execute Command                      sysbus.cpu GetOpcodeCounter "addi"
125    Should Be Equal As Numbers                  ${c}  1
126    ${c}=  Execute Command                      sysbus.cpu GetOpcodeCounter "csrrw"
127    Should Be Equal As Numbers                  ${c}  1
128