2017-12-09 04:12:31 +00:00
|
|
|
/*
|
|
|
|
* main.c
|
|
|
|
*
|
|
|
|
* Here we define the main entry point for the program; we also define a
|
|
|
|
* couple of functions to run when we start (init) and finish
|
|
|
|
* (...finish).
|
|
|
|
*/
|
|
|
|
|
2017-11-22 05:24:51 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-12-08 23:06:21 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2017-11-22 05:24:51 +00:00
|
|
|
|
2017-12-16 04:22:40 +00:00
|
|
|
#include "apple2.h"
|
2017-11-22 05:24:51 +00:00
|
|
|
#include "log.h"
|
2017-12-08 23:06:21 +00:00
|
|
|
#include "option.h"
|
2017-12-17 04:45:39 +00:00
|
|
|
#include "vm_screen.h"
|
2017-11-22 05:24:51 +00:00
|
|
|
|
2017-12-06 22:43:30 +00:00
|
|
|
/*
|
|
|
|
* This function will establish the base environment that we want to use
|
|
|
|
* while we execute.
|
|
|
|
*/
|
2017-11-22 05:24:51 +00:00
|
|
|
static void
|
2017-12-08 23:06:21 +00:00
|
|
|
init(int argc, char **argv)
|
2017-11-22 05:24:51 +00:00
|
|
|
{
|
2017-12-08 23:06:21 +00:00
|
|
|
int options_ok;
|
|
|
|
|
|
|
|
// If the option_parse() function returns zero, that means that it's
|
|
|
|
// signaled to us that we should stop now. Whether that means we are
|
|
|
|
// stopping in _error_ (bad input), or just because you asked for
|
|
|
|
// --help, is not really specified. We exit with a non-zero error
|
|
|
|
// code in any case.
|
|
|
|
options_ok = option_parse(argc, argv);
|
|
|
|
if (options_ok == 0) {
|
|
|
|
const char *err = option_get_error();
|
|
|
|
|
|
|
|
if (strlen(err) > 0) {
|
|
|
|
fprintf(stderr, "%s\n", err);
|
|
|
|
option_print_help();
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-12-06 22:43:30 +00:00
|
|
|
// We're literally using stdout in this heavy phase of development.
|
|
|
|
log_open(stdout);
|
2017-12-17 04:45:39 +00:00
|
|
|
|
2017-12-18 05:31:56 +00:00
|
|
|
if (vm_screen_init() != OK) {
|
|
|
|
fprintf(stderr, "Couldn't initialize video\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-11-22 05:24:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 22:43:30 +00:00
|
|
|
/*
|
|
|
|
* And this is the teardown function.
|
|
|
|
*/
|
2017-11-22 05:24:51 +00:00
|
|
|
static void
|
|
|
|
finish()
|
|
|
|
{
|
2017-12-08 23:09:58 +00:00
|
|
|
// Close any file sources we had opened
|
|
|
|
for (int i = 1; i <= OPTION_MAX_DISKS; i++) {
|
|
|
|
FILE *stream = option_get_input(i);
|
|
|
|
|
|
|
|
if (stream != NULL) {
|
|
|
|
fclose(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 05:24:51 +00:00
|
|
|
log_close();
|
2017-12-17 04:45:39 +00:00
|
|
|
|
|
|
|
vm_screen_finish();
|
2017-11-22 05:24:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 22:43:30 +00:00
|
|
|
/*
|
|
|
|
* This is what will run when the program begins, if you were new to how
|
|
|
|
* C works.
|
|
|
|
*/
|
2017-11-22 05:24:51 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2017-12-16 04:22:40 +00:00
|
|
|
apple2 *mach;
|
2017-12-17 22:42:05 +00:00
|
|
|
vm_screen *screen;
|
2017-12-16 04:22:40 +00:00
|
|
|
int err;
|
|
|
|
|
2017-12-08 23:06:21 +00:00
|
|
|
init(argc, argv);
|
2017-11-22 05:24:51 +00:00
|
|
|
|
2017-12-06 22:43:30 +00:00
|
|
|
// When we exit, we want to wrap up a few loose ends. This syscall
|
|
|
|
// will ensure that `finish()` runs whether we return from main
|
|
|
|
// successfully or if we run `exit()` from elsewhere in the program.
|
2017-11-22 05:24:51 +00:00
|
|
|
atexit(finish);
|
|
|
|
|
2017-12-21 02:45:26 +00:00
|
|
|
// Let's build the basic machine, using the width and height
|
|
|
|
// indicated by the user.
|
|
|
|
mach = apple2_create(option_get_width(),
|
|
|
|
option_get_height());
|
2017-12-16 04:22:40 +00:00
|
|
|
|
2017-12-21 02:45:26 +00:00
|
|
|
// Ok, it's time to boot this up!
|
|
|
|
err = apple2_boot(mach);
|
2017-12-16 04:22:40 +00:00
|
|
|
if (err != OK) {
|
|
|
|
fprintf(stderr, "Bootup failed!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-12-21 02:45:26 +00:00
|
|
|
// This will run for as long as we want to hang out in the emulated
|
|
|
|
// machine.
|
|
|
|
apple2_run_loop(mach);
|
2017-12-17 04:45:39 +00:00
|
|
|
|
2017-12-21 02:45:26 +00:00
|
|
|
// We're all done, so let's tear everything down.
|
|
|
|
apple2_free(mach);
|
2017-12-17 04:45:39 +00:00
|
|
|
|
2017-12-06 22:43:30 +00:00
|
|
|
// ha ha ha ha #nervous #laughter
|
2017-12-06 22:45:16 +00:00
|
|
|
printf("Hello, world\n");
|
2017-11-22 05:24:51 +00:00
|
|
|
}
|