OpenCores
URL https://opencores.org/ocsvn/copyblaze/copyblaze/trunk

Subversion Repositories copyblaze

[/] [copyblaze/] [trunk/] [copyblaze/] [sw/] [tools/] [asm/] [pBlazASM/] [cpBlazeMRG/] [cpBlazeMRG.c] - Rev 68

Compare with Previous | Blame | View Log

//--------------------------------------------------------------------------------
//-- Company: 
//--
//-- File: cpBlazeMRG.c
//--
//-- Description:
//--	projet copyBalze
//--	initialize a VHDL memory
//--
//-- File history:
//-- v1.0: 20/10/11: Creation
//--
//-- Targeted device: ProAsic A3P250 VQFP100
//-- Author: AbdAllah Meziti
//--------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
 
#define PROG_NAME		"cpBlazeMRG"
#define PROG_REV		"01.00"
 
#define	ko	1024
 
#define PROGRAM_COUNT	1*ko			/* total program word */
#define MAX_LINE_COUNT	PROGRAM_COUNT	/* max 1000 lines allowed */
 
#define MAX_LINE_LENTH	200				/* max number of caractere per line */
 
#define EXTENSION_IN	".hex"
#define EXTENSION_OUT	".vhd"
 
char filename[MAX_LINE_LENTH];
FILE *ifp;
FILE *ofp;
FILE *ffp;
 
char linebuf[MAX_LINE_LENTH];
int line_count = 0;
 
unsigned program_word[PROGRAM_COUNT]; /* program word array */
 
 
/*====================================== */
void error_out(void)
{
	exit(1);
}
 
/*====================================== */
/* write program word in hex format */
void write_bin(void)
{
	int i;
	char *ptr;
 
	ptr = strstr(filename, ".log");
	*ptr = '\0';
	strcat(filename,".bin");
 
	ffp = fopen(filename, "w");
	if (ffp == NULL){
        printf("\nCan not open bin file\n");
		exit(1);
	}
 
	for(i = 0; i < PROGRAM_COUNT; i++){
		fprintf(ffp, "%3d : %05X\n", i, program_word[i]);
	}
 
	fclose(ffp);
}
 
/*====================================== */
/* write vhdl module ROM */
void write_vhd(void)
{
#define DATA_HEX
#define	WIDTH_ADDRESS		10
#define	WIDTH_INSTRUCTION	18
#define WIDTH_ROM			20
#define WIDTH_LENGTH		PROGRAM_COUNT
 
	int i, j;
	char *ptr;
	char basename[MAX_LINE_LENTH];
 
	time_t rawtime;
	struct tm * timeinfo;
 
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
	ptr = strstr(filename, ".bin");
	*ptr = '\0';
	strcpy(basename, filename);
	strcat(filename,".vhd");
 
	ffp = fopen(filename, "w");
	if (ffp == NULL){
        printf("\nCan not open vhd file\n");
		exit(1);
	}
 
	/* *************** */
	/* VHDL generation */
	/* *************** */
	// VHDL : header
	fprintf(ffp,"--------------------------------------------------------------------------------\n");
	fprintf(ffp,"-- AUTOMATICALLY GENERATED BY  : %s rev %s										 \n", PROG_NAME, PROG_REV);
	fprintf(ffp,"-- Company:                                                                     \n");
	fprintf(ffp,"--                                                                              \n");
	fprintf(ffp,"-- File: %s.vhd																 \n",basename);
	fprintf(ffp,"--                                                                              \n");
	fprintf(ffp,"-- Description:                                                                 \n");
	fprintf(ffp,"--	  projet copyBalze                                                           \n");
	fprintf(ffp,"--	  copyBalze µP code ROM content : width[%d] x depth[%d]						 \n", WIDTH_INSTRUCTION, PROGRAM_COUNT);
	fprintf(ffp,"--                                                                              \n");
	fprintf(ffp,"-- File history:                                                                \n");
	fprintf(ffp,"-- v1.0: %s", asctime (timeinfo));
	fprintf(ffp,"--                                                                              \n");
	fprintf(ffp,"-- Targeted device: ProAsic A3P250 VQFP100                                      \n");
	fprintf(ffp,"-- Author: AbdAllah Meziti                                                      \n");
	fprintf(ffp,"--------------------------------------------------------------------------------\n");
 
	// VHDL : library
	fprintf(ffp,"library ieee;\n");
	fprintf(ffp,"use ieee.std_logic_1164.all;\n");
	fprintf(ffp,"use ieee.numeric_std.all;\n\n");
 
	// VHDL : entity
	fprintf(ffp,"--------------------------------------------------------------------------------\n");
	fprintf(ffp,"-- Entity: %s                                                         \n",basename);
	fprintf(ffp,"--                                                                              \n");
	fprintf(ffp,"-- Description:                                                                 \n");
	fprintf(ffp,"--	                                                                             \n");
	fprintf(ffp,"--	REMARQUE:                                                                    \n");
	fprintf(ffp,"--                                                                              \n");
	fprintf(ffp,"--	                                                                             \n");
	fprintf(ffp,"-- History:                                                                     \n");
	fprintf(ffp,"-- v1.0: %s", asctime (timeinfo));
	fprintf(ffp,"-- AM: Creation		                                                         \n");
	fprintf(ffp,"-- ---------------------                                                        \n");
	fprintf(ffp,"--				                                                                 \n");
	fprintf(ffp,"--------------------------------------------------------------------------------\n");
 
	fprintf(ffp,"entity %s is\n",basename);
	fprintf(ffp,"\tgeneric\n");
	fprintf(ffp,"\t(\n");
	fprintf(ffp,"\t	GEN_WIDTH_PC		: positive := %d;\n",WIDTH_ADDRESS);
	fprintf(ffp,"\t	GEN_WIDTH_INST		: positive := %d\n",WIDTH_INSTRUCTION);
	fprintf(ffp,"\t);\n");
	fprintf(ffp,"\tport\n");
	fprintf(ffp,"\t(\n");
	fprintf(ffp,"\t\tClk_i\t\t: in std_ulogic;\n");
	fprintf(ffp,"\t\tAddress_i\t: in std_ulogic_vector(GEN_WIDTH_PC-1 downto 0);\n");
	fprintf(ffp,"\t\tDout_o\t\t: out std_ulogic_vector(GEN_WIDTH_INST-1 downto 0)\n");
	fprintf(ffp,"\t);\n");
	fprintf(ffp,"end;\n\n");
 
	// VHDL : architecture
	fprintf(ffp, "--------------------------------------------------------------------------------\n");
	fprintf(ffp, "-- Architecture: RTL                                                            \n");
	fprintf(ffp, "-- of entity : %s\n", basename);
	fprintf(ffp, "--------------------------------------------------------------------------------\n");
	fprintf(ffp,"architecture rtl of %s is\n\n", basename);
 
	fprintf(ffp,"\tconstant ROM_WIDTH\t: INTEGER:= %d;\n",WIDTH_ROM);
	fprintf(ffp,"\tconstant ROM_LENGTH\t: INTEGER:= %d;\n\n",WIDTH_LENGTH);
 
	fprintf(ffp,"\tsubtype rom_word is std_ulogic_vector(ROM_WIDTH-1 downto 0);\n");
	fprintf(ffp,"\ttype\trom_table is array (0 to ROM_LENGTH-1) of rom_word;\n\n");
 
	fprintf(ffp,"constant rom: rom_table := rom_table'(\n");
	// VHDL : start initialization of the ROM memory
	for(i = 0; i < PROGRAM_COUNT-1; i++){
#ifdef	DATA_HEX
		fprintf(ffp, "\tx\"");
		fprintf(ffp, "%05X", program_word[i]);
#else	//DATA_BIN
		fprintf(ffp, "\t\"");
		for(j = WIDTH_INSTRUCTION-1; j >= 0; j--) fprintf(ffp, "%d", (program_word[i]>>j) & 1); //print binary
#endif
		fprintf(ffp, "\",");
		fprintf(ffp, "\t-- %03X%\n",i);
	}
 
#ifdef	DATA_HEX
		fprintf(ffp, "\tx\"");
		fprintf(ffp, "%05X", program_word[i]);
#else	//DATA_BIN
		fprintf(ffp, "\t\"");
		for(j = WIDTH_INSTRUCTION-1; j >= 0; j--) fprintf(ffp, "%d", (program_word[i]>>j) & 1); //print binary
#endif
		fprintf(ffp, "\"");
		fprintf(ffp, "\t-- %03X%\n",i);
 
		fprintf(ffp, "\t);\n\n");
	// VHDL : end Initialize the ROM memory
 
	fprintf(ffp,"\tsignal	iData	: std_ulogic_vector(GEN_WIDTH_INST-1 downto 0);\n");
	fprintf(ffp,"\n");
	fprintf(ffp,"begin\n");
	fprintf(ffp,"\n");
 
	// VHDL : end Initialize the ROM memory
	fprintf(ffp,"\t--------------------------------------------------------------------------------\n");
	fprintf(ffp,"\t-- Process : ROM_Proc                                                           \n");
	fprintf(ffp,"\t-- Description: ROM Memory                                                      \n");
	fprintf(ffp,"\t--------------------------------------------------------------------------------\n");
	fprintf(ffp,"\tROM_Proc : process (Clk_i)\n");
	fprintf(ffp,"\tbegin\n");
	fprintf(ffp,"\t\tif ( rising_edge(Clk_i) ) then\n");
	fprintf(ffp,"\t\t\tiData <= rom(to_integer(unsigned(Address_i)))(GEN_WIDTH_INST-1 downto 0);\n");
	fprintf(ffp,"\t\tend if;\n");
	fprintf(ffp,"\tend process ROM_Proc;\n");
	fprintf(ffp,"\n");
 
	fprintf(ffp,"\t--------------------------------------------------------------------------------\n");
	fprintf(ffp,"\t-- Outputs                                                                      \n");
	fprintf(ffp,"\t--------------------------------------------------------------------------------\n");
	fprintf(ffp,"\tDout_o <= iData(GEN_WIDTH_INST-1 downto 0);\n");
 
	fprintf(ffp,"\n");
	fprintf(ffp,"end rtl;\n");
 
	fclose(ffp);
}
 
 
#define xtod(c) ((c>='0' && c<='9') ? c-'0' : ((c>='A' && c<='F') ? c-'A'+10 : ((c>='a' && c<='f') ? c-'a'+10 : 0)))
 
