+.ifdef DEBUG
+ldi tmp,0x52
+out UDR,tmp
+.endif
+
+; input & pullup
+cbi DDRD,DDD6
+sbi PORTD,PD6
+
+; activate led
+sbi PORTB,LED_CAM
+cbi PORTB,LED_CARD
+
+; toggle state
+eor state,state_m
+
+; toggle icp sense
+rcall TOGGLE_ICP_SENSE
+
+; fwd to cam if in stupid mode
+sbrc mode,STUPID_F
+rcall FWD_TO_CAM
+
+; calculate delta clocks if in cool mode
+sbrc mode,COOL_F
+rcall CALC_DELTA_CLOCK
+
+; send time and state via uart
+sbrc mode,COOL_F
+rcall PREPARE_UART
+
+; return
+reti
+
+; ------------------------
+; calc_delta_clock routine
+; ------------------------
+
+CALC_DELTA_CLOCK:
+
+.ifdef DEBUG
+ldi tmp,0x63
+out UDR,tmp
+.endif
+
+; store counters
+mov counter_l_tmp,counter_l
+mov counter_h_tmp,counter_h
+
+; get new ones
+in counter_l,ICR1L
+in counter_h,ICR1H
+
+; delta calc on host software by now
+
+; return
+ret
+
+; ------------------------
+; toggle_icp_sense routine
+; ------------------------
+
+TOGGLE_ICP_SENSE:
+
+.ifdef DEBUG
+ldi tmp,0x54
+out UDR,tmp
+.endif
+
+; toggle according to state
+in tmp,TCCR1B
+cbr tmp,ICES1
+sbrs state,HIGH ; maybe toggle according to TCCR1B?
+sbr tmp,ICES1
+out TCCR1B,tmp
+
+; return
+ret
+
+; ------------------
+; fwd_to_cam routine
+; ------------------
+
+FWD_TO_CAM:
+
+.ifdef DEBUG
+ldi tmp,0x66
+out UDR,tmp
+.endif
+
+; activate led
+sbi PORTB,LED_FWD_TO_CARD
+cbi PORTB,LED_FWD_TO_CAM
+
+; disable external interrupt 1 while toggling edge
+in tmp,GIMSK
+cbr tmp,INT1
+out GIMSK,tmp
+
+; output state on port to cam
+in tmp1,PORTD
+sbr tmp1,PD3
+sbrs state,HIGH_F
+cbr tmp1,PD3
+
+; configure as output and push-pull low/high
+sbi DDRD,DDD3
+out PORTD,tmp1;
+
+; reenable external interrupt 1
+sbr tmp,INT1
+out GIMSK,tmp
+
+; return
+ret
+
+; --------------------
+; prepare_uart routine
+; --------------------
+
+PREPARE_UART:
+
+.ifdef DEBUG
+ldi tmp,0x50
+out UDR,tmp
+.endif
+
+; write transfer data to sram
+ldi ZL,0x60
+st Z+,counter_l
+st Z+,counter_h
+st Z+,overflow_counter
+st Z+,state
+
+; enable uart data register empty interrupt
+sbi UCR,UDRIE
+
+; return
+ret
+
+
+; ---------------
+; rec_cam routine
+; ---------------
+
+REC_CAM:
+
+.ifdef DEBUG
+ldi tmp,0x72
+out UDR,tmp
+.endif
+
+; first thing - pullup on
+cbi DDRD,DDD3
+sbi DDRD,PD3
+
+; activate led
+sbi PORTB,LED_CARD
+cbi PORTB,LED_CAM
+
+; toggle state
+eor state,state_m
+
+; toggle int sense
+rcall TOGGLE_INT_SENSE
+
+; fwd to card if in stupid mode
+sbrc mode,STUPID_F
+rcall FWD_TO_CARD
+
+; calculate delta clocks if in cool mode
+sbrc mode,COOL_F
+rcall CALC_DELTA_CLOCK
+
+; return
+reti
+
+; ------------------------
+; toggle_int_sense routine
+; ------------------------
+
+TOGGLE_INT_SENSE:
+
+.ifdef DEBUG
+ldi tmp,0x73
+out UDR,tmp
+.endif
+
+in tmp,MCUCR
+cbr tmp,ISC10
+sbrs state,HIGH_F
+sbr tmp,ISC10
+out MCUCR,tmp
+
+; return
+ret
+
+; -------------------
+; fwd_to_card routine
+; -------------------
+
+FWD_TO_CARD:
+
+.ifdef DEBUG
+ldi tmp,0x46
+out UDR,tmp
+.endif
+
+; activate led
+sbi PORTB,LED_FWD_TO_CAM
+cbi PORTB,LED_FWD_TO_CARD
+
+; disable icp interrupt while toggling edge
+in tmp,TIMSK
+cbr tmp,TICIE
+out TIMSK,tmp
+
+; output state on port to card
+in tmp1,PORTD
+sbr tmp1,PD6
+sbrs state,HIGH_F
+cbr tmp1,PD6
+
+; configure as output and push-pull low/high
+sbi DDRD,DDD6
+out PORTD,tmp1;
+
+; reenable icp interrupt
+sbr tmp,TICIE
+out TIMSK,tmp
+
+; return
+ret
+
+; -------------------
+; t1_overflow routine
+; -------------------
+
+T1_OVERFLOW:
+
+.ifdef DEBUG
+ldi tmp,0x74
+out UDR,tmp
+.endif
+
+; increment counter overflow
+add overflow_counter,one
+
+; toggle led status
+mov tmp,overflow_counter
+and tmp,one
+sbi PORTB,LED_OVERFLOW
+sbrs tmp,1
+cbi PORTB,LED_OVERFLOW
+
+; return
+reti
+
+; ---------------
+; rst_cam routine
+; ---------------
+
+RST_CAM:
+
+.ifdef DEBUG
+ldi tmp,0x69
+out UDR,tmp
+.endif
+
+; by now just jump to init
+rjmp INIT
+
+; ----------------
+; uart_out routine
+; ----------------
+
+UART_OUT:
+
+; disable uart data register empty interrupt
+cbi UCR,UDRIE
+
+; init counter(s)
+mov tmp,zero
+
+; send the data
+rcall UART_SEND
+
+; return
+reti
+
+; -----------------
+; uart_send routine
+; -----------------
+
+UART_SEND:
+
+; read next byte from memmory and transfer via uart
+sbic USR,UDRE
+rcall UART_GS
+
+; return if everything was sent
+cpi tmp,uart_data_len
+brne UART_SEND
+ret
+
+; ---------------
+; uart_gs routine
+; ---------------
+
+UART_GS:
+
+;
+; wie macht man load mit autoinc richtig?
+;
+
+; read byte from memory and write via uart
+ld tmp1,Z+
+out UDR,tmp1
+
+; increment counter (maybe needed later)
+add tmp,one
+
+; return
+ret