; author: hackbard@hackdaworld.dyndns.org
;
-include "../include/2313def.inc"
+
+; at90s2313
+; setup:
+; vcc card --- atmel vcc
+; vcc cam ---
+; rst cam --- atmel int0 (pd2)
+; rst card --- atmel (pd4)
+; clk card -\
+; - atmel t1 (pd5)
+; clk cam -/
+; gnd card --- gnd cam --- atmel gnd
+; i/o card --- atmel icp (pd6)
+; i/o cam --- atmel int1 (pd3)
+
+.include "../include/2313def.inc"
+
+; functions by now:
+;
+; stupid mode only by now. just see what cam/card do and redirect
+; this to card/cam.
+;
+; next implementation:
+;
+; try to read one byte of card/cam communication and output via uart.
+; output time information in some way.
+;
+; future:
+;
+; buffer/parse whole strings and decide whether to send to card or not.
+
+; some defines
+; ------------
+
+; baudrate = clock freq / etu
+; std smartcard etu: 372
+; other smartcards: 625
+.define clock 8000000
+.define clk_h 4000000
+.define etu 625
+.define etu_h 312
+.define baudrate 5 ; UBRR value for 76800 bits/s (8mhz clock)
+
+; names for registers
+.define tmp r16
+.define bitcount r17
+.define tmp1 r18
+.define tmp2 r19
+.define byte r20
+.define overflow_counter r21
+.define one r1
+.define zero r0
+
+
+;
+; programm code
+; -------------
;
; interrupt vectors:
;
; reset
-rjmp INIT ; init routine
+rjmp INIT
-; int 0
-rjmp REC_FROM_CARD ; smartcard sends data, we listen
+; int0
+rjmp RST_CAM
-; int 1
-rjmp REC_FROM_CAM ; cam sends data, we listen
+; int1
+rjmp REC_CAM
; timer/counter capt 1
-reti ; we just count the smartcard clocks
+rjmp REC_CARD
; timer/counter compare
-reti ; not in use
+reti
; timer/counter overflow 1
-reti ; not in use
+rjmp CLK_OVERFLOW
; timer/counter overflow 0
-reti ; not in use
+reti
; uart rx complete
-reti ; maybe we need it later
+reti
; uart data register empty
-reti ; maybe we need it later
+rjmp UART_OUT
; uart tx complete
-reti ; maybe we need it later
+reti
; analog comparator
-reti ; not in use
+reti
;
; init routine
INIT:
; set stackpointer
-ldi r16,low(RAMEND)
-out SPL,r16
+ldi tmp,low(RAMEND)
+out SPL,tmp
+
+; enable interrupts int0,int1
+ldi tmp,((1<<INT0)|(1<<INT1))
+out GIMSK,tmp
+; int0/1 setup
+ldi tmp,((1<<ISC01)|(0<<ISC00)|(1<<ISC11)|(0<<ISC10))
+out MCUCR,tmp
+
+; enable t/c overflow interrupt and icp
+ldi tmp,((1<<TOIE1)|(1<<TICIE1))
+out TIMSK,tmp
+; setup t/c and icp
+ldi tmp,((1<<CS12)|(1<<CS11)|(1<<CS10)|(1<<ICNC1)|(1<<ICES1))
+out TCCR1B,tmp
+
+; configure uart - interrupt enabled when i/o
+ldi tmp,baudrate
+out UBRR,tmp
+sbi UCR,TXEN
+
+; init registers
+ldi bitcounter,0
+ldi byte,0
+ldi overflow_counter,0
+ldi tmp1,0
+ldi tmp2,0
+ldi tmp,1
+mov one,tmp
+ldi tmp,0
+mov zero,tmp
; enable interrupts (global)
-ldi r16,(1<<7)
-out SREG,r16
+sei
+
+; jump to mainloop
+rjmp MAIN
+
+;
+; main routine
+;
+
+MAIN:
+
+rjmp MAIN
+
+;
+; rec_card routines
+;
+
+REC_CARD:
+
+; decide what to do
+sbic PORTD,PD6
+rjmp REC_CARD_HIGH
+rjmp REC_CARD_LOW
+
+REC_CARD_HIGH:
+
+; save counter
+in r24,ICR1L
+in r25,ICR1H
+
+; output high on port to cam
+sbi PORTD,PD3
+
+; toggle icp sense
+cbi TCCR1B,ICES1
+;in r16,TCCR1B
+;cbr r16,ICES1
+;out TCCR1B,r16
+
+; status tu submit
+ldi r26,1
+
+; enable uart dre interrupt
+sbi UCR,UDRIE
+
+reti
+
+REC_CARD_LOW:
+
+; save counter
+in r27,ICR1L
+in r28,ICR1H
+
+; output low on port to cam
+cbi PORTD,PD3
+
+; toggle icp sense
+sbi TCCR1B,ICES1
+
+; status to submit
+ldi r26,0
+; enable uart dre interrupt
+sbi UCR,UDRIE
+reti
+
+;
+; rec_cam routines
+;
+
+REC_CAM:
+
+; decide what to do
+sbic PORTD,PD3
+rjmp REC_CAM_HIGH
+rjmp REC_CAM_LOW
+
+REC_CAM_HIGH:
+
+; output high on port to card
+sbi PORTD,PD6
+
+; toggle int1 sense
+in r16,MCUCR
+cbr r16,ISC10
+out MCUCR,r16
+
+reti
+
+REC_CAM_LOW:
+
+; output low on port to card
+cbi PORTD,PD6
+
+; toggle int 1 sense
+in r16,MCUCR
+sbr r16,ISC10
+out MCUCR,r16
+
+reti
+
+;
+; t1_overflow routine
+;
+
+T1_OVERFLOW:
+
+add r22,r1 ; inc counter overflow register
+
+reti
+
+;
+; rst_cam routine
+;
+
+RST_CAM:
+
+; decide what to do
+sbic PORTD,PD2
+rjmp RST_CAM_HIGH
+rjmp RST_CAM_LOW
+
+RST_CAM_HIGH:
+
+; output high on rst port to card
+sbi PORTD,PD4
+
+; toggle int0 sense
+in r16,MCUCR
+cbr r16,ISC00
+out MCUCR,r16
+
+reti
+
+RST_CAM_LOW:
+
+; output low on rst port to cam
+cbi PORTD,PD4
+
+; toggle int0 sense
+in r16,MCUCR
+sbr r16,ISC00
+out MCUCR,r16
+
+reti
+
+;
+; uart_out routines
+;
+
+UART_OUT:
+
+;
+; hier weiter ...
+;