1*** Variables ***
2${UART}                       sysbus.uart
3${STDOUT}                     sysbus.stdout
4${URI}                        @https://dl.antmicro.com/projects/renode
5
6*** Keywords ***
7Create Machine
8    [Arguments]  ${elf}
9
10    Execute Command          mach create
11    Execute Command          machine LoadPlatformDescription @platforms/boards/A2_CV32E40P.repl
12
13    Execute Command          sysbus LoadELF ${URI}/${elf}
14
15Create Test File
16    [Arguments]              ${file}
17    ${string} =              Set Variable   ${EMPTY}
18    FOR  ${index}  IN RANGE  128
19         ${string}=          Catenate  ${string}  00  ${index}
20    END
21
22    ${bytes}=                Convert To Bytes  ${string}  int
23    Create Binary File       ${file}  ${bytes}
24
25*** Test Cases ***
26Should Print Hello to UART
27    Create Machine           arnold-pulp-hello-s_354412-d0f4d2860104d3bb1d4524c4ee76ef476bbe1d1e
28    Create Terminal Tester   ${UART}
29
30    Start Emulation
31
32    Wait For Line On Uart    Hello !
33
34Should Set GPIO Output to High
35    Create Machine           arnold-pulp-gpio-s_380728-f9f273e2063a3ea7d4f9607cce4d7f12ea10bf63
36    Execute Command          machine LoadPlatformDescriptionFromString "gpio: { 5 -> led@0 }; led: Miscellaneous.LED @ gpio 5"
37
38    Create LED Tester        sysbus.gpio.led
39
40    Start Emulation
41
42    Assert LED State         true  1
43
44Should Print to UART Using a Timer
45    Create Machine            arnold-pulp-timer-s_365004-fc268eecd231afb88a571748c864b6c3ab0bcb5d
46    Create Terminal Tester    ${UART}
47
48    Set Test Variable         ${SLEEP_TIME}                 163
49    Set Test Variable         ${SLEEP_TOLERANCE}            10
50    Set Test Variable         ${REPEATS}                    20
51
52    Start Emulation
53
54    ${l}=               Create List
55    ${MAX_SLEEP_TIME}=  Evaluate  ${SLEEP_TIME} + ${SLEEP_TOLERANCE}
56
57    FOR  ${i}  IN RANGE  0  ${REPEATS}
58         ${r}        Wait For Line On Uart     Entered user handler
59                     Append To List            ${l}  ${r.timestamp}
60    END
61
62    FOR  ${i}  IN RANGE  1  ${REPEATS}
63         ${i1}=  Get From List   ${l}                       ${i - 1}
64         ${i2}=  Get From List   ${l}                       ${i}
65         ${d}=   Evaluate        ${i2} - ${i1}
66                 Should Be True  ${d} >= ${SLEEP_TIME}      Too short sleep detected between entries ${i} and ${i + 1}: expected ${SLEEP_TIME}, got ${d}
67                 Should Be True  ${d} <= ${MAX_SLEEP_TIME}  Too long sleep detected between entires ${i} and ${i + 1}: expected ${SLEEP_TIME}, got ${d}
68    END
69
70Should Echo Characters on UART
71    Create Machine            arnold-pulp-echo-s_387788-cf79547cd654f7ebad125546dd2c98e58b47731e
72    Create Terminal Tester    ${UART}
73
74    Start Emulation
75
76    Wait For Line On Uart     Entered test
77
78    Write Char On Uart        t
79    Wait For Prompt On Uart   t
80    Write Char On Uart        e
81    Wait For Prompt On Uart   e
82    Write Char On Uart        s
83    Wait For Prompt On Uart   s
84    Write Char On Uart        t
85    Wait For Prompt On Uart   t
86
87Should Fail When I2S Not Configured
88    Create Machine            arnold-pulp-i2s_capture-s_566452-62d9e3e48551334dac1ab17af371728e66586c7a
89    Create Log Tester         1
90
91    Start Emulation
92
93    Wait For Log Entry        Starting reception without an input file! Aborting
94
95Should Output I2S Samples From File
96    Create Machine            arnold-pulp-i2s_capture-s_566452-62d9e3e48551334dac1ab17af371728e66586c7a
97
98    ${input_file}=            Allocate Temporary File
99    Create Test File          ${input_file}
100    Execute Command           sysbus.i2s InputFile '${input_file}'
101    Create Terminal Tester    ${STDOUT}
102
103    Start Emulation
104
105    Wait For Line On Uart     Value of sample 0 = 0
106    Wait For Line On Uart     Value of sample 127 = 127
107
108Should Run I2C test
109    Create Machine            arnold-pulp-i2c-s_389340-a622fbd1302bc8a0a52b70f8713bc028f341c902
110    Execute Command           machine LoadPlatformDescriptionFromString "i2c_echo: Mocks.EchoI2CDevice @ i2c0 0x55"
111
112    # There is a problem with the rt_i2c_write() function in the simulation.
113    # It configures the DMA transfer with the first set of commands (CFG, START, WRITE address, RPT, WRITE), but fails to continue with the next one.
114    # The i2c_step1() callback is being called as a result of the interrupt, but it doesn't start the transfer of the actual data.
115    # It looks like the state of the X8 register is wrong - it should contain a base address of the controller, but instead it is 0.
116    # As a result the write that should be directed to I2C's register ends up in the memory.
117    #
118    # The code belows patches the value of the register by setting it to the TxBufferBaseAddress register of the i2c0 device
119    # in the i2c_step1() function (https://github.com/pulp-platform/pulp-rt/blob/master/kernel/riscv/udma-v3.S#L305).
120    #
121    # WARNING: the addresses below are binary-specific and should be adapted after rebuilding the demo
122
123    Execute Command           sysbus.cpu AddHook 0x1c008208 "self.SetRegisterUlong(12, 0x1a102190)"
124    Execute Command           sysbus.cpu AddHook 0x1c00822c "self.SetRegisterUlong(12, 0x1a102190)"
125
126    Create Terminal Tester    ${UART}
127
128    Start Emulation
129
130    Wait For Line On Uart     Entering test
131    Wait For Line On Uart     reading...
132    Wait For Line On Uart      0x0: 0x0
133    Wait For Line On Uart      0x1: 0x0
134    Wait For Line On Uart      0x2: 0x0
135    Wait For Line On Uart      0x3: 0x0
136    Wait For Line On Uart     writing...
137    Wait For Line On Uart     readng again...
138    Wait For Line On Uart      0x0: 0x0
139    Wait For Line On Uart      0x1: 0x1
140    Wait For Line On Uart      0x2: 0x2
141    Wait For Line On Uart      0x3: 0x3
142    Wait For Line On Uart     the end
143
144SPI Should Send Bytes
145    Create Machine           arnold--pulp-rt-examples--spim-send.elf-s_414648-d0100af113d67254d957e26c9ea62c014a753a0c
146    Execute Command          machine LoadPlatformDescriptionFromString "dummySlave: Mocks.DummySPISlave @ spi"
147    Create Log Tester        0.1
148    Execute Command          logLevel 0 spi.dummySlave
149
150    Start Emulation
151
152    Wait For Log Entry       Data received: 0x0
153    Wait For Log Entry       Data received: 0x1
154    Wait For Log Entry       Data received: 0x2
155    Wait For Log Entry       Data received: 0x3
156    Wait For Log Entry       Data received: 0x4
157    Wait For Log Entry       Data received: 0x5
158    Wait For Log Entry       Data received: 0x6
159    Wait For Log Entry       Data received: 0x7
160    Wait For Log Entry       Data received: 0x8
161    Wait For Log Entry       Data received: 0x9
162    Wait For Log Entry       Data received: 0xA
163    Wait For Log Entry       Data received: 0xB
164    Wait For Log Entry       Data received: 0xC
165    Wait For Log Entry       Data received: 0xD
166    Wait For Log Entry       Data received: 0xE
167    Wait For Log Entry       Data received: 0xF
168
169SPI Should Send Bytes Async
170    Create Machine           arnold--spim-async.elf-s_417852-3cf040621fd22ac14bd67be8decfe3a84f7d8d78
171    Execute Command          machine LoadPlatformDescriptionFromString "dummySlave: Mocks.DummySPISlave @ spi"
172    Create Terminal Tester   sysbus.uart
173    Create Log Tester        0.1
174    Execute Command          logLevel 0 spi.dummySlave
175
176    Start Emulation
177
178    Wait For Line On Uart    Executing callback!
179    Wait For Log Entry       Data received: 0x0
180    Wait For Log Entry       Data received: 0x1F
181    Wait For Line On Uart    Executing callback!
182    Wait For Log Entry       Data received: 0x0
183    Wait For Log Entry       Data received: 0x1F
184    Wait For Line On Uart    Executing callback!
185    Wait For Log Entry       Data received: 0x0
186    Wait For Log Entry       Data received: 0x1F
187
188SPI Should Receive Bytes
189    Create Machine           arnold--pulp-rt-examples--spim-receive.elf-s_414528-ba2983aef41f1493d1f6b13c58ea0d7b843bf57a
190    Execute Command          machine LoadPlatformDescriptionFromString "dummySlave: Mocks.DummySPISlave @ spi"
191    Execute Command          logLevel 0 spi.dummySlave
192
193    ${res}=  Execute Command         sysbus ReadDoubleWord 0x1C00A7F8
194    Should Contain                   ${res}      0x00000000
195    ${res}=  Execute Command         sysbus ReadDoubleWord 0x1C00A7FC
196    Should Contain                   ${res}      0x00000000
197    ${res}=  Execute Command         sysbus ReadDoubleWord 0x1C00A800
198    Should Contain                   ${res}      0x00000000
199    ${res}=  Execute Command         sysbus ReadDoubleWord 0x1C00A804
200    Should Contain                   ${res}      0x00000000
201
202    FOR  ${i}  IN RANGE  0  16
203        Execute Command              sysbus.spi.dummySlave EnqueueValue ${i}
204    END
205
206    Start Emulation
207
208    Wait For Log Entry               Data received: 0x0 (idx: 15)
209    ${res}=  Execute Command         sysbus ReadDoubleWord 0x1C00A7F8
210    Should Contain                   ${res}      0x03020100
211    ${res}=  Execute Command         sysbus ReadDoubleWord 0x1C00A7FC
212    Should Contain                   ${res}      0x07060504
213    ${res}=  Execute Command         sysbus ReadDoubleWord 0x1C00A800
214    Should Contain                   ${res}      0x0B0A0908
215    ${res}=  Execute Command         sysbus ReadDoubleWord 0x1C00A804
216    Should Contain                   ${res}      0x0F0E0D0C
217
218Should Read Frames From Camera
219    Create Machine           arnold-pulp-camera-s_391968-f3ac0d1bcaf06ba5811c3e5c333aeac8286c5bdc
220    Execute Command          machine LoadPlatformDescriptionFromString "himax: Sensors.HiMaxHM01B0 @ camera_controller"
221
222    Execute Command          sysbus.camera_controller.himax AddFrame ${URI}/images/person_image_0.jpg-s_3853-7f2125e28423fa117a1079d84785b17c9b70f62d
223
224    Create Terminal Tester   ${UART}
225    Start Emulation
226
227    Wait For Line On Uart    Entering main controller
228    Wait For Line On Uart    Frame 1 captured!
229    Wait For Line On Uart    0xff 0xd8 0xff 0xe0 0x0 0x10 0x4a 0x46
230    Wait For Line On Uart    Frame 2 captured!
231    Wait For Line On Uart    0xff 0xd8 0xff 0xe0 0x0 0x10 0x4a 0x46
232    Wait For Line On Uart    Frame 3 captured!
233    Wait For Line On Uart    0xff 0xd8 0xff 0xe0 0x0 0x10 0x4a 0x46
234    Wait For Line On Uart    Frame 4 captured!
235    Wait For Line On Uart    0xff 0xd8 0xff 0xe0 0x0 0x10 0x4a 0x46
236    Wait For Line On Uart    Frame 5 captured!
237    Wait For Line On Uart    0xff 0xd8 0xff 0xe0 0x0 0x10 0x4a 0x46
238    Wait For Line On Uart    Frame 6 captured!
239    Wait For Line On Uart    0xff 0xd8 0xff 0xe0 0x0 0x10 0x4a 0x46
240    Wait For Line On Uart    Frame 7 captured!
241    Wait For Line On Uart    0xff 0xd8 0xff 0xe0 0x0 0x10 0x4a 0x46
242    Wait For Line On Uart    Frame 8 captured!
243    Wait For Line On Uart    0xff 0xd8 0xff 0xe0 0x0 0x10 0x4a 0x46
244    Wait For Line On Uart    Frame 9 captured!
245    Wait For Line On Uart    0xff 0xd8 0xff 0xe0 0x0 0x10 0x4a 0x46
246    Wait For Line On Uart    Frame 10 captured!
247    Wait For Line On Uart    0xff 0xd8 0xff 0xe0 0x0 0x10 0x4a 0x46
248    Wait For Line On Uart    Test success
249