1*** Variables ***
2${starting_pc}                  0x1000
3
4*** Keywords ***
5Create Machine
6    Execute Command             using sysbus
7    Execute Command             mach create
8    Execute Command             machine LoadPlatformDescriptionFromString 'clint: IRQControllers.CoreLevelInterruptor @ sysbus 0x02000000 { frequency: 1000000 }'
9    Execute Command             machine LoadPlatformDescriptionFromString 'cpu: CPU.RiscV32 @ sysbus { cpuType: "rv32ic"; timeProvider: clint }'
10    Execute Command             machine LoadPlatformDescriptionFromString 'clint: { [0,1] -> cpu@[3,7] }'
11    Execute Command             machine LoadPlatformDescriptionFromString 'mem: Memory.MappedMemory @ sysbus 0x1000 { size: 0x1000000 }'
12
13    Execute Command             cpu PerformanceInMips 1
14    Execute Command             cpu PC ${starting_pc}
15    Execute Command             cpu SetRegister 1 0x02004000  # address of MTimeCmpHart0Lo register
16    Execute Command             cpu SetRegister 3 0x02004004  # address of MTimeCmpHart0Hi register
17
18    Execute Command             cpu SetHookAtBlockEnd "self.DebugLog('block ended: ' + 'PC '+ str(self.PC))"
19    Execute Command             cpu SetHookAtBlockBegin "self.DebugLog('block started: ' + 'PC '+ str(self.PC))"
20
21    # setting compare value surrounded by nop instructions
22    Execute Command             sysbus WriteDoubleWord 0x1000 0x00000013 # nop
23    Execute Command             sysbus WriteDoubleWord 0x1004 0x00000013 # nop
24    # set Compare to the value that is in x2 register
25    Execute Command             sysbus WriteDoubleWord 0x1008 0x0020A023 # sw x2, 0(x1)
26    Execute Command             sysbus WriteDoubleWord 0x100C 0x0001A023 # sw x0, 0(x3)
27    Execute Command             sysbus WriteDoubleWord 0x1010 0x00000013 # nop
28    Execute Command             sysbus WriteDoubleWord 0x1014 0x00000013 # nop
29    Execute Command             sysbus WriteDoubleWord 0x1018 0x00000013 # nop
30    Execute Command             sysbus WriteDoubleWord 0x101C 0x00000013 # nop
31    Execute Command             sysbus WriteDoubleWord 0x1020 0x00000013 # nop
32    Execute Command             sysbus WriteDoubleWord 0x1024 0x00000013 # nop
33    Execute Command             sysbus WriteDoubleWord 0x1028 0x00000013 # nop
34    Execute Command             sysbus WriteDoubleWord 0x102C 0x00000013 # nop
35
36    Create Log Tester           0.01  defaultPauseEmulation=true
37    Execute Command             logLevel -1
38
39*** Test Cases ***
40Should Tick Between Chained Blocks
41    [Tags]                      instructions_counting
42    Create Machine
43    Execute Command             cpu MaximumBlockSize 1
44    Execute Command             cpu SetRegister 2 5  # new Compare value = 5
45
46    Execute Command             emulation RunFor "0.000012"
47    Wait For Log Entry          block ended: PC 0x1014  timeout=0
48    Wait For Log Entry          IRQ  timeout=0
49    Wait For Log Entry          block started: PC 0x1014  timeout=0
50
51Should Tick In The Same Block
52    [Tags]                      instructions_counting
53    Create Machine
54    Execute Command             cpu MaximumBlockSize 8
55    Execute Command             cpu SetRegister 2 6  # new Compare value = 6
56
57    Execute Command             emulation RunFor "0.000008"
58    Wait For Log Entry          block started: PC 0x1000  timeout=0
59    Should Not Be In Log        block ended: PC 0x100  timeout=0
60    Should Not Be In Log        block ended: PC 0x101  timeout=0
61    Wait For Log Entry          block ended: PC 0x1020  timeout=0
62    Wait For Log Entry          IRQ  timeout=0
63
64Should Tick At Exact Time
65    [Tags]                      instructions_counting
66    Create Machine
67    Execute Command             cpu MaximumBlockSize 8
68    Execute Command             cpu SetRegister 2 6  # new Compare value = 6
69
70    Execute Command             emulation RunFor "0.000005"
71    Should Not Be In Log        IRQ  timeout=0
72    Execute Command             emulation RunFor "0.000001"
73    Wait For Log Entry          IRQ  timeout=0
74