; Orange programmer module v2.6
; (c) 2004 Alex Plusov
; Atmel AT89S
; CHIP=AT89S8252,8K

; tested


ALLPINS=40
INFO="AT89S"

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

PINO=XTAL1,12,19 

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


CDELAY=5 ; 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
CFREQ=2000000
SCK=0
MISO=Z
RESET=0,P=200
RESET=1,P=20000

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

P=10


[READ]
R0=0x01 ;read CODE memory
;R0=0x05 ;read DATA memory
R1=ADR,R1=/256,R1=*8,R0=|R1 
_DOUT
LOOP=(7,0){MOSI=ADR[I],SCK=1,SCK=0}
_DIN
DATA=R0


[WRITE]
R0=0x02 	;write memory
R1=ADR,R1=/256,R1=*8,R0=|R1 
_DOUT
LOOP=(7,0){MOSI=ADR[I],SCK=1,SCK=0}
R0=DATA,_DOUT
P=5000 ; 4ms


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



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

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

[WriteLockBits]
R0=0xAC,_DOUT ;Write Lock Bits 0,0
R0=0x07,_DOUT
R0=0x00,_DOUT

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


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