1
0
mirror of https://github.com/catseye/SixtyPical.git synced 2025-04-04 19:29:28 +00:00

Add Emitter framework, and 6502 Generator (will likely change.)

This commit is contained in:
Chris Pressey 2015-10-16 22:59:35 +01:00
parent 20ff255b40
commit bb7fbcda6c
2 changed files with 156 additions and 0 deletions

65
src/sixtypical/emitter.py Normal file
View File

@ -0,0 +1,65 @@
class Word(object):
def __init__(self, value):
self.value = value
def size(self):
return 2
def serialize(self):
word = self.value
low = word & 255
high = (word >> 8) & 255
return chr(low) + chr(high)
class Label(object):
def __init__(self, name, addr=None):
self.name = name
self.addr = addr
def set_addr(self, addr):
self.addr = addr
def size(self):
return 2
def serialize(self):
if self.addr is None:
raise ValueError(self.addr)
return Word(self.addr).serialize()
class Emitter(object):
def __init__(self, addr):
self.accum = []
self.addr = addr
self.name_counter = 0
def gen(self, *things):
for thing in things:
if isinstance(thing, int):
if thing < -127 or thing > 255:
raise ValueError(thing)
if thing < 0:
thing += 256
self.accum.append(thing)
self.addr += 1
else:
self.accum.append(thing)
self.addr += thing.size()
def serialize(self, stream):
for thing in self.accum:
if isintance(thing, int):
stream.write(chr(thing))
else:
stream.write(thing.serialize())
def make_label(self, name=None):
if name is None:
name = 'label' + self.name_counter
self.name_counter += 1
return Label(name, addr=self.addr)
def resolve_label(self, label):
label.set_addr(self.addr)

91
src/sixtypical/gen6502.py Normal file
View File

@ -0,0 +1,91 @@
"""This is just a sketch for now."""
from sixtypical.emitter import Emitter, Word, Label
class Generator(object):
def __init__(self, addr):
self.emitter = Emitter(addr)
### ld ###
def gen_lda_imm(self, b):
self.emitter.emit(0xa9, b)
def gen_lda_abs(self, addr):
self.emitter.emit(0xad, addr)
def gen_ldx_imm(self, b):
self.emitter.emit(0xa2, b)
def gen_ldx_abs(self, addr):
self.emitter.emit(0xae, addr)
def gen_tax(self):
self.emitter.emit(0xaa)
def gen_tay(self):
self.emitter.emit(0xa8)
def gen_txa(self):
self.emitter.emit(0x8a)
def gen_tya(self):
self.emitter.emit(0x98)
### st ###
def gen_sta_abs(self, addr):
self.emitter.emit(0x8d, addr)
def gen_stx_abs(self, addr):
self.emitter.emit(0x8e, addr)
def gen_sty_abs(self, addr):
self.emitter.emit(0x8c, addr)
### add ###
def gen_adc_imm(self, b):
self.emitter.emit(0x69, b)
def gen_adc_abs(self, addr):
self.emitter.emit(0x6d, addr)
### sub ###
def gen_sbc_imm(self, b):
self.emitter.emit(0xe9, b)
def gen_sbc_abs(self, addr):
self.emitter.emit(0xed, addr)
### inc ###
def gen_inc_abs(self, addr):
self.emitter.emit(0xee, addr)
def gen_inx(self):
self.emitter.emit(0xe8)
def gen_iny(self):
self.emitter.emit(0xc8)
### dec ###
def gen_dec_abs(self, addr):
self.emitter.emit(0xce, addr)
def gen_dex(self):
self.emitter.emit(0xca)
def gen_dey(self):
self.emitter.emit(0x88)
### and ###
def gen_and_imm(self, b):
self.emitter.emit(0x29, b)
def gen_and_abs(self, addr):
self.emitter.emit(0x2d, addr)