git @ Cat's Eye Technologies SixtyPical / bb7fbcd
Add Emitter framework, and 6502 Generator (will likely change.) Chris Pressey 6 years ago
2 changed file(s) with 156 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 class Word(object):
1 def __init__(self, value):
2 self.value = value
3
4 def size(self):
5 return 2
6
7 def serialize(self):
8 word = self.value
9 low = word & 255
10 high = (word >> 8) & 255
11 return chr(low) + chr(high)
12
13
14 class Label(object):
15 def __init__(self, name, addr=None):
16 self.name = name
17 self.addr = addr
18
19 def set_addr(self, addr):
20 self.addr = addr
21
22 def size(self):
23 return 2
24
25 def serialize(self):
26 if self.addr is None:
27 raise ValueError(self.addr)
28 return Word(self.addr).serialize()
29
30
31 class Emitter(object):
32 def __init__(self, addr):
33 self.accum = []
34 self.addr = addr
35 self.name_counter = 0
36
37 def gen(self, *things):
38 for thing in things:
39 if isinstance(thing, int):
40 if thing < -127 or thing > 255:
41 raise ValueError(thing)
42 if thing < 0:
43 thing += 256
44 self.accum.append(thing)
45 self.addr += 1
46 else:
47 self.accum.append(thing)
48 self.addr += thing.size()
49
50 def serialize(self, stream):
51 for thing in self.accum:
52 if isintance(thing, int):
53 stream.write(chr(thing))
54 else:
55 stream.write(thing.serialize())
56
57 def make_label(self, name=None):
58 if name is None:
59 name = 'label' + self.name_counter
60 self.name_counter += 1
61 return Label(name, addr=self.addr)
62
63 def resolve_label(self, label):
64 label.set_addr(self.addr)
0 """This is just a sketch for now."""
1
2 from sixtypical.emitter import Emitter, Word, Label
3
4
5 class Generator(object):
6 def __init__(self, addr):
7 self.emitter = Emitter(addr)
8
9 ### ld ###
10
11 def gen_lda_imm(self, b):
12 self.emitter.emit(0xa9, b)
13
14 def gen_lda_abs(self, addr):
15 self.emitter.emit(0xad, addr)
16
17 def gen_ldx_imm(self, b):
18 self.emitter.emit(0xa2, b)
19
20 def gen_ldx_abs(self, addr):
21 self.emitter.emit(0xae, addr)
22
23 def gen_tax(self):
24 self.emitter.emit(0xaa)
25
26 def gen_tay(self):
27 self.emitter.emit(0xa8)
28
29 def gen_txa(self):
30 self.emitter.emit(0x8a)
31
32 def gen_tya(self):
33 self.emitter.emit(0x98)
34
35 ### st ###
36
37 def gen_sta_abs(self, addr):
38 self.emitter.emit(0x8d, addr)
39
40 def gen_stx_abs(self, addr):
41 self.emitter.emit(0x8e, addr)
42
43 def gen_sty_abs(self, addr):
44 self.emitter.emit(0x8c, addr)
45
46 ### add ###
47
48 def gen_adc_imm(self, b):
49 self.emitter.emit(0x69, b)
50
51 def gen_adc_abs(self, addr):
52 self.emitter.emit(0x6d, addr)
53
54 ### sub ###
55
56 def gen_sbc_imm(self, b):
57 self.emitter.emit(0xe9, b)
58
59 def gen_sbc_abs(self, addr):
60 self.emitter.emit(0xed, addr)
61
62 ### inc ###
63
64 def gen_inc_abs(self, addr):
65 self.emitter.emit(0xee, addr)
66
67 def gen_inx(self):
68 self.emitter.emit(0xe8)
69
70 def gen_iny(self):
71 self.emitter.emit(0xc8)
72
73 ### dec ###
74
75 def gen_dec_abs(self, addr):
76 self.emitter.emit(0xce, addr)
77
78 def gen_dex(self):
79 self.emitter.emit(0xca)
80
81 def gen_dey(self):
82 self.emitter.emit(0x88)
83
84 ### and ###
85
86 def gen_and_imm(self, b):
87 self.emitter.emit(0x29, b)
88
89 def gen_and_abs(self, addr):
90 self.emitter.emit(0x2d, addr)