private void loadProgramsToStorage() { //write.prog VMInfo vminfo = new VMInfo() { code = Util.getCode("SETB 12\nOPENFILEHANDLE\nSAVEB 8\nSETCX 4\nSETA 4\nREAD\nMOVA 4\nSETB 1\nADD\nSAVEA 0\nSETA 0\nMOVB 8\nSETA 0\nSEEK 0\nSETCX 4\nWRITE\nCLOSEHANDLE\nJMP 0\nHALT\n"), data = Util.getData("000000010000000200000003\"file\"\"name\"\"lon\0\"") }; int sz = Util.getProgramSizeInFile(vminfo); StorageFile program = StorageFile.createFile(codeStorage, "write.prog", sz); Util.saveCodeToFile(program, vminfo); //read.prog vminfo = new VMInfo() { code = Util.getCode("SETB 4\nOPENFILEHANDLE\nSETCX 4\nSETA 0\nREAD\nCLOSEHANDLE\nMOVA 0\nPRINTA\nHALT\n"), data = Util.getData("FFFFFFFF\"file\"\"name\"\"lon\0\"") }; sz = Util.getProgramSizeInFile(vminfo); program = StorageFile.createFile(codeStorage, "read.prog", sz); Util.saveCodeToFile(program, vminfo); //inf.prog vminfo = new VMInfo() { code = Util.getCode("MOVA 0\nMOVATOCX\nLOOP 0\nHALT\n"), data = Util.getData("00000004") }; sz = Util.getProgramSizeInFile(vminfo); program = StorageFile.createFile(codeStorage, "inf.prog", sz); Util.saveCodeToFile(program, vminfo); }
private void loadProgramsToStorage() { VMInfo vminfo; int sz; StorageFile program; //battery.prog vminfo = new VMInfo() { code = Util.getCode("MOUNT 0\nMOVA 0\nMOVATOCX\nMOVA 8\nWRITE\nMOVA 4\nCLOSEHANDLE\nHALT\n"), data = Util.getData("000000040000000100000004") }; sz = Util.getProgramSizeInFile(vminfo); program = StorageFile.createFile(codeStorage, "battery.prog", sz); Util.saveCodeToFile(program, vminfo); //battery2.prog vminfo = new VMInfo() { code = Util.getCode("MOUNT 0\nMOVA 0\nMOVATOCX\nMOVA 4\nREAD\nCLOSEHANDLE\nHALT\n"), data = Util.getData("0000000400000000") }; sz = Util.getProgramSizeInFile(vminfo); program = StorageFile.createFile(codeStorage, "battery2.prog", sz); Util.saveCodeToFile(program, vminfo); //inf.prog vminfo = new VMInfo() { code = Util.getCode("MOVA 0\nMOVATOCX\nLOOP 0\nHALT\n"), data = Util.getData("00000004") }; sz = Util.getProgramSizeInFile(vminfo); program = StorageFile.createFile(codeStorage, "inf.prog", sz); Util.saveCodeToFile(program, vminfo); }
public void addProgramFromFile(string fileName) { //var codeStorage = new Storage(fileName); //byte[] altcode = Util.getCode("MOVB 4\nOPENFILEHANDLE\nSAVEB 12\nMOVA 20\nMOVATOCX\nMOVA 0\nWRITE\nHALT\n"); //byte[] altcode = Util.getCode("MOVB 4\nOPENFILEHANDLE\nSAVEB 12\nMOVA 20\nMOVATOCX\nMOVA 0\nREAD\nCLOSEHANDLE\nHALT\n"); //byte[] altcode = Util.getCode("MOVA 20\nMOVATOCX\nMOVA 4\nPRINTC\nHALT\n"); //byte[] altcode = Util.getCode("MOVA 20\nMOVATOCX\nMOVA 4\nREADC\nHALT\n"); //byte[] altcode = Util.getCode("MOUNT 0\nMOVA 0\nMOVATOCX\nMOVA 8\nWRITE\nMOVA 4\nREAD\nHALT\n"); //string t = "0000001000000008\"big\0\"00000000FFFFFFFF00000004"; //string t2 = "0000001000000008\"big\0\"00000000BBBBBBBB00000004"; //string t = "0000000C00000008\"big\0\"00000000BBBBBBBB00000004"; //string t = "000000040000000100000004"; //byte[] altdata = Util.getData(t); StorageFile codeFile = StorageFile.Open(this.codeStorage, fileName); VMInfo programData = Util.readCodeFromFile(codeFile); //Util.saveCodeToHdd(codeStorage, 10, new VMInfo { code = altcode, data = altdata }); uint rowCount = (uint)((programData.code.Length + programData.data.Length) / Constants.BLOCK_SIZE) + 1; //uint rowCount = 10; MemAccesser memAccesser = virtualMemory.reserveMemory(rowCount); memAccesser.writeFromAddr(0, programData.code); memAccesser.writeFromAddr((uint)programData.code.Length, programData.data); Program program = new Program(fileName, memAccesser); program.registers.CS = 0; program.registers.DS = 0 + (uint)programData.code.Length; programs.Add(program); }
public static int getProgramSizeInFile(VMInfo info) { byte[] codeLengthBytes = BitConverter.GetBytes(info.code.Length); byte[] dataLengthBytes = BitConverter.GetBytes(info.data.Length); return(info.code.Length + info.data.Length + codeLengthBytes.Length + dataLengthBytes.Length); // 8 bytes for code and data size + eof }
public static Boolean saveCodeToFile(StorageFile file, VMInfo info) { byte[] codeLengthBytes = BitConverter.GetBytes(info.code.Length); byte[] dataLengthBytes = BitConverter.GetBytes(info.data.Length); int totalLength = info.code.Length + info.data.Length + codeLengthBytes.Length + dataLengthBytes.Length; // 8 bytes for code and data size + eof if (file.size() < totalLength) { throw new Exception("File is too small to store code and data"); } byte[] dataToWrite = new byte[totalLength]; int used = 0; codeLengthBytes.CopyTo(dataToWrite, used); used += codeLengthBytes.Length; dataLengthBytes.CopyTo(dataToWrite, used); used += dataLengthBytes.Length; info.code.CopyTo(dataToWrite, used); used += info.code.Length; info.data.CopyTo(dataToWrite, used); used += info.data.Length; file.setBytes(dataToWrite); return(true); }
public static int saveCodeToHdd(Storage storage, int location, VMInfo info) { byte[] codeLengthBytes = BitConverter.GetBytes(info.code.Length); byte[] dataLengthBytes = BitConverter.GetBytes(info.data.Length); int totalLength = info.code.Length + info.data.Length + codeLengthBytes.Length + dataLengthBytes.Length; // 8 bytes for code and data size + eof byte[] dataToWrite = new byte[totalLength]; int used = 0; codeLengthBytes.CopyTo(dataToWrite, used); used += codeLengthBytes.Length; dataLengthBytes.CopyTo(dataToWrite, used); used += dataLengthBytes.Length; info.code.CopyTo(dataToWrite, used); used += info.code.Length; info.data.CopyTo(dataToWrite, used); used += info.data.Length; for (int i = 0; i < totalLength; i++) { storage[location + i] = dataToWrite[i]; } return(totalLength); }
public override void run() { switch (this.IC) { case 0: { StorageFile file = StorageFile.Open(this.kernelStorage.codeStorage, programName); programData = Util.readCodeFromFile(file); this.rowCount = Math.Ceiling((programData.data.Length + programData.code.Length) / (double)Constants.BLOCK_SIZE); this.IC++; break; } case 1: { for (int i = 0; i < Convert.ToInt32(rowCount); i++) { this.resourceRequestor.request(ResType.Memory); } this.IC++; break; } case 2: { uint rowCount = this.getResourceTypeCount(ResType.Memory); this.memAccesser = this.kernelStorage.virtualMemory.reserveMemory(rowCount); this.IC++; break; } case 3: { //byte[] altcode = Util.getCode("MOVA 0\nMOVATOCX\nMOVB 1\nADD\nLOOP 3\nHALT\n"); //byte[] altdata = Util.getData("000000050000000166696C652E747874"); //byte[] altcode = Util.getCode("MOVB 4\nOPENFILEHANDLE\nSAVEB 12\nMOVA 20\nMOVATOCX\nMOVA 0\nWRITE\nHALT\n"); //byte[] altcode = Util.getCode("MOVB 4\nOPENFILEHANDLE\nSAVEB 12\nMOVA 20\nMOVATOCX\nMOVA 0\nREAD\nCLOSEHANDLE\nHALT\n"); //byte[] altcode = Util.getCode("MOUNT 0\nMOVA 0\nMOVATOCX\nMOVA 8\nWRITE\nMOVA 4\nREAD\nCLOSEHANDLE\nHALT\n"); //byte[] altcode = Util.getCode("MOVA 20\nMOVATOCX\nMOVA 4\nPRINTC\nHALT\n"); //byte[] altcode = Util.getCode("MOVA 20\nMOVATOCX\nMOVA 4\nREADC\nHALT\n"); //string t = "0000001000000008\"big\0\"00000000FFFFFFFF00000004"; //string t = "0000001000000008\"big\0\"00000000BBBBBBBB00000004"; //string t = "000000040000000100000004"; //byte[] altdata = Util.getData(t); memAccesser.writeFromAddr(0, programData.code); memAccesser.writeFromAddr((uint)programData.code.Length, programData.data); this.IC++; break; } case 4: { Program program = new Program("a", memAccesser); byte[] PTRIInfo = new byte[] { (byte)rowCount, (byte)Constants.MAX_BLOCK_COUNT, (byte)0, (byte)0 }; program.registers.PTRI = BitConverter.ToUInt32(PTRIInfo, 0); program.registers.CS = 0; program.registers.DS = 0 + (uint)programData.code.Length; this.virtualMachine = new VirtualMachine(program, memAccesser, kernelStorage, this.id); kernelStorage.processes.add(virtualMachine); this.IC++; break; } case 5: { this.resourceRequestor.request(ResType.Any, this.id); this.IC++; break; } case 6: { Resource resource = this.getFirstResource(ResType.Any, this.id); Program program = this.virtualMachine.program; if (resource.type == ResType.Interrupt && (program.registers.SI == SiInt.Halt || program.registers.PI == PiInt.InvalidCommand)) { this.IC = 8; break; } else { this.IC++; } break; } case 7: { Resource resource = this.getFirstResource(ResType.Any, this.id); if (resource.type == ResType.Interrupt) { this.intHandler.handleInt((Interrupt)resource); } else if (resource is BaseHandleResource) // we are only looking at responses. Should be impossible for request to get here. { this.intHandler.handleResponse(resource); this.kernelStorage.resources.add(new Resource(ResType.FromInterrupt, this.id, false, virtualMachine.id)); } else { throw new NotImplementedException(); } resource.release(); this.IC = 5; break; } case 8: { kernelStorage.processes.remove(this.virtualMachine); this.IC++; break; } case 9: { kernelStorage.resources.add(new ProgramStartKill(this.id, true, this.programName)); this.IC++; break; } case 10: { this.resourceRequestor.request(ResType.NonExistent); break; } default: throw new NotImplementedException(); } }