OpenCores
URL https://opencores.org/ocsvn/a-z80/a-z80/trunk

Subversion Repositories a-z80

[/] [a-z80/] [trunk/] [tools/] [z80_pla_checker/] [source/] [FormMain.cs] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 gdevic
using System;
2
using System.Collections.Generic;
3
using System.IO;
4
using System.Windows.Forms;
5
 
6
namespace z80_pla_checker
7
{
8
    public partial class FormMain : Form
9
    {
10
        /// 
11
        /// Master PLA table
12
        /// 
13
        private readonly ClassPla pla = new ClassPla();
14
 
15
        /// 
16
        /// Current modifiers
17
        /// 
18
        private ClassPlaEntry.Modifier modifier = ClassPlaEntry.Modifier.XX | ClassPlaEntry.Modifier.NHALT;
19
 
20
        /// 
21
        /// Various opcode tables we need to display mnemonics
22
        /// 
23
        private readonly ClassOpcodeTable tableXX__ = new ClassOpcodeTable();
24
        private readonly ClassOpcodeTable tableCBXX = new ClassOpcodeTable();
25
        private readonly ClassOpcodeTable tableEDXX = new ClassOpcodeTable();
26
        private readonly ClassOpcodeTable tableDDXX = new ClassOpcodeTable();
27
        private readonly ClassOpcodeTable tableDDCB = new ClassOpcodeTable();
28
 
29
        private readonly List commands = new List();
30
        private int commandsBrowseIndex;
31
 
32
        public FormMain()
33
        {
34
            InitializeComponent();
35
            WindowState = FormWindowState.Maximized;
36
        }
37
 
38
        /// 
39
        /// This is the main program's startup function
40
        /// 
41
        public void OnStart()
42
        {
43
            ClassLog.Log("PLA Checker Tool  Copyright (C) 2014  Goran Devic");
44
            ClassLog.Log("This program comes with ABSOLUTELY NO WARRANTY");
45
            ClassLog.Log("This is free software and you are welcome to redistribute it under certain conditions; for details see GPLv3 license.");
46
            ClassLog.Log("---------------------------------------------------------------------------------------------------------------------");
47
 
48
            // Load the PLA table from a text file.
49
            String plaFile = Properties.Settings.Default.plaFileName;
50
            if (!pla.Load(plaFile))
51
            {
52
                ClassLog.Log("*** Error loading the master input PLA source table ***");
53
                ClassLog.Log("Click on File -> Load PLA table... and select a resource file z80-pla.txt");
54
                return;
55
            }
56
 
57
            // Load opcode tables from a previously selected directory
58
            // We use only IX variation tables since IY's are the same
59
            String opFile = Properties.Settings.Default.opcodeDir;
60
            char p = Path.DirectorySeparatorChar;
61
            tableXX__.Load(opFile + p + "opcodes-xx.txt", 0);
62
            tableCBXX.Load(opFile + p + "opcodes-cb-xx.txt", 2);
63
            tableEDXX.Load(opFile + p + "opcodes-ed-xx.txt", 2);
64
            tableDDXX.Load(opFile + p + "opcodes-dd-xx.txt", 2);
65
            tableDDCB.Load(opFile + p + "opcodes-dd-cb.txt", 7);
66
 
67
            ClassLog.Log(Command("?"));
68
        }
69
 
70
        /// 
71
        /// Print out a log message & always show the last line
72
        /// 
73
        public void Log(String s)
74
        {
75
            logText.AppendText(s + Environment.NewLine);
76
            logText.SelectionStart = logText.Text.Length;
77
            logText.ScrollToCaret();
78
        }
79
 
80
        /// 
81
        /// Exit the application
82
        /// 
83
        private void ExitToolStripMenuItemClick(object sender, EventArgs e)
84
        {
85
            Close();
86
        }
87
 
88
        /// 
89
        /// Application is closing
90
        /// 
91
        private void FormMainFormClosing(object sender, FormClosingEventArgs e)
92
        {
93
            Properties.Settings.Default.Save();
94
        }
95
 
96
        private static int ScanNumber(string arg, int baseValue)
97
        {
98
            try
99
            {
100
                return Convert.ToInt32(arg, baseValue);
101
            }
102
            catch(Exception ex)
103
            {
104
                ClassLog.Log(ex.Message + ": " + arg);
105
                return -1;
106
            }
107
        }
108
 
109
        /// 
110
        /// List all PLA entries that trigger on a specific opcode
111
        /// If the opcode # (hex) was not given, dump that information for all opcodes (0-FF)
112
        /// 
113
        private void MatchPLA(string arg)
114
        {
115
            int op = -1;
116
            if (!string.IsNullOrEmpty(arg))
117
            {
118
                op = ScanNumber(arg, 16);
119
                if (op < 0)
120
                    return;
121
            }
122
            for (int x = 0; x < 256; x++)
123
            {
124
                if (op >= 0 && x != op)
125
                    continue;
126
                ClassLog.Log(String.Format("Opcode: {0:X02} ", x));
127
 
128
                Byte opcode = Convert.ToByte(x);
129
                List m = pla.TableMatch(modifier, opcode);
130
 
131
                foreach (var s in m)
132
                    ClassLog.Log(s);
133
            }
134
        }
135
 
136
        /// 
137
        /// List all opcodes that trigger on a given PLA table index
138
        /// 
139
        private void MatchOpcodes(ClassPlaEntry.Modifier modifier, string arg)
140
        {
141
            int index = ScanNumber(arg, 10);
142
            if (index < 0)
143
                return;
144
            List m = pla.MatchPLA(modifier, index);
145
            if (m.Count == 0)
146
                return;
147
            ClassLog.Log(String.Format("PLA Entry: {0}  Modifier: {1}", index, modifier));
148
            foreach (var s in m)
149
                ClassLog.Log(s);
150
        }
151
 
152
        /// 
153
        /// Dumps one of the opcode tables depending on the current set of modifiers
154
        /// 
155
        private void DumpOpcodeTable(List t)
156
        {
157
            bool cb = (modifier & ClassPlaEntry.Modifier.CB) != 0;
158
            bool ed = (modifier & ClassPlaEntry.Modifier.ED) != 0;
159
            bool ix = (modifier & ClassPlaEntry.Modifier.IXY1) != 0;
160
            if (ix & cb)
161
                tableDDCB.Dump(t);
162
            else if (cb)
163
                tableCBXX.Dump(t);
164
            else if (ed)
165
                tableEDXX.Dump(t);
166
            else if (ix)
167
                tableDDXX.Dump(t);
168
            else tableXX__.Dump(t);
169
        }
170
 
171
        /// 
172
        /// Select the input PLA table file to load
173
        /// 
174
        private void LoadPlaTable(object sender, EventArgs e)
175
        {
176
            var dlg = new OpenFileDialog();
177
            dlg.Title = "Select a PLA table source file";
178
            dlg.Filter = @"z80-pla.txt|*.txt|All files|*.*";
179
            dlg.FileName = Properties.Settings.Default.plaFileName;
180
            if (dlg.ShowDialog() == DialogResult.OK)
181
                Properties.Settings.Default.plaFileName = dlg.FileName;
182
        }
183
 
184
        /// 
185
        /// Select the directory that contains opcode tables
186
        /// 
187
        private void SelectOpcodeDir(object sender, EventArgs e)
188
        {
189
            var dlg = new FolderBrowserDialog();
190
            dlg.Description = "Select the directory containing opcode files (opcodes-xx.txt etc.):";
191
            if (dlg.ShowDialog() == DialogResult.OK)
192
                Properties.Settings.Default.opcodeDir = dlg.SelectedPath;
193
        }
194
 
195
        /// 
196
        /// Clear the log text panel
197
        /// 
198
        private void BtClearClick(object sender, EventArgs e)
199
        {
200
            logText.Clear();
201
        }
202
 
203
        /// 
204
        /// User clicked on the Redo button: repeat the command
205
        /// 
206
        private void BtRedoClick(object sender, EventArgs e)
207
        {
208
            ClassLog.Log(string.Format("{0}>>> {1}", commands.Count, commands[commands.Count - 1]));
209
            string response = Command(commands[commands.Count - 1]);
210
            if (!string.IsNullOrEmpty(response))
211
                ClassLog.Log(response);
212
        }
213
 
214
        /// 
215
        /// Update button state after the internal flag state change
216
        /// 
217
        private void UpdateButtons()
218
        {
219
            btIX0.Checked = (modifier & ClassPlaEntry.Modifier.IXY0) != 0;
220
            btIX1.Checked = (modifier & ClassPlaEntry.Modifier.IXY1) != 0;
221
            btHALT.Checked = (modifier & ClassPlaEntry.Modifier.NHALT) != 0;
222
            btALU.Checked = (modifier & ClassPlaEntry.Modifier.ALU) != 0;
223
            btXX.Checked = (modifier & ClassPlaEntry.Modifier.XX) != 0;
224
            btCB.Checked = (modifier & ClassPlaEntry.Modifier.CB) != 0;
225
            btED.Checked = (modifier & ClassPlaEntry.Modifier.ED) != 0;
226
 
227
            ClassLog.Log("Set modifier to " + modifier);
228
        }
229
 
230
        private void BtIx0Click(object sender, EventArgs e)
231
        {
232
            if ((modifier & ClassPlaEntry.Modifier.IXY0) != 0)
233
                modifier &= ~ClassPlaEntry.Modifier.IXY0;
234
            else
235
            {
236
                modifier |= ClassPlaEntry.Modifier.IXY0;
237
                modifier &= ~ClassPlaEntry.Modifier.IXY1;
238
            }
239
            UpdateButtons();
240
        }
241
 
242
        private void BtIx1Click(object sender, EventArgs e)
243
        {
244
            if ((modifier & ClassPlaEntry.Modifier.IXY1) != 0)
245
                modifier &= ~ClassPlaEntry.Modifier.IXY1;
246
            else
247
            {
248
                modifier |= ClassPlaEntry.Modifier.IXY1;
249
                modifier &= ~ClassPlaEntry.Modifier.IXY0;
250
            }
251
            UpdateButtons();
252
        }
253
 
254
        private void BtNHaltClick(object sender, EventArgs e)
255
        {
256
            modifier ^= ClassPlaEntry.Modifier.NHALT;
257
            UpdateButtons();
258
        }
259
 
260
        private void BtAluClick(object sender, EventArgs e)
261
        {
262
            modifier ^= ClassPlaEntry.Modifier.ALU;
263
            UpdateButtons();
264
        }
265
 
266
        private void BtXxClick(object sender, EventArgs e)
267
        {
268
            if ((modifier & ClassPlaEntry.Modifier.XX) != 0)
269
                modifier &= ~ClassPlaEntry.Modifier.XX;
270
            else
271
            {
272
                modifier |= ClassPlaEntry.Modifier.XX;
273
                modifier &= ~(ClassPlaEntry.Modifier.CB | ClassPlaEntry.Modifier.ED);
274
            }
275
            UpdateButtons();
276
        }
277
 
278
        private void BtCbClick(object sender, EventArgs e)
279
        {
280
            if ((modifier & ClassPlaEntry.Modifier.CB) != 0)
281
                modifier &= ~ClassPlaEntry.Modifier.CB;
282
            else
283
            {
284
                modifier |= ClassPlaEntry.Modifier.CB;
285
                modifier &= ~(ClassPlaEntry.Modifier.XX | ClassPlaEntry.Modifier.ED);
286
            }
287
            UpdateButtons();
288
        }
289
 
290
        private void BtEdClick(object sender, EventArgs e)
291
        {
292
            if ((modifier & ClassPlaEntry.Modifier.ED) != 0)
293
                modifier &= ~ClassPlaEntry.Modifier.ED;
294
            else
295
            {
296
                modifier |= ClassPlaEntry.Modifier.ED;
297
                modifier &= ~(ClassPlaEntry.Modifier.XX | ClassPlaEntry.Modifier.CB);
298
            }
299
            UpdateButtons();
300
        }
301
 
302
        /// 
303
        /// Implements a simple command history
304
        /// 
305
        private void TextOpKeyDown(object sender, KeyEventArgs e)
306
        {
307
            if (e.KeyCode == Keys.Enter)
308
            {
309
                // Mark the handled flag so this key won't be processed.
310
                e.Handled = true;
311
                string cmd = textOp.Text.Trim();
312
                if (cmd.Length > 0)
313
                {
314
                    commands.Add(cmd);
315
                    btRedo.Enabled = true;
316
                    ClassLog.Log(string.Format("{0}>>> {1}", commands.Count, cmd));
317
                    string response = Command(textOp.Text);
318
                    if (!string.IsNullOrEmpty(response))
319
                        ClassLog.Log(response);
320
 
321
                    commandsBrowseIndex = commands.Count;
322
                    textOp.Text = "";
323
                }
324
                textOp.Focus();
325
            }
326
            if (e.KeyCode == Keys.PageUp && commandsBrowseIndex > 0)
327
            {
328
                commandsBrowseIndex--;
329
                textOp.Text = commands[commandsBrowseIndex];
330
                e.Handled = true;
331
            }
332
            if (e.KeyCode == Keys.PageDown && commandsBrowseIndex < commands.Count - 1)
333
            {
334
                commandsBrowseIndex++;
335
                textOp.Text = commands[commandsBrowseIndex];
336
                e.Handled = true;
337
            }
338
            if (e.KeyCode == Keys.Escape)
339
            {
340
                textOp.Text = "";
341
                e.Handled = true;
342
            }
343
        }
344
 
345
        /// 
346
        /// Execute a command
347
        /// 
348
        private string Command(string cmd)
349
        {
350
            try
351
            {
352
                string[] tokens = cmd.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
353
                if (tokens.Length == 0)
354
                    return "";
355
                switch (tokens[0])
356
                {
357
                    case "?":
358
                    case "h":
359
                        return Environment.NewLine +
360
                            "p         - Dump the content of the PLA table" + Environment.NewLine +
361
                            "p [#]     - For a given PLA entry # (dec) show opcodes that trigger it" + Environment.NewLine +
362
                            "m [#]     - Match opcode # (hex) with a PLA entry (or match 0-FF)" + Environment.NewLine +
363
                            "g         - Generate a Verilog PLA module" + Environment.NewLine +
364
                            "t [#] <#> - Show opcode table in various ways" + Environment.NewLine +
365
                            "            0 - Display number of PLA entries that trigger on each opcode" + Environment.NewLine +
366
                            "            1 - For each opcode, display all PLA entry numbers that trigger" + Environment.NewLine +
367
                            "            <#> - Add a * to opcodes for which the specified PLA entry triggers" + Environment.NewLine +
368
                            "q 101000... Query PLA table string" + Environment.NewLine +
369
                            "c         - Clear the screen";
370
                    case "p": if (tokens.Length > 1)
371
                            MatchOpcodes(modifier, tokens[1]);
372
                        else
373
                            pla.Dump();
374
                        break;
375
                    case "m": MatchPLA(tokens.Length > 1 ? tokens[1] : "");
376
                        break;
377
                    case "g": pla.GenVerilogPla();
378
                        break;
379
                    case "c": BtClearClick(null, null);
380
                        break;
381
                    case "t":
382
                        {
383
                            int arg1 = 0, arg2 = -1;
384
                            if (tokens.Length > 1)
385
                                arg1 = ScanNumber(tokens[1], 10);
386
                            if (tokens.Length > 2)
387
                                arg2 = ScanNumber(tokens[2], 10);
388
                            if (arg1 == 0 || arg1 == 1)
389
                            {
390
                                List tagged = pla.Table(modifier, arg1, arg2);
391
                                DumpOpcodeTable(tagged);
392
                            }
393
                            else
394
                                ClassLog.Log("Invalid table number!");
395
                        }
396
                        break;
397
                    case "q": pla.QueryPla(tokens[1].Trim());
398
                        break;
399
                    default:
400
                        return "?";
401
                }
402
            }
403
            catch (Exception ex)
404
            {
405
                ClassLog.Log("Error: " + ex.Message);
406
            }
407
            return string.Empty;
408
        }
409
    }
410
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.