Moderadores: 51, guest2003, Renie, gpenga
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <bios.h>
#define pport 0x378
#define iport 0x379
#define uchar unsigned char
void fim()
{
outportb(pport,0xff & ~8); //desliga vcc
_setcursortype(_NORMALCURSOR);
printf("\n");
exit(1);
}
void tempo(unsigned char t)
{
unsigned char j=0;
outportb(0x61,0); //desliga contador timer2
outportb(0x43,0xb0); //timer2, modo 0, lsb -> msb
outportb(0x42,t); //lsb
outportb(0x42,0); //msb
outportb(0x61,1); //liga contador
/*
nok:
asm {
mov al,0xc8
out 0x43,al
in al,0x42
and al,0x80
jz nok
}
*/
while(!j)
{
outportb(0x43,0xc8); //read back
j=inportb(0x42); //status da saida
j&=0x80;
}
}
unsigned char shift(unsigned char d)
{
unsigned char i,p,resultado=0;
for (i=0;i<8;i++)
{
if (d&0x80) p=~5; //bit3=vcc=1,bit2=reset=1,bit0=dado=1
else p=~4; // bit0=dado=0
outportb(pport,p); //bit0=dado
p&=~2; //bit1=clock em 1
outportb(pport,p); //bit0=dado
// tempo(5);
resultado<<=1;// shift the result to get it ready for the next bit
if (!(inportb(iport)&0x40)) resultado|=0x01;// if result is one, then set LS bit
p|=2; //bit1=clock em 0
outportb(pport,p);
// tempo(5);
d<<=1;
}
return resultado;
}
//escreve e le 1 byte
uchar rwbyte(uchar a1, uchar a2, uchar a3, uchar d)
{
shift(a1);shift(a2);shift(a3); //envia o endereço
return shift(d); //envia e le o dado
}
void
main(int argc,char *argv[])
{
FILE *fin;
char arq1[40];
unsigned char temp,dado[0x2000];
unsigned long chksum=0, i=0;
unsigned int k,j=0,y,ah,al;
printf("\nATS 1.1A - (c) LLR-2004 Programador de 89S52 ATMEL\n");
if (argc!=2) {printf("\nUse: ats [nome do arquivo (.bin)]\n");fim();}
strcpy(arq1,argv[1]);
if (!(fin=fopen(arq1,"rb"))) {printf("\nArquivo nao encontrado\n");fim();}
for (i=0;i<0x2000;i++) {
fread(&dado[i],1,1,fin);
temp=dado[i];chksum+=temp;
}
fclose(fin);
printf("%s%s%s","\nArquivo: ",arq1," -> CHECKSUM = ");
textcolor(14);cprintf("%X\n",chksum);
printf("\n");
printf("\n");
printf("\n256 primeiros caracteres do arquivo:\n");
for (j=0;j<0x256;j++)
{
printf("%c",dado[j]);
}
printf("\n");
printf("\n256 Ultimos caracteres do arquivo:\n");
for (j=0x2000-256;j<0x2000;j++)
{
printf("%c",dado[j]);
}
printf("\n");
outportb(pport,0xff); //vcc em 1 reset em 0
delay(500);
outportb(pport,8 | ~4); //vcc e reset em 1
delay(500);
_setcursortype(_NOCURSOR);
if (rwbyte(0xac,0x53,0,0)!=0x69) //program enable
{
textcolor(14);
cprintf("\n%s","NAO ACHEI O CI!!");fim();
}
rwbyte(0xac,0x53,0,0);
delay(100);
temp=rwbyte(0x28,0,0,0);
printf("%s%X\n"," Signature byte 1= ",temp);
delay(100);
rwbyte(0xac,0x53,0,0);
delay(100);
temp=rwbyte(0x28,1,0,0);
printf("%s%X\n"," Signature byte 2= ",temp);
delay(100);
rwbyte(0xac,0x53,0,0);
temp=rwbyte(0x28,2,0,0);
printf("%s%X\n"," Signature byte 3= ",temp);
delay(100);
rwbyte(0xac,0x53,0,0);
delay(100);
temp=rwbyte(0x24,0,0,0)&0x1c; //so interessam bits 4,3,2
printf("%s%X\n\n"," Lock Bits= ",temp);
y=wherey();
printf("Tecle <ENTER> para gravar ou <ESC> para sair");
if (getch()==27) fim();
rwbyte(0xac,0x53,0,0);
delay(100);
rwbyte(0xac,0x80,0,0); //chip erase
delay(100);
gotoxy(1,y);
printf("GRAVANDO E VERIFICANDO... (Para cancelar, pressione qualquer tecla)");
printf("\n");
y=wherey();
chksum=0;
rwbyte(0xac,0x53,0,0);
delay(200);
for (i=0;i<0x2000;i++)
{
ah=i/256;
al=i-ah*256;
if (kbhit()) fim(); //tecle algo p/ sair
rwbyte(0xac,0x53,0,0); //prog enable
rwbyte(0x40,ah,al,dado[i]); //grava o dado
rwbyte(0xac,0x53,0,0); //prog enable
temp=rwbyte(0x20,ah,al,0); //le o dado
if (temp!=dado[i])
{
printf("\n");
textcolor(14);cprintf("\n%s%X","ERRO endereco= ",i);
fim();
}
chksum+=temp;
k=(j+4)/4;
gotoxy(k,y);printf("Þ ");
j=((i+1)*100)/0x2000;
gotoxy(27,y);cprintf("%u%s",j,"%");
}
printf("\n");
printf("%s","\n\nCHECKSUM DO CI= ");
textcolor(14);cprintf("%X\n",chksum);
printf("\nTecle <ENTER> para gravar Lock Bits ou <ESC> para nao gravar\n");
if (getch()==27) {
printf("\n");
cprintf("LOCK BITS NAO GRAVADOS !!!");
printf("\n");
goto nglb;
}
rwbyte(0xac,0x53,0,0);
delay(100);
rwbyte(0xac,0xe1,0,0); //grava lock bit1
delay(100);
rwbyte(0xac,0x53,0,0);
delay(100);
rwbyte(0xac,0xe2,0,0); //grava lock bit2
delay(100);
rwbyte(0xac,0x53,0,0);
delay(100);
rwbyte(0xac,0xe3,0,0); //grava lock bit3
delay(100);
nglb:
rwbyte(0xac,0x53,0,0);
delay(100);
temp=rwbyte(0x28,0,0,0);
printf("%s%X\n"," Signature byte 1= ",temp);
delay(100);
rwbyte(0xac,0x53,0,0);
temp=rwbyte(0x28,1,0,0);
printf("%s%X\n"," Signature byte 2= ",temp);
delay(100);
rwbyte(0xac,0x53,0,0);
temp=rwbyte(0x28,2,0,0);
printf("%s%X\n"," Signature byte 2= ",temp);
delay(100);
rwbyte(0xac,0x53,0,0);
delay(100);
temp=rwbyte(0x24,0,0,0)&0x1c; //so interessam bits 4,3,2
if (temp) printf("%s%X\n"," Lock Bits= ",temp);
else cprintf("%s%X\n"," Lock Bits= ",temp);
outportb(pport,0xff&~8); //desliga vcc
printf("\n");
_setcursortype(_NORMALCURSOR);
}
Usuários navegando neste fórum: Bing [Bot] e 1 visitante