diff src/dsarm7.c @ 0:c84446dfb3f5

initial add
author paulo@localhost
date Fri, 13 Mar 2009 00:39:12 -0700
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/dsarm7.c	Fri Mar 13 00:39:12 2009 -0700
     1.3 @@ -0,0 +1,200 @@
     1.4 +/*---------------------------------------------------------------------------------
     1.5 +
     1.6 +	LOCKJAW Tetromino Game: ARM7 core
     1.7 +
     1.8 +	Copyright (C) 2005-2007
     1.9 +		Damian Yerrick (tepples)
    1.10 +
    1.11 +	based on
    1.12 +	default ARM7 core
    1.13 +
    1.14 +	Copyright (C) 2005-2007
    1.15 +		Michael Noland (joat)
    1.16 +		Jason Rogers (dovoto)
    1.17 +		Dave Murphy (WinterMute)
    1.18 +
    1.19 +	This software is provided 'as-is', without any express or implied
    1.20 +	warranty.  In no event will the authors be held liable for any
    1.21 +	damages arising from the use of this software.
    1.22 +
    1.23 +	Permission is granted to anyone to use this software for any
    1.24 +	purpose, including commercial applications, and to alter it and
    1.25 +	redistribute it freely, subject to the following restrictions:
    1.26 +
    1.27 +	1.	The origin of this software must not be misrepresented; you
    1.28 +		must not claim that you wrote the original software. If you use
    1.29 +		this software in a product, an acknowledgment in the product
    1.30 +		documentation would be appreciated but is not required.
    1.31 +	2.	Altered source versions must be plainly marked as such, and
    1.32 +		must not be misrepresented as being the original software.
    1.33 +	3.	This notice may not be removed or altered from any source
    1.34 +		distribution.
    1.35 +
    1.36 +---------------------------------------------------------------------------------*/
    1.37 +#include <stdlib.h>
    1.38 +#include <nds.h>
    1.39 +#include "talkback.h"  // custom talkback
    1.40 +
    1.41 +void ljSoundEffects(unsigned long int sounds,
    1.42 +                           int countdown);
    1.43 +void ljMusic(void);
    1.44 +extern int order;
    1.45 +extern char ticksLeft;
    1.46 +extern char row;
    1.47 +extern char musicPaused;
    1.48 +
    1.49 +
    1.50 +
    1.51 +
    1.52 +void powerOffDS(void)
    1.53 +{
    1.54 +  writePowerManagement(PM_CONTROL_REG, 1 << 6);//6 DS power (0: on, 1: shut down!)
    1.55 +}
    1.56 +
    1.57 +void doSleep(void) {
    1.58 +
    1.59 +  // Check if the lid has been closed.
    1.60 +  if(IPC->buttons & BIT(7)) {
    1.61 +    // Save the current interrupt sate.
    1.62 +    u32 ie_save = REG_IE;
    1.63 +    // Turn the speaker down.
    1.64 +    swiChangeSoundBias(0,0x400);
    1.65 +    // Save current power state.
    1.66 +    int power = readPowerManagement(PM_CONTROL_REG);
    1.67 +    // Set sleep LED.
    1.68 +    writePowerManagement(PM_CONTROL_REG, PM_LED_CONTROL(1));
    1.69 +    // Register for the lid interrupt.
    1.70 +    REG_IE = IRQ_LID;
    1.71 +   
    1.72 +    // Power down till we get our interrupt.
    1.73 +    swiSleep(); //waits for PM (lid open) interrupt
    1.74 +   
    1.75 +    REG_IF = ~0;
    1.76 +    // Restore the interrupt state.
    1.77 +    REG_IE = ie_save;
    1.78 +    // Restore power state.
    1.79 +    writePowerManagement(PM_CONTROL_REG, power);
    1.80 +   
    1.81 +    // Turn the speaker up.
    1.82 +    swiChangeSoundBias(1,0x400);
    1.83 +    IPC->buttons &= ~BIT(7);
    1.84 +  }
    1.85 +}
    1.86 +
    1.87 +void VblankHandler(void) {
    1.88 +
    1.89 +  doSleep();
    1.90 +
    1.91 +  volatile P8A7Talkback *tb = (P8A7Talkback *)IPC->soundData;
    1.92 +  if (tb) {
    1.93 +      int cmd = tb->cmd;
    1.94 +      tb->cmd = 0;
    1.95 +
    1.96 +      switch (cmd) {
    1.97 +      case TALKBACK_POWER_OFF:
    1.98 +        powerOFF(POWER_SOUND);
    1.99 +        powerOffDS();
   1.100 +        swiWaitForVBlank();
   1.101 +        break;
   1.102 +      case TALKBACK_PLAY_MUSIC:
   1.103 +        musicPaused = 0;
   1.104 +        break;
   1.105 +      case TALKBACK_STOP_MUSIC:
   1.106 +        musicPaused = 1;
   1.107 +        order = 0;
   1.108 +        row = 0;
   1.109 +        ticksLeft = 1;
   1.110 +        break;
   1.111 +      case TALKBACK_PAUSE_MUSIC:
   1.112 +        musicPaused = 1;
   1.113 +      }
   1.114 +      unsigned long int fx = tb->sounds;
   1.115 +      tb->sounds = 0;
   1.116 +      
   1.117 +      ljSoundEffects(fx, tb->countdown);
   1.118 +  }
   1.119 +
   1.120 +  ljMusic();
   1.121 +
   1.122 +}
   1.123 +
   1.124 +
   1.125 +
   1.126 +// All below this is wintermute's and may need to be replaced
   1.127 +// with new versions of libnds
   1.128 +
   1.129 +
   1.130 +touchPosition first,tempPos;
   1.131 +
   1.132 +//---------------------------------------------------------------------------------
   1.133 +void VcountHandler() {
   1.134 +//---------------------------------------------------------------------------------
   1.135 +	static int lastbut = -1;
   1.136 +	
   1.137 +	uint16 but=0, x=0, y=0, xpx=0, ypx=0, z1=0, z2=0;
   1.138 +
   1.139 +	but = REG_KEYXY;
   1.140 +
   1.141 +	if (!( (but ^ lastbut) & (1<<6))) {
   1.142 + 
   1.143 +		tempPos = touchReadXY();
   1.144 +
   1.145 +		if ( tempPos.x == 0 || tempPos.y == 0 ) {
   1.146 +			but |= (1 <<6);
   1.147 +			lastbut = but;
   1.148 +		} else {
   1.149 +			x = tempPos.x;
   1.150 +			y = tempPos.y;
   1.151 +			xpx = tempPos.px;
   1.152 +			ypx = tempPos.py;
   1.153 +			z1 = tempPos.z1;
   1.154 +			z2 = tempPos.z2;
   1.155 +		}
   1.156 +		
   1.157 +	} else {
   1.158 +		lastbut = but;
   1.159 +		but |= (1 <<6);
   1.160 +	}
   1.161 +
   1.162 +	IPC->touchX			= x;
   1.163 +	IPC->touchY			= y;
   1.164 +	IPC->touchXpx		= xpx;
   1.165 +	IPC->touchYpx		= ypx;
   1.166 +	IPC->touchZ1		= z1;
   1.167 +	IPC->touchZ2		= z2;
   1.168 +	IPC->buttons		= but;
   1.169 +
   1.170 +}
   1.171 +
   1.172 +
   1.173 +
   1.174 +//---------------------------------------------------------------------------------
   1.175 +int main(int argc, char ** argv) {
   1.176 +//---------------------------------------------------------------------------------
   1.177 +
   1.178 +    // set up custom ipc struct
   1.179 +    IPC->soundData = 0;
   1.180 +
   1.181 +	// read User Settings from firmware
   1.182 +	readUserSettings();
   1.183 +
   1.184 +	//enable sound
   1.185 +	powerON(POWER_SOUND);
   1.186 +	writePowerManagement(PM_CONTROL_REG, ( readPowerManagement(PM_CONTROL_REG) & ~PM_SOUND_MUTE & ~(1 << 6) ) | PM_SOUND_AMP );
   1.187 +	SOUND_CR = SOUND_ENABLE | SOUND_VOL(0x7F);
   1.188 +
   1.189 +	irqInit();
   1.190 +
   1.191 +	// Start the RTC tracking IRQ
   1.192 +	initClockIRQ();
   1.193 +
   1.194 +	SetYtrigger(80);
   1.195 +	irqSet(IRQ_VCOUNT, VcountHandler);
   1.196 +	irqSet(IRQ_VBLANK, VblankHandler);
   1.197 +
   1.198 +
   1.199 +	irqEnable( IRQ_VBLANK | IRQ_VCOUNT);
   1.200 +
   1.201 +	// Keep the ARM7 mostly idle
   1.202 +	while (1) swiWaitForVBlank();
   1.203 +}