-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
243 lines (162 loc) · 4.35 KB
/
main.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
Tangerine Risc-V SOC emulator
Copyright (c) 2024, Michał Kubecki - qubeck78@wp.pl
Supplied under BSD-3 Clause license ( see LICENSE file )
Note: While not enforced by the license, I kindly request that any application
using this software includes "Michal Kubecki - qubeck78@wp.pl" in its about
or credits section as a token of appreciation for the open-source contribution.
*/
#include "main.h"
#include <cstring>
#include <climits>
#include "tangerine.h"
#include "tgVideoOut.h"
#include "emul.h"
#include "memio.h"
#include "srec.h"
#include "sdCard.h"
#include "disasm.h"
#include "debugger.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/html5.h>
#endif
tangerineCtx_t tgctx;
debugCtx_t dbgctx;
void mainLoop()
{
uint32_t cpurv;
uint32_t i;
uint32_t j;
if( tgctx.debuggerActive )
{
if( dbgMain( &dbgctx ) == RV_QUIT )
{
#ifdef __EMSCRIPTEN__
emscripten_cancel_main_loop();
#else
tgctx.exitMainLoop = 1;
#endif
}
tgRedrawScreen( &tgctx );
//audioMain( &tgctx.audioContext );
}
else
{
for( j = 0; j < 1000; j++ )
{
for( i = 0; i < 1000; i++ )
{
cpurv = rvStep( &tgctx.cpuctx );
if( cpurv )
{
tgctx.debuggerActive = 1; //init & run debugger
break;
}
}
if( tgctx.rootRegs.mtime < tgctx.rootRegs.mtimeCmp )
{
tgctx.mtimeIrqTriggered = 0;
}
tgctx.rootRegs.mtime += 1000;
if( ( tgctx.rootRegs.mtime >= tgctx.rootRegs.mtimeCmp ) && ( tgctx.mtimeIrqTriggered == 0 ) )
{
//trigger mtime irq
rvTriggerMtimeIRQ( &tgctx.cpuctx );
tgctx.mtimeIrqTriggered = 1;
}
}
tgRedrawScreen( &tgctx );
tgctx.rootRegs.frameTimer++;
tgctx.rootRegs.videoVSync = 1; //sim vsync
audioMain( &tgctx.audioContext );
if( tgHandleEvents( &tgctx ) == RV_QUIT )
{
#ifdef __EMSCRIPTEN__
emscripten_cancel_main_loop();
#else
tgctx.exitMainLoop = 1;
#endif
}
}
}
int main( int argc, char** argv )
{
uint32_t i;
printf( "TangerineRiscVSOC emulator B20241110 -qUBECk78@wp.pl-\n\n" );
//memory access
if( mioInit( &tgctx) )
{
return 1;
}
tgctx.cpuctx.fetchInstruction = fetchInstruction;
tgctx.cpuctx.fetchData = fetchData;
tgctx.cpuctx.storeData = storeData;
//sd card
if( sdcInit( &tgctx.sdCardContext, (char*)"sdcardPrv.img" ) )
{
if( sdcInit( &tgctx.sdCardContext, (char*)"sdcard.img" ) )
{
printf( "Can't load sd card image. Ensure srcard.img is in the same dir as emulator executable.\n" );
}
}
//usb host
if( usbHostInit( &tgctx.usbHostContext ) )
{
printf( "Error, can't init usb host emulation\n" );
}
tgctx.displayFullscreen = 0;
//hardware emulation
if( tgInit( &tgctx ) )
{
tgClose( &tgctx );
return 1;
}
//audio, has to be called after tgInit because it depends on SDL
if( audioInit( &tgctx.audioContext, tgctx.dmaRAM ) )
{
printf( "Error, can't init audio\n" );
}
rvReset( &tgctx.cpuctx );
if( srecLoadFile( ( char* )"boot.rec", &i ) )
{
printf( "Error, can't load bootloader. Ensure boot.rec file is in the same dir as emulator executable.\n" );
}
if( argc > 1 )
{
printf( "Loading: \"%s\" ", argv[1] );
if( srecLoadFile( argv[1], &i ) )
{
printf( "error\n" );
}
else
{
tgctx.cpuctx.pc = i;
printf( "ok\n" );
}
}
else
{
printf( "Error: No app to load given - running default bootloader.\nusage: tangerine program.rec\n" );
}
printf( "\n" );
if( dbgInit( &dbgctx, &tgctx ) )
{
printf( "Error, can't init debugger\n" );
}
else
{
printf( "Press F12 to activate debugger\n" );
}
tgctx.exitMainLoop = 0;
do
{
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop( mainLoop, 0, 1 );
#else
mainLoop();
#endif
}while( !tgctx.exitMainLoop );
tgClose( &tgctx );
return 0;
}