***************************************************************************
***************************************************************************
**   EDIS ADVANCE - ISSUES ADVANCE WORD (SAW)
**
**   (C) 2002 - B. A. Bowling And A. C. Grippo
**
**   This header must appear on all derivatives of this code.
**
***************************************************************************
***************************************************************************

.header 'MegaSquirt'
.pagewidth 130
.pagelength 90

.nolist
	include "MC68HC908QT4.equ"
.list
	org	RamStart
;	include "edisstim.h"


adreadlow: ds      1
adreadhigh: ds     1
toothcnt:    ds     1


initCONFIG1: equ    %00001111     ;Init. for Low Power Consumption
initCONFIG2: equ    %01000000	  ;Enable IRQ pin as IRQ, no pullup
initStack:   equ    RamLast       ;init. stack pointer to last RAM

SelectCH2AD: equ    %00000010	  ;initial ADSCR select and Read CH2
SelectCH3AD: equ    %00000011     ;initial ADSCR Select and Read CH3
Div2ADICLK  equ    %01000000     ;initial ADICLK = Bus/4
Timerstop    equ  %01110010     ;Stop timer, reset of timer counter, prescale for 1 usec
Timergo      equ  %01000010     ;Enable timer, prescale of 4 for 1 usec increment, interrupts
SetPWM       equ  %00011010     ;PWM, clear on compare, toggle state on overflow
SetPWMNoTog  equ  %00011000     ;PWM, clear on compare, no toggle on overflow
SetOCInitH   equ  %00000000     ; Clear output

***************************************************************************
**
** Main Routine Here - Initialization and main loop
**
***************************************************************************
	org	FlashStart
Start:
             clra                 ;Init. Acc. For simulation
             mov    #initCONFIG1,CONFIG1 ;initialize Config1
             mov    #initCONFIG2,CONFIG2 ;initialize Config2
             ldhx   #initStack+1  ;initialize the stack pointer
             txs
;             mov #$01,osctrim
;
;   Clear all RAM.
;
             ldhx   #RamStart     ;point to start of RAM
ClearRAM:
             clr    ,x            ;clear RAM location
             aix    #1            ;advance pointer
             cphx   #RamLast+1    ;done ?
             bne    ClearRAM      ;loop back if not


; Set up the timer
        MOV     #Timerstop,tsc        ; Stop Timer so it can be set up
        mov     #117T,TMODH	      ; full period
        mov     #48T,TMODL

        mov     #SetPWM,TSC0

        mov     #3T,TCH0H	      ; compare (duty)
        mov     #0T,TCH0L

        MOV     #Timergo,TSC          ;  set timer prescaler to divide by 4

;        mov     #Div2ADICLK,adiclk
        clr     toothcnt

        cli

***************************************************************************
********************    M A I N  E V E N T  L O O P     *******************
***************************************************************************

LOOPER:
        clrh
        mov    #SelectCH2AD,ADSCR ;Select channel2 PTA4
ADREAD:
        brclr  7,ADSCR,ADREAD ;Wait till read is complete

        lda    ADR           ;Load A with Analog Data Register
        tax
        lda     ADVANCEHIGHBYTES,x
        sta     adreadhigh
        lda     ADR
        tax
        lda     ADVANCELOWBYTES,x
        sta     adreadlow

        jmp       LOOPER

***************************************************************************


TIMER_ROLL:
	pshh

        lda     tsc
        MOV     #Timerstop,tsc        ; Stop Timer - let IRQ restart

	pulh
	rti

; PIP Command word receive - sets up timer with SAW pulsewidth
DOPIP:
	pshh

        lda     tsc        
        MOV     #Timerstop,tsc        ; Stop Timer so it can be set up

        mov     SetOCInitH,TSC0        
        mov     #SetPWMNoTog,TSC0

        mov     adreadhigh,TCH0H
        mov     adreadlow,TCH0L

;        lda     adreadlow
;        bne     NO_HIGH_OV_FLOW

        lda     adreadhigh
        sta     TMODH

        lda     adreadlow
        inca                            ; Increment the high byte because low byte is $FF
        sta     TMODL
        bra     RUN_THE_TIMER
                
NO_HIGH_OV_FLOW:
        lda     adreadhigh
        sta     TMODH
        lda     adreadlow
        inca
        sta     TMODL      

RUN_THE_TIMER:
        MOV     #Timergo,TSC          ;  set timer prescaler to divide by 4

	bset	ACK1,INTSCR	; Flush out any new interrupts pending
	bclr	IMASK1,INTSCR   ; Enable IRQ
        bset    IRQF1,INTSCR
	pulh
	rti


***************************************************************************
**
** Dummy ISR - just performs RTS
**
***************************************************************************
Dummy:			; Dummy vector - there just to keep the assembler happy
	rti



;
;*  Vectors  *********************************************************
;*

             org    Vadc
             fdb    Dummy         ;ADC vector
             fdb    Dummy         ;Keyboard vector
             org    Vtimov
             fdb    TIMER_ROLL    ;Timer overflow vector
             org    Vtimch1
             fdb    Dummy         ;Timer channel 1 vector
             fdb    Dummy         ;Timer channel 0 vector
             org    Virq
             fdb    DOPIP         ;IRQ vector
             fdb    Dummy         ;SWI vector
             fdb    Start         ;Reset vector

        org     $F500

;=======================================================================
; Advance value for timer output compare
; Equation is Period(ms) = 1536 - (25.4 * angle_in_degrees)
; Values generated in Excel, both high and low byte values
;

ADVANCEHIGHBYTES:
	db	5T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	4T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	3T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	2T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	1T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T

ADVANCELOWBYTES:
	db	0T
	db	250T
	db	245T
	db	240T
	db	235T
	db	230T
	db	225T
	db	220T
	db	215T
	db	209T
	db	204T
	db	199T
	db	194T
	db	189T
	db	184T
	db	179T
	db	174T
	db	168T
	db	163T
	db	158T
	db	153T
	db	148T
	db	143T
	db	138T
	db	133T
	db	128T
	db	122T
	db	117T
	db	112T
	db	107T
	db	102T
	db	97T
	db	92T
	db	87T
	db	81T
	db	76T
	db	71T
	db	66T
	db	61T
	db	56T
	db	51T
	db	46T
	db	40T
	db	35T
	db	30T
	db	25T
	db	20T
	db	15T
	db	10T
	db	5T
	db	0T
	db	250T
	db	245T
	db	240T
	db	235T
	db	230T
	db	225T
	db	220T
	db	215T
	db	209T
	db	204T
	db	199T
	db	194T
	db	189T
	db	184T
	db	179T
	db	174T
	db	168T
	db	163T
	db	158T
	db	153T
	db	148T
	db	143T
	db	138T
	db	133T
	db	128T
	db	122T
	db	117T
	db	112T
	db	107T
	db	102T
	db	97T
	db	92T
	db	87T
	db	81T
	db	76T
	db	71T
	db	66T
	db	61T
	db	56T
	db	51T
	db	46T
	db	40T
	db	35T
	db	30T
	db	25T
	db	20T
	db	15T
	db	10T
	db	5T
	db	0T
	db	250T
	db	245T
	db	240T
	db	235T
	db	230T
	db	225T
	db	220T
	db	215T
	db	209T
	db	204T
	db	199T
	db	194T
	db	189T
	db	184T
	db	179T
	db	174T
	db	168T
	db	163T
	db	158T
	db	153T
	db	148T
	db	143T
	db	138T
	db	133T
	db	128T
	db	122T
	db	117T
	db	112T
	db	107T
	db	102T
	db	97T
	db	92T
	db	87T
	db	81T
	db	76T
	db	71T
	db	66T
	db	61T
	db	56T
	db	51T
	db	46T
	db	40T
	db	35T
	db	30T
	db	25T
	db	20T
	db	15T
	db	10T
	db	5T
	db	255T
	db	250T
	db	245T
	db	240T
	db	235T
	db	230T
	db	225T
	db	220T
	db	215T
	db	209T
	db	204T
	db	199T
	db	194T
	db	189T
	db	184T
	db	179T
	db	174T
	db	168T
	db	163T
	db	158T
	db	153T
	db	148T
	db	143T
	db	138T
	db	133T
	db	127T
	db	122T
	db	117T
	db	112T
	db	107T
	db	102T
	db	97T
	db	92T
	db	87T
	db	81T
	db	76T
	db	71T
	db	66T
	db	61T
	db	56T
	db	51T
	db	46T
	db	40T
	db	35T
	db	30T
	db	25T
	db	20T
	db	15T
	db	10T
	db	5T
	db	255T
	db	250T
	db	245T
	db	240T
	db	235T
	db	230T
	db	225T
	db	220T
	db	215T
	db	209T
	db	204T
	db	199T
	db	194T
	db	189T
	db	184T
	db	179T
	db	174T
	db	168T
	db	163T
	db	158T
	db	153T
	db	148T
	db	143T
	db	138T
	db	133T
	db	127T
	db	122T
	db	117T
	db	112T
	db	107T
	db	102T
	db	97T
	db	92T
	db	87T
	db	81T
	db	76T
	db	71T
	db	66T
	db	61T
	db	56T
	db	51T
	db	46T
	db	40T
	db	35T
	db	30T
	db	25T
	db	20T
	db	15T
	db	10T
	db	5T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T
	db	0T

	end

