]> git.sesse.net Git - pistorm/blobdiff - m68kcpu.h
Improve 68k cycle state machine
[pistorm] / m68kcpu.h
index 923f143b49d2c64c02dcdee6aff4e1e8a0a81a76..ef25ad1b156343911527f76d189f0f8e9c4a73f9 100644 (file)
--- a/m68kcpu.h
+++ b/m68kcpu.h
@@ -43,6 +43,7 @@ extern "C" {
 #include <endian.h>
 
 #include <setjmp.h>
+#include <stdio.h>
 
 /* ======================================================================== */
 /* ==================== ARCHITECTURE-DEPENDANT DEFINES ==================== */
@@ -667,6 +668,7 @@ extern jmp_buf m68ki_aerr_trap;
                        longjmp(m68ki_aerr_trap, 1); \
                }
 #endif
+       #define m68ki_bus_error(ADDR,WRITE_MODE) m68ki_aerr_address=ADDR;m68ki_aerr_write_mode=WRITE_MODE;m68ki_exception_bus_error()
 
        #define m68ki_check_address_error_010_less(ADDR, WRITE_MODE, FC) \
                if (CPU_TYPE_IS_010_LESS(CPU_TYPE)) \
@@ -682,12 +684,12 @@ extern jmp_buf m68ki_aerr_trap;
 /* Logging */
 #if M68K_LOG_ENABLE
        #include <stdio.h>
-       extern FILE* M68K_LOG_FILEHANDLE
+//     extern FILE* M68K_LOG_FILEHANDLE;
        extern const char *const m68ki_cpu_names[];
 
-       #define M68K_DO_LOG(A) if(M68K_LOG_FILEHANDLE) fprintf A
+       #define M68K_DO_LOG(A) do{printf("*************");printf A;}while(0) //if(M68K_LOG_FILEHANDLE) fprintf A
        #if M68K_LOG_1010_1111
-               #define M68K_DO_LOG_EMU(A) if(M68K_LOG_FILEHANDLE) fprintf A
+               #define M68K_DO_LOG_EMU(A) printf A //if(M68K_LOG_FILEHANDLE) fprintf A
        #else
                #define M68K_DO_LOG_EMU(A)
        #endif
@@ -1073,6 +1075,13 @@ char* m68ki_disassemble_quick(unsigned int pc, unsigned int cpu_type);
 
 
 /* ---------------------------- Read Immediate ---------------------------- */
+typedef struct
+{
+    unsigned int lower;
+    unsigned int upper;
+    unsigned char *data;
+} address_translation_cache;
+
 
 extern unsigned char read_ranges;
 extern unsigned int read_addr[8];
@@ -1083,6 +1092,8 @@ extern unsigned int write_addr[8];
 extern unsigned int write_upper[8];
 extern unsigned char *write_data[8];
 
+extern address_translation_cache code_translation_cache;
+
 // clear the instruction cache
 inline void m68ki_ic_clear()
 {
@@ -1150,46 +1161,21 @@ static inline uint32 m68ki_ic_readimm16(uint32 address)
 /* Handles all immediate reads, does address error check, function code setting,
  * and prefetching if they are enabled in m68kconf.h
  */
-static inline uint m68ki_read_imm_16(void)
-{
-       m68ki_set_fc(FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */
-       m68ki_cpu.mmu_tmp_fc = FLAG_S | FUNCTION_CODE_USER_PROGRAM;
-       m68ki_cpu.mmu_tmp_rw = 1;
-       m68ki_cpu.mmu_tmp_sz = M68K_SZ_WORD;
-       m68ki_check_address_error(REG_PC, MODE_READ, FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */
+uint m68ki_read_imm6_addr_slowpath(uint32_t pc, address_translation_cache *cache);
 
-#if M68K_EMULATE_PREFETCH
-{
-       uint result;
-       if(REG_PC != CPU_PREF_ADDR)
-       {
-               CPU_PREF_DATA = m68ki_ic_readimm16(REG_PC);
-               CPU_PREF_ADDR = m68ki_cpu.mmu_tmp_buserror_occurred ? ((uint32)~0) : REG_PC;
-       }
-       result = MASK_OUT_ABOVE_16(CPU_PREF_DATA);
-       REG_PC += 2;
-       if (!m68ki_cpu.mmu_tmp_buserror_occurred) {
-               // prefetch only if no bus error occurred in opcode fetch
-               CPU_PREF_DATA = m68ki_ic_readimm16(REG_PC);
-               CPU_PREF_ADDR = m68ki_cpu.mmu_tmp_buserror_occurred ? ((uint32)~0) : REG_PC;
-               // ignore bus error on prefetch
-               m68ki_cpu.mmu_tmp_buserror_occurred = 0;
-       }
-       return result;
-}
-#else
-
-       uint32_t address = ADDRESS_68K(REG_PC);
-       REG_PC += 2;
 
-       for (int i = 0; i < read_ranges; i++) {
-               if(address >= read_addr[i] && address < read_upper[i]) {
-                       return be16toh(((unsigned short *)(read_data[i] + (address - read_addr[i])))[0]);
-               }
-       }
 
-       return m68k_read_immediate_16(address);
-#endif /* M68K_EMULATE_PREFETCH */
+static inline uint m68ki_read_imm_16(void)
+{
+       uint32_t pc = REG_PC;
+
+    address_translation_cache *cache = &code_translation_cache;
+    if(pc >= cache->lower && pc < cache->upper)
+    {
+        REG_PC += 2;
+        return be16toh(((unsigned short *)(cache->data + (pc - cache->lower)))[0]);
+    }
+    return m68ki_read_imm6_addr_slowpath(pc, cache);
 }
 
 static inline uint m68ki_read_imm_8(void)
@@ -1206,6 +1192,13 @@ static inline uint m68ki_read_imm_32(void)
 //         address = pmmu_translate_addr(address,1);
 #endif
 #endif
+       uint32_t address = ADDRESS_68K(REG_PC);
+       for (int i = 0; i < read_ranges; i++) {
+               if(address >= read_addr[i] && address < read_upper[i]) {
+                       REG_PC += 4;
+                       return be32toh(((unsigned int *)(read_data[i] + (address - read_addr[i])))[0]);
+               }
+       }
 
 #if M68K_EMULATE_PREFETCH
        uint temp_val;
@@ -1233,15 +1226,7 @@ static inline uint m68ki_read_imm_32(void)
 
        return temp_val;
 #else
-       m68ki_set_fc(FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */
-       m68ki_check_address_error(REG_PC, MODE_READ, FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */
-       uint32_t address = ADDRESS_68K(REG_PC);
        REG_PC += 4;
-       for (int i = 0; i < read_ranges; i++) {
-               if(address >= read_addr[i] && address < read_upper[i]) {
-                       return be32toh(((unsigned int *)(read_data[i] + (address - read_addr[i])))[0]);
-               }
-       }
 
        return m68k_read_immediate_32(address);
 #endif /* M68K_EMULATE_PREFETCH */
@@ -2279,13 +2264,32 @@ static inline void m68ki_exception_address_error(void)
                CPU_STOPPED = STOP_LEVEL_HALT;
                return;
        }
-       CPU_RUN_MODE = RUN_MODE_BERR_AERR_RESET;
 
-       /* Note: This is implemented for 68000 only! */
-       m68ki_stack_frame_buserr(sr);
+       CPU_RUN_MODE = RUN_MODE_BERR_AERR_RESET_WSF;
+
+       if (CPU_TYPE_IS_000(CPU_TYPE))
+       {
+               /* Note: This is implemented for 68000 only! */
+               m68ki_stack_frame_buserr(sr);
+       }
+       else if (CPU_TYPE_IS_010(CPU_TYPE))
+       {
+               /* only the 68010 throws this unique type-1000 frame */
+               m68ki_stack_frame_1000(REG_PPC, sr, EXCEPTION_BUS_ERROR);
+       }
+       else if (m68ki_cpu.mmu_tmp_buserror_address == REG_PPC)
+       {
+               m68ki_stack_frame_1010(sr, EXCEPTION_BUS_ERROR, REG_PPC, m68ki_cpu.mmu_tmp_buserror_address);
+       }
+       else
+       {
+               m68ki_stack_frame_1011(sr, EXCEPTION_BUS_ERROR, REG_PPC, m68ki_cpu.mmu_tmp_buserror_address);
+       }
 
        m68ki_jump_vector(EXCEPTION_ADDRESS_ERROR);
 
+       m68ki_cpu.run_mode = RUN_MODE_BERR_AERR_RESET;
+
        /* Use up some clock cycles. Note that we don't need to undo the
        instruction's cycles here as we've longjmp:ed directly from the
        instruction handler without passing the part of the excecute loop