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

Subversion Repositories a-z80

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

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.Linq;
5
 
6
namespace z80_pla_checker
7
{
8
    /// 
9
    /// This class defines a complete PLA table and operations on it
10
    /// 
11
    class ClassPla
12
    {
13
        /// 
14
        /// List of all PLA entries that we read from the input file
15
        /// 
16
        private readonly List pla = new List();
17
 
18
        /// 
19
        /// List of PLA entries which we want to ignore for various reasons
20
        /// 
21
        public List IgnoredPla = new List();
22
 
23
        /// 
24 8 gdevic
        /// List of PLA entries not used by our Timings matrix
25
        /// 
26
        public List NotUsedPla = new List();
27
 
28
        /// 
29 3 gdevic
        /// Returns the total number of PLA table entries
30
        /// 
31
        public int Count()
32
        {
33
            return pla.Count();
34
        }
35
 
36
        /// 
37
        /// Read the master PLA table from a text file
38
        /// 
39
        public bool Load(string filename)
40
        {
41
            // Read each line of the file into a string array. Each element
42
            // of the array is one line of the file.
43
            ClassLog.Log("Loading PLA: " + filename);
44
 
45
            try
46
            {
47
                string[] lines = File.ReadAllLines(filename);
48
                pla.Clear();
49
                foreach (string line in lines)
50
                {
51
                    if (line[0] == '#')
52
                        continue;
53
                    var p = new ClassPlaEntry();
54
                    if (p.Init(line))
55
                        pla.Add(p);
56
                }
57
            }
58
            catch (Exception ex)
59
            {
60
                ClassLog.Log(ex.Message);
61
                return false;
62
            }
63
            ClassLog.Log(string.Format("Total {0} PLA lines", pla.Count()));
64
 
65
            ////============================================================
66
            //// Ignore duplicate PLA entries
67 8 gdevic
            //IgnoredPla.Add(98);     // Duplicate of 37
68
            //IgnoredPla.Add(94);     // Duplicate of 12 and 18
69
            //IgnoredPla.Add(93);     // Duplicate of 11 and 19
70 3 gdevic
            //IgnoredPla.Add(90);     // Duplicate of 26
71 8 gdevic
            //IgnoredPla.Add(87);     // Duplicate of 83
72 3 gdevic
            //IgnoredPla.Add(71);     // Duplicate of 25
73
            //IgnoredPla.Add(63);     // Duplicate of 17
74
            //IgnoredPla.Add(60);     // Duplicate of 15
75
            //IgnoredPla.Add(41);     // Duplicate of 3
76 8 gdevic
            //IgnoredPla.Add(36);     // Duplicate of 8
77 3 gdevic
            //IgnoredPla.Add(32);     // Duplicate of 4
78 8 gdevic
            //IgnoredPla.Add(19);     // Duplicate of 11 and 93
79
            //IgnoredPla.Add(18);     // Duplicate of 12 and 94
80 3 gdevic
 
81
            ////============================================================
82
            //// Special signals (not instructions)
83
            //IgnoredPla.Add(91);     // This signal goes along block IN/OUT instructions.
84
            //IgnoredPla.Add(75);     // This signal specifies a decrement operation for PLA 53, 66 and 105. Otherwise, it is an increment.
85
            //IgnoredPla.Add(55);     // This signal specifies (HL) addressing for all CB-table instructions, PLA entries 70, 72, 73, 74.
86
            //IgnoredPla.Add(44);     // This signal specifies a regular CB opcode (ignoring IX/IY).
87
            //IgnoredPla.Add(33);     // This signal specifies whether the register is being loaded or stored to memory for PLA entry 31.
88
            //IgnoredPla.Add(28);     // This signal specifies the OUT operation for PLA 37. Otherwise, it is operation.
89
            //IgnoredPla.Add(27);     // This signal goes along individual IN/OUT instructions in the ED table.
90
            //IgnoredPla.Add(16);     // This signal specifies a PUSH operation for PLA23. Otherwise, it is a POP operation.
91
            //IgnoredPla.Add(13);     // This signal specifies whether the value is being loaded or stored for PLA entries 8, 30 and 38.
92
            //IgnoredPla.Add(0);      // This signal specifies *not* to repeat block instructions.
93
 
94
            ////============================================================
95
            //// Ignore our own reserved entries
96 8 gdevic
            //IgnoredPla.Add(107);
97 3 gdevic
            //IgnoredPla.Add(106);
98
 
99
            //============================================================
100
            // Remove op-bits so we the output is more readable
101
            IgnoredPla.Add(99);
102
            IgnoredPla.Add(100);
103
            IgnoredPla.Add(101);
104
            IgnoredPla.Add(102);
105
            IgnoredPla.Add(103);
106
            IgnoredPla.Add(104);
107
 
108
            // Remove ALU operation entries so the output is more readable
109
            IgnoredPla.Add(88);
110
            IgnoredPla.Add(86);
111
            IgnoredPla.Add(85);
112
            IgnoredPla.Add(84);
113
            IgnoredPla.Add(80);
114
            IgnoredPla.Add(79);
115
            IgnoredPla.Add(78);
116
            IgnoredPla.Add(76);
117
 
118
            //============================================================
119 8 gdevic
            // Signals not used in the Timings spreadsheet. For those, PLA table entries are not generated.
120
            // This list is used only when generating the PLA table.
121
            NotUsedPla.Add(67);       // This signal defines a specific in(), but we use in/out pla[27] + pla[34]
122
            NotUsedPla.Add(62);       // This signal is issued for all CB opcodes
123
            NotUsedPla.Add(54);       // This signal specifies every CB with IX/IY
124
            NotUsedPla.Add(22);       // This signal specifies CB prefix w/o IX/IY
125
            NotUsedPla.Add(14);       // This signal specifies a decrement operation for PLA 9. Otherwise, it is an increment.
126
            NotUsedPla.Add(4);        // This signal goes along instructions that access I and R register (PLA 57 and 83).
127
 
128
            //============================================================
129 3 gdevic
            // Mark all PLA entries we decided to ignore
130
            foreach (var p in pla)
131
            {
132
                if (IgnoredPla.Contains(p.N))
133
                    p.Ignored = true;
134
            }
135
            return true;
136
        }
137
 
138
        /// 
139
        /// Dumps the content of the entire PLA table
140
        /// 
141
        public void Dump()
142
        {
143
            ClassLog.Log("Content of the PLA table:");
144
            foreach (var p in pla.Where(p => !p.IsDuplicate()))
145
                ClassLog.Log(p.Raw);
146
        }
147
 
148
        /// 
149
        /// Find and return all PLA table entries that trigger on a given condition.
150
        /// 
151
        public List TableMatch(ClassPlaEntry.Modifier modifier, byte instr)
152
        {
153
            var t = new bool[pla.Count];
154
 
155
            // First do a simple search to find the list of *all* PLA entries that match
156
            foreach (var p in pla)
157
            {
158
                if (p.Ignored) continue;
159
                String match = p.Match(modifier, instr);
160
                t[p.N] = !string.IsNullOrEmpty(match);
161
            }
162
 
163
            ////============================================================
164
            //// Apply any intra-PLA conditions. These are hard-coded into the
165
            //// timing spreadsheet and we are duplicating them here:
166
 
167
            //// INC/DEC variations with register, (hl) or (ix+d)
168
            //if (t[66] && !(t[53] || t[105])) ; else t[66] = false;
169
 
170
            //// Generic LD r,r' + (hl), IX variations and on top of that NHALT
171
            //if (t[61] && !(t[59] || t[103] || t[58] || t[102] || t[95])) ; else t[61] = false;
172
            //if (t[58] && !t[95]) ; else t[58] = false;
173
            //if (t[102] && !t[95]) ; else t[102] = false;
174
            //if (t[59] && !t[95]) ; else t[59] = false;
175
            //if (t[103] && !t[95]) ; else t[103] = false;
176
 
177
            //// A single LD (hl),n and LD (ix+d),n has precedence over a set of LD r,n
178
            //if (t[17] && !(t[40] || t[50])) ; else t[17] = false;
179
 
180
            //// ALU A,r' and variations on (hl) and (ix+d)
181
            //if (t[65] && !(t[52] || t[104])) ; else t[65] = false;
182
 
183
            //// ALU
184
            //if (t[88] && (t[65] || t[64] || t[52] || t[104])) ; else t[88] = false;
185
            //if (t[86] && (t[65] || t[64] || t[52] || t[104])) ; else t[86] = false;
186
            //if (t[85] && (t[65] || t[64] || t[52] || t[104])) ; else t[85] = false;
187
            //if (t[84] && (t[65] || t[64] || t[52] || t[104])) ; else t[84] = false;
188
            //if (t[80] && (t[65] || t[64] || t[52] || t[104])) ; else t[80] = false;
189
            //if (t[79] && (t[65] || t[64] || t[52] || t[104])) ; else t[79] = false;
190
            //if (t[78] && (t[65] || t[64] || t[52] || t[104])) ; else t[78] = false;
191
            //if (t[76] && (t[65] || t[64] || t[52] || t[104])) ; else t[76] = false;
192
 
193
            //============================================================
194
 
195
            // Finally, collect and return all PLA entries that are left asserted
196
            return (from p in pla
197
                    where t[p.N]
198
                    select p.Match(modifier, instr)).ToList();
199
        }
200
 
201
        /// 
202
        /// Given the PLA ID, return a list of all opcodes that trigger it
203
        /// 
204
        public List MatchPLA(ClassPlaEntry.Modifier modifier, int id)
205
        {
206
            var m = new List();
207
 
208
            // Find the pla with a given index
209
            foreach (ClassPlaEntry p in pla)
210
            {
211
                if (p.N == id)
212
                {
213
                    // For each possible opcode...
214
                    for (int i = 0; i < 256; i++)
215
                    {
216
                        String match = p.Match(modifier, Convert.ToByte(i));
217
                        if (!string.IsNullOrEmpty(match))
218
                            m.Add(string.Format("{0:X02} => {1}", i, match));
219
                    }
220
                    return m;
221
                }
222
            }
223
            ClassLog.Log("Non-existent PLA index");
224
            return m;
225
        }
226
 
227
        /// 
228
        /// Dump opcode table in various ways.
229
        /// Returns a "selected" list of opcode numbers, that is, opcodes which were tagged by
230
        /// the optional input PLA table number given in arg parameter.
231
        /// 
232
        public List Table(ClassPlaEntry.Modifier modifier, int testNum, int arg)
233
        {
234
            ClassLog.Log(new string('-', 242));
235
            List tagged = new List();
236
            for (int y = 0; y < 16; y++)
237
            {
238
                string line = string.Format("{0:X} ", y);
239
                for (int x = 0; x < 16; x++)
240
                {
241
                    char prefix = ' ';
242
                    byte opcode = Convert.ToByte(y * 16 + x);
243
                    List match = TableMatch(modifier, opcode);
244
                    foreach (string oneMatch in match.Where(oneMatch => Convert.ToInt32(oneMatch.Substring(1, oneMatch.LastIndexOf(']') - 1)) == arg))
245
                    {
246
                        tagged.Add(y * 16 + x);
247
                        prefix = '*';
248
                    }
249
                    string entry = "";
250
 
251
                    //===============================================================================
252
                    // Table 0 - Show the number of PLA entries that match each opcode
253
                    //===============================================================================
254
                    if (testNum == 0)
255
                    {
256
                        entry = string.Join(",", match);
257
                        if (match.Count == 0)
258
                            entry = ".";
259
                        if (match.Count > 1)
260
                            entry = "[" + match.Count + "]";
261
                    }
262
 
263
                    //===============================================================================
264
                    // Table 1 - For each opcode, show all PLA entries that trigger
265
                    //===============================================================================
266
                    if (testNum == 1)
267
                    {
268
                        foreach (string oneMatch in match)
269
                        {
270
                            string n = oneMatch.Substring(1, oneMatch.LastIndexOf(']') - 1);
271
                            entry += n + ",";
272
                        }
273
                        entry = entry.TrimEnd(',');
274
                    }
275
 
276
                    // -------------------------------------------
277
                    if (entry.Length > 12)
278
                        entry = entry.Substring(0, 12);
279
                    line += string.Format(" |{0}{1,-12}", prefix, entry);
280
                }
281
                ClassLog.Log(line);
282
            }
283
            return tagged;
284
        }
285
 
286
        /// 
287
        /// Query PLA table string given as a vector of 0's and 1's
288
        /// This vector is coped from a ModelSim simulation run. The function will decode PLA string
289
        /// into a set of PLA entries that are being triggered ("1")
290
        /// 
291
        public void QueryPla(String bits)
292
        {
293
            int max = pla.Count();
294
            if (bits.Count() != max)
295
            {
296
                ClassLog.Log("Invalid PLA length - the bit array should be " + max + " and it is " + bits.Count());
297
                return;
298
            }
299
            for (int i = 0; i < max; i++)
300
                if (bits[max - i - 1] == '1')
301
                    ClassLog.Log(string.Format(@"pla[{0,3}] = 1;   // {1}", pla[i].N, pla[i].Comment));
302
        }
303
 
304
        /// 
305
        /// Generates a Verilog module with the PLA logic
306
        /// 
307
        public void GenVerilogPla()
308
        {
309
            string max = (pla.Count() - 1).ToString();
310
            string module = "";
311
            module += @"//=====================================================================================" + Environment.NewLine;
312
            module += @"// This file is automatically generated by the z80_pla_checker tool. Do not edit!      " + Environment.NewLine;
313
            module += @"//=====================================================================================" + Environment.NewLine;
314 8 gdevic
            module += @"module pla_decode" + Environment.NewLine;
315
            module += @"(" + Environment.NewLine;
316
            module += @"    input wire [6:0] prefix," + Environment.NewLine;
317
            module += @"    input wire [7:0] opcode," + Environment.NewLine;
318
            module += @"    output wire [" + max + ":0] pla" + Environment.NewLine;
319
            module += @");" + Environment.NewLine;
320 3 gdevic
            module += @"" + Environment.NewLine;
321
 
322
            foreach (var p in pla)
323
            {
324 8 gdevic
                if (p.IsDuplicate() || NotUsedPla.Contains(p.N))
325 3 gdevic
                    continue;
326 8 gdevic
 
327 3 gdevic
                String bitstream = p.GetBitstream();
328 8 gdevic
                module += string.Format(@"assign pla[{0,3}] = (({{prefix[6:0], opcode[7:0]}} & 15'b{1}) == 15'b{2}) ? 1'b1 : 1'b0;   // {3}",
329
                    p.N,
330
                    bitstream.Replace('0', '1').Replace('X', '0'),  // Create "AND" mask
331
                    bitstream.Replace('X', '0'),                    // Create a value to compare to
332
                    p.Comment) + Environment.NewLine;
333 3 gdevic
            }
334
 
335 8 gdevic
            // List all PLA entries that are not used
336 3 gdevic
            module += @"" + Environment.NewLine;
337 8 gdevic
            module += @"// Entries not used by our timing matrix" + Environment.NewLine;
338
            foreach (var n in NotUsedPla)
339
            {
340
                module += string.Format(@"assign pla[{0,3}] = 1'b0;   // {1}", n, pla[n].Comment) + Environment.NewLine;
341
            }
342
 
343
            // List all PLA entries that are ignored
344
            module += @"" + Environment.NewLine;
345
            module += @"// Duplicate entries" + Environment.NewLine;
346 3 gdevic
            foreach (var p in pla)
347
            {
348
                if (p.IsDuplicate())
349 8 gdevic
                    module += string.Format(@"assign pla[{0,3}] = 1'b0;   // {1}", p.N, p.Comment) + Environment.NewLine;
350 3 gdevic
            }
351
 
352
            module += @"" + Environment.NewLine;
353
            module += @"endmodule" + Environment.NewLine;
354
 
355
            ClassLog.Log(module);
356
        }
357
    }
358
}

powered by: WebSVN 2.1.0

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