1*** Variables ***
2${URI}                        @https://dl.antmicro.com/projects/renode
3
4*** Keywords ***
5Create Machine
6    Execute Command           using sysbus
7    Execute Command           mach create "Ambiq Apollo4"
8
9    Execute Command           machine LoadPlatformDescription @platforms/cpus/ambiq-apollo4.repl
10    Create Terminal Tester    sysbus.uart2  10
11
12Load Example
13    [Arguments]    ${axf_filename}
14    Execute Command           sysbus LoadELF ${URI}/${axf_filename}
15
16Start Example
17    [Arguments]    ${axf_filename}
18    Load Example              ${axf_filename}
19    Start Emulation
20
21Load Bytes To Memory
22    [Arguments]    ${address}    @{bytes}
23    FOR    ${byte}    IN    @{bytes}
24        Execute Command       sysbus WriteByte ${address} ${byte}
25        ${address} =          Evaluate    ${address} + 1
26    END
27
28*** Test Cases ***
29Should Successfully Run hello_world_uart Example
30    Create Machine
31    Start Example             hello_world_uart.axf-s_307536-899c2682fa35d4bf27992bba5a0b5845ae331ba3
32
33    Wait For Line On Uart     Hello World!
34    Wait For Line On Uart     Vendor Name:
35
36Should Successfully Run stimer Example
37    Create Machine
38    Start Example             stimer.axf-s_252512-bd594169c2dce5d6771bb426a479fc622b1c6182
39
40    Wait For Line On Uart     SUCCESS!  pauseEmulation=true
41
42    Execute Command           emulation RunFor '00:00:02.5000'
43    # A single "SUCCESS!" line should be printed each second (approximately) so check if there are two such lines.
44    Wait For Line On Uart     SUCCESS!  0
45    Wait For Line On Uart     SUCCESS!  0
46
47    # There shouldn't be any more SUCCESS! lines on UART.
48    Run Keyword And Expect Error    *Line containing >>SUCCESS!<< event: failure*    Wait For Line On Uart  SUCCESS!  0
49
50Should Successfully Run rtc_print Example
51    Create Machine
52    Start Example             rtc_print.axf-s_259852-abb4ec53fd71107857a6b0a60994e81cda77d4d5
53
54    # A fixed time set in the test instead of the compilation time.
55    Wait For Line On Uart     It is now 9 : 00 : 00.00 Tuesday April 26, 2022    includeUnfinishedLine=true
56
57    # Check RTC setting and progress.
58    Execute Command           pause
59    Execute Command           rtc SetDateTime 2022 12 31 23 59 59 15
60    ${res}=  Execute Command  rtc PrintPreciseCurrentDateTime
61    Should Contain            ${res}  2022-12-31T23:59:59.1500000
62    ${res}=  Execute Command  emulation RunFor '00:00:00.12'; rtc PrintPreciseCurrentDateTime
63    Should Contain            ${res}  2022-12-31T23:59:59.2700000
64
65    # Set the alarm to every second.
66    Execute Command           rtc WriteDoubleWord 0x0 0xE
67    # Alarm whenever millisecond value equals 310.
68    Execute Command           rtc WriteDoubleWord 0x30 0x31
69    # Make sure alarm is properly set.
70    ${res}=  Execute Command  rtc PrintNextAlarmDateTime
71    Should Contain            ${res}  2022-12-31T23:59:59.3100000
72    # Make sure the interrupt status is off.
73    ${res}=  Execute Command  rtc ReadDoubleWord 0x204
74    Should Contain            ${res}  0x00000000
75
76    # Progress the timer and stop before the event
77    ${res}=  Execute Command  emulation RunFor '00:00:00.03'; rtc PrintPreciseCurrentDateTime
78    Should Contain            ${res}  2022-12-31T23:59:59.3000000
79    # Check the interrupt status.
80    ${res}=  Execute Command  rtc ReadDoubleWord 0x204
81    Should Contain            ${res}  0x00000000
82
83    # Progress the timer - this time the event should trigger
84    ${res}=  Execute Command  emulation RunFor '00:00:00.01'; rtc PrintPreciseCurrentDateTime
85    Should Contain            ${res}  2022-12-31T23:59:59.3100000
86    # Check the interrupt status.
87    ${res}=  Execute Command  rtc ReadDoubleWord 0x204
88    Should Contain            ${res}  0x00000001
89    # Make sure the next alarm is properly scheduled.
90    ${res}=  Execute Command  rtc PrintNextAlarmDateTime
91    Should Contain            ${res}  2023-01-01T00:00:00.3100000
92    # Clear the interrupt status and check if it's reset.
93    ${res}=  Execute Command  rtc WriteDoubleWord 0x208 0x1; rtc ReadDoubleWord 0x204
94    Should Contain            ${res}  0x00000000
95    # Progress the timer to the next year.
96    ${res}=  Execute Command  emulation RunFor '00:00:01.00'; rtc PrintPreciseCurrentDateTime
97    Should Contain            ${res}  2023-01-01T00:00:00.3100000
98    # Check the interrupt status.
99    ${res}=  Execute Command  rtc ReadDoubleWord 0x204
100    Should Contain            ${res}  0x00000001
101
102    # Set the alarm interval to every week.
103    Execute Command           rtc WriteDoubleWord 0x0 0x6
104    # The alarm will be set to the next Sunday at 00:00:00.31 because only the alarm's milliseconds are set (Sunday is weekday=0).
105    ${res}=  Execute Command  rtc PrintNextAlarmDateTime
106    Should Contain            ${res}  2023-01-08T00:00:00.3100000
107    # Set the alarm's weekday to Friday.
108    Execute Command           rtc WriteDoubleWord 0x34 0x50000
109    # Trigger the alarm exactly at 12:34:56.78.
110    Execute Command           rtc WriteDoubleWord 0x30 0x12345678
111    # Check if the alarm is set properly.
112    ${res}=  Execute Command  rtc PrintNextAlarmDateTime
113    Should Contain            ${res}  2023-01-06T12:34:56.7800000
114
115    # Test if the alarm is properly adjusted after setting new date.
116    Execute Command           rtc SetDateTime 2022 05 06 20 48 20 15
117    ${res}=  Execute Command  rtc PrintNextAlarmDateTime
118    Should Contain            ${res}  2022-05-13T12:34:56.7800000
119
120    # Test setting the alarm to once a year on 23rd of February.
121    Execute Command           rtc WriteDoubleWord 0x34 0x0223
122    Execute Command           rtc WriteDoubleWord 0x0 0x2
123    ${res}=  Execute Command  rtc PrintNextAlarmDateTime
124    Should Contain            ${res}  2023-02-23T12:34:56.7800000
125
126    # Test disabling the alarm.
127    Execute Command           rtc WriteDoubleWord 0x0 0x0
128    ${res}=  Execute Command  rtc PrintNextAlarmDateTime
129    Should Contain            ${res}  Alarm not set.
130
131Should Successfully Run ios_fifo_host Example
132    Create Machine
133    Execute Command           machine LoadPlatformDescriptionFromString "dummySpi: Mocks.DummySPISlave @ iom1 0"
134    Create Log Tester         10
135    Execute Command           logLevel 0 iom1.dummySpi
136    Start Example             ios_fifo_host.axf-s_253032-865941e8c49057c68d2d866e9c9308febe684644
137
138    Wait For Line On Uart     IOS Test Host: Waiting for at least 10000 bytes from the slave.    includeUnfinishedLine=true
139
140    # Currently only writes to the slave peripheral are tested.
141    Wait For Log Entry        iom1.dummySpi: Data received: 0xF8
142    Wait For Log Entry        iom1.dummySpi: Data received: 0x1
143    Wait For Log Entry        iom1.dummySpi: Data received: 0x80
144    Wait For Log Entry        iom1.dummySpi: Data received: 0x0
145
146Should Successfully Run binary_counter Example
147    Create Machine
148    Execute Command           machine LoadPlatformDescriptionFromString 'gpio: { 30 -> led30@0; 90 -> led90@0; 91 -> led91@0 }; led30: Miscellaneous.LED @ gpio 30; led90: Miscellaneous.LED @ gpio 90; led91: Miscellaneous.LED @ gpio 91'
149    Load Example              binary_counter.axf-s_264304-49508d8e17aeaaa88845426e007dde2ce4416892
150
151    ${led30_tester}=          Create LED Tester  sysbus.gpio.led30  defaultTimeout=0
152    ${led90_tester}=          Create LED Tester  sysbus.gpio.led90  defaultTimeout=0
153    ${led91_tester}=          Create LED Tester  sysbus.gpio.led91  defaultTimeout=0
154
155    Execute Command           emulation RunFor "1"
156
157    Assert LED State          false  testerId=${led30_tester}
158    Assert LED State          false  testerId=${led90_tester}
159    Assert LED State          false  testerId=${led91_tester}
160
161    # this simulates an IRQ from the timer
162    Execute Command           nvic OnGPIO 14 true; nvic OnGPIO 14 false
163
164    Execute Command           emulation RunFor "1"
165
166    Assert LED State          true  testerId=${led30_tester}
167    Assert LED State          true  testerId=${led90_tester}
168    Assert LED State          true  testerId=${led91_tester}
169
170Test Calling Unimplemented Bootrom Function
171    Create Machine
172    Create Log Tester         1
173
174    ${FUNCTION_ADDRESS} =     Set Variable  0x08000098
175    ${FUNCTION_NAME} =        Set Variable  Recovery
176
177    Execute Command           cpu PC ${FUNCTION_ADDRESS}
178    Start Emulation
179    Wait For Log Entry        bootrom_logger: Unimplemented BOOTROM function called: ${FUNCTION_NAME} (${FUNCTION_ADDRESS})
180
181Should Successfully Run adc_measure Example
182    Create Machine
183    Start Example             adc_measure.axf-s_258564-3c473c4e7c59b73cdbf00b22b633e3301923ed61
184
185    Wait For Line On Uart     ADC correction offset = 0.0
186    Wait For Line On Uart     ADC correction gain * = 0.0  treatAsRegex=true  # Regex handles the output's triple space.
187
188    # Make sure slot channels are correct
189    Wait For Line On Uart     ADC SLOT0 = 0x.....1..    treatAsRegex=true
190    Wait For Line On Uart     ADC SLOT1 = 0x.....2..    treatAsRegex=true
191    Wait For Line On Uart     ADC SLOT2 = 0x.....4..    treatAsRegex=true
192    Wait For Line On Uart     ADC SLOT3 = 0x.....6..    treatAsRegex=true
193
194    # A single software trigger is sent by the example so a single read from each slot should be printed.
195    # These values are the example defaults set in the REPL file.
196    Wait For Line On Uart     ADC#0 sample read=681, measured voltage=197.0 mV
197    Wait For Line On Uart     ADC#1 sample read=1363, measured voltage=395.0 mV
198    Wait For Line On Uart     ADC#2 sample read=2729, measured voltage=792.0 mV
199    Wait For Line On Uart     ADC#3 sample read=4092, measured voltage=1188.0 mV
200
201    # Change some values and manually trigger a scan.
202    Execute Command           adc Channel2Data 0x00040
203    Execute Command           adc Channel4Data 0x35E00
204    Execute Command           adc ScanAllSlots
205    Wait For Line On Uart     ADC#0 sample read=681, measured voltage=197.0 mV
206    Wait For Line On Uart     ADC#1 sample read=0, measured voltage=0.0 mV
207    Wait For Line On Uart     ADC#2 sample read=3445, measured voltage=1000.0 mV
208    Wait For Line On Uart     ADC#3 sample read=4092, measured voltage=1188.0 mV
209
210Test Calculating CRC32 Value
211    Create Machine
212    Create Log Tester         1
213    Execute Command           logLevel -1 security
214
215    # The data and its CRC come from SDK's 'em9304_patches.c'.
216    ${address} =              Set Variable  0x0
217    ${crc} =                  Set Variable  0xF9FC5EF5
218    Load Bytes To Memory      ${address}
219    ...                           0x33  0x39  0x6D  0x65  0x20  0x00  0x00  0x00
220    ...                           0x01  0x0B  0x18  0x14  0x11  0x0C  0x02  0x00
221    ...                           0x75  0xDE  0xC7  0x98  0x81  0x00  0x00  0x00
222    ...                           0x01  0x00  0x00  0x00  0x01  0x00  0x00  0x00
223    ${length} =               Set Variable  0x20
224
225    # Check if the SDK's 'am_hal_crc32' steps work.
226    Execute Command           security WriteDoubleWord 0x30 0xFFFFFFFF  # Seed initial value.
227    Execute Command           security WriteDoubleWord 0x10 ${address}  # Set source address.
228    Execute Command           security WriteDoubleWord 0x20 ${length}   # Set length.
229    Execute Command           security WriteDoubleWord 0x0 0x0          # Set FUNCTION field to CRC32.
230
231    # Start the calculation by setting the ENABLE bit and wait for the result log.
232    Execute Command           security WriteDoubleWord 0x0 0x1
233    Wait For Log Entry        security: CRC32 calculation result: ${crc}
234
235    # Check if the ENABLE bit has been cleared.
236    ${control_register} =     Execute Command  security ReadDoubleWord 0x0
237    Should Contain            ${control_register}  0x0
238
239    # Check the result register's value.
240    ${result_register} =      Execute Command  security ReadDoubleWord 0x30
241    Should Contain            ${result_register}  ${crc}
242