OSDN Git Service

signal and pthread ok
authorastoria-d <astoria-d@mail.goo.ne.jp>
Tue, 12 Feb 2013 07:09:59 +0000 (16:09 +0900)
committerastoria-d <astoria-d@mail.goo.ne.jp>
Tue, 12 Feb 2013 07:09:59 +0000 (16:09 +0900)
emulator/Makefile
emulator/bus.c
emulator/clock.c
emulator/clock.h
emulator/cpu.c [new file with mode: 0644]
emulator/emu-main.c

index 712e794..c94f30d 100644 (file)
@@ -1,10 +1,10 @@
 
 BIN=motonesemu
 
-OBJS=emu-main.o clock.o tools.o bus.o \
+OBJS=emu-main.o clock.o tools.o bus.o cpu.o \
 
 
-#LIBS=-L../libs -lmotones
+LIBS=-L../libs -pthread
 
 INCLUDE=-I../include
 
index 2cbca97..b033663 100644 (file)
@@ -1,4 +1,13 @@
+#include "tools.h"
 
-static char addr_bus [16];
-static char data_bus [8];
+
+static unsigned short addr_bus [16];
+static unsigned char data_bus [8];
+
+int init_bus(void) {
+    return TRUE;
+}
+
+void clean_bus(void){
+}
 
index 520a076..7105d29 100644 (file)
@@ -1,36 +1,84 @@
 
+#include <stdlib.h>
 #include <time.h>
+#include <pthread.h>
+
 #include "tools.h"
 #include "clock.h"
 
 static int exit_loop;
+static pthread_t clock_thread_id;
 
 struct clock_handler {
     struct slist l;
-    clock_handler_t *handler;
+    clock_func_t *handler;
 };
 
 static struct clock_handler *handler_list;
 
-void clock_loop(void) {
+#define DEB_SLOW
+
+static void *clock_loop(void* arg) {
     struct timespec ts;
     struct clock_handler *ch;
 
+#ifdef DEB_SLOW
+    ts.tv_sec = 1;
+#else
     ts.tv_sec = 0;
+#endif
     ts.tv_nsec = 1;
 
     while (!exit_loop) {
+        //dprint("loop...\n");
         ch = handler_list;
         while (ch != NULL) {
             if (!ch->handler())
                 break;
-            ch = (struct slist*)ch->l.next;
+            ch = (struct clock_handler*)ch->l.next;
         }
         nanosleep(&ts, NULL);
     }
+
+    return NULL;
+}
+
+int start_clock(void) {
+    int ret;
+    pthread_attr_t attr;
+
+    ret = pthread_attr_init(&attr);
+    if (ret != RT_OK)
+        return FALSE;
+
+    ret = pthread_create(&clock_thread_id, &attr, clock_loop, NULL);
+    return ret == TRUE;
+}
+
+static void end_loop(void) {
+    void* ret;
+    exit_loop = TRUE;
+
+    //join the running thread.
+    pthread_join(clock_thread_id, &ret);
+
+    dprint("clock thread joined.\n");
+
 }
 
-int register_clock_hander(clock_handler_t *handler) {
+int register_clock_hander(clock_func_t *handler) {
+    struct clock_handler *ch;
+
+    ch = malloc(sizeof(struct clock_handler));
+    ch->l.next = NULL;
+    ch->handler = handler;
+
+    if (handler_list == NULL) {
+        handler_list = ch;
+    }
+    else {
+        slist_add_tail(&handler_list->l, &ch->l);
+    }
     return TRUE;
 }
 
@@ -40,3 +88,17 @@ int init_clock(void) {
     return TRUE;
 }
 
+void clean_clock(void) {
+    struct clock_handler *ch = handler_list;
+
+    end_loop();
+
+    while (ch != NULL) {
+        struct clock_handler *pp = ch;
+        ch = (struct clock_handler*)ch->l.next;
+        free(pp);
+    }
+    dprint("clean_clock done.\n");
+    
+
+}
index d416610..732d4e2 100644 (file)
@@ -1,12 +1,13 @@
 #ifndef __clock_h__
 #define __clock_h__
 
-typedef int (clock_handler_t) (void);
+typedef int (clock_func_t) (void);
 
-void clock_loop(void);
 int init_clock(void);
+void clean_clock(void);
 
-int register_clock_hander(clock_handler_t *handler);
+int register_clock_hander(clock_func_t *handler);
+int start_clock(void);
 
 #endif /*__clock_h__*/
 
diff --git a/emulator/cpu.c b/emulator/cpu.c
new file mode 100644 (file)
index 0000000..35df0f8
--- /dev/null
@@ -0,0 +1,35 @@
+#include "tools.h"
+#include "clock.h"
+
+
+/*
+ * clock handler.
+ * */
+int clock_cpu(void) {
+    dprint("clock cpu...\n");
+
+    return TRUE;
+}
+
+void reset_cpu(void) {
+}
+
+static void fetch_inst(void) {
+}
+
+static void decode_inst(void) {
+}
+
+static void execute_inst(void) {
+}
+
+int init_cpu(void) {
+    int ret;
+    ret = register_clock_hander(clock_cpu);
+    if (!ret) {
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
index 6f6647f..4e2e3f2 100644 (file)
@@ -1,16 +1,67 @@
 #include <stdio.h>
+#include <unistd.h>
+#include <signal.h>
+#include <string.h>
+
 #include "tools.h"
 #include "clock.h"
 
+int init_cpu(void);
+int init_bus(void);
+void clean_bus(void);
+void reset_cpu(void);
+
+static int clock_done;
+
 static int init_datas(void) {
     int ret;
 
+    clock_done = FALSE;
+
+    ret = init_bus();
+    if (!ret) {
+        fprintf(stderr, "bus init err.\n");
+        return FALSE;
+    }
+
     ret = init_clock();
     if (!ret) {
         fprintf(stderr, "clock init err.\n");
         return FALSE;
     }
 
+    ret = init_cpu();
+    if (!ret) {
+        fprintf(stderr, "cpu init err.\n");
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+static void clean_datas(void) {
+    dprint("clean data...\n");
+    clean_clock();
+    clean_bus();
+}
+
+static void sig_handler(int sig) {
+    clock_done = TRUE;
+}
+
+static int prepare_sig(void) {
+    int ret;
+    struct sigaction sigact;
+
+    memset(&sigact, 0, sizeof(struct sigaction));
+    sigact.sa_handler = sig_handler;
+    if ( sigemptyset(&sigact.sa_mask) ) {
+        return FALSE;
+    }
+    if ( sigaction(SIGINT, &sigact, NULL) ) {
+        return FALSE;
+    }
+
     return TRUE;
 }
 
@@ -24,7 +75,21 @@ int main(int argc, char* argv[]) {
         return RT_ERROR;
     }
 
-    clock_loop();
+    ///register the Ctrl-C signal handler.
+    ret = prepare_sig();
+    if (!ret) {
+        fprintf(stderr, "signal handling error...\n");
+        return RT_ERROR;
+    }
+    reset_cpu();
+    start_clock();
+
+    while (!clock_done) {
+        sleep(1);
+    }
+
+    clean_datas();
 
     return 0;
 }