git @ Cat's Eye Technologies SixtyPical / 07541d7
Compile code for saving a, x, or y on the stack. Chris Pressey 3 years ago
3 changed file(s) with 61 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
00 # encoding: UTF-8
11
2 from sixtypical.ast import Program, Routine, Block, Instr, SingleOp, If, Repeat, For, WithInterruptsOff
2 from sixtypical.ast import Program, Routine, Block, Instr, SingleOp, If, Repeat, For, WithInterruptsOff, Save
33 from sixtypical.model import (
44 ConstantRef, LocationRef, IndexedRef, IndirectRef, AddressRef,
55 TYPE_BIT, TYPE_BYTE, TYPE_WORD,
1111 Immediate, Absolute, AbsoluteX, AbsoluteY, ZeroPage, Indirect, IndirectY, Relative,
1212 LDA, LDX, LDY, STA, STX, STY,
1313 TAX, TAY, TXA, TYA,
14 PHA, PLA,
1415 CLC, SEC, ADC, SBC, ROL, ROR,
1516 INC, INX, INY, DEC, DEX, DEY,
1617 CMP, CPX, CPY, AND, ORA, EOR,
168169 return self.compile_for(instr)
169170 elif isinstance(instr, WithInterruptsOff):
170171 return self.compile_with_interrupts_off(instr)
172 elif isinstance(instr, Save):
173 return self.compile_save(instr)
171174 else:
172175 raise NotImplementedError
173176
612615 self.emitter.emit(SEI())
613616 self.compile_block(instr.block)
614617 self.emitter.emit(CLI())
618
619 def compile_save(self, instr):
620 location = instr.locations[0]
621 if location == REG_A:
622 self.emitter.emit(PHA())
623 self.compile_block(instr.block)
624 self.emitter.emit(PLA())
625 elif location == REG_X:
626 self.emitter.emit(TXA())
627 self.emitter.emit(PHA())
628 self.compile_block(instr.block)
629 self.emitter.emit(PLA())
630 self.emitter.emit(TAX())
631 elif location == REG_Y:
632 self.emitter.emit(TYA())
633 self.emitter.emit(PHA())
634 self.compile_block(instr.block)
635 self.emitter.emit(PLA())
636 self.emitter.emit(TAY())
637 else:
638 raise NotImplementedError
305305 }
306306
307307
308 class PHA(Instruction):
309 opcodes = {
310 Implied: 0x48,
311 }
312
313
314 class PLA(Instruction):
315 opcodes = {
316 Implied: 0x68,
317 }
318
319
308320 class ROL(Instruction):
309321 opcodes = {
310322 Implied: 0x2a, # Accumulator
578578 = $0815 BNE $080F
579579 = $0817 RTS
580580
581 Compiling `save`.
582
583 | routine main
584 | inputs a
585 | outputs a
586 | trashes z, n
587 | {
588 | save a {
589 | save x {
590 | ld a, 0
591 | ld x, 1
592 | }
593 | }
594 | }
595 = $080D PHA
596 = $080E TXA
597 = $080F PHA
598 = $0810 LDA #$00
599 = $0812 LDX #$01
600 = $0814 PLA
601 = $0815 TAX
602 = $0816 PLA
603 = $0817 RTS
604
581605 Indexed access.
582606
583607 | byte one