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

📄 blockgen.java

📁 Java Op Processor java vhdl processor
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		}				fr.close();				StringBuffer binary_buffer = new StringBuffer();				for (int i = 0; i < mem.length; i ++) {			binary_buffer.append(toBinary(mem[i], width));		}				return binary_buffer.toString();	}		/**	 * Brute force parse of a text file to extract hex data.	 * Good for files that only contain the desired hex data and whitespace or	 * delimeters such as commas or semicolons.	 * 	 * @return String containing binary data.	 * @throws IOException	 */			public String parseTextFile() throws IOException {				FileReader fr = new FileReader(src_filename);		BufferedReader br = new BufferedReader(fr);				StringBuffer hex_buffer = new StringBuffer();				String line = br.readLine();				while (line != null) {						line = line.toUpperCase();			line = findAndReplace(line, "0X", ""); 			line = endBefore(line, "/"); 			line = filterNonHexDigits(line);			if (!stringEmpty(line)) {				hex_buffer.append(line);			}						line = br.readLine();		}				br.close();				return longHexStringToBinary(hex_buffer.toString());	}		/**	 * Test for whether a string is null or of zero length.	 * 	 * @param str string to test	 * @return true if string is empty or null	 */	public static boolean stringEmpty(String str) {		if (str == null) return true;		if (str.length() == 0) return true;		return false;	}		/**	 * Given a string with binary data, divide it into words of length <code>w</code> and	 * subdivide it based on the port width in <code>p_w</code> and then select which of the subdivisions	 * to extract given the value in <code>b_c</code>.	 */		public static String getBlockBits(String input, int p_w, int w, int b_c) {		StringBuffer output = new StringBuffer();		int n_p = w / p_w;		b_c = n_p - b_c - 1;		for (int i = 0; i < input.length(); i += w) {			int s = i + (b_c * p_w);			int e = s + p_w;			output.append(input.substring(s, e));		}		return output.toString();	}		/**	 * Reverse the input string by extracting blocks of length len and reordering them.	 * For example, if <code>len</code> equals <code>2</code> and <code>input</code> is	 * <code>A1B2C3D4E5</code>, then the return value will be <code>E5D4C3B2A1</code>.	 * @param input string to reverse	 * @param len length of blocks within string	 * @return reversed string	 */		public static String reverse(String input, int len) {		StringBuffer output = new StringBuffer();		int i = input.length() - len;		while (i >= 0) {			output.append(input.substring(i, i + len));			i -= len;		}		return output.toString();	}		/**	 * Split the input string into an array of strings of length <code>count</code>	 * @param input string to split	 * @param count character count to split at	 * @return array of strings	 */		public static String[] split(String input, int count) {		ArrayList list = new ArrayList();		int i = 0;		while (i < input.length()) {			list.add(input.substring(i, Math.min(i + count, input.length())));			i += count;		}		return (String[])list.toArray(new String[]{});	}		/**	 * Replace all instances of string <code>find</code> in the input string with string	 * <code>replace</code>.	 * @param input string to search	 * @param find string to search for	 * @param replace string to replace with	 * @return processed string	 */	public static String findAndReplace(String input, String find, String replace) {		String output = input;		int idx;		while ((idx = output.indexOf(find)) != -1) {			output = output.substring(0, idx) + replace + output.substring(idx+find.length());		}		return output;			}		/**	 * Input parameter <code>terms<code> containst a set of <code>find</code> and <code>replace</code> pairs to 	 * apply to the input string.	 * @param input string to search	 * @param terms hashmap of find and replace pairs	 * @return processed string	 */	public static String findAndReplace(String input, HashMap terms) {		String output = input;		for (Iterator i = terms.keySet().iterator(); i.hasNext();) {			String find = i.next().toString();			String replace = terms.get(find).toString();						int idx;			while ((idx = output.indexOf(find)) != -1) {				output = output.substring(0, idx) +				replace +				output.substring(idx+find.length());			}			//Pattern p = Pattern.compile(find, Pattern.CASE_INSENSITIVE);			//Matcher m = p.matcher(output);			//output = m.replaceAll(replace);					}		return output;	}		/**	 * Terminate the input string at the first instance of the string passed as <code>terminator</code>.	 * @param input string to terminate	 * @param terminator string to find in the input string and to terminate input before	 * @return terminated string	 */		public static String endBefore(String input, String terminator) {		String output = input;		int idx;		if ((idx = output.indexOf(terminator)) != -1) {			output = output.substring(0, idx);		}		return output;			}		/**	 * Remove any characters that are not 0 to 9 or A to F (case sensitive).	 * 	 * @param input string to process for non-hex characters	 * @return string with non-hex characters removed	 */		public static String filterNonHexDigits(String input) {		StringBuffer output = new StringBuffer();		for (int i = 0; i < input.length(); i ++) {			char c = input.charAt(i);			if (Character.isDigit(c) || ((c >= 'A') && (c <= 'F'))) {				output.append(c);			}		}		return output.toString();			}		/**	 * Read a file into a string.	 * 	 * @param file file to read	 * @return contents of file	 */		public static String readFileContents(File file) {		String contents = null;		try {			FileInputStream fis = new FileInputStream(file);			byte[] data = new byte[(int)file.length()];			int r = fis.read(data);			contents = new String(data, 0, r);		}		catch (Exception e) {		}		return contents;	}		/**	 * Read a file into a string.	 * 	 * @param file_name filename of file to read	 * @return contents of file	 */		public static String readFileContents(String file_name) {		File file = new File(file_name);		return readFileContents(file);	}		/**	 * Convert an integer into a hex string with correct zero padding for word aligment.	 * 	 * @param i integer value to convert	 * @param word word size to align hex return value to	 * @return hex return value	 */		public static String toHex(int i, int word) {		String result = Integer.toHexString(i);		if ((result.length() % word) != 0) result = zero(word - (result.length() % word)) + result;		return result;	}		/**	 * Convert an long integer into a binary string with correct zero padding for word aligment.	 * 	 * @param n long integer value to convert	 * @param word word size to align hex return value to	 * @return hex return value	 */		public static String toBinary(long n, int word) {		String result = Long.toBinaryString(n);		if ((result.length() % word) != 0) result = zero(word - (result.length() % word)) + result;		return result;	}		/**	 * Generate a string of zeros of the specified count	 * @param count number of zeroes to generate	 * @return string with the specified number of zeros	 */		public static String zero(int count) {		StringBuffer result = new StringBuffer();		while (result.length() < count) result.append('0');		return result.toString();	}		/**	 * Convert a hex string into a binary string with correct zero padding for word aligment.	 * Use for hex string of less than 8 characters.	 * @param hex input hex string	 * @param word word size to align binary return value to 	 * @return binary return value	 */		public static String hexToBinary(String hex, int word) {		int i = Integer.parseInt(hex, 16);		return toBinary(i, word);	}		/**	 * Convert a hex string into a binary string with correct zero padding for word aligment.	 * Use for hex string of 8 or more characters.	 * @param hex input hex string	 * @return binary return value	 */		public static String longHexStringToBinary(String hex) {		StringBuffer output = new StringBuffer();		for (int i = 0; i < hex.length(); i++) {			String h = hex.substring(i, i + 1);			output.append(hexToBinary(h, 4));		}		return output.toString();	}		/**	 * Convert a binary string into a hex string.	 * 	 * @param input binary string	 * @return hex string	 */	public static String longBinaryStringToHex(String input) {		StringBuffer output = new StringBuffer();		for (int i = 0; i < input.length(); i += 4) {			String nibble = input.substring(i, i + 4);			int n = 0;			if (nibble.charAt(3) == '1') n += 1;			if (nibble.charAt(2) == '1') n += 2;			if (nibble.charAt(1) == '1') n += 4;			if (nibble.charAt(0) == '1') n += 8;			output.append(Integer.toHexString(n));		}		return output.toString();	}		/**	 * Given a binary string of a length less that <code>size</code>, append	 * trailing zeroes to pad it to the specified size.	 * @param input the input string	 * @return string with the correct length	 */		public static String padTrailingBitsToBlockSize(String input, int size) {		StringBuffer output = new StringBuffer(input);		if (output.length() < size) output.append(zero(size - output.length()));		return output.toString();	}		/**	 * Process the command line parameters	 * 	 * @param clist command line parameters passed to main() method	 * @return true if options processed correctly	 */	public boolean processOptions( String clist[] ){		boolean success = true;				for( int i = 0; i < clist.length; i++ ){			if ( clist[i].equals("-w")  ){				width =  Integer.parseInt(clist[ ++i ]);			} else if ( clist[i].equals("-d")  ){				depth =  Integer.parseInt(clist[ ++i ]);			} else if ( clist[i].equals("-b")  ){				block =  Integer.parseInt(clist[ ++i ]);			} else if ( clist[i].equals("-m")  ){				module_name = clist[ ++i ];			} else if ( clist[i].equals("-o")  ){				dst_dir = clist[ ++i ];			} else if ( clist[i].equals("-pd")  ){				preserve_depth = true;			} else if ( clist[i].startsWith("-")  ){				printUsage();				System.out.println("Unknown parameter: " + clist[i]);				System.exit(-1);			} else { 				if (stringEmpty(src_filename)) src_filename = clist[i];				else dst_filename = clist[i];			}		}				return success;	}		public static void printUsage() {		System.out.println("BlockRAM VHDL Module Generator");		System.out.println("By Ed Anuff <ed@anuff.com>");		System.out.println("Version built: " + build_date);		System.out.println();		System.out.println("Usage:");		System.out.println("java BlockGen [-w #] [-d #] [-b #] [-m name] [-o dir] [-pd] [src [dst]]");		System.out.println();		System.out.println("Generates a memory module in VHDL using Xilinx Synchronous Block RAM with");		System.out.println("the given parameters.");		System.out.println();		System.out.println("Input file should be in Altera MIF (.mif) format or be a text file with");		System.out.println("hex data that can be extracted.");		System.out.println();		System.out.println("If no input file is specified, an uninitialized memory module will be");		System.out.println("generated.");		System.out.println();		System.out.println("Parameters:");		System.out.println(" -w  : RAM data width in # of bits to a data word");		System.out.println(" -d  : RAM address depth in # of words");		System.out.println(" -b  : Size of Block RAM for the target device - ");		System.out.println("       4096 for Spartan2/2E/Virtex/E");		System.out.println("       16384 for Spartan3/Virtex2/2Pro/2ProX");		System.out.println(" -m  : Name of module generated");		System.out.println(" -o  : Output directory");		System.out.println(" -pd : Preserve depth - Use specified address depth even");		System.out.println("       if larger address range is available in the Block RAM used.");		System.out.println();	}		public static void main(String[] args) {		if (args.length < 1) {			printUsage();			System.exit(-1);		}		BlockGen bgen = new BlockGen(args);		bgen.process();				//System.out.println(hexToBinary("A3", 8));			}}

⌨️ 快捷键说明

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