code: def twos_comp_repr(val, bitwidth):
      
      correctbw = abs(val).bit_length() + 1
      if bitwidth < correctbw:
          raise pyrtl.PyrtlError("please choose a larger target bitwidth")
      if val >= 0:
          return val
      else:
          return (~abs(val) & (2**bitwidth-1)) + 1
docs: Converts a value to it's two's-complement (positive) integer representation using a
      given bitwidth (only converts the value if it is negative).
      For use with Simulation.step() etc. in passing negative numbers, which it does not accept