Add support for LD of 16-bit immediates

This commit is contained in:
2019-12-28 16:54:59 -08:00
parent 7ff5021236
commit 1e8a72d826
3 changed files with 129 additions and 2 deletions

View File

@@ -10,6 +10,7 @@ from .instructions.dec import Dec
from .instructions.nop import Nop from .instructions.nop import Nop
from .instructions.stop import Stop from .instructions.stop import Stop
from .instructions.jr import Jr from .instructions.jr import Jr
from .instructions.ld import Ld
from .arguments import ArgumentType, Argument from .arguments import ArgumentType, Argument
from typing import Callable, Dict, List, Optional from typing import Callable, Dict, List, Optional
@@ -24,7 +25,8 @@ GB_INSTRUCTIONS = [
Stop(), Stop(),
Inc(), Inc(),
Dec(), Dec(),
Jr() Jr(),
Ld(),
] ]
def build_instruction_map() -> Dict[str, Instruction]: def build_instruction_map() -> Dict[str, Instruction]:
@@ -124,7 +126,6 @@ def assemble(lines: str) -> bytes:
raise ValueError("Failed to parse line %s,\n%s", line_num, line) raise ValueError("Failed to parse line %s,\n%s", line_num, line)
byte_offset += parse_line_size(instruction, args) byte_offset += parse_line_size(instruction, args)
if step == 'SIZE': if step == 'SIZE':
logger.info("Program size: %s bytes", byte_offset) logger.info("Program size: %s bytes", byte_offset)
logger.debug("Found labels: %s", labels) logger.debug("Found labels: %s", labels)

View File

@@ -0,0 +1,45 @@
from .Instruction import Instruction
from ..arguments.ArgumentTypes import Register16, Immediate16
from ..arguments import Argument
from typing import Callable, List
class Ld(Instruction):
def __init__(self):
argtypes = [[Register16(), Immediate16()]]
super().__init__("LD", argtypes)
def num_bytes(self, arguments) -> int:
# the instruction_address and label_resolver are unused
return len(self.to_bytes(arguments, 0, lambda x: None))
def encode_reg_16(self,
register: Register16,
immediate: Immediate16) -> bytes:
reg_dict = {
"BC": 0x01,
"DE": 0x11,
"HL": 0x21,
"SP": 0x31
}
imm = int(immediate.value, 0)
print("immediate", imm)
out = bytearray()
out.append(reg_dict[register.value])
out.extend(imm.to_bytes(2, "little", signed=imm < 0))
return bytes(out)
def to_bytes(self,
arguments: List[Argument],
instruction_address: int,
label_resolver: Callable[[str], int]) -> 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])
raise TypeError("Unhandled argument types")

View File

@@ -0,0 +1,81 @@
---
name: ld_bc
program: |
LD BC 0
expected:
- 0x01
- 0x00
- 0x00
---
name: ld_de
program: |
LD DE 0
expected:
- 0x11
- 0x00
- 0x00
---
name: ld_hl
program: |
LD HL 0
expected:
- 0x21
- 0x00
- 0x00
---
name: ld_sp
program: |
LD SP 0
expected:
- 0x31
- 0x00
- 0x00
---
name: ld_16_max
program: |
LD BC 65535
expected:
- 0x01
- 0xFF
- 0xFF
---
name: ld_16_negative
program: |
LD BC -1
expected:
- 0x01
- 0xFF
- 0xFF
---
name: ld_16_min
program: |
LD BC -32768
expected:
- 0x01
- 0x00
- 0x80
---
name: ld_16_00FF
program: |
LD BC 0x00FF
expected:
- 0x01
- 0xFF
- 0x00
---
name: ld_16_FF00
program: |
LD BC 0xFF00
expected:
- 0x01
- 0x00
- 0xFF
---
name: ld_16_100
program: |
LD BC 10
expected:
- 0x01
- 0x0A
- 0x00