Skip to content

Commit

Permalink
sim/util: fix int conversion in i2c utils
Browse files Browse the repository at this point in the history
  • Loading branch information
vk2seb committed Feb 2, 2024
1 parent a662a96 commit 3038ef7
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gateware/sim/util/i2s.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ async def i2s_clock_in_u32(bick, sdin):

def bits_not(n, width):
"""Bitwise NOT from positive integer of `width` bits."""
n = int(n)
return (1 << width) - 1 - n

def bits_from_signed(n, width):
n = int(n)
"""Bits (2s complement) of `width` from signed integer."""
return n if n >= 0 else bits_not(-n, width) + 1

def signed_from_bits(n, width):
n = int(n)
"""Signed integer from (2s complement) bits of `width`."""
if (1 << (width-1) & n) > 0:
return -int(bits_not(n, width) + 1)
Expand Down

0 comments on commit 3038ef7

Please sign in to comment.