summaryrefslogtreecommitdiff
path: root/emu/emul.c
blob: bf64d13dcd916178455832971e03c980ca3efda7 (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
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
#include "emul.h"

#include <stdio.h>
#include <stdlib.h>

void emul_init( emul_t *emul, cpu_6502_t *cpu, bus_t *bus, int width, int height )
{
	emul->cpu = cpu;
	emul->bus = bus;
	emul->gui = false;
	emul->width = width;
	emul->height = height;
	emul->debug = false;
}

void emul_start( emul_t *emul )
{
	if( emul->gui ) {
#ifdef WITH_GUI
		int rt = SDL_Init( SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO );
		if( rt < 0 ) {
			fprintf( stderr, "ERROR: SDL_Init failed: %s\n", SDL_GetError( ) );
			exit( EXIT_FAILURE );
		}
		atexit( SDL_Quit );
		SDL_ShowCursor( SDL_ENABLE );
		
		int display = -1;
		for( int i = 0; i < SDL_GetNumVideoDisplays( ); i++ ) {
			SDL_Rect rect;
			if( SDL_GetDisplayBounds( i, &rect ) == 0 ) {
				fprintf( stderr, "INFO: display %d has dimensions %dx%d\n",
					i, rect.w, rect.h );
				display = i;
			} else {
				fprintf( stderr, "ERROR: SDL_GetDisplayBounds failed: %s\n", SDL_GetError( ) );
				exit( EXIT_FAILURE );
			}
		}
		if( display < 0 ) {
			fprintf( stderr, "ERROR: no video display found\n" );
			exit( EXIT_FAILURE );
		}
		
		emul->window = SDL_CreateWindow( "6502 emu",  
			SDL_WINDOWPOS_UNDEFINED_DISPLAY( display ),
			SDL_WINDOWPOS_UNDEFINED_DISPLAY( display ),
			emul->width, emul->height, 0 );
		if( emul->window == NULL ) {
			fprintf( stderr, "ERROR: SDL_CreateWindow failed: %s\n", SDL_GetError( ) );
			exit( EXIT_FAILURE );
		}

		emul->renderer = SDL_CreateRenderer( emul->window, -1, 0 );
		if( emul->renderer == NULL ) {
			fprintf( stderr, "ERROR: SDL_Renderer failed: %s\n", SDL_GetError( ) );
			exit( EXIT_FAILURE );
		}
		
		SDL_ShowWindow( emul->window );
		SDL_SetRenderDrawColor( emul->renderer, 0, 0, 0, 255 );
		SDL_RenderClear( emul->renderer );
		
		emul->background_image = SDL_LoadBMP( "../other/breadboard.bmp" );
		emul->background_texture = SDL_CreateTextureFromSurface( emul->renderer, emul->background_image );
		SDL_RenderCopy( emul->renderer, emul->background_texture, NULL, NULL );
				
		SDL_RenderPresent( emul->renderer );
#else
		fprintf( stderr, "WARN: gui enabled and not compiled with WITH_GUI (SDL2)\n" );
#endif
	}
}

void emul_run( emul_t *emul, int nof_steps )
{
#ifdef WITH_GUI
	if( emul->gui ) {
		SDL_Event event;
		bool done = false;
		while( !done ) {
			uint32_t frame_start = SDL_GetTicks( );
			
			SDL_PollEvent( &event );
			
			switch( event.type ) {
				case SDL_KEYDOWN:
					switch( event.key.keysym.sym ) {
						case SDLK_ESCAPE:
							done = true;
							break;
					}
					break;

				case SDL_QUIT:
					done = true;
					break;
			}
			
			cpu_6502_run( emul->cpu, CPU_FREQUENCY / DISPLAY_FPS );
			
			if( emul->debug ) {
				cpu_6502_print_state( emul->cpu, 0 );
			}
			
			SDL_RenderCopy( emul->renderer, emul->background_texture, NULL, NULL );

			for( int i = 0; i < emul->bus->nof_devices; i++ ) {
				device_t *device = emul->bus->devices[i].device;
				device->vtable->draw( device, emul->renderer );
			}

			SDL_RenderPresent( emul->renderer );

			uint32_t frame_end = SDL_GetTicks( );
			int delay = frame_start + 1000 / DISPLAY_FPS - frame_end;
			if( delay > 0 ) {
				SDL_Delay( delay );
			}
			
			if( nof_steps != -1 && emul->cpu->steps > nof_steps ) {
				fprintf( stderr, "INFO: final number of steps reached (%d), terminating now\n", emul->cpu->steps );
				done = true;
			}
		}
	} else {
		cpu_6502_run( emul->cpu, nof_steps );
	}
#else
	cpu_6502_run( emul->cpu, nof_steps );
#endif
}

void emul_free( emul_t *emul )
{
#ifdef WITH_GUI
	if( emul->gui ) {
		SDL_DestroyTexture( emul->background_texture );
		SDL_FreeSurface( emul->background_image );
		SDL_DestroyRenderer( emul->renderer );
		SDL_DestroyWindow( emul->window );
	}
#endif
}