;**************************************************************************
; Application:  Led flasher
; Author:       Dan Williams
; Date:         
; Assembler:    AVR Studio 4.07.
; Target:       Atmel ATtiny13.
;***************************************************************************
;------ objective -----
;PB0  PWM1   
;PB1  PWM2   
;PB2  ANA2   
;PB3  SC
;PB4  SD
;PB5  ANA1  
;
;---- this test -----
;PB0  LED1
;PB1  LED2
;PB2  LED3
;PB3  SC
;PB4  SD
;PB5  LED4
;
;****************************************************************************

.device ATtiny13 

; ------------- Memory locations ---------------
;
;  .def bar =r01 ; one byte ram location
;
; ----------------------------------------------

; R 0 ->15 are gen. purp.

.def A          = r16    
.def B          = r17            ; and logic instructions
.def C          = r18
.def D          = r19

.def curstate   = r20
.def laststate  = r21
.def _R         = r22
.def _S         = r23
.def _T         = r24
.def _U         = r25
.def _V         = r26
.def _w         = r27
.def _X         = r28
.def _Y         = r29
.def _Z         = r30


;cant use r25 -> r31

; ------------- Equates ------------------

.equ INPUT   = 0
.equ OUTPUT  = 1

; --- Port Directions ---
.equ PB0DIR  = OUTPUT
.equ PB1DIR  = OUTPUT
.equ PB2DIR  = OUTPUT
.equ PB3DIR  = INPUT
.equ PB4DIR  = INPUT
.equ PB5DIR  = OUTPUT

; --- Initial port state ---
.equ INPB0   = 0
.equ INPB1   = 0
.equ INPB2   = 0
.equ INPB3   = 0
.equ INPB4   = 0
.equ INPB5   = 0

; --- Port Names ---
.equ LED1    = 0  ; clock fall
.equ LED2    = 1  ; stop
.equ LED3    = 2  ; clock high
.equ SC      = 3  
.equ SD      = 4  
.equ LED4    = 5  ; start

; --- amalgimate ---

.equ INITDIR   = (PB5DIR<<DDB5)|(PB4DIR<<DDB4)|(PB3DIR<<DDB3)|(PB2DIR<<DDB2)|(PB1DIR<<DDB1)|(PB0DIR<<DDB0)
.equ INITSTATE = (INPB5<<PORTB5)|(INPB4<<PORTB4)|(INPB3<<PORTB3)|(INPB2<<PORTB2)|(INPB1<<PORTB1)|(INPB0<<PORTB0)



;========================| SYSTEM VECTORS |===========================
;
.cseg                                   ;CODE segment
.org 0x0000
    rjmp    init   ; RESET 
    reti           ; EXT_INT0  
    reti           ; PCINT0   (pin change)
    reti           ; TIM0_OVF  
    reti           ; EE_RDY  
    reti           ; ANA_COMP  
    reti           ; TIM0_COMPA  
    reti           ; TIM0_COMPB  
    reti           ; WATCHDOG  
    reti           ; ADC  

;=============================| MAIN |================================
;


.org 0x000A
init:   
        ; Set up stack
        ldi     A,   low(RAMEND)        ; load low address of ram to R16...
        out     SPL, A               ; Set Stack Pointer to top of RAM

        ; Disable interrupts
        cli        

        ; Set up io ports
        ldi   A,    INITSTATE
        out   PORTB, A
        ldi   A,    INITDIR
        out   DDRB,  A  

MainLoup:

        sbic  PORTB, SC
		  sbi  PORTB, LED1
        sbis  PORTB, SC
		  cbi  PORTB, LED1

        sbic  PORTB, SD
		  sbi  PORTB, LED2
        sbis  PORTB, SD
		  cbi  PORTB, LED2
      
     
        rjmp    MainLoup                ; go back and do it all over again


;========================| SUBROUTINES |==============================
;


  
clock0:
  sbi  PORTB, LED1 
  rjmp irqdone

stop:
  sbi  PORTB, LED2
  rjmp irqdone

clock1:
  sbi  PORTB, LED3
  rjmp irqdone

start:
  sbi  PORTB, LED4
  rjmp irqdone

irqdone:
  ret



wait:    
        ldi     B, 3              ; load count1 with decimal 200

d1:
        ldi     C, 200              ; load count2 with decimal 200

d2:
        ldi     D, 200

d3:
        dec     D
        brne    d3
        dec     C                  ; decrement count2
        brne    d2                 ; loop if not zero              
        dec     B                  ; decrement count1 if count2 is zero
        brne    d1                 ; do inside loop again if count2 nz

		cbi     PORTB, LED1
		cbi     PORTB, LED2
		cbi     PORTB, LED3
		cbi     PORTB, LED4
        ret

