1*** Variables ***
2${csr_script}=  SEPARATOR=
3...  if request.isRead:                                                  ${\n}${SPACE}
4...      cpu.DebugLog('CSR read!')                                       ${\n}
5...  elif request.isWrite:                                               ${\n}${SPACE}
6...      cpu.DebugLog('CSR written: {}!'.format(hex(request.value)))
7
8${xadd}=  SEPARATOR=
9...  src_reg_a = (instruction >> 3) & 0xF                                                            ${\n}
10...  src_reg_b = (instruction >> 12) & 0xF                                                           ${\n}
11...  res = cpu.GetRegister(src_reg_a).RawValue + cpu.GetRegister(src_reg_b).RawValue                 ${\n}
12...  state['res'] = res
13
14${xmv}=  SEPARATOR=
15...  dst_reg = (instruction >> 3) & 0xF                                  ${\n}
16...  cpu.SetRegister(dst_reg, state['res'])
17
18*** Keywords ***
19Create Machine
20    Execute Command                             mach create
21    Execute Command                             machine LoadPlatformDescriptionFromString "cpu: CPU.RiscV64 @ sysbus { cpuType: \\"rv64imac_zicsr\\"; timeProvider: empty }"
22    Execute Command                             machine LoadPlatformDescriptionFromString "mem: Memory.MappedMemory @ sysbus 0x0 { size: 0x1000 }"
23
24    Execute Command                             sysbus.cpu PC 0x0
25
26Load Code To Memory
27    # li x1, 0x147
28    Execute Command                             sysbus WriteDoubleWord 0x0 0x14700093
29
30    # csrw 0xf0d, x1
31    Execute Command                             sysbus WriteDoubleWord 0x4 0xf0d09073
32
33    # csrr x2, 0xf0d
34    Execute Command                             sysbus WriteDoubleWord 0x8 0xf0d02173
35
36*** Test Cases ***
37Should Install Custom 16-bit Instruction
38    Create Machine
39    Create Log Tester                           1
40
41    Execute Command                             sysbus.cpu InstallCustomInstructionHandlerFromString "1011001110001110" "cpu.DebugLog('custom instruction executed!')"
42    Execute Command                             logLevel 0
43    Execute Command                             sysbus WriteWord 0x0 0xb38e
44
45    Execute Command                             log "--- start ---"
46    Execute Command                             sysbus.cpu Step
47    Execute Command                             log "--- stop ---"
48
49    Wait For Log Entry                          --- start ---
50    Wait For Log Entry                          custom instruction executed!
51    Wait For Log Entry                          --- stop ---
52
53Should Not Install Custom Instructions With Invalid Length Encoding
54    Create Machine
55
56    # 16 bit
57    Run Keyword And Expect Error                *Pattern 0xB38F is invalid for 16 bits long instruction. Expected instruction in format: xxxxxxxxxxxxxxAA, AA != 11*
58    ...    Execute Command                      sysbus.cpu InstallCustomInstructionHandlerFromString "1011001110001111" "cpu.DebugLog('custom instruction executed!')"
59
60    # 32 bit
61    Run Keyword And Expect Error                *Pattern 0xB38F0F82 is invalid for 32 bits long instruction. Expected instruction in format: xxxxxxxxxxxxxxxxxxxxxxxxxxxBBB11, BBB != 111*
62    ...    Execute Command                      sysbus.cpu InstallCustomInstructionHandlerFromString "10110011100011110000111110000010" "cpu.DebugLog('custom instruction executed!')"
63
64    Run Keyword And Expect Error                *Pattern 0xB38F0F9F is invalid for 32 bits long instruction. Expected instruction in format: xxxxxxxxxxxxxxxxxxxxxxxxxxxBBB11, BBB != 111*
65    ...    Execute Command                      sysbus.cpu InstallCustomInstructionHandlerFromString "10110011100011110000111110011111" "cpu.DebugLog('custom instruction executed!')"
66
67    Run Keyword And Expect Error                *Pattern 0xB38F0F9E is invalid for 32 bits long instruction. Expected instruction in format: xxxxxxxxxxxxxxxxxxxxxxxxxxxBBB11, BBB != 111*
68    ...    Execute Command                      sysbus.cpu InstallCustomInstructionHandlerFromString "10110011100011110000111110011110" "cpu.DebugLog('custom instruction executed!')"
69
70    # 64 bit
71    Run Keyword And Expect Error                *Pattern 0xB38F0F82B38F0FBB is invalid for 64 bits long instruction. Expected instruction in format: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0111111*
72    ...    Execute Command                      sysbus.cpu InstallCustomInstructionHandlerFromString "1011001110001111000011111000001010110011100011110000111110111011" "cpu.DebugLog('custom instruction executed!')"
73
74    Run Keyword And Expect Error                *Pattern 0xB38F0F82B38F0FFF is invalid for 64 bits long instruction. Expected instruction in format: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0111111*
75    ...    Execute Command                      sysbus.cpu InstallCustomInstructionHandlerFromString "1011001110001111000011111000001010110011100011110000111111111111" "cpu.DebugLog('custom instruction executed!')"
76
77Should Install Custom 32-bit Instruction
78    Create Machine
79    Create Log Tester                           1
80
81    Execute Command                             sysbus.cpu InstallCustomInstructionHandlerFromString "10110011100011110000111110000011" "cpu.DebugLog('custom instruction executed!')"
82    Execute Command                             logLevel 0
83    Execute Command                             sysbus WriteDoubleWord 0x0 0xb38f0f83
84
85    Execute Command                             log "--- start ---"
86    Execute Command                             sysbus.cpu Step
87    Execute Command                             log "--- stop ---"
88
89    Wait For Log Entry                          --- start ---
90    Wait For Log Entry                          custom instruction executed!
91    Wait For Log Entry                          --- stop ---
92
93Should Install Custom 64-bit Instruction
94    Create Machine
95    Create Log Tester                           1
96
97    Execute Command                             sysbus.cpu InstallCustomInstructionHandlerFromString "1011001110001111000011111000001010110011100011110000111110111111" "cpu.DebugLog('custom instruction executed!')"
98    Execute Command                             logLevel 0
99    Execute Command                             sysbus WriteDoubleWord 0x0 0xb38f0fbf
100    Execute Command                             sysbus WriteDoubleWord 0x4 0xb38f0f82
101
102    Execute Command                             log "--- start ---"
103    Execute Command                             sysbus.cpu Step
104    Execute Command                             log "--- stop ---"
105
106    Wait For Log Entry                          --- start ---
107    Wait For Log Entry                          custom instruction executed!
108    Wait For Log Entry                          --- stop ---
109
110Should Override An Existing 32-bit Instruction
111    Create Machine
112    Create Log Tester                           1
113
114    # normally this instruction means "li x1, 0x147"
115    # but we override it with a custom implementation
116    Execute Command                             sysbus.cpu InstallCustomInstructionHandlerFromString "00010100011100000000000010010011" "cpu.DebugLog('custom instruction executed!')"
117    Execute Command                             logLevel 0
118    Execute Command                             sysbus WriteDoubleWord 0x0 0x14700093
119
120    Register Should Be Equal                    1  0x0
121
122    Execute Command                             log "--- start ---"
123    Execute Command                             sysbus.cpu Step
124    Execute Command                             log "--- stop ---"
125
126    Register Should Be Equal                    1  0x0
127
128    Wait For Log Entry                          --- start ---
129    Wait For Log Entry                          custom instruction executed!
130    Wait For Log Entry                          --- stop ---
131
132Should Install Custom 32-bit Instructions Sharing State
133    Create Machine
134    Create Log Tester                           1
135
136    Execute Command                             sysbus.cpu InstallCustomInstructionHandlerFromString "1011001110001111bbbb11111aaaa011" "${xadd}"
137    Execute Command                             sysbus.cpu InstallCustomInstructionHandlerFromString "0011001110001111000011111aaaa011" "${xmv}"
138
139    # li x1, 0x147
140    Execute Command                             sysbus WriteDoubleWord 0x0 0x14700093
141    # li x2, 0x19
142    Execute Command                             sysbus WriteDoubleWord 0x4 0x01900113
143
144    # add values of x1 and x2 and store in the local register
145    Execute Command                             sysbus WriteDoubleWord 0x8 0xB38F2F8B
146    # move the local register to x3
147    Execute Command                             sysbus WriteDoubleWord 0xC 0x338F0F9B
148
149    Register Should Be Equal                    1  0x0
150    Register Should Be Equal                    2  0x0
151    Register Should Be Equal                    3  0x0
152
153    Execute Command                             sysbus.cpu Step 2
154
155    Register Should Be Equal                    1  0x147
156    Register Should Be Equal                    2  0x19
157    Register Should Be Equal                    3  0x0
158
159    Execute Command                             sysbus.cpu Step
160    Register Should Be Equal                    1  0x147
161    Register Should Be Equal                    2  0x19
162    Register Should Be Equal                    3  0x0
163
164    Execute Command                             sysbus.cpu Step
165    Register Should Be Equal                    1  0x147
166    Register Should Be Equal                    2  0x19
167    Register Should Be Equal                    3  0x160
168
169Should Register Simple Custom CSR
170    Create Machine
171
172    Execute Command                             sysbus.cpu CSRValidation 0
173    Execute Command                             sysbus.cpu RegisterCustomCSR "test csr" 0xf0d 3
174
175    Load Code To Memory
176
177    Register Should Be Equal                    1  0x0
178    Register Should Be Equal                    2  0x0
179
180    Execute Command                             sysbus.cpu Step 3
181
182    PC Should Be Equal                          0xc
183
184    Register Should Be Equal                    1  0x147
185    Register Should Be Equal                    2  0x147
186
187Should Register Custom CSR
188    Create Machine
189    Create Log Tester                           1
190
191    Execute Command                             sysbus.cpu CSRValidation 0
192    Execute Command                             sysbus.cpu RegisterCSRHandlerFromString 0xf0d "${csr_script}"
193    Execute Command                             logLevel 0
194
195    Load Code To Memory
196
197    Execute Command                             sysbus.cpu Step 3
198
199    ${pc}=  Execute Command                     sysbus.cpu PC
200    Should Be Equal                             0xc  ${pc.replace('\n', '')}
201
202    Register Should Be Equal                    1  0x147
203    Register Should Be Equal                    2  0x147
204
205    Wait For Log Entry                          CSR written: 0x147L!
206    Wait For Log Entry                          CSR read!
207