📄 form1.cs
字号:
MessageBox.Show(exc.Message, "Check the File Attributes");
}
}
/* Summary
Used by Create_Script
*/
public void LoadHexScript(string fname, bool blow)
{
list.Clear();
list1.Clear();
string line, sOffset, tmp;
int v;
if (fname.Equals("VendAX.hex"))
{
//Fills the vendax firmware into the Arraylist list
AddVend_to_list();
}
else
{
//Fills the list from hex file
FileStream fs = new FileStream(fname, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
while (!sr.EndOfStream)
{
list.Add(sr.ReadLine());
}
sr.Close();
fs.Close();
}
int Ramsize = Maxaddr;
string AddrBoundary = textBox2.Text.Substring(2, 4);
// Delete non-data records
for (int i = list.Count - 1; i >= 0; i--)
{
line = (string)list[i];
if (line.Length > 0)
{
tmp = line.Substring(7, 2); // Get the Record Type into v
v = (int)Util.HexToInt(tmp);
if (v != 0) list.Remove(list[i]); // Data records are type == 0
}
}
for (int i = 0; i < list.Count; i++)
{
line = (string)list[i];
// Remove comments
v = line.IndexOf("//");
if (v > -1)
line = line.Substring(0, v - 1);
// Build string that just contains the offset followed by the data bytes
if (line.Length > 0)
{
// Get the offset
sOffset = line.Substring(3, 4);
// Get the string of data chars
tmp = line.Substring(1, 2);
v = (int)Util.HexToInt(tmp) * 2;
string s = line.Substring(9, v);
list1.Add(sOffset + s);
}
}
if (blow) Hold(true);
Reqcode = blow ? (byte)0xA0 : (byte)0xA3;
windex = 0;
int iindex = 0;
Datastring = "";
int nxtoffset = 0;
int xferLen = 0;
wvalue = 0;
foreach (string lines in list1)
{
line = lines.Substring(0, 4);
ushort offset = (ushort)Util.HexToInt(line);
int slen = lines.Length;
int no_bytes = (slen - 4) / 2;
int lastaddr = offset + no_bytes;
if ((blow && (lastaddr < Ramsize)) || (!blow && (lastaddr >= Ramsize)))
{
xferLen += no_bytes;
if ((offset == nxtoffset) && (xferLen < Max_Ctlxfer_size))
{
Datastring += lines.Substring(4, no_bytes * 2);
}
else
{
int len = Datastring.Length;
if (!len.Equals(0))
{
int bufLen = len / 2;
byte[] buf = new byte[bufLen];
string d;
for (int j = 0; j < bufLen; j++)
{
d = Datastring.Substring(j * 2, 2);
buf[j] = (byte)Util.HexToInt(d);
}
Write_script(ref buf, ref bufLen);
}
wvalue = (ushort)Util.HexToInt(line);
Datastring = lines.Substring(4, no_bytes * 2);
xferLen = no_bytes;
}
nxtoffset = offset + no_bytes;
}
iindex++;
}
int len1 = Datastring.Length;
if (!len1.Equals(0))
{
int bufLen = len1 / 2;
byte[] buf1 = new byte[bufLen];
string d;
for (int j = 0; j < bufLen; j++)
{
d = Datastring.Substring(j * 2, 2);
buf1[j] = (byte)Util.HexToInt(d);
}
Write_script(ref buf1, ref bufLen);
}
if (blow) Hold(false);
}
/* Summary
For resetting the CPU or reverse
*/
public void Hold(bool bHold)
{
byte[] buf = new byte[1];
buf[0] = (bHold) ? (byte)1 : (byte)0;
int bufLen = 1;
Reqcode = 0xA0;
wvalue = Resetreg;
windex = 0;
Write_script(ref buf, ref bufLen);
}
/* Summary
Final step of Create_Script --> writes to a opened stream
*/
public void Write_script(ref byte[] buffer, ref int buflen)
{
int len = buflen;
Xaction.bReqType = CyConst.DIR_TO_DEVICE | CyConst.REQ_VENDOR | CyConst.TGT_DEVICE;
Xaction.CtlReqCode = Reqcode;
Xaction.wValue = wvalue;
Xaction.wIndex = windex;
Xaction.DataLen = (uint)len;
Xaction.Timeout = 15;
Xaction.RecordSize = (uint)len + TTransaction.TotalHeaderSize;
Xaction.WriteToStream(stream);
Xaction.WriteFromBuffer(stream, ref buffer, ref buflen);
}
/* Summary
Get's the monitor's filename to load
*/
private void Select_Monitor_Click(object sender, EventArgs e)
{
fx2 = Fx2DeviceSelected();
if (fx2 == null)
{
return;
}
string tmpFilter = FOpenDialog.Filter;
string tmpTitle = FOpenDialog.Title;
FOpenDialog.Title = "Select a Monitor for Debugging: ";
FOpenDialog.Filter = "Intel HEX files (*.hex) | *.hex";
if (FOpenDialog.ShowDialog() == DialogResult.OK)
{
monitorfile = FOpenDialog.FileName;
StatLabel.Text = "Monitor Selected.... " + FOpenDialog.FileName;
Refresh();
}
else
{
FOpenDialog.Title = tmpTitle;
FOpenDialog.Filter = tmpFilter;
return;
}
FOpenDialog.FileName = "";
FOpenDialog.Title = tmpTitle;
FOpenDialog.Filter = tmpFilter;
Load_Monitor.Enabled = true;
}
/* Summary
Executes the selected monitor
*/
private void Load_Monitor_Click(object sender, EventArgs e)
{
fx2 = Fx2DeviceSelected();
if (fx2 == null)
{
return;
}
if (monitorfile.Length == 0)
{
MessageBox.Show("Select a monitor before loading.", "Select Monitor");
return;
}
fx2.LoadExternalRam(monitorfile);
monitorfile = "";
Load_Monitor.Enabled = false;
}
/* Summary
Just get's the file name of .spt for playing
*/
private void load_button_Click(object sender, EventArgs e)
{
fx2 = Fx2DeviceSelected();
if (fx2 == null)
{
return;
}
string tmpFilter = FOpenDialog.Filter;
string tmpTitle = FOpenDialog.Title;
FOpenDialog.Title = "Select a Script file to load: ";
FOpenDialog.Filter = "Script files (*.spt) | *.spt";
if (FOpenDialog.ShowDialog() == DialogResult.OK)
{
playscriptfile = FOpenDialog.FileName;
StatLabel.Text = "Script loaded.... " + FOpenDialog.FileName;
Refresh();
}
else
{
FOpenDialog.Title = tmpTitle;
FOpenDialog.Filter = tmpFilter;
return;
}
FOpenDialog.FileName = "";
FOpenDialog.Title = tmpTitle;
FOpenDialog.Filter = tmpFilter;
play_button.Enabled = true;
}
/* Summary
Executes the script loaded
*/
private void play_button_Click(object sender, EventArgs e)
{
fx2 = Fx2DeviceSelected();
if (fx2 == null)
{
return;
}
if (playscriptfile.Length == 0)
{
MessageBox.Show("Load a script before playing it.", "Load script");
return;
}
StatLabel.Text = "Playing Script " + FOpenDialog.FileName + " in Outputbox";
Refresh();
FileStream stream = new FileStream(playscriptfile, FileMode.Open, FileAccess.Read);
if (stream.Length > 0)
{
try
{
Xaction.ReadFromStream(stream);
if (fx2.Config != Xaction.ConfigNum)
fx2.Config = Xaction.ConfigNum;
if (fx2.AltIntfc != Xaction.AltIntfc)
fx2.AltIntfc = Xaction.AltIntfc;
stream.Close();
stream = new FileStream(playscriptfile, FileMode.Open, FileAccess.Read);
long totalFileSize = stream.Length;
long file_bytes_read = 0;
do
{
Xaction.ReadFromStream(stream);
file_bytes_read += 32;
if (Xaction.Tag == 0xFF)
{
Thread.Sleep(100);
}
else
{
byte[] buffer = new byte[Xaction.DataLen];
int len = (int)Xaction.DataLen;
curEndpt = fx2.EndPointOf(Xaction.EndPtAddr);
if (curEndpt != null)
{
if (curEndpt.Attributes == 0)
{
/* Control transfer */
CyControlEndPoint ctlEpt = curEndpt as CyControlEndPoint;
byte tmp = Xaction.bReqType;
ctlEpt.Target = (byte)(tmp & TTransaction.ReqType_TGT_MASK);
ctlEpt.ReqType = (byte)(tmp & TTransaction.ReqType_TYPE_MASK);
ctlEpt.Direction = (byte)(tmp & TTransaction.ReqType_DIR_MASK);
ctlEpt.ReqCode = Xaction.CtlReqCode;
ctlEpt.Value = Xaction.wValue;
ctlEpt.Index = Xaction.wIndex;
if (Xaction.Tag == 0)
{
Xaction.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -