Exemplo n.º 1
0
 def test_shift_left(self):
     random.seed(777906373)
     zero = pyrtl.Const(0, 1)
     one = pyrtl.Const(1, 1)
     self.out_zeros <<= barrel.barrel_shifter(self.inp_val, zero, one, self.inp_shift)
     self.out_ones <<= barrel.barrel_shifter(self.inp_val, one, one, self.inp_shift)
     sim_trace = pyrtl.SimulationTrace()
     sim = pyrtl.Simulation(tracer=sim_trace)
     vals = [random.randint(0, 20) for v in range(20)]
     shifts = [random.randint(0, 3) for s in range(20)]
     for i in range(len(vals)):
         sim.step({
             self.inp_val: vals[i],
             self.inp_shift: shifts[i]
         })
         base_sum = vals[i] * pow(2, shifts[i])
         self.assertEquals(sim.inspect(self.out_zeros), base_sum)
         self.assertEquals(sim.inspect(self.out_ones), base_sum + pow(2, shifts[i]) - 1)
Exemplo n.º 2
0
 def test_shift_left(self):
     random.seed(777906373)
     zero = pyrtl.Const(0, 1)
     one = pyrtl.Const(1, 1)
     self.out_zeros <<= barrel.barrel_shifter(self.inp_val, zero, one, self.inp_shift)
     self.out_ones <<= barrel.barrel_shifter(self.inp_val, one, one, self.inp_shift)
     sim_trace = pyrtl.SimulationTrace()
     sim = pyrtl.Simulation(tracer=sim_trace)
     vals = [random.randint(0, 20) for v in range(20)]
     shifts = [random.randint(0, 3) for s in range(20)]
     for i in range(len(vals)):
         sim.step({
             self.inp_val: vals[i],
             self.inp_shift: shifts[i]
         })
         base_sum = vals[i] * pow(2, shifts[i])
         self.assertEqual(sim.inspect(self.out_zeros), base_sum)
         self.assertEqual(sim.inspect(self.out_ones), base_sum + pow(2, shifts[i]) - 1)
Exemplo n.º 3
0
 def test_shift_right(self):
     random.seed(777906374)
     zero = pyrtl.Const(0, 1)
     one = pyrtl.Const(1, 1)
     self.out_zeros <<= barrel.barrel_shifter(self.inp_val, zero, zero, self.inp_shift)
     self.out_ones <<= barrel.barrel_shifter(self.inp_val, one, zero, self.inp_shift)
     sim_trace = pyrtl.SimulationTrace()
     sim = pyrtl.Simulation(tracer=sim_trace)
     vals = [random.randint(0, 20) for v in range(20)]
     shifts = [random.randint(0, 3) for s in range(20)]
     for i in range(len(vals)):
         sim.step({
             self.inp_val: vals[i],
             self.inp_shift: shifts[i]
         })
         base_sum = int(vals[i]/pow(2, shifts[i]))
         self.assertEqual(sim.inspect(self.out_zeros), base_sum, "failed on value %d" % vals[i])
         extra_sum = sum([pow(2, len(self.inp_val)-b-1) for b in range(shifts[i])])
         self.assertEquals(sim.inspect(self.out_ones), base_sum + extra_sum,
                           "failed on value %d" % vals[i])
Exemplo n.º 4
0
 def test_shift_right(self):
     random.seed(777906374)
     zero = pyrtl.Const(0, 1)
     one = pyrtl.Const(1, 1)
     self.out_zeros <<= barrel.barrel_shifter(self.inp_val, zero, zero, self.inp_shift)
     self.out_ones <<= barrel.barrel_shifter(self.inp_val, one, zero, self.inp_shift)
     sim_trace = pyrtl.SimulationTrace()
     sim = pyrtl.Simulation(tracer=sim_trace)
     vals = [random.randint(0, 20) for v in range(20)]
     shifts = [random.randint(0, 3) for s in range(20)]
     for i in range(len(vals)):
         sim.step({
             self.inp_val: vals[i],
             self.inp_shift: shifts[i]
         })
         base_sum = int(vals[i] / pow(2, shifts[i]))
         self.assertEqual(sim.inspect(self.out_zeros), base_sum, "failed on value %d" % vals[i])
         extra_sum = sum([pow(2, len(self.inp_val) - b - 1) for b in range(shifts[i])])
         self.assertEqual(sim.inspect(self.out_ones), base_sum + extra_sum,
                          "failed on value %d" % vals[i])
Exemplo n.º 5
0
def shift_right_logical(bits_to_shift, shift_amount):
    """ Shift right logical operation.

    :param bits_to_shift: WireVector to shift left
    :param shift_amount: WireVector specifying amount to shift
    :return: WireVector of same length as bits_to_shift

    This function returns a new WireVector of length equal to the length
    of the input `bits_to_shift` but where the bits have been shifted
    to the right.  An logical shift is one that treats the value as
    as unsigned number, meaning the zeros are shifted in regardless of
    the "sign bit".  Note that `shift_amount` is treated as unsigned.
    """
    a, shamt = _check_shift_inputs(bits_to_shift, shift_amount)
    bit_in = Const(0)  # shift in a 0
    dir = Const(0)  # shift right
    return barrel.barrel_shifter(bits_to_shift, bit_in, dir, shift_amount)
Exemplo n.º 6
0
def shift_right_arithmetic(bits_to_shift, shift_amount):
    """ Shift right arithmetic operation.

    :param bits_to_shift: WireVector to shift right
    :param shift_amount: WireVector specifying amount to shift
    :return: WireVector of same length as bits_to_shift

    This function returns a new WireVector of length equal to the length
    of the input `bits_to_shift` but where the bits have been shifted
    to the right.  An arithemetic shift is one that treats the value as
    as signed number, meaning the sign bit (the most significant bit of
    `bits_to_shift`) is shifted in. Note that `shift_amount` is treated as
    unsigned.
    """
    a, shamt = _check_shift_inputs(bits_to_shift, shift_amount)
    bit_in = bits_to_shift[-1]  # shift in sign_bit
    dir = Const(0)  # shift right
    return barrel.barrel_shifter(bits_to_shift, bit_in, dir, shift_amount)
Exemplo n.º 7
0
def shift_left_logical(bits_to_shift, shift_amount):
    """ Shift left logical operation.

    :param bits_to_shift: WireVector to shift left
    :param shift_amount: WireVector or integer specifying amount to shift
    :return: WireVector of same length as bits_to_shift

    This function returns a new WireVector of length equal to the length
    of the input `bits_to_shift` but where the bits have been shifted
    to the left.  A logical shift is one that treats the value as
    as unsigned number, meaning the zeroes are shifted in.  Note that
    `shift_amount` is treated as unsigned.
    """
    if isinstance(shift_amount, int):
        return concat(bits_to_shift[:-shift_amount], Const(0, shift_amount))

    bit_in = Const(0)  # shift in a 0
    dir = Const(1)  # shift left
    return barrel.barrel_shifter(bits_to_shift, bit_in, dir, shift_amount)