		CpuMhz = 50
		CpuPins = 28	;=18,28,48, or 52
		CpuLongDate = 0	;=0 for old 4 digit date code, =1 for new "A" 8 digit date code
		CpuMode = 0	;=0 for debug, =1 for full speed
		DebugMemSpi=1	;=1 for debug, =0 for normal

IF CpuPins = 18 
 IF CpuLongDate = 1
		device	SX18L, turbo, stackx, optionx, carryx
 ELSE
		device	pins18, pages1, banks8, turbo, stackx, optionx, carryx
  ENDIF
 ENDIF

IF CpuPins = 28
 IF CpuLongDate = 1
		device	SX28L, turbo, stackx, optionx, carryx
 ELSE
		device	pins28, pages2, banks8, turbo, stackx, optionx, carryx
  ENDIF
 ENDIF

IF CpuPins = 52 or CpuPins = 48
 IF CpuLongDate = 1
		error 'A longdate SX48/52 did not exist at the time this ap was written'
 ELSE
		device 	DRTOFF, TURBO, STACKX, OPTIONX, CARRYX
  ENDIF
 ENDIF

IF CpuMode = 1
 IF CpuLongDate = 1
		device	OSCXTMAX
 ELSE
		device	oschs			;full speed operation
  ENDIF
ELSE
		device	oscrc			;debug operation
 ENDIF

IF CpuMhz = 50
		freq	50_000_000
ENDIF
IF CpuMhz = 100
		freq	100_000_000
ENDIF

IF CpuPins > 18
 IF CpuPins > 28
GPRegOrg	=	$0A	;$0A to $0F - limited to 6 bytes - global
 ELSE
GPRegOrg	=	8	;$08 to $0F - limited to 8 bytes - global
 ENDIF
ELSE
GPRegOrg	=	7	;$07 to $0F - limited to 9 bytes - global
ENDIF

		id	'CENTRONX'

IF CpuPins = 18 
		error 'this application requires at least 28 pins'
ENDIF

		reset	reset_entry

;EQUATES *************************************************************************
OptRTCisW	=	%01111111	;And with Opts to make register 1 show W
OptRTCEnable	=	%10111111	;And with Opts to enable rtcc interrupt
OptRTCInternal	=	%11011111	;And with Opts to make rtcc internal
OptRTCPrescale	=	%11110111	;And with Opts to enable rtcc prescaler

;base Options. Last 3 bits are divider.
;000=1:2, 001=1:4, 010=1:8, 011=1:16, 100=1:32, 101=1:64, 110=1:128, 111=1:256

IF CpuMhz = 100	
;50 cycles per interrupt. 400khz I2C via ISR. PP at .6us strobe via ISR
Opts		=	%11111000	
myOpts		=	Opts & OptRTCEnable & OptRTCInternal
IntPeriod	=	50		;will be subtracted from 256 to inc RTCC
ENDIF

IF CpuMhz = 50	
;120 cycles per interrupt. 200khz I2C via ISR. PP at .5us strobe via delay loops
;416,666 Hz interrupt rate 0.000,002,400 seconds per interrupt
Opts		=	%11111001	;prescale 1:4
IntPeriod	=	30		;will be subtracted from 256 to inc RTCC
myOpts		=	Opts & OptRTCEnable & OptRTCInternal & OptRTCPrescale &OptRTCisW
ENDIF

WKPND_B		=	$09
WKED_B		=	$0A
WKEN_B		=	$0B
TRIS		=	$1F

;MACROS --------------------------------------------------------------------------
Subroutine MACRO
	IF ($/$100)//2 = 1
	org	(($/$100)+1)*$100
;	org $200
	ENDIF
	ENDM


in	EQU	$F00
out	EQU	$FFF
pull	EQU	$E00
float	EQU	$EFF
cmos	EQU	$D00
ttl	EQU	$DFF

porthelp MACRO
	ERROR 'USAGE: port r[a,b,c,d,e] [in,out,pull,float,cmos,ttl] bits'
	ENDM

_PortMode = $1F

PortMode MACRO 1
; IF _PortMode <> \1
  _PortMode = \1
	mov	w,#_PortMode
	mov	m,w
;  ENDIF
 ENDM

port	MACRO	3
	noexpand
IF \1=RA OR \1=RB OR \1=RC OR \1=RD OR \1=RE
ELSE
	porthelp
ENDIF
IF \2=in OR \2=out OR \2=pull OR \2=float OR \2=cmos OR \2=ttl
ELSE
	porthelp
ENDIF
	expand
	PortMode (\2 / $100) | $10
	_PortMask = (\2//$100)^\3
	 mov !\1, #_PortMask

	noexpand
	ENDM

mynop	MACRO
	noexpand
	page $
	ENDM

usec	EQU	-6
msec	EQU	-3
sec	EQU	1
cycles	EQU	0

cyclefor MACRO 1
_cycles = \1
IF _cycles > 0
_temp	=	$//4
IF _temp = 2
 IF _cycles < 5 
  REPT	_cycles
	expand
	 mynop
	noexpand
  ENDR
_cycles = 0
 ELSE
	expand
	 mynop
	noexpand
_cycles = _cycles -1
  ENDIF
 ENDIF
IF _temp = 1
 IF _cycles < 6 
  REPT	_cycles
	expand
	 mynop
	noexpand
   ENDR
_cycles = 0
 ELSE
	expand
	 mynop
	 mynop
	noexpand
_cycles = _cycles -2
  ENDIF
 ENDIF
IF _cycles > 3
	expand
	 mov w, #_cycles / 3
	 decsz 1
	 clrb 2.1
	noexpand
 _cycles = _cycles // 3 ;cycles left over
 ENDIF
IF _cycles > 0
  REPT	_cycles
	expand
	 mynop
	noexpand
   ENDR
 ENDIF
ENDIF
	ENDM

delayhelp MACRO
	ERROR 'USAGE: delay value, [usec,msec,sec,cycles]'
	ENDM

delay	MACRO	2
noexpand
;Calculates cycles from delay value and units (milli seconds, micro seconds, or seconds)
;calls cyclefor to delay that number of cycles
IF (\2=usec OR \2=msec OR \2=sec) AND (\1<1000 AND \1>0)
 IF \2=sec
  _cycles = (\1 * 1000000 / (1000/CpuMhz))
  ENDIF
 IF \2=msec
  _cycles = (\1 * 1000 / (1000/CpuMhz))
  ENDIF
 IF \2=usec
  _cycles = (\1 / (1000/CpuMhz))
  ENDIF
 IF \2=cycles
  _cycles = \2
  ENDIF
 IF _cycles = 0
  expand
	 ;delay less than one cycle at this processor speed'
  noexpand
 ELSE
  IF _cycles > 255
   REPT (_cycles / 256)
    cyclefor 256
    _cycles = _cycles - 256
    ENDR
   ENDIF
  cyclefor _cycles
  ENDIF

ELSE
	delayhelp
ENDIF
	ENDM

eq	equ	0
lt	equ	1
ne	equ	2
gt	equ	3

repeat	MACRO
RepeatLabel	=	$
	ENDM

until	MACRO 3
	expand
	stc
	mov	w,\1
	mov	w,\3-w
targetPg = (RepeatLabel)/$200
basePg = ($+2)/$200
	noexpand
IF (RepeatLabel)/$200 <> ($+2)/$200
	expand
	page	RepeatLabel
	noexpand
ENDIF
IF \2 = eq
	expand
	sz
	noexpand
ENDIF
IF \2 = ne
	Expand
	snz
	noexpand
ENDIF
IF \2 = lt
	expand
	snc
	noexpand
ENDIF
IF \2 = gt
	expand
	sc
	noexpand
ENDIF
	expand
	jmp	RepeatLabel
	noexpand
	ENDM

ifthen	MACRO 4
	expand
	stc
	mov	w,\1
	mov	w,\3-w
	page	\4
	noexpand
IF \2 = eq
	expand
	sz
	noexpand
ENDIF
IF \2 = ne
	Expand
	snz
	noexpand
ENDIF
IF \2 = lt
	expand
	snc
	noexpand
ENDIF
IF \2 = gt
	expand
	sc
	noexpand
ENDIF
	expand
	jmp	\4
	noexpand
	ENDM


reset_entry
	org $1fb
test:
	repeat
	nop
	until status, gt, fsr

	ifthen	status, lt, fsr, testfwd

testfwd	nop