Compare commits
17 Commits
b940f4a018
...
a28bf834ac
Author | SHA1 | Date |
---|---|---|
Anton Lydike | a28bf834ac | 1 year ago |
Anton Lydike | 207cf918ef | 1 year ago |
Anton Lydike | c7e14a3b42 | 1 year ago |
Anton Lydike | 7a4972d48f | 1 year ago |
Sasha Lopoukhine | 25d059da09 | 2 years ago |
Anton Lydike | d6d3a18aa6 | 2 years ago |
Anton Lydike | 1ea5bb2edc | 2 years ago |
Anton Lydike | 2f6073b4df | 2 years ago |
Anton Lydike | 189dc63ceb | 2 years ago |
Anton Lydike | 0c37be3c4d | 2 years ago |
Anton Lydike | a51681811f | 2 years ago |
Anton Lydike | 87968d08d9 | 2 years ago |
Anton Lydike | 94d01a97d9 | 2 years ago |
Anton Lydike | 448b19c144 | 2 years ago |
Anton Lydike | 5515c7795c | 2 years ago |
Anton Lydike | e1fbe4f11d | 2 years ago |
Anton Lydike | dd77d1b387 | 2 years ago |
@ -0,0 +1,2 @@
|
|||||||
|
# introduces black formatting
|
||||||
|
5515c7795cfd690d346aad10ce17b30acf914648
|
@ -0,0 +1,49 @@
|
|||||||
|
# This workflow will install Python dependencies, run tests and lint with a single version of Python
|
||||||
|
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
|
||||||
|
|
||||||
|
name: CI - Python-based Testing
|
||||||
|
|
||||||
|
on:
|
||||||
|
# Trigger the workflow on push or pull request,
|
||||||
|
# but only for the master branch
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
python-version: ['3.10']
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python-version }}
|
||||||
|
|
||||||
|
- name: Upgrade pip
|
||||||
|
run: |
|
||||||
|
pip install --upgrade pip
|
||||||
|
|
||||||
|
- name: Install the package locally
|
||||||
|
run: pip install -r requirements.txt -r requirements-dev.txt
|
||||||
|
|
||||||
|
- name: Test with pytest
|
||||||
|
run: |
|
||||||
|
pytest -W error
|
||||||
|
- name: Test with lit
|
||||||
|
run: |
|
||||||
|
lit -v test/filecheck
|
||||||
|
|
||||||
|
#- name: Execute lit tests
|
||||||
|
# run: |
|
||||||
|
# export PYTHONPATH=$(pwd)
|
||||||
|
# lit -v tests/filecheck/
|
@ -0,0 +1,34 @@
|
|||||||
|
# This workflow check the format all files in the repository
|
||||||
|
# * It checks that all nonempty files have a newline at the end
|
||||||
|
# * It checks that there are no whitespaces at the end of lines
|
||||||
|
# * It checks that Python files are formatted with black
|
||||||
|
|
||||||
|
name: Code Formatting
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
push:
|
||||||
|
branches: [master]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
code-formatting:
|
||||||
|
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
python-version: ['3.10']
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python-version }}
|
||||||
|
|
||||||
|
- name: Upgrade pip
|
||||||
|
run: |
|
||||||
|
pip install --upgrade pip
|
||||||
|
|
||||||
|
- name: Run code formatting checks with pre-commit
|
||||||
|
uses: pre-commit/action@v3.0.0
|
@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (riscemu)" project-jdk-type="Python SDK" />
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (riscemu)" project-jdk-type="Python SDK" />
|
||||||
</project>
|
</project>
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
# See https://pre-commit.com for more information
|
||||||
|
# See https://pre-commit.com/hooks.html for more hooks
|
||||||
|
repos:
|
||||||
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
|
rev: v3.2.0
|
||||||
|
hooks:
|
||||||
|
- id: trailing-whitespace
|
||||||
|
- id: end-of-file-fixer
|
||||||
|
- repo: https://github.com/psf/black
|
||||||
|
rev: 23.3.0
|
||||||
|
hooks:
|
||||||
|
- id: black
|
@ -0,0 +1,5 @@
|
|||||||
|
black==23.3.0
|
||||||
|
pre-commit==3.2.2
|
||||||
|
pytest==7.3.1
|
||||||
|
filecheck==0.0.23
|
||||||
|
lit==16.0.2
|
@ -1,2 +1,2 @@
|
|||||||
from .decoder import decode, RISCV_REGS
|
from .decoder import decode, RISCV_REGS
|
||||||
from .formatter import format_ins
|
from .formatter import format_ins
|
||||||
|
@ -1,6 +1,34 @@
|
|||||||
RISCV_REGS = [
|
RISCV_REGS = [
|
||||||
'zero', 'ra', 'sp', 'gp', 'tp', 't0', 't1', 't2',
|
"zero",
|
||||||
's0', 's1', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7',
|
"ra",
|
||||||
's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', 's10', 's11',
|
"sp",
|
||||||
't3', 't4', 't5', 't6'
|
"gp",
|
||||||
|
"tp",
|
||||||
|
"t0",
|
||||||
|
"t1",
|
||||||
|
"t2",
|
||||||
|
"s0",
|
||||||
|
"s1",
|
||||||
|
"a0",
|
||||||
|
"a1",
|
||||||
|
"a2",
|
||||||
|
"a3",
|
||||||
|
"a4",
|
||||||
|
"a5",
|
||||||
|
"a6",
|
||||||
|
"a7",
|
||||||
|
"s2",
|
||||||
|
"s3",
|
||||||
|
"s4",
|
||||||
|
"s5",
|
||||||
|
"s6",
|
||||||
|
"s7",
|
||||||
|
"s8",
|
||||||
|
"s9",
|
||||||
|
"s10",
|
||||||
|
"s11",
|
||||||
|
"t3",
|
||||||
|
"t4",
|
||||||
|
"t5",
|
||||||
|
"t6",
|
||||||
]
|
]
|
||||||
|
@ -1,81 +1,81 @@
|
|||||||
from typing import Dict, Tuple
|
from typing import Dict, Tuple
|
||||||
|
|
||||||
MCAUSE_TRANSLATION: Dict[Tuple[int, int], str]= {
|
MCAUSE_TRANSLATION: Dict[Tuple[int, int], str] = {
|
||||||
(1, 0): 'User software interrupt',
|
(1, 0): "User software interrupt",
|
||||||
(1, 1): 'Supervisor software interrupt',
|
(1, 1): "Supervisor software interrupt",
|
||||||
(1, 3): 'Machine software interrupt',
|
(1, 3): "Machine software interrupt",
|
||||||
(1, 4): 'User timer interrupt',
|
(1, 4): "User timer interrupt",
|
||||||
(1, 5): 'Supervisor timer interrupt',
|
(1, 5): "Supervisor timer interrupt",
|
||||||
(1, 7): 'Machine timer interrupt',
|
(1, 7): "Machine timer interrupt",
|
||||||
(1, 8): 'User external interrupt',
|
(1, 8): "User external interrupt",
|
||||||
(1, 9): 'Supervisor external interrupt',
|
(1, 9): "Supervisor external interrupt",
|
||||||
(1, 11): 'Machine external interrupt',
|
(1, 11): "Machine external interrupt",
|
||||||
(0, 0): 'Instruction address misaligned',
|
(0, 0): "Instruction address misaligned",
|
||||||
(0, 1): 'Instruction access fault',
|
(0, 1): "Instruction access fault",
|
||||||
(0, 2): 'Illegal instruction',
|
(0, 2): "Illegal instruction",
|
||||||
(0, 3): 'Breakpoint',
|
(0, 3): "Breakpoint",
|
||||||
(0, 4): 'Load address misaligned',
|
(0, 4): "Load address misaligned",
|
||||||
(0, 5): 'Load access fault',
|
(0, 5): "Load access fault",
|
||||||
(0, 6): 'Store/AMO address misaligned',
|
(0, 6): "Store/AMO address misaligned",
|
||||||
(0, 7): 'Store/AMO access fault',
|
(0, 7): "Store/AMO access fault",
|
||||||
(0, 8): 'environment call from user mode',
|
(0, 8): "environment call from user mode",
|
||||||
(0, 9): 'environment call from supervisor mode',
|
(0, 9): "environment call from supervisor mode",
|
||||||
(0, 11): 'environment call from machine mode',
|
(0, 11): "environment call from machine mode",
|
||||||
(0, 12): 'Instruction page fault',
|
(0, 12): "Instruction page fault",
|
||||||
(0, 13): 'Load page fault',
|
(0, 13): "Load page fault",
|
||||||
(0, 15): 'Store/AMO page fault',
|
(0, 15): "Store/AMO page fault",
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
Assigns tuple (interrupt bit, exception code) to their respective readable names
|
Assigns tuple (interrupt bit, exception code) to their respective readable names
|
||||||
"""
|
"""
|
||||||
|
|
||||||
MSTATUS_OFFSETS: Dict[str, int] = {
|
MSTATUS_OFFSETS: Dict[str, int] = {
|
||||||
'uie': 0,
|
"uie": 0,
|
||||||
'sie': 1,
|
"sie": 1,
|
||||||
'mie': 3,
|
"mie": 3,
|
||||||
'upie': 4,
|
"upie": 4,
|
||||||
'spie': 5,
|
"spie": 5,
|
||||||
'mpie': 7,
|
"mpie": 7,
|
||||||
'spp': 8,
|
"spp": 8,
|
||||||
'mpp': 11,
|
"mpp": 11,
|
||||||
'fs': 13,
|
"fs": 13,
|
||||||
'xs': 15,
|
"xs": 15,
|
||||||
'mpriv': 17,
|
"mpriv": 17,
|
||||||
'sum': 18,
|
"sum": 18,
|
||||||
'mxr': 19,
|
"mxr": 19,
|
||||||
'tvm': 20,
|
"tvm": 20,
|
||||||
'tw': 21,
|
"tw": 21,
|
||||||
'tsr': 22,
|
"tsr": 22,
|
||||||
'sd': 31
|
"sd": 31,
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
Offsets for all mstatus bits
|
Offsets for all mstatus bits
|
||||||
"""
|
"""
|
||||||
|
|
||||||
MSTATUS_LEN_2 = ('mpp', 'fs', 'xs')
|
MSTATUS_LEN_2 = ("mpp", "fs", "xs")
|
||||||
"""
|
"""
|
||||||
All mstatus parts that have length 2. All other mstatus parts have length 1
|
All mstatus parts that have length 2. All other mstatus parts have length 1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
CSR_NAME_TO_ADDR: Dict[str, int] = {
|
CSR_NAME_TO_ADDR: Dict[str, int] = {
|
||||||
'mstatus': 0x300,
|
"mstatus": 0x300,
|
||||||
'misa': 0x301,
|
"misa": 0x301,
|
||||||
'mie': 0x304,
|
"mie": 0x304,
|
||||||
'mtvec': 0x305,
|
"mtvec": 0x305,
|
||||||
'mepc': 0x341,
|
"mepc": 0x341,
|
||||||
'mcause': 0x342,
|
"mcause": 0x342,
|
||||||
'mtval': 0x343,
|
"mtval": 0x343,
|
||||||
'mip': 0x344,
|
"mip": 0x344,
|
||||||
'mvendorid': 0xF11,
|
"mvendorid": 0xF11,
|
||||||
'marchid': 0xF12,
|
"marchid": 0xF12,
|
||||||
'mimpid': 0xF13,
|
"mimpid": 0xF13,
|
||||||
'mhartid': 0xF14,
|
"mhartid": 0xF14,
|
||||||
'time': 0xc01,
|
"time": 0xC01,
|
||||||
'timeh': 0xc81,
|
"timeh": 0xC81,
|
||||||
'halt': 0x789,
|
"halt": 0x789,
|
||||||
'mtimecmp': 0x780,
|
"mtimecmp": 0x780,
|
||||||
'mtimecmph': 0x781,
|
"mtimecmph": 0x781,
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
Translation for named registers
|
Translation for named registers
|
||||||
"""
|
"""
|
||||||
|
@ -1,29 +1,56 @@
|
|||||||
from . import MemorySection, InstructionContext, MemoryFlags, T_RelativeAddress, Instruction
|
from typing import Optional
|
||||||
|
from . import (
|
||||||
|
MemorySection,
|
||||||
|
InstructionContext,
|
||||||
|
MemoryFlags,
|
||||||
|
T_RelativeAddress,
|
||||||
|
Instruction,
|
||||||
|
)
|
||||||
from ..types.exceptions import MemoryAccessException
|
from ..types.exceptions import MemoryAccessException
|
||||||
|
|
||||||
|
|
||||||
class BinaryDataMemorySection(MemorySection):
|
class BinaryDataMemorySection(MemorySection):
|
||||||
def __init__(self, data: bytearray, name: str, context: InstructionContext, owner: str, base: int = 0, flags: MemoryFlags = None):
|
def __init__(
|
||||||
self.name = name
|
self,
|
||||||
self.base = base
|
data: bytearray,
|
||||||
self.context = context
|
name: str,
|
||||||
self.size = len(data)
|
context: InstructionContext,
|
||||||
self.flags = flags if flags is not None else MemoryFlags(False, False)
|
owner: str,
|
||||||
|
base: int = 0,
|
||||||
|
flags: Optional[MemoryFlags] = None,
|
||||||
|
):
|
||||||
|
super().__init__(
|
||||||
|
name,
|
||||||
|
flags if flags is not None else MemoryFlags(False, False),
|
||||||
|
len(data),
|
||||||
|
base,
|
||||||
|
owner,
|
||||||
|
context,
|
||||||
|
)
|
||||||
self.data = data
|
self.data = data
|
||||||
self.owner = owner
|
|
||||||
|
|
||||||
def read(self, offset: T_RelativeAddress, size: int) -> bytearray:
|
def read(self, offset: T_RelativeAddress, size: int) -> bytearray:
|
||||||
if offset + size > self.size:
|
if offset + size > self.size:
|
||||||
raise MemoryAccessException("Out of bounds access in {}".format(self), offset, size, 'read')
|
raise MemoryAccessException(
|
||||||
return self.data[offset:offset + size]
|
"Out of bounds access in {}".format(self), offset, size, "read"
|
||||||
|
)
|
||||||
|
return self.data[offset : offset + size]
|
||||||
|
|
||||||
def write(self, offset: T_RelativeAddress, size: int, data: bytearray):
|
def write(self, offset: T_RelativeAddress, size: int, data: bytearray):
|
||||||
if offset + size > self.size:
|
if offset + size > self.size:
|
||||||
raise MemoryAccessException("Out of bounds access in {}".format(self), offset, size, 'write')
|
raise MemoryAccessException(
|
||||||
|
"Out of bounds access in {}".format(self), offset, size, "write"
|
||||||
|
)
|
||||||
if len(data[0:size]) != size:
|
if len(data[0:size]) != size:
|
||||||
raise MemoryAccessException("Invalid write parameter sizing", offset, size, 'write')
|
raise MemoryAccessException(
|
||||||
self.data[offset:offset + size] = data[0:size]
|
"Invalid write parameter sizing", offset, size, "write"
|
||||||
|
)
|
||||||
|
self.data[offset : offset + size] = data[0:size]
|
||||||
|
|
||||||
def read_ins(self, offset: T_RelativeAddress) -> Instruction:
|
def read_ins(self, offset: T_RelativeAddress) -> Instruction:
|
||||||
raise MemoryAccessException("Tried reading instruction on non-executable section {}".format(self),
|
raise MemoryAccessException(
|
||||||
offset, 4, 'instruction fetch')
|
"Tried reading instruction on non-executable section {}".format(self),
|
||||||
|
offset,
|
||||||
|
4,
|
||||||
|
"instruction fetch",
|
||||||
|
)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
build
|
build
|
||||||
source/riscemu*.rst
|
source/riscemu*.rst
|
||||||
source/modules.rst
|
source/modules.rst
|
||||||
source/help
|
source/help
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
from .test_tokenizer import *
|
from .test_tokenizer import *
|
||||||
from .test_helpers import *
|
from .test_helpers import *
|
||||||
from .test_integers import *
|
from .test_integers import *
|
||||||
|
@ -1,73 +0,0 @@
|
|||||||
import contextlib
|
|
||||||
import os
|
|
||||||
from abc import abstractmethod
|
|
||||||
from tempfile import NamedTemporaryFile
|
|
||||||
from typing import Optional, Union, Tuple
|
|
||||||
from unittest import TestCase
|
|
||||||
|
|
||||||
from riscemu import CPU, UserModeCPU, InstructionSetDict, RunConfig
|
|
||||||
from riscemu.types import Program
|
|
||||||
|
|
||||||
|
|
||||||
class EndToEndTest(TestCase):
|
|
||||||
|
|
||||||
def __init__(self, cpu: Optional[CPU] = None):
|
|
||||||
super().__init__()
|
|
||||||
if cpu is None:
|
|
||||||
cpu = UserModeCPU(InstructionSetDict.values(), RunConfig())
|
|
||||||
self.cpu = cpu
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def get_source(self) -> Tuple[str, Union[bytes, str, bytearray]]:
|
|
||||||
"""
|
|
||||||
This method returns the source code of the program
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
def test_run_program(self):
|
|
||||||
"""
|
|
||||||
Runs the program and verifies output
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
with self.with_source_file() as names:
|
|
||||||
fname, orig_name = names
|
|
||||||
loader = self.cpu.get_best_loader_for(fname)
|
|
||||||
self.program = loader.instantiate(fname, loader.get_options([])).parse()
|
|
||||||
self._change_program_file_name(self.program, orig_name)
|
|
||||||
self.cpu.load_program(self.program)
|
|
||||||
self.after_program_load(self.program)
|
|
||||||
if isinstance(self.cpu, UserModeCPU):
|
|
||||||
self.cpu.setup_stack()
|
|
||||||
try:
|
|
||||||
self.cpu.launch(self.program)
|
|
||||||
except Exception as ex:
|
|
||||||
if self.is_exception_expected(ex):
|
|
||||||
pass
|
|
||||||
raise ex
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
|
||||||
def with_source_file(self):
|
|
||||||
name, content = self.get_source()
|
|
||||||
if isinstance(content, str):
|
|
||||||
f = NamedTemporaryFile('w', suffix=name, delete=False)
|
|
||||||
else:
|
|
||||||
f = NamedTemporaryFile('wb', suffix=name, delete=False)
|
|
||||||
f.write(content)
|
|
||||||
f.flush()
|
|
||||||
f.close()
|
|
||||||
try:
|
|
||||||
yield f.name, name
|
|
||||||
finally:
|
|
||||||
os.unlink(f.name)
|
|
||||||
|
|
||||||
def after_program_load(self, program):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def is_exception_expected(self, ex: Exception) -> bool:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def _change_program_file_name(self, program: Program, new_name: str):
|
|
||||||
program.name = new_name
|
|
||||||
for sec in program.sections:
|
|
||||||
sec.owner = new_name
|
|
@ -0,0 +1,2 @@
|
|||||||
|
8.096814e-02 hello-world.asm
|
||||||
|
9.465098e-02 fibs.asm
|
@ -0,0 +1,23 @@
|
|||||||
|
// RUN: python3 -m riscemu -v --ignore-exit-code %s || true | filecheck %s
|
||||||
|
.data
|
||||||
|
fibs: .space 1024
|
||||||
|
|
||||||
|
.text
|
||||||
|
main:
|
||||||
|
addi s1, zero, 0 // storage index
|
||||||
|
addi s2, zero, 1024 // last storage index
|
||||||
|
addi t0, zero, 1 // t0 = F_{i}
|
||||||
|
addi t1, zero, 1 // t1 = F_{i+1}
|
||||||
|
loop:
|
||||||
|
sw t0, fibs(s1) // save
|
||||||
|
add t2, t1, t0 // t2 = F_{i+2}
|
||||||
|
addi t0, t1, 0 // t0 = t1
|
||||||
|
addi t1, t2, 0 // t1 = t2
|
||||||
|
addi s1, s1, 4 // increment storage pointer
|
||||||
|
blt s1, s2, loop // loop as long as we did not reach array length
|
||||||
|
// exit gracefully
|
||||||
|
add a0, zero, t2
|
||||||
|
addi a7, zero, 93
|
||||||
|
scall // exit with code 0
|
||||||
|
|
||||||
|
// CHECK: [CPU] Program exited with code 1265227608
|
@ -0,0 +1,15 @@
|
|||||||
|
// RUN: python3 -m riscemu -v %s | filecheck %s
|
||||||
|
.data
|
||||||
|
msg: .ascii "Hello world\n"
|
||||||
|
.text
|
||||||
|
addi a0, zero, 1 // print to stdout
|
||||||
|
addi a1, zero, msg // load msg address
|
||||||
|
addi a2, zero, 12 // write 12 bytes
|
||||||
|
addi a7, zero, SCALL_WRITE // write syscall code
|
||||||
|
scall
|
||||||
|
addi a0, zero, 0 // set exit code to 0
|
||||||
|
addi a7, zero, SCALL_EXIT // exit syscall code
|
||||||
|
scall
|
||||||
|
|
||||||
|
// CHECK: Hello world
|
||||||
|
// CHECK: [CPU] Program exited with code 0
|
@ -0,0 +1,9 @@
|
|||||||
|
import lit.formats
|
||||||
|
import os
|
||||||
|
|
||||||
|
config.test_source_root = os.path.dirname(__file__)
|
||||||
|
xdsl_src = os.path.dirname(os.path.dirname(config.test_source_root))
|
||||||
|
|
||||||
|
config.name = "riscemu"
|
||||||
|
config.test_format = lit.formats.ShTest(preamble_commands=[f"cd {xdsl_src}"])
|
||||||
|
config.suffixes = ['.asm']
|
@ -1,7 +1,15 @@
|
|||||||
from unittest import TestCase
|
|
||||||
|
|
||||||
from riscemu.helpers import *
|
from riscemu.helpers import *
|
||||||
|
|
||||||
|
|
||||||
class TestHelpers(TestCase):
|
def test_align_address():
|
||||||
pass
|
assert align_addr(3, 1) == 3
|
||||||
|
assert align_addr(3, 2) == 4
|
||||||
|
assert align_addr(3, 4) == 4
|
||||||
|
assert align_addr(3, 8) == 8
|
||||||
|
assert align_addr(8, 8) == 8
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_numeric():
|
||||||
|
assert parse_numeric_argument("13") == 13
|
||||||
|
assert parse_numeric_argument("0x100") == 256
|
||||||
|
assert parse_numeric_argument("-13") == -13
|
||||||
|
@ -1,19 +1,15 @@
|
|||||||
from unittest import TestCase
|
|
||||||
|
|
||||||
from riscemu.types import Int32, UInt32
|
from riscemu.types import Int32, UInt32
|
||||||
|
|
||||||
|
|
||||||
class TestTokenizer(TestCase):
|
def test_logical_right_shift():
|
||||||
|
a = Int32(100)
|
||||||
def test_logical_right_shift(self):
|
assert a.shift_right_logical(0) == a
|
||||||
a = Int32(100)
|
assert a.shift_right_logical(10) == 0
|
||||||
self.assertEqual(a.shift_right_logical(0), a)
|
assert a.shift_right_logical(1) == 100 >> 1
|
||||||
self.assertEqual(a.shift_right_logical(10), 0)
|
|
||||||
self.assertEqual(a.shift_right_logical(1), 100>>1)
|
|
||||||
|
|
||||||
a = Int32(-100)
|
a = Int32(-100)
|
||||||
self.assertEqual(a.shift_right_logical(0), a)
|
assert a.shift_right_logical(0) == a
|
||||||
self.assertEqual(a.shift_right_logical(1), 2147483598)
|
assert a.shift_right_logical(1) == 2147483598
|
||||||
self.assertEqual(a.shift_right_logical(10), 4194303)
|
assert a.shift_right_logical(10) == 4194303
|
||||||
self.assertEqual(a.shift_right_logical(31), 1)
|
assert a.shift_right_logical(31) == 1
|
||||||
self.assertEqual(a.shift_right_logical(32), 0)
|
assert a.shift_right_logical(32) == 0
|
||||||
|
@ -1,53 +0,0 @@
|
|||||||
from riscemu import AssemblyFileLoader
|
|
||||||
from riscemu.colors import *
|
|
||||||
|
|
||||||
FMT_SUCCESS = FMT_GREEN + FMT_BOLD
|
|
||||||
|
|
||||||
def run_test(path: str):
|
|
||||||
from riscemu import CPU, UserModeCPU, RunConfig
|
|
||||||
from riscemu.instructions import InstructionSetDict
|
|
||||||
from test.test_isa import TestIS
|
|
||||||
import os
|
|
||||||
|
|
||||||
fname = os.path.basename(path)
|
|
||||||
|
|
||||||
ISAs = list(InstructionSetDict.values())
|
|
||||||
ISAs.append(TestIS)
|
|
||||||
|
|
||||||
cpu = UserModeCPU(ISAs, RunConfig())
|
|
||||||
try:
|
|
||||||
program = AssemblyFileLoader(path, {}).parse()
|
|
||||||
cpu.load_program(program)
|
|
||||||
cpu.launch(program)
|
|
||||||
except Exception as ex:
|
|
||||||
print(FMT_ERROR + '[Test] 🔴 failed with exception "{}" ({})'.format(ex, fname) + FMT_NONE)
|
|
||||||
raise ex
|
|
||||||
|
|
||||||
if cpu.halted:
|
|
||||||
for isa in cpu.instruction_sets:
|
|
||||||
if isinstance(isa, TestIS):
|
|
||||||
if not isa.failed:
|
|
||||||
print(FMT_SUCCESS + '[Test] 🟢 successful {}'.format(fname) + FMT_NONE)
|
|
||||||
return not isa.failed
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
|
||||||
import os
|
|
||||||
import glob
|
|
||||||
|
|
||||||
successes = 0
|
|
||||||
failures = 0
|
|
||||||
ttl = 0
|
|
||||||
|
|
||||||
for path in glob.glob(f'{os.path.dirname(__file__)}/*.asm'):
|
|
||||||
print(FMT_BLUE + '[Test] running testcase ' + os.path.basename(path) + FMT_NONE)
|
|
||||||
ttl += 1
|
|
||||||
if run_test(path):
|
|
||||||
successes += 1
|
|
||||||
else:
|
|
||||||
failures += 1
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
|||||||
.data
|
|
||||||
|
|
||||||
data:
|
|
||||||
.word 0xFFFFFFFF, 0x0000FFFF, 0xFF00FF00, 0x7FFFFFFF
|
|
||||||
|
|
||||||
.text
|
|
||||||
ebreak
|
|
@ -1,20 +0,0 @@
|
|||||||
.text
|
|
||||||
|
|
||||||
main:
|
|
||||||
addi a0, zero, main
|
|
||||||
addi a1, zero, main
|
|
||||||
addi t0, zero, 1000
|
|
||||||
assert a0, ==, 0x100
|
|
||||||
1:
|
|
||||||
addi a1, a1, 1
|
|
||||||
blt a1, t0, 1b
|
|
||||||
sub a1, a1, a0
|
|
||||||
j 1f
|
|
||||||
addi a1, zero, 0
|
|
||||||
fail
|
|
||||||
1:
|
|
||||||
assert a1, ==, 744
|
|
||||||
add a0, zero, a1 ; set exit code to a1
|
|
||||||
addi a7, zero, SCALL_EXIT ; exit syscall code
|
|
||||||
scall
|
|
||||||
fail
|
|
Loading…
Reference in New Issue