-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.c
145 lines (117 loc) · 3.88 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
/*
* Copyright (c) 2005 Travis Geiselbrecht
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/sys.h>
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <config.h>
#include <sys/ioctl.h>
#include <termios.h>
static struct termios oldstdin;
static struct termios oldstdout;
static void usage(int argc, char **argv)
{
fprintf(stderr, "usage: %s [-b binary] [-c cpu type] [-r romfile] [-n cycle count]\n", argv[0]);
exit(1);
}
static int init_sdl(void)
{
atexit(SDL_Quit);
return SDL_Init(SDL_INIT_TIMER);
}
static void resetconsole(void)
{
tcsetattr(0, TCSANOW, &oldstdin);
tcsetattr(1, TCSANOW, &oldstdout);
}
static void setconsole(void)
{
struct termios t;
tcgetattr(0, &oldstdin);
tcgetattr(1, &oldstdout);
atexit(&resetconsole);
t = oldstdin;
t.c_lflag = ISIG; // no input processing
// Don't interpret various control characters, pass them through instead
t.c_cc[VINTR] = t.c_cc[VQUIT] = t.c_cc[VSUSP] = '\0';
t.c_cc[VMIN] = 0; // nonblocking read
t.c_cc[VTIME] = 0; // nonblocking read
tcsetattr(0, TCSANOW, &t);
fcntl(0, F_SETFL, O_NONBLOCK);
t = oldstdout;
t.c_lflag = ISIG; // no output processing
// Don't interpret various control characters, pass them through instead
t.c_cc[VINTR] = t.c_cc[VQUIT] = t.c_cc[VSUSP] = '\0';
tcsetattr(1, TCSANOW, &t);
}
int main(int argc, char **argv)
{
// load the configuration of the emulator from the command line and config file
load_config(argc, argv);
// read in any overriding configuration from the command line
for (;;) {
int c;
int option_index = 0;
static struct option long_options[] = {
{"rom", 1, 0, 'r'},
{"cpu", 1, 0, 'c'},
{0, 0, 0, 0},
};
c = getopt_long(argc, argv, "r:c:", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'r':
printf("load rom option: '%s'\n", optarg);
add_config_key("rom", "file", optarg);
break;
case 'c':
printf("cpu core option: '%s'\n", optarg);
add_config_key("cpu", "core", optarg);
break;
default:
usage(argc, argv);
break;
}
}
setconsole();
// bring up the SDL system
if (init_sdl() < 0) {
fprintf(stderr, "error initializing sdl. aborting...\n");
return 1;
}
// initialize the system
if (initialize_system() < 0) {
fprintf(stderr, "failed to initialize system, bailing\n");
return 1;
}
// start the system, should spawn a cpu thread
system_start();
// run the SDL message loop
system_message_loop();
return 0;
}