This repository was archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
275 lines (237 loc) · 8.08 KB
/
main.c
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
/*
* S51D - Simple MCS51 Debugger
*
* Copyright (C) 2011, Michał Dobaczewski / mdobak@(Google mail)
*
* 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.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "Global.h"
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <time.h>
#include "Utils.h"
#include "MCS51.h"
#include "Debugger.h"
void version(void)
{
print(C_PROLOG "%s %i.%i.%i (build at %s %s)\nCopyright (C) 2011 %s\n" C_RESET,
APP_NAME_LONG, APP_MAJOR_VERSION, APP_MINOR_VERSION,
APP_PATH_VERSION, __DATE__, __TIME__, APP_AUTHOR);
}
void help(void)
{
version();
puts("Usage: s51d [-hv] [-n] [-e file] [-p prompt] [-f format]\n");
puts("Options:");
puts(" -h --help Print out this help.");
puts(" -v --version Print out version number.");
puts(" --no-prolog Do not display prolog.");
puts(" --no-colors Do not use colors.");
puts(" -e <file> --exec <file> Execute command from 'file'.");
puts(" -p <prompt> --prompt <prompt> Specify string for prompt.");
puts(" -f <format> --format <format> Specify string for disassemble listing.");
puts(" -E <file> --stderr <file> Specify file to write errors.");
puts(" -o <file> --stdout <file> Specify file to write output.");
puts(" --dont-remove-escapes Don't remove escape codes when use redirect output.\n");
puts(" --use-stdout Send errors to stdout instead of stderr.\n");
puts(" -m --mcu-type Type of MCU. 8031 8032 8051 8052 89S51 89S52\n");
puts(" --irom-size Send errors to stdout instead of stderr.\n");
puts(" --xrom-size Send errors to stdout instead of stderr.\n");
puts(" --iram-size Send errors to stdout instead of stderr.\n");
puts(" --xram-size Send errors to stdout instead of stderr.\n");
puts("To display available command type help in program console.\n");
}
void cleanUp(void)
{
removeMCU(AppSettings()->mcu);
free(AppSettings()->mcu);
#ifndef NDEBUG
MEMORY_STATUS();
#endif
}
int main(int argc, char* argv[])
{
/*
* Initialize vt100 emulator for Windows.
*/
#ifdef _WIN32
vtInitVT100();
#endif // _WIN32
/*
* Set default values.
*/
srand(time(NULL));
bool showProlog = true;
AppSettings()->mcu = malloc(sizeof(MCU));
AppSettings()->mcu->noDebug = false;
AppSettings()->format =
C_BOLD C_FWHITE "%a: " C_NBOLD "%o " C_FGREEN "%m " C_BOLD" %p " C_RESET "\n";
AppSettings()->commandPrompt = "cmd:>";
AppSettings()->exitkey = 0x04; /* Ctrl + D */
AppSettings()->lastKey = 0;
AppSettings()->realtime = true;
AppSettings()->keyAvailable = false;
AppSettings()->stopThread = false;
AppSettings()->noColors = false;
AppSettings()->dontRemoveEscapeCodes = false;
AppSettings()->pauseOnError = true;
AppSettings()->simTimeBeforeStop = 0;
AppSettings()->simSyncTimeBeforeStop = 0;
AppSettings()->mcuSec = 0;
AppSettings()->simSec = 0;
AppSettings()->in = stdin;
AppSettings()->out = stdout;
AppSettings()->defaultIn = stdin;
AppSettings()->defaultOut = stdout;
AppSettings()->errorOut = stderr;
init8052MCU(AppSettings()->mcu);
atexit(cleanUp);
#ifndef USE_COLORS
AppSettings()->noColors = true;
#endif
/*
* Read options.
*/
int c;
while (1) {
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{"no-prolog", no_argument, 0, 1000},
#ifdef USE_COLORS
{"no-colors", no_argument, 0, 1001},
#endif
{"exec", required_argument, 0, 'e'},
{"prompt", required_argument, 0, 'p'},
{"format", required_argument, 0, 'f'},
{"stderr", required_argument, 0, 'E'},
{"stdout", required_argument, 0, 'o'},
{"dont-remove-escape", required_argument, 0, 1002},
{"use-stdout", required_argument, 0, 1003},
{"mcu-type", required_argument, 0, 'm'},
{"irom-size", required_argument, 0, 1004},
{"xrom-size", required_argument, 0, 1005},
{"iram-size", required_argument, 0, 1006},
{"xram-size", required_argument, 0, 1007},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long(argc, argv, "hve:p:f:E:o:m:", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'h':
help();
exit(EXIT_SUCCESS);
break;
case 'v':
version();
exit(EXIT_SUCCESS);
break;
case 1000:
showProlog = false;
break;
case 'e':
AppSettings()->in = fopen(optarg, "r");
if (AppSettings()->in == NULL) {
fprintf(stderr, "Error while trying to open '%s': %s.\n", optarg,
strerror(errno));
AppSettings()->in = stdin;
}
break;
case 'p':
AppSettings()->commandPrompt = optarg;
break;
#ifdef USE_COLORS
case 1001:
AppSettings()->noColors = true;
break;
#endif
case 'f':
AppSettings()->format = optarg;
break;
case 'E':
AppSettings()->errorOut = fopen(optarg, "r");
if (AppSettings()->errorOut == NULL) {
fprintf(stderr, "Error while trying to open '%s': %s.\n", optarg,
strerror(errno));
AppSettings()->errorOut = stderr;
}
break;
case 'o':
AppSettings()->defaultOut = fopen(optarg, "r");
if (AppSettings()->defaultOut == NULL) {
fprintf(stderr, "Error while trying to open '%s': %s.\n", optarg,
strerror(errno));
AppSettings()->defaultOut = stdout;
}
break;
case 1002:
AppSettings()->dontRemoveEscapeCodes = true;
break;
case 'm':
if (0 == strcmp(optarg, "8031"))
init8031MCU(AppSettings()->mcu);
if (0 == strcmp(optarg, "8032"))
init8032MCU(AppSettings()->mcu);
else if (0 == strcmp(optarg, "8051"))
init8051MCU(AppSettings()->mcu);
else if (0 == strcmp(optarg, "8052"))
init8052MCU(AppSettings()->mcu);
else if (0 == strcmp(optarg, "89S51"))
init89S51MCU(AppSettings()->mcu);
else if (0 == strcmp(optarg, "89S52"))
init89S52MCU(AppSettings()->mcu);
else
fprintf(stderr, "Invalid MCU type '%s'.\n", optarg);
break;
case 1003:
AppSettings()->errorOut = stdout;
break;
case 1004:
AppSettings()->mcu->iromMemorySize = hextoi(optarg, 0x0, 0x10000, 0x10000, NULL);
break;
case 1005:
AppSettings()->mcu->xromMemorySize = hextoi(optarg, 0x0, 0x10000, 0x10000, NULL);
break;
case 1006:
AppSettings()->mcu->idataMemorySize = hextoi(optarg, 0x10, 0x100, 0x100, NULL);
break;
case 1007:
AppSettings()->mcu->xdataMemorySize = hextoi(optarg, 0x0, 0x10000, 0x10000, NULL);
break;
case '?':
break;
default:
abort();
}
}
/*
* Prolog.
*/
if (showProlog)
version();
/*
* Debugger.
*/
runDebugger();
/* Go to cleanUp() before exit !!! */
return EXIT_SUCCESS;
/* La fin de mon monde... */
}
/*
vi:ts=4:et:nowrap
*/