-
Notifications
You must be signed in to change notification settings - Fork 3
/
MainForm.cs
152 lines (131 loc) · 4.27 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.IO;
using Be.Windows.Forms;
namespace GD77_FlashManager
{
public partial class MainForm : Form
{
public static byte [] eeprom = new byte[1024 * 1024];
public static int startAddress;
public static int transferLength;
private FixedByteProvider _dbp;
private bool _hexboxHasChanged = false;
public MainForm()
{
InitializeComponent();
hexBox.ByteProvider = _dbp = new FixedByteProvider(eeprom);
_dbp.Changed += new EventHandler(onDataProviderChanged);// No point doing something every time this changes, as it only alerts to the fact that something has changed and not what specific byte has changed
}
public void onDataProviderChanged(object sender, EventArgs e)
{
_hexboxHasChanged = true;
}
private void btnRead_Click(object sender, EventArgs e)
{
CommPrgForm commPrgForm = new CommPrgForm();
commPrgForm.StartPosition = FormStartPosition.CenterParent;
commPrgForm.IsRead = true;
MainForm.startAddress = int.Parse(txtStartAddr.Text, System.Globalization.NumberStyles.HexNumber);
if (MainForm.startAddress % 32 != 0)
{
MessageBox.Show("Start address must be a multiple of 0x20");
return;
}
MainForm.transferLength = int.Parse(txtLen.Text, System.Globalization.NumberStyles.HexNumber);
if (MainForm.transferLength == 0)
{
MessageBox.Show("Length cant be zero");
return;
}
if (MainForm.transferLength % 32 != 0)
{
MessageBox.Show("Length must be a multplie of 0x20");
return;
}
if (MainForm.startAddress + MainForm.transferLength > MainForm.eeprom.Length)
{
MessageBox.Show("Start address and length settings would result in reading beyond the end of memory");
return;
}
commPrgForm.ShowDialog();
hexBox.ByteProvider = _dbp = new FixedByteProvider(eeprom);
_hexboxHasChanged = false;
}
private void btnWrite_Click(object sender, EventArgs e)
{
CommPrgForm commPrgForm = new CommPrgForm();
commPrgForm.StartPosition = FormStartPosition.CenterParent;
commPrgForm.IsRead = false;
MainForm.startAddress = int.Parse(txtStartAddr.Text, System.Globalization.NumberStyles.HexNumber);
if (MainForm.startAddress % 32 != 0)
{
MessageBox.Show("Start address must be a multiple of 0x20");
return;
}
MainForm.transferLength = int.Parse(txtLen.Text, System.Globalization.NumberStyles.HexNumber);
if (MainForm.transferLength % 32 != 0)
{
MessageBox.Show("Length must be a multplie of 0x20");
return;
}
if (MainForm.startAddress + MainForm.transferLength > MainForm.eeprom.Length)
{
MessageBox.Show("Start address and length settings would result in writing beyond the end of memory");
return;
}
copyHexboxToEeprom();
commPrgForm.ShowDialog();
}
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "binary files (*.bin)|*.bin|All files (*.*)|*.*";
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
MainForm.eeprom = File.ReadAllBytes(openFileDialog1.FileName);
hexBox.ByteProvider = _dbp = new FixedByteProvider(eeprom);
_hexboxHasChanged = false;
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "binary files (*.bin)|*.bin|All files (*.*)|*.*";
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
copyHexboxToEeprom();
File.WriteAllBytes(saveFileDialog1.FileName, MainForm.eeprom);
}
}
private void copyHexboxToEeprom()
{
// if (_hexboxHasChanged)
{
Console.WriteLine("HexBox changed");
Array.Copy(_dbp.Bytes.ToArray<byte>(), MainForm.eeprom, MainForm.eeprom.Length);
_hexboxHasChanged = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}