1using sysbus
2logLevel 0
3
4# This file requires variables:
5# example_elf - path to the elf with test code
6# example_sig - path to the file with example signature (has to be local absolute path)
7
8include @tests/peripherals/CLIC/CLIC-test-platform.repl
9
10machine UserState $example_sig
11echo $example_sig
12
13set end_hookscript
14"""
15start_addr = cpu.Bus.GetSymbolAddress("begin_signature")
16end_addr = cpu.Bus.GetSymbolAddress("end_signature")
17sig_path = machine.UserState
18
19generated_signatures = []
20
21curr_addr = start_addr
22while curr_addr < end_addr:
23    s = ""
24    for j in range(4, 0, -1):
25        s += "%02x" % cpu.Bus.ReadByte(curr_addr + j - 1)
26    curr_addr += 4
27    generated_signatures.append(s)
28
29reference_signatures = []
30cpu.Log(LogLevel.Error, "Sig path: ({0})", sig_path)
31with open(sig_path, "r") as f:
32    reference_signatures = f.readlines()
33
34if len(generated_signatures) != len(reference_signatures):
35    cpu.Log(LogLevel.Error, "Different number of generated ({0}) and reference"
36        "({1}) signatures", len(generated_signatures), len(reference_signatures))
37
38all_equal = True
39for i, sig in enumerate(generated_signatures):
40    ref_sig = reference_signatures[i].rstrip()
41    if sig != ref_sig:
42        cpu.Log(LogLevel.Error, "Wrong generated signature #{0}: {1} different than reference signature {2}".format(i, sig, ref_sig))
43        all_equal = False
44    else:
45        cpu.Log(LogLevel.Info, "Signature {0} OK: {1} == {2}".format(i, sig, ref_sig))
46
47if all_equal:
48    cpu.Log(LogLevel.Info, "All signatures correct")
49
50machine.UserState = ''
51"""
52
53set sigwrite_hookscript
54"""
55if machine.UserState != '':
56    sig_path = machine.UserState
57    start_addr = sysbus.GetSymbolAddress("begin_signature")
58    signature_idx = offset/4
59
60    reference_signatures = []
61    with open(sig_path, "r") as f:
62        reference_signatures = f.readlines()
63
64    value = int(value)
65    ref_sig = int(reference_signatures[signature_idx].rstrip(), 16)
66    if value == ref_sig:
67        machine.Log(LogLevel.Info, "Signature OK: 0x{:08X} == 0x{:08X}".format(value, ref_sig))
68    else:
69        machine.Log(LogLevel.Warning, "Writing signature #{}: 0x{:08X} different than reference signature 0x{:08X} at address {:08X}".format(signature_idx, value, ref_sig, start_addr + offset))
70"""
71
72sysbus LoadELF $example_elf
73
74cpu AddHook `sysbus GetSymbolAddress "rvtest_code_end"` $end_hookscript
75#sysbus SetHookBeforePeripheralWrite signature $sigwrite_hookscript
76
77sysbus LogPeripheralAccess signature
78
79# The Sail model treats WFI as NOP, see https://github.com/riscv/sail-riscv/blob/ba35af52e8ee57b7b30772490e9e35d537c769d9/model/riscv_platform.sail#L447
80cpu WfiAsNop true
81