aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/ds/arm7/source/main.cpp
blob: bee39f1efe18a0bf9ca6ba590f4f2c2b44a13088 (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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header: /cvsroot/scummvm/scummvm/backends/fs/fs.cpp,v 1.3.2.1 2004/12/18 02:33:52 fingolfin Exp $
 */

//////////////////////////////////////////////////////////////////////
// Simple ARM7 stub (sends RTC, TSC, and X/Y data to the ARM 9)
// -- joat
// -- modified by Darkain and others
//////////////////////////////////////////////////////////////////////

//#define USE_LIBCARTRESET

#include <nds.h>

#include <bios.h>
#include <arm7/touch.h>
#include <arm7/clock.h>
#include <stdlib.h>
#include <string.h>
//#include <registers_alt.h>	// not needed in current libnds
#include <NDS/scummvm_ipc.h>
//////////////////////////////////////////////////////////////////////
#ifdef USE_DEBUGGER
#include <dswifi7.h>
#endif

#include "cartreset_nolibfat.h"

#define TOUCH_CAL_X1 (*(vs16*)0x027FFCD8)
#define TOUCH_CAL_Y1 (*(vs16*)0x027FFCDA)
#define TOUCH_CAL_X2 (*(vs16*)0x027FFCDE)
#define TOUCH_CAL_Y2 (*(vs16*)0x027FFCE0)
#define SCREEN_WIDTH    256
#define SCREEN_HEIGHT   192
s32 TOUCH_WIDTH  = TOUCH_CAL_X2 - TOUCH_CAL_X1;
s32 TOUCH_HEIGHT = TOUCH_CAL_Y2 - TOUCH_CAL_Y1;
s32 TOUCH_OFFSET_X = ( ((SCREEN_WIDTH -60) * TOUCH_CAL_X1) / TOUCH_WIDTH  ) - 28;
s32 TOUCH_OFFSET_Y = ( ((SCREEN_HEIGHT-60) * TOUCH_CAL_Y1) / TOUCH_HEIGHT ) - 28;

vu8* soundData;

vu8* soundBuffer;
vu8* arm9Buffer;
bool soundFilled[4];

int playingSection;

bool needSleep = false;
int temp;

int adpcmBufferNum = 0;

// those are pixel positions of the two points you click when calibrating
#define TOUCH_CNTRL_X1   (*(vu8*)0x027FFCDC)
#define TOUCH_CNTRL_Y1   (*(vu8*)0x027FFCDD)
#define TOUCH_CNTRL_X2   (*(vu8*)0x027FFCE2)
#define TOUCH_CNTRL_Y2   (*(vu8*)0x027FFCE3)


//////////////////////////////////////////////////////////////////////

/*
void startSound(int sampleRate, const void* data, uint32 bytes, u8 channel=0, u8 vol=0x7F,  u8 pan=63, u8 format=0) {
  SCHANNEL_TIMER(channel)  = SOUND_FREQ(sampleRate);
  SCHANNEL_SOURCE(channel) = (uint32)data;
  SCHANNEL_LENGTH(channel) = bytes;
  SCHANNEL_CR(channel)     = SOUND_ENABLE | SOUND_ONE_SHOT | SOUND_VOL(vol) | SOUND_PAN(pan) | (format==1?SOUND_8BIT:SOUND_16BIT);
}


s8 getFreeSoundChannel() {
  for (int i=0; i<16; i++) {
    if ( (SCHANNEL_CR(i) & SOUND_ENABLE) == 0 ) return i;
  }
  return -1;
}
*/


s8 getFreeSoundChannel() {
//  return 0;
  for (int i=0; i<16; i++) {
    if ( (SCHANNEL_CR(i) & SCHANNEL_ENABLE) == 0 ) return i;
  }
  return -1;
}

void startSound(int sampleRate, const void* data, uint32 bytes, u8 channel=0, u8 vol=0x7F,  u8 pan=63, u8 format=0) {
//  REG_IME = IME_DISABLE;

  channel = getFreeSoundChannel();
/*  if (format == 2) {
	channel = 1;
  } else {
	channel = 0;
  }*/

  if (channel > 1) channel = 1;

  bytes &= ~7;		// Multiple of 4 bytes!
//  bytes += 4;

  SCHANNEL_CR(channel) = 0;
  SCHANNEL_TIMER(channel)  = SOUND_FREQ(sampleRate);
  SCHANNEL_SOURCE(channel) = ((uint32) (data));
  SCHANNEL_LENGTH(channel) = ((bytes & 0x7FFFFFFF) >> 2);
  SCHANNEL_REPEAT_POINT(channel) = 0;

  SCHANNEL_CR(channel + 2) = 0;
  SCHANNEL_TIMER(channel + 2)  = SOUND_FREQ(sampleRate);
  SCHANNEL_SOURCE(channel + 2) = ((uint32) (data));
  SCHANNEL_LENGTH(channel + 2) = ((bytes & 0x7FFFFFFF) >> 2);
  SCHANNEL_REPEAT_POINT(channel + 2) = 0;

  uint32 flags = SCHANNEL_ENABLE | SOUND_VOL(vol) | SOUND_PAN(pan);

  switch (format) {
	case 1: {
		flags |= SOUND_8BIT;
		flags |= SOUND_REPEAT;// | (1 << 15);
		break;
	}

	case 0: {
		flags |= SOUND_16BIT;
		flags |= SOUND_REPEAT;// | (1 << 15);
		break;
	}

	case 2: {
		flags |= SOUND_FORMAT_ADPCM;
		flags |= SOUND_ONE_SHOT;// | (1 << 15);

		SCHANNEL_SOURCE(channel) = (unsigned int) IPC->adpcm.buffer[0];
		//bytes += 32;
		SCHANNEL_LENGTH(channel) = (((bytes + 4) & 0x7FFFFFFF) >> 2);

		SCHANNEL_CR(channel + 1) = 0;
		SCHANNEL_SOURCE(channel + 1) = (unsigned int) IPC->adpcm.buffer[0];
		SCHANNEL_LENGTH(channel + 1) = (((bytes + 4) & 0x7FFFFFFF) >> 2);
		SCHANNEL_TIMER(channel + 1) = SOUND_FREQ(sampleRate);
		SCHANNEL_REPEAT_POINT(channel + 1) = 0;
		SCHANNEL_CR(channel + 1) = flags;
		temp = bytes;
		adpcmBufferNum = 0;
		break;
	}
  }


//  if (bytes & 0x80000000) {
//    flags |= SOUND_REPEAT;
//  } else {
//  }




  soundData = (vu8* ) data;

  SCHANNEL_CR(channel)     = flags;
  SCHANNEL_CR(channel + 2)     = flags;



  if (channel == 0) {
	for (volatile int i = 0; i < 16384 * 2; i++) {
		// Delay loop - this makes everything stay in sync!
	}

	TIMER0_CR = 0;
	TIMER0_DATA = SOUND_FREQ(sampleRate) * 2;
	TIMER0_CR = TIMER_ENABLE | TIMER_DIV_1;

	TIMER1_CR = 0;
	TIMER1_DATA = 65536 - ((bytes & 0x7FFFFFFF) >> 3);		// Trigger four times during the length of the buffer
	TIMER1_CR = TIMER_ENABLE | TIMER_IRQ_REQ | TIMER_CASCADE;

    playingSection = 0;
  } else {
	for (volatile int i = 0; i < 16384 * 2; i++) {
		// Delay loop - this makes everything stay in sync!
	}

	TIMER2_CR = 0;
	TIMER2_DATA = SOUND_FREQ(sampleRate) * 2;
	TIMER2_CR = TIMER_ENABLE | TIMER_DIV_1;

	TIMER3_CR = 0;
	TIMER3_DATA = 65536 - ((bytes & 0x7FFFFFFF) >> 3);		// Trigger four times during the length of the buffer
	TIMER3_CR = TIMER_ENABLE | TIMER_IRQ_REQ | TIMER_CASCADE;

	for (int r = 0; r < 4; r++) {
//		IPC->streamFillNeeded[r] = true;
	}

	IPC->streamPlayingSection = 0;
  }



//  IPC->fillSoundFirstHalf = true;
//  IPC->fillSoundSecondHalf = true;
//  soundFirstHalf = true;

//  REG_IME = IME_ENABLE;
}

void stopSound(int chan) {
 SCHANNEL_CR(chan) = 0;
}

void DummyHandler() {
	REG_IF = REG_IF;
}

uint16 powerManagerWrite(uint32 command, u32 data, bool enable) {

  uint16 result;
  SerialWaitBusy();

  // Write the command and wait for it to complete
  REG_SPICNT = SPI_ENABLE | SPI_BAUD_1MHz | (1 << 11);
  REG_SPIDATA = command | 0x80;
  SerialWaitBusy();

  // Write the second command and clock in the data
  REG_SPICNT = SPI_ENABLE | SPI_BAUD_1MHz;
  REG_SPIDATA = 0;
  SerialWaitBusy();

  result = REG_SPIDATA & 0xFF;



  // Write the command and wait for it to complete
  REG_SPICNT = SPI_ENABLE | SPI_BAUD_1MHz | (1 << 11);
  REG_SPIDATA = command;
  SerialWaitBusy();

  // Write the second command and clock in the data
  REG_SPICNT = SPI_ENABLE | SPI_BAUD_1MHz;
  REG_SPIDATA = enable? (result | data): (result & ~data);
  SerialWaitBusy();
  
  // FIXME: This function should either return something, or have a comment
  // explaining why it is valid for it to not return something. :-)
}

/*
void performSleep() {

  powerManagerWrite(0, 0x30, true);

  // Here, I set up a dummy interrupt handler, then trigger all interrupts.
  // These are just aknowledged by the handler without doing anything else.
  // Why?  Because without it the sleep mode will only happen once, and then
  // never again.  I got the idea from reading the MoonShell source.
  IME = 0;
  u32 irq = (u32) IRQ_HANDLER;
  IRQ_HANDLER = DummyHandler;
  IF = ~0;
  IME = 1;


  // Now save which interrupts are enabled, then set only the screens unfolding
  // interrupt to be enabled, so that the first interrupt that happens is the
  // one I want.
  int saveInts = IE;



  IE = IRQ_TIMER0;		// Screens unfolding interrupt

  // Now call the sleep function in the bios
  bool b;
  do {
    TIMER0_CR = 0;
	TIMER0_DATA = TIMER_FREQ(20);
	TIMER0_CR = TIMER_ENABLE | TIMER_DIV_64;

	swiDelay(100);

	swiSleep();

	swiDelay(100);

	powerManagerWrite(0, 0x30, b = !b);
  } while (!(TIMER0_CR & TIMER_ENABLE));

  TIMER0_CR = 0;

  // We're back from sleep, now restore the interrupt state and IRQ handler
  IRQ_HANDLER = (void (*)()) irq;
  IE = saveInts;
  IF = ~0;
  IME = 1;



  powerManagerWrite(0, 0x30, false);
}

*/
void performSleep() {
  powerManagerWrite(0, 0x30, true);

  IPC->performArm9SleepMode = true;	// Tell ARM9 to sleep

//  u32 irq = (u32) IRQ_HANDLER;
//  IRQ_HANDLER = DummyHandler;
//  POWER_CR &= ~POWER_SOUND;

//  int saveInts = REG_IE;
//  REG_IE = (1 << 22) | IRQ_VBLANK;		// Lid open
//  *((u32*) (0x0380FFF8)) = *((u32*) (0x0380FFF8)) | (REG_IE & REG_IF);
//  VBLANK_INTR_WAIT_FLAGS = IRQ_VBLANK;


  int r = 0;
  while ((REG_KEYXY & (1 << 7))) {		// Wait for lid to open
	swiDelay(1000000);
	r++;
  }

//  IRQ_HANDLER = (void (*)()) irq;
  IPC->performArm9SleepMode = false;	// Tell ARM9 to wake up
//  REG_IE = saveInts;

//  POWER_CR |= POWER_SOUND;

  powerManagerWrite(0, 0x30, false);
}

void powerOff() {
	powerManagerWrite(0, 0x40, true);
}
//////////////////////////////////////////////////////////////////////



void InterruptTimer1() {

	IPC->fillNeeded[playingSection] = true;
	soundFilled[playingSection] = false;

	if (playingSection == 3) {
//		IME = IME_DISABLED;

	//	while (SCHANNEL_CR(0) & SCHANNEL_ENABLE) {
	//	}
//		SCHANNEL_CR(0) &= ~SCHANNEL_ENABLE;

//		SCHANNEL_CR(0) |= SCHANNEL_ENABLE;
//		TIMER1_CR = 0;
//		TIMER1_CR = TIMER_ENABLE | TIMER_IRQ_REQ | TIMER_CASCADE;

		playingSection = 0;

//		IME = IME_ENABLED;
	} else {
		playingSection++;
	}

	IPC->playingSection = playingSection;

/*	for (int r = 0; r < 4; r++) {
		//if ((!soundFilled[r]) && (!IPC->fillNeeded[playingSection])) {
			memcpy((void *) (soundBuffer + (r * 1024)), (void *) (arm9Buffer + (r * 1024)), 1024);

			vu16* p = (vu16 *) (soundBuffer);
			//for (int t = 0; t < 2048; t++) {
		//		*(p + t) = (t & 1)? 0xF000: 0x0000;
			//}
			soundFilled[r] = true;
		//}
	}*/
}

void InterruptTimer3() {
	while (IPC->adpcm.semaphore);		// Wait for buffer to become free if needed
	IPC->adpcm.semaphore = true;		// Lock the buffer structure to prevent clashing with the ARM7

	IPC->streamFillNeeded[IPC->streamPlayingSection] = true;

	if (IPC->streamPlayingSection == 3) {
		IPC->streamPlayingSection = 0;
	} else {
		IPC->streamPlayingSection++;
	}


	IPC->adpcm.semaphore = false;
}

//  IPC->performArm9SleepMode = false;

	// precalculate some values
//  static int16 TOUCH_WIDTH  = TOUCH_CAL_X2 - TOUCH_CAL_X1;
//  static int16 TOUCH_HEIGHT = TOUCH_CAL_Y2 - TOUCH_CAL_Y1;
//  static int16 CNTRL_WIDTH  = TOUCH_CNTRL_X2 - (TOUCH_CNTRL_X1 - 8);
//  static int16 CNTRL_HEIGHT = TOUCH_CNTRL_Y2 - (TOUCH_CNTRL_Y1 - 8);




 void InterruptVBlank() {
    uint16 but=0, x=0, y=0, xpx=0, ypx=0, z1=0, z2=0, batt=0, aux=0;
    int t1=0, t2=0;
    uint32 temp=0;
    uint8 ct[sizeof(IPC->curtime)];
	static int heartbeat = 0;
    // Update the heartbeat
    heartbeat++;

    // Read the X/Y buttons and the /PENIRQ line
    but = REG_KEYXY;
    if (!(but & 0x40)) {
      // Read the touch screen
	  touchPosition p = touchReadXY();

//      x = touchRead(TSC_MEASURE_X);
 //     y = touchRead(TSC_MEASURE_Y);

	  x = p.x;
	  y = p.y;

	  xpx = p.px;
	  ypx = p.py;

//      xpx = ( ((SCREEN_WIDTH -60) * x) / TOUCH_WIDTH  ) - TOUCH_OFFSET_X;
  //    ypx = ( ((SCREEN_HEIGHT-60) * y) / TOUCH_HEIGHT ) - TOUCH_OFFSET_Y;

//	  xpx = (IPC->touchX - (int16) TOUCH_CAL_X1) * CNTRL_WIDTH  / TOUCH_WIDTH  + (int16) (TOUCH_CNTRL_X1 - 8);
	//  ypx = (IPC->touchY - (int16) TOUCH_CAL_Y1) * CNTRL_HEIGHT / TOUCH_HEIGHT + (int16) (TOUCH_CNTRL_Y1 - 8);


      z1 = touchRead(TSC_MEASURE_Z1);
      z2 = touchRead(TSC_MEASURE_Z2);
    }

    if (but & (1 << 7)) {		// Check if screen is folded
	  needSleep = true;
	}


    batt = touchRead(TSC_MEASURE_BATTERY);
    aux  = touchRead(TSC_MEASURE_AUX);

    // Read the time
    rtcGetTime((uint8 *)ct);
    BCDToInteger((uint8 *)&(ct[1]), 7);

    // Read the temperature
    temp = touchReadTemperature(&t1, &t2);


    // Update the IPC struct
    IPC->heartbeat = heartbeat;
    IPC->buttons   = but;
    IPC->touchX    = x;
    IPC->touchY    = y;
    IPC->touchXpx  = xpx;
    IPC->touchYpx  = ypx;
    IPC->touchZ1   = z1;
    IPC->touchZ2   = z2;
    IPC->battery   = batt;
    IPC->aux       = aux;

    for (u32 i=0; i<sizeof(ct); i++) {
      IPC->curtime[i] = ct[i];
    }

    IPC->temperature = temp;
    IPC->tdiode1 = t1;
    IPC->tdiode2 = t2;



	//sound code  :)
    TransferSound *snd = IPC->soundData;
    IPC->soundData = 0;
    if (snd) {
      for (int i=0; i<snd->count; i++) {
        s8 chan = getFreeSoundChannel();
		if (snd->data[i].rate > 0) {
			if (chan >= 0) {
			  startSound(snd->data[i].rate, snd->data[i].data, snd->data[i].len, chan, snd->data[i].vol, snd->data[i].pan, snd->data[i].format);
			}
		} else {
			stopSound(-snd->data[i].rate);
		}
      }
    }


 #ifdef USE_DEBUGGER
    Wifi_Update(); // update wireless in vblank
 #endif
}

//////////////////////////////////////////////////////////////////////


#ifdef USE_DEBUGGER

// callback to allow wifi library to notify arm9
void arm7_synctoarm9() { // send fifo message
   REG_IPC_FIFO_TX = 0x87654321;
}
// interrupt handler to allow incoming notifications from arm9
void arm7_fifo() { // check incoming fifo messages
   u32 msg = REG_IPC_FIFO_RX;
   if (msg==0x87654321) Wifi_Sync();
}



void initDebugger() {

	// set up the wifi irq
	irqSet(IRQ_WIFI, Wifi_Interrupt); // set up wifi interrupt
	irqEnable(IRQ_WIFI);

    //get them talking together

	// sync with arm9 and init wifi
	u32 fifo_temp;

	while (1) { // wait for magic number
		while (REG_IPC_FIFO_CR&IPC_FIFO_RECV_EMPTY) swiWaitForVBlank();
		fifo_temp=REG_IPC_FIFO_RX;
		if (fifo_temp==0x12345678) break;
	}

	while (REG_IPC_FIFO_CR&IPC_FIFO_RECV_EMPTY) swiWaitForVBlank();
	fifo_temp=REG_IPC_FIFO_RX; // give next value to wifi_init
	Wifi_Init(fifo_temp);

	irqSet(IRQ_FIFO_NOT_EMPTY,arm7_fifo); // set up fifo irq
	irqEnable(IRQ_FIFO_NOT_EMPTY);
	REG_IPC_FIFO_CR = IPC_FIFO_ENABLE | IPC_FIFO_RECV_IRQ;

	Wifi_SetSyncHandler(arm7_synctoarm9); // allow wifi lib to notify arm9
	// arm7 wifi init complete

}
#endif

#ifdef USE_LIBCARTRESET
void reboot() {
	cartExecute();
}
#endif


int main(int argc, char ** argv) {


#ifdef USE_DEBUGGER
  REG_IPC_FIFO_CR = IPC_FIFO_ENABLE | IPC_FIFO_SEND_CLEAR;
#endif

  // Reset the clock if needed
  rtcReset();

  //enable sound
  powerON(POWER_SOUND);
  SOUND_CR = SOUND_ENABLE | SOUND_VOL(0x7F);
  IPC->soundData = 0;
  IPC->reset = false;




  for (int r = 0; r < 8; r++) {
	IPC->adpcm.arm7Buffer[r] = (u8 *) malloc(512);
  }

  for (int r = 0; r < 4; r++) {
	soundFilled[r] = false;
  }


  // Set up the interrupt handler

  irqInit();

  irqSet(IRQ_VBLANK, InterruptVBlank);
  irqEnable(IRQ_VBLANK);

  irqSet(IRQ_TIMER1, InterruptTimer1);
  irqEnable(IRQ_TIMER1);

  irqSet(IRQ_TIMER3, InterruptTimer3);
  irqEnable(IRQ_TIMER3);

/*  REG_IME = 0;
  IRQ_HANDLER = &InterruptHandler;
  REG_IE = IRQ_VBLANK | IRQ_TIMER1 | IRQ_TIMER3;
  REG_IF = ~0;
  DISP_SR = DISP_VBLANK_IRQ;
  REG_IME = 1;
  */


#ifdef USE_DEBUGGER
  initDebugger();
#endif

  // Keep the ARM7 out of main RAM
  while ((1)) {
	if (needSleep) {
		performSleep();
		needSleep = false;
	}

#ifdef USE_LIBCARTRESET
	if (passmeloopQuery()) {
		reboot();
	}
#endif

	if (IPC->reset) {
		powerOff();
	}

	swiWaitForVBlank();
  }
  return 0;
}


//////////////////////////////////////////////////////////////////////