;------------------------------------------------------------------------- 
; This file contain definitions of some useful Macro-Instructions
; that can be called in source file placing the directive:
; .input "macro.asm"
;-------------------------------------------------------------------------  
macro   .def    084h
macro2  .def    085h

;-------------------------------------------------------------------------
; Function: rotate right mem/reg trough carry
; Example : rr w
; Comment : same as rrc but extended to all registers (excluding A)
;-------------------------------------------------------------------------
	.macro  rr      op1
	ld      macro2,a
	ld      a,op1
	rrc     a
	ld      op1,a
	ld      a,macro2
	.endm 
;-------------------------------------------------------------------------
; Function: rotate left mem/reg trough carry
; Example : rl w
; Comment : same as rlc but extended to all registers (excluding A)
;-------------------------------------------------------------------------
	.macro  rl      op1
	ld      macro,a
	ld      a,op1
	rlc     a
	ld      op1,a
	ld      a,macro
	.endm
;-------------------------------------------------------------------------
; Function: shift right mem/reg
; Example : sr w
; Comment : same as sra but extended to all registers (excluding A)
;-------------------------------------------------------------------------
	.macro  sr      op1
	ld      macro2,a
	ld      a,op1
	sra     a
	ld      op1,a
	ld      a,macro2
	.endm
;-------------------------------------------------------------------------
; Function: shift left mem/reg
; Example : sl w
; Comment : same as sla but extended to all registers (excluding A)
;-------------------------------------------------------------------------
	.macro  sl      op1
	ld      macro,a
	ld      a,op1
	sla     a
	ld      op1,a
	ld      a,macro
	.endm
;-------------------------------------------------------------------------
; Function: rotate right trough carry
; Example : rrc a
; Comment : operand can be only accumulator!
;-------------------------------------------------------------------------
	.macro  rrc     op1,?loop
	ldi     macro,8
loop    rlc     op1
	dec     macro
	jrnz    loop
	.endm
;-------------------------------------------------------------------------
; Function: shift right
; Example : sra a
; Comment : operand can be only accumulator!
;-------------------------------------------------------------------------
	.macro  sra     op1
	rrc     op1
	res     7,op1
	.endm
;-------------------------------------------------------------------------
; Function: logical or immediate
; Example : ori a,11h
; Comment : operands can be only accumulator and constant!
;-------------------------------------------------------------------------
	.macro  ori     op1,op2
	ldi     a,op2                   ;or =((negazione op1) and  (negazione
	com     a                       ;    op2)) e complemento
	ld      v,a
	com     op1
	and     op1,v                   ;      
	com     op1
	.endm
;-------------------------------------------------------------------------
; Function: logical or
; Example : or a,w
; Comment : operands can be only accumulator and mem/reg (excluding A)
;-------------------------------------------------------------------------
	.macro  or      op1,op2
	com     op1
	ld      macro,op1
	ld      a,op2
	com     a
	and     a,macro
	com     a
	.endm
;-------------------------------------------------------------------------
; Function: logical ex-or immediate
; Example : xri a,11h
; Comment : operands can be only accumulator and constant
;-------------------------------------------------------------------------
	.macro  xri     op1,op2
	ld      macro,op1
	com     op1
	andi    op1,op2
	ld      macro2,op1
	ld      op1,macro
	andi    op1,op2^0FFh
	or      op1,macro2
	.endm
;-------------------------------------------------------------------------
; Function: jump relative on zero flag
; Example : jrze label
; Comment : same as jrz but with no displacement limits
;-------------------------------------------------------------------------
	.macro  jrze    op1
	jrnz    $+3
	jp      op1
	.endm
;-------------------------------------------------------------------------
; Function: jump relative on non zero flag
; Example : jrnze label
; Comment : same as jrz but with no displacement limits
;-------------------------------------------------------------------------
	.macro  jrnze   op1
	jrz     $+3
	jp      op1
	.endm
;-------------------------------------------------------------------------
; Function: jump relative on carry flag
; Example : jrce label
; Comment : same as jrc but with no displacement limits
;-------------------------------------------------------------------------
	.macro  jrce    op1
	jrnc    $+3
	jp      op1
	.endm
;-------------------------------------------------------------------------
; Function: jump relative on non carry flag
; Example : jrnce label
; Comment : same as jrnc but with no displacement limits
;-------------------------------------------------------------------------
	.macro  jrnce   op1
	jrc     $+3
	jp      op1
	.endm
;-------------------------------------------------------------------------
; Function: jump relative on reset flag
; Example : jrre label
; Comment : same as jrr but with no displacement limits
;-------------------------------------------------------------------------
	.macro  jrre    op1,op2,op3
	jrs     op1,op2,$+5
	jp      op3
	.endm
;-------------------------------------------------------------------------
; Function: jump relative if set
; Example : jrse label
; Comment : same as jrs but with no displacement limits
;-------------------------------------------------------------------------
	.macro  jrse    op1,op2,op3
	jrr     op1,op2,$+5
	jp      op3
	.endm
;-------------------------------------------------------------------------
; Function: hang out uP
; Example : hang
; Comment : wait for NMI interrupt
;-------------------------------------------------------------------------
	.macro  hang
	call    step_by_step
	.endm
;-------------------------------------------------------------------------
; Function: load rom-window offset
; Example : lea x,rom_table
;           ld a,(x)
; Comment : first operand can be x or y
;-------------------------------------------------------------------------
	 .macro lea op1,op2
	 ldi     drwr,op2 >> 6
	 ldi     op1,(op2 & 111111b) + 40h
	 .endm
;-------------------------------------------------------------------------
; Function: waiting between IIC_bus operation
; Example : pause_iic
; Comment : reloading watchdog
;-------------------------------------------------------------------------
	.macro pause_iic ?pause
	ldi     macro,IIC_DELAY
pause   down
	dec     macro
	jrnz    pause
	.endm
;-------------------------------------------------------------------------
; Function: wait for end of A/D conversion
; Example : weoc
; Comment : warning! this waiting do not reload watchdog-counter
;-------------------------------------------------------------------------
	 .macro weoc ?eoc
eoc      jrr    6,adcr,eoc
	 .endm
;-------------------------------------------------------------------------
; Function: Reload watchdog
; Example : down
; Comment :
;-------------------------------------------------------------------------
	.macro down
	ldi     wdr,0Feh        ;time max (fe watchdog disabled)
	.endm
;------------------------------
