git @ Cat's Eye Technologies SixtyPical / e41dd1a
Attempt to add Zero Page addressing; the emitter may need rethink. Chris Pressey 4 years ago
5 changed file(s) with 52 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
4848 buffer pointed to is implemented with "indirect indexed" addressing, as in
4949
5050 LDA ($02), Y
51 STA ($02), Y
5152
5253 We will likely have a new mode of `copy` for this, like
5354
54 copy 100, [ptr] + y
55 copy ^buf, ptr // this is the only way to initialize a pointer
56 add ptr, 4 // ok, but only if it does not exceed buffer's size
57 ld y, 0 // you must set this to something yourself
58 copy [ptr] + y, byt // read memory through pointer, into byte
59 copy 100, [ptr] + y // write memory through pointer (still trashes a)
5560
56 where `ptr` is a user-defined storage location of `pointer` type, and `+ y`
57 is mandatory (and you can/should set it to zero yourself if you want.)
61 where `ptr` is a user-defined storage location of `pointer` type, and the
62 `+ y` part is mandatory.
5863
5964 This instruction will likely be unchecked, at least to start. Basically,
6065 this is to allow us to write to the `byte buffer[2048]` known as "the screen",
77 )
88 from sixtypical.emitter import Byte, Label, Offset, LowAddressByte, HighAddressByte
99 from sixtypical.gen6502 import (
10 Immediate, Absolute, AbsoluteX, AbsoluteY, Indirect, IndirectY, Relative,
10 Immediate, Absolute, AbsoluteX, AbsoluteY, ZeroPage, Indirect, IndirectY, Relative,
1111 LDA, LDX, LDY, STA, STX, STY,
1212 TAX, TAY, TXA, TYA,
1313 CLC, SEC, ADC, SBC, ROL, ROR,
298298 self.emitter.emit(LDA(Absolute(Offset(src_label, 1))))
299299 self.emitter.emit(STA(Absolute(Offset(dest_label, 1))))
300300 elif isinstance(src.type, BufferType) and isinstance(dest.type, PointerType):
301 raise NotImplementedError('zeropage')
301 # TODO: this maybe should not be the 'copy' opcode at all that means this
302 src_label = self.labels[src.name]
303 dest_label = self.labels[dest.name]
304 self.emitter.emit(LDA(Immediate(HighAddressByte(src_label))))
305 self.emitter.emit(STA(ZeroPage(dest_label)))
306 self.emitter.emit(LDA(Immediate(LowAddressByte(src_label))))
307 #self.emitter.emit(STA(ZeroPage(Offset(dest_label, 1))))
308 self.emitter.emit(STA(ZeroPage(dest_label)))
302309 elif isinstance(src.type, VectorType) and isinstance(dest.type, VectorType):
303310 src_label = self.labels[src.name]
304311 dest_label = self.labels[dest.name]
5959 def serialize_relative_to(self, addr):
6060 assert self.addr is not None, "unresolved label: %s" % self.name
6161 return Byte(self.addr - (addr + 2)).serialize()
62
63 def serialize_as_zero_page(self, offset=0):
64 assert self.addr is not None, "unresolved label: %s" % self.name
65 return Byte(self.addr + offset).serialize()
6266
6367 def __repr__(self):
6468 addrs = ', addr=%r' % self.addr if self.addr is not None else ''
5757 pass
5858
5959
60 class ZeroPage(AddressingMode):
61 def __init__(self, value):
62 assert isinstance(value, (Label, Offset))
63 self.value = value
64
65 def size(self):
66 return 1
67
68 def serialize(self, addr=None):
69 return self.value.serialize_as_zero_page()
70
71
6072 class Indirect(AddressingMode):
6173 def __init__(self, value):
6274 assert isinstance(value, Label)
7587 self.value = value
7688
7789 def size(self):
78 return 2
90 return 1
7991
8092 def serialize(self, addr=None):
8193 return self.value.serialize()
256268 AbsoluteX: 0xbd,
257269 AbsoluteY: 0xb9,
258270 IndirectY: 0xb1,
271 ZeroPage: 0xa5,
259272 }
260273
261274
333346 AbsoluteX: 0x9d,
334347 AbsoluteY: 0x99,
335348 IndirectY: 0x91,
349 ZeroPage: 0x85,
336350 }
337351
338352
340340 | goto bar
341341 | }
342342 = 00c0a0c84c06c060a2c860
343
344 Buffers and pointers.
345
346 > | buffer[2048] buf
347 > | pointer ptr : 254
348 > |
349 > | routine main
350 > | inputs buf
351 > | outputs buf, y
352 > | trashes a, z, n, ptr
353 > | {
354 > | ld y, 0
355 > | copy buf, ptr
356 > | // copy 123, [ptr] + y
357 > | }
358 > = 00c0a000a90b850dc0a9c0850ec060