OSDN Git Service

update and improved clock timing
authorastoria-d <astoria-d@mail.goo.ne.jp>
Fri, 8 Mar 2013 05:14:39 +0000 (14:14 +0900)
committerastoria-d <astoria-d@mail.goo.ne.jp>
Fri, 8 Mar 2013 05:14:39 +0000 (14:14 +0900)
emulator/bus.c
emulator/clock.c
emulator/cpu.c
emulator/ram.c
emulator/rom.c

index d55cea2..98f4966 100644 (file)
@@ -1,5 +1,6 @@
-
 #include <string.h>
+#include <semaphore.h>
+
 #include "tools.h"
 #include "bus.h"
 #include "rom.h"
@@ -17,6 +18,7 @@ struct cpu_pin {
 static unsigned short addr_bus;
 static unsigned char data_bus;
 static struct cpu_pin pin_status;
+static sem_t sem_bus_wait;
 
 /*
  * NES memory map
@@ -37,10 +39,19 @@ static struct cpu_pin pin_status;
 #define IO_APU_MASK 0x001F
 #define ROM_MASK    0x7FFF
 
+void release_bus(void) {
+    pin_status.ready = 1;
+    sem_post(&sem_bus_wait);
+}
+
 void start_bus(void) {
     if (addr_bus & ROM_BIT) {
         /*case rom*/
+        pin_status.ready = 0;
         set_rom_ce_pin(TRUE);
+
+        //wait for the bus ready.
+        sem_wait(&sem_bus_wait);
     }
     else if (addr_bus & IO_APU_BIT) {
     }
@@ -48,7 +59,11 @@ void start_bus(void) {
     }
     else {
         /*case ram*/
+        pin_status.ready = 0;
         set_ram_ce_pin(TRUE);
+
+        //wait for the bus ready.
+        sem_wait(&sem_bus_wait);
     }
 }
 
@@ -68,6 +83,9 @@ void end_bus(void) {
 }
 
 void set_bus_addr(unsigned short addr) {
+    if (!pin_status.ready)
+        return;
+
     if (addr & ROM_BIT) {
         /*case rom*/
         set_rom_addr(addr & ROM_MASK);
@@ -92,6 +110,9 @@ void set_bus_addr(unsigned short addr) {
 }
 
 void set_bus_data(unsigned char data){
+    if (!pin_status.ready)
+        return;
+
     if (addr_bus & ROM_BIT) {
     }
     else if (addr_bus & IO_APU_BIT) {
@@ -106,6 +127,9 @@ void set_bus_data(unsigned char data){
 }
 
 char get_bus_data(void) {
+    if (!pin_status.ready)
+        return 0;
+
     if (addr_bus & ROM_BIT) {
         data_bus = get_rom_data();
     }
@@ -128,12 +152,21 @@ void set_rw_pin(int rw) {
 }
 
 int init_bus(void) {
+    int ret;
+
     addr_bus = 0;
     data_bus = 0;
     memset(&pin_status, 0, sizeof(struct cpu_pin));
+    pin_status.ready = 1;
+
+    ret = sem_init(&sem_bus_wait, 0, 0);
+    if (ret != RT_OK)
+        return FALSE;
+
     return TRUE;
 }
 
 void clean_bus(void){
+    sem_destroy(&sem_bus_wait);
 }
 
index 7b6a484..f9193a8 100644 (file)
@@ -30,7 +30,7 @@ static void* cpu_clock_loop(void* arg) {
         long sec;
         long nsec;
 
-        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &begin);
+        clock_gettime(CLOCK_REALTIME, &begin);
         dprint("-----------------\nclock ");
         ch = handler_list;
         while (ch != NULL) {
@@ -39,7 +39,7 @@ static void* cpu_clock_loop(void* arg) {
             ch = (struct clock_handler*)ch->l.next;
         }
         
-        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
+        clock_gettime(CLOCK_REALTIME, &end);
         //calcurate sleep time.
         if (end.tv_sec < begin.tv_sec )
             sec = LONG_MAX - begin.tv_sec + end.tv_sec + 1;
index c41f903..ad4dea9 100644 (file)
@@ -59,15 +59,10 @@ int clock_cpu(void) {
 }
 
 unsigned char load_memory(unsigned short addr) {
-    struct timespec ts = {CPU_CLOCK_SEC, CPU_CLOCK_NSEC / 2};
 
     set_rw_pin(0);
     set_bus_addr(addr);
     start_bus();
-
-    //must wait half cycle for the bus ready
-    nanosleep(&ts, NULL);
-
     cpu_data_buffer = get_bus_data();
     end_bus();
 
@@ -75,15 +70,11 @@ unsigned char load_memory(unsigned short addr) {
 }
 
 void store_memory(unsigned short addr, unsigned char data) {
-    struct timespec ts = {CPU_CLOCK_SEC, CPU_CLOCK_NSEC / 2};
 
     set_rw_pin(1);
     set_bus_addr(addr);
     set_bus_data(data);
     start_bus();
-
-    //must wait half cycle for the bus ready
-    nanosleep(&ts, NULL);
     end_bus();
 
     cpu_addr_buffer = addr;
index cecfb5a..f1247ac 100644 (file)
@@ -7,6 +7,8 @@
 #include "tools.h"
 #include "clock.h"
 
+void release_bus(void);
+
 struct ram_pin {
     unsigned int ce     :1;     /*chip enable*/
     unsigned int oe     :1;     /*assert on read ready.*/
@@ -69,6 +71,7 @@ static void *ram_loop(void* arg) {
                 nanosleep(&ts, NULL);
                 ram_buffer[ram_addr] = ram_data;
             }
+            release_bus();
         }
     }
     return NULL;
index a5c0c19..ff6318d 100644 (file)
@@ -7,6 +7,8 @@
 #include "tools.h"
 #include "clock.h"
 
+void release_bus(void);
+
 struct rom_pin {
     unsigned int rw     :1;     /*assert on write.*/
     unsigned int ce     :1;     /*chip enable*/
@@ -59,6 +61,7 @@ static void *rom_loop(void* arg) {
         if (rom_pin_status.ce) {
             nanosleep(&ts, NULL);
             rom_data = rom_buffer[rom_addr];
+            release_bus();
         }
     }
     return NULL;