public static HEXFile Load(string FileName) { HEXFile file = new HEXFile(); string[] lines; try { lines = File.ReadAllLines(FileName); } catch { return(null); } foreach (string line in lines) { string sLen = line.Substring(1, 2); int len = int.Parse(sLen, System.Globalization.NumberStyles.AllowHexSpecifier); string sAddr = line.Substring(3, 4); int addr = int.Parse(sAddr, System.Globalization.NumberStyles.AllowHexSpecifier); string op = line.Substring(7, 2); if (op != "00") //Very basic support, only data records supported (I8HEX) { continue; } string data = line.Substring(9, len * 2); int currentAddress = addr; for (int buc = 0; buc < len * 2; buc += 2) { file.Data[currentAddress] = byte.Parse(data.Substring(buc, 2), System.Globalization.NumberStyles.AllowHexSpecifier); currentAddress++; } if (addr < file.StartAddress) { file.StartAddress = addr; } if (addr + len > file.EndAddress) { file.EndAddress = addr + len; } } return(file); }
/// <summary> /// Loads the selected hex file. /// </summary> private void LoadHEXFile() { // // unload HEX file // this.ClearProgram(); // // initialize program memory array // this.AllocateProgramArray(); // // read and process the hex file // try { // // read the HEX file // HEXFile hex = new HEXFile(this.txtHEXFile.Text); hex.ReadFile(this.programMemory, this.configMemory, this.selectedDevice.ConfigAddress); // // load it into listview and enable programming // this.LoadProgramWords(); // // enable program buttons // this.tbProgram.Enabled = true; this.tbVerify.Enabled = true; // // display message // this.txtOutput.AppendText(Strings.HEXFileLoaded); this.txtOutput.AppendText(Environment.NewLine); } catch (IndexOutOfRangeException) { // // display error (device does not have enough memory for this // HEX file // MessageBox.Show(Strings.InsufficientDeviceMemory, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); // // disable program buttons // this.tbProgram.Enabled = false; } catch (Exception ex) { // // something went wrong, display error // this.txtOutput.AppendText(ex.Message); this.txtOutput.AppendText(Environment.NewLine); MessageBox.Show(ex.Message, Strings.Error); // // disable program button // this.tbProgram.Enabled = false; } }
private void ProgramExecutive() { try { AppendText appendText = new AppendText(this.txtOutput.AppendText); string file = string.Empty; string executivePath = ConfigurationManager.AppSettings["ExecutivePath"]; this.Invoke(appendText, string.Format("Looking for executive in: {0}", executivePath)); this.Invoke(appendText, Environment.NewLine); // // // foreach (string f in Directory.GetFiles(executivePath)) { // // strip path from filename // file = Path.GetFileName(f); if (file.StartsWith(this.programmer.ExecutiveFile) && file.EndsWith(".hex")) { this.Invoke(appendText, string.Format("Found executive file: {0}", file)); this.Invoke(appendText, Environment.NewLine); file = f; break; } } // // // byte[] executiveMemory = new byte[2048 * 3]; for (int i = 0; i < executiveMemory.Length; i++) { executiveMemory[i] = 0xFF; } // // load the executive HEX file // HEXFile executive = new HEXFile(file); executive.ReadFile(new byte[] {}, executiveMemory, 0x800000); // // erase executive memory // this.Invoke(appendText, "Erasing program and executive memory..."); this.programmer.EraseProgramAndExecutiveMemory(); this.Invoke(appendText, Strings.Success); this.Invoke(appendText, Environment.NewLine); // // program the executive // this.Invoke(appendText, "Programming executive..."); this.programmer.WriteProgramMemory(0x800000, executiveMemory, 0x000000); this.Invoke(appendText, Strings.Success); this.Invoke(appendText, Environment.NewLine); } catch (DirectoryNotFoundException) { throw; } }
public ZXSerialLoaderResult LoadFile(string SerialPort, string FileName, int BlockSize, Action <int> Progress) { string ext = Path.GetExtension(FileName).ToLower(); SpectrumFile program; switch (ext) { case ".hex": program = HEXFile.Load(FileName); break; case ".sna": program = SNAFile.Load(FileName); break; case ".z80": program = Z80File.Load(FileName); break; case ".tap": program = TAPFile.Load(FileName); break; default: return(ZXSerialLoaderResult.Unsupported); } if (program == null) { return(ZXSerialLoaderResult.FileError); } try { string dvResponse; using (SerialPort serial = new SerialPort(SerialPort, serialSpeed, Parity.None, 8, StopBits.One)) { serial.Open(); serial.ReadTimeout = 5000; serial.WriteTimeout = 5000; serial.Write(program.Operation); if ((dvResponse = serial.ReadLine()) != "RDY") { return(ZXSerialLoaderResult.UnknownResponse); } if (program.Header != null) { serial.Write(program.Header, 0, program.Header.Length); } int pos = program.StartAddress; bool finished = false; double passPercent = BlockSize * 100 / (program.EndAddress - program.StartAddress); double currentPercent = 0; while (pos < program.EndAddress) { int segLen = Math.Min(BlockSize, program.EndAddress - pos); byte[] tmpBuffer = new byte[2]; if (pos + segLen >= program.EndAddress) //Is last segment? { finished = true; tmpBuffer[0] = 1; } serial.Write(tmpBuffer, 0, 1); //Send last segment tmpBuffer[0] = (byte)(pos & 0xFF); tmpBuffer[1] = (byte)((pos >> 8) & 0xFF); serial.Write(tmpBuffer, 0, 2); //Send segment address tmpBuffer[0] = (byte)(segLen & 0xFF); tmpBuffer[1] = (byte)((segLen >> 8) & 0xFF); serial.Write(tmpBuffer, 0, 2); //Send segment size if ((dvResponse = serial.ReadLine()) != "OK") { return(ZXSerialLoaderResult.UnknownResponse); } //Send segment serial.Write(program.Data, pos, segLen); //Wait for acknowledge if (!finished) { if ((dvResponse = serial.ReadLine()) != "NEXT") { return(ZXSerialLoaderResult.UnknownResponse); } } currentPercent += passPercent; if (Progress != null) { Progress((int)Math.Min(100, currentPercent)); } pos += segLen; } if (Progress != null) { Progress(1000); } Console.WriteLine(serial.ReadLine()); if (Progress != null) { Progress(2000); } Console.WriteLine(serial.ReadLine()); if (Progress != null) { Progress(3000); } serial.Close(); return(ZXSerialLoaderResult.Success); } } catch { return(ZXSerialLoaderResult.SerialPortError); } }