Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package theGhastModding.AVRCom.main;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- public class EEPROMWriter {
- private AVRCom com;
- private int eepromSize;
- public EEPROMWriter(AVRCom com, int eepromSize){
- this.com = com;
- this.eepromSize = eepromSize;
- }
- public void write(byte[] data) throws Exception {
- if(data.length > eepromSize) throw new Exception("Array too big to fit in EEPROM");
- com.writeByte((byte)'w');
- System.out.println((char)com.readByte());
- for(int i = 0; i < data.length; i++){
- System.out.println(i);
- com.writeByte((byte)'x');
- com.readByte();
- com.writeByte(data[i]);
- }
- com.writeByte((byte)'E');
- com.writeByte((byte)'E');
- }
- public byte[] read(int howMuch) throws Exception {
- if(howMuch> eepromSize) throw new Exception("Array is bigger then EEPROM");
- byte[] toReturn = new byte[howMuch];
- com.writeByte((byte)'r');
- System.out.println((char)com.readByte());
- for(int i = 0; i < toReturn.length; i++){
- System.out.println(i);
- com.writeByte((byte)'x');
- com.readByte();
- toReturn[i] = com.readByte();
- }
- com.writeByte((byte)'E');
- com.writeByte((byte)'E');
- return toReturn;
- }
- public int verify(byte[] b) throws Exception {
- if(b.length > eepromSize) throw new Exception("Array is bigger then EEPROM");
- byte[] read = read(b.length);
- for(int i = 0; i < read.length; i++){
- if(b[i] != read[i]) return i;
- }
- return -1;
- }
- public static void main(String[] args){
- try {
- /*File toLoad = new File("weird_ore.png");
- byte[] b = new byte[(int)toLoad.length()];
- FileInputStream fis = new FileInputStream(toLoad);
- fis.read(b);
- fis.close();
- AVRCom com = new AVRCom("COM6");
- com.open();
- EEPROMWriter writer = new EEPROMWriter(com, 4096);
- writer.write(b);
- b = writer.read(b.length);
- FileOutputStream fos = new FileOutputStream(new File("output.png"));
- fos.write(b);
- fos.close();
- System.out.println("--->" + writer.verify(b));
- com.close();*/
- File toLoad = new File("microcode1.rom");
- FileInputStream fis = new FileInputStream(toLoad);
- AVRCom com = new AVRCom("COM4");
- com.open(9600);
- while(com.readByte() != 0x69) {Thread.sleep(512);}
- boolean b = false;
- for(int i = 0; i < 2048; i++) {
- if(i == 1 && b == false) {
- i = 0;
- b = true;
- }
- System.out.println("Writing: " + Integer.toString(i));
- com.writeByte(i == 0 && b == false ? 0 : (fis.available() <= 0 ? 0 : (byte)fis.read()));
- while(com.readByte() != 1) {}
- }
- fis.close();
- byte[] received = new byte[2048];
- b = false;
- for(int i = 0; i < 2048; i++) {
- if(i == 1 && b == false) {
- i = 0;
- b = true;
- }
- System.out.println("Reading: " + Integer.toString(i));
- com.writeByte((byte)0x55);
- received[i] = com.readByte();
- }
- com.readByte();
- fis = new FileInputStream(toLoad);
- byte receivedByte = 0;
- for(int i = 0; i < 2048; i++) {
- receivedByte = fis.available() <= 0 ? 0 : (byte)fis.read();
- if(received[i] != receivedByte) {
- System.err.println("Error on " + Integer.toString(i) + ": is " + Integer.toString(received[i] & 0xFF) + ", expected " + Integer.toString(receivedByte & 0xFF));
- }
- }
- fis.close();
- FileOutputStream fos = new FileOutputStream("file_verified.rom");
- fos.write(received);
- fos.flush();
- fos.close();
- com.close();
- }catch(Exception e){
- System.err.println("Error: ");
- e.printStackTrace();
- System.exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement