Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  signed/unsigned  (Read 1144 times)
0 Members and 1 Guest are viewing this topic.
Offline erikd

JGO Ninja

Medals: 15
Projects: 4
Exp: 14 years


« Posted 2004-08-02 13:45:23 »

Hi there,

I've finally started with creating an M68000 emulator. This is a 32bit processor in many aspects and I want to use integeres for the registers (which are 32 bit).
Now here's the trouble; we don't have any unsigned datatypes in java (except for char) and Sun says it's not necessary to have them. (which sucks btw)
So, say the PC points to memory address 0xffffffff (memory is an array), how would I do that?

Obviously, doing something like
byte fetched = memory[PC];

would get me an ArrayOutOfBoundsException since PC is 0xffffffff, which is -1.
I don't want to revert to long because I feel that should not be necessary. Any thoughts?

Offline jbanes

JGO Coder

Projects: 1

"Java Games? Incredible! Mr. Incredible, that is!"

« Reply #1 - Posted 2004-08-02 14:20:02 »

Just thinking out loud here...

byte[] lowermem = new byte[0x7FFFFFFF];
byte[] highmem = new byte[0x7FFFFFFF];


if((memloc & 0x80000000) > 0) highmem[(memloc & 0x7FFFFFFF)] = databyte;
else lowermem[memloc] = memloc;

I *think* that'll do it. Obviously you wouldn't want to allocate 4 gigs of memory, so replace that with what mem sizes you're actually using. Smiley

Java Game Console Project
Last Journal Entry: 12/17/04
Offline erikd

JGO Ninja

Medals: 15
Projects: 4
Exp: 14 years


« Reply #2 - Posted 2004-08-02 15:22:17 »

*think* that'll do it. Obviously you wouldn't want to allocate 4 gigs of memory, so replace that with what mem sizes you're actually using.

Thanks, although that was the kind of thing I wanted to avoid  Smiley
It's all hypothetical anyway for now since the 68K addressing space is 24bit, but maybe later I will have a problem.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Raghar

Junior Member

Ue ni taete 'ru hitomi ni kono mi wa dou utsuru

« Reply #3 - Posted 2004-08-05 18:35:04 »

Actually it's unnecessary to have them. You just need a unsigned multiplication/division and some unsigned comparissons.
Java lacks them as well.
int[] someArray;

read(int c1){
     int temp = someArray[c1>>>2];
     return (byte) ((temp>>>((c1%4)*Cool)&0xff);
Would it work? Who knows. Would it be faster than:
Offline swpalmer

JGO Coder

Where's the Kaboom?

« Reply #4 - Posted 2004-08-06 00:58:03 »

You will never REALLY have a 4gig array will you?

And how about the more general problem of dealing with memory at discontinuous addresses.  E.g. one ROM for sound data at 0xF0000000, one for graphics data at 0xE0000000, and the code at 0x10000000 just for kicks?

Also imagine emulating a Windows machine, where all protected kernel addresses are >0x80000000, even though you may only have 256MB of RAM.

I don't think there is any getting around the fact that you need an address translation function, and that solves the original problem.

Offline erikd

JGO Ninja

Medals: 15
Projects: 4
Exp: 14 years


« Reply #5 - Posted 2004-08-10 13:53:17 »

You're right and this is just a bad example of my issues with java types.
The original problem is not addressing that much memory, the original problem is the lack of unsigned types and generally all inconveniences dealing with byte and bits in java.

When dealing with bits and bytes, you are forced to continuously cast back and forth between byte and int (with all performance implications), which makes java IMO a bad choice for this kind of thing.

With emulating 8bit machines, the example makes perfect sense though.
I have 3 options for the PC (Program Counter) or other 16 bit registers:
* Use an int. I have to keep masking them by doing an &0xffff to prevent them from going out of scope. Yuck.
* Use short. I can't use it anymore for addressing since it can become negative for high memory.
* Use char. Possible (= the only unsigned type in java), but if I do one logical operation it gets cast to an int and I have to cast it back again. Yuck.

Offline swpalmer

JGO Coder

Where's the Kaboom?

« Reply #6 - Posted 2004-08-10 21:45:32 »

You are correct that Java is not suited to bit twiddling.  But one would hope that after JIT compiling the casts cost nothing because the compiler can see that simply using an instruction that processes the data as unsigned gives the correct result.

No argument that it is a significant pain when you need to write the Java code though.

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.


Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (42 views)
2014-04-15 18:08:23

BurntPizza (38 views)
2014-04-15 03:46:01

UprightPath (54 views)
2014-04-14 17:39:50

UprightPath (36 views)
2014-04-14 17:35:47

Porlus (53 views)
2014-04-14 15:48:38

tom_mai78101 (75 views)
2014-04-10 04:04:31

BurntPizza (134 views)
2014-04-08 23:06:04

tom_mai78101 (234 views)
2014-04-05 13:34:39

trollwarrior1 (195 views)
2014-04-04 12:06:45

CJLetsGame (203 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!