; Orange programmer module v3.4 beta
; (c) 2001-2002 Alex Plusov
; Atmel AVR

;CHIP=AT90S8515,8K,90s8515.hpl
;AREA=FLASH,8K,0,0
;AREA=EEPROM,512,0,0

; Primary tested
;todo FUSE bits

ALLPINS=40
INFO="AVR Adapter"

PINO=SCK,  0,8
PINO=MOSI, 2,6 
PINO=MISO, 1,7 
PINO=RESET,3,9 
PINI=MISO, 1,7 

PING=GND,255,20 
PINO=VCC,254,40
BUSO=CFREQ,128

R9=Lock,L,Off,Prog,All

CDELAY=4 ; minimum periods for the serial clock (SCK) > 4 XTAL1 clock cycle!


[_DOUT]
R1=0;
LOOP=(7,0){MOSI=R0[I],SCK=1,R1[I]=MISO,SCK=0}

[_DIN]
R0=0
LOOP=(7,0){SCK=1,R0[I]=MISO,SCK=0}


[INIT]
;EO=0,P=10 ;BB on
SCK=0
MISO=Z
CFREQ=2000000
RESET=1,P=20
RESET=0,P=100000


LOOP=(0,31){    ; 32 attempts
R0=0xAC,_DOUT ;Programming Enable
R0=0x53,_DOUT
R0=0x00,_DOUT
R3=R1
R0=0x00,_DOUT
R3?0x53{BREAK}
SCK=P
}
R3?!0x53{PRINT=("ANSWER ERROR: %02lX",R1),EXIT}

P=10



[READ]
$AREA?0{
R0=0x20 ;program memory
R1=ADR,R0[3]=R1[0] ;Hi/Lo
;R0=0xA0  ;eeprom
_DOUT
LOOP=(16,9){MOSI=ADR[I],SCK=P}
LOOP=(8,1){MOSI=ADR[I],SCK=P}
}

$AREA?1{
R0=0xA0      ;eeprom area
_DOUT
LOOP=(15,8){MOSI=ADR[I],SCK=P}
LOOP=(7,0){MOSI=ADR[I],SCK=P}
}


_DIN
DATA=R0


[WRITE]
$AREA?0{
R0=0x40 ;program memory
R1=ADR,R0[3]=R1[0] ;Hi/Lo
;R0=0xC0  ;eeprom
_DOUT
LOOP=(16,9){MOSI=ADR[I],SCK=P}
LOOP=(8,1){MOSI=ADR[I],SCK=P}
R0=DATA,_DOUT
P=4000 ; 4ms
}

$AREA?1{
R0=0xC0 ;eeprom memory area 
_DOUT
LOOP=(15,8){MOSI=ADR[I],SCK=P}
LOOP=(7,0){MOSI=ADR[I],SCK=P}
R0=DATA,_DOUT
P=9000 ; 4ms
}


[WRITEEND]
P=10000
RESET=1,P=20,
RESET=0,P=20000


[ReadID]
R0=0x30,_DOUT
R0=0x00,_DOUT
R0=0x00,_DOUT
_DIN
R2=R0,R2=<<8

R0=0x30,_DOUT
R0=0x00,_DOUT
R0=0x01,_DOUT
_DIN
R2=|R0,R2=<<8

R0=0x30,_DOUT
R0=0x00,_DOUT
R0=0x02,_DOUT
_DIN
R2=|R0

PRINT=("ID: %06lX",R2)


[ERASE]
R0=0xAC,_DOUT ;Chip Erase
R0=0x80,_DOUT 
R0=0x00,_DOUT 
R0=0x00,_DOUT 

P=10000
RESET=1,P=20,
RESET=0,P=20000


[Lock]
R0=0xAC,_DOUT ;Write lock bits
R0=0xE0,_DOUT 
R0=0x00,_DOUT 
R0=0x00,_DOUT 

P=10000
RESET=1,P=20,
RESET=0,P=20000

[END]
RESET=1
;EO=1,P=10 ;BB off