;****************************************************************************************
;*   This file is the firmware to implement 
;*	 1 timer, which on expiration can trigger 16 different single event routines
;*      furthermore the possibility of insertion of code into periodic routines 
;*	which occur every  8 milliseconds, 2 seconds and 16 seconds
;*
;*   Refer to the MPASM User's Guide for additional information on     			*
;*   features of the assembler (Document DS33014).                     			*
;*                                                                     			*
;*   Refer to the respective PICmicro data sheet for additional        			*
;*   information on the instruction set.                               			*
;*                                                                     			*
;****************************************************************************************	
;*                                                                 			*
;*    Filename:	    MICROCHIP OS for 10F200  .asm                              		                *
;*    Date:         December, 2009                                    			*
;*    File Version: A, this is a new file                                    		*
;*                                                                     			*
;*    Author:       G Michael Drygas                                      		*
;*                  Applications Engineer                    				*
;*    Company:      MICHAELS Unternehmergesellschaft(haftungsbeschrnkt)                *
;*                  EU TAX ID  263676163                               			*
;****************************************************************************************
;*                                			                                *
;*    Files required:   m0-3.asm (macros)                                          		*
;*                                                                     			*
;*                                                                     			*
;*                                                                     			*
;****************************************************************************************
;*                                                                     			*
;*    Notes:        hardware 	RGB led on GP0,1,2          							*
;*		         					*
;*                                                                     			*
;****************************************************************************************
	list      p=10F200	; list directive to define processor
	#include  m0-3.asm
	config200
 	
#define	OPTIONinit	b'11000111'	; no wakeup, no pullup, Clock 1:256
#define TRISIOinit 	b'00001000'	; all ports output bar GP3	
;#define GPIOinit 	b'00000000'	; GPIO  all LOW 
;****************************************************************************************


	init2xx			; reasonable initiation of special function registers 


	cblock	_MINRAM			; start of variables
	 SyS1       ;,SyS2
     dir,rgb
	endc


#define STACK		_MAXRAM
#define	GPis_on		SyS1,0
#define	GPwas_on	SyS1,1
#define	TMR07Bit	SyS1,7
	

;PWRUP	
		eq_ SyS1,=,0,   lit8
		eq_	dir,=,.16,    lit8
        eq_ rgb,=,0,        lit8

TC0used = 2					; use 2 bytes of timer for TC0
	cblock
	 TC0:TC0used
	 NmbrEvent0
	endc	 
		EVENT0in	.500,msec,.15  ; set time @ 500 milliseconds
;		clrf	NmbrEvent0		; set execution  for EVENT0

;TC1used = 2
;	cblock
;	 TC1:TC1used,NmbrEvent1
;	endc
;		EVENT1in	1,msec,.15	;EVENT15 in 1 msec, goto MLOOP	 

JMPTable	andlw	0x0F
		addwf	PCL,F
		 goto	EVENT0	
		 goto	EVENT1
		 goto	EVENT2	
		 goto	EVENT3
		 goto	EVENT4
		 goto	EVENT5
		 goto	EVENT6
		 goto	EVENT7
		 goto	EVENT8
		 goto	EVENT9
		 goto	EVENT10
		 goto	EVENT11
		 goto	EVENT12
		 goto	EVENT13
		 goto	EVENT14
		 goto	EVENT15


tabca   addwf PCL,f  
    dt  .127 ,  .0,  .0      ;0
    dt  .255 , .90 , .0      ;1
    dt  .255 ,.161,  .0     ;2
	dt  .229 ,.225,  .0     ;3
    dt  .173 ,.255,  .0     ;4
    dt   .50 ,.255, .16    ;5
    dt   .38 ,.255, .38     ;6
    dt   .19 ,.236,.158    ;7  
    dt   .15 , .86,.255    ;8
    dt    .0 ,.139,.175    ;9
    dt   .21 , .86,.205    ;10
    dt    .0 , .56,.225    ;11
    dt   .15 , .86,.255    ;12
    dt  .221 ,  .0,.255    ;13
    dt  .255 , .98,.255    ;14
    dt  .255 ,.255,.255    ;15
    dt  .255 ,.255,.255    ;15


	cblock
	 oldtmr0,RedPoint,GreenPoint,BluePoint,oldrgb
	endc
;
onepoint   
         movwf   STACK
        swapf   rgb,w
        andlw   0x0F
        addwf   STACK,F
        addwf   STACK,F
        addwf   STACK,F
        movfw   STACK
        call tabca
        movwf  INDF
       swapf   rgb,w
       andlw   0xF0
       movwf   STACK-1
       do_     STACK,=,STACK,+,3, lit8
       call tabca
       subwf INDF,W
       movwf   STACK

#define	Mi_Nus	SyS1,0

;
;        if_C
;         bsf Mi_Nus
;        if_NC
;         bcf Mi_Nus
;rlfstack
;       rrf     SyS1,W     ; load Carry with bit minus
;       rrf     STACK,f
;       movfw   STACK
;       clrC
;       rlf     STACK-1,f
;       if_C
;         addwf INDF,f
;       movf    STACK-1,f
;       if_NZ 
;         goto rlfstack
;

        retlw 0
colortable
        eq_  FSR,=,RedPoint,lit8
        movlw  0
        call   onepoint
;       eq_  FSR,=,GreenPoint,lit8
;       movlw  1
;       call   onepoint        
;        eq_  FSR,=,BluePoint,lit8
;        movlw  2
;        call   onepoint

        goto returnpoint       
;

;	org	0x100


#define Red_LED GPIO,0
;#define	TMR00Bit	SyS1,0	
MLOOP	clrwdt
;        if__m   TMR00Bit,not_but,TMR0,0
;          bsf   TMR00Bit
;          goto  exec256us
;        end_m
;        if__m   TMR0,0,not_but,TMR00Bit
;          bcf   TMR00Bit
;          goto  exec256us
;        end_m
;        goto    no256us
        if_ TMR0,==,oldtmr0,        byte
         goto   no256us
exec256us
        eq_     oldtmr0,=,TMR0,     byte
        movf    oldtmr0,F
        if_Z
          clrf  GPIO            ; all on


;       if_m oldtmr0, ==, 0  ,      lit8              ;
;          if_ redpoint,>, 0                           ;
;           bcf Red_LED                         ;
;        else_m                                    ; oldtmr > 0
                                                  ;        
           if_ oldtmr0,==,RedPoint,byte  
               bsf Red_LED        
           if_ oldtmr0,==,GreenPoint,byte  
               bsf Green_LED
           if_m oldtmr0,==,BluePoint,byte  
               bsf Blue_LED

            nop                             ; read wait until is GPIO,3 is high
                                  ;
           end_m                                    ;
                                                  ;
;         if_m oldtmr0 = green                      
;            clr grnbit
;         if_ oldtmr0 = blue
;            clr bluebit
;        end_m                                     ; oldtmr > 0
; 
        if_m rgb,!=, oldrgb,        byte
           eq_ oldrgb,=,rgb,        byte
           goto colortable
returnpoint
        end_m
    
;if__m   lowdetectet,
;         eq__ gpio,3,STATUS,C
 ;        rlf    pollrgb
;         if_C 
;          bcf low_detected
;          
;          rgb = pollrgb
;        else_m
;           if_m gpio,3,is_off, 1
;           setC
;           clrf pollrgb
;          rlf pollrgb
;          bsf lowdetected
;        endif_m
                                           

no256us	
	if_ 	TMR0,5,is_on,1			; software emulation of
	 goto	set7			        ; timer 0 overflow of any bit
	if_  	TMR07Bit,is_off,1		;
	 goto	set7+1
;	if_not	INTCON,T0IF		        ; wait for timer TMR0 to time out
;	 goto	MLOOP
;	bcf	INTCON,T0IF			
	goto	PERIODIC8		        ; which is 256*64 us =8 milliseconds
exitPERIODIC8

	decfsz	TC0,F
	 goto	endMLOOP0
	goto	PERIODIC2048		    ; after first TC0 expired, 2048 milliseconds 
exitPERIODIC2048			        ; have passed
	if TC0used > 1
;	movlw	0x07			        ;example for very long periodic events
;	andwf	TC0+1,W
;	movwf	STACK
;	movlw	-7
;	addwf	STACK,W
;	skpnZ
;	 goto	PERIODIC16sec		    ; every 16 seconds (Event0 must be later!)
exitPERIODIC16sec
	decfsz	TC0+1,F
	 goto	endMLOOP0
	endif
					                ; after TC0+1 expired, 8 minutes 44 seconds
	if TC0used > 2			        ; have passed
	decfsz	TC0+2,F
	 goto	endMLOOP0
	endif 
	movfw	NmbrEvent0		        ; after TC0+2 expired, 37 hours 17 minutes
	goto	JMPTable 		        ; now jump to loaded Event
	 
endMLOOP0				        ; and continue execution here	 
end0MLOOP0
    if TC1used>0
	decfsz	TC1,F			        ; analogous as TC0
	 goto	endMLOOP1
    endif
	if TC1used > 1
	decfsz	TC1+1,F
	 goto	endMLOOP1
	endif
	if TC1used > 2
	decfsz	TC1+2,F
	 goto	endMLOOP1
	endif
    if TC1used > 0 
	movfw	NmbrEvent1
	goto	JMPTable 
	endif 
	 
set7	bsf	TMR07Bit	
endMLOOP1	goto	MLOOP

;	org	0x200
;PERIODIC64usec			
;       

;		goto	exitPERIODIC64usec

PERIODIC8
		goto	exitPERIODIC8			

PERIODIC2048	
		goto	exitPERIODIC2048
		
PERIODIC16sec	
		goto	exitPERIODIC16sec	
				 
EVENT0		
		
EVENT1	

EVENT2			
		
EVENT3
		 
EVENT4		

EVENT5		
		
EVENT6	

EVENT7		

EVENT8		

EVENT9		

EVENT10		

EVENT11		


EVENT12	
		EVENT0in .60,sec,0	;loop

EVENT13


EVENT14 

   
EVENT15 
        do_  rgb,=,rgb,+,dir,   byte
        if_m rgb,==,0,          lit8
           eq_ dir,=,.16,        lit8
        end_m
         
        if_m  rgb,>=,.240,       lit8
           eq_ dir,=,.256-.16,      lit8  
        end_m
		EVENT0in 2,sec,.15	



		end