Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // sorry for the improper indenting, i copied this from my source
- private void write(String file, char[] Data) throws FileNotFoundException {
- String filename = file;
- FileOutputStream fos = null;
- DataOutputStream dos = null;
- try {
- fos = new FileOutputStream(filename);
- dos = new DataOutputStream(fos);
- for(char d : Data) {
- dos.writeChar(d);
- }
- }
- catch (FileNotFoundException fnfe) {
- System.out.println("File not found" + fnfe);
- }
- catch (IOException ioe) {
- System.out.println("Error while writing to file" + ioe);
- }
- finally {
- try {
- if (dos != null) {
- dos.close();
- }
- if (fos != null) {
- fos.close();
- }
- }
- catch (Exception e) {
- System.out.println("Error while closing streams" + e);
- }
- }
- }
- private int get_lines(String file) throws IOException {
- InputStream is = new BufferedInputStream(new FileInputStream(file));
- try {
- byte[] c = new byte[1024];
- int count = 0;
- int readChars = 0;
- boolean empty = true;
- while ((readChars = is.read(c)) != -1) {
- empty = false;
- for (int i = 0; i < readChars; ++i) {
- if (c[0] == ';') {
- --count;
- }
- if (c[i] == '\n') {
- ++count;
- }
- }
- }
- return (count == 0 && !empty) ? 1 : count;
- } finally {
- is.close();
- }
- }
- private static String getChecksum(Serializable object) throws IOException, NoSuchAlgorithmException {
- ByteArrayOutputStream baos = null;
- ObjectOutputStream oos = null;
- try {
- baos = new ByteArrayOutputStream();
- oos = new ObjectOutputStream(baos);
- oos.writeObject(object);
- MessageDigest md = MessageDigest.getInstance("MD5");
- byte[] thedigest = md.digest(baos.toByteArray());
- return DatatypeConverter.printHexBinary(thedigest);
- } finally {
- oos.close();
- baos.close();
- }
- }
- /**
- * Compiles JCPU compatible code.
- * @param file File to compile.
- * @throws FileNotFoundException Thrown when the file doesnt exist.
- */
- public void compile(String file, String toFile) throws FileNotFoundException {
- try {
- String argchk = "NO CHECKSUM DEFINED";
- char[] data = new char[get_lines(file) + 1];
- System.out.println("Compiling " + file + " with about " + (get_lines(file) + 1) + " lines.");
- BufferedReader reader = new BufferedReader(new FileReader(file));
- String line = reader.readLine();
- int chr = 0;
- while (line != null) {
- if(line.startsWith(";")) {
- System.out.println("New line");
- }
- if(line.startsWith("P,")) {
- String arg = line.substring(2);
- int x = Integer.parseInt(arg);
- if(x > 999) {
- x = 0;
- }
- data[chr] = (char) (0x4000 + x);
- System.out.println("Print command. Value: " + Integer.toHexString(0x4000 + x));
- }
- if(line.startsWith("CHK,")) {
- argchk = line.substring(4);
- System.out.println("Verifying checksum after compile.");
- }
- if(line.startsWith("A,")) {
- String arg = line.substring(2);
- int x = Integer.parseInt(arg);
- if(x > 999) {
- x = 0;
- }
- data[chr] = (char) (0x1000 + x);
- System.out.println("Write to register A command. Value: " + Integer.toHexString(0x1000 + x));
- }
- if(line.startsWith("B,")) {
- String arg = line.substring(2);
- int x = Integer.parseInt(arg);
- if(x > 999) {
- x = 0;
- }
- data[chr] = (char) (0x2000 + x);
- System.out.println("Write to register B command. Value: " + Integer.toHexString(0x2000 + x));
- }
- if(line.startsWith("T,")) {
- String arg = line.substring(2);
- int x = Integer.parseInt(arg);
- if(x > 999) {
- x = 0;
- }
- data[chr] = (char) (0x3000 + x);
- System.out.println("Write to register Temp command. Value: " + Integer.toHexString(0x3000 + x));
- }
- if(line.startsWith("J,")) {
- String arg = line.substring(2);
- int x = Integer.parseInt(arg);
- if(x > 999) {
- x = 0;
- }
- data[chr] = (char) (0x5000 + x);
- System.out.println("Jump command. Value: " + Integer.toHexString(0x5000 + x));
- }
- if(line.startsWith("M,")) {
- String arg = line.substring(2);
- int x = Integer.parseInt(arg);
- if(x > 999) {
- x = 0;
- }
- data[chr] = (char) (0x6000 + x);
- System.out.println("Math command. Value: " + Integer.toHexString(0x6000 + x));
- }
- if(line.startsWith("NON")) {
- System.out.println("Writing a No Line");
- data[chr] = (char) (0x00EE);
- }
- if(line.startsWith("LIN")) {
- System.out.println("Writing an enable line");
- data[chr] = (char) (0x00ED);
- }
- if(line.startsWith("FJT")) {
- System.out.println("Writing an jump from location");
- data[chr] = (char) (0x00EF);
- }
- if(line.startsWith("CHC")) {
- System.out.println("Checksum verify command");
- data[chr] = (char) (0x00EC);
- }
- if(line.startsWith("END")) {
- System.out.println("Writing an end");
- data[chr] = (char) (0xF000);
- }
- if(line.startsWith("RGAB")) {
- System.out.println("Getting a compare");
- data[chr] = (char) (0x00EB);
- }
- if(line.startsWith("SKP")) {
- System.out.println("Skipping over next instruction");
- data[chr] = (char) (0x00EA);
- }
- chr = chr + 1;
- line = reader.readLine();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement