Kodewerx
https://www.kodewerx.org/forum/

DS Lite backlight control in any game
https://www.kodewerx.org/forum/viewtopic.php?f=2&t=1457
Page 1 of 2

Author:  chishm [ Fri Apr 20, 2007 9:38 pm ]
Post subject:  DS Lite backlight control in any game

Yesterday, while playing Metroid Prime: Hunters, I was in a crucial part of the game, but it was hard to see with the screen's backlight on a low level. Frustrated by the inability to change the brightness without rebooting, I created this code.

This code allows you to change the backlight level within any game at any time (provided you can get the code to run of course). To use it you hold L+R+SELECT then press either UP to increase the brightness or DOWN to decrease the brightness.

Code:
<cheat>
   <name>Backlight control</name>
   <note>Hold L+R+SELECT then press UP to increase brightness or DOWN to decrease brightness</note>
   <codes>94000130 FCFB0000 C2000001 000000a0 a21ab5f0 88234c24 80138811 d02a428b 25803490 f0002000 1c06f82a f0002004 2703f826 21404007 d003420b 420b2180 e018d00c 4231210c 2f03d006 1c79d013 f0002004 e00ef816 e0094331 438e210c 2f001c31 1e79d004 f0002004 e002f80a f0002000 bcf0f806 4718bc08 30800000 88222100 d1fc422a 80224a08 88208060 d1fc4228 80220c12 88228061 d1fc422a 21ff8860 47704008 04000130 80028802 d2000000 00000000 </codes>
</cheat>


This code uses the Nitro Hax cheat engine's custom code type C2 (execute custom function), so it won't work on the Action Replay in this form. It also needs to run on the ARM7, so if the cheat engine is hooked into the ARM9 instead, it won't work.

Author:  chishm [ Fri Apr 20, 2007 9:43 pm ]
Post subject: 

Here is the source code for it.

brightness.s:
Code:
.thumb

@ all of the following are 16 bit
.equ   REG_SPICNT,  0x040001C0
.equ   REG_SPIDATA,    0x040001C2
.equ   REG_KEYINPUT,   0x04000130

.equ   KEYS_L_R_SELECT, ((1<<9) | (1<<8) | (1<<2))
.equ   KEYS_UP,   (1<<6)
.equ   KEYS_DOWN,   (1<<7)

.equ   PM_CONTROL_REG, 0
.equ   PM_LITE_BRIGHT_REG, 4
.equ   PM_BACKLIGHTS, ((1<<2) | (1<<3))
.equ   SPI_BUSY, 0x80


cheat_header:
   .word   0x94000130, ((~KEYS_L_R_SELECT & 0xFFFF) << 16)   @ Button activator
   .word   0xC2000001, (brightnessHandler_end - brightnessHandler)   @ Execute custom function in thumb mode

brightnessHandler:
   push   {r4-r7, lr}
   
   adr   r2, old_key_state
   ldr   r4, =REG_KEYINPUT
   ldrh   r3, [r4]   @ Current key state
   ldrh    r1, [r2]   @ Old key state
   strh   r3,   [r2]   @ Save current key state for next time
   cmp      r3, r1      @ if keys in same state as before
   beq   brightnessHandler_exit

   @ must be done before calling readPowerMangement or writePowerManagement
   add      r4, #(REG_SPICNT - REG_KEYINPUT)
   mov      r5, #SPI_BUSY

   @ Old power management setting
   mov   r0, #PM_CONTROL_REG
   bl   readPowerMangement
   mov   r6, r0      @ r6 = old power management setting

   @ Old backlight level setting
   mov   r0, #PM_LITE_BRIGHT_REG
   bl   readPowerMangement
   mov   r7, #0x03
   and   r7, r0      @ r7 = old backlight level setting

   mov   r1, #KEYS_UP
   tst   r3, r1
   beq   brightnessHandler_keyUp
   mov   r1, #KEYS_DOWN
   tst   r3, r1
   beq   brightnessHandler_keyDown
   b   brightnessHandler_exit
   
brightnessHandler_keyUp:
   @ enable backlights if off
   mov   r1, #PM_BACKLIGHTS
   tst   r1,   r6
   beq   brightnessHandler_turnOnScreens

   @ increase brightness
   cmp      r7, #0x03
   beq      brightnessHandler_exit
   add      r1, r7, #1
   
   mov   r0, #PM_LITE_BRIGHT_REG
   bl   writePowerManagement
   b   brightnessHandler_exit

brightnessHandler_turnOnScreens:
   @ enable backlights
   orr   r1,   r6
   b   brightnessHandler_setScreenStatus
   
brightnessHandler_keyDown:
   @ get ready to disable backlights if needed
   mov   r1, #PM_BACKLIGHTS
   bic   r6, r1
   mov   r1, r6
   @ turn off screens if already on lowest level
   cmp      r7, #0x00
   beq      brightnessHandler_setScreenStatus
   
   @ reduce brightness
   sub      r1, r7, #1
   mov   r0, #PM_LITE_BRIGHT_REG
   bl   writePowerManagement
   b   brightnessHandler_exit

brightnessHandler_setScreenStatus:
   @ set new power management control
   mov   r0, #PM_CONTROL_REG
   bl   writePowerManagement
      
brightnessHandler_exit:
   pop      {r4-r7}
   pop   {r3}
   bx   r3
   
old_key_state:
   .hword 0x0000
   
readPowerMangement:
   add      r0, #(1<<7)
   mov      r1, #0
   @ fall through to _writePowerManagement


writePowerManagement:   @ (reg, command)
   
   @ while (REG_SPICNT & SPI_BUSY);
writePowerManagement_loop1:
   ldrh   r2, [r4]
   tst      r2, r5
   bne      writePowerManagement_loop1
   
   @ REG_SPICNT = SPI_ENABLE | SPI_BAUD_1MHZ | SPI_BYTE_MODE | SPI_CONTINUOUS | SPI_DEVICE_POWER;
   ldr      r2, =((1<<15) | 2 | (0<<10) | (1<<11) | (0<<8)) | (((1<<15) | 2 | (0<<10) | (0<<8)) << 16)
   strh   r2, [r4]
   
   @ REG_SPIDATA = reg;
   strh      r0, [r4, #2]
   
   @ while (REG_SPICNT & SPI_BUSY);
writePowerManagement_loop2:
   ldrh   r0, [r4]
   tst      r0, r5
   bne      writePowerManagement_loop2
   
   @ REG_SPICNT = SPI_ENABLE | SPI_BAUD_1MHZ | SPI_BYTE_MODE | SPI_DEVICE_POWER;
   lsr      r2, #16
   strh   r2, [r4]
   
   @ REG_SPIDATA = command;
   strh   r1, [r4, #2]
   
   @ while (REG_SPICNT & SPI_BUSY);
writePowerManagement_loop3:
   ldrh   r2, [r4]
   tst      r2, r5
   bne      writePowerManagement_loop3
   
   @ return REG_SPIDATA & 0xFF;
   ldrh   r0, [r4, #2]
   mov      r1, #0xff
   and      r0, r1
   bx       lr

constant_pool:
.pool

.align 3
brightnessHandler_end:

cheat_footer:
   .word   0xd2000000,   0x00000000   @ End if


Makefile:
Code:
include $(DEVKITARM)/ds_rules

brightness.cht   :   brightness.bin
   @od -A n -t x4 -w8 $< > $@
   @echo Raw cheat codes written to $@

brightness.bin   :   brightness.o
   $(OBJCOPY) -O binary $< $@

brightness.o   :   brightness.s


Author:  Smalls1652 [ Fri Apr 20, 2007 10:08 pm ]
Post subject: 

One question, this work on Regular DS?

Author:  mutantdreams [ Fri Apr 20, 2007 10:21 pm ]
Post subject: 

Yeah and I hope it does I love it.

Author:  Smalls1652 [ Fri Apr 20, 2007 10:24 pm ]
Post subject: 

Wait, nvm, didn't see the, dosn't work for AR part
Locks up on meh games

Author:  mutantdreams [ Fri Apr 20, 2007 10:26 pm ]
Post subject: 

Yeah and NVM I know it won't work now XD

Author:  chishm [ Fri Apr 20, 2007 11:40 pm ]
Post subject: 

Ask kenobi if he can modify it to work with his AR hack that allows custom code execution. The ASM is in THUMB, so his hack might need to be modified.

Author:  kenobi [ Sat Apr 21, 2007 2:07 am ]
Post subject: 

I guess adding something like 'add r0,r15,1 ; bx r0' (ie. E28F0001 E12FFF10) at the very start of your asm codes could be used.
Also I just wanted to point out that this code will only work on the NDS Lite version. On the regular NDS, light just goes on/off when you press Up, and off when you press down (and making some bad noise).

So anyone wanting to use chishm's Backlight control code on the AR should replace the :

...
C2000001 000000A0
A21AB5F0 88234C24
...

with

...
023FE074 012FFF11
E0000000 000000A8
E28F0001 E12FFF10
A21AB5F0 88234C24
...

and the

...
D2000000 00000000
...

with

...
023FE074 E3520003
D2000000 00000000
...

Author:  chishm [ Sat Apr 21, 2007 3:44 am ]
Post subject: 

Thanks kenobi. For those too lazy to make the modifications themselves, here is the code modified by kenobi to work for the official Action Replay (notice how it's longer, Nitro Hax is better :-p):
Code:
<cheat>
   <name>Backlight control</name>
   <note>Hold L+R+SELECT then press UP to increase brightness or DOWN to decrease brightness</note>
   <codes>94000130 FCFB0000 023FE074 012FFF11 E0000000 000000A8 E28F0001 E12FFF10 A21AB5F0 88234C24  80138811 d02a428b 25803490 f0002000 1c06f82a f0002004 2703f826 21404007 d003420b 420b2180 e018d00c 4231210c 2f03d006 1c79d013 f0002004 e00ef816 e0094331 438e210c 2f001c31 1e79d004 f0002004 e002f80a f0002000 bcf0f806 4718bc08 30800000 88222100 d1fc422a 80224a08 88208060 d1fc4228 80220c12 88228061 d1fc422a 21ff8860 47704008 04000130 80028802 023FE074 E3520003 d2000000 00000000 </codes>
</cheat>


I wasn't sure about the DS Phat (forgot to mention that I hadn't tested it) since I don't have my Phat with me. I suppose I could make a cut down code that switches the lights on/off when you press L+R+SELECT + UP/DOWN, even if it isn't as useful with this supported in some games. I just think it's a shame Nintendo didn't add this ability to the Lite, even if it they would have to do it with a hardware button.

Author:  Smalls1652 [ Sat Apr 21, 2007 11:38 am ]
Post subject: 

It'll come in handy, especially with the battery

Author:  Master SL [ Sat Apr 21, 2007 11:45 am ]
Post subject: 

I don't believe it works with the phat because the phat does not have the ability to adjust the light like the lite does.

Author:  Illusion [ Sat Apr 21, 2007 12:19 pm ]
Post subject: 

So, this works with the original Nintendo DS or not?

Author:  Z3R0 [ Sat Apr 21, 2007 12:23 pm ]
Post subject: 

regular ds would just make it turn on/off it did, but still very useful.

Author:  Illusion [ Sat Apr 21, 2007 12:25 pm ]
Post subject: 

I am going to test it right now.

Author:  James0x57 [ Sat Apr 21, 2007 12:30 pm ]
Post subject: 

Nice job! :D

Author:  Smalls1652 [ Sat Apr 21, 2007 12:40 pm ]
Post subject: 

It works on the original DS
Only problem is sound loss, I have to do the buttons correctly

Author:  Illusion [ Sat Apr 21, 2007 1:04 pm ]
Post subject: 

I just tried it and it worked perfectly. No sound loss or anything.

Author:  Smalls1652 [ Sat Apr 21, 2007 1:05 pm ]
Post subject: 

What do you have Illusion?, Lite or Regular

**Dosn't work for Animal Crossing**

Author:  Illusion [ Sat Apr 21, 2007 1:07 pm ]
Post subject: 

Regular, just tried it on MPH.

Author:  Smalls1652 [ Sat Apr 21, 2007 2:11 pm ]
Post subject: 

Hmm, creepy, Ilose some sound, it turns down the volume to low

Author:  chishm [ Sat Apr 21, 2007 8:14 pm ]
Post subject: 

Striker:
You probably started drawing too much power. If the battery light is red, don't go to a higher brightness.

Smalls1652:
Make sure you have a master code enabled for Animal Crossing. Nitro Hax v0.82 and above can use the Action Replay master code.

DS Phat owners:
I'll make a new code specifically for DS Phats when I can get ahold of mine to test on.

Author:  +++NdsH4ck3r [ Sat Apr 21, 2007 8:20 pm ]
Post subject: 

i tested it too i have dslite, tried it on MPH, works like a charm, no problems what so ever... its funny how if u turn the backlight down to much u turn the screen black...

Author:  chishm [ Sun Apr 22, 2007 1:06 am ]
Post subject: 

Here's the code for DS Phats:
Code:
<cheat>
   <name>Backlight control (Phat)</name>
   <note>Hold L+R+SELECT then press UP to turn on screens or DOWN to turn off screens</note>
   <codes>94000130 fcfb0000 c2000001 00000080 a21cb5f0 88234c1c 80138811 d019428b 25803490 f0002000 1c06f818 420b2140 2180d003 d005420b 210ce00c d1094231 e0044331 4231210c 438ed004 20001c31 f805f000 bc08bcf0 30804718 88222100 d1fc422a 80224a09 88208060 d1fc4228 80220c12 88228061 d1fc422a 21ff8860 47704008 00000000 04000130 80028802 d2000000 00000000 </codes>
</cheat>

Author:  Porkchop21 [ Tue Apr 24, 2007 9:07 pm ]
Post subject: 

Very nice work you all.

I cant wait to see what other kind of stuff like this you all might come up with

Author:  tatertot [ Wed Apr 25, 2007 7:38 am ]
Post subject: 

uh...for some reason when i use this on mph it freezes the game afer about 10mins...=/
it's for the ds lite btw

Author:  +++NdsH4ck3r [ Wed Apr 25, 2007 4:32 pm ]
Post subject: 

Freezing, eh? Well when i tried the code, have to admit i didnt have it in effect for but a few seconds. ill have to test it again for a longer period of time and see what happens.

Author:  ZachsterPoke [ Sun Apr 29, 2007 2:46 pm ]
Post subject:  Re: DS Lite backlight control in any game

So, if I decided to use this code, where would I go about putting it (as in under which game)? Also, does this specific code work for other games other than MPH as well?

Author:  Smalls1652 [ Mon Apr 30, 2007 10:22 am ]
Post subject: 

chishm wrote:
Smalls1652:
Make sure you have a master code enabled for Animal Crossing. Nitro Hax v0.82 and above can use the Action Replay master code.


Master code?
AR DS dosn't need a master code lol

Page 1 of 2 All times are UTC - 8 hours [ DST ]
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/