Hi. Because of the lack of java support on my '51, I'm now attempting to learn C.
But it isn't going so great. C's complicated syntax is spanking me with compiler errors. I can see why there's courses for this language. It's not something you just start typing, and it works.
Anyway, I'm a noob when it comes to compiled languages. I have no idea what needs to be fixed. I figured since in C there's no garbage collector, it'd be a good idea to have a big list of all the surfaces loaded into memory, so I can keep track of them, and delete them through that object. Good concept anyway, right?
So I figured....arr[16] of pointers that points to arr[64] of pointers to surfaces. That combo allows up to 1024 surfaces, which is more than I could possibly use, while also keeping memory usage to ~(16*4 + n*64*4 + overhead) bytes.
Unfortunately, that concept maps very poorly into actual code, since my brain and fingers don't know how to get it working.
So anyways...Java:
public class imageCache { public int MaxChunks = 16; public int ChunkSize = 64; public int maxSize = MaxChunks*ChunkSize; public int Count = 0; public Chunk[] Chunks = new Chunk[MaxChunks]; final public boolean outOfBounds(int iNum) { return (iNum < 0 || iNum >= maxSize); } final public SDL_Surface get(int iOffset) { if(outOfBounds(iOffset)) return null; int iChunk = Count >> 6; int iIndex = Count & 63; if(Chunks[iChunk] == null) return null; return Chunks[iChunk].Arr[iIndex]; // May be null } final public int add(SDL_Surface mySurf) { if(Count >= maxSize) return -1; int iChunk = Count >> 6; int iIndex = Count & 63; if(Chunks[iChunk] == null) Chunks[iChunk] = new Chunk(ChunkSize); Chunks[iChunk].Arr[iIndex] = mySurf; iIndex = Count; Count++; return iIndex; // Return Index/Offset } private class Chunk { public SDL_Surface Arr[]; Chunk(int iSize) { Arr = new SDL_Surface[iSize]; } } }
If anyone has the time to convert that, I'd be grateful.
-Kramy
kramy java, you certainly don't need dynamic memory allocations for your code. You would be much better of with static variables, that are allocated during compilation. that saves you lots of overhead your 8 bitter cannot like and is generally discouraged even for 32 bitters (google: "memory fragmentation"). so, instead of
public Chunk[] Chunks = new Chunk[MaxChunks];
you would have
static Chunk s_chunks[MaxChunks];
's_chunks' will point to the beginning of your array. You didn't give the definition of 'SDL_Surface' so how can anybody help you? Either way, the best you can do is spend a couple of days learning C.
C's complicated syntax is spanking me with compiler errors.
C's syntax isn't that much more complex than Java's - probably even less since it does not have classes, etc.
Anyway, I'm a noob when it comes to compiled languages.
Java is a compiled language. It's just not compiled to run on a particular processor, but rather a defined virtual machine.
I figured since in C there's no garbage collector, it'd be a good idea to have a big list of all the surfaces loaded into memory, so I can keep track of them, and delete them through that object. Good concept anyway, right?
What object ? The C compiler does not know about objects. Programs in C usually aren't object-oriented (unless the programmer jumps through quite a few hoops).
A standard '51 has 256 bytes of RAM ! Even if you set n to 1, your construct will not fit in there.
And what is a surface ?
In your code, I see references to SDL_something - as far as I am aware, the SDL library is used to program stuff on PCs, not on microcontrollers.
Here's what you need to do:
1. Get a good textbook on C, read it, understand it, try out the examples. 2. Get some literature (including the datasheets) on the '51. Familiarize yourself with it, and especially throw most notions of "PC programming" overboard. 3. Analyze the Java code and find out what it does. Write C code that does the same thing.
If this is too much work, there's an easier alternative:
1. Hire (and pay) a consultant to do the job for you.
Surfaces - are you trying to do something graphical with the '51 processor?
By the way, Java has taken quite a lot of it's grammar from C++, and C++ was originally a strict superset for C. So the grammar for C shouldn't be too strange.
Just remember that C can have real compound objects without references to the sub-objects.
I see some SDL_... things in there ... so that might mean he's trying to use the Simple DirectMedia Layer, on a '51.
"C's complicated syntax..."
The syntax of 'C' is not complicated.
Mastering how to use the language is, of course, an entirely different matter!
"I can see why there's courses for this language"
So take one!
"It's not something you just start typing..."
Certainly it isn't - so don't try to do that!
Porting from one language to another requires a sound understanding of both languages!
Programming embedded systems - especially small embedded systems based on the 8051 or similar - also requires a sound understanding of the underlying platform.
See the 'Books' page for both general programming books, and books specifically about the 8051: http://www.keil.com/books/
See also: www.8052.com/books.phtml
For details of the 8051 architecture, you will need to study the so-called "bible" for the 8051:
Chapter 1 - 80C51 Family Architecture: www.nxp.com/.../80C51_FAM_ARCH_1.pdf
Chapter 2 - 80C51 Family Programmer's Guide and Instruction Set: www.nxp.com/.../80C51_FAM_PROG_GUIDE_1.pdf
Chapter 3 - 80C51 Family Hardware Description: www.nxp.com/.../80C51_FAM_HARDWARE_1.pdf
And, of course, the Data Sheet(s) for your particular processor, platform, and any other peripherals you are using.
If you can't (or won't) do this yourself, try: http://www.keil.com/condb