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
|
#include "emul.h"
#include <stdio.h>
#include <stdlib.h>
void emul_init( emul_t *emul, cpu_6502_t *cpu, memory_t *memory )
{
emul->cpu = cpu;
emul->memory = memory;
emul->gui = 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;
SDL_Rect display_rect;
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;
display_rect = rect;
} 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 ),
600, 250, 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 )
{
#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_QUIT:
done = true;
break;
}
cpu_6502_run( emul->cpu, CPU_FREQUENCY / DISPLAY_FPS );
cpu_6502_print_state( emul->cpu, 0 );
SDL_RenderCopy( emul->renderer, emul->background_texture, NULL, NULL );
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 );
}
}
} else {
cpu_6502_run( emul->cpu, 100 );
}
#else
cpu_6502_run( emul->cpu, 100 );
#endif
}
void emul_free( emul_t *emul )
{
SDL_DestroyTexture( emul->background_texture );
SDL_FreeSurface( emul->background_image );
SDL_DestroyRenderer( emul->renderer );
SDL_DestroyWindow( emul->window );
}
|