Browse Source

Started on shift operations

k#
master
Raphael Roberts 6 years ago
parent
commit
5371ea1572
  1. 23
      register.py
  2. 8
      test.py

23
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()

8
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)
Loading…
Cancel
Save