Advertisement
TheGhastModding

EEPROMWriter

Aug 9th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.53 KB | None | 0 0
  1. package theGhastModding.AVRCom.main;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6.  
  7. public class EEPROMWriter {
  8.    
  9.     private AVRCom com;
  10.     private int eepromSize;
  11.    
  12.     public EEPROMWriter(AVRCom com, int eepromSize){
  13.         this.com = com;
  14.         this.eepromSize = eepromSize;
  15.     }
  16.    
  17.     public void write(byte[] data) throws Exception {
  18.         if(data.length > eepromSize) throw new Exception("Array too big to fit in EEPROM");
  19.         com.writeByte((byte)'w');
  20.         System.out.println((char)com.readByte());
  21.         for(int i = 0; i < data.length; i++){
  22.             System.out.println(i);
  23.             com.writeByte((byte)'x');
  24.             com.readByte();
  25.             com.writeByte(data[i]);
  26.         }
  27.         com.writeByte((byte)'E');
  28.         com.writeByte((byte)'E');
  29.     }
  30.    
  31.     public byte[] read(int howMuch) throws Exception {
  32.         if(howMuch> eepromSize) throw new Exception("Array is bigger then EEPROM");
  33.         byte[] toReturn = new byte[howMuch];
  34.         com.writeByte((byte)'r');
  35.         System.out.println((char)com.readByte());
  36.         for(int i = 0; i < toReturn.length; i++){
  37.             System.out.println(i);
  38.             com.writeByte((byte)'x');
  39.             com.readByte();
  40.             toReturn[i] = com.readByte();
  41.         }
  42.         com.writeByte((byte)'E');
  43.         com.writeByte((byte)'E');
  44.         return toReturn;
  45.     }
  46.    
  47.     public int verify(byte[] b) throws Exception {
  48.         if(b.length > eepromSize) throw new Exception("Array is bigger then EEPROM");
  49.         byte[] read = read(b.length);
  50.         for(int i = 0; i < read.length; i++){
  51.             if(b[i] != read[i]) return i;
  52.         }
  53.         return -1;
  54.     }
  55.    
  56.     public static void main(String[] args){
  57.         try {
  58.             /*File toLoad = new File("weird_ore.png");
  59.             byte[] b = new byte[(int)toLoad.length()];
  60.             FileInputStream fis = new FileInputStream(toLoad);
  61.             fis.read(b);
  62.             fis.close();
  63.             AVRCom com = new AVRCom("COM6");
  64.             com.open();
  65.             EEPROMWriter writer = new EEPROMWriter(com, 4096);
  66.             writer.write(b);
  67.             b = writer.read(b.length);
  68.             FileOutputStream fos = new FileOutputStream(new File("output.png"));
  69.             fos.write(b);
  70.             fos.close();
  71.             System.out.println("--->" + writer.verify(b));
  72.             com.close();*/
  73.            
  74.             File toLoad = new File("microcode1.rom");
  75.             FileInputStream fis = new FileInputStream(toLoad);
  76.             AVRCom com = new AVRCom("COM4");
  77.             com.open(9600);
  78.             while(com.readByte() != 0x69) {Thread.sleep(512);}
  79.             boolean b = false;
  80.             for(int i = 0; i < 2048; i++) {
  81.                 if(i == 1 && b == false) {
  82.                     i = 0;
  83.                     b = true;
  84.                 }
  85.                 System.out.println("Writing: " + Integer.toString(i));
  86.                 com.writeByte(i == 0 && b == false ? 0 : (fis.available() <= 0 ? 0 : (byte)fis.read()));
  87.                 while(com.readByte() != 1) {}
  88.             }
  89.             fis.close();
  90.             byte[] received = new byte[2048];
  91.             b = false;
  92.             for(int i = 0; i < 2048; i++) {
  93.                 if(i == 1 && b == false) {
  94.                     i = 0;
  95.                     b = true;
  96.                 }
  97.                 System.out.println("Reading: " + Integer.toString(i));
  98.                 com.writeByte((byte)0x55);
  99.                 received[i] = com.readByte();
  100.             }
  101.             com.readByte();
  102.             fis = new FileInputStream(toLoad);
  103.             byte receivedByte = 0;
  104.             for(int i = 0; i < 2048; i++) {
  105.                 receivedByte = fis.available() <= 0 ? 0 : (byte)fis.read();
  106.                 if(received[i] != receivedByte) {
  107.                     System.err.println("Error on " + Integer.toString(i) + ": is " + Integer.toString(received[i] & 0xFF) + ", expected " + Integer.toString(receivedByte & 0xFF));
  108.                 }
  109.             }
  110.             fis.close();
  111.             FileOutputStream fos = new FileOutputStream("file_verified.rom");
  112.             fos.write(received);
  113.             fos.flush();
  114.             fos.close();
  115.             com.close();
  116.         }catch(Exception e){
  117.             System.err.println("Error: ");
  118.             e.printStackTrace();
  119.             System.exit(1);
  120.         }
  121.     }
  122.    
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement