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
+#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){
+}
+#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;
}
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");
+
+
+}
#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__*/
--- /dev/null
+#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;
+}
+
#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;
}
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;
}