;	WinSTon

;	MOVEP Dx,(d,Ay)
;	MOVEP (d,Ay),Dx

;	XNZVC
;	-----

PUBLIC _movep_dn_an_word
PUBLIC _movep_dn_an_long
PUBLIC _movep_an_dn_word
PUBLIC _movep_an_dn_long

;-----------------------------------------------------------------------
;Write byte 'bl' to 'ebp', check intercepts and leave 'ecx' alone
WriteByte_EBP PROC NEAR
	push	ecx
	INTERCEPT_CHECK_ADDR_WRITEBYTE					;Write 'bl' to 'ebp', corrupts 'ecx'
	pop		ecx
	ret
WriteByte_EBP ENDP

;-----------------------------------------------------------------------
ReadByte_EBP PROC NEAR
	INTERCEPT_CHECK_ADDR_READBYTE					;Read 'bl' from 'ebp' and ret
ReadByte_EBP ENDP

;-----------------------------------------------------------------------
;MOVEP.W Dx,(d,Ay) (OpCode 0x100)
_movep_dn_an_word PROC NEAR
	mov		edx,DECODE_2[ecx]						;Get address Ay
	mov		ebp,[edx]
	mov		bx,IMM_WORD[esi]						;Read offset word
	add		esi,SIZE_WORD
	SWAP_ENDIAN_WORD_BX
	movsx	ebx,bx									;Sign extend
	add		ebp,ebx									;(d,Ay)
	MASK_24BIT_ADDR(ebp)
	mov		[MovePAddr],ebp							;Store for later

	mov		edx,DECODE_1[ecx]
	mov		ecx,[edx]								;Dx

	mov		bl,ch									;Write bytes into memory
	call	WriteByte_EBP

	mov		ebp,[MovePAddr]
	add		ebp,2
	mov		bl,cl
	call	WriteByte_EBP

	FRET
_movep_dn_an_word ENDP

;MOVEP.L Dx,(d,Ay) (OpCode 0x101)
_movep_dn_an_long PROC NEAR
	mov		edx,DECODE_2[ecx]						;Get address Ay
	mov		ebp,[edx]
	mov		bx,IMM_WORD[esi]						;Read offset word
	add		esi,SIZE_WORD
	SWAP_ENDIAN_WORD_BX
	movsx	ebx,bx									;Sign extend
	add		ebp,ebx									;(d,Ay)
	MASK_24BIT_ADDR(ebp)
	mov		[MovePAddr],ebp							;Store for later

	mov		edx,DECODE_1[ecx]
	mov		ecx,[edx]								;Dx

	rol		ecx,16
	mov		bl,ch									;Write bytes into memory
	call	WriteByte_EBP

	mov		ebp,[MovePAddr]
	add		ebp,2
	mov		bl,cl
	call	WriteByte_EBP

	rol		ecx,16									;(Back to original endian)
	mov		ebp,[MovePAddr]
	add		ebp,4
	mov		bl,ch
	call	WriteByte_EBP

	mov		ebp,[MovePAddr]
	add		ebp,6
	mov		bl,cl
	call	WriteByte_EBP

	FRET
_movep_dn_an_long ENDP

;-----------------------------------------------------------------------
;MOVEP.W (d,Ay),Dx
_movep_an_dn_word PROC NEAR
	mov		edx,DECODE_2[ecx]						;Get address Ay
	mov		ebp,[edx]
	mov		bx,IMM_WORD[esi]						;Read offset word
	add		esi,SIZE_WORD
	SWAP_ENDIAN_WORD_BX
	movsx	ebx,bx									;Sign extend
	add		ebp,ebx									;(d,Ay)
	MASK_24BIT_ADDR(ebp)
	mov		[MovePAddr],ebp							;Store for later

	mov		ebx,DECODE_1[ecx]						;Get address of Dx
	push	ebx

	call	ReadByte_EBP
	mov		ch,bl

	mov		ebp,[MovePAddr]
	add		ebp,2
	call	ReadByte_EBP
	mov		cl,bl

	pop		ebx
	mov		WORD PTR [ebx],cx						;And store

	FRET
_movep_an_dn_word ENDP

;MOVEP.L (d,Ay),Dx
_movep_an_dn_long PROC NEAR
	mov		edx,DECODE_2[ecx]						;Get address Ay
	mov		ebp,[edx]
	mov		bx,IMM_WORD[esi]						;Read offset word
	add		esi,SIZE_WORD
	SWAP_ENDIAN_WORD_BX
	movsx	ebx,bx									;Sign extend
	add		ebp,ebx									;(d,Ay)
	MASK_24BIT_ADDR(ebp)
	mov		[MovePAddr],ebp							;Store for later

	mov		ebx,DECODE_1[ecx]						;Get address of Dx
	push	ebx

	call	ReadByte_EBP							;Read bytes into register
	mov		ch,bl
		
	mov		ebp,[MovePAddr]
	add		ebp,2
	call	ReadByte_EBP
	mov		cl,bl
	shl		ecx,16

	mov		ebp,[MovePAddr]
	add		ebp,4
	call	ReadByte_EBP
	mov		ch,bl
		
	mov		ebp,[MovePAddr]
	add		ebp,6
	call	ReadByte_EBP
	mov		cl,bl

	pop		ebx
	mov		[ebx],ecx								;And store

	FRET
_movep_an_dn_long ENDP
