; Orange programmer module v2.4
; (c) 2008 CnCLab
; Atmel AT89S
;CHIP=AT89S8253,12K,89s8253.hpl
;AREA=FLASH,12K,0,0
;AREA=EEPROM,2K,0,0x3000

; not 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

R9=Lock,C3,LB3,LB2,LB1

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]
R9=0xFF ;lock
;EO=0,P=10 ;BB on
CFREQ=2000000
SCK=0
MISO=Z
RESET=0,P=1000
RESET=1,P=20000

;LOOP=(0,31){    ; 32 attempts
_DOUT(0xAC) ;Programming Enable
_DOUT(0x53)
_DOUT(0x00)
_DIN
PRINT=S("ANSWER: %02lX",R0)
;EXIT


P=10


[READ]
R0=0x20 ;read CODE memory
$AREA?1{
R0=0xA0 ;Read Data Memory
}

_DOUT ;read cmd
R0=ADR,R0=/256
_DOUT
R0=ADR,R0=&0xFF
_DOUT
_DIN
DATA=R0



[WRITE]
R0=0x40 ;write CODE memory
$AREA?1{
R0=0xC0 ;write Data Memory
}

_DOUT 
R0=ADR,R0=/256
_DOUT
R0=ADR,R0=&0xFF
_DOUT
R0=DATA
_DOUT



P=5000 ; 4ms


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


[READID]
_DOUT(0x28) ;Read ID
_DOUT(0x00)
_DOUT(0x00)
_DIN
R4=R0


[ReadLockBits]
_DOUT(0x24) ;Read Lock Bits
_DOUT(0x00)
_DOUT(0x00)
_DIN
R9=R0,R9=&7
GET=("Read Lock Bits",R9)

[ERASE]
_DOUT(0xAC) ;Chip Erase
_DOUT(0x80)
_DOUT(0x00)
_DOUT(0x00)

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

[WriteLockBits]
GET=("Write Lock Bits",R9)
RA?0{EXIT}
_DOUT(0xAC) ;Write Lock Bits 0,0
R0=R9,R0=&0x07,R0=|0xE0
_DOUT
_DOUT(0x00)
_DOUT(0x00)

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


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