-
Notifications
You must be signed in to change notification settings - Fork 10
/
conddebug.cpp
528 lines (446 loc) · 9.51 KB
/
conddebug.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/* FCEUXD SP - NES/Famicom Emulator
*
* Copyright notice for this file:
* Copyright (C) 2005 Sebastian Porst
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* The parser was heavily inspired by the following two websites:
* http:https://www.cs.utsa.edu/~wagner/CS5363/rdparse.html
* http:https://www.engr.mun.ca/~theo/Misc/exp_parsing.htm
*
* Grammar of the parser:
*
* P -> Connect
* Connect -> Compare {('||' | '&&') Compare}
* Compare -> Sum {('==' | '!=' | '<=' | '>=' | '<' | '>') Sum}
* Sum -> Product {('+' | '-') Product}
* Product -> Primitive {('*' | '/') Primitive}
* Primitive -> Number | Address | Register | Flag | PC Bank | '(' Connect ')'
* Number -> '#' [1-9A-F]*
* Address -> '$' [1-9A-F]* | '$' '[' Connect ']'
* Register -> 'A' | 'X' | 'Y' | 'P' | 'S'
* Flag -> 'N' | 'C' | 'Z' | 'I' | 'B' | 'V' | 'U' | 'D'
* PC Bank -> 'K'
* Data Bank -> 'T'
* Value -> 'R' | 'W'
*/
#include "types.h"
#include "conddebug.h"
#include "utils/memory.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cctype>
uint16 debugLastAddress = 0; // used by 'T' and 'R' conditions
uint8 debugLastOpcode = 0; // used to evaluate 'W' condition
// Next non-whitespace character in string
static char next = 0;
static int ishex(char c)
{
return isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}
static void scan(const char** str)
{
do
{
next = **str;
(*str)++;
} while (isspace(next));
}
// Generic function to handle all infix operators but the last one in the precedence hierarchy. : '(' E ')'
static Condition* InfixOperator(const char** str, Condition(*nextPart(const char**)), int(*operators)(const char**))
{
Condition* t = nextPart(str);
Condition* t1;
Condition* mid;
int op;
if (t == nullptr)
{
return nullptr;
}
while ((op = operators(str)))
{
scan(str);
t1 = nextPart(str);
if (t1 == nullptr)
{
delete t;
return 0;
}
mid = new Condition();
if (mid == nullptr)
{
delete t;
delete t1;
return nullptr;
}
mid->lhs = t;
mid->rhs = t1;
mid->op = op;
t = mid;
}
return t;
}
// Generic handler for two-character operators
static int TwoCharOperator(const char** str, char c1, char c2, int op)
{
if (next == c1 && **str == c2)
{
scan(str);
return op;
}
else
{
return 0;
}
}
// Determines if a character is a flag
static int isFlag(char c)
{
return c == 'N' || c == 'I' || c == 'C' || c == 'V' || c == 'Z' || c == 'B' || c == 'U' || c == 'D';
}
// Determines if a character is a register
static int isRegister(char c)
{
return c == 'A' || c == 'X' || c == 'Y' || c == 'P' || c == 'S';
}
// Determines if a character is for PC bank
static int isPCBank(char c)
{
return c == 'K';
}
// Determines if a character is for Data bank
static int isDataBank(char c)
{
return c == 'T';
}
// Determines if a character is for value read
static int isValueRead(char c)
{
return c == 'R';
}
// Determines if a character is for value write
static int isValueWrite(char c)
{
return c == 'W';
}
// Reads a hexadecimal number from str
static int getNumber(unsigned int* number, const char** str)
{
// char buffer[5];
if (sscanf(*str, "%X", number) == EOF || *number > 0xFFFF)
{
return 0;
}
// Older, inferior version which doesn't work with leading zeros
// sprintf(buffer, "%X", *number);
// *str += strlen(buffer);
while (ishex(**str)) (*str)++;
scan(str);
return 1;
}
static Condition* Connect(const char** str);
// Handles the following part of the grammar: '(' E ')'
static Condition* Parentheses(const char** str, Condition* c, char openPar, char closePar)
{
if (next == openPar)
{
scan(str);
if (!c) return 0;
c->lhs = Connect(str);
if (next == closePar)
{
scan(str);
return c;
}
else
{
return 0;
}
}
return 0;
}
/*
* Check for primitives
* Flags, Registers, Numbers, Addresses and parentheses
*/
static Condition* Primitive(const char** str, Condition* c)
{
if (isFlag(next)) /* Flags */
{
if (c->type1 == TYPE_NO)
{
c->type1 = TYPE_FLAG;
c->value1 = next;
}
else
{
c->type2 = TYPE_FLAG;
c->value2 = next;
}
scan(str);
return c;
}
else if (isRegister(next)) /* Registers */
{
if (c->type1 == TYPE_NO)
{
c->type1 = TYPE_REG;
c->value1 = next;
}
else
{
c->type2 = TYPE_REG;
c->value2 = next;
}
scan(str);
return c;
}
else if (isPCBank(next)) /* PC Bank */
{
if (c->type1 == TYPE_NO)
{
c->type1 = TYPE_PC_BANK;
c->value1 = next;
}
else
{
c->type2 = TYPE_PC_BANK;
c->value2 = next;
}
scan(str);
return c;
}
else if (isDataBank(next)) /* Data Bank */
{
if (c->type1 == TYPE_NO)
{
c->type1 = TYPE_DATA_BANK;
c->value1 = next;
}
else
{
c->type2 = TYPE_DATA_BANK;
c->value2 = next;
}
scan(str);
return c;
}
else if (isValueRead(next))
{
if (c->type1 == TYPE_NO)
{
c->type1 = TYPE_VALUE_READ;
c->value1 = next;
}
else
{
c->type2 = TYPE_VALUE_READ;
c->value2 = next;
}
scan(str);
return c;
}
else if (isValueWrite(next))
{
if (c->type1 == TYPE_NO)
{
c->type1 = TYPE_VALUE_WRITE;
c->value1 = next;
}
else
{
c->type2 = TYPE_VALUE_WRITE;
c->value2 = next;
}
scan(str);
return c;
}
else if (next == '#') /* Numbers */
{
unsigned int number = 0;
if (!getNumber(&number, str))
{
return 0;
}
if (c->type1 == TYPE_NO)
{
c->type1 = TYPE_NUM;
c->value1 = number;
}
else
{
c->type2 = TYPE_NUM;
c->value2 = number;
}
return c;
}
else if (next == '$') /* Addresses */
{
if ((**str >= '0' && **str <= '9') || (**str >= 'A' && **str <= 'F')) /* Constant addresses */
{
unsigned int number = 0;
if (!getNumber(&number, str))
{
return 0;
}
if (c->type1 == TYPE_NO)
{
c->type1 = TYPE_ADDR;
c->value1 = number;
}
else
{
c->type2 = TYPE_ADDR;
c->value2 = number;
}
return c;
}
else if (**str == '[') /* Dynamic addresses */
{
scan(str);
Parentheses(str, c, '[', ']');
if (c->type1 == TYPE_NO)
{
c->type1 = TYPE_ADDR;
}
else
{
c->type2 = TYPE_ADDR;
}
return c;
}
else
{
return 0;
}
}
else if (next == '(')
{
return Parentheses(str, c, '(', ')');
}
return 0;
}
/* Handle * and / operators */
static Condition* Term(const char** str)
{
Condition* t;
Condition* t1;
Condition* mid;
t = new Condition();
if (t == nullptr)
{
return NULL;
}
if (!Primitive(str, t))
{
delete t;
return 0;
}
while (next == '*' || next == '/')
{
int op = next == '*' ? OP_MULT : OP_DIV;
scan(str);
if ((t1 = new Condition()) == nullptr)
{
delete t;
return nullptr;
}
if (!Primitive(str, t1))
{
delete t;
delete t1;
return 0;
}
if ((mid = new Condition()) == nullptr)
{
delete t;
delete t1;
return nullptr;
}
mid->lhs = t;
mid->rhs = t1;
mid->op = op;
t = mid;
}
return t;
}
/* Check for + and - operators */
static int SumOperators(const char** str)
{
switch (next)
{
case '+': return OP_PLUS;
case '-': return OP_MINUS;
default: return OP_NO;
}
}
/* Handle + and - operators */
static Condition* Sum(const char** str)
{
return InfixOperator(str, Term, SumOperators);
}
/* Check for <=, =>, ==, !=, > and < operators */
static int CompareOperators(const char** str)
{
int val = TwoCharOperator(str, '=', '=', OP_EQ);
if (val) return val;
val = TwoCharOperator(str, '!', '=', OP_NE);
if (val) return val;
val = TwoCharOperator(str, '>', '=', OP_GE);
if (val) return val;
val = TwoCharOperator(str, '<', '=', OP_LE);
if (val) return val;
val = next == '>' ? OP_G : 0;
if (val) return val;
val = next == '<' ? OP_L : 0;
if (val) return val;
return OP_NO;
}
/* Handle <=, =>, ==, !=, > and < operators */
static Condition* Compare(const char** str)
{
return InfixOperator(str, Sum, CompareOperators);
}
/* Check for || or && operators */
static int ConnectOperators(const char** str)
{
int val = TwoCharOperator(str, '|', '|', OP_OR);
if(val) return val;
val = TwoCharOperator(str, '&', '&', OP_AND);
if(val) return val;
return OP_NO;
}
/* Handle || and && operators */
static Condition* Connect(const char** str)
{
return InfixOperator(str, Compare, ConnectOperators);
}
/* Root of the parser generator */
Condition* generateCondition(const char* str)
{
Condition* c;
scan(&str);
c = Connect(&str);
if (!c || next != 0)
{
if (c) delete c;
return 0;
}
else return c;
}