📄 seeval.c
字号:
/*
* copyright (c) 1995,2000 TriMedia Technologies Inc.
*
* +-----------------------------------------------------------------+
* | This software is furnished under a license and may only be used |
* | and copied in accordance with the terms and conditions of such |
* | a license and with the inclusion of the this copy right notice. |
* | this software or any other copies of this software may not be |
* | provided or otherwise made available to any other person. The |
* | ownership and title of this software is not transferred. |
* +-----------------------------------------------------------------+
*
* Module name : seeval.c
*
* Description : Generates EEPROM file in the format used
* by Microchip technology's serial
* EEPROM programmer.
* Supports 1, 2, or 4 KB proms only.
* File formats are roughly known only in
* these cases.
*
* INPUT: a binary file f.eeprom generated by the program
* l1rom which adds 47 byte eeprom header and byte swaps the
* TM1 binary image
* For info on l1rom, see the comments in l1rom.c file
*
* RUN: seeval f.eeprom SIZE {where SIZE is 1 (KB), 2 (KB) or 4 (KB) }
*
* OUTPUT: f.tst contains an EEPROM file in the format used by
* Microchip technology's serial EEPROM programmer.
*
*
* Assumption: 1. f.eeprom contains less than 2048+47 bytes,
* prom size is 1, 2 or 4 KB or less
* 2. short is 2 bytes.
*
*/
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
/* max file size: 2 KB - prom header, then rounded down to nearest 4 bytes */
#define MAX_FILE_SIZE 2048+47
#define MAX_EPROM_SIZE (1024 * 4)
#define BUF_SIZE (MAX_EPROM_SIZE + 12)
#define NUM_SEEVAL_HEADER 3
void basename (char *fname, char *bname);
int
read_file(unsigned char *buffer, char *filename)
{
FILE *L1fp;
int L1fd, n, nbytes;
struct stat file_stat;
if ((L1fd = open (filename, O_RDONLY)) == -1) {
fprintf (stderr, "Unable to open file: %s\n", filename);
fprintf (stderr, "File doesn't exist or not readable\n");
exit(1);
}
if (fstat (L1fd, &file_stat)) {
fprintf (stderr, "Unable to fstat file: %s\n", filename);
exit(1);
}
nbytes = (unsigned long) file_stat.st_size;
close(L1fd);
if ((L1fp = fopen (filename, "rb")) == NULL) {
fprintf (stderr, "Unable to open file: %s\n", filename);
fprintf (stderr, "File doesn't exist or not readable\n");
exit(1);
}
if (nbytes > MAX_FILE_SIZE) {
fprintf (stderr, "File has %5d bytes. must be less than %5d bytes \n",
nbytes, MAX_FILE_SIZE);
exit(1);
}
n = fread (buffer, 1, nbytes, L1fp);
if (n != nbytes) {
fprintf (stderr, "Unable to read %5d bytes, error no: %5d\n",
nbytes, errno);
exit(1);
}
fprintf (stderr, "Bytes in EEPROM: %5d bytes\n", nbytes);
return (nbytes);
}
/*
* Header bytes are hard-coded. Read the TM-1 boot block paper
* to see what needs to go in here for AUTO boot.
*/
int
add_seeval_header (unsigned char buffer[], int prom_size)
{
int tmp;
int i = 0;
switch (prom_size) {
case 1:
buffer[i++] = 0x40; /* LSB of num of rows 64 */
buffer[i++] = 0x10; /* MSB of num of rows, LSB of num of cols = 0x10. */
/* rows * cols = 1024 */
buffer[i++] = 0;
break;
case 2:
buffer[i++] = 0x80; /* LSB of num of rows 128 */
buffer[i++] = 0x10; /* MSB of num of rows, LSB of num of cols = 0x10 */
/* rows * cols = 2048 */
buffer[i++] = 0;
break;
case 4:
buffer[i++] = 0; /* LSB of num of rows 256 */
buffer[i++] = 0x110; /* MSB of num of rows 256 = 0x1. LSB of cols = 0x10 */
/* total size is 256 * 16 = 4096 */
buffer[i++] = 0;
break;
default:
fprintf (stderr, "Prom size must be 1, 2 or 4\n");
exit(1);
}
fprintf (stderr, "\nSeeval Header Size: %5d bytes\n", NUM_SEEVAL_HEADER);
return (i);
}
main (int argc, char **argv)
{
int i, j, file_size;
int header_bytes, prom_size = 0;
FILE *fp;
char *o_file_name, *cp;
unsigned char padding[8] = {0};
unsigned char ibuffer[BUF_SIZE] = {0};
unsigned short obuffer[BUF_SIZE] = {0};
char *seeval_header = "Microchip Technology SEEVAL ";
if (argc < 3) {
fprintf (stderr, "Usage: prom file.eeprom eeprom_size (1, 2 or 4)\n");
exit(1);
}
prom_size = atoi (argv[2]);
switch (prom_size) {
case 1:
/* 0xa (newline) terminates seeval header for 24c08 (1 KB) prom */
seeval_header[27] = 0xa ;
break;
case 2:
/* 0xb terminates seeval header for 24c16 (2 KB) prom */
seeval_header[27] = 0xb ;
break;
case 4:
/* 0xd terminates seeval header for 24c32 (4 KB) prom */
seeval_header[27] = 0xd ;
break;
default:
fprintf (stderr, "Supported prom sizes are: 1, 2, 4 KB\n");
exit(1);
}
/* find output file name */
i = strlen(argv[1]);
o_file_name = (char *) malloc(i+5); /* .tst extension needs 4+1 chars */
if (o_file_name == NULL) {
fprintf (stderr, "unable to malloc\n");
exit(1);
}
/* skip all directory names */
if ((cp = strrchr(argv[1], '/')) == NULL) {
cp = argv[1];
}
basename (cp, o_file_name);
i = strlen(o_file_name);
o_file_name[i++] = '.';
o_file_name[i++] = 't';
o_file_name[i++] = 's';
o_file_name[i++] = 't';
o_file_name[i++] = '\0';
if ((fp = fopen(o_file_name, "wb")) == NULL) {
fprintf (stderr, "Could not open (binary) file %s for write\n",
o_file_name);
exit(1);
}
if ((i = fwrite (seeval_header, 1, 28, fp)) != 28) {
fprintf (stderr, "Unable to write 28 bytes of Seeval header."
"Wrote %5d bytes\n", i);
exit(1);
}
header_bytes = add_seeval_header(ibuffer, prom_size);
file_size = read_file(ibuffer + header_bytes, argv[1]);
if (file_size > prom_size*1024) {
fprintf (stderr, "EEprom size %5d less than no of bytes to be written\n",
prom_size*1024, file_size );
exit(1);
}
for (i = 0; i < (header_bytes + file_size); i++) {
obuffer[i] = (unsigned short) ibuffer[i];
/* printf ("%i: \t0x%x 0x%x\n", i, ibuffer[i], obuffer[i]); */
}
/* char array to short array. Seeval input format requires this */
j = fwrite(obuffer, sizeof(short), prom_size*1024, fp);
if (j != prom_size*1024) {
fprintf (stderr, "Unable to write %5d shorts. Wrote %5d \n",
prom_size*1024, j);
exit(1);
}
/* For some reason unknown to me, Seeval file contains 7 zero bytes
* at the end
*/
j = fwrite(padding, 1, 7, fp);
if (j != 7) {
fprintf (stderr, "Unable to write %5d padding bytes. Wrote %5d \n", 7, j);
exit(1);
}
close(fp);
fprintf (stderr, "Seeval Trailer Size: %5d bytes\n", 7);
return (0);
}
void
basename(char *fname, char *bname)
{
char *ptr, *ptr2;
int i;
if ((ptr = strrchr(fname, '.')) == NULL){
strcpy(bname, fname);
}
else {
for (ptr2 = fname, i=0; ptr2 != ptr; ptr2++, i++){
bname[i] = *ptr2;
}
bname[i] = '\0';
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -