Compare commits
4 Commits
master
...
fixed-inst
| Author | SHA1 | Date | |
|---|---|---|---|
| 0455d9cd3c | |||
| 0f134f96d7 | |||
| 412d9c9db6 | |||
| 2a1b50c83a |
@@ -4,13 +4,7 @@ import argparse
|
|||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from .instructions import Instruction
|
from .instructions import Instruction. get_instructions
|
||||||
from .instructions.inc import Inc
|
|
||||||
from .instructions.dec import Dec
|
|
||||||
from .instructions.nop import Nop
|
|
||||||
from .instructions.stop import Stop
|
|
||||||
from .instructions.jr import Jr
|
|
||||||
from .instructions.ld import Ld
|
|
||||||
from .arguments import ArgumentParser, Argument
|
from .arguments import ArgumentParser, Argument
|
||||||
|
|
||||||
from typing import Callable, Dict, List, Optional
|
from typing import Callable, Dict, List, Optional
|
||||||
@@ -20,21 +14,9 @@ logger = logging.getLogger(__name__)
|
|||||||
COMMENT_CHAR = "#"
|
COMMENT_CHAR = "#"
|
||||||
LABEL_SUFFIX = ":"
|
LABEL_SUFFIX = ":"
|
||||||
|
|
||||||
GB_INSTRUCTIONS = [
|
|
||||||
Nop(),
|
|
||||||
Stop(),
|
|
||||||
Inc(),
|
|
||||||
Dec(),
|
|
||||||
Jr(),
|
|
||||||
Ld(),
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def build_instruction_map() -> Dict[str, Instruction]:
|
def build_instruction_map() -> Dict[str, Instruction]:
|
||||||
d = {} # type: Dict[str, Instruction]
|
return {i.token: i for i in get_instructions()} # type: Dict[str, Instruction]
|
||||||
for i in GB_INSTRUCTIONS:
|
|
||||||
d[i.token] = i
|
|
||||||
return d
|
|
||||||
|
|
||||||
|
|
||||||
def try_parse_arguments(
|
def try_parse_arguments(
|
||||||
|
|||||||
@@ -1 +1,28 @@
|
|||||||
from .Instruction import Instruction
|
from .Instruction import Instruction
|
||||||
|
from .dec import Dec
|
||||||
|
from .inc import Inc
|
||||||
|
from .jr import Jr
|
||||||
|
from .ld import Ld
|
||||||
|
from .fixed import Ccf, Cpl, Daa, Di, Ei, Halt, Nop, Ret, Reti, Rla, Rlca, Rra, Scf, Stop
|
||||||
|
|
||||||
|
def get_instructions() -> List[Instruction]:
|
||||||
|
return [
|
||||||
|
Ccf(),
|
||||||
|
Cpl(),
|
||||||
|
Daa(),
|
||||||
|
Dec(),
|
||||||
|
Di(),
|
||||||
|
Ei(),
|
||||||
|
Inc(),
|
||||||
|
Halt(),
|
||||||
|
Jr(),
|
||||||
|
Nop(),
|
||||||
|
Ret(),
|
||||||
|
Reti(),
|
||||||
|
Rla(),
|
||||||
|
Rlca(),
|
||||||
|
Rra(),
|
||||||
|
Scf(),
|
||||||
|
Stop(),
|
||||||
|
]
|
||||||
|
|
||||||
|
|||||||
97
src/gbasm/instructions/fixed.py
Normal file
97
src/gbasm/instructions/fixed.py
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
from .Instruction import Instruction
|
||||||
|
from ..arguments import Argument
|
||||||
|
from typing import Callable, List
|
||||||
|
|
||||||
|
|
||||||
|
class FixedInstruction(Instruction):
|
||||||
|
def __init__(self, token: str, data: bytes):
|
||||||
|
argtypes = [[]]
|
||||||
|
self.data = data
|
||||||
|
self.token = token
|
||||||
|
super().__init__(token, argtypes)
|
||||||
|
|
||||||
|
def num_bytes(self, arguments) -> int:
|
||||||
|
return self.data
|
||||||
|
|
||||||
|
def to_bytes(
|
||||||
|
self,
|
||||||
|
arguments: List[Argument],
|
||||||
|
instruction_addr: int,
|
||||||
|
label_resolver: Callable[[str], int],
|
||||||
|
) -> bytes:
|
||||||
|
|
||||||
|
if len(arguments) != 0:
|
||||||
|
raise ValueError(f"Incorrect number of arguments for instruction {self.token}")
|
||||||
|
|
||||||
|
return self.data
|
||||||
|
|
||||||
|
|
||||||
|
class Ccf(FixedInstruction):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("CCF", bytes([0x10]))
|
||||||
|
|
||||||
|
|
||||||
|
class Cpl(FixedInstruction):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("CPL", bytes([0x10]))
|
||||||
|
|
||||||
|
|
||||||
|
class Daa(FixedInstruction):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("DAA", bytes([0x10]))
|
||||||
|
|
||||||
|
|
||||||
|
class Di(FixedInstruction):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("DI", bytes([0x10]))
|
||||||
|
|
||||||
|
|
||||||
|
class Ei(FixedInstruction):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("EI", bytes([0x10]))
|
||||||
|
|
||||||
|
|
||||||
|
class Halt(FixedInstruction):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("HALT", bytes([0x10]))
|
||||||
|
|
||||||
|
|
||||||
|
class Nop(FixedInstruction):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("NOP", bytes([0x00]))
|
||||||
|
|
||||||
|
|
||||||
|
class Ret(FixedInstruction):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("RET", bytes([0x10]))
|
||||||
|
|
||||||
|
|
||||||
|
class Reti(FixedInstruction):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("RETI", bytes([0x10]))
|
||||||
|
|
||||||
|
|
||||||
|
class Rla(FixedInstruction):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("RLA", bytes([0x10]))
|
||||||
|
|
||||||
|
|
||||||
|
class Rlca(FixedInstruction):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("RLCA", bytes([0x10]))
|
||||||
|
|
||||||
|
|
||||||
|
class Rra(FixedInstruction):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("RRCA", bytes([0x10]))
|
||||||
|
|
||||||
|
|
||||||
|
class Scf(FixedInstruction):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("SCF", bytes([0x10]))
|
||||||
|
|
||||||
|
|
||||||
|
class Stop(FixedInstruction):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("STOP", bytes([0x10]))
|
||||||
|
|
||||||
@@ -16,7 +16,6 @@ class Ld(Instruction):
|
|||||||
def encode_reg_16(self, register: Register16, immediate: Immediate16) -> bytes:
|
def encode_reg_16(self, register: Register16, immediate: Immediate16) -> bytes:
|
||||||
reg_dict = {"BC": 0x01, "DE": 0x11, "HL": 0x21, "SP": 0x31}
|
reg_dict = {"BC": 0x01, "DE": 0x11, "HL": 0x21, "SP": 0x31}
|
||||||
imm = int(immediate.value, 0)
|
imm = int(immediate.value, 0)
|
||||||
print("immediate", imm)
|
|
||||||
out = bytearray()
|
out = bytearray()
|
||||||
out.append(reg_dict[register.value])
|
out.append(reg_dict[register.value])
|
||||||
out.extend(imm.to_bytes(2, "little", signed=imm < 0))
|
out.extend(imm.to_bytes(2, "little", signed=imm < 0))
|
||||||
@@ -29,12 +28,6 @@ class Ld(Instruction):
|
|||||||
label_resolver: Callable[[str], int],
|
label_resolver: Callable[[str], int],
|
||||||
) -> bytes:
|
) -> bytes:
|
||||||
|
|
||||||
# print(arguments)
|
|
||||||
# print("Register16", type(arguments[0]))
|
|
||||||
# print("Immediate16", isinstance(arguments[1], Immediate16))
|
|
||||||
|
|
||||||
# if isinstance(arguments[0], Register16) and \
|
|
||||||
# isinstance(arguments[1], Immediate16):
|
|
||||||
return self.encode_reg_16(arguments[0], arguments[1])
|
return self.encode_reg_16(arguments[0], arguments[1])
|
||||||
|
|
||||||
raise TypeError("Unhandled argument types")
|
raise TypeError("Unhandled argument types")
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
from .Instruction import Instruction
|
|
||||||
from ..arguments import Argument
|
|
||||||
from typing import Callable, List
|
|
||||||
|
|
||||||
|
|
||||||
class Nop(Instruction):
|
|
||||||
def __init__(self):
|
|
||||||
argtypes = [[]]
|
|
||||||
super().__init__("NOP", argtypes)
|
|
||||||
|
|
||||||
def num_bytes(self, arguments) -> int:
|
|
||||||
return 1
|
|
||||||
|
|
||||||
def to_bytes(
|
|
||||||
self,
|
|
||||||
arguments: List[Argument],
|
|
||||||
instruction_addr: int,
|
|
||||||
label_resolver: Callable[[str], int],
|
|
||||||
) -> bytes:
|
|
||||||
|
|
||||||
if len(arguments) != 0:
|
|
||||||
raise ValueError("Incorrect number of arguments")
|
|
||||||
|
|
||||||
return bytes([0x00])
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
from .Instruction import Instruction
|
|
||||||
from ..arguments import Argument
|
|
||||||
from typing import Callable, List
|
|
||||||
|
|
||||||
|
|
||||||
class Stop(Instruction):
|
|
||||||
def __init__(self):
|
|
||||||
argtypes = [[]]
|
|
||||||
super().__init__("STOP", argtypes)
|
|
||||||
|
|
||||||
def num_bytes(self, arguments) -> int:
|
|
||||||
return 1
|
|
||||||
|
|
||||||
def to_bytes(
|
|
||||||
self,
|
|
||||||
arguments: List[Argument],
|
|
||||||
instruction_addr: int,
|
|
||||||
label_resolver: Callable[[str], int],
|
|
||||||
) -> bytes:
|
|
||||||
|
|
||||||
if len(arguments) != 0:
|
|
||||||
raise ValueError("Incorrect number of arguments")
|
|
||||||
|
|
||||||
return bytes([0x10])
|
|
||||||
Reference in New Issue
Block a user