mifwrite.java

Download readbin.zip.

This program reads a binary executable file and generates a mif format file. It uses readbin.java to actually read the input file.


// mifwrite.java
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;

public class mifwrite
{
	public static void main(String args[])
	{
	    String filename;
	    Scanner inp = new Scanner(System.in);
	    if (args.length>0) filename = args[0];
	    else {
		System.out.print("Enter input (bin) file name: ");
		filename = inp.nextLine();
	    }
	    readbin bin = new readbin();
	    boolean found = bin.open(filename);
	    if (!found) System.exit( -1 );
	    if (args.length>1) filename = args[1];
	    else {
		System.out.print("Enter output (mif) file name: ");
		filename = inp.nextLine();
	    }
	    int offset = 0;
	    if (args.length>2) offset = (int) Long.parseLong(args[2],16);

	    mifwrite mif = new mifwrite();
	    if (!mif.open(filename)) System.exit(-1);
	    mif.write(bin,offset);

	}

	private Formatter output;
	public boolean open(String filename)
	{
	    try {
		output = new Formatter(filename);
	    }
	    catch (FileNotFoundException e) {
		System.err.println("Error opening file: " + filename);
		return false;
	    }
	    catch (SecurityException e) {
		System.err.println("You do not have access to this file: " + filename);
		return false;
	    }
	    return true;
	}

	public void write(readbin bin, int offset)
	{
		int address, contents;
		boolean first = true;
		int ndepth = 256;
		int nwidth = 32;
		output.format("DEPTH = %d;\n",ndepth);
		output.format("WIDTH = %d;\n\n",nwidth);
		output.format("ADDRESS_RADIX = HEX;\n");
		output.format("DATA_RADIX = HEX;\n");
		address = 0;
		offset = offset>>2;
		while (bin.hasNext()) {
		    if (address<offset) {
			offset--;
			continue;
		    }
		    if (first) {
			first = false;
			//System.out.printf("\n-- address_offset %x\n\n",address_offset);
			output.format("CONTENTS\n  BEGIN\n");
			output.format("[0..%x]   :  0;\n",ndepth-1);
		    }
		    contents = bin.getInt();
		    output.format("%04x  : %08x;\n",address,contents);
		    address++;
		    if (address>=ndepth) break;
		}
		output.format("END;\n");
		output.close();
	}
}


Maintained by John Loomis, updated Sun Feb 04 21:34:56 2007