Min преди 4 години
родител
ревизия
8726844fdd

+ 2 - 2
emulator/devices/cpu/decoder.h

@@ -36,8 +36,8 @@ void decode(Emulator *emu, uint16_t instruction, bool disassemble);
 uint16_t fetch(Emulator *emu);
 
 enum { 
-  WORD, 
-  BYTE 
+  EMU_WORD,
+  EMU_BYTE
 };
 
 enum {

+ 8 - 8
emulator/devices/cpu/flag_handler.c

@@ -27,14 +27,14 @@
  */
 uint8_t is_zero (uint16_t *result_addr, uint8_t bw_flag)
 {
-  if (bw_flag == WORD) {
+  if (bw_flag == EMU_WORD) {
     if (*result_addr == 0 ) {
       return 1;
     }
 
     return 0;
   }
-  else if (bw_flag == BYTE) {
+  else if (bw_flag == EMU_BYTE) {
     if (*(int8_t *) result_addr == 0) {
       return 1;
     }
@@ -53,14 +53,14 @@ uint8_t is_zero (uint16_t *result_addr, uint8_t bw_flag)
  */
 uint8_t is_negative(int16_t *result_addr, uint8_t bw_flag)
 {
-  if (bw_flag == WORD) {
+  if (bw_flag == EMU_WORD) {
     if (*result_addr < 0) {
       return 1;
     }
 
     return 0;
   }
-  else if (bw_flag == BYTE) {
+  else if (bw_flag == EMU_BYTE) {
     if (*((int8_t *) result_addr) < 0) {
       return 1;
     }
@@ -81,7 +81,7 @@ uint8_t is_negative(int16_t *result_addr, uint8_t bw_flag)
 uint8_t is_carried(uint32_t original_dst_value, uint32_t source_value,
                    uint8_t bw_flag)
 {
-  if (bw_flag == WORD) {
+  if (bw_flag == EMU_WORD) {
     if ((65535 - (uint16_t)source_value) < (uint16_t)original_dst_value ||
         ((original_dst_value + source_value) >> 16) != 0) {
       return 1;
@@ -89,7 +89,7 @@ uint8_t is_carried(uint32_t original_dst_value, uint32_t source_value,
 
     return 0;
   }
-  else if (bw_flag == BYTE) {
+  else if (bw_flag == EMU_BYTE) {
     if ((255 - (uint8_t)source_value) < (uint8_t)original_dst_value ||
         ((original_dst_value + source_value) >> 8) != 0) {
       return 1;
@@ -112,7 +112,7 @@ uint8_t is_carried(uint32_t original_dst_value, uint32_t source_value,
 uint8_t is_overflowed(uint16_t source_value, uint16_t destination_value,
                       uint16_t *result, uint8_t bw_flag)
 {
-  if (bw_flag == WORD) {
+  if (bw_flag == EMU_WORD) {
     if ( (source_value >> 15) == (destination_value >> 15) &&
          (*result >> 15) != (destination_value >> 15) ) {
       return 1;
@@ -120,7 +120,7 @@ uint8_t is_overflowed(uint16_t source_value, uint16_t destination_value,
 
     return 0;
   }
-  else if (bw_flag == BYTE) {
+  else if (bw_flag == EMU_BYTE) {
     uint8_t dst_prev_value = (uint8_t) destination_value;
     uint8_t src_value = (uint8_t) source_value;
 

+ 44 - 44
emulator/devices/cpu/formatI.c

@@ -93,7 +93,7 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
     destination_addr = (uint16_t *)d_reg;         // Destination Register
 
     if (!disassemble) {
-      bw_flag == BYTE ? *d_reg &= 0x00FF : 0;
+      bw_flag == EMU_BYTE ? *d_reg &= 0x00FF : 0;
     }
   }
   
@@ -182,7 +182,7 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
     destination_addr = (uint16_t *)d_reg;          /* Destination register */
     
     if (!disassemble) {
-      bw_flag == BYTE ? *d_reg &= 0x00FF : 0;
+      bw_flag == EMU_BYTE ? *d_reg &= 0x00FF : 0;
     }
   }
 
@@ -272,7 +272,7 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
     destination_addr = (uint16_t*)d_reg;          /* Destination Register */
 
     if (!disassemble) {
-      bw_flag == BYTE ? *d_reg &= 0x00FF : 0;
+      bw_flag == EMU_BYTE ? *d_reg &= 0x00FF : 0;
     }
   }
 
@@ -330,11 +330,11 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
       sprintf(hex_str_part, "%04X", (uint16_t) source_value);
       strncat(hex_str, hex_str_part, sizeof hex_str);
 
-      if (bw_flag == WORD) {
+      if (bw_flag == EMU_WORD) {
 	sprintf(asm_operands, "#0x%04X, %s", 
 		(uint16_t) source_value, d_reg_name);
       }
-      else if (bw_flag == BYTE) {
+      else if (bw_flag == EMU_BYTE) {
 	sprintf(asm_operands, "#0x%04X, %s",
 		(uint8_t) source_value, d_reg_name);
       }
@@ -345,14 +345,14 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
       sprintf(asm_operands, "@%s+, %s", s_reg_name, d_reg_name);
       
       if (!disassemble) {
-	bw_flag == WORD ? *s_reg += 2 : (*s_reg += 1);
+	bw_flag == EMU_WORD ? *s_reg += 2 : (*s_reg += 1);
       }
     }
 
     destination_addr = (uint16_t*)d_reg;           /* Destination Register */
 
     if (!disassemble) {
-      bw_flag == BYTE ? *d_reg &= 0x00FF : 0;
+      bw_flag == EMU_BYTE ? *d_reg &= 0x00FF : 0;
     }
   }
 
@@ -384,7 +384,7 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
       sprintf(asm_operands, "@%s+, ", s_reg_name);	
 
       if (!disassemble) {
-	bw_flag == WORD ? *s_reg += 2 : (*s_reg += 1);
+	bw_flag == EMU_WORD ? *s_reg += 2 : (*s_reg += 1);
       }
     }
 
@@ -428,10 +428,10 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
        */
       case 0x4: {
 
-        if (bw_flag == WORD) {		
+        if (bw_flag == EMU_WORD) {
           *destination_addr = source_value;
         }
-        else if (bw_flag == BYTE) {
+        else if (bw_flag == EMU_BYTE) {
           *((uint8_t *) destination_addr) = (uint8_t) source_value;
         }
         
@@ -456,10 +456,10 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
 
         uint16_t original_dst_value = *destination_addr;
 
-        if (bw_flag == WORD) {
+        if (bw_flag == EMU_WORD) {
           *destination_addr += source_value;
         }
-        else if (bw_flag == BYTE) {
+        else if (bw_flag == EMU_BYTE) {
           *((uint8_t *) destination_addr) += (uint8_t) source_value;
         }
 
@@ -489,10 +489,10 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
 
         uint16_t original_dst_value = *destination_addr;
 
-        if (bw_flag == WORD) {
+        if (bw_flag == EMU_WORD) {
           *destination_addr += source_value + cpu->sr.carry;
         }
-        else if (bw_flag == BYTE) {
+        else if (bw_flag == EMU_BYTE) {
           *((uint8_t *) destination_addr) += (uint8_t) source_value + cpu->sr.carry;
         }
         
@@ -524,10 +524,10 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
         int16_t original_dst_value = *destination_addr;
         source_value = ~source_value; /* 1's comp */
         
-        if (bw_flag == WORD) {
+        if (bw_flag == EMU_WORD) {
           *(int16_t *)destination_addr += source_value + cpu->sr.carry; 
         }
-        else if (bw_flag == BYTE) {
+        else if (bw_flag == EMU_BYTE) {
           *(int8_t *)destination_addr += (int8_t) source_value + cpu->sr.carry;
         }
 
@@ -559,10 +559,10 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
         int16_t original_dst_value = *destination_addr;
         source_value = ~source_value + 1;
  
-        if (bw_flag == WORD) {
+        if (bw_flag == EMU_WORD) {
           *(uint16_t *)destination_addr += source_value; 
         }
-        else if (bw_flag == BYTE) {
+        else if (bw_flag == EMU_BYTE) {
           *(uint8_t *)destination_addr += (uint8_t) source_value;
         }
 
@@ -596,10 +596,10 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
 
         bool early_carry = is_carried((uint16_t)~source_value, 1, bw_flag);
 
-        if (bw_flag == WORD) {
+        if (bw_flag == EMU_WORD) {
           result = *destination_addr + (uint16_t) unsigned_source_value; 
         }
-        else if (bw_flag == BYTE) {
+        else if (bw_flag == EMU_BYTE) {
           result = *(uint8_t *)destination_addr + (uint8_t)unsigned_source_value;
         }
         
@@ -623,10 +623,10 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
        */
       case 0xA:{
 
-        if (bw_flag == WORD) {
+        if (bw_flag == EMU_WORD) {
           
         }
-        else if (bw_flag == BYTE) {
+        else if (bw_flag == EMU_BYTE) {
           
         }
         
@@ -642,14 +642,14 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
       */
       case 0xB:{
 
-        if (bw_flag == WORD) {
+        if (bw_flag == EMU_WORD) {
           uint16_t result = ((uint16_t) source_value) & (*destination_addr);
 
           cpu->sr.zero = (result == 0);
           cpu->sr.negative = result >> 15;
           cpu->sr.carry = (result != 0);
         }
-        else if (bw_flag == BYTE) {
+        else if (bw_flag == EMU_BYTE) {
           uint8_t result = 
             ((uint8_t) source_value) & (*(uint8_t *) destination_addr);
 
@@ -669,10 +669,10 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
        */
       case 0xC:{
 
-        if (bw_flag == WORD) {
+        if (bw_flag == EMU_WORD) {
           *destination_addr &= (uint16_t) ~source_value;
         }
-        else if (bw_flag == BYTE) {
+        else if (bw_flag == EMU_BYTE) {
           *(uint8_t *) destination_addr &= (uint8_t) ~source_value;	
         }
         
@@ -684,10 +684,10 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
        */
       case 0xD:{
 
-        if (bw_flag == WORD) {
+        if (bw_flag == EMU_WORD) {
           *destination_addr |= (uint16_t) source_value;
         }
-        else if (bw_flag == BYTE) {
+        else if (bw_flag == EMU_BYTE) {
           *(uint8_t *) destination_addr |= (uint8_t) source_value;	
         }
         
@@ -703,7 +703,7 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
        */
       case 0xE:{
 
-        if (bw_flag == WORD) {
+        if (bw_flag == EMU_WORD) {
           cpu->sr.overflow = 
             (*destination_addr >> 15) && ((uint16_t)source_value >> 15);
 
@@ -713,7 +713,7 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
           cpu->sr.zero = (*destination_addr == 0);
           cpu->sr.carry = (*destination_addr != 0);
         }
-        else if (bw_flag == BYTE) {
+        else if (bw_flag == EMU_BYTE) {
           cpu->sr.overflow = 
             (*(uint8_t *)destination_addr >> 7) && ((uint8_t)source_value >> 7);
 
@@ -736,14 +736,14 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
        */
       case 0xF:{
 
-        if (bw_flag == WORD) {
+        if (bw_flag == EMU_WORD) {
           *destination_addr &= (uint16_t)source_value;	
 
           cpu->sr.negative = (*destination_addr) >> 15;
           cpu->sr.zero = (*destination_addr == 0);
           cpu->sr.carry = (*destination_addr != 0);
         }
-        else if (bw_flag == BYTE) {
+        else if (bw_flag == EMU_BYTE) {
           *(uint8_t *) destination_addr &= (uint8_t) source_value;	
 
           cpu->sr.negative = (*(uint8_t *) destination_addr) >> 7;
@@ -766,84 +766,84 @@ void decode_formatI(Emulator *emu, uint16_t instruction, bool disassemble)
   else {
     switch (opcode) {      
       case 0x4: {
-	bw_flag == WORD ? 
+	bw_flag == EMU_WORD ?
           strncpy(mnemonic, "MOV", sizeof mnemonic) :
           strncpy(mnemonic, "MOV.B", sizeof mnemonic);
 
         break;
       }
       case 0x5: {
-        bw_flag == WORD ? 
+        bw_flag == EMU_WORD ?
           strncpy(mnemonic, "ADD", sizeof mnemonic) :
           strncpy(mnemonic, "ADD.B", sizeof mnemonic);
 
         break;
       }
       case 0x6: {
-        bw_flag == WORD ? 
+        bw_flag == EMU_WORD ?
           strncpy(mnemonic, "ADDC", sizeof mnemonic) :
           strncpy(mnemonic, "ADDC.B", sizeof mnemonic);
 	
         break;
       }
       case 0x7: {
-        bw_flag == WORD ? 
+        bw_flag == EMU_WORD ?
           strncpy(mnemonic, "SUBC", sizeof mnemonic) :
           strncpy(mnemonic, "SUBC.B", sizeof mnemonic);
         
         break;
       }
       case 0x8: {
-        bw_flag == WORD ? 
+        bw_flag == EMU_WORD ?
           strncpy(mnemonic, "SUB", sizeof mnemonic) :
           strncpy(mnemonic, "SUB.B", sizeof mnemonic);
 
         break;
       }
       case 0x9: {
-        bw_flag == WORD ? 
+        bw_flag == EMU_WORD ?
           strncpy(mnemonic, "CMP", sizeof mnemonic) :
           strncpy(mnemonic, "CMP.B", sizeof mnemonic);
 
         break;
       }
       case 0xA: {
-	bw_flag == WORD ? 
+	bw_flag == EMU_WORD ?
           strncpy(mnemonic, "DADD", sizeof mnemonic) :
           strncpy(mnemonic, "DADD.B", sizeof mnemonic);
         
         break;
       }
       case 0xB: {
-        bw_flag == WORD ? 
+        bw_flag == EMU_WORD ?
           strncpy(mnemonic, "BIT", sizeof mnemonic) :
           strncpy(mnemonic, "BIT.B", sizeof mnemonic);
 
         break;
       }     
       case 0xC: {
-        bw_flag == WORD ? 
+        bw_flag == EMU_WORD ?
           strncpy(mnemonic, "BIC", sizeof mnemonic) :
           strncpy(mnemonic, "BIC.B", sizeof mnemonic);
                 
         break;
       }
       case 0xD: {
-        bw_flag == WORD ? 
+        bw_flag == EMU_WORD ?
           strncpy(mnemonic, "BIS", sizeof mnemonic) :
           strncpy(mnemonic, "BIS.B", sizeof mnemonic);
                 
         break;
       }
       case 0xE: {
-        bw_flag == WORD ? 
+        bw_flag == EMU_WORD ?
           strncpy(mnemonic, "XOR", sizeof mnemonic) :
           strncpy(mnemonic, "XOR.B", sizeof mnemonic);
         
         break;
       }
       case 0xF: {
-        bw_flag == WORD ? 
+        bw_flag == EMU_WORD ?
           strncpy(mnemonic, "AND", sizeof mnemonic) :
           strncpy(mnemonic, "AND.B", sizeof mnemonic);
         

+ 15 - 15
emulator/devices/cpu/formatII.c

@@ -91,7 +91,7 @@ void decode_formatII(Emulator *emu, uint16_t instruction, bool disassemble)
       sprintf(asm_operand, "%s", reg_name);
     }
 
-    bw_flag == BYTE ? *reg &= 0x00FF : 0;
+    bw_flag == EMU_BYTE ? *reg &= 0x00FF : 0;
   }
 
   /* Indexed;      Ex: PUSH 0x0(Rs) */
@@ -172,10 +172,10 @@ void decode_formatII(Emulator *emu, uint16_t instruction, bool disassemble)
       sprintf(hex_str_part, "%04X", (uint16_t) source_value);
       strncat(hex_str, hex_str_part, sizeof hex_str);
 
-      if (bw_flag == WORD) {
+      if (bw_flag == EMU_WORD) {
         sprintf(asm_operand, "#0x%04X", (uint16_t) source_value);
       }
-      else if (bw_flag == BYTE) {
+      else if (bw_flag == EMU_BYTE) {
         sprintf(asm_operand, "#0x%04X", (uint8_t) source_value);
       }
     }
@@ -184,7 +184,7 @@ void decode_formatII(Emulator *emu, uint16_t instruction, bool disassemble)
       source_value = *source_address;
 
       sprintf(asm_operand, "@%s+", reg_name);
-      bw_flag == WORD ? *reg += 2 : (*reg += 1);
+      bw_flag == EMU_WORD ? *reg += 2 : (*reg += 1);
     }
   }
 
@@ -208,12 +208,12 @@ void decode_formatII(Emulator *emu, uint16_t instruction, bool disassemble)
     case 0x0:{
       bool CF = cpu->sr.carry;
 
-      if (bw_flag == WORD) {
+      if (bw_flag == EMU_WORD) {
 	cpu->sr.carry = *source_address & 0x0001;  /* Set CF from LSB */
 	*source_address >>= 1;                /* Shift one right */
 	CF ? *source_address |= 0x8000 : 0;   /* Set MSB from prev CF */
       }
-      else if (bw_flag == BYTE){
+      else if (bw_flag == EMU_BYTE){
 	cpu->sr.carry = *(uint8_t *) source_address & 0x01;
 	*(uint8_t *) source_address >>= 1;
 	CF ? *(uint8_t *) source_address |= 0x80 : 0;
@@ -249,13 +249,13 @@ void decode_formatII(Emulator *emu, uint16_t instruction, bool disassemble)
        * V: Reset
        */
     case 0x2:{
-      if (bw_flag == WORD) {
+      if (bw_flag == EMU_WORD) {
 	cpu->sr.carry = *source_address & 0x0001;
 	bool msb = *source_address >> 15;
 	*source_address >>= 1;
 	msb ? *source_address |= 0x8000 : 0; /* Extend Sign */
       }
-      else if (bw_flag == BYTE) {
+      else if (bw_flag == EMU_BYTE) {
 	cpu->sr.carry = *source_address & 0x0001;
 	bool msb = *source_address >> 7;
 	*source_address >>= 1;
@@ -287,8 +287,8 @@ void decode_formatII(Emulator *emu, uint16_t instruction, bool disassemble)
 	*source_address &= 0x00FF;
       }
     
-      cpu->sr.negative = is_negative((int16_t*)source_address, WORD);
-      cpu->sr.zero = is_zero(source_address, WORD);
+      cpu->sr.negative = is_negative((int16_t*)source_address, EMU_WORD);
+      cpu->sr.zero = is_zero(source_address, EMU_WORD);
       cpu->sr.carry = ! cpu->sr.zero;
       cpu->sr.overflow = false;
 
@@ -306,10 +306,10 @@ void decode_formatII(Emulator *emu, uint16_t instruction, bool disassemble)
       cpu->sp -= 2; /* Yes, even for BYTE Instructions */
       uint16_t *stack_address = get_stack_ptr(emu);
     
-      if (bw_flag == WORD) {
+      if (bw_flag == EMU_WORD) {
 	*stack_address = source_value;
       }
-      else if (bw_flag == BYTE) {
+      else if (bw_flag == EMU_BYTE) {
 	*stack_address &= 0xFF00; /* Zero out bottom half for pushed byte */
 	*stack_address |= (uint8_t) source_value;
       }
@@ -349,7 +349,7 @@ void decode_formatII(Emulator *emu, uint16_t instruction, bool disassemble)
   else {    
     switch (opcode) {
     case 0x0: {
-      bw_flag == WORD ?
+      bw_flag == EMU_WORD ?
 	strncpy(mnemonic, "RRC", sizeof mnemonic) :
 	strncpy(mnemonic, "RRC.B", sizeof mnemonic);    
 
@@ -360,7 +360,7 @@ void decode_formatII(Emulator *emu, uint16_t instruction, bool disassemble)
       break;
     }
     case 0x2: {
-      bw_flag == WORD ?
+      bw_flag == EMU_WORD ?
 	strncpy(mnemonic, "RRA", sizeof mnemonic) :
 	strncpy(mnemonic, "RRA.B", sizeof mnemonic);     
 
@@ -371,7 +371,7 @@ void decode_formatII(Emulator *emu, uint16_t instruction, bool disassemble)
       break;
     }
     case 0x4: {
-      bw_flag == WORD ?
+      bw_flag == EMU_WORD ?
 	strncpy(mnemonic, "PUSH", sizeof mnemonic) :
 	strncpy(mnemonic, "PUSH.B", sizeof mnemonic);    
 

+ 38 - 12
emulator/devices/peripherals/bcm.c

@@ -18,6 +18,28 @@
 
 #include "bcm.h"
 
+#ifdef _MSC_VER
+#include <Windows.h>
+#else
+#include <time.h>
+#endif
+
+uint64_t getnano() {
+#ifdef _MSC_VER
+    static LARGE_INTEGER frequency;
+    if (frequency.QuadPart == 0) QueryPerformanceFrequency(&frequency);
+    LARGE_INTEGER now;
+    QueryPerformanceCounter(&now);
+    double x = (double)now.QuadPart / (double)frequency.QuadPart;
+    return (uint64_t)(x * 1.0e9);
+#else
+    struct timespec now;
+    clock_gettime(CLOCK_MONOTONIC, &now);
+    return now.tv_sec * 1000000000 + now.tv_nsec;
+#endif
+}
+
+
 void handle_bcm (Emulator *emu) 
 {
   Cpu *cpu = emu->cpu;
@@ -163,27 +185,29 @@ void setup_bcm (Emulator *emu)
 }
 
 
-uint64_t nanosec_diff(struct timespec *timeA_p, struct timespec *timeB_p)
-{
-    return ((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) - ((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);
-}
+//uint64_t nanosec_diff(struct timespec *timeA_p, struct timespec *timeB_p)
+//{
+//    return ((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) - ((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);
+//}
 
 void mclk_wait_cycles (Emulator *emu, uint64_t cycles)
 {
     Cpu *cpu = emu->cpu;
     Bcm *bcm = cpu->bcm;  
 
-    struct timespec start, end;
+    uint64_t start, end;
     uint64_t i, elapsed_nsecs;
   
     for (i = 0;i < cycles;i++)
     {
-        clock_gettime(CLOCK_MONOTONIC, &start);
+        start = getnano();
+//        clock_gettime(CLOCK_MONOTONIC, &start);
 
         while (true)
         {
-            clock_gettime(CLOCK_MONOTONIC, &end);
-            elapsed_nsecs = nanosec_diff(&end, &start);
+//            clock_gettime(CLOCK_MONOTONIC, &end);
+            end = getnano();
+            elapsed_nsecs = end - start;//nanosec_diff(&end, &start);
 
             // Choose timing based on clock source
             if (bcm->mclk_source == DCOCLK)
@@ -206,15 +230,17 @@ void smclk_wait_cycles (Emulator *emu, uint64_t cycles)
   Cpu *cpu = emu->cpu;
   Bcm *bcm = cpu->bcm;  
   
-  struct timespec start, end;
+  uint64_t start, end;
   uint64_t i, elapsed_nsecs;
   
   for (i = 0;i < cycles;i++) {
-    clock_gettime(CLOCK_MONOTONIC, &start);
+    start = getnano();
+    //    clock_gettime(CLOCK_MONOTONIC, &start);
 
     while (true) {
-      clock_gettime(CLOCK_MONOTONIC, &end);
-      elapsed_nsecs = nanosec_diff(&end, &start);
+      end = getnano();
+//      clock_gettime(CLOCK_MONOTONIC, &end);
+      elapsed_nsecs = end - start;//nanosec_diff(&end, &start);
 
       // Choose timing based on clock source
       if (bcm->mclk_source == DCOCLK) {

+ 0 - 1
emulator/devices/peripherals/bcm.h

@@ -21,7 +21,6 @@
 
 #include <stdlib.h>
 #include <stdio.h>
-#include <time.h>
 
 #include "../cpu/registers.h"
 #include "../utilities.h"

+ 47 - 47
emulator/devices/peripherals/port1.c

@@ -44,9 +44,9 @@ void handle_port_1 (Emulator *emu)
     //////////////////// P1.0 ////////////////////////
 
     // Check Direction
-    if (*p->DIR & 0x01) {
+    if (*p->_DIR & 0x01) {
         p->DIR_0 = true;           // Set P1DIR.0 flag
-        if (*p->OUT & 0x01)        // Check OUTPUT
+        if (*p->_OUT & 0x01)        // Check OUTPUT
             p->OUT_0 = true;       // Set P1OUT.0 flag
         else
             p->OUT_0 = false;      // Reset P1OUT.0 flag
@@ -57,12 +57,12 @@ void handle_port_1 (Emulator *emu)
     }
     
     /// Check if Interrupt Enabled for pin 
-    if (*p->IE & 0x01)
+    if (*p->_IE & 0x01)
     {         
         p->IE_0 = true;
 
         // Check For Interrupt Pending 
-        if (*p->IFG & 0x01)
+        if (*p->_IFG & 0x01)
         {    
             // Set p->IFG.0 flag indicating INT 
             p->IFG_0 = true;
@@ -78,7 +78,7 @@ void handle_port_1 (Emulator *emu)
     }    
     
     // Check primary select
-    if (*p->SEL & 0x01) {
+    if (*p->_SEL & 0x01) {
         if (p->SEL_0 == false) {
             puts("P1_SEL_0 = 1");
         }
@@ -94,7 +94,7 @@ void handle_port_1 (Emulator *emu)
     }
 
     // Check secondary select
-    if (*p->SEL2 & 0x01) {
+    if (*p->_SEL2 & 0x01) {
         if (p->SEL2_0 == false) {
             puts("P1_SEL2_0 = 1");
         }
@@ -112,9 +112,9 @@ void handle_port_1 (Emulator *emu)
     //////////////////// P1.1 ////////////////////////
 
     // Check Direction and IN/OUT
-    if (*p->DIR & 0x02) {
+    if (*p->_DIR & 0x02) {
         p->DIR_1 = true;
-        if (*p->OUT & 0x02) {
+        if (*p->_OUT & 0x02) {
             p->OUT_1 = true;
         }
         else {
@@ -126,10 +126,10 @@ void handle_port_1 (Emulator *emu)
     }
 
     // Check Interrupts
-    if (*p->IE & 0x02) {
+    if (*p->_IE & 0x02) {
         p->IE_1 = true;
 
-        if (*p->IFG & 0x02) {
+        if (*p->_IFG & 0x02) {
             p->IFG_1 = true;
         }
         else {
@@ -141,7 +141,7 @@ void handle_port_1 (Emulator *emu)
     }
 
     // Check primary select
-    if (*p->SEL & 0x02) {
+    if (*p->_SEL & 0x02) {
         if (p->SEL_1 == false) {
             puts("P1_SEL_1 = 1");
         }
@@ -157,7 +157,7 @@ void handle_port_1 (Emulator *emu)
     }
 
     // Check secondary select
-    if (*p->SEL2 & 0x02) {
+    if (*p->_SEL2 & 0x02) {
         if (p->SEL2_1 == false) {
             p->SEL2_1 = true;
             puts("P1_SEL2_1 = 1");
@@ -172,11 +172,11 @@ void handle_port_1 (Emulator *emu)
 
     //////////////////// P1.2 ////////////////////////
 
-    if (*p->DIR & 0x04)
+    if (*p->_DIR & 0x04)
     {
         p->DIR_2 = true;
 
-        if (*p->OUT & 0x04)
+        if (*p->_OUT & 0x04)
         {
             p->OUT_2 = true;
         }
@@ -190,11 +190,11 @@ void handle_port_1 (Emulator *emu)
         p->DIR_2 = false;
     }
 
-    if (*p->IE & 0x04)
+    if (*p->_IE & 0x04)
     {
         p->IE_2 = true;
 
-        if (*p->IFG & 0x04)
+        if (*p->_IFG & 0x04)
         {
             p->IFG_2 = true;
         }
@@ -209,7 +209,7 @@ void handle_port_1 (Emulator *emu)
     }
 
     // Check primary select
-    if (*p->SEL & 0x04)
+    if (*p->_SEL & 0x04)
     {
         if (p->SEL_2 == false) 
         {
@@ -229,7 +229,7 @@ void handle_port_1 (Emulator *emu)
     }
 
     // Check secondary select
-    if (*p->SEL2 & 0x04) {
+    if (*p->_SEL2 & 0x04) {
         if (p->SEL2_2 == false) {
             puts("P1_SEL2_2 = 1");
         }
@@ -247,9 +247,9 @@ void handle_port_1 (Emulator *emu)
     ////////////////////////////////////////////////
 
     // Handler P1.3 
-    if (*p->DIR & 0x08) {
+    if (*p->_DIR & 0x08) {
         p->DIR_3 = true;
-        if (*p->OUT & 0x08) {
+        if (*p->_OUT & 0x08) {
             p->OUT_3 = true;
         }
         else {
@@ -260,10 +260,10 @@ void handle_port_1 (Emulator *emu)
         p->DIR_3 = false;
     }
 
-    if (*p->IE & 0x08) {
+    if (*p->_IE & 0x08) {
         p->IE_3 = true;
 
-        if (*p->IFG & 0x08) {
+        if (*p->_IFG & 0x08) {
             p->IFG_3 = true;
         }
         else {
@@ -277,9 +277,9 @@ void handle_port_1 (Emulator *emu)
     ///////////////////////////////////////////////////////////////
 
     // Handler P1.4 
-    if (*p->DIR & 0x10) {
+    if (*p->_DIR & 0x10) {
         p->DIR_4 = true;
-        if (*p->OUT & 0x10) {
+        if (*p->_OUT & 0x10) {
             p->OUT_4 = true;
         }
         else {
@@ -290,10 +290,10 @@ void handle_port_1 (Emulator *emu)
         p->DIR_4 = false;
     }
 
-    if (*p->IE & 0x10) {
+    if (*p->_IE & 0x10) {
         p->IE_4 = true;
 
-        if (*p->IFG & 0x10) {
+        if (*p->_IFG & 0x10) {
             p->IFG_4 = true;
         }
         else {
@@ -307,9 +307,9 @@ void handle_port_1 (Emulator *emu)
     /////////////////////////////////////////////////
 
     // Handler P1.5 
-    if (*p->DIR & 0x20) {
+    if (*p->_DIR & 0x20) {
         p->DIR_5 = true;
-        if (*p->OUT & 0x20) {
+        if (*p->_OUT & 0x20) {
             p->OUT_5 = true;
         }
         else {
@@ -320,10 +320,10 @@ void handle_port_1 (Emulator *emu)
         p->DIR_5 = false;
     }
 
-    if (*p->IE & 0x20) {
+    if (*p->_IE & 0x20) {
         p->IE_5 = true;
 
-        if (*p->IFG & 0x20) {
+        if (*p->_IFG & 0x20) {
             p->IFG_5 = true;
         }
         else {
@@ -337,10 +337,10 @@ void handle_port_1 (Emulator *emu)
     ////////////////////////////////////////////////////
 
     // Handler P1.6 
-    if (*p->DIR & 0x40)
+    if (*p->_DIR & 0x40)
     {
         p->DIR_6 = true;
-        if (*p->OUT & 0x40)
+        if (*p->_OUT & 0x40)
         {
             p->OUT_6 = true;
         }
@@ -354,11 +354,11 @@ void handle_port_1 (Emulator *emu)
         p->DIR_6 = false;
     }
 
-    if (*p->IE & 0x40)
+    if (*p->_IE & 0x40)
     {
         p->IE_6 = true;
 
-        if (*p->IFG & 0x40)
+        if (*p->_IFG & 0x40)
         {
             p->IFG_6 = true;
         }
@@ -375,9 +375,9 @@ void handle_port_1 (Emulator *emu)
     ////////////////////////////////////////////////////
 
     // Handler P1.7 
-    if (*p->DIR & 0x80) {
+    if (*p->_DIR & 0x80) {
         p->DIR_7 = true;
-        if (*p->OUT & 0x80) {
+        if (*p->_OUT & 0x80) {
             p->OUT_7 = true;
         }
         else {
@@ -388,10 +388,10 @@ void handle_port_1 (Emulator *emu)
         p->DIR_7 = false;
     }
 
-    if (*p->IE & 0x80) {
+    if (*p->_IE & 0x80) {
         p->IE_7 = true;
 
-        if (*p->IFG & 0x80) {
+        if (*p->_IFG & 0x80) {
             p->IFG_7 = true;
         }
         else {
@@ -418,15 +418,15 @@ void setup_port_1 (Emulator *emu)
     static const uint16_t SEL2_VLOC = 0x41;   // Select 2
     static const uint16_t REN_VLOC  = 0x27;   // Resistor Enable
   
-    *(p->IN   = (uint8_t *) get_addr_ptr(IN_VLOC))   = 0;
-    *(p->OUT  = (uint8_t *) get_addr_ptr(OUT_VLOC))  = 0;
-    *(p->DIR  = (uint8_t *) get_addr_ptr(DIR_VLOC))  = 0;
-    *(p->IFG  = (uint8_t *) get_addr_ptr(IFG_VLOC))  = 0;
-    *(p->IES  = (uint8_t *) get_addr_ptr(IES_VLOC))  = 0;
-    *(p->IE   = (uint8_t *) get_addr_ptr(IE_VLOC))   = 0;
-    *(p->SEL  = (uint8_t *) get_addr_ptr(SEL_VLOC))  = 0;
-    *(p->SEL2 = (uint8_t *) get_addr_ptr(SEL2_VLOC)) = 0;
-    *(p->REN  = (uint8_t *) get_addr_ptr(REN_VLOC))  = 0;
+    *(p->_IN   = (uint8_t *) get_addr_ptr(IN_VLOC))   = 0;
+    *(p->_OUT  = (uint8_t *) get_addr_ptr(OUT_VLOC))  = 0;
+    *(p->_DIR  = (uint8_t *) get_addr_ptr(DIR_VLOC))  = 0;
+    *(p->_IFG  = (uint8_t *) get_addr_ptr(IFG_VLOC))  = 0;
+    *(p->_IES  = (uint8_t *) get_addr_ptr(IES_VLOC))  = 0;
+    *(p->_IE   = (uint8_t *) get_addr_ptr(IE_VLOC))   = 0;
+    *(p->_SEL  = (uint8_t *) get_addr_ptr(SEL_VLOC))  = 0;
+    *(p->_SEL2 = (uint8_t *) get_addr_ptr(SEL2_VLOC)) = 0;
+    *(p->_REN  = (uint8_t *) get_addr_ptr(REN_VLOC))  = 0;
 
   
     p->DIR_0 = false; p->OUT_0 = false; p->IFG_0 = false; 

+ 9 - 9
emulator/devices/peripherals/port1.h

@@ -26,15 +26,15 @@ struct Port_1 {
   // Peripheral register pointers
   
   // Port 1        = r/w =   = reset? =
-  uint8_t *IN;   /* r          -      */
-  uint8_t *OUT;  /* r/w     unchanged */
-  uint8_t *DIR;  /* r/w     PUC reset */
-  uint8_t *IFG;  /* r/w     PUC reset */
-  uint8_t *IES;  /* r/w     unchanged */
-  uint8_t *IE;   /* r/w     PUC reset */
-  uint8_t *SEL;  /* r/w     PUC reset */
-  uint8_t *SEL2; /* r/w     PUC reset */
-  uint8_t *REN;  /* r/w     PUC reset */
+  uint8_t *_IN;   /* r          -      */
+  uint8_t *_OUT;  /* r/w     unchanged */
+  uint8_t *_DIR;  /* r/w     PUC reset */
+  uint8_t *_IFG;  /* r/w     PUC reset */
+  uint8_t *_IES;  /* r/w     unchanged */
+  uint8_t *_IE;   /* r/w     PUC reset */
+  uint8_t *_SEL;  /* r/w     PUC reset */
+  uint8_t *_SEL2; /* r/w     PUC reset */
+  uint8_t *_REN;  /* r/w     PUC reset */
 
   // Peripherals activation flags (for emulator)
   bool DIR_0, OUT_0, IFG_0, IE_0, SEL_0, SEL2_0;

+ 8 - 9
emulator/devices/peripherals/usci.h

@@ -40,8 +40,7 @@ typedef struct Ctl0
   // Idle-line multiprocessor mode (01)
   // Address-bit multiprocessor mode (10)
   // UART mode with automatic baud rate detection (11)
-  uint8_t UCMODE : 2;
-  
+  uint8_t UCMODE;
   bool UCSYNC;       // Synchronous mode enable: Async (0) Sync (1)
 } Ctl0;
 
@@ -49,28 +48,28 @@ typedef struct Ctl0
 typedef struct Ctl1 {
   // USCI clock source select, these bits select the Baud rate source clock.
   // UCLK: 00 | ACLK: 01 | SMCLK: 10 | SMCLK: 11
-  uint8_t UCSSEL : 2;  
+  uint8_t UCSSEL;
 
-  bool UCRXEIE : 1; // Recv erroneous-character interrupt enable
-  bool UCBRKIE : 1; // receive break character interrupt enable
+  bool UCRXEIE; // Recv erroneous-character interrupt enable
+  bool UCBRKIE; // receive break character interrupt enable
 
   // dormant. Puts SCI into sleep mode: 
   // 0 - Not dormant. All received chars will set UCAxRXIFG
   // 1 - Dormant. Only chars that are preceded by an idle line or addr bit
-  bool UCDORM : 1;
+  bool UCDORM;
 
   // Transmit address, Next frame to be transmitted will be marked as address
   // Depedning on the selected multiproc mode. Data (0) Address (1)
-  bool UCTXADDR : 1;
+  bool UCTXADDR;
 
   // Transmit break, transmits a break with the next write to the TX buffer
   // Not a break (0) Transmit a break (1)
-  bool UCTXBRK : 1;
+  bool UCTXBRK;
   
   // Software reset enable: 
   // Disabled. USCI reset released for operation (0)
   // Enabled. USCI logic held in reset start. (1)
-  bool UCSWRST : 1;  
+  bool UCSWRST;
 } Ctl1;
 
 typedef struct Usci 

+ 5 - 1
emulator/emulator.h

@@ -3,7 +3,7 @@
 #define _EMULATOR_H_
 
 #include <stdio.h>
-#include <time.h>
+//#include <time.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <string.h>
@@ -20,6 +20,9 @@
 #include <libwebsockets.h>
 #endif
 
+// Windows compatibility
+
+
 typedef struct Emulator Emulator;
 
 typedef struct Cpu Cpu;
@@ -49,6 +52,7 @@ typedef struct Packet Packet;
 #include "debugger/debugger.h"
 #include "debugger/register_display.h"
 #include "debugger/disassembler.h"
+#include "win.h"
 
 #ifdef PYTHON
 #include "python/py_interface.h"

+ 17 - 0
emulator/win.c

@@ -0,0 +1,17 @@
+#include "win.h"
+
+#ifdef _MSC_VER
+
+#include <windows.h>
+
+void usleep(__int64 usec){
+    HANDLE timer;
+    LARGE_INTEGER ft;
+    ft.QuadPart = -(10*usec);
+    timer = CreateWaitableTimer(NULL, TRUE, NULL);
+    SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
+    WaitForSingleObject(timer, INFINITE);
+    CloseHandle(timer);
+}
+
+#endif

+ 11 - 0
emulator/win.h

@@ -0,0 +1,11 @@
+/*
+ Compatibility functions for windows msvc compiler
+*/
+#ifndef _WIN_H_
+#define _WIN_H_
+
+#define strncasecmp(x,y,z) _strnicmp(x,y,z)
+
+void usleep(__int64 usec);
+
+#endif

+ 19 - 11
msp430emu/emulator.py

@@ -193,8 +193,16 @@ class EmulatorWindow(wx.Frame):
         self.Bind(wx.EVT_BUTTON, self.OnStart, btn_start_emu)
         btn_stop_emu = wx.Button(self, -1, "Pause")
         self.Bind(wx.EVT_BUTTON, self.OnPause, btn_stop_emu)
+
+        btn_key = wx.Button(self, -1, "Press Key")
+        self.Bind(wx.EVT_BUTTON, self.OnKeyPress, btn_key)
+        btn_rst = wx.Button(self, -1, "Reset")
+        self.Bind(wx.EVT_BUTTON, self.OnKeyReset, btn_rst)
+
+        self.sizer2.Add(btn_key, 1, wx.EXPAND)
         self.sizer2.Add(btn_start_emu, 1, wx.EXPAND)
         self.sizer2.Add(btn_stop_emu, 1, wx.EXPAND)
+        self.sizer2.Add(btn_rst, 1, wx.EXPAND)
 
         self.sizer = wx.BoxSizer(wx.VERTICAL)
 
@@ -217,23 +225,23 @@ class EmulatorWindow(wx.Frame):
         # dc.SetBrush(wx.WHITE_BRUSH)
         # dc.DrawRectangle(50, 50, 500, 500)
 
-        self.sizer_key_rst = wx.BoxSizer(wx.HORIZONTAL)
-        btn_key = wx.Button(self, -1, "Press Key")
-        self.Bind(wx.EVT_BUTTON, self.OnKeyPress, btn_key)
-        btn_rst = wx.Button(self, -1, "Reset")
-        self.Bind(wx.EVT_BUTTON, self.OnKeyReset, btn_rst)
-        self.sizer_key_rst.Add(btn_key, 1, wx.EXPAND)
-        self.sizer_key_rst.Add(btn_rst, 1, wx.EXPAND)
+        # self.sizer_key_rst = wx.BoxSizer(wx.HORIZONTAL)
+        # btn_key = wx.Button(self, -1, "Press Key")
+        # self.Bind(wx.EVT_BUTTON, self.OnKeyPress, btn_key)
+        # btn_rst = wx.Button(self, -1, "Reset")
+        # self.Bind(wx.EVT_BUTTON, self.OnKeyReset, btn_rst)
+        # self.sizer_key_rst.Add(btn_key, 1, wx.EXPAND)
+        # self.sizer_key_rst.Add(btn_rst, 1, wx.EXPAND)
 
         self.sizer_diagram = wx.BoxSizer(wx.VERTICAL)
         self.sizer_diagram.Add(panel, 1, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL)
         #
-        self.sizer_left = wx.BoxSizer(wx.VERTICAL)
-        self.sizer_left.Add(self.sizer_diagram, 1, wx.EXPAND)
-        self.sizer_left.Add(self.sizer_key_rst, 0, wx.ALIGN_BOTTOM)
+        # self.sizer_left = wx.BoxSizer(wx.VERTICAL)
+        # self.sizer_left.Add(self.sizer_diagram, 1, wx.EXPAND)
+        # self.sizer_left.Add(self.sizer_key_rst, 0, wx.ALIGN_BOTTOM)
 
         self.sizer1 = wx.BoxSizer(wx.HORIZONTAL)
-        self.sizer1.Add(self.sizer_left, 0, wx.EXPAND)
+        self.sizer1.Add(self.sizer_diagram, 0, wx.EXPAND)
         self.sizer1.Add(self.control, 1, wx.EXPAND)
         self.sizer1.Add(self.sizer0, 1, wx.EXPAND)
 

+ 4 - 3
setup.py

@@ -21,12 +21,13 @@ emulator_files = [
     'emulator/debugger/disassembler.c',
     'emulator/python/py_functions.c',
     'emulator/python/py_interface.c',
+    'emulator/win.c',
 ]
 libraries = [
     # 'websockets',
-    'readline',
-    'rt',
-    'pthread',
+    # 'readline',
+    # 'rt',
+    # 'pthread',
     # 'ssl',
     # 'crypto',
 ]