gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > working Java for DS - pre-pre-alpha

#136932 - josath - Mon Aug 06, 2007 5:12 pm

I took Torlus's Java port for GBA and modified it to work on NDS with DLDI.
Currently there is no access to the NDS hardware, besides printing text.

To use this demo, place kvm.nds and all the *.class files on your flashcart. Run kvm.nds, then select a .class file to run. There are a few examples testing various stuff. LcdSwapTest.class is a very simple example to prove that it is feasible to interface with the DS hardware, once a Java KNI wrapper is written around libnds.

Download alpha R2

If you want to compile your own java files, make sure they target version 1.4 (I'm not sure all the features this supports, but it wont even start if you do 1.5 or higher). For example, this will compile Hello.java into Hello.class:
Code:
javac -source 1.4 -target 1.4 Hello.java


Someone please tell me this is really dumb, before I waste too much time on it ;)

JSensebe wrote:
Java4DS: When PALib Just Isn't Slow or Clunky Enough?


-------
UPDATE: Posted a new version, this includes a way to get input from the user, read files from FAT, and to display sprites onscreen.


Last edited by josath on Wed Aug 08, 2007 5:39 pm; edited 1 time in total

#136938 - Ant6n - Mon Aug 06, 2007 6:03 pm

wouldn't it make more sense to compile java code to arm asm, using gcj or something?
it makes sense to offer some java alternative, since school's these days put a lot more emphasis on that language; and thus there would be more possible developers.

#136940 - josath - Mon Aug 06, 2007 6:40 pm

It might be more efficient, but maintaining a whole compiler is a lot more work, since I now have to target multiple differing platforms (PC -- windows, mac, etc), versus simply targeting the NDS.

#136947 - Quirky - Mon Aug 06, 2007 8:10 pm

With all due respect, you have gone the easy route ;-) A more challenging and future-proof solution, though a lot tougher, would have been to provide a DS layer for the current GPL code that works with the newer JDKs.

Perfectly fine to test the waters of course, it would be interesting to see what could be done with Java on the DS, but with no access to anything graphical it is a bit pointless.

I'm not even sure what the best way to do the graphics would be - is there any sort of mobile swing available? Or an open GL type layer? Or some custom native classes? Custom native classes have to have their native bits compiled into the the embedded VM of course (no dynamic libraries on the DS). Plus testing would be tricky... PC "emulation" of the DS native classes for testing, providing PC builds of the embedded VM including these classes? It'd be a lot of work!

#136952 - magicdanw - Mon Aug 06, 2007 8:27 pm

I like what I see so far! I see you started implementing libnds (or PAlib?) functions as Java methods, which is very cool. I'm curious how you did this, though. I see you import nds.Video, but I don't see any nds class. Did you compile the class into the VM? If so, maybe you could release the source code so I or other people could try to implement more functionality based on your initial example?

Once again, great work! Your ability to use makefiles is clearly superior to mine, since I was unable to even compile Java4gba in its original state!

#136956 - josath - Mon Aug 06, 2007 9:42 pm

As far as interfacing with the hardware, I'll probably try to make a very basic wrapper over libnds using KNI (the light version of JNI), basically lets you call C functions from Java. I looked at MIDP (standardized library for gfx/input/sound in Java on mobile devices) briefly, but it looked like way too much work to port to the DS.

These libraries will be compiled into the VM (like all the other standard libraries). The downside to this is that you need the whole devkitARM setup and have to recompile the VM in order to add / make changes to the built in classes.

As far as testing, you can simply run the DS KVM directly in an emulator on the PC (so no need to reimplement the native DS classes on the PC). It works quite well under iDeaS with the R4 DLDI emulation.

Here's my test nds/Video.java file:
Code:
package nds;

public class Video {
        public static native void lcdSwap();
}


And a corresponding Java_nds_Video.c:
Code:
#include <nds.h>
#include <nds/arm9/video.h>
#include <kni.h>
#include <stdio.h>

KNIEXPORT KNI_RETURNTYPE_VOID Java_nds_Video_lcdSwap() {
    lcdSwap(); // this function is defined in libnds
    KNI_ReturnVoid();
}


Right now I'm having trouble with the GC crashing when I do anything more complex than the above KNI, so I'll need to track that down before I can do any more.

My plan is to finish this basic wrapper, so people can at least load stuff into VRAM, read key input, and poke arbitrary hardware registers.

#136962 - magicdanw - Mon Aug 06, 2007 10:27 pm

I think a nice feature for now would be to make a wrapper for PALib's keyboard routines, so that people can make functional console programs. (anyone up for a good old text adventure? :D)

#136963 - josath - Mon Aug 06, 2007 10:29 pm

Java and PALib both suck too much, that combining them in a single app would cause some kind implosion and/or blackhole.

I dunno why I'm even doing this, it's not like Java really has any advantage over C++.

#136964 - magicdanw - Mon Aug 06, 2007 10:35 pm

Lol. The one advantage I can think of is that I know Java and I don't know C++. And that Java programs that already exist could be ported to the DS if a good JVM were written for the DS.

If you don't want to do this, I understand why. But could you post the source code and makefiles you use online? I'd like to mess around with this, but I can't get my copy of the Java4gba source to compile at all...

#137132 - lores - Wed Aug 08, 2007 9:23 am

Hey josath

i wanted to do exactly the same but i didn't have the time.
My plan is to port MIDP 2.0 to the DS, and possibly JSR-184 for 3D.
As for MIDP 2.0: Some stuff is unimportant to me, Forms for example are for whinies ;)

Why am i interested in doing that? Because i'm a mobile games developer, developing in Java. I want to create DS games without having to worry (too much) about the DS's internals. I am used to create games for very limited devices, so are many other mobile game developers. If i could take my game, make minor or no changes and play it on the DS - that would be awesome.

#137136 - melw - Wed Aug 08, 2007 9:53 am

lores wrote:
Why am i interested in doing that? Because i'm a mobile games developer, developing in Java. I want to create DS games without having to worry (too much) about the DS's internals. I am used to create games for very limited devices, so are many other mobile game developers. If i could take my game, make minor or no changes and play it on the DS - that would be awesome.


You know, C/C++ and Java aren't that different in the end... In your case it would be probably a lot easier and more efficient to write a wrapper or bunch of defines where C and Java differ and use the same code base for both mobile and DS builds. Especially if you forget all Java-specific features the "porting" can be surprisingly easy. String implementation gives some headache and array definitions can be annoying, but that's mostly it. :)

Of course I'm not stopping you from porting MIDP2 nor JSR-184, but that might get sluggish in the end even with huge amount of work poured into making that happen.

#137176 - josath - Wed Aug 08, 2007 4:12 pm

melw wrote:
Of course I'm not stopping you from porting MIDP2 nor JSR-184, but that might get sluggish in the end even with huge amount of work poured into making that happen.


Yes...MIDP2 itself is larger than the entire KVM, with much more platform-specific code, so it may be quite a task to port :)

#137183 - josath - Wed Aug 08, 2007 5:41 pm

An update: I've written a wrapper around parts of libnds, there's enough there to load files from the flashcart, create sprites, and read user input.

I included a couple of example java files as well, so you can test it without compiling anything:

http://blog.davr.org/2007/08/08/java-for-nds/

#137211 - dantheman - Wed Aug 08, 2007 9:24 pm

This looks really interesting to me. I'm about to start an AP Computer Science class for my senior year of high school, which will be my first real introduction to programming for any system. The class will be teaching Java, so it would be cool to be able to see the things I learn take form on the DS as well as on the computer. Of course I realize there will be differences to take into account, but it should still be fun to use.

Wishing you the best of luck josath.

Just out of curiosity, could a text viewer be feasible in its current status? You said it can now read files from a media card and always had the ability to print text, so would it be a simple matter to read a text file from the card and print its contents to the screen?

#137215 - josath - Wed Aug 08, 2007 9:31 pm

dantheman wrote:
Just out of curiosity, could a text viewer be feasible in its current status? You said it can now read files from a media card and always had the ability to print text, so would it be a simple matter to read a text file from the card and print its contents to the screen?


Yes, one of the included examples reads the first 20 bytes of 'test.txt' and displays it. Of course there are much better ds homebrew already written for reading text files.

#137741 - Inopia - Wed Aug 15, 2007 8:23 am

I think this is a very nice development. Java is my weapon of choice for anything non-embedded, and developing a solid VM has some uses.

First of all, if you can get MIDP to run, you can run Opera Mini. There's a an open source MIDP implementation on top of the standard Java libs called MicroEmulator (http://www.microemu.org). There's also a bunch of MIDP games out there you can toy around with.

Furthermore, a multithreading programming environment such as this makes is a lot easier to write simple networking programs.

Don't stop working on this, it could be a lot of fun:)[/url]

#138229 - lores - Tue Aug 21, 2007 2:03 pm

Hey josath, that's just awesome. Please continue your work on the libnds wrapper. With that wrapper you can create almost anything with java on the ds.

I would like to implement the MIDP lib on top of that to get my mobile stuff going on the DS.

Even if you're not interested in MIDP: Your wrapper will allow for easier homebrew development on the DS. Go go, josath!

#138230 - Lick - Tue Aug 21, 2007 2:54 pm

May I suggest melw's fileLocator for avoiding root-chaos?

Nice work by the way. I'm not much of a Java-guy but this project definitely shows its worth.
_________________
http://licklick.wordpress.com