⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tpu3misc.c

📁 mpc564 时钟中断 时钟中断
💻 C
字号:
/**************************************************************************/
/* FILE NAME: tpu3misc.c                      COPYRIGHT (c) MOTOROLA 2000 */
/* VERSION:  1.0                                  All Rights Reserved     */
/*                                                                        */
/* DESCRIPTION:                                                           */
/* This program reads an s-record file and calculates a MISC value for    */
/* the file. The s-record must be an s19 file starting at 0.              */
/*                                                                        */
/*========================================================================*/
/* COMPILER: gcc 2.95.2                       AUTHOR: Jeff Loeliger       */
/*                                                                        */
/* UPDATE HISTORY                                                         */
/* REV      AUTHOR      DATE       DESCRIPTION OF CHANGE                  */
/* ----  -----------  ---------    ---------------------                  */
/* 1.00  J. Loeliger  25/Jul/00    Initial version of program.            */
/**************************************************************************/

/*****************
 * include files *
 *****************/
#include <stdio.h>
#include <stdlib.h>
#include "string.h"

/********************
 * global constants *
 ********************/
#define POLY 0x80400007 /*this comes from the polynomial: 1+x+x^2+x^22+x^31 */
#define TRUE 1
#define FALSE 0

/********************
 * global variables *
 ********************/
FILE *input_file;
char *program_name;
unsigned char *sram;		/* 8 bit data from s-record */
unsigned int *data;		/* 32 bit data in host computer format */
unsigned int DPTRAM_size = 6;   /* size of the DPTRAM module */
         			/* default size is 6k        */
unsigned int DPTRAM_fill = 0;   /* value to put in used DPRTAM locations */
         			/* default value is 0                    */
unsigned int verbose = FALSE;   /* default is verbose mode off */

/***********************
 * function prototypes *
 ***********************/
void usage (void);
void read_file (FILE * input_file);
int calc_misc (void);
int get_hex_nib (void);
int get_hex_byte (void);
int get_hex_word (void);
int get_hex_3bytes (void);
int get_hex_long (void);

/****************
 * main program *
 ****************/
int main (int argc, char *argv[])
{
    unsigned int misc = 0;
    int i;

/* save program name for future use */
    program_name = argv[0];

/* loop for each option
 * stop when we run out of arguments
 * or we get an argument without a dash
 */
    while ((argc > 1) && (argv[1][0] == '-')) {
        /*argv[1][1] is the actual option character */
        switch (argv[1][1]) {
        case 'h':
            usage ();
            return (1);
            break;
        case 'f':
            sscanf (&argv[2][0], "%x", &DPTRAM_fill);
            ++argv;
            --argc;
            break;
        case 's':
            sscanf (&argv[2][0], "%x", &DPTRAM_size);
            ++argv;
            --argc;
            break;
        case 'v':
            verbose = TRUE;     /* turn verbose mode on */
            break;
        default:
            printf ("ERROR unknown option %s\n", argv[1]);
            usage ();
            return (1);
        }
        ++argv;
        --argc;
    }

/* check for filename */
    if (argc == 1) {
        printf ("ERROR a filename must be specified.\n");
        usage ();
        return (1);
    }

/* allocate memory for arrays */
    sram = malloc ((DPTRAM_size * 1024));
    data = calloc ((DPTRAM_size * 256), sizeof (unsigned int));

    if (sram == NULL || data == NULL) {
        printf ("ERROR can not allocate enough memory.\n");
        return (1);
    }

/* fill memory with default value */
    memset (sram, DPTRAM_fill, (DPTRAM_size * 1024));


/* check filename */
    if ((input_file = fopen (argv[1], "r")) == NULL) {
        printf ("ERROR can not open file %s\n", argv[1]);
    }
    else {

        read_file (input_file);

        fclose (input_file);

        printf ("%s version 1.0 by Jeff Loeliger - 25 July 2000 Copyright Motorola 2000.\n\n", program_name);
        printf ("The MISC value calculation used a RAM fill value of 0x%X and a size of %uk.\n", DPTRAM_fill, DPTRAM_size);
        printf ("DPTRAM MISC value for the file %s is 0x%X.\n", argv[1], calc_misc ());
    };

};

/*******************************************************************************
FUNCTION        : void usage()
PURPOSE         : This function displays the usage information.
INPUTS NOTES    : none
RETURNS NOTES   : none
GENERAL NOTES   : This function uses the program_name global variable.
*******************************************************************************/
void usage (void)
{
    printf ("Usage: %s [options] filename\n", program_name);
    printf ("Calculate MISC value for DPTRAM and TPU3.\n\n");

    printf ("Options:\n");
    printf ("  -f n  fill value (32 bit) for unused area of DPTRAM (0 is default)\n");
    printf ("  -h    help, display this help and exit.\n");
    printf ("  -s n  size, where n is the size of the DPTRAM in kbytes (6 for 6k is default)\n");
    printf ("  -v    verbose, list intermediate values of calculation\n\n");
}

/*******************************************************************************
FUNCTION        : void read_file (FILE * input_file)
PURPOSE         : This function reads the input file and creates a binary
                    image in the sram array.
INPUTS NOTES    : FILE structure
RETURNS NOTES   : none
GENERAL NOTES   : This function only reads S19 files. This is the only function
                   that needs to be changed to support other s-record formats.
*******************************************************************************/
void read_file (FILE * input_file)
{
    char line[100];
    int address;
    int type;
    int length;
    int i;

    while (!feof (input_file)) {

        if (toupper (fgetc (input_file)) != 'S') {
            /* ignore line is it does not start with S */
            fgets (line, sizeof (line), input_file);
        }
        else {

            type = get_hex_nib ();
            if (type == 1) {    /* only process S1 lines */

                length = get_hex_byte ();
                address = get_hex_word ();

                for (i = 0; i < (length - 3); i++) {
                    sram[address] = get_hex_byte ();
                    address = address + 1;
                }
            }
            fgets (line, sizeof (line), input_file);
        }
    }

};

/*******************************************************************************
FUNCTION        : void calc_misc()
PURPOSE         : This function calculates the MISC value.
INPUTS NOTES    : none
RETURNS NOTES   : MISC value
GENERAL NOTES   :
*******************************************************************************/
int calc_misc (void)
{
    int j;	/* loop counters */
    int i;

    unsigned int misc = 0;


    /*first move byte array in sram to long words in data */
    /*This changes the big endian data in the s-record to 32 bit integers in the host */
    /*  because the value is calculated it will work with a big or little endian host */
    for (i = 0; i < ((DPTRAM_size*1024)-1); i += 4) {
        data[i / 4] = (sram[i]<<24) + (sram[i + 1] << 16) + (sram[i + 2]<<8) + (sram[i + 3]);
    }

    for (j = ((DPTRAM_size*1024)/4)-1; j >= 0 ; j--) {
        if (verbose)
            printf ("Address: %04X data: %08X %i, carry=%1X, ",j*4, data[j], data[j], misc & 0x1);
        if (misc & 0x1) {
            misc >>= 1;
            misc ^= POLY;
        }
        else {
            misc >>= 1;
        }
        misc ^= data[j];
        if (verbose)
            printf ("New Signature is: %08X\n", misc);
    }
    return (misc);
};


/*******************************************************************************
FUNCTION        : int get_hex_nib()
PURPOSE         : This function reads a hex nibble (4 bits) from an input file.
INPUTS NOTES    : none
RETURNS NOTES   : hex value of ASCII nibble
GENERAL NOTES   : none
*******************************************************************************/
int get_hex_nib (void)
{
    int c;

    c = toupper (fgetc (input_file));
    if (!isxdigit (c))
        return (0);
    if (isdigit (c))
        return (c - '0');
    else
        return (c - 'A' + 10);
}

/*******************************************************************************
FUNCTION        : int get_hex_byte()
PURPOSE         : This function reads a hex byte (8 bits) from an input file.
INPUTS NOTES    : none
RETURNS NOTES   : hex value of ASCII byte
GENERAL NOTES   : none
*******************************************************************************/
int get_hex_byte (void)
{
    int x;

    x = get_hex_nib () << 4;
    return (x + get_hex_nib ());
}

/*******************************************************************************
FUNCTION        : int get_hex_word()
PURPOSE         : This function reads a hex word (16 bits) from an input file.
INPUTS NOTES    : none
RETURNS NOTES   : hex value of ASCII word
GENERAL NOTES   : none
*******************************************************************************/
int get_hex_word (void)
{
    int x;

    x = get_hex_byte () << 8;
    return (x + get_hex_byte ());
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -