Include 'M64DEF.INC'
ORG 0X0000
JMP main
ORG 0X0002
JMP EXT_INT0_ISR
ORG 0X0010
JMP EXT_INT7_ISR
ORG 0X0050

main:
LDI R16,low(RAMEND)
OUT SPL,R16
LDI R16,high(RAMEND)
OUT SPH,R16
LDI R16,0X00
OUT EICRA,R16 
LDI R16,0X40
OUT EICRB,R16
LDI R16,0X81
OUT EIMSK,R16
LDI R16,0X00
OUT DDRA,R16
OUT DDRB,R16
OUT DDRC,R16
OUT DDRD,R16
LDI R16,0X0FF
OUT DDRE,R16 
CLR R16
CLR R23
CLR R24
SEI

main_loop:
IN R20,PINA
LDI R17,0b00000001
AND R17,R20
CPI R17,0x01
BREQ main_loop
MUL R20,R20
STS 0x1200,R0
STS 0x1201,R1
JMP main_loop

EXT_INT7_ISR:
IN R21,PINB
IN R22,PINC
CP R21,R22
BREQ EQUAL
BRCC NOT_EQ
STS 0X1300,R21
JMP B

NOT_EQ:
STS 0X1300,R22
JMP B


EQUAL:
INC R16
CPI R16,255
BREQ A
JMP B

A:
CLR R16
JMP B

EXT_INT0_ISR:
IN R22,PIND
LOOP:
CPI R24,8
BREQ DONE
LSR R22
BRCS NEXT
INC R23 ; شمارش تعداد بیت 0
INC R24 
JMP LOOP

NEXT:
INC R24
JMP LOOP


DONE:
CPI R23,0
BREQ ZERO
CPI R23,1
BREQ ONE
CPI R23,2
BREQ TWO
CPI R23,3
BREQ THREE
CPI R23,4
BREQ FOUR
CPI R23,5
BREQ FIVE
CPI R23,6
BREQ SIX
SPI R23,7
BREQ SEVEN
CPI R23,8
BREQ EIGHT
JMP B

ZERO:
LDI R25,0b00111111
OUT PORTE,R25
JMP B

ONE:
LDI R25,0b00000110
OUT PORTE,R25
JMP B

TWO:
LDI R25,0b01011011
OUT PORTE,R25
RETI

THREE:
LDI R25,0b01001111
OUT PORTE,R25
JMP B

FOUR:
LDI R25,0b01001111
OUT PORTE,R25
JMP B

FIVE:
LDI R25,0b01101101
OUT PORTE,R25
JMP B

SIX:
LDI R25,0b01111101
OUT PORTE,R25
JMP B

SVEN:
LDI R25,0b00000111
OUT PORTE,R25
JMP B

EIGHT:
LDI R25,0b01111111
OUT PORTE,R25
JMP B

B:
RETI










