Skip to content

Commit

Permalink
some jump instructions (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
devanshshukla99 authored Mar 28, 2023
1 parent aebf634 commit f14cc6d
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 39 deletions.
18 changes: 10 additions & 8 deletions core/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, console=None) -> None:
self.console = Console()
# operations
self.op = Operations()
self.op.super_memory.PC("0x30") # RAM general scratch pad area
# self.op.super_memory.PC("0x30") # RAM general scratch pad area
# instruction set
self._jump_flag = False
self._address_jump_flag = None
Expand All @@ -30,7 +30,7 @@ def __init__(self, console=None) -> None:
# callstack
self._callstack = []
self.ready = False
self._jump_methods = []
self._jump_methods = self.op._jump_instructions
self._wrap_bounceable_methods()
self._run_idx = 0
return
Expand Down Expand Up @@ -62,7 +62,7 @@ def _func(*args, **kwargs):

def _bounce_to_label(self, label):
idx, _ = self._locate_jump_label(label)
print(f"JUMPING to {idx}")
print(f"JUMPING to label: {label} index: {idx}")
self._run_idx = idx
return True

Expand Down Expand Up @@ -103,7 +103,7 @@ def _target_label(self, label) -> bool:
_data_to_write = decompose_byte(str(_jump_label._counter))
self.op.memory_write(str(_target_label._counter + 1), _data_to_write[1])
self.op.memory_write(str(_target_label._counter + 2), _data_to_write[0])
_assembler = self.op._assembler[_target_label._command].replace("0xff", "").strip()
_assembler = self.op._assembler[_target_label._command].replace("oxff", "").strip()
self.op._assembler[_target_label._command] = " ".join(
[_assembler, _data_to_write[1], _data_to_write[0]]
)
Expand Down Expand Up @@ -155,9 +155,12 @@ def parse(self, command):
opcode, args, kwargs = self._parser(command)
self.console.log(f"opcode: {opcode}; args: {args}; kwargs: {kwargs}")
if self.instruct_set._is_jump_opcode(opcode):
print("JUMP instruction")
# if JNZ | JC | etc ** kwargs the target-label **
kwargs["target-label"] = JumpFlag(args[0], self.op.super_memory.PC, command)
args.extend(["0xff", "0xff"]) # placeholder
kwargs["target-label"] = JumpFlag(args[0], self.op.super_memory.PC + len(self.op._internal_PC), command)
args.append("offset") # placeholder
# kwargs["label"] = _label
# args.append("offset") # placeholder
opcode_func = self._lookup_opcode_func(opcode)
self._addjob(opcode, opcode_func, args, kwargs)
self.op.prepare_operation(command, opcode, *args)
Expand All @@ -169,12 +172,11 @@ def parse(self, command):
The function should execute at both commands; if `target-label` or `label` then look for
`target-label` and `label`;
if `target-lable` is found then replace the placeholder obtained using the `PC` in `label`
if `target-tabel` is found then replace the placeholder obtained using the `PC` in `label`
"""
_label = kwargs.get("target-label", kwargs.get("label", None))
if _label:
self._target_label(_label)

self.ready = True
return True

Expand Down
80 changes: 79 additions & 1 deletion core/instruction_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Instructions:
def __init__(self, op) -> None:
self.op = op
self._jump_flag = False
self._jump_instructions = [] # Add later
self._jump_instructions = op._jump_instructions
self._base = 16
self.flags = self.op.super_memory.PSW
pass
Expand Down Expand Up @@ -215,4 +215,82 @@ def pop(self, addr: str) -> bool:
data = self.op.super_memory.SP.read()
return self.op.memory_write(addr, data)

def jz(self, label, *args, **kwargs) -> bool:
"""Jump if accumulator is zero"""
bounce_to_label = kwargs.get("bounce_to_label")
print(self.op.memory_read("A"))
if int(self.op.memory_read("A")) == 0:
return bounce_to_label(label)
return True

def jnz(self, label, *args, **kwargs) -> bool:
"""Jump if accumulator is not zero"""
bounce_to_label = kwargs.get("bounce_to_label")
print(self.op.memory_read("A"))
if int(self.op.memory_read("A")) != 0:
return bounce_to_label(label)
return True

def jc(self, label, *args, **kwargs) -> bool:
"""Jump if carry"""
bounce_to_label = kwargs.get("bounce_to_label")
print(self.op.flags.CY)
if self.op.flags.CY:
return bounce_to_label(label)
return True

def jnc(self, label, *args, **kwargs) -> bool:
"""Jump if no carry"""
bounce_to_label = kwargs.get("bounce_to_label")
print(self.op.flags.CY)
if not self.op.flags.CY:
return bounce_to_label(label)
return True

def djnz(self, addr, label, *args, **kwargs) -> bool:
"""Jump if accumulator is not zero"""
bounce_to_label = kwargs.get("bounce_to_label")
if label == "offset":
label = addr
addr = "A"
data = self.op.memory_read(addr)

result = int(str(data), 16) - 1
self.op.memory_write(addr, hex(result))
if int(self.op.memory_read(addr)) != 0:
return bounce_to_label(label)
return True

def cjne(self, addr, addr2, label, *args, **kwargs) -> bool:
"""Compare and jump if not equal"""
bounce_to_label = kwargs.get("bounce_to_label")
data_1 = self.op.memory_read(addr)
addr2, _ = self._resolve_addressing_mode(addr2)
data_2 = self.op.memory_read(addr2)
if int(data_1) != int(data_2):
if int(data_1) < int(data_2):
self.flags.CY = True
# Jump if not equal
return bounce_to_label(label)
self.flags.CY = False
return True

def jb(self, addr, label, *args, **kwargs) -> bool:
"""Jump if bit is true"""
raise NotImplemented
bounce_to_label = kwargs.get("bounce_to_label")
data = self.op.memory_read(addr)
if data:
return bounce_to_label(label)
return True

def jnb(self, addr, label, *args, **kwargs) -> bool:
"""Jump if bit is false"""
raise NotImplemented
bounce_to_label = kwargs.get("bounce_to_label")
data = self.op.memory_read(addr)
if data:
return bounce_to_label(label)
return True

pass
58 changes: 29 additions & 29 deletions core/opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"INC R5": "0x0D",
"INC R6": "0x0E",
"INC R7": "0x0F",
"JBC BIT offset": "0x10",
"JBC BIT DIRECT DIRECT": "0x10",
"ACALL addr11": "0x11",
"LCALL addr16": "0x12",
"RRC A": "0x13",
Expand All @@ -31,7 +31,7 @@
"DEC R5": "0x1D",
"DEC R6": "0x1E",
"DEC R7": "0x1F",
"JB BIT offset": "0x20",
"JB BIT DIRECT DIRECT": "0x20",
"AJMP addr11": "0x21",
"RET": "0x22",
"RL A": "0x23",
Expand All @@ -47,7 +47,7 @@
"ADD A R5": "0x2D",
"ADD A R6": "0x2E",
"ADD A R7": "0x2F",
"JNB BIT offset": "0x30",
"JNB BIT DIRECT DIRECT": "0x30",
"ACALL addr11": "0x31",
"RETI": "0x32",
"RLC A": "0x33",
Expand All @@ -63,7 +63,7 @@
"ADDC A R5": "0x3D",
"ADDC A R6": "0x3E",
"ADDC A R7": "0x3F",
"JC offset": "0x40",
"JC DIRECT DIRECT": "0x40",
"AJMP addr11": "0x41",
"ORL DIRECT A": "0x42",
"ORL DIRECT #IMMED": "0x43",
Expand All @@ -79,7 +79,7 @@
"ORL A R5": "0x4D",
"ORL A R6": "0x4E",
"ORL A R7": "0x4F",
"JNC offset": "0x50",
"JNC DIRECT DIRECT": "0x50",
"ACALL addr11": "0x51",
"ANL DIRECT A": "0x52",
"ANL DIRECT #IMMED": "0x53",
Expand All @@ -95,7 +95,7 @@
"ANL A R5": "0x5D",
"ANL A R6": "0x5E",
"ANL A R7": "0x5F",
"JZ offset": "0x60",
"JZ DIRECT DIRECT": "0x60",
"AJMP addr11": "0x61",
"XRL DIRECT A": "0x62",
"XRL DIRECT #IMMED": "0x63",
Expand All @@ -111,7 +111,7 @@
"XRL A R5": "0x6D",
"XRL A R6": "0x6E",
"XRL A R7": "0x6F",
"JNZ offset": "0x70",
"JNZ DIRECT DIRECT": "0x70",
"ACALL addr11": "0x71",
"ORL C BIT": "0x72",
"JMP @A+DPTR": "0x73",
Expand All @@ -127,7 +127,7 @@
"MOV R5 #IMMED": "0x7D",
"MOV R6 #IMMED": "0x7E",
"MOV R7 #IMMED": "0x7F",
"SJMP offset": "0x80",
"SJMP DIRECT DIRECT": "0x80",
"AJMP addr11": "0x81",
"ANL C BIT": "0x82",
"MOVC A @A+PC": "0x83",
Expand Down Expand Up @@ -179,18 +179,18 @@
"ACALL addr11": "0xB1",
"CPL BIT": "0xB2",
"CPL C": "0xB3",
"CJNE A #IMMED offset": "0xB4",
"CJNE A DIRECT offset": "0xB5",
"CJNE @R0 #IMMED offset": "0xB6",
"CJNE @R1 #IMMED offset": "0xB7",
"CJNE R0 #IMMED offset": "0xB8",
"CJNE R1 #IMMED offset": "0xB9",
"CJNE R2 #IMMED offset": "0xBA",
"CJNE R3 #IMMED offset": "0xBB",
"CJNE R4 #IMMED offset": "0xBC",
"CJNE R5 #IMMED offset": "0xBD",
"CJNE R6 #IMMED offset": "0xBE",
"CJNE R7 #IMMED offset": "0xBF",
"CJNE A #IMMED DIRECT DIRECT": "0xB4",
"CJNE A DIRECT DIRECT DIRECT": "0xB5",
"CJNE @R0 #IMMED DIRECT DIRECT": "0xB6",
"CJNE @R1 #IMMED DIRECT DIRECT": "0xB7",
"CJNE R0 #IMMED DIRECT DIRECT": "0xB8",
"CJNE R1 #IMMED DIRECT DIRECT": "0xB9",
"CJNE R2 #IMMED DIRECT DIRECT": "0xBA",
"CJNE R3 #IMMED DIRECT DIRECT": "0xBB",
"CJNE R4 #IMMED DIRECT DIRECT": "0xBC",
"CJNE R5 #IMMED DIRECT DIRECT": "0xBD",
"CJNE R6 #IMMED DIRECT DIRECT": "0xBE",
"CJNE R7 #IMMED DIRECT DIRECT": "0xBF",
"PUSH DIRECT": "0xC0",
"AJMP addr11": "0xC1",
"CLR BIT": "0xC2",
Expand All @@ -212,17 +212,17 @@
"SETB BIT": "0xD2",
"SETB C": "0xD3",
"DA A": "0xD4",
"DJNZ DIRECT offset": "0xD5",
"DJNZ DIRECT DIRECT": "0xD5",
"XCHD A @R0": "0xD6",
"XCHD A @R1": "0xD7",
"DJNZ R0 offset": "0xD8",
"DJNZ R1 offset": "0xD9",
"DJNZ R2 offset": "0xDA",
"DJNZ R3 offset": "0xDB",
"DJNZ R4 offset": "0xDC",
"DJNZ R5 offset": "0xDD",
"DJNZ R6 offset": "0xDE",
"DJNZ R7 offset": "0xDF",
"DJNZ R0 DIRECT DIRECT": "0xD8",
"DJNZ R1 DIRECT DIRECT": "0xD9",
"DJNZ R2 DIRECT DIRECT": "0xDA",
"DJNZ R3 DIRECT DIRECT": "0xDB",
"DJNZ R4 DIRECT DIRECT": "0xDC",
"DJNZ R5 DIRECT DIRECT": "0xDD",
"DJNZ R6 DIRECT DIRECT": "0xDE",
"DJNZ R7 DIRECT DIRECT": "0xDF",
"MOVX A @DPTR": "0xE0",
"AJMP addr11": "0xE1",
"MOVX A @R0": "0xE2",
Expand Down
24 changes: 23 additions & 1 deletion core/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ def __init__(self) -> None:
self._generate_keywords()
self._assembler = {}
self._internal_PC = []

# Jump instructions
self._jump_instructions = [
"SJMP",
"AJMP",
"LJMP",
"JMP",
"JC",
"JNC",
"JB",
"JNB",
"JBC",
"JZ",
"JNZ",
"DJNZ",
"CJNE",
] # Add later
pass

def _generate_keywords(self):
Expand Down Expand Up @@ -83,6 +100,10 @@ def _opcode_fetch(self, opcode, *args, **kwargs) -> None:
if x[0] == "@":
print("Register indirect")
_args_params.append(x)
elif x == "B":
print("B")
_args_params.append("DIRECT")
_args_hexs.append(["0xF0"]) # memory location for `B`
else:
_args_params.append(x)
else:
Expand All @@ -98,7 +119,8 @@ def _opcode_fetch(self, opcode, *args, **kwargs) -> None:
else:
print("direct")
_args_params.append("DIRECT")
_args_hexs.append(decompose_byte(tohex(x)))
if opcode not in self._jump_instructions:
_args_hexs.append(decompose_byte(tohex(x)))

print(_args_params)
print(_args_hexs)
Expand Down

0 comments on commit f14cc6d

Please sign in to comment.