X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=cpu%2FCyclone%2FMain.cpp;h=5647fb1d3accc1a6e756856f1bef87983a46aa97;hb=2270612aa7ee7d04549e95c2ab4d72f6aa67fa8d;hp=a85aebcef924414327e2219d338b3c877c6304e7;hpb=c008977e8ec74b68cbccc017620a156cc39b79b2;p=picodrive.git diff --git a/cpu/Cyclone/Main.cpp b/cpu/Cyclone/Main.cpp index a85aebc..5647fb1 100644 --- a/cpu/Cyclone/Main.cpp +++ b/cpu/Cyclone/Main.cpp @@ -3,12 +3,14 @@ static FILE *AsmFile=NULL; -static int CycloneVer=0x0086; // Version number of library +static int CycloneVer=0x0087; // Version number of library int *CyJump=NULL; // Jump table int ms=USE_MS_SYNTAX; // If non-zero, output in Microsoft ARMASM format char *Narm[4]={ "b", "h","",""}; // Normal ARM Extensions for operand sizes 0,1,2 char *Sarm[4]={"sb","sh","",""}; // Sign-extend ARM Extensions for operand sizes 0,1,2 int Cycles; // Current cycles for opcode +int pc_dirty; // something changed PC during processing +static int arm_op_count; void ot(const char *format, ...) @@ -21,6 +23,9 @@ void ot(const char *format, ...) for(i=0, len=strlen(format); i < len && format[i] != '\n'; i++); if(i < len-1 && format[len-1] != '\n') printf("\nWARNING: possible improper newline placement:\n%s\n", format); + if (format[0] == ' ' && format[1] == ' ' && format[2] != ' ' && format[2] != '.') + arm_op_count++; + va_start(valist,format); if (AsmFile) vfprintf(AsmFile,format,valist); va_end(valist); @@ -32,7 +37,7 @@ void ltorg() else ot(" .ltorg\n"); } -#if CYCLONE_FOR_GENESIS +#if (CYCLONE_FOR_GENESIS == 2) // r12=ptr to tas in table, trashes r0,r1 static void ChangeTAS(int norm) { @@ -97,9 +102,10 @@ static void PrintException(int ints) ot("no_sp_swap%i%s\n",ints,ms?"":":"); ot(" ldr r10,[r7,#0x60] ;@ Get Memory base\n"); - ot(" mov r1,r4,lsl #8\n"); - ot(" sub r1,r1,r10,lsl #8 ;@ r1 = Old PC\n"); - ot(" mov r1,r1,asr #8 ;@ push sign extended\n"); +// ot(" mov r1,r4,lsl #8\n"); +// ot(" sub r1,r1,r10,lsl #8 ;@ r1 = Old PC\n"); +// ot(" mov r1,r1,asr #8 ;@ push sign extended\n"); + ot(" sub r1,r4,r10 ;@ r1 = Old PC\n"); OpPush32(); OpPushSr(1); ot(" mov r0,r11\n"); @@ -142,6 +148,15 @@ void CheckInterrupt(int op) ot("\n"); } +void FlushPC(void) +{ +#if MEMHANDLERS_NEED_PC + if (pc_dirty) + ot(" str r4,[r7,#0x40] ;@ Save PC\n"); +#endif + pc_dirty = 0; +} + static void PrintFramework() { ot(";@ --------------------------- Framework --------------------------\n"); @@ -186,7 +201,7 @@ static void PrintFramework() ot("CycloneEnd%s\n", ms?"":":"); ot(" sub r4,r4,#2\n"); ot("CycloneEndNoBack%s\n", ms?"":":"); -#ifdef CYCLONE_FOR_PICODRIVE +#if (CYCLONE_FOR_GENESIS == 2) ot(" ldr r1,[r7,#0x54]\n"); ot(" mov r9,r9,lsr #28\n"); ot(" tst r1,r1\n"); @@ -216,7 +231,7 @@ static void PrintFramework() ot(" ldr r1,[r0,#-4]\n"); ot(" tst r1,r1\n"); ot(" movne pc,lr ;@ already uncompressed\n"); - ot(" add r3,r12,#0xa000*4 ;@ handler table pointer, r12=dest\n"); + ot(" add r3,r12,#0xa000*4 ;@ handler table pointer, r12=dest\n"); ot("unc_loop%s\n", ms?"":":"); ot(" ldrh r1,[r0],#2\n"); ot(" and r2,r1,#0xf\n"); @@ -263,6 +278,7 @@ static void PrintFramework() ot(" bx lr\n"); ot("\n"); #endif + // 68k: XNZVC, ARM: NZCV if (ms) ot("CycloneSetSr\n"); else ot("CycloneSetSr:\n"); ot(" mov r2,r1,lsr #8\n"); @@ -277,8 +293,8 @@ static void PrintFramework() ot(" str r3,[r0,#0x3C]\n"); ot(" str r2,[r0,#0x48]\n"); ot("setsr_noswap%s\n",ms?"":":"); - ot(" mov r2,r1,lsr #3\n"); - ot(" strb r2,[r0,#0x45] ;@ the X flag\n"); + ot(" mov r2,r1,lsl #25\n"); + ot(" str r2,[r0,#0x4c] ;@ the X flag\n"); ot(" bic r2,r1,#0xf3\n"); ot(" tst r1,#1\n"); ot(" orrne r2,r2,#2\n"); @@ -296,8 +312,8 @@ static void PrintFramework() ot(" orrne r2,r2,#2\n"); ot(" tst r1,#2\n"); ot(" orrne r2,r2,#1\n"); - ot(" ldrb r1,[r0,#0x45] ;@ the X flag\n"); - ot(" tst r1,#2\n"); + ot(" ldr r1,[r0,#0x4c] ;@ the X flag\n"); + ot(" tst r1,#0x20000000\n"); ot(" orrne r2,r2,#0x10\n"); ot(" ldrb r1,[r0,#0x44] ;@ the SR high\n"); ot(" orr r0,r2,r1,lsl #8\n"); @@ -306,7 +322,7 @@ static void PrintFramework() if (ms) ot("CycloneSetRealTAS\n"); else ot("CycloneSetRealTAS:\n"); -#if CYCLONE_FOR_GENESIS +#if (CYCLONE_FOR_GENESIS == 2) ot(" ldr r12,=CycloneJumpTab\n"); ot(" tst r0,r0\n"); ot(" add r12,r12,#0x4a00*4\n"); @@ -367,7 +383,6 @@ static void PrintFramework() ot("\n"); ot("Exception%s\n", ms?"":":"); - ot("\n"); ot(" stmdb sp!,{lr} ;@ Preserve ARM return address\n"); PrintException(0); ot(" ldmia sp!,{pc} ;@ Return\n"); @@ -377,23 +392,40 @@ static void PrintFramework() // --------------------------------------------------------------------------- // Call Read(r0), Write(r0,r1) or Fetch(r0) // Trashes r0-r3,r12,lr -int MemHandler(int type,int size) +int MemHandler(int type,int size,int addrreg) { int func=0; func=0x68+type*0xc+(size<<2); // Find correct offset -#if MEMHANDLERS_NEED_PC - ot(" str r4,[r7,#0x40] ;@ Save PC\n"); -#endif #if MEMHANDLERS_NEED_FLAGS ot(" mov r3,r9,lsr #28\n"); ot(" strb r3,[r7,#0x46] ;@ Save Flags (NZCV)\n"); #endif -#if MEMHANDLERS_NEED_CYCLES - ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n"); -#endif + FlushPC(); - ot(" mov lr,pc\n"); +#if (MEMHANDLERS_ADDR_MASK & 0xff000000) + ot(" bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0xff000000); + addrreg=0; +#endif +#if (MEMHANDLERS_ADDR_MASK & 0x00ff0000) + ot(" bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0x00ff0000); + addrreg=0; +#endif +#if (MEMHANDLERS_ADDR_MASK & 0x0000ff00) + ot(" bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0x0000ff00); + addrreg=0; +#endif +#if (MEMHANDLERS_ADDR_MASK & 0x000000ff) + ot(" bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0x000000ff); + addrreg=0; +#endif + if (addrreg != 0) + { + ot(" add lr,pc,#4\n"); // helps to prevent interlocks + ot(" mov r0,r%i\n", addrreg); + } + else + ot(" mov lr,pc\n"); ot(" ldr pc,[r7,#0x%x] ;@ Call ",func); // Document what we are calling: @@ -405,9 +437,6 @@ int MemHandler(int type,int size) else ot("%d(r0)", 8<=0; u--) if(op == CyJump[u]) break; // already done with this op? - if(u==-1 && op >= 0) { - ott("Op%.4x",op," ;@ %.4x\n",i,handlers,2); - indexes[op] = handlers; - handlers++; + for(u=i-1; u>=0; u--) if(op == CyJump[u]) break; // already done with this op? + if(u==-1 && op >= 0) { + ott("Op%.4x",op," ;@ %.4x\n",i,handlers,2); + indexes[op] = handlers; + handlers++; } - } - if(handlers&7) { - fseek(AsmFile, -1, SEEK_CUR); // remove last comma - for(i = 8-(handlers&7); i > 0; i--) - ot(",000000"); - ot("\n"); - } - if(ms) { - for(i = (0x4000-handlers)/8; i > 0; i--) - ot(" dcd 0,0,0,0,0,0,0,0\n"); - } else { - ot(ms?"":" .rept 0x%x\n .long 0,0,0,0,0,0,0,0\n .endr\n", (0x4000-handlers)/8); - } + } + if(handlers&7) { + fseek(AsmFile, -1, SEEK_CUR); // remove last comma + for(i = 8-(handlers&7); i > 0; i--) + ot(",000000"); + ot("\n"); + } + if(ms) { + for(i = (0x4000-handlers)/8; i > 0; i--) + ot(" dcd 0,0,0,0,0,0,0,0\n"); + } else { + ot(ms?"":" .rept 0x%x\n .long 0,0,0,0,0,0,0,0\n .endr\n", (0x4000-handlers)/8); + } printf("total distinct hanlers: %i\n",handlers); - // output data - for(i=0,ip=0; i < 0xf000; i++, ip++) { + // output data + for(i=0,ip=0; i < 0xf000; i++, ip++) { op=CyJump[i]; - if(op == -2) { - // it must skip a-line area, because we keep our data there - ott("0x%.4x", handlers<<4, "\n",0,ip++,1); - ott("0x%.4x", 0x1000, "\n",0,ip,1); - i+=0xfff; - continue; - } - for(reps=1; i < 0xf000; i++, reps++) if(op != CyJump[i+1]) break; - if(op>=0) out=indexes[op]<<4; else out=0; // unrecognised - if(reps <= 0xe || reps==0x10) { - if(reps!=0x10) out|=reps; else out|=0xf; // 0xf means 0x10 (0xf appeared to be unused anyway) - ott("0x%.4x", out, "\n",0,ip,1); + if(op == -2) { + // it must skip a-line area, because we keep our data there + ott("0x%.4x", handlers<<4, "\n",0,ip++,1); + ott("0x%.4x", 0x1000, "\n",0,ip,1); + i+=0xfff; + continue; + } + for(reps=1; i < 0xf000; i++, reps++) if(op != CyJump[i+1]) break; + if(op>=0) out=indexes[op]<<4; else out=0; // unrecognised + if(reps <= 0xe || reps==0x10) { + if(reps!=0x10) out|=reps; else out|=0xf; // 0xf means 0x10 (0xf appeared to be unused anyway) + ott("0x%.4x", out, "\n",0,ip,1); } else { - ott("0x%.4x", out, "\n",0,ip++,1); - ott("0x%.4x", reps,"\n",0,ip,1); - } + ott("0x%.4x", out, "\n",0,ip++,1); + ott("0x%.4x", reps,"\n",0,ip,1); + } } - if(ip&1) ott("0x%.4x", 0, "\n",0,ip++,1); - if(ip&7) fseek(AsmFile, -1, SEEK_CUR); // remove last comma - ot("\n"); - if(ip&7) { - for(i = 8-(ip&7); i > 0; i--) - ot(",0x0000"); - ot("\n"); - } - if(ms) { - for(i = (0x2000-ip/2)/8+1; i > 0; i--) - ot(" dcd 0,0,0,0,0,0,0,0\n"); - } else { - ot(" .rept 0x%x\n .long 0,0,0,0,0,0,0,0\n .endr\n", (0x2000-ip/2)/8+1); - } - ot("\n"); - free(indexes); + if(ip&1) ott("0x%.4x", 0, "\n",0,ip++,1); + if(ip&7) fseek(AsmFile, -1, SEEK_CUR); // remove last comma + if(ip&7) { + for(i = 8-(ip&7); i > 0; i--) + ot(",0x0000"); + } + ot("\n"); + if(ms) { + for(i = (0x2000-ip/2)/8+1; i > 0; i--) + ot(" dcd 0,0,0,0,0,0,0,0\n"); + } else { + ot(" .rept 0x%x\n .long 0,0,0,0,0,0,0,0\n .endr\n", (0x2000-ip/2)/8+1); + } + ot("\n"); + free(indexes); #else - ot("CycloneJumpTab%s\n", ms?"":":"); + ot("CycloneJumpTab%s\n", ms?"":":"); len=0xfffe; // Hmmm, armasm 2.50.8684 messes up with a 0x10000 long jump table // notaz: same thing with GNU as 2.9-psion-98r2 (reloc overflow) // this is due to COFF objects using only 2 bytes for reloc count @@ -632,7 +663,7 @@ static void PrintJumpTable() else if(op==-3) ott("Op__fl",0, " ;@ %.4x\n",i-7,i,2); else ott("Op____",0, " ;@ %.4x\n",i-7,i,2); } - if(i&7) fseek(AsmFile, -1, SEEK_CUR); // remove last comma + if(i&7) fseek(AsmFile, -1, SEEK_CUR); // remove last comma ot("\n"); ot(";@ notaz: we don't want to crash if we run into those 2 missing opcodes\n"); @@ -656,7 +687,7 @@ static int CycloneMake() ot("\n;@ Dave's Cyclone 68000 Emulator v%x.%.3x - Assembler Output\n\n",CycloneVer>>12,CycloneVer&0xfff); ot(";@ (c) Copyright 2003 Dave, All rights reserved.\n"); - ot(";@ some code (c) Copyright 2005-2006 notaz, All rights reserved.\n"); + ot(";@ some code (c) Copyright 2005-2007 notaz, All rights reserved.\n"); ot(";@ Cyclone 68000 is free for non-commercial use.\n\n"); ot(";@ For commercial use, separate licencing terms must be obtained.\n\n"); @@ -683,9 +714,9 @@ static int CycloneMake() ot(" .global CycloneRun\n"); ot(" .global CycloneSetSr\n"); ot(" .global CycloneGetSr\n"); - ot(" .global CycloneSetRealTAS\n"); ot(" .global CycloneVer\n"); -#ifdef CYCLONE_FOR_PICODRIVE +#if (CYCLONE_FOR_GENESIS == 2) + ot(" .global CycloneSetRealTAS\n"); ot(" .global CycloneDoInterrupt\n"); ot(" .global CycloneJumpTab\n"); #endif @@ -694,7 +725,9 @@ static int CycloneMake() ot("\n"); PrintFramework(); + arm_op_count = 0; PrintOpcodes(); + printf("~%i ARM instructions used for opcode handlers\n", arm_op_count); PrintJumpTable(); if (ms) ot(" END\n");