summaryrefslogtreecommitdiff
path: root/emu/emu.c
blob: 2723880f5a5dce62a7c6a379d33ddf8c04e49752 (plain)
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
#include "emul.h"
#include "6502.h"
#include "memory.h"
#include "7seg.h"

#include <stdlib.h>

#include "options.h"
#include "version.h"

static int parse_options_and_arguments( int argc, char *argv[], struct gengetopt_args_info *args_info ) {
        cmdline_parser_init( args_info );

        if( cmdline_parser2( argc, argv, args_info, 1, 0, 1 ) != 0 ) {
        	cmdline_parser_free( args_info );
        	return 1;
        }

        return 0;
}

int main( int argc, char *argv[] )
{
	struct gengetopt_args_info args_info;
	emul_t emul;
	cpu_6502_t cpu;
	memory_t memory;
	seg7_t seg7;

	if( parse_options_and_arguments( argc, argv, &args_info ) != 0 ) {
		exit( EXIT_FAILURE );
	}
	
	if( args_info.long_version_given ) {
		printf( "emu version: %s, Copyright (c) 2020, LGPLv3, Andreas Baumann <mail at andreasbaumann dot cc>\n", EMU_VERSION );
		exit( EXIT_SUCCESS );
	}

	seg7_init( &seg7 );
		
	memory_init( &memory, &seg7 );
	memory_load( &memory, ROM_START, ROM_SIZE, args_info.rom_arg );
		
	cpu_6502_init( &cpu, &memory );
	if( args_info.debug_given ) {
		if( args_info.print_cpu_given ) {
			cpu.debug_flags |= DEBUG_STATUS;
		}
		if( args_info.print_zero_page_given ) {
			cpu.debug_flags |= DEBUG_ZERO_PAGE;
		}
		if( args_info.print_stack_given ) {
			cpu.debug_flags |= DEBUG_STACK;
		}
		if( args_info.print_7seg_given ) {
			seg7.debug = true;
		}
	}
	cpu_6502_reset( &cpu );
		
	emul_init( &emul, &cpu, &memory, args_info.width_arg, args_info.height_arg );
	if( args_info.gui_given ) {
		if( args_info.debug_given ) {
			fprintf( stderr, "ERROR: don't run debug and GUI together! Uses too many resources!\n" );
			exit( EXIT_SUCCESS );
		}
		emul.gui = true;	
	}
	emul_start( &emul );
	
	emul_run( &emul, args_info.steps_arg );
	
	emul_free( &emul );
	exit( EXIT_SUCCESS );
}