diff --git a/register.py b/register.py index a6da108..49e5c7a 100644 --- a/register.py +++ b/register.py @@ -56,6 +56,7 @@ class Accumulator(Register): parity_flag = False def set_flags(self): + "Set accumulator flags after an arithmetic operation" self.negative_flag = False self.zero_flag = False @@ -84,7 +85,25 @@ class Accumulator(Register): self.set_flags() def shift_right(self, through_carry=True): - pass + was_one = (self.get() & 1) == 1 + if through_carry: + add_bit = self.carry_flag + else: + add_bit = was_one + if add_bit: + self.value |= 1 << (self.n_bits) + self.value >>= 1 + self.carry_flag = was_one + self.set_flags() def shift_left(self, through_carry=True): - pass + was_one = (self.get() & (1 << (self.n_bits - 1))) != 0 + if through_carry: + add_bit = self.carry_flag + else: + add_bit = was_one + self.value <<= 1 + if add_bit: + self.value |= 1 + self.carry_flag = was_one + self.set_flags() diff --git a/test.py b/test.py new file mode 100644 index 0000000..26ef4ed --- /dev/null +++ b/test.py @@ -0,0 +1,8 @@ +from register import Accumulator + +a = Accumulator(8) +a.carry_flag = True +a.set(8) +print(a) +a.shift_right(True) +print(a)