diff --git a/CHANGELOG.md b/CHANGELOG.md index c7499eb..e6ac77c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,17 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [v2.1.0] + +### Changed + +* Refactor bit manipulation and remove `myhdl` dependency by @fsagbuya in https://github.com/linien-org/pyrp3/pull/12 + ## [v2.0.2] ### Fixed * Don't use setuptools_scm & specify version manually by @doronbehar in https://github.com/linien-org/pyrp3/pull/14 +[v2.1.0]: https://github.com/linien-org/pyrp3/compare/v2.0.2...v2.1.0 [v2.0.2]: https://github.com/linien-org/pyrp3/compare/v2.0.1...v2.0.2 diff --git a/LICENSE b/LICENSE index 7e9b925..ee85329 100644 --- a/LICENSE +++ b/LICENSE @@ -2,7 +2,9 @@ BSD 3-Clause License Copyright (c) 2016, Pierre Cladé All rights reserved. Copyright (c) 2018-2019, Benjamin Wiegand All rights reserved. -Copyright (c) 2022, Bastian Leykauf All rights reserved. +Copyright (c) 2022-2024, Bastian Leykauf All rights reserved. +Copyright (c) 2024, Doron Behar All rights reserved. +Copyright (c) 2024, Florian Agbuya All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/pyproject.toml b/pyproject.toml index 2da39c7..ec8e695 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,13 +4,14 @@ build-backend = "setuptools.build_meta" [project] name = "pyrp3" -version = "2.0.2" +version = "2.1.0" description = "Python utilities for RedPitaya" authors = [ { name = "Pierre Cladé", email = "pierre.clade@upmc.fr" }, { name = "Benjamin Wiegand", email = "benjamin.wiegand@physik.hu-berlin.de" }, { name = "Bastian Leykauf", email = "leykauf@physik.hu-berlin.de" }, { name = "Doron Behar", email = "doron.behar@gmail.com" }, + { name = "Florian Agbuya", email = "fa@m-labs.ph" }, ] maintainers = [ { name = "Bastian Leykauf", email = "leykauf@physik.hu-berlin.de" }, @@ -20,7 +21,6 @@ readme = "README.md" requires-python = ">=3.10" dependencies = [ "cached_property>=1.5.2,<2.0", - "myhdl>=0.11", "numpy>=1.21.5,<2.0", "rpyc>=6.0.0,<7.0", ] diff --git a/pyrp3/instrument.py b/pyrp3/instrument.py index d58ed84..e234b57 100644 --- a/pyrp3/instrument.py +++ b/pyrp3/instrument.py @@ -1,7 +1,6 @@ from math import ceil, log import numpy as np -from myhdl import intbv from .enum import Enum from .memory import MemoryInterface @@ -12,10 +11,14 @@ def __init__(self, size): self.size = size def to_python(self, val): - return int(val & (2**self.size - 1)) + if not 0 <= val < (1 << self.size): + raise ValueError("Input value %s exceeds %s-bit limit" % (val, self.size)) + return int(val & ((1 << self.size) - 1)) def to_binary(self, val): - return intbv(val, max=2**self.size, min=0)._val + if not 0 <= val < (1 << self.size): + raise ValueError("Input value %s exceeds %s-bit limit" % (val, self.size)) + return val & ((1 << self.size) - 1) class SignedInteger: @@ -23,12 +26,23 @@ def __init__(self, size): self.size = size def to_python(self, val): - return int(intbv(val & (2**self.size - 1), min=0, max=2**self.size).signed()) + val &= (1 << self.size) - 1 + if val >= (1 << (self.size - 1)): + val -= 1 << self.size + if not -(1 << (self.size - 1)) <= val < (1 << (self.size - 1)): + raise ValueError( + "Input value %s exceeds %s-bit signed limit" % (val, self.size) + ) + return int(val) def to_binary(self, val): - return intbv(val, min=-(2 ** (self.size - 1)), max=2 ** (self.size - 1))[ - self.size : - ]._val + if not -(1 << (self.size - 1)) <= val < (1 << self.size): + raise ValueError( + "Input value %s exceeds %s-bit signed limit" % (val, self.size) + ) + if val < 0: + val += 1 << self.size + return val & ((1 << self.size) - 1) class EnumTypeWrapper(UnsignedInteger): @@ -80,11 +94,10 @@ def to_binary(self, val): val = val / 1.8 assert val >= 0 assert val <= 1 - out = intbv(0, min=0, max=2**32) - tmp = np.long(np.round(val * (17 * 157 - 1))) - out[32:16] = tmp // 17 - out[16:0] = 2**tmp % 17 - 1 - return out[32:]._val + tmp = np.longlong(np.round(val * (17 * 157 - 1))) + high = tmp // 17 + low = (1 << (tmp % 17)) - 1 + return (high << 16) | low class RegisterProperty: @@ -128,13 +141,14 @@ def __get__(self, instance, owner): if instance is None: return self out = instance.read(self.addr) - val = intbv(out)[self.pos] - return val if self._bit_type is None else self._bit_type.to_python(val) + val = (out >> self.pos) & 1 + return ( + bool(val) if self._bit_type is None else self._bit_type.to_python(bool(val)) + ) def __set__(self, instance, value): current = instance.read(self.addr) - new_value = intbv(current) - new_value[self.pos] = value + new_value = current & ~(1 << self.pos) | ((value & 1) << self.pos) return instance.write(self.addr, int(new_value))