diff --git a/README.md b/README.md index e60e73b..417f77c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,161 @@ # Processor CI Tests -[![Pylint](https://github.com/LSC-Unicamp/processor-ci-tests/actions/workflows/pylint.yml/badge.svg)](https://github.com/LSC-Unicamp/processor-ci-tests/actions/workflows/pylint.yml) \ No newline at end of file +[![Pylint](https://github.com/LSC-Unicamp/processor-ci-tests/actions/workflows/pylint.yml/badge.svg)](https://github.com/LSC-Unicamp/processor-ci-tests/actions/workflows/pylint.yml) + +Standalone home for the Processor CI RISC-V verification assets: ISA tests, reference memories, benchmark build support, and the serial test runner used to validate a processor core. + +## What this repo contains + +- `riscv_test_runner/`: Python package for the test runner +- `riscv_test_runner/core/`: runner, `-march` parsing, and small data helpers +- `main.py`: stable compatibility wrapper around the package CLI +- `tests/`: RISC-V ISA test corpus grouped by architecture extension and level +- `benchmarks/`: bare-metal benchmark sources, ports, linker scripts, and build helpers +- `third_party_repositories/`: imported or converted upstream test material +- `config.json`: example test directory configuration consumed by the runner +- `docs/`: repository layout and maintenance notes +- `requirements.txt`: Python dependencies for the runner and CI checks + +## Quick start + +1. Create a Python virtual environment. +2. Install dependencies: + +```bash +pip install -r requirements.txt +``` + +3. Make sure the Processor CI communication package is available next to this repository or on `PYTHONPATH`. + +4. Run the test runner: + +```bash +python -m riscv_test_runner.cli \ + --port /dev/ttyUSB0 \ + --config config.json \ + --march rv32im_zicsr \ + --key 0x4e455859 +``` + +Or use the compatibility wrapper: + +```bash +python main.py \ + --port /dev/ttyUSB0 \ + --config config.json \ + --march rv32im_zicsr \ + --key 0x4e455859 +``` + +Both entrypoints use the same parser and behavior. + +## Common usage + +- Show CLI help: + +```bash +python -m riscv_test_runner.cli --help +``` + +- Run only the tests selected by an architecture string: + +```bash +python -m riscv_test_runner.cli \ + -p /dev/ttyUSB0 \ + -c config.json \ + -m rv32im +``` + +- Run with a longer serial timeout and core execution timeout: + +```bash +python -m riscv_test_runner.cli \ + -p /dev/ttyUSB0 \ + -c config.json \ + -m rv32im_zicsr \ + -s 10 \ + -t 5000 +``` + +- Run against a core with separate instruction and data memories: + +```bash +python -m riscv_test_runner.cli \ + -p /dev/ttyUSB0 \ + -c config.json \ + -m rv32im \ + --core-use-two-memories +``` + +- Request extra benchmark-style tests listed in `config.json`: + +```bash +python -m riscv_test_runner.cli \ + -p /dev/ttyUSB0 \ + -c config.json \ + -m rv32im \ + -e coremark +``` + +## Test Layout + +The ISA tests live under `tests///`. + +```text +tests/RV32I/basic/src/ assembly source files +tests/RV32I/basic/memory/ compiled instruction memory hex files +tests/RV32I/basic/reference/ expected result hex files +``` + +Architectures are named like `RV32I`, `RV32M`, `RV64I`, or `RV32Zicsr`. Test levels are: + +- `basic`: currently executed by the Python runner +- `advanced`: broader instruction and behavior coverage +- `invalid`: invalid instruction or exception-oriented cases + +`config.json` maps each architecture directory to its path and breakpoint addresses. The current sample uses `/eda/processor_ci_tests/...` paths for CI/container use; adjust those paths for local runs if needed. + +## Benchmarks + +`benchmarks/` keeps benchmark source and porting support separate from the precompiled ISA test corpus. + +- `benchmarks/coremark/`: CoreMark source tree +- `benchmarks/port/`: bare-metal boot and platform hooks +- `benchmarks/common/`: minimal C library support +- `benchmarks/target/`: target-specific library and Makefile +- `benchmarks/compile_coremark.sh`: builds CoreMark hex output with `riscv32-unknown-elf-*` +- `benchmarks/compile_common.sh`: builds the minimal common library + +Generated benchmark build outputs are written under `benchmarks/build/`. + +## CLI Reference + +```text +usage: python -m riscv_test_runner.cli [-h] [-b BAUDRATE] -p PORT + [-s SERIAL_TIMEOUT] -c CONFIG + [-m MARCH] [-e EXTRA_TESTS] [-k KEY] + [-t TIMEOUT] [-ctm] +``` + +- `-p`, `--port`: serial port used to communicate with the FPGA board. Required. +- `-c`, `--config`: JSON test configuration. Required. +- `-b`, `--baudrate`: serial baudrate. Defaults to `115200`. +- `-s`, `--serial-timeout`: serial timeout in seconds. Defaults to `5`. +- `-m`, `--march`: RISC-V architecture string used to select test directories. Defaults to `rv32i`. +- `-e`, `--extra-tests`: comma-separated extra tests from `config.json`. +- `-k`, `--key`: hex sync key expected from the board. +- `-t`, `--timeout`: core execution timeout in clock cycles. Defaults to `1000`. +- `-ctm`, `--core-use-two-memories`: use separate instruction and data memories. + +## Developer Notes + +- `riscv_test_runner.cli.build_parser()` exposes the CLI parser for tests. +- `riscv_test_runner.cli.main(argv=None)` supports argument injection for wrappers and tests. +- `riscv_test_runner.core.runner.ProcessorCITest` is the hardware-facing runner. +- Root-level `core.*` imports remain as compatibility shims for older scripts. +- Test execution writes a JUnit report named `test_results_.xml`. + +## Notes + +- The current organization keeps large verification data in place while moving Python implementation into a package, matching the shape used by `processor_ci_discover`. +- The runner currently executes the `basic` category. `advanced` and `invalid` data are preserved for expansion and maintenance. diff --git a/core/__init__.py b/core/__init__.py index dc29bcf..2c86cb6 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -1,191 +1,12 @@ -import time -import os -import sys +"""Compatibility imports for the former root-level core package.""" -# processor_ci_communication/core/serial.py -sys.path.append( - os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')) -) -from processor_ci_communication.core.serial import ProcessorCIInterface -from junitparser import TestCase, TestSuite, JUnitXml, Skipped, Error, Failure -# TEST_CATEGORIES = ['basic', 'advanced', 'invalid'] -TEST_CATEGORIES = ['basic'] +def __getattr__(name: str): + if name == 'ProcessorCITest': + from riscv_test_runner.core.runner import ProcessorCITest + return ProcessorCITest + raise AttributeError(name) -class ProcessorCITest: - def __init__( - self, - port: str = '/dev/ttyUSB0', - baudrate: int = 115200, - serial_timeout: int = 1, - sync_keyword: int = 0x4E455859, - timeout: int = 1000, - directories: list[dict] = [], - extra_tests: list[dict] = [], - core_use_two_memories: bool = False, - ): - self.port = port - self.baudrate = baudrate - self.serial_timeout = serial_timeout - self.sync_keyword = sync_keyword - self.timeout = timeout - self.directories = directories - self.extra_tests = extra_tests - self.core_use_two_memories = core_use_two_memories - self.xml = JUnitXml() - - self.processor_ci = ProcessorCIInterface( - self.port, self.baudrate, self.serial_timeout - ) - - id = self.processor_ci.get_module_id() - - if id is None: - print('Failed to get module ID. Exiting.') - sys.exit(1) - - print(f'Connected to FPGA with ID: {id}') - - print('Checking for sync keyword...') - if id != self.sync_keyword: - print( - f'Sync keyword mismatch. Expected: {self.sync_keyword}, Got: {id}' - ) - sys.exit(1) - - print('Sync keyword matched.') - self.processor_ci.set_timeout(self.timeout) - self.processor_ci.load_msb_accumulator(0) - self.processor_ci.load_lsb_accumulator(0) - - print('Testsuite configurated.') - - def _execute_test( - self, test_path: str, reference_path: str, breakpoint: int - ) -> tuple[bool, int, int]: # result, expected, received - test_file = open(test_path, 'r') - reference_file = open(reference_path, 'r') - - test_content = [] - test_lines = test_file.readlines() - test_len = len(test_lines) - - for i in range(0, breakpoint + 4, 4): - shiffted = i >> 2 - if shiffted >= test_len: - test_content.append(0) - else: - test_content.append(int(test_lines[shiffted], 16)) - test_file.close() - - self.processor_ci.write_from_accumulator( - (breakpoint + 4) >> 2, test_content - ) - - if(self.core_use_two_memories): - self.processor_ci.write_memory( - breakpoint, 0, self.core_use_two_memories) - - self.processor_ci.execute_until_stop() - - reference_lines = reference_file.readlines() - reference_content = [int(data, 16) for data in reference_lines] - - ok = True - expected = 0 - received = 0 - - difference_address = (len(reference_content) - 1) << 2 - - for i in reference_content: - received = int.from_bytes( - self.processor_ci.read_memory( - breakpoint - difference_address, self.core_use_two_memories - ) - ) - expected = i - - if expected != received: - ok = False - break - - difference_address -= 4 - - return ok, expected, received - - def _run_test_dir(self, config: dict) -> None: - path = config['path'] - name = config['name'] - - print(f'Running tests: {name} in {path}') - - if not os.path.exists(path): - raise FileNotFoundError(f'Test directory {path} does not exist.') - - for category in TEST_CATEGORIES: - suite = TestSuite(f'{name}.{category}') - - category_path = os.path.join(path, category) - category_breakpoint = config.get(f'{category}_breakpoint', 50) - memory_files_path = os.path.join(category_path, 'memory') - referece_files_path = os.path.join(category_path, 'reference') - - self.processor_ci.set_execution_end_address(category_breakpoint) - - print( - f'Running {category} tests in {category_path}, with breakpoint {category_breakpoint}' - ) - - memory_files = sorted(os.listdir(memory_files_path)) - reference_files = sorted(os.listdir(referece_files_path)) - - if len(memory_files) != len(reference_files): - raise ValueError( - f'Memory files and reference files count mismatch in {category_path}.' - ) - - for memory_file, reference_file in zip( - memory_files, reference_files - ): - memory_file_path = os.path.join(memory_files_path, memory_file) - reference_file_path = os.path.join( - referece_files_path, reference_file - ) - - print(f'Running test: {memory_file}') - - result, expected, received = self._execute_test( - memory_file_path, reference_file_path, category_breakpoint - ) - - if result: - test_case = TestCase( - name=f'{memory_file}', classname=f'{name}.{category}' - ) - suite.add_testcase(test_case) - else: - test_case = TestCase( - name=f'{memory_file}', - classname=f'{name}.{category}', - ) - test_case.result = [Failure( - f'Expected: {expected}, Received: {received}' - )] - suite.add_testcase(test_case) - - self.xml.add_testsuite(suite) - - def run_tests(self): - for dir in self.directories: - self._run_test_dir(dir) - - def run_extra_tests(self): - ... - - def generate_junit_report(self): - # Generate JUnit XML report - current_time = time.time() - self.xml.write(f'test_results_{current_time}.xml') - print(f'JUnit XML report generated: test_results_{current_time}.xml') +__all__ = ['ProcessorCITest'] diff --git a/core/march.py b/core/march.py index 1d4ef51..c61dda0 100644 --- a/core/march.py +++ b/core/march.py @@ -1,41 +1,5 @@ -import re +"""Compatibility wrapper for riscv_test_runner.core.march.""" +from riscv_test_runner.core.march import parse_march -def parse_march(march: str): - """ - Faz o parse de uma string 'march' e retorna: - - XLEN: 32 ou 64 - - extensões principais e opcionais (ex: I, M, Zicsr, etc.) - """ - # Determina XLEN - xlen_match = re.match(r'rv(32|64)', march) - if not xlen_match: - raise ValueError(f'Formato inválido de march: {march}') - xlen = int(xlen_match.group(1)) - - # Remove o prefixo rv32/rv64 - march_body = march[xlen_match.end() :] - - # Divide a parte principal das extensões custom (ex: _Zicsr) - base_match = re.match(r'([a-z]+)', march_body) - base_exts = list(base_match.group(1).upper()) if base_match else [] - - # Captura extensões Z (e outras prefixadas com _) ex: _Zicsr_Zifencei - extra_exts = re.findall(r'_([A-Za-z0-9]+)', march_body) - - # Junta tudo - all_extensions = base_exts + extra_exts - - return {'xlen': xlen, 'extensions': all_extensions} - - -if __name__ == '__main__': - # Exemplos de uso - print(parse_march('rv32imac')) - # Saída: {'xlen': 32, 'extensions': ['I', 'M', 'A', 'C']} - - print(parse_march('rv64i_Zicsr_Zifencei')) - # Saída: {'xlen': 64, 'extensions': ['I', 'Zicsr', 'Zifencei']} - - print(parse_march('rv32imafd_Zicsr')) - # Saída: {'xlen': 32, 'extensions': ['I', 'M', 'A', 'F', 'D', 'Zicsr']} +__all__ = ['parse_march'] diff --git a/core/utils.py b/core/utils.py index a726c2e..eb46d6a 100644 --- a/core/utils.py +++ b/core/utils.py @@ -1,32 +1,9 @@ -def hex_string_to_bytes(hex_str: str) -> bytes: - # Remove o prefixo '0x' se existir - hex_str = hex_str[2:] if hex_str.startswith('0x') else hex_str - # Garante número par de caracteres - if len(hex_str) % 2 != 0: - hex_str = '0' + hex_str - return bytes.fromhex(hex_str) +"""Compatibility wrapper for riscv_test_runner.core.utils.""" +from riscv_test_runner.core.utils import ( + hex_string_to_bytes, + hex_to_int, + hex_to_string, +) -def hex_to_int(hex_str: str, endian='big') -> int: - hex_str = hex_str[2:] if hex_str.startswith('0x') else hex_str - byte_data = bytes.fromhex(hex_str) - - if endian == 'little': - byte_data = byte_data[::-1] - - return int.from_bytes(byte_data, byteorder='big') - - -def hex_to_string(hex_str: str, endian='big') -> str: - # Remove o "0x" do começo se existir - hex_str = hex_str[2:] if hex_str.startswith('0x') else hex_str - - # Converte para bytes - byte_data = bytes.fromhex(hex_str) - - # Ajusta a ordem dos bytes - if endian == 'little': - byte_data = byte_data[::-1] - - # Converte para string ASCII - return byte_data.decode('ascii') +__all__ = ['hex_string_to_bytes', 'hex_to_int', 'hex_to_string'] diff --git a/docs/README.md b/docs/README.md index e69de29..62dd75e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,50 @@ +# Repository Layout + +This repo is organized around two kinds of assets: + +- source code that runs tests against a core +- data files used by those tests and benchmarks + +## Python Runner + +```text +riscv_test_runner/ + cli.py + core/ + runner.py + march.py + utils.py +main.py +core/ +``` + +`riscv_test_runner/` is the canonical package. `main.py` and root-level `core/` are compatibility wrappers for older scripts. + +## ISA Test Corpus + +```text +tests///src/ +tests///memory/ +tests///reference/ +``` + +- `src/`: assembly source for the test +- `memory/`: compiled hex loaded into the processor memory +- `reference/`: expected values read back by the runner + +The architecture folder names are used by the `--march` selector and entries in `config.json`. + +## Benchmarks + +```text +benchmarks/coremark/ +benchmarks/common/ +benchmarks/port/ +benchmarks/target/ +``` + +The benchmark area keeps source, platform hooks, linker scripts, and build helpers together. Generated files should stay under benchmark build directories rather than being mixed into the ISA test corpus. + +## Third-Party Material + +`third_party_repositories/` stores imported sources and converted assembly used to derive tests. Keep local edits documented when refreshing these files from upstream material. diff --git a/main.py b/main.py index 9c715f9..c67a854 100644 --- a/main.py +++ b/main.py @@ -1,124 +1,4 @@ -import os -import json -import argparse -from core.march import parse_march -from core import ProcessorCITest -from core.utils import hex_string_to_bytes - - -def main() -> None: - parser = argparse.ArgumentParser( - description='Script to run tests in a directory' - ) - - parser.add_argument( - '-b', - '--baudrate', - type=int, - default=115200, - help='Baudrate for serial connection', - ) - parser.add_argument( - '-p', - '--port', - type=str, - required=True, - help='Port for serial connection', - ) - parser.add_argument( - '-s', - '--serial-timeout', - type=int, - default=5, - help='Serial timeout in seconds', - ) - parser.add_argument( - '-c', - '--config', - type=str, - required=True, - help='Path to the configuration file', - ) - parser.add_argument( - '-m', - '--march', - type=str, - default='rv32i', - help='Architecture for the tests', - ) - parser.add_argument( - '-e', - '--extra-tests', - type=str, - default='', - help='Extra tests to run', - ) - - parser.add_argument( - '-k', - '--key', - type=str, - default='', - help='Key to sync with the board', - ) - parser.add_argument( - '-t', - '--timeout', - type=int, - default=1000, - help='Timeout for the tests in cicles of clock', - ) - # core_use_two_memories - parser.add_argument( - '-ctm', - '--core-use-two-memories', - action='store_true', - help='Use two memories in the core', - default=False, - ) - - args = parser.parse_args() - - config_data = None - extra_tests_parse = args.extra_tests.split(',') if args.extra_tests else [] - - with open(args.config) as f: - config_data = json.load(f) - - parsed_arch = parse_march(args.march) - XLEN = parsed_arch['xlen'] - MARCH = parsed_arch['extensions'] - XLEN_STR = str(XLEN) - - directories = config_data['test_directories'] - print(directories[0]['name'][2:4]) - directories = [ - dir - for dir in directories - if (dir['name'][2:4] == XLEN_STR or XLEN == 64) - and dir['name'][4:] in MARCH - ] - extra_tests = config_data['extra_tests'] - extra_tests = [ - test for test in extra_tests if test['name'] in extra_tests_parse - ] - - sync_key_hex = hex_string_to_bytes(args.key) - - p_tester = ProcessorCITest( - port=args.port, - baudrate=args.baudrate, - serial_timeout=args.serial_timeout, - sync_keyword=sync_key_hex, - timeout=args.timeout, - directories=directories, - extra_tests=extra_tests, - core_use_two_memories=args.core_use_two_memories, - ) - p_tester.run_tests() - p_tester.run_extra_tests() - p_tester.generate_junit_report() - print('All tests finished.') +from riscv_test_runner.cli import main if __name__ == '__main__': diff --git a/riscv_test_runner/__init__.py b/riscv_test_runner/__init__.py new file mode 100644 index 0000000..7d0c4e4 --- /dev/null +++ b/riscv_test_runner/__init__.py @@ -0,0 +1 @@ +"""Processor CI test runner package.""" diff --git a/riscv_test_runner/cli.py b/riscv_test_runner/cli.py new file mode 100644 index 0000000..d1f1a3d --- /dev/null +++ b/riscv_test_runner/cli.py @@ -0,0 +1,125 @@ +import argparse +import json + +from riscv_test_runner.core.march import parse_march +from riscv_test_runner.core.utils import hex_string_to_bytes + + +def build_parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser( + description='Run Processor CI tests against a RISC-V core over serial' + ) + + parser.add_argument( + '-b', + '--baudrate', + type=int, + default=115200, + help='Baudrate for serial connection', + ) + parser.add_argument( + '-p', + '--port', + type=str, + required=True, + help='Port for serial connection', + ) + parser.add_argument( + '-s', + '--serial-timeout', + type=int, + default=5, + help='Serial timeout in seconds', + ) + parser.add_argument( + '-c', + '--config', + type=str, + required=True, + help='Path to the configuration file', + ) + parser.add_argument( + '-m', + '--march', + type=str, + default='rv32i', + help='RISC-V architecture string for the tests', + ) + parser.add_argument( + '-e', + '--extra-tests', + type=str, + default='', + help='Comma-separated list of extra tests to run', + ) + parser.add_argument( + '-k', + '--key', + type=str, + default='', + help='Hex key used to sync with the board', + ) + parser.add_argument( + '-t', + '--timeout', + type=int, + default=1000, + help='Timeout for the tests in clock cycles', + ) + parser.add_argument( + '-ctm', + '--core-use-two-memories', + action='store_true', + help='Use separate instruction and data memories in the core', + default=False, + ) + + return parser + + +def main(argv: list[str] | None = None) -> None: + args = build_parser().parse_args(argv) + from riscv_test_runner.core.runner import ProcessorCITest + + extra_tests_parse = args.extra_tests.split(',') if args.extra_tests else [] + + with open(args.config, encoding='utf-8') as config_file: + config_data = json.load(config_file) + + parsed_arch = parse_march(args.march) + xlen = parsed_arch['xlen'] + extensions = parsed_arch['extensions'] + xlen_str = str(xlen) + + directories = [ + directory + for directory in config_data['test_directories'] + if ( + directory['name'][2:4] == xlen_str or xlen == 64 + ) + and directory['name'][4:] in extensions + ] + extra_tests = [ + test + for test in config_data['extra_tests'] + if test['name'] in extra_tests_parse + ] + + tester = ProcessorCITest( + port=args.port, + baudrate=args.baudrate, + serial_timeout=args.serial_timeout, + sync_keyword=hex_string_to_bytes(args.key), + timeout=args.timeout, + directories=directories, + extra_tests=extra_tests, + core_use_two_memories=args.core_use_two_memories, + ) + tester.run_tests() + tester.run_extra_tests() + tester.generate_junit_report() + print('All tests finished.') + + +if __name__ == '__main__': + main() diff --git a/riscv_test_runner/core/__init__.py b/riscv_test_runner/core/__init__.py new file mode 100644 index 0000000..9d1787d --- /dev/null +++ b/riscv_test_runner/core/__init__.py @@ -0,0 +1,12 @@ +"""Core helpers for the Processor CI test runner.""" + + +def __getattr__(name: str): + if name == 'ProcessorCITest': + from riscv_test_runner.core.runner import ProcessorCITest + + return ProcessorCITest + raise AttributeError(name) + + +__all__ = ['ProcessorCITest'] diff --git a/riscv_test_runner/core/march.py b/riscv_test_runner/core/march.py new file mode 100644 index 0000000..551ecf9 --- /dev/null +++ b/riscv_test_runner/core/march.py @@ -0,0 +1,45 @@ +import re + + +def parse_march(march: str): + """ + Faz o parse de uma string 'march' e retorna: + - XLEN: 32 ou 64 + - extensões principais e opcionais (ex: I, M, Zicsr, etc.) + """ + # Determina XLEN + xlen_match = re.match(r'rv(32|64)', march) + if not xlen_match: + raise ValueError(f'Formato inválido de march: {march}') + xlen = int(xlen_match.group(1)) + + # Remove o prefixo rv32/rv64 + march_body = march[xlen_match.end() :] + + # Divide a parte principal das extensões custom (ex: _Zicsr) + base_match = re.match(r'([a-z]+)', march_body) + base_exts = list(base_match.group(1).upper()) if base_match else [] + + # Captura extensões Z (e outras prefixadas com _) ex: _Zicsr_Zifencei + extra_exts = [ + ext[0].upper() + ext[1:] for ext in re.findall( + r'_([A-Za-z0-9]+)', march_body + ) + ] + + # Junta tudo + all_extensions = base_exts + extra_exts + + return {'xlen': xlen, 'extensions': all_extensions} + + +if __name__ == '__main__': + # Exemplos de uso + print(parse_march('rv32imac')) + # Saída: {'xlen': 32, 'extensions': ['I', 'M', 'A', 'C']} + + print(parse_march('rv64i_Zicsr_Zifencei')) + # Saída: {'xlen': 64, 'extensions': ['I', 'Zicsr', 'Zifencei']} + + print(parse_march('rv32imafd_Zicsr')) + # Saída: {'xlen': 32, 'extensions': ['I', 'M', 'A', 'F', 'D', 'Zicsr']} diff --git a/riscv_test_runner/core/runner.py b/riscv_test_runner/core/runner.py new file mode 100644 index 0000000..306b224 --- /dev/null +++ b/riscv_test_runner/core/runner.py @@ -0,0 +1,191 @@ +import time +import os +import sys + +# processor_ci_communication/core/serial.py +sys.path.append( + os.path.abspath(os.path.join(os.path.dirname(__file__), '../../..')) +) +from processor_ci_communication.core.serial import ProcessorCIInterface +from junitparser import TestCase, TestSuite, JUnitXml, Skipped, Error, Failure + +# TEST_CATEGORIES = ['basic', 'advanced', 'invalid'] +TEST_CATEGORIES = ['basic'] + + +class ProcessorCITest: + def __init__( + self, + port: str = '/dev/ttyUSB0', + baudrate: int = 115200, + serial_timeout: int = 1, + sync_keyword: int = 0x4E455859, + timeout: int = 1000, + directories: list[dict] = [], + extra_tests: list[dict] = [], + core_use_two_memories: bool = False, + ): + self.port = port + self.baudrate = baudrate + self.serial_timeout = serial_timeout + self.sync_keyword = sync_keyword + self.timeout = timeout + self.directories = directories + self.extra_tests = extra_tests + self.core_use_two_memories = core_use_two_memories + + self.xml = JUnitXml() + + self.processor_ci = ProcessorCIInterface( + self.port, self.baudrate, self.serial_timeout + ) + + id = self.processor_ci.get_module_id() + + if id is None: + print('Failed to get module ID. Exiting.') + sys.exit(1) + + print(f'Connected to FPGA with ID: {id}') + + print('Checking for sync keyword...') + if id != self.sync_keyword: + print( + f'Sync keyword mismatch. Expected: {self.sync_keyword}, Got: {id}' + ) + sys.exit(1) + + print('Sync keyword matched.') + self.processor_ci.set_timeout(self.timeout) + self.processor_ci.load_msb_accumulator(0) + self.processor_ci.load_lsb_accumulator(0) + + print('Testsuite configurated.') + + def _execute_test( + self, test_path: str, reference_path: str, breakpoint: int + ) -> tuple[bool, int, int]: # result, expected, received + test_file = open(test_path, 'r') + reference_file = open(reference_path, 'r') + + test_content = [] + test_lines = test_file.readlines() + test_len = len(test_lines) + + for i in range(0, breakpoint + 4, 4): + shiffted = i >> 2 + if shiffted >= test_len: + test_content.append(0) + else: + test_content.append(int(test_lines[shiffted], 16)) + test_file.close() + + self.processor_ci.write_from_accumulator( + (breakpoint + 4) >> 2, test_content + ) + + if(self.core_use_two_memories): + self.processor_ci.write_memory( + breakpoint, 0, self.core_use_two_memories) + + self.processor_ci.execute_until_stop() + + reference_lines = reference_file.readlines() + reference_content = [int(data, 16) for data in reference_lines] + + ok = True + expected = 0 + received = 0 + + difference_address = (len(reference_content) - 1) << 2 + + for i in reference_content: + received = int.from_bytes( + self.processor_ci.read_memory( + breakpoint - difference_address, self.core_use_two_memories + ) + ) + expected = i + + if expected != received: + ok = False + break + + difference_address -= 4 + + return ok, expected, received + + def _run_test_dir(self, config: dict) -> None: + path = config['path'] + name = config['name'] + + print(f'Running tests: {name} in {path}') + + if not os.path.exists(path): + raise FileNotFoundError(f'Test directory {path} does not exist.') + + for category in TEST_CATEGORIES: + suite = TestSuite(f'{name}.{category}') + + category_path = os.path.join(path, category) + category_breakpoint = config.get(f'{category}_breakpoint', 50) + memory_files_path = os.path.join(category_path, 'memory') + referece_files_path = os.path.join(category_path, 'reference') + + self.processor_ci.set_execution_end_address(category_breakpoint) + + print( + f'Running {category} tests in {category_path}, with breakpoint {category_breakpoint}' + ) + + memory_files = sorted(os.listdir(memory_files_path)) + reference_files = sorted(os.listdir(referece_files_path)) + + if len(memory_files) != len(reference_files): + raise ValueError( + f'Memory files and reference files count mismatch in {category_path}.' + ) + + for memory_file, reference_file in zip( + memory_files, reference_files + ): + memory_file_path = os.path.join(memory_files_path, memory_file) + reference_file_path = os.path.join( + referece_files_path, reference_file + ) + + print(f'Running test: {memory_file}') + + result, expected, received = self._execute_test( + memory_file_path, reference_file_path, category_breakpoint + ) + + if result: + test_case = TestCase( + name=f'{memory_file}', classname=f'{name}.{category}' + ) + suite.add_testcase(test_case) + else: + test_case = TestCase( + name=f'{memory_file}', + classname=f'{name}.{category}', + ) + test_case.result = [Failure( + f'Expected: {expected}, Received: {received}' + )] + suite.add_testcase(test_case) + + self.xml.add_testsuite(suite) + + def run_tests(self): + for dir in self.directories: + self._run_test_dir(dir) + + def run_extra_tests(self): + ... + + def generate_junit_report(self): + # Generate JUnit XML report + current_time = time.time() + self.xml.write(f'test_results_{current_time}.xml') + print(f'JUnit XML report generated: test_results_{current_time}.xml') diff --git a/riscv_test_runner/core/utils.py b/riscv_test_runner/core/utils.py new file mode 100644 index 0000000..a726c2e --- /dev/null +++ b/riscv_test_runner/core/utils.py @@ -0,0 +1,32 @@ +def hex_string_to_bytes(hex_str: str) -> bytes: + # Remove o prefixo '0x' se existir + hex_str = hex_str[2:] if hex_str.startswith('0x') else hex_str + # Garante número par de caracteres + if len(hex_str) % 2 != 0: + hex_str = '0' + hex_str + return bytes.fromhex(hex_str) + + +def hex_to_int(hex_str: str, endian='big') -> int: + hex_str = hex_str[2:] if hex_str.startswith('0x') else hex_str + byte_data = bytes.fromhex(hex_str) + + if endian == 'little': + byte_data = byte_data[::-1] + + return int.from_bytes(byte_data, byteorder='big') + + +def hex_to_string(hex_str: str, endian='big') -> str: + # Remove o "0x" do começo se existir + hex_str = hex_str[2:] if hex_str.startswith('0x') else hex_str + + # Converte para bytes + byte_data = bytes.fromhex(hex_str) + + # Ajusta a ordem dos bytes + if endian == 'little': + byte_data = byte_data[::-1] + + # Converte para string ASCII + return byte_data.decode('ascii')