flatmush / dingoo-sdk

Automatically exported from code.google.com/p/dingoo-sdk
2 stars 3 forks source link

please add code for C++ coding with SDK #46

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi, SDK developers!

We use this SDK in our ZX-Spectrum emulator Unreal Speccy Portable.
But it lacks basic C++ functionality.

So, i slightly modify it for our needs and C++ code works.
Can you include this modifications?

1. wchar_t type is built-in in GCC (i get errors), so i comment line with 
typedef in stddef.h.
2. NULL define in C++ must be simple 0, not (void*)0, so, stddef.h looks like 
this in NULL definition:
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif

3. linker script dingoo.xn must contain definitions for 
constructors/destructors for static C++ vars, so, i added some code:

here:
    .dingoo 0x80A00000 :
    {
        dingoo.o (.text)
        *(.text*)
        *(.rodata*)
        *(.data*)
        *(.eh_frame*)
        *(.gcc_except_table*)

        . = ALIGN(8);
        __CTOR_LIST__ = ABSOLUTE(.);
        KEEP(*(SORT(.ctors*)))
        __CTOR_END__ = ABSOLUTE(.);
        __DTOR_LIST__ = ABSOLUTE(.);
        KEEP(*(SORT(.dtors*)))
        __DTOR_END__ = ABSOLUTE(.);

        *(.lit8)
        *(.lit4)
        *(.sdata)
    }

and here (because .bss section discarded):

    /DISCARD/ :
    {
    *(.reginfo)
    }

4. we need to call ctors/dtors in startup/cleanup code (now implemented in our 
code, but can be included in SDK? (entry_point.c or similar?))

typedef void (*_PVFV)();
extern _PVFV __CTOR_LIST__[];
extern _PVFV __CTOR_END__[];
extern _PVFV __DTOR_LIST__[];
extern _PVFV __DTOR_END__[];

static void CrtCallList(const _PVFV* beg, const _PVFV* end)
{
    for(; beg < end; ++beg)
    {
        if(*beg) (**beg)();
    }
}
static void CrtInit()
{
    CrtCallList(__CTOR_LIST__, __CTOR_END__); //global constructors call
}
static void CrtDone()
{
    CrtCallList(__DTOR_LIST__, __DTOR_END__); //global destructors call
}

5. we need operators new/delete and some other stuff.
This may be implemented so (now in our code, but can be included in SDK?)

void* operator new(size_t size) { return malloc(size); }
void* operator new[](size_t size) { return malloc(size); }
void operator delete(void* p) { free(p); }
void operator delete[](void* p) { free(p); }
extern "C" void __cxa_pure_virtual() {}

After all this modifications emulator works with SDK!
Thanks for attention, djdron.

Original issue reported on code.google.com by djd...@gmail.com on 11 Jul 2010 at 11:27

GoogleCodeExporter commented 9 years ago
Hi

We're definitely interested in C++ support. Would you like commit access? That 
way you can add these modifications yourself.

If you're interested in more C++ support, Spiller was working on it for his 
"dingkit", which is built on flatmush original toolchain. I don't know in what 
condition it is however. 
http://boards.dingoonity.org/dingoo-development/giving-up-and-back/

Original comment by hart...@gmail.com on 12 Jul 2010 at 9:05

GoogleCodeExporter commented 9 years ago
Hi, harteex!

Ok, i like commit access :-)
I will add zlib & tinyxml (C libs) in emulator, so i will test C and C++ code 
mix.

PS .bss section must include *(.bss*) sections from objs (i found a bug with 
uninitialized static vars (C++ code in namespaces and inside functions)).

Original comment by djd...@gmail.com on 12 Jul 2010 at 10:28

GoogleCodeExporter commented 9 years ago
Alright, you now have commit access.

The more testing, the better ;)

Original comment by hart...@gmail.com on 13 Jul 2010 at 4:50

GoogleCodeExporter commented 9 years ago
I'll assign this issue to you now. BTW, thanks for the commits!

Original comment by hart...@gmail.com on 17 Jul 2010 at 9:32

GoogleCodeExporter commented 9 years ago
Hello! :)
I added stlport library to dingoo-sdk/include/stlport (without streams).
It is not required additional library build, only include headers.
Commit this?

this code works:

----------------
#include <string>
#include <map>
#include <vector>

void* g_pGameDecodeBuf = NULL;

int main(int argc, char**argv)
{
    using namespace std;
    string path = argv[0];
    string::size_type p = path.rfind('/');
    if(p == string::npos)
        p = path.rfind('\\');
    if(p == string::npos)
        return -1;
    path = path.substr(0, p + 1);
    path += "stl_test.log";

    map<string, string> string_map;
    string_map["key_petya"] = "Petya is a bad guy!";
    string_map["key_vasya"] = "Vasily is a good boy ;-)";

    vector<string> string_vec;
    string_vec.push_back("vector test failed");
    string_vec.push_back("vector test unknown");
    string_vec.push_back("vector test passed");

    FILE* f = fopen(path.c_str(), "w");
    string p1 = string("Is stlport") + " works?";
    fputs(p1.c_str(), f);
    fputs("\n", f);
    fputs(string_map["key_vasya"].c_str(), f);
    fputs("\n", f);
    for(vector<string>::const_iterator it = string_vec.begin(); it != string_vec.end(); ++it)
    {
        fputs(it->c_str(), f);
        fputs("\n", f);
    }
    fclose(f);
    return 0;
}

Original comment by djd...@gmail.com on 1 Aug 2010 at 9:43

GoogleCodeExporter commented 9 years ago
How does it work then, with only include files? Is it using the libs that came 
with the toolchain?

In the long run it's better to port libs to the dingoo, but feel free to commit 
this in the meantime.

Original comment by hart...@gmail.com on 2 Aug 2010 at 5:26

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
It has mode _STLP_NO_IOSTREAMS, then only headers used, without building 
library.
It used c-libs from this sdk.
I can post patch, you can test it.

Original comment by djd...@gmail.com on 2 Aug 2010 at 7:58

Attachments:

GoogleCodeExporter commented 9 years ago
after patching SDK, ./install it.
then in makefile use
LIBS      = -lc -ljz4740 -lgcc -lstdc++
INCLUDE   = -I$(DINGOO_SDK)/include -I$(DINGOO_SDK)/include/stlport 
-I$(MIPSTOOLS)/mipsel-linux/include -I. -I$
STD_OPTS  = -G0 -O3 $(INCLUDE) -Wall -finline-functions -fomit-frame-pointer 
-msoft-float -fno-builtin -fno-exceptions -mips32 -mno-abicalls -fno-pic -c 
-DMPU_JZ4740 -DNDEBUG -D_DINGOO -DNO_PREFETCH
CPP_OPTS  = $(STD_OPTS) -fno-rtti -fno-threadsafe-statics

Original comment by djd...@gmail.com on 2 Aug 2010 at 8:02

GoogleCodeExporter commented 9 years ago
Ah ok, I see.
I don't have time to play around with it now, but it sounds good. Feel free to 
commit.

Original comment by hart...@gmail.com on 3 Aug 2010 at 8:33

GoogleCodeExporter commented 9 years ago
i did it! :)

Original comment by djd...@gmail.com on 3 Aug 2010 at 10:46

GoogleCodeExporter commented 9 years ago
Great :)

Btw, if you have time, I noticed virtual members doesn't work.

undefined reference to `vtable for __cxxabiv1::__class_type_info'
undefined reference to `vtable for __cxxabiv1::__si_class_type_info'

I used the virtual members example from this page (without the streams of 
course):
http://www.cplusplus.com/doc/tutorial/polymorphism/

Original comment by hart...@gmail.com on 4 Aug 2010 at 6:58

GoogleCodeExporter commented 9 years ago
Hello, harteex!
It is because of runtime type info (RTTI) is enabled by default.
I see you don't use dynamic_cast, typeid, etc.. in this examples.

Try to compile with this options:
STD_OPTS  = -G0 -O3 $(INCLUDE) -Wall -finline-functions -fomit-frame-pointer 
-msoft-float -fno-builtin -fno-exceptions -mips32 -mno-abicalls -fno-pic -c 
-DMPU_JZ4740 -DNDEBUG -D_DINGOO
CPP_OPTS  = $(STD_OPTS) -fno-rtti -fno-threadsafe-statics

Original comment by djd...@gmail.com on 4 Aug 2010 at 7:16

GoogleCodeExporter commented 9 years ago
Now only basic C++ functionality was added:
1. new/delete operators.
2. static constructors/destructors call.
3. stub for pure virtual function call.

More advanced unimplemented features:
RTTI, exceptions. I dont know how to implement this now.

If you want you can look at our "unreal speccy portable" sources, we used 
virtual functions, and they works fine :)

Original comment by djd...@gmail.com on 4 Aug 2010 at 7:33

GoogleCodeExporter commented 9 years ago
Seems to compile OK now, thanks. I'll play around with it a bit more later.

Original comment by hart...@gmail.com on 4 Aug 2010 at 7:55

GoogleCodeExporter commented 9 years ago

Original comment by Flatmush@googlemail.com on 12 Feb 2011 at 12:33