cryptosystem.org

Archive for the ‘Coding’ Category

Using MATLAB for numerical solutions to the discrete Poisson equation

without comments

Let’s say you’ve got a 2D image (or matrix) of the Laplacian of some function, and you want to solve for the function. This is actually relatively easy to do with MATLAB, but searching for a step-by-step example I came up with nothing, so I thought I’d show how to do it here.

Poisson’s equation relates the Laplacian (\Delta f = \nabla^2 f = \nabla \cdot \nabla f) of some function with its result:

\left(\nabla^2 u \right)_{ij}=g_{ij}

So in the discrete Poisson equation we have g_{ij} defined as the non-boundary subset of a 2-D rectangular matrix of dimensions m \times n. Here 2 \le i \le m-1, 2 \le j \le n-1 to allow for boundary conditions.

Transforming u_{ij}, g_{ij} into natural ordered vectors \left[U\right], \left[b\right] allows us to rewrite as the \left(m-2\right)\left(n-2\right) \times \left(m-2\right)\left(n-2\right) linear system:

\left[A\right]\left[U\right] = \left[b\right]

So, on to the juicy bit, how to express and solve this in MATLAB given you have a m \times n 2-D matrix defining g_{ij} (for consistency, we’ll use variable names equivalent to what’s in these equations).

First, we need to reshape g into a \left(m-2\right)\left(n-2\right)\times 1 vector b that skips the boundaries:

b = -reshape(g(2:(m-1),2:(n-1)),(m-2)*(n-2),1);

If we have uniform Dirichlet boundary conditions u=0, b should now be correct. If you want other boundary conditions for u, you’ll need to add them to the appropriate positions in b, which I won’t cover here.

Thankfully, MATLAB has a nice built-in gallery of matrices which includes the sparse tridiagonal matrix we need for A:

A = gallery('poisson',m-2);

We can now solve using the backslash operator:

U = A\b;

This gives us the result in vector form which we can reshape back to what we want:

u = reshape(U,m-2,n-2);

You can pad back your u = 0 boundary conditions with:

u = padarray(u,[1 1]);

And verify the results with:

laplacian = del2(u)*4;

Which should correspond to g (with, of course, border discrepancies). There is, perhaps, some entirely built-in way of doing all this, hidden somewhere in the terse documentation.

Written by ryanfb

September 4th, 2009 at 2:27 pm

Posted in Coding, MATLAB, Math

libfat/DLDI-enabled DS BIOS Dumper

with 6 comments

I’m aware that FluBBa and pepsiman have already written BIOS dumpers, but neither of these work well with modern carts (i.e. Slot-1 carts), and no source was available. So I whipped up my own ARM7/ARM9 BIOS dumper that should work on any cart supported by DLDI:

http://www.cryptosystem.org/projects/nds/biosdumper/

Thanks to Martin Korth for the info on GBATEK, and cearn for helping me with my assembly.

Edit: And right after I posted this I found out about DSBF_dump. Oh well.

Written by ryanfb

February 25th, 2007 at 4:47 pm

Posted in Coding, Homebrew, NDS

PicoDriveDS 0.1.7

with 136 comments

PicoDriveDS 0.1.7 is up! Changes:

  • Updated for DKA r20 and latest libnds.
  • Switched to libfat with DLDI support.
  • Fixed bug where select key must be released quickly in order to go back to file selection menu.
  • Appended ROM check now only happens if FAT is unable to init.
  • Added untested Opera RAM expansion support, and RAM support for SCCF.

Grab it here.

Written by ryanfb

February 4th, 2007 at 3:13 pm

Posted in Coding, Homebrew, NDS

Simple GBA Mode Switcher

with 19 comments

With Slot-1 DS flash carts, you sometimes want to boot a GBA cart in Slot-2. There’s probably something already out there for this, but I couldn’t find one so I made a small and simple GBA mode switcher. It uses the setting you’ve selected in your DS firmware to determine which screen to run on. This is probably the shortest DS code I’ll ever write, so source is included in this post.

Read the rest of this entry »

Written by ryanfb

January 24th, 2007 at 5:05 pm

Posted in Coding, Homebrew, NDS

2006 Christmas Coding Compo Entry

without comments

I’ve decided to go ahead and post my entry to the Second Annual Drunken Coders Christmas Game Competition. I started working on it pretty late, so it’s not as polished as I would like, but I think it’s still enjoyable.

Title Screen

Gameplay

Grab it here.

Written by ryanfb

January 21st, 2007 at 7:40 am

Posted in Coding, Homebrew, NDS

PicoDriveDS 0.1.6

with 41 comments

PicoDriveDS 0.1.6 is up! Changes:

  • Added Max Media Dock FAT support (thanks chishm/Bonic)
  • Added EZ4 FAT support (thanks cory1492/Rudolph)
  • Reduced memory usage, 3MB ROMs should work again
  • Added EXPERIMENTAL support for using the additional RAM on SuperCard SD carts to load ROMs larger than 3MB. This comes with the standard warning that this feature may cause file corruption on your SD card. The SCSD may also keep the Genesis ROM in GBA ROM space after a reboot, causing PicoDriveDS to see it as an appended ROM and crash on startup – to fix this issue simply power down, take the SCSD out, and wait a few seconds.

Grab it here or in the usual location.

Written by ryanfb

November 26th, 2006 at 11:12 pm

Posted in Coding, Homebrew, NDS

PicoDriveDS 0.1.5

with 66 comments

PicoDriveDS 0.1.5 is up! Changes:

  • Added support for appended ROMs. This should allow users with carts not supported by gba_nds_fat to use PicoDriveDS. Simply append a ROM to the PicoDriveDS binary, and it will run it (on *nix systems this can be done with “cat romname.ext >> picodriveds.ext”, on Windows with “copy /b picodriveds.ext+romname.ext picodriveds-withrom.ext”, substituting the appropriate filenames of course). You can only append one ROM at a time. In this mode, Genesis SRAM will be saved to GBA SRAM. Some patchers may work correctly with this and save the SRAM to your removable media, I have only been able to test with the EZ4 Lite software.
  • Fixed M3 reset code, added M3SD Mini insertion detection code (untested, may not work)
  • Massive code restructure and cleanup (though some cleanup is still in progress)
  • Added save state support, accessed via the X button. Due to the current size of the save states, this is only supported for FAT devices and is disabled in appended ROM mode. Currently there is only one save state per ROM (saved to romname.pds). This is still a very experimental feature and save states may not be compatible with future versions of PicoDriveDS.

Grab it here or in the usual location. The source code tarball is also in the release directory, and I will check in the code to the PicoDriveDS Bounty Source project repository soon and begin using that as my primary Subversion server.

Written by ryanfb

August 22nd, 2006 at 9:52 pm

Posted in Coding, Homebrew, NDS

PicoDriveDS 0.1.4

with 54 comments

PicoDriveDS 0.1.4 is up! This is mostly a minor bugfix/enhancement release while I continue work on the new renderer. Changes:

  • Fixed bug where R could scroll past end of file list
  • Switched to unified builds, now one build should work for all SD/CF carts (thanks WinterMute)
  • Added NeoFlash MK2/MK3 support (thanks WinterMute)
  • Changed it so that pressing Select again without choosing a file will resume emulation

Get it here.

Written by ryanfb

June 22nd, 2006 at 8:35 pm

Posted in Coding, Homebrew, NDS

PicoDriveDS 0.1.3

with 7 comments

PicoDriveDS 0.1.3 is up! Changes:

  • Added long filename support
  • Added support for going up a directory by pressing B
  • Added support for skipping ahead/back 5 files at a time with R/L
  • Added automatic save/load SRAM support

Files are the same place as before or you can use the handy new release directories to be sure.

The SRAM feature is not the same thing as savestates. Basically it emulates a Genesis cart’s internal Save RAM, and whenever the SRAM is written to it writes out (romname).srm. When you load a game, it looks to see if that SRAM file is there and loads it if so.

Written by ryanfb

June 13th, 2006 at 6:15 pm

Posted in Coding, Homebrew, NDS

PicoDriveDS 0.1.2

with one comment

Since my last post about PicoDriveDS, 0.1.2 has been released. Changes:

  • Switched from Cyclone 0.0080 to Reesy’s Cyclone 0.0084
  • Switched from software scaling to hardware scaling
  • Added three scale modes: Stretch, Aspect, and 1:1
  • Cycle through scale modes with L
  • Position the window in 1:1 mode by holding R and using the D-Pad
  • Added soft reset (press select, takes you back to the ROM list)
  • Added NDS banner information (shows up in WMB)

The links without version numbers will always point to the latest version, although I’m going to start making backup copies of each version in case people ever want an old one for some reason.

Built against SaTa’s modified FAT libraries from REIN (SD users probably want this):

Built against chishm’s FAT libraries with just CF enabled:

Written by ryanfb

June 10th, 2006 at 9:36 pm

Posted in Coding, Homebrew, NDS