Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CMP Opcode #109

Merged
merged 18 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 57 additions & 39 deletions easier68k/core/models/memory_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@
from ..enum.op_size import OpSize


def mask_value_for_length(size: OpSize, unsigned_value: int) -> int:
"""
Masks an unsigned value to a certain size
:param size:
:param unsigned_value:
:return:
"""
if size is OpSize.BYTE:
return 0xFF & unsigned_value
if size is OpSize.WORD:
return 0xFFFF & unsigned_value
if size is OpSize.LONG:
return 0xFFFFFFFF & unsigned_value
return None


class MemoryValue:
"""
Representation of some value in memory
Expand Down Expand Up @@ -182,17 +198,19 @@ def get_value_bytearray(self) -> bytearray:
"""
return bytearray(self.__bytes__())

def get_msb(self):
def get_msb(self, size: OpSize=None):
"""
Get the most significant bit indicating that the value is negative
:return:
"""
if size is None:
size = self.length

# default mask is one byte
mask = 0x80
if self.length is OpSize.WORD:
if size is OpSize.WORD:
mask = 0x8000
elif self.length is OpSize.LONG:
elif size is OpSize.LONG:
mask = 0x80000000

# determine if the unsigned value MSB is set to 1
Expand All @@ -211,8 +229,7 @@ def __eq__(self, other) -> bool:
return self.get_value_signed() == other.get_value_signed()
elif isinstance(other, int):
return self.get_value_signed() == other
else:
return NotImplemented
return NotImplemented

def __add__(self, other):
"""
Expand All @@ -231,8 +248,25 @@ def __add__(self, other):
n = MemoryValue(self.length)
n.set_value_signed_int(total_value)
return n
else:
return NotImplemented
return NotImplemented

def __sub__(self, other):
"""
Subtracts the value of two memory values from each other
:param other:
:return:
"""
if isinstance(other, MemoryValue):
val = self.get_value_unsigned() - other.get_value_unsigned()
n = MemoryValue(self.length)
n.set_value_unsigned_int(mask_value_for_length(self.length, val))
return n
elif isinstance(other, int):
val = self.get_value_unsigned() - other
n = MemoryValue(self.length)
n.set_value_unsigned_int(mask_value_for_length(self.length, val))
return n
return NotImplemented

def __gt__(self, other):
"""
Expand All @@ -244,8 +278,7 @@ def __gt__(self, other):
return self.get_value_signed() > other.get_value_signed()
elif isinstance(other, int):
return self.get_value_signed() > other
else:
return NotImplemented
return NotImplemented

def __lt__(self, other):
"""
Expand All @@ -257,8 +290,7 @@ def __lt__(self, other):
return self.get_value_signed() < other.get_value_signed()
elif isinstance(other, int):
return self.get_value_signed() < other
else:
return NotImplemented
return NotImplemented

def __le__(self, other):
"""
Expand All @@ -270,8 +302,7 @@ def __le__(self, other):
return self.get_value_signed() <= other.get_value_signed()
elif isinstance(other, int):
return self.get_value_signed() <= other
else:
return NotImplemented
return NotImplemented

def __ne__(self, other):
"""
Expand All @@ -283,8 +314,7 @@ def __ne__(self, other):
return self.get_value_signed() != other.get_value_signed()
elif isinstance(other, int):
return self.get_value_signed() != other
else:
return NotImplemented
return NotImplemented

def __ge__(self, other):
"""
Expand All @@ -296,8 +326,7 @@ def __ge__(self, other):
return self.get_value_signed() >= other.get_value_signed()
elif isinstance(other, int):
return self.get_value_signed() >= other
else:
return NotImplemented
return NotImplemented

def __str__(self):
"""
Expand Down Expand Up @@ -331,8 +360,7 @@ def lsl(self, other):
n = MemoryValue(self.length)
n.set_value_unsigned_int(val)
return n
else:
return NotImplemented
return NotImplemented

def lsr(self, other):
"""
Expand All @@ -352,8 +380,7 @@ def lsr(self, other):
n = MemoryValue(self.length)
n.set_value_unsigned_int(val)
return n
else:
return NotImplemented
return NotImplemented

def __lshift__(self, other):
"""
Expand All @@ -373,8 +400,7 @@ def __lshift__(self, other):
n = MemoryValue(self.length)
n.set_value_signed_int(val)
return n
else:
return NotImplemented
return NotImplemented

def __rshift__(self, other):
"""
Expand All @@ -395,8 +421,7 @@ def __rshift__(self, other):
n = MemoryValue(self.length)
n.set_value_signed_int(val)
return n
else:
return NotImplemented
return NotImplemented

def __xor__(self, other):
"""
Expand All @@ -416,8 +441,7 @@ def __xor__(self, other):
n = MemoryValue(self.length)
n.set_value_signed_int(val)
return n
else:
return NotImplemented
return NotImplemented

def __invert__(self):
"""
Expand Down Expand Up @@ -455,8 +479,7 @@ def __or__(self, other):
n = MemoryValue(self.length)
n.set_value_unsigned_int(val)
return n
else:
return NotImplemented
return NotImplemented

def __and__(self, other):
"""
Expand All @@ -476,8 +499,7 @@ def __and__(self, other):
n = MemoryValue(self.length)
n.set_value_unsigned_int(val)
return n
else:
return NotImplemented
return NotImplemented

def __floordiv__(self, other):
"""
Expand All @@ -497,8 +519,7 @@ def __floordiv__(self, other):
n = MemoryValue(self.length)
n.set_value_unsigned_int(val)
return n
else:
return NotImplemented
return NotImplemented

def __mul__(self, other):
"""
Expand All @@ -518,8 +539,7 @@ def __mul__(self, other):
n = MemoryValue(self.length)
n.set_value_unsigned_int(val)
return n
else:
return NotImplemented
return NotImplemented

def __mod__(self, other):
"""
Expand All @@ -539,8 +559,7 @@ def __mod__(self, other):
n = MemoryValue(self.length)
n.set_value_unsigned_int(val)
return n
else:
return NotImplemented
return NotImplemented

def __pow__(self, power, modulo=None):
"""
Expand All @@ -561,5 +580,4 @@ def __pow__(self, power, modulo=None):
n = MemoryValue(self.length)
n.set_value_unsigned_int(val)
return n
else:
return NotImplemented
return NotImplemented
3 changes: 3 additions & 0 deletions easier68k/core/opcodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
'opcode_or',
'eor',
'add',
'adda',
'cmp',
'cmpi',
'sub',
'subq',
'adda',
Expand Down
1 change: 0 additions & 1 deletion easier68k/core/opcodes/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from ..util.parsing import parse_assembly_parameter
from ..models.assembly_parameter import AssemblyParameter
from ..enum.condition_status_code import ConditionStatusCode
import binascii
from ..models.memory_value import MemoryValue


Expand Down
Loading