Rom2vhdl

From Hamsterworks Wiki!

Jump to: navigation, search

This small C routine converts an 8k binary file into VHDL that uses four block RAMs, which can then be used within a VHDL project.

It is designed to dump the ubiquitous 2764 ROM.

Once compiled, use something like this to generate the VHDL:

rom2vhdl rom.01 > rom_01.vhd

Source file

// rom2vhdl.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"   /* For Windows */

#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <string.h>

static unsigned char data[8192];
static char itoa[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

/**************************************************************************************/
static void printInitStr(unsigned char *data, int line, unsigned char lowbit)
{
   int i;
   printf("      INIT_%02X => X\"",line);
   for(i = 63; i >= 0; i--)
   {
     unsigned char v0,v1;
     int index = i*2+line*128;
     assert(index < 8192);
     v0 = (data[index]>>lowbit) & 0x3;
     v1 = (data[index+1]>>lowbit) & 0x3;
     putchar(itoa[ (v1<<2) + v0]);
   }
   putchar('"');
}

/**************************************************************************************/
static int genROM2bits(char *name, unsigned char lowbit, unsigned char *data)
{
   char *bits;
   int i;
   switch(lowbit)
   {
      case 0: bits = "10"; break;
      case 2: bits = "32"; break;
      case 4: bits = "54"; break;
      case 6: bits = "76"; break;
      default: return 0;
   }
   printf("RAMB16_S2_%s_%s : RAMB16_S2\n",name,bits);
   printf("   generic map (\n");
   printf("      INIT       => X\"0\",\n");
   printf("      SRVAL      => X\"0\",\n");
   printf("      WRITE_MODE => \"WRITE_FIRST\",\n");

   for(i = 0; i < 0x3F; i++)
   {
     printInitStr(data,i,lowbit);
     printf(",\n");
   }
   printInitStr(data,i,lowbit);
   printf("\n");

   printf("   ) port map (\n");
   printf("      DO   => D%s,\n",bits);
   printf("      ADDR => ADDR,\n");
   printf("      CLK  => CLK,\n");
   printf("      DI   => \"00\",\n");
   printf("      EN   => '1',\n");
   printf("      SSR  => '0',\n");
   printf("      WE   => '0'\n");
   printf("   );\n");
   return 0;
}

/**************************************************************************************/
int main(int argc, char *argv[])
{
  char *name;
  FILE *f;
  int i;

  if(argc != 2)
  {
    fprintf(stderr, "Please supply file name to convert\n");
    return 0;
  }

  f = fopen(argv[1],"rb");
  if(f == NULL) {
    fprintf(stderr, "Unable to open file '%s'\n",argv[1]);
    return 0;
  }

  if(fread(data,8192,1,f) != 1)
  {
    fprintf(stderr, "Unable to read file\n");
    return 0;
  }
  fclose(f);

  name = (char *)malloc(strlen(argv[1])+1);

  for(i=0; argv[1][i] != '\0'; i++)
   if(argv[1][i] == '.' || argv[1][i] == '-' )
      name[i] = '_';
   else
      name[i] = argv[1][i];
  name[i++] = '\0';

  printf("----------------------------------------------------------------------------------\n");
  printf("-- Produced by from Rom2VHDL by Mike Field <hamster@snap.net.nz>\n");
  printf("--\n");
  printf("-- Description: %s.vhd\n",name);
  printf("--\n");
  printf("--\n");
  printf("----------------------------------------------------------------------------------\n");
  printf("library IEEE;\n");
  printf("use IEEE.STD_LOGIC_1164.ALL;\n");
  printf("Library UNISIM;\n");
  printf("use UNISIM.vcomponents.all;\n");
  printf("\n");
  printf("entity %s is\n", name);
  printf("    Port ( clk  : in  STD_LOGIC;\n");
  printf("           addr : in  STD_LOGIC_VECTOR (12 downto 0);\n");
  printf("           data : out STD_LOGIC_VECTOR ( 7 downto 0));\n");
  printf("end %s;\n",name);

  printf("architecture Behavioral of %s is\n",name);
  printf("   signal d10 : std_logic_vector(1 downto 0);\n");
  printf("   signal d32 : std_logic_vector(1 downto 0);\n");
  printf("   signal d54 : std_logic_vector(1 downto 0);\n");
  printf("   signal d76 : std_logic_vector(1 downto 0);\n");
  printf("BEGIN\n");
  printf("   data <= d76 & d54 & d32 & d10;\n");
  printf("\n");

  genROM2bits(name, 0, data);
  genROM2bits(name, 2, data);
  genROM2bits(name, 4, data);
  genROM2bits(name, 6, data);
  printf("END Behavioral;\n");
  return 0;
}

Personal tools