; File: MOS6507.i
; Author: Neil Cafferkey
; ======================
; Translations for MOS 6507 microprocessor instructions.
;
; $VER: MOS6507.i 1.4 (12.10.99)
;

	ifnd	mos6507_i
mos6507_i	set	1


; 68020 Condition Codes
; =====================

extend		equ	4
negative	equ	3
zero		equ	2
overflow	equ	1
carry		equ	0




INTERRUPT_VECTOR	equ	$fffe
STACK_PAGE		equ	$0	; should be $100 for other systems



	machine	68020

	opt	0


ADDR_MASK	equ	$1fff	; Makes a 13-bit address


; Register definitions
; ====================

A	equr	d2
X	equr	d3
Y	equr	d4
P	equr	d5
D_FLAG	equr	d6
S	equr	d7

DATA	equr	a2
CLOCK	equr	a3	; this is an address register so that adding to it
			;	won't affect condition codes
ENV	equr	a4


; Macro to save status flags
; ==========================

	macro	SAVE_P
	move.w	ccr,d0
	and.b	#\1,d0		; clear irrelevant bits in ccr
	and.b	#~\1,P		; clear relevant bits in P
	or.b	d0,P		; move relevant bits to P
	endm


; Macro to restore status flags
; =============================

	macro	LOAD_P
	move.w	P,ccr
	endm


; Macro to restore status flags within a macro
; ============================================

	macro	LOAD_P_INTERNAL
	move.w	P,ccr
	endm


; Macro: ABI_CLK
; ==============
; Adds one extra to the clock cycle counter if a page boundary is crossed.

	macro	ABI_CLK

	ifgt	128-\1
	moveq	#\1,d0
	else
	move.l	#\1,d0
	endc

	add.b	\2,d0

	moveq	#0,d0
	roxl.b	#1,d0
	addq.b	\3,d0
	add.l	d0,CLOCK

	endm


; Macro: IIY_CLK
; ==============
; Adds one extra to the clock cycle counter if a page boundary is crossed.

	macro	IIY_CLK

	ifgt	128-\1
	moveq	#\1,d0
	else
	move.l	#\1,d0
	endc

	move.b	(\1,DATA),d0
	add.b	Y,d0

	moveq	#0,d0
	roxl.b	#1,d0
	addq.b	\2,d0
	add.l	d0,CLOCK

	endm


; Zero Page Indexed
; =================

	macro	_ZPI
	ifgt	128-\1
	moveq	#\1,d1
	else
	move.l	#\1,d1
	endc
	add.b	\2,d1
	endm


; Absolute Indexed
; ================

	macro	_ABI
	ifgt	128-\1
	moveq	#\1,d1
	else
	move.l	#\1,d1
	endc
	add.w	\2,d1
	andi.w	#ADDR_MASK,d1
	endm


; Indexed Indirect
; ================

	macro	_IIX
	move.l	#\1,d1			; Move offset to temp reg
	add.b	X,d1			; Add in index X
	move.w	(DATA,d1),d1
	rol.w	#8,d1			; Adjust Endianness
	andi.w	#ADDR_MASK,d1
	endm


; Indirect Indexed
; ================

	macro	_IIY
	move.w	(\1,DATA),d1
	rol.w	#8,d1
	add.w	Y,d1
	andi.w	#ADDR_MASK,d1
	endm


; Translation of `adc'
; ====================

; General translation

	macro	ADC_
	move.b	P,d1
	lsr.b	#1,d1
	tst.b	D_FLAG
	bne.s	\@1$		; if in BCD mode
	addx.b	d0,A
	bra.s	\@2$
\@1$:	move.b	A,d1
	abcd.b	d0,A

	eor.b	A,d1
	eor.b	A,d0
	and.b	d1,d0

	move.w	ccr,d1
	lsr.b	#7,d0
	lsl.b	#4,d1
	roxl.b	#1,d0

	tst.b	A
	move.w	ccr,d1
	or.b	d0,d1
	move.w	d1,ccr

\@2$:
	endm

; Immediate

	macro	ADC_IMM
	addq.l	#2,CLOCK
	move.b	\1,d0
	ADC_
	endm

; Zero Page

	macro	ADC_ZP
	addq.l	#3,CLOCK
	move.b	(\1,DATA),d0
	ADC_
	endm

; Zero Page Indexed

	macro	ADC_ZPI
	addq.l	#4,CLOCK
	_ZPI	\1,\2
	move.b	(DATA,d1),d0
	ADC_
	endm

; Absolute

	macro	ADC_AB
	move.b	(\1,DATA),d0
	ADC_
	addq.l	#4,CLOCK
	endm

; Absolute Indexed

	macro	ADC_ABI
	ABI_CLK	\1,\2,#4
	_ABI	\1,\2
	move.b	(DATA,d1),d0
	ADC_
	endm

; Indexed Indirect

	macro	ADC_IIX
	addq.l	#6,CLOCK
	_IIX	\1
	move.b	(DATA,d1),d0
	ADC_
	endm

; Indirect Indexed

	macro	ADC_IIY
	IIY_CLK	\1,#5
	_IIY	\1
	move.b	(DATA,d1.w),d0
	ADC_
	endm



; Translation of `and'
; ====================

; Immediate

	macro	AND_IMM
	addq.l	#2,CLOCK
	andi.b	\1,A
	endm

; Zero Page

	macro	AND_ZP
	addq.l	#3,CLOCK
	and.b	(\1,DATA),A
	endm

; Zero Page Indexed

	macro	AND_ZPI
	addq.l	#4,CLOCK
	_ZPI	\1,\2
	and.b	(DATA,d1),A
	endm

; Absolute

	macro	AND_AB
	addq.l	#4,CLOCK
	and.b	(\1,DATA),A
	endm

; Absolute Indexed

	macro	AND_ABI
	ABI_CLK	\1,\2,#4
	_ABI	\1,\2
	and.b	(DATA,d1),A
	endm

; Indexed Indirect

	macro	AND_IIX
	addq.l	#6,CLOCK
	_IIX	\1
	and.b	(DATA,d1),A
	endm

; Indirect Indexed

	macro	AND_IIY
	IIY_CLK	\1,#5
	_IIY	\1
	and.b	(DATA,d1.w),A
	endm


; Translation of `asl'
; ====================

; General translation

	macro	ASL_
	movem.w	(\1-1,DATA),d0
	asl.b	#1,d0
	movem.w	d0,(\1-1,DATA)
	endm

; Accumulator

	macro	ASL_ACC
	addq.l	#2,CLOCK
	asl.b	#1,A
	endm

; Zero Page

	macro	ASL_ZP
	addq.l	#5,CLOCK
	ASL_	\1
	endm

; Zero Page Indexed

	macro	ASL_ZPI
	addq.l	#6,CLOCK
	_ZPI	\1,\2
	movem.w	(-1,DATA,d1),d0
	asl.b	#1,d0
	movem.w	d0,(-1,DATA,d1)
	endm

; Absolute

	macro	ASL_AB
	addq.l	#6,CLOCK
	ASL_	\1
	endm

; Absolute Indexed

	macro	ASL_ABI
	addq.l	#7,CLOCK
	_ABI	\1,\2
	move.w	(-1,DATA,d1),d0
	asl.b	#1,d0
	movem.w	d0,(-1,DATA,d1)
	endm


; Translation of `bcc'
; ====================

	macro	BCC_
	move.w	P,ccr
	bcs.s	\@1$
	addq.l	#3+\2,CLOCK
	bra	\1
\@1$:	addq.l	#2,CLOCK
	endm


; Translation of `bcs'
; ====================

	macro	BCS_
	move.w	P,ccr
	bcc.s	\@1$
	addq.l	#3+\2,CLOCK
	bra	\1
\@1$:	addq.l	#2,CLOCK
	endm


; Translation of `beq'
; ====================

	macro	BEQ_
	move.w	P,ccr
	bne.s	\@1$
	addq.l	#3+\2,CLOCK
	bra	\1
\@1$:	addq.l	#2,CLOCK
	endm


; Translation of `bit'
; ====================

; General translation

	macro	BIT_
	moveq	#0,d1
	move.b	(\1,DATA),d0
	bpl.s	\@0$
	bset.b	#negative,d1
\@0$:	btst.b	#6,d0
	beq.s	\@1$
	bset.b	#overflow,d1
\@1$:	and.b	A,d0
	bne.s	\@2$
	bset.b	#zero,d1
\@2$:	move.w	d1,ccr

	endm

; Zero Page

	macro	BIT_ZP
	addq.l	#3,CLOCK
	BIT_	\1
	endm

; Absolute

	macro	BIT_AB
	addq.l	#4,CLOCK
	BIT_	\1
	endm


; Translation of `bmi'
; ====================

	macro	BMI_
	move.w	P,ccr
	bpl.s	\@1$
	addq.l	#3+\2,CLOCK
	bra	\1
\@1$:	addq.l	#2,CLOCK
	endm


; Translation of `bne'
; ====================

	macro	BNE_
	move.w	P,ccr
	beq.s	\@1$
	addq.l	#3+\2,CLOCK
	bra	\1
\@1$:	addq.l	#2,CLOCK
	endm


; Translation of `bpl'
; ====================

	macro	BPL_
	move.w	P,ccr
	bmi.s	\@1$
	addq.l	#3+\2,CLOCK
	bra	\1
\@1$:	addq.l	#2,CLOCK
	endm


; Translation of `brk'
; ====================

	macro	BRK_

	addq.l	#7,CLOCK

	subq.l	#2,sp

	subq.b	#2,S
	move.b	P,(STACK_PAGE,DATA,S)
	subq.b	#1,S

	INTERRUPT

	endm


; Translation of `bvc'
; ====================

	macro	BVC_
	move.w	P,ccr
	bvs.s	\@1$
	addq.l	#3+\2,CLOCK
	bra	\1
\@1$:	addq.l	#2,CLOCK
	endm


; Translation of `bvs'
; ====================

	macro	BVS_
	move.w	P,ccr
	bvc.s	\@1$
	addq.l	#3+\2,CLOCK
	bra	\1
\@1$:	addq.l	#2,CLOCK
	endm


; Translation of `clc'
; ====================

	macro	CLC_
	addq.l	#2,CLOCK
	moveq	#0,d0		; clear carry flag
	endm


; Translation of `cld'
; ====================

	macro	CLD_
	addq.l	#2,CLOCK
	moveq	#0,D_FLAG
	endm


; Translation of `cli' (not yet implemented)
; ====================

	macro	CLI_
	addq.l	#2,CLOCK
	endm


; Translation of `clv'
; ====================

	macro	CLV_
	addq.l	#2,CLOCK
	moveq	#0,d0		; clear overflow flag
	endm


; Translation of `cmp'
; ====================

; Immediate

	macro	CMP_IMM
	addq.l	#2,CLOCK
	cmpi.b	\1,A
	eori.b	#$01,ccr	; flip carry flag
	endm

; Zero Page

	macro	CMP_ZP
	addq.l	#3,CLOCK
	cmp.b	(\1,DATA),A
	eori.b	#$01,ccr	; flip carry flag
	endm

; Zero Page Indexed

	macro	CMP_ZPI
	_ZPI	\1,\2
	cmp.b	(DATA,d1),A
	addq.l	#4,CLOCK
	eori.b	#$01,ccr	; flip carry flag
	endm

; Absolute

	macro	CMP_AB
	addq.l	#4,CLOCK
	cmp.b	(\1,DATA),A
	eori.b	#$01,ccr	; flip carry flag
	endm

; Absolute Indexed

	macro	CMP_ABI
	ABI_CLK	\1,\2,#4
	_ABI	\1,\2
	cmp.b	(DATA,d1),A
	eori.b	#$01,ccr	; flip carry flag
	endm

; Indexed Indirect

	macro	CMP_IIX
	addq.l	#6,CLOCK
	_IIX	\1
	cmp.b	(DATA,d1),A
	eori.b	#$01,ccr	; flip carry flag
	endm

; Indirect Indexed

	macro	CMP_IIY
	IIY_CLK	\1,#5
	_IIY	\1
	cmp.b	(DATA,d1.w),A
	eori.b	#$01,ccr	; flip carry flag
	endm


; Translation of `cpx'
; ====================

; Immediate

	macro	CPX_IMM
	addq.l	#2,CLOCK
	cmpi.b	\1,X
	eori.b	#$01,ccr	; flip carry flag
	endm

; Zero Page

	macro	CPX_ZP
	addq.l	#3,CLOCK
	cmp.b	(\1,DATA),X
	eori.b	#$01,ccr	; flip carry flag
	endm

; Absolute

	macro	CPX_AB
	addq.l	#4,CLOCK
	cmp.b	(\1,DATA),X
	eori.b	#$01,ccr	; flip carry flag
	endm


; Translation of `cpy'
; ====================

; Immediate

	macro	CPY_IMM
	addq.l	#2,CLOCK
	cmpi.b	\1,Y
	eori.b	#$01,ccr	; flip carry flag
	endm

; Zero Page

	macro	CPY_ZP
	addq.l	#3,CLOCK
	cmp.b	(\1,DATA),Y
	eori.b	#$01,ccr	; flip carry flag
	endm

; Absolute

	macro	CPY_AB
	addq.l	#4,CLOCK
	cmp.b	(\1,DATA),Y
	eori.b	#$01,ccr	; flip carry flag
	endm


; Translation of `dec'
; ====================

; Zero Page

	macro	DEC_ZP
	addq.l	#5,CLOCK
	subq.b	#1,(\1,DATA)
	endm

; Zero Page Indexed

	macro	DEC_ZPI
	addq.l	#6,CLOCK
	_ZPI	\1,\2
	subq.b	#1,(DATA,d1)
	endm

; Absolute

	macro	DEC_AB
	addq.l	#6,CLOCK
	subq.b	#1,(\1,DATA)
	endm

; Absolute Indexed

	macro	DEC_ABI
	addq.l	#7,CLOCK
	_ABI	\1,\2
	subq.b	#1,(DATA,d1.w)
	endm

; Translation of `dex'
; ====================

	macro	DEX_
	addq.l	#2,CLOCK
	subq.b	#1,X
	endm


; Translation of `dey'
; ====================

	macro	DEY_
	addq.l	#2,CLOCK
	subq.b	#1,Y
	endm

; Translation of `eor'
; ====================

; Immediate

	macro	EOR_IMM
	addq.l	#2,CLOCK
	eori.b	\1,A
	endm

; Zero Page

	macro	EOR_ZP
	addq.l	#3,CLOCK
	move.b	(\1,DATA),d0
	eor.b	d0,A
	endm

; Zero Page Indexed

	macro	EOR_ZPI
	addq.l	#4,CLOCK
	_ZPI	\1,\2
	move.b	(DATA,d1),d0
	eor.b	d0,A
	endm

; Absolute

	macro	EOR_AB
	addq.l	#4,CLOCK
	move.b	(\1,DATA),d0
	eor.b	d0,A
	endm

; Absolute Indexed

	macro	EOR_ABI
	ABI_CLK	\1,\2,#4
	_ABI	\1,\2
	move.b	(DATA,d1),d0
	eor.b	d0,A
	endm

; Indexed Indirect

	macro	EOR_IIX
	addq.l	#6,CLOCK
	_IIX	\1
	move.b	(DATA,d1),d0
	eor.b	d0,A
	endm

; Indirect Indexed

	macro	EOR_IIY
	IIY_CLK	\1,#5
	_IIY	\1
	move.b	(DATA,d1.w),d0
	eor.b	d0,A
	endm


; Translation of `inc'
; ====================

; Zero Page

	macro	INC_ZP
	addq.l	#5,CLOCK
	addq.b	#1,(\1,DATA)
	endm

; Zero Page Indexed

	macro	INC_ZPI
	addq.l	#6,CLOCK
	_ZPI	\1,\2
	addq.b	#1,(DATA,d1)
	endm

; Absolute

	macro	INC_AB
	addq.l	#6,CLOCK
	addq.b	#1,(\1,DATA)
	endm

; Absolute Indexed

	macro	INC_ABI
	addq.l	#7,CLOCK
	_ABI	\1,\2
	addq.b	#1,(DATA,d1)
	endm


; Translation of `inx'
; ====================

	macro	INX_
	addq.l	#2,CLOCK
	addq.b	#1,X
	endm


; Translation of `iny'
; ====================

	macro	INY_
	addq.l	#2,CLOCK
	addq.b	#1,Y
	endm


; Translation of `jmp'
; ====================

; Absolute

	macro	JMP_AB
	addq.l	#3,CLOCK
	bra	\1
	endm

; Indirect (not yet implemented)

	macro	JMP_MI
	addq.l	#5,CLOCK
	bra	\1	; this is wrong
	endm


; Translation of `jsr'
; ====================

	macro	JSR_
	addq.l	#6,CLOCK
	subq.b	#2,S
	bsr	\1
	endm


; Translation of `lda'
; ====================

; Immediate

	macro	LDA_IMM
	addq.l	#2,CLOCK
	ifgt	128-\1
	moveq	#\1,A
	else
	move.b	#\1,A
	endif
	endm

; Zero Page

	macro	LDA_ZP
	move.b	(\1,DATA),A
	addq.l	#3,CLOCK
	endm

; Zero Page Indexed

	macro	LDA_ZPI
	_ZPI	\1,\2
	move.b	(DATA,d1),A
	addq.l	#4,CLOCK
	endm

; Absolute

	macro	LDA_AB
	move.b	(\1,DATA),A
	addq.l	#4,CLOCK
	endm

; Absolute Indexed

	macro	LDA_ABI
	ABI_CLK	\1,\2,#4
	_ABI	\1,\2
	move.b	(DATA,d1),A
	endm

; Indexed Indirect

	macro	LDA_IIX
	addq.l	#6,CLOCK
	_IIX	\1
	move.b	(DATA,d1),A
	endm

; Indirect Indexed

	macro	LDA_IIY
	IIY_CLK	\1,#5
	_IIY	\1
	move.b	(DATA,d1.w),A
	endm


; Translation of `ldx'
; ====================

; Immediate

	macro	LDX_IMM
	addq.l	#2,CLOCK
	ifgt	128-\1
	moveq	#\1,X
	else
	move.b	#\1,X
	endif
	endm

; Zero Page

	macro	LDX_ZP
	move.b	(\1,DATA),X
	addq.l	#3,CLOCK
	endm

; Zero Page Indexed

	macro	LDX_ZPI
	_ZPI	\1,\2
	move.b	(DATA,d1),X
	addq.l	#4,CLOCK
	endm

; Absolute

	macro	LDX_AB
	move.b	(\1,DATA),X
	addq.l	#4,CLOCK
	endm

; Absolute Indexed

	macro	LDX_ABI
	ABI_CLK	\1,\2,#4
	_ABI	\1,\2
	move.b	(DATA,d1),X
	endm

; Translation of `ldy'
; ====================

; Immediate

	macro	LDY_IMM
	addq.l	#2,CLOCK
	ifgt	128-\1
	moveq	#\1,Y
	else
	move.b	#\1,Y
	endif
	endm

; Zero Page

	macro	LDY_ZP
	move.b	(\1,DATA),Y
	addq.l	#3,CLOCK
	endm

; Zero Page Indexed

	macro	LDY_ZPI
	_ZPI	\1,\2
	move.b	(DATA,d1),Y
	addq.l	#4,CLOCK
	endm

; Absolute

	macro	LDY_AB
	move.b	(\1,DATA),Y
	addq.l	#4,CLOCK
	endm

; Absolute Indexed

	macro	LDY_ABI
	ABI_CLK	\1,\2,#4
	_ABI	\1,\2
	move.b	(DATA,d1),Y
	endm


; Translation of `lsr'
; ====================

; General translation

	macro	LSR_
	movem.w	(\1-1,DATA),d0
	lsr.b	#1,d0
	movem.w	d0,(\1-1,DATA)
	endm

; Accumulator

	macro	LSR_ACC
	addq.l	#2,CLOCK
	lsr.b	#1,A
	endm

; Zero Page

	macro	LSR_ZP
	addq.l	#5,CLOCK
	LSR_	\1
	endm

; Zero Page Indexed

	macro	LSR_ZPI
	addq.l	#6,CLOCK
	_ZPI	\1,\2
	movem.w	(-1,DATA,d1),d0
	lsr.b	#1,d0
	movem.w	d0,(-1,DATA,d1)
	endm

; Absolute

	macro	LSR_AB
	addq.l	#6,CLOCK
	LSR_	\1
	endm

; Absolute Indexed

	macro	LSR_ABI
	addq.l	#7,CLOCK
	_ABI	\1,\2
	move.w	(-1,DATA,d1),d0
	lsr.b	#1,d0
	movem.w	d0,(-1,DATA,d1)
	endm


; Translation of `nop'
; ====================

	macro	NOP_
	addq.l	#2,CLOCK
	endm


; Translation of `ora'
; ====================

; Immediate

	macro	ORA_IMM
	ori.b	\1,A
	addq.l	#2,CLOCK
	endm

; Zero Page

	macro	ORA_ZP
	or.b	(\1,DATA),A
	addq.l	#3,CLOCK
	endm

; Zero Page Indexed

	macro	ORA_ZPI
	_ZPI	\1,\2
	or.b	(DATA,d1),A
	addq.l	#4,CLOCK
	endm

; Absolute

	macro	ORA_AB
	or.b	(\1,DATA),A
	addq.l	#4,CLOCK
	endm

; Absolute Indexed

	macro	ORA_ABI
	ABI_CLK	\1,\2,#4
	_ABI	\1,\2
	or.b	(DATA,d1),A
	endm

; Indexed Indirect

	macro	ORA_IIX
	addq.l	#6,CLOCK
	_IIX	\1
	or.b	(DATA,d1),A
	endm

; Indirect Indexed

	macro	ORA_IIY
	IIY_CLK	\1,#5
	_IIY	\1
	or.b	(DATA,d1.w),A
	endm


; Translation of `pha'
; ====================

	macro	PHA_
	addq.l	#3,CLOCK
	subq.l	#2,sp
	move.b	A,(STACK_PAGE,DATA,S)
	subq.b	#1,S
	endm


; Translation of `php'
; ====================

	macro	PHP_
	addq.l	#3,CLOCK
	subq.l	#2,sp
	move.b	P,(STACK_PAGE,DATA,S)
	subq.b	#1,S
	endm


; Translation of `pla'
; ====================

	macro	PLA_
	addq.l	#4,CLOCK
	addq.l	#2,sp
	addq.b	#1,S
	move.b	(STACK_PAGE,DATA,S),A
	endm


; Translation of `plp'
; ====================

	macro	PLP_
	addq.l	#4,CLOCK
	addq.l	#2,sp
	addq.b	#1,S
	move.w	(STACK_PAGE-1,DATA,S),ccr
	endm


; Translation of `rol'
; ====================

; General translation

	macro	ROL_
	move.w	(\1-1,DATA),d0
	move.b	P,d1
	lsr.b	#1,d1
	roxl.b	#1,d0
	movem.w	d0,(\1-1,DATA)
	endm

; Accumulator

	macro	ROL_ACC
	addq.l	#2,CLOCK
	move.b	P,d1
	lsr.b	#1,d1
	roxl.b	#1,A
	endm

; Zero Page

	macro	ROL_ZP
	addq.l	#5,CLOCK
	ROL_	\1
	endm

; Zero Page Indexed

	macro	ROL_ZPI
	addq.l	#6,CLOCK
	_ZPI	\1,\2
	move.b	P,d0
	lsr.b	#1,d0
	movem.w	(-1,DATA,d1),d0
	roxl.b	#1,d0
	movem.w	d0,(-1,DATA,d1)
	endm

; Absolute

	macro	ROL_AB
	addq.l	#6,CLOCK
	ROL_	\1
	endm

; Absolute Indexed

	macro	ROL_ABI
	addq.l	#7,CLOCK	; used to be 6
	_ABI	\1,\2
	move.b	P,d0
	lsr.b	#1,d0
	movem.w	(-1,DATA,d1.w),d0
	roxl.b	#1,d0
	movem.w	d0,(-1,DATA,d1.w)
	endm


; Translation of `ror'
; ====================

; General translation

	macro	ROR_
	move.w	(\1-1,DATA),d0
	move.b	P,d1
	lsr.b	#1,d1
	roxr.b	#1,d0
	movem.w	d0,(\1-1,DATA)
	endm

; Accumulator

	macro	ROR_ACC
	addq.l	#2,CLOCK
	move.b	P,d1
	lsr.b	#1,d1
	roxr.b	#1,A
	endm

; Zero Page

	macro	ROR_ZP
	addq.l	#5,CLOCK
	ROR_	\1
	endm

; Zero Page Indexed

	macro	ROR_ZPI
	addq.l	#6,CLOCK
	_ZPI	\1,\2
	move.b	P,d0
	lsr.b	#1,d0
	movem.w	(-1,DATA,d1),d0
	roxr.b	#1,d0
	movem.w	d0,(-1,DATA,d1)
	endm

; Absolute

	macro	ROR_AB
	addq.l	#6,CLOCK
	ROR_	\1
	endm

; Absolute Indexed

	macro	ROR_ABI
	addq.l	#7,CLOCK	; used to be 6
	_ABI	\1,\2
	move.b	P,d0
	lsr.b	#1,d0
	movem.w	(-1,DATA,d1.w),d0
	roxr.b	#1,d0
	movem.w	d0,(-1,DATA,d1.w)
	endm


; Translation of `rti'
; ====================

	macro	RTI_

	addq.l	#6,CLOCK

	move.l	(sp)+,a0

	addq.l	#2,sp
	addq.b	#1,S
	move.b	(STACK_PAGE-1,DATA,S),P

	addq.b	#2,S

	move.w	P,ccr

	jmp	(a0)

	endm


; Translation of `rts'
; ====================

	macro	RTS_
	addq.l	#6,CLOCK
	addq.b	#2,S
	rts
	endm


; Translation of `sbc'
; ====================

; General translation

	macro	SBC_
	move.b	P,d1
	eori.b	#$01,d1
	lsr.b	#1,d1
	tst.b	D_FLAG
	bne.s	\@1$		; if in BCD mode
	subx.b	d0,A
	bra.s	\@2$
\@1$:	ori.b	#$04,ccr	; set zero flag
	sbcd.b	d0,A
\@2$:	eori.b	#$1,ccr		; flip carry bit
	endm

; Immediate

	macro	SBC_IMM
	addq.l	#2,CLOCK
	move.b	\1,d0
	SBC_
	endm

; Zero Page

	macro	SBC_ZP
	move.b	(\1,DATA),d0
	SBC_	\1
	addq.l	#3,CLOCK
	endm

; Zero Page Indexed

	macro	SBC_ZPI
	_ZPI	\1,\2
	move.b	(DATA,d1),d0
	SBC_
	addq.l	#4,CLOCK
	endm

; Absolute

	macro	SBC_AB
	move.b	(\1,DATA),d0
	SBC_
	addq.l	#4,CLOCK
	endm

; Absolute Indexed

	macro	SBC_ABI
	ABI_CLK	\1,\2,#4
	_ABI	\1,\2
	move.b	(DATA,d1),d0
	SBC_
	endm

; Indexed Indirect

	macro	SBC_IIX
	_IIX	\1
	move.b	(DATA,d1),d0
	SBC_
	addq.l	#6,CLOCK
	endm

; Indirect Indexed

	macro	SBC_IIY
	IIY_CLK	\1,#5
	_IIY	\1
	move.b	(DATA,d1.w),d0
	SBC_
	endm



; Translation of `sec'
; ====================

	macro	SEC_
	addq.l	#2,CLOCK
	move.b	#$01,ccr	; set carry bit
	endm


; Translation of `sed'
; ====================

	macro	SED_
	addq.l	#2,CLOCK
	moveq	#1,D_FLAG
	endm


; Translation of `sei'
; ====================

	macro	SEI_
	addq.l	#2,CLOCK
	endm


; Translation of `sta'
; ====================

; Zero Page

	macro	STA_ZP
	addq.l	#3,CLOCK
	move.b	A,(\1,DATA)
	endm

; Zero Page Indexed

	macro	STA_ZPI
	_ZPI	\1,\2
	move.b	A,(DATA,d1)
	addq.l	#4,CLOCK
	endm

; Absolute

	macro	STA_AB
	GET_ADDRESS	\1
	move.b	A,(ADDRESS,DATA)
	addq.l	#4,CLOCK
	endm

; Absolute Indexed

	macro	STA_ABI
	addq.l	#5,CLOCK
	_ABI	\1,\2
	move.b	A,(DATA,d1)
	endm

; Indexed Indirect

	macro	STA_IIX
	_IIX	\1
	move.b	A,(DATA,d1)
	addq.l	#6,CLOCK
	endm

; Indirect Indexed

	macro	STA_IIY
	_IIY	\1
	move.b	A,(DATA,d1.w)
	addq.l	#6,CLOCK
	endm


; Translation of `stx'
; ====================

; Zero Page

	macro	STX_ZP
	move.b	X,(\1,DATA)
	addq.l	#3,CLOCK
	endm

; Zero Page Indexed

	macro	STX_ZPI
	_ZPI	\1,\2
	move.b	X,(DATA,d1)
	addq.l	#4,CLOCK
	endm

; Absolute

	macro	STX_AB
	move.b	X,(\1,DATA)
	addq.l	#4,CLOCK
	endm


; Translation of `sty'
; ====================

; Zero Page

	macro	STY_ZP
	move.b	Y,(\1,DATA)
	addq.l	#3,CLOCK
	endm

; Zero Page Indexed

	macro	STY_ZPI
	_ZPI	\1,\2
	move.b	Y,(DATA,d1)
	addq.l	#4,CLOCK
	endm

; Absolute

	macro	STY_AB
	move.b	Y,(\1,DATA)
	addq.l	#4,CLOCK
	endm


; Translation of `tax'
; ====================

	macro	TAX_
	move.b	A,X
	addq.l	#2,CLOCK
	endm


; Translation of `tay'
; ====================

	macro	TAY_
	move.b	A,Y
	addq.l	#2,CLOCK
	endm


; Translation of `tsx'
; ====================

	macro	TSX_
	move.b	S,X
	addq.l	#2,CLOCK
	endm


; Translation of `txa'
; ====================

	macro	TXA_
	move.b	X,A
	addq.l	#2,CLOCK
	endm


; Translation of `txs'
; ====================

	macro	TXS_
	addq.l	#2,CLOCK

	sub.l	X,S
	lsl.l	#1,S
	suba.l	S,sp

	move.l	X,S
	endm


; Translation of `tya'
; ====================

	macro	TYA_
	move.b	Y,A
	addq.l	#2,CLOCK
	endm


	endc