int HextoDec(char *hex)
{
    if (*hex==0) return 0;
    return  HextoDec(hex-1)*16 +  xtod(*hex) ; 
}
 
int xstrtoi(char *hex)      // hex string to integer
{
    return HextoDec(hex+strlen(hex)-1);
}
 
/*====================================== */
int main(int argc, char **argv)
{
	char *ptr;
	int i;
 
    if(argc != 2){
		printf("\nCommand line syntax:\n\n %s file%s\n", PROG_NAME, EXTENSION_IN);
		exit(1);
	}
 
	strcpy(filename, argv[1]);
	ptr = strstr(filename, EXTENSION_IN);
	if (ptr == NULL){
		printf("\nInvalid file type, use %s extension\n", EXTENSION_IN);
		exit(1);
	}
	*ptr = '\0';
	strcat(filename,".log");
 
    // Open the ifp="input file"
	ifp = fopen(argv[1], "r");
    if (ifp == NULL){
        printf("\nCan not open input file\n");
		exit(1);
	}
 
	// Open the ofp="output file" (log)
	ofp = fopen(filename, "w");
	if (ofp == NULL){
        printf("\nCan not open output file\n");
		exit(1);
	}
	fprintf(ofp, "ROM Code generation %s logfile\n\n",EXTENSION_IN);
 
 
	printf("Reading input file...\n");
	fprintf(ofp,"Reading input file...\n");
 
	// Initialize the "program_word" table
	for(i = 0; i < PROGRAM_COUNT; i++)
		program_word[i] = 0xFFFFF;
 
	// Read the input file
	while ( fgets(linebuf, 128, ifp) != NULL ) {
		if(line_count >= MAX_LINE_COUNT){
			printf("\nInput exceed maximum line number\n");
            goto error;
			//error_out();
		}
 
		// Extract the data from In file to program_word
		ptr = strstr(linebuf, "\r\n");
		*ptr = '\0';
 
		//xtoi(linebuf, &program_word[line_count]);
 
		program_word[line_count] = xstrtoi(linebuf);
 
		//printf("\n%s=>%05X",linebuf,program_word[line_count]);
 
		line_count++;
	}
 
error:
    //error_out();
 
	printf("\nWrite output files...\n");
	fprintf(ofp,"Write output files...\n");
	write_bin();
	write_vhd();
 
	printf("Program completed...\n");
	fprintf(ofp,"Program completed...\n");
	fclose(ifp);
	fclose(ofp);
	return(0);
}
 
 

Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.