Hi,

i have attached the corresponding source code. Perhaps there is the solution for your string problem included in the c file.

Mfg Markus

Am 22.07.10 15:56, schrieb Yann GUIDON:
Hi,
Hi !

i am using ghdl in a little different way than most others.
Or at least, that's what you suppose :-)

I have
linked ghdl and c code, I start my c application and it starts ghdl. In
my c application, I start a java virtual machine. I can call c methods
>from ghdl code and i can call java methods from c without any problems.
Have you tried compiling the Java code with GCC ?

But when I call the c methods that previously called java without any
problems from ghdl, I always get memory errors when they try to access
the java methods:
   * Initializing the jvm before calling the ghdl main method leads to:
     NULL access dereferenced
   * Calling the ghdl main method and initializing the jvm lazy causes
     a segmentation fault
I'm having difficulty getting around GHDL's bound checks as well.
Mixing C and GHDL is far from obvious and i'm currently
fighting the system concerning the strings :-D
Maybe mixing 3 langages is too much...
Remeber that GHDL is already written in Ada :-)

Has  ghdl some restrictions using memory?
certainly, I'm more and more thinking about looking
at the source code.

Can anyone confirm these
problems? Are there any options to solve these problems or other ideas
how to deal with it?
show us some typical source code first :-)

Greetings Markus
_o/ good luck !

yg



_______________________________________________
Ghdl-discuss mailing list
[email protected]
https://mail.gna.org/listinfo/ghdl-discuss


#include <jni.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
 #define PATH_SEPARATOR ':'
 #define USER_CLASSPATH "../cbdes/bin/"
 #define JINTERFACE_CLASSNAME "de/uniwue/info6/cbdes/ghdlHooks/GHDLCompHook"



struct int_bounds
{
  int left;
  int right;
  char dir;
  unsigned int len;
};

struct ghdl_string
{
  char *base;
  struct int_bounds *bounds;
};

int counter=0;

JNIEnv *env;
JavaVM *jvm;
jclass clazz_GHDLCompHook;
jmethodID mid_getIntValueFrom;
int init = 0;

static int getEnv() {
        if (init == 0) {
                printf("getEnv->createVM");
                create_vm();
                init = 1;
        }
        if(!jvm)exit(1);
        int res = 0;
        res = (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
        //int res = (*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2);
        if(!env)destroy();
        if(*env==NULL)destroy();
        counter++;
        printf("getEnv Counter: %i\n",counter);
        if (res < 0) {
                fprintf(stderr, "Attach failed\n");
                return res;
        }

        return 0;
}

jclass getJavaClass(JNIEnv *jenv, char *name) {
        printf("getJavaClass\n");
        jclass cls;
        if(*jenv==NULL)destroy();
        cls = (*jenv)->FindClass(jenv, name);
        if (cls == NULL) {
             destroy();
        }
        printf("Accessing Java Class %s\n", name);
        return cls;
}







create_vm() {

     jint res;
     jclass cls;
     jmethodID mid;
     jstring jstr;
     jclass stringClass;
     jobjectArray args;

 #ifdef JNI_VERSION_1_2
     //JNIEnv *env;
     JavaVMInitArgs vm_args;
     JavaVMOption options[2];
     options[0].optionString =
         "-Djava.class.path=" USER_CLASSPATH;
     options[1].optionString = "-Djava.compiler=NONE";

     vm_args.version = 0x00010002;
     vm_args.options = options;
     vm_args.nOptions = 2;
     vm_args.ignoreUnrecognized = JNI_TRUE;
     /* Create the Java VM */
     res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
     //(*jvm)->DetachCurrentThread(jvm);
 #else
     JDK1_1InitArgs vm_args;
     char classpath[1024];
     vm_args.version = 0x00010001;
     JNI_GetDefaultJavaVMInitArgs(&vm_args);
     /* Append USER_CLASSPATH to the default system class path */
     sprintf(classpath, "%s%c%s",
             vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
     vm_args.classpath = classpath;
     /* Create the Java VM */
     res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
 #endif /* JNI_VERSION_1_2 */
     clazz_GHDLCompHook = getJavaClass(env, JINTERFACE_CLASSNAME);
     mid_getIntValueFrom = (*env)->GetStaticMethodID(env, clazz_GHDLCompHook, 
"getIntValueFrom",
                                             "(Ljava/lang/String;)I");
     if (res < 0) {
         fprintf(stderr, "Can't create Java VM\n");
         exit(1);
     }
     //(*jvm)->DetachCurrentThread(jvm);

     /*
     cls = getJavaClass("Prog");

     mid = (*env)->GetStaticMethodID(env, cls, "main",
                                     "([Ljava/lang/String;)V");
     if (mid == NULL) {
         destroy();
     }
     jstr = (*env)->NewStringUTF(env, " from C!");
     if (jstr == NULL) {
         destroy();
     }
     stringClass = (*env)->FindClass(env, "java/lang/String");
     args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
     if (args == NULL) {
         destroy();
     }
     (*env)->CallStaticVoidMethod(env, cls, mid, args);


    if ((*env)->ExceptionOccurred(env)) {
         (*env)->ExceptionDescribe(env);
     }
     (*jvm)->DestroyJavaVM(jvm);*/
 }



destroy() {

        printf("Destroying JVM...\n");
        if(env && *env) {
                if ((*env)->ExceptionOccurred(env)) {
                        (*env)->ExceptionDescribe(env);
             }
        }
        (*jvm)->DestroyJavaVM(jvm);
}

writeToBlackboard(struct ghdl_string *id, int value) {
        printf("writeToBlackboard\n");
        getEnv();
        if(*env==NULL)destroy();
        jclass clazz = getJavaClass(env, JINTERFACE_CLASSNAME);
        jstring jstr = (*env)->NewStringUTF(env, id->base);
        jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "registerInt",
                                             "(Ljava/lang/String;I)V");
        jint v = (jint) value;
        (*env)->CallStaticVoidMethod(env, clazz, mid, jstr, v);
        //(*jvm)->DetachCurrentThread(jvm);
}

int readIntFromBlackboard(struct ghdl_string *id) {
        printf("len: %d, left:%d, buffer:%s\n", id->bounds->len, 
id->bounds->left, id->base);
        char mystring[100];
        int i = 0;
        for (i = 0; id->base[i+id->bounds->left-1] && i<id->bounds->len && 
i<99; i++) mystring[i]=id->base[i+id->bounds->left-1];
        mystring[i]=0;
        printf("readIntFromBlackboard\n");
        getEnv();
        printf("after getEnv");
        if(*env==NULL) {
                printf("VM destroyed\n");
                destroy();
                return -1;
        }
        //jclass clazz = getJavaClass(env, JINTERFACE_CLASSNAME);
        jstring jstr = (*env)->NewStringUTF(env, mystring);

        printf("Stringinhalt: %s\n", mystring);
        jthrowable exc = (*env)->ExceptionOccurred(env);
        printf("Exception: %d\n", (void*)exc);
        printf("String: %d\n", (void*)jstr);
        printf("int read-----\n");
        printf("Thread: %d\n", pthread_self());
        printf("%d\n",(*env));
        printf("%d\n", clazz_GHDLCompHook);
        //jmethodID mid = (*env)->GetStaticMethodID(env, clazz, 
"getIntValueFrom",
          //                                   "(Ljava/lang/String;)I");
        int v = (*env)->CallStaticIntMethod(env, clazz_GHDLCompHook, 
mid_getIntValueFrom, jstr);
        printf("____after method call\n");
        //(*jvm)->DetachCurrentThread(jvm);
        return (int) v;
}

int readFromBlackboard(struct ghdl_string *id) {
        return readIntFromBlackboard(id);
}

//hitherto unused..
writeToBlackboardString(struct ghdl_string *id, struct ghdl_string *value) {
        getEnv();
        if(*env==NULL)destroy();
        jclass clazz = getJavaClass(env, JINTERFACE_CLASSNAME);
        jstring jstr = (*env)->NewStringUTF(env, id->base);
        jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "registerString",
                                             
"(Ljava/lang/String;Ljava/lang/String;)V");
        jstring v = (*value).base;
        (*env)->CallStaticVoidMethod(env, clazz, mid, jstr, v);
        //(*jvm)->DetachCurrentThread(jvm);
}

//hitherto unused...
jstring readStringFromBlackboard(struct ghdl_string *id) {
                getEnv();
                if(*env==NULL)destroy();
                jclass clazz = getJavaClass(env, JINTERFACE_CLASSNAME);
                jstring jstr = (*env)->NewStringUTF(env, id->base);
                jmethodID mid = (*env)->GetStaticMethodID(env, clazz, 
"getStringValueFrom",
                                                     
"(Ljava/lang/String;)Ljava/lang/String;");
                jstring v = (*env)->CallStaticIntMethod(env, clazz, mid, jstr);
                //(*jvm)->DetachCurrentThread(jvm);
                return v;
}

int refreshTime(int time) {
        printf("refreshTime\n");
        getEnv();
        printf("Zeit (fromC): %i\n", time);
        //if(!env) create_vm();
        //printf("VM created \n");
        //(*jvm)->
(jvm, (void**)&env, NULL);
        //printf("Thread attached \n");
        if(*env==NULL)destroy();
        jclass clazz = getJavaClass(env, JINTERFACE_CLASSNAME);
        jmethodID mid = (*env)->GetStaticMethodID(env, clazz, 
"advanceTimeFromGHDL",
                                                     "()V");
        if (mid == NULL) {
                 destroy();
             }
        (*env)->CallStaticVoidMethod(env, clazz, mid);
        //(*jvm)->DetachCurrentThread(jvm);
        return 0;
}

extern int ghdl_main (int argc, char **argv, char **envp);



int main(int argc, char **argv, char **envp) {
//      struct ghdl_string test;
//      struct int_bounds bounds;
//      test.bounds=&bounds;
//      //create_vm();
//      test.base="test.test1";
//      test.bounds->left=1;
//      test.bounds->len=strlen(test.base);
//      refreshTime(0);
//      //(*jvm)->DetachCurrentThread(jvm);
//      writeToBlackboard(&test,23);
//      //(*jvm)->DetachCurrentThread(jvm);
//      refreshTime(1);
//      //(*jvm)->DetachCurrentThread(jvm);
//      int v = readFromBlackboard(&test);
//      printf("Value of %s : %i\n", test.base, v);
//      //(*jvm)->DetachCurrentThread(jvm);
//      struct ghdl_string test2;
//      test2.bounds=&bounds;
//      test2.base="dieseIdistnichtvorhanden";
//      test2.bounds->left=1;
//      test2.bounds->len=strlen(test2.base);
//      int v2 = readFromBlackboard(&test2);
//      printf("Value of %s : %i\n", test2.base, v2);
//      struct ghdl_string test3;
//      test3.bounds=&bounds;
//      test3.base="Schalter1";
//      test3.bounds->left=1;
//      test3.bounds->len=strlen(test3.base);
//      int v3 = readFromBlackboard(&test3);
//      printf("Value of %s : %i\n", test3.base, v3);
        //(*jvm)->DetachCurrentThread(jvm);
        //(*jvm)->DetachCurrentThread(jvm);
        getEnv();
        int res = ghdl_main (argc, argv, envp);
        //int res = 0;
        destroy();
        return res;
}

GHDL=ghdl
CC=gcc -g
RM=rm

all: lampswitch
#all: CJavaForGHDL

lampswitch: CJavaForGHDL.o random.o
        $(GHDL) -e -Wl,CJavaForGHDL.o -Wl,-lm -Wl,-I/usr/lib/jvm/jdk/include 
-Wl,-I/usr/lib/jvm/jdk/include/linux 
-Wl,-L/usr/lib/jvm/jdk/jre/lib/i386/server/ -Wl,-L./ -Wl,-ljvm -Wl,-lpthread 
lampswitch

random.o: random.vhdl
        $(GHDL) -a $<
        
CJavaForGHDL.o: CJavaForGHDL.c
        $(CC) -c CJavaForGHDL.c -I/usr/lib/jvm/jdk/include 
-I/usr/lib/jvm/jdk/include/linux -L/usr/lib/jvm/jdk/jre/lib/i386/server/ -ljvm 
-lpthread -lc

#CJavaForGHDL:
#       $(CC) CJavaForGHDL.c -o CJavaForGHDL -I/usr/lib/jvm/java-6-sun/include 
-I/usr/lib/jvm/java-6-sun/include/linux 
-L/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/i386/server/ -ljvm -lpthread -lc

#CJavaForGHDL:
#       $(CC) CJavaGHDL2.c -o CJavaGHDL2 -I/usr/lib/jvm/java-6-sun/include 
-I/usr/lib/jvm/java-6-sun/include/linux 
-L/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/i386/server/ -ljvm -lpthread -lc

clean:
        $(RM) lampswitch CJavaForGHDL.o CJavaForGHDL CJavaGHDL2.o CJavaGHDL2
        $(RM) *.o
package blackboard is
        function readBB (id: string) return integer;
        attribute foreign of readBB : function is "VHPIDIRECT 
readFromBlackboard";
        function writeToBB (id: string; value: integer) return integer;
        attribute foreign of writeToBB : function is "VHPIDIRECT 
writeToBlackboard";
        function refreshTime(mytime : integer) return integer;
        attribute foreign of refreshTime : function is "VHPIDIRECT refreshTime";
end blackboard;

package body blackboard is
        function readBB (id: string) return integer is
        begin
                assert false severity failure;
        end readBB;
        function writeToBB (id: string; value: integer) return integer is
        begin
                assert false severity failure;
        end writeToBB;
        function refreshTime (mytime: integer) return integer is
        begin
                assert false severity failure;
        end refreshTime;
end blackboard;

use work.blackboard.all;
use std.textio.all;
ENTITY switch IS
        Port (lamp : Out    integer; ID : in string);
END switch ;


architecture BEHAVIORAL of switch is
        signal tempswitch: integer;
begin
        lamp <= tempswitch;
  process
        variable l : line;
  begin
        wait for 1 ms;
        write (l, tempswitch);
        writeline (output, l);  
        tempswitch <= readBB(ID);
  end process;

end BEHAVIORAL;

use work.blackboard.all;
ENTITY lamp IS
        Port (SWITCH : In    integer; ID: In string);
END lamp ;

architecture BEHAVIORAL of lamp is
begin
  process
        variable tempswitch: integer;
  begin
        wait for 1 ms;
        tempswitch := writeToBB(ID, SWITCH);
  end process;

end BEHAVIORAL;

use work.blackboard.all;
ENTITY clock IS
END clock ;

architecture BEHAVIORAL of clock is
        signal mytime: integer :=0;
begin
        mytime <= mytime + 1 after 1 ms;
        process
        variable temp: integer;
        begin
                wait for 1 ms;
                temp := refreshTime(mytime);
        end process;
end BEHAVIORAL;

use work.blackboard.all;
entity lampswitch is
end lampswitch;

architecture test of lampswitch is


        component switch 
                Port (lamp : Out    integer; ID : in string);
        END component;
        
        component lamp
                Port (SWITCH : In    integer; ID : In string);
        END component;

        component clock
        END component;

        signal wire: integer := 1;
        signal wire2: integer := 1;
  
begin

        schalter1: switch port map (lamp => wire, ID => "Schalter1");
        lampe1: lamp port map (SWITCH => wire, ID => "Lampe1");
        schalter2: switch port map (lamp => wire2, ID => "Schalter2");
        lampe2: lamp port map (SWITCH => wire2, ID => "Lampe2");
        lampe3: lamp port map (SWITCH => wire2, ID => "Lampe3");
        uhr: clock;

  process
  begin
        wait;
  end process;
    
end test;

_______________________________________________
Ghdl-discuss mailing list
[email protected]
https://mail.gna.org/listinfo/ghdl-discuss

Reply via email to