?? nvemul.c
字號:
/* nvemul.c */
/*
EPROM Emulator using NVRAM module.
32Kbyte capacity.
Dhananjay V. Gadre
12th July 1997
*/
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
#include<time.h>
#include<alloc.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
/*port addresses of the parallel adapter*/
unsigned int dport, sport, cport;
/*these ports control data, voltage to the ZIF socket,*/
/*control the tri-state buffers respectively*/
unsigned char port_0, port_1, port_2, port_3, error_byte;
#define MEMORY 32768 /*last address of the target controller memory*/
/*the Intelhex file has lines of code. each line begins with a : */
/*next is number of bytes in the line*/
#define LL 1 /*offset in the hex file where the line length is stored*/
#define ADDR 3 /*offset in the hex file where the destination address is stored*/
#define ZEROS 7
#define CODE_ST 9 /*offset of the beginning of the code*/
/*local global variables*/
unsigned char ram[MEMORY+10];
unsigned int curr_address;
FILE *fp1;
/*defines for port1*/
#define ENB_DATA 0x80
#define DIS_DATA 0x7F
#define ENB_LOW 0x40
#define ENB_HIGH 0xbf
#define ENB_WR 0x20
#define ENB_OE 0x10
#define ENB_CS 0x09
/* local routines */
int initialze(void); /* initialzes the external hardware */
int fill_buffer(void); /*read the intelhex format file & fill up the internal buffer */
int chk_writer(void); /*check if the programmer is connected and if +12V is ON*/
int write_verify_bytes(void);
/*routines to generate pulse on each of the 4 control port pins*/
void pulse_c0(void);
void pulse_c1(void);
void pulse_c2(void);
void pulse_c3(void);
void pulse_c0(void)
{
unsigned char temp;
temp=inportb(cport);
temp=temp & 0xfe;
outportb(cport, temp);
outportb(cport, temp);
outportb(cport, temp);
temp=temp | 0x01;
outportb(cport, temp);
}
void pulse_c1(void)
{
unsigned char temp;
temp=inportb(cport);
temp=temp | 0x02;
outportb(cport, temp);
outportb(cport, temp);
outportb(cport, temp);
temp=temp & 0xfd;
outportb(cport, temp);
}
void pulse_c2(void)
{
unsigned char temp;
temp=inportb(cport);
temp=temp & 0xfb;
outportb(cport, temp);
outportb(cport, temp);
outportb(cport, temp);
temp=temp | 0x04;
outportb(cport, temp);
}
void pulse_c3(void)
{
unsigned char temp;
temp=inportb(cport);
temp=temp | 0x08;
outportb(cport, temp);
outportb(cport, temp);
outportb(cport, temp);
temp=temp & 0xf7;
outportb(cport, temp);
}
char chartoi(char val)
{
unsigned char temp;
temp = toupper(val);
if(temp>0x39) {temp = temp -0x37;}
else {temp=temp-0x30;}
return temp;
}
int initialize(void)
{
dport = peek(0x40, 8);
sport=dport+1;
cport=dport+2;
if(dport ==0) return 0;
outportb(dport, 0);
outportb(cport, 0x05); /*all cport outputs high, except C0*/
outportb(cport, 0x0a); /*all cport pins are low, except C0*/
outportb(cport, 0x05); /*all cport outputs high, except C0*/
port_0=0;
port_1=0;
port_2=0;
port_3=0;
return 1;
}
int fill_buffer(void) /*read the intelhex format file & fill up the
internal buffer */
{
unsigned char ch, temp4, temp1, temp2, temp3;
unsigned char chk_sum=0, buf[600], num[10];
unsigned int line_length, address, line_temp, tempx, count=0;
count=0;
while(!feof(fp1) )
{
chk_sum=0;
/* check if start of line = ':' */
fgets(buf, 600, fp1);
tempx=strlen(buf);
/*printf("\n\nString length=%d\n", tempx);*/
/*printf("\n\n%s", buf);*/
if( buf[0] != ':') {printf("\nError... Source file not in Intelhex format. Aborting");
fclose(fp1);
return 0;
}
/* convert the next 2 characters to a byte which equals line length */
temp1=buf[LL];
temp2=buf[LL+1];
if( !isxdigit(temp1) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
if( !isxdigit(temp2) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
temp4 = chartoi(temp1);
chk_sum=chk_sum + 16*temp4;
line_length=(int)temp4;
temp4=chartoi(temp2);
chk_sum=chk_sum + temp4;
line_length = 16*line_length + (int)temp4;
/*printf("Entries=%d ", line_length);*/
if(line_length ==0) {
return count;
}
temp1=buf[ADDR];
temp2=buf[ADDR+1];
temp3=buf[ADDR+2];
temp4=buf[ADDR+3];
if( !isxdigit(temp1) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
if( !isxdigit(temp2) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
if( !isxdigit(temp3) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
if( !isxdigit(temp4) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
ch = chartoi(temp1);
temp1=ch;
ch = chartoi(temp2);
temp2=ch;
chk_sum = chk_sum + 16*temp1 + temp2;
ch = chartoi(temp3);
temp3=ch;
ch = chartoi(temp4);
temp4=ch;
chk_sum = chk_sum + 16*temp3 + temp4;
address = 0x1000 * (int)temp1 + 0x100 * (int)temp2 + 0x10*(int)temp3 + (int)temp4;
/*printf("Start Address=%x hex\n", address);*/
if(address > MEMORY)
{
printf("\nError in source file. Bad address. Aborting");
fclose(fp1);
return 0;
}
/*check for the next byte. It has to be 00 **/
temp1=buf[ZEROS];
temp2=buf[ZEROS+1];
if( !isxdigit(temp1) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
if( !isxdigit(temp2) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
ch = chartoi(temp1);
temp1=ch;
ch=chartoi(temp2);
temp2=ch;
ch = 16*temp1 + temp2;
if(ch != 0)
{
printf("\nError... Source file not in Intelhex format. Aborting");
fclose(fp1);
return 0;
}
/* now read bytes from the file & put it in buffer*/
for(line_temp=0; line_temp<line_length; line_temp++)
{
temp1=buf[2*line_temp+CODE_ST];
temp2=buf[2*line_temp+CODE_ST+1];
if( !isxdigit(temp1) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
if( !isxdigit(temp2) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
ch = chartoi(temp1);
temp1=ch;
ch=chartoi(temp2);
temp2=ch;
ch = 16*temp1 + temp2;
chk_sum=chk_sum + ch;
if(address > MEMORY)
{
printf("\nError in source file. Bad address. Aborting");
fclose(fp1);
return 0;
}
/* printf("%X ",ch);*/
ram[address]=ch;
address++;
count++;
}
/*get the next byte. this is the chksum */
temp1=buf[2*line_length+CODE_ST];
temp2=buf[2*line_length+CODE_ST+1];
if( !isxdigit(temp1) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
if( !isxdigit(temp2) ) {
printf("\nError in source file. Aborting");
fclose(fp1);
return 0;
}
ch = chartoi(temp1);
temp1=ch;
ch=chartoi(temp2);
temp2=ch;
ch = 16*temp1 + temp2;
chk_sum=chk_sum + ch;
/*printf("csum=%x", chk_sum);*/
if(chk_sum !=0)
{
printf("\nChecksum Error... Aborting");
fclose(fp1);
return 0;
}
}
return count;
}
int write_verify_bytes(void)
{
unsigned char temp, low_temp, high_temp, low_addr, high_addr;
long program_length;
for(program_length=0; program_length < MEMORY; program_length++)
{
curr_address=program_length;
/*generate the address for the NVRAM*/
/*this puts the data into the data latch but the latch is yet to
be enabled*/
port_0=ram[program_length];
outportb(dport, port_0);
pulse_c0();
/*enable the data on the data pins*/
port_1=port_1 | ENB_DATA;
outportb(dport, port_1);
pulse_c1();
/*write data to the NVRAM*/
low_addr=(unsigned char)program_length;
high_addr=(unsigned char) ((program_length>>8) & 0x00ff);
port_2=high_addr;
outportb(dport, port_2);
pulse_c2();
port_3=low_addr;
outportb(dport, port_3);
pulse_c3();
/*generate chip select*/
port_1=port_1 | ENB_CS;
outportb(dport, port_1);
pulse_c1();
/*generate write strobe*/
port_1=port_1 | ENB_WR;
outportb(dport, port_1);
pulse_c1();
/*disable write strobe*/
port_1=port_1 & ~ENB_WR;
outportb(dport, port_1);
pulse_c1();
/*disable data from the data output pin of the source*/
port_1=port_1 & DIS_DATA;
outportb(dport, port_1);
pulse_c1();
/*now enable read strobe to read back the data*/
port_1=port_1 | ENB_OE;
outportb(dport, port_1);
pulse_c1();
/*read low nibble*/
port_1=port_1 | ENB_LOW;
outportb(dport, port_1);
pulse_c1();
low_temp=inportb(sport);
/*read high nibble*/
port_1=port_1 & ENB_HIGH;
outportb(dport, port_1);
pulse_c1();
high_temp=inportb(sport);
/*disable cs strobe*/
port_1=port_1 & ~ENB_CS;
outportb(dport, port_1);
pulse_c1();
temp= (high_temp & 0xf0) | ( (low_temp >>4) & 0x0f);
temp=temp ^ 0x88;
/*printf("%X ", temp);*/
if(temp != ram[program_length])
{
error_byte=temp;
printf("\nError in program verify at address %X (hex). Aborting...", program_length);
printf("\nProgram data %X, read back data %X\n", ram[program_length], temp);
return 0;
}
}
for(program_length=0; program_length < MEMORY; program_length++)
{
curr_address=program_length;
/*generate the address for the NVRAM*/
/*write data to the NVRAM*/
low_addr=(unsigned char)program_length;
high_addr=(unsigned char) ((program_length>>8) & 0x00ff);
port_2=high_addr;
outportb(dport, port_2);
pulse_c2();
port_3=low_addr;
outportb(dport, port_3);
pulse_c3();
/*generate chip select*/
port_1=port_1 | ENB_CS;
outportb(dport, port_1);
pulse_c1();
/*now enable read strobe to read back the data*/
port_1=port_1 | ENB_OE;
outportb(dport, port_1);
pulse_c1();
/*read low nibble*/
port_1=port_1 | ENB_LOW;
outportb(dport, port_1);
pulse_c1();
low_temp=inportb(sport);
/*read high nibble*/
port_1=port_1 & ENB_HIGH;
outportb(dport, port_1);
pulse_c1();
high_temp=inportb(sport);
/*disable cs strobe*/
port_1=port_1 & ~ENB_CS;
outportb(dport, port_1);
pulse_c1();
temp= (high_temp & 0xf0) | ( (low_temp >>4) & 0x0f);
temp=temp ^ 0x88;
printf("%X ", temp);
if(temp != ram[program_length])
{
error_byte=temp;
printf("\nError in program verify at address %X (hex). Aborting...", program_length);
printf("\nProgram data %X, read back data %X\n", ram[program_length], temp);
return 0;
}
}
return 1;
}
main(argc, argv)
int argc;
char *argv[];
{
time_t start, endt;
unsigned long temp;
int byte_value, return_val, total_bytes;
printf("\n\n\n\tEPROM Emulator using NVRAM module Ver: 1.0\n");
printf("\t------------------------------------------\n");
printf("\t\t Dhananjay V. Gadre");
printf("\n\t\t July 1997.\n"); /* 12th July 1997*/
if(argc != 2)
{printf("\nError... Specify Intelhex source filename. Aborting");
printf("\nFormat: AtmelP intelhex_sourcefile");
exit(-1);
}
if((fp1=fopen(argv[1], "r")) == NULL)
{printf("\nError...Cannot open source file. Aborting");
exit(-1);
}
return_val=initialize(); /*Initialize the printer adapter port*/
if(return_val == 0) {printf("\nLPT1 not available. Aborting...");
fclose(fp1);
exit(-1);
}
printf("\nLPT1 DATA port address = %X (hex)", dport);
printf("\nChecking internal memory buffer...");
for(temp=0; temp < MEMORY; temp++)
{
ram[temp]=(unsigned char) temp;
}
for(temp=0; temp < MEMORY; temp++)
{
if( ram[temp] != (unsigned char) temp)
{
printf("\nError in internal memory allocation...Aborting.");
fclose(fp1);
exit(-1);
}
}
printf("\nInternal memory buffer OK.");
printf("\nReading Intelhex source file...:");
return_val=fill_buffer();
if(return_val == 0)
{
exit(0);
}
printf("\nIntel hex file %s read successful. Total bytes read =%d", argv[1], return_val);
fclose(fp1);
printf("\nStoring data in NVRAM module and Verifying...\n");
return_val=write_verify_bytes();
if(return_val == 0)
{printf("\nFailed to store data in NVRAM at address: %X (hex)\n", curr_address);
printf("Program value: %X\n", ram[curr_address]);
printf("Verify value: %X\n", error_byte);
exit(-1);
}
printf("\nData stored in NVRAM and verified");
printf("\nPower Off the RAMWriter and remove the NVRAM module");
return 1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -