X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=cpu%2FCyclone%2FOpMove.cpp;h=78adae5bc52dffd709e51d72a8064ec3b39c5234;hb=f3a57b2d6c6a32dc09a6efd4fbef31eb876263b2;hp=b0916ea6a6659dec9a6db54361fb79bc9e4c39e2;hpb=60305cdd4d832c9a0b69914a78d4668a1b4d2b48;p=picodrive.git diff --git a/cpu/Cyclone/OpMove.cpp b/cpu/Cyclone/OpMove.cpp index b0916ea..78adae5 100644 --- a/cpu/Cyclone/OpMove.cpp +++ b/cpu/Cyclone/OpMove.cpp @@ -7,7 +7,7 @@ void OpFlagsToReg(int high) { ot(" ldr r0,[r7,#0x4c] ;@ X bit\n"); - ot(" mov r1,r9,lsr #28 ;@ ____NZCV\n"); + ot(" mov r1,r10,lsr #28 ;@ ____NZCV\n"); ot(" eor r2,r1,r1,ror #1 ;@ Bit 0=C^V\n"); ot(" tst r2,#1 ;@ 1 if C!=V\n"); ot(" eorne r1,r1,#3 ;@ ____NZVC\n"); @@ -21,55 +21,55 @@ void OpFlagsToReg(int high) // Convert SR/CRR register in r0 to our flags // trashes r0,r1 -void OpRegToFlags(int high) +void OpRegToFlags(int high, int srh_reg) { ot(" eor r1,r0,r0,ror #1 ;@ Bit 0=C^V\n"); ot(" mov r2,r0,lsl #25\n"); ot(" tst r1,#1 ;@ 1 if C!=V\n"); ot(" eorne r0,r0,#3 ;@ ___XNZCV\n"); ot(" str r2,[r7,#0x4c] ;@ Store X bit\n"); - ot(" mov r9,r0,lsl #28 ;@ r9=NZCV...\n"); + ot(" mov r10,r0,lsl #28 ;@ r10=NZCV...\n"); if (high) { - ot(" mov r0,r0,ror #8\n"); - ot(" and r0,r0,#0xa7 ;@ only take defined bits\n"); - ot(" strb r0,[r7,#0x44] ;@ Store SR high\n"); + int mask=EMULATE_TRACE?0xa7:0x27; + ot(" mov r%i,r0,ror #8\n",srh_reg); + ot(" and r%i,r%i,#0x%02x ;@ only take defined bits\n",srh_reg,srh_reg,mask); + ot(" strb r%i,[r7,#0x44] ;@ Store SR high\n",srh_reg); } ot("\n"); } -// checks for supervisor bit, if not set, jumps to SuperEnd() -// also sets r11 to SR high value, SuperChange() uses this -void SuperCheck(int op) -{ - ot(" ldr r11,[r7,#0x44] ;@ Get SR high\n"); - ot(" tst r11,#0x20 ;@ Check we are in supervisor mode\n"); - ot(" beq WrongPrivilegeMode ;@ No\n"); - ot("\n"); -} - void SuperEnd(void) { ot(";@ ----------\n"); ot(";@ tried execute privileged instruction in user mode\n"); ot("WrongPrivilegeMode%s\n",ms?"":":"); +#if EMULATE_ADDRESS_ERRORS_JUMP || EMULATE_ADDRESS_ERRORS_IO + ot(" ldr r1,[r7,#0x58]\n"); + ot(" sub r4,r4,#2 ;@ last opcode wasn't executed - go back\n"); + ot(" orr r1,r1,#4 ;@ set activity bit: 'not processing instruction'\n"); + ot(" str r1,[r7,#0x58]\n"); +#else ot(" sub r4,r4,#2 ;@ last opcode wasn't executed - go back\n"); - ot(" mov r0,#0x20 ;@ privilege violation\n"); +#endif + ot(" mov r0,#8 ;@ privilege violation\n"); ot(" bl Exception\n"); Cycles=34; - OpEnd(0x10); + OpEnd(0); } // does OSP and A7 swapping if needed // new or old SR (not the one already in [r7,#0x44]) should be passed in r11 -// trashes r0,r11 -void SuperChange(int op,int load_srh) +// uses srh from srh_reg (loads if < 0), trashes r0,r11 +void SuperChange(int op,int srh_reg) { ot(";@ A7 <-> OSP?\n"); - if (load_srh) + if (srh_reg < 0) { ot(" ldr r0,[r7,#0x44] ;@ Get other SR high\n"); - ot(" eor r0,r0,r11\n"); + srh_reg=0; + } + ot(" eor r0,r%i,r11\n",srh_reg); ot(" tst r0,#0x20\n"); ot(" beq no_sp_swap%.4x\n",op); ot(" ;@ swap OSP and A7:\n"); @@ -121,24 +121,27 @@ int OpMove(int op) OpStart(op,sea,tea); Cycles=4; - EaCalcRead(-1,1,sea,size,0x003f); - if (movea==0) { - ot(" adds r1,r1,#0 ;@ Defines NZ, clears CV\n"); - ot(" mrs r9,cpsr ;@ r9=NZCV flags\n"); + EaCalcRead(-1,0,sea,size,0x003f); + ot(" adds r1,r0,#0 ;@ Defines NZ, clears CV\n"); + ot(" mrs r10,cpsr ;@ r10=NZCV flags\n"); ot("\n"); } + else + { + EaCalcRead(-1,1,sea,size,0x003f); + size=2; // movea always expands to 32-bits + } - if (movea) size=2; // movea always expands to 32-bits - + eawrite_check_addrerr=1; #if SPLIT_MOVEL_PD if ((tea&0x38)==0x20 && size==2) { // -(An) - EaCalc (10,0x0e00,tea,size,0,0); + EaCalc (8,0x0e00,tea,size,0,0); ot(" mov r11,r1\n"); - ot(" add r0,r10,#2\n"); + ot(" add r0,r8,#2\n"); EaWrite(0, 1,tea,1,0x0e00,0,0); - EaWrite(10, 11,tea,1,0x0e00,1); + EaWrite(8, 11,tea,1,0x0e00,1); } else #endif @@ -148,8 +151,8 @@ int OpMove(int op) } #if CYCLONE_FOR_GENESIS && !MEMHANDLERS_CHANGE_CYCLES - // this is a bit hacky - if ((tea==0x39||(tea&0x38)==0x10)&&size>=1) + // this is a bit hacky (device handlers might modify cycles) + if (tea==0x39||((0x10<=tea&&tea<0x30)&&size>=1)) ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n"); #endif @@ -177,6 +180,7 @@ int OpLea(int op) OpStart(op,sea,tea); + eawrite_check_addrerr=1; EaCalc (1,0x003f,sea,0); // Lea EaCalc (0,0x0e00,tea,2); EaWrite(0, 1,tea,2,0x0e00); @@ -217,14 +221,14 @@ int OpMoveSr(int op) use=OpBase(op,size); if (op!=use) { OpUse(op,use); return 0; } // Use existing handler - OpStart(op,ea); + // 68000 model allows reading whole SR in user mode (but newer models don't) + OpStart(op,ea,0,0,type==3); Cycles=12; if (type==0) Cycles=(ea>=8)?8:6; - if (type==3) SuperCheck(op); // 68000 model allows reading whole SR in user mode (but newer models don't) - if (type==0 || type==1) { + eawrite_check_addrerr=1; OpFlagsToReg(type==0); EaCalc (0,0x003f,ea,size,0,0); EaWrite(0, 1,ea,size,0x003f,0,0); @@ -233,10 +237,13 @@ int OpMoveSr(int op) if (type==2 || type==3) { EaCalcReadNoSE(-1,0,ea,size,0x003f); - OpRegToFlags(type==3); + OpRegToFlags(type==3,1); if (type==3) { - SuperChange(op,0); - CheckInterrupt(op); + SuperChange(op,1); + opend_check_interrupt = 1; + opend_check_trace = 1; + OpEnd(ea); + return 0; } } @@ -251,6 +258,7 @@ int OpArithSr(int op) { int type=0,ea=0; int use=0,size=0; + int sr_mask=EMULATE_TRACE?0xa7:0x27; type=(op>>9)&5; if (type==4) return 1; size=(op>>6)&1; // ccr or sr? @@ -259,22 +267,53 @@ int OpArithSr(int op) use=OpBase(op,size); if (op!=use) { OpUse(op,use); return 0; } // Use existing handler - OpStart(op,ea); Cycles=16; + OpStart(op,ea,0,0,size!=0); Cycles=16; - if (size) SuperCheck(op); + EaCalcRead(-1,0,ea,size,0x003f); - EaCalc(10,0x003f,ea,size); - EaRead(10, 10,ea,size,0x003f); + ot(" eor r1,r0,r0,ror #1 ;@ Bit 0=C^V\n"); + ot(" tst r1,#1 ;@ 1 if C!=V\n"); + ot(" eorne r0,r0,#3 ;@ ___XNZCV\n"); + ot(" ldr r2,[r7,#0x4c] ;@ Load old X bit\n"); + + // note: old srh is already in r11 (done by OpStart) + if (type==0) { + ot(" orr r10,r10,r0,lsl #28\n"); + ot(" orr r2,r2,r0,lsl #25 ;@ X bit\n"); + if (size!=0) { + ot(" orr r1,r11,r0,lsr #8\n"); + ot(" and r1,r1,#0x%02x ;@ mask-out unused bits\n",sr_mask); + } + } + if (type==1) { + ot(" and r10,r10,r0,lsl #28\n"); + ot(" and r2,r2,r0,lsl #25 ;@ X bit\n"); + if (size!=0) + ot(" and r1,r11,r0,lsr #8\n"); + } + if (type==5) { + ot(" eor r10,r10,r0,lsl #28\n"); + ot(" eor r2,r2,r0,lsl #25 ;@ X bit\n"); + if (size!=0) { + ot(" eor r1,r11,r0,lsr #8\n"); + ot(" and r1,r1,#0x%02x ;@ mask-out unused bits\n",sr_mask); + } + } - OpFlagsToReg(size); - if (type==0) ot(" orr r0,r1,r10\n"); - if (type==1) ot(" and r0,r1,r10\n"); - if (type==5) ot(" eor r0,r1,r10\n"); - OpRegToFlags(size); - if (size) { - SuperChange(op,0); - CheckInterrupt(op); + ot(" str r2,[r7,#0x4c] ;@ Save X bit\n"); + if (size!=0) + ot(" strb r1,[r7,#0x44]\n"); + ot("\n"); + + // we can't enter supervisor mode, nor unmask irqs just by using OR + if (size!=0 && type!=0) { + SuperChange(op,1); + ot("\n"); + opend_check_interrupt = 1; } + // also can't set trace bit with AND + if (size!=0 && type!=1) + opend_check_trace = 1; OpEnd(ea); @@ -296,10 +335,10 @@ int OpPea(int op) OpStart(op,ea); - ot(" ldr r10,[r7,#0x3c]\n"); + ot(" ldr r11,[r7,#0x3c]\n"); EaCalc (1,0x003f, ea,0); ot("\n"); - ot(" sub r0,r10,#4 ;@ Predecrement A7\n"); + ot(" sub r0,r11,#4 ;@ Predecrement A7\n"); ot(" str r0,[r7,#0x3c] ;@ Save A7\n"); ot("\n"); MemHandler(1,2); // Write 32-bit @@ -337,25 +376,36 @@ int OpMovem(int op) use=OpBase(op,size); if (op!=use) { OpUse(op,use); return 0; } // Use existing handler - OpStart(op,ea); + OpStart(op,ea,0,1); ot(" ldrh r11,[r4],#2 ;@ r11=register mask\n"); - - ot(";@ r10=Register Index*4:\n"); - if (decr) ot(" mov r10,#0x40 ;@ order reversed for -(An)\n"); - else ot(" mov r10,#-4\n"); - ot("\n"); ot(";@ Get the address into r6:\n"); EaCalc(6,0x003f,cea,size); +#if !MEMHANDLERS_NEED_PREV_PC + // must save PC, need a spare register + ot(" str r4,[r7,#0x40] ;@ Save PC\n"); +#endif + + ot(";@ r4=Register Index*4:\n"); + if (decr) ot(" mov r4,#0x40 ;@ order reversed for -(An)\n"); + else ot(" mov r4,#-4\n"); + ot("\n"); ot(" tst r11,r11\n"); // sanity check ot(" beq NoRegs%.4x\n",op); +#if EMULATE_ADDRESS_ERRORS_IO + ot("\n"); + ot(" tst r6,#1 ;@ address error?\n"); + ot(" movne r0,r6\n"); + ot(" bne ExceptionAddressError_%c_data\n",dir?'r':'w'); +#endif + ot("\n"); ot("Movemloop%.4x%s\n",op, ms?"":":"); - ot(" add r10,r10,#%d ;@ r10=Next Register\n",decr?-4:4); + ot(" add r4,r4,#%d ;@ r4=Next Register\n",decr?-4:4); ot(" movs r11,r11,lsr #1\n"); ot(" bcc Movemloop%.4x\n",op); ot("\n"); @@ -365,14 +415,27 @@ int OpMovem(int op) if (dir) { ot(" ;@ Copy memory to register:\n",1<S\n"); - ot(" str r1,[r7,r10,lsr #7] ;@ S->T\n"); + ot(" str r0,[r7,r3,lsl #2] ;@ T->S\n"); + ot(" str r1,[r7,r2,lsr #7] ;@ S->T\n"); ot("\n"); OpEnd(); @@ -521,44 +584,48 @@ int OpMovep(int op) OpStart(op,ea); - if(dir) { // reg to mem + if(dir) // reg to mem + { EaCalcReadNoSE(-1,11,rea,size,0x0e00); - EaCalc(10,0x000f,ea,size); + EaCalc(8,0x000f,ea,size); if(size==2) { // if operand is long ot(" mov r1,r11,lsr #24 ;@ first byte\n"); - EaWrite(10,1,ea,0,0x000f); // store first byte - ot(" add r0,r10,#%i\n",(aadd+=2)); + EaWrite(8,1,ea,0,0x000f); // store first byte + ot(" add r0,r8,#%i\n",(aadd+=2)); ot(" mov r1,r11,lsr #16 ;@ second byte\n"); EaWrite(0,1,ea,0,0x000f); // store second byte - ot(" add r0,r10,#%i\n",(aadd+=2)); + ot(" add r0,r8,#%i\n",(aadd+=2)); } else { - ot(" mov r0,r10\n"); + ot(" mov r0,r8\n"); } ot(" mov r1,r11,lsr #8 ;@ first or third byte\n"); EaWrite(0,1,ea,0,0x000f); - ot(" add r0,r10,#%i\n",(aadd+=2)); + ot(" add r0,r8,#%i\n",(aadd+=2)); ot(" and r1,r11,#0xff\n"); EaWrite(0,1,ea,0,0x000f); - } else { // mem to reg - EaCalc(10,0x000f,ea,size,1); - EaRead(10,11,ea,0,0x000f,1); // read first byte - ot(" add r0,r10,#2\n"); + } + else // mem to reg + { + EaCalc(6,0x000f,ea,size,1); + EaRead(6,11,ea,0,0x000f,1); // read first byte + ot(" add r0,r6,#2\n"); EaRead(0,1,ea,0,0x000f,1); // read second byte if(size==2) { // if operand is long ot(" orr r11,r11,r1,lsr #8 ;@ second byte\n"); - ot(" add r0,r10,#4\n"); + ot(" add r0,r6,#4\n"); EaRead(0,1,ea,0,0x000f,1); ot(" orr r11,r11,r1,lsr #16 ;@ third byte\n"); - ot(" add r0,r10,#6\n"); + ot(" add r0,r6,#6\n"); EaRead(0,1,ea,0,0x000f,1); ot(" orr r1,r11,r1,lsr #24 ;@ fourth byte\n"); } else { ot(" orr r1,r11,r1,lsr #8 ;@ second byte\n"); } // store the result - EaCalc(11,0x0e00,rea,size,1); // reg number -> r11 - EaWrite(11,1,rea,size,0x0e00,1); + EaCalc(0,0x0e00,rea,size,1); + EaWrite(0,1,rea,size,0x0e00,1); + ot(" ldr r6,[r7,#0x54]\n"); } Cycles=(size==2)?24:16; @@ -572,9 +639,7 @@ int OpStopReset(int op) { int type=(op>>1)&1; // stop/reset - OpStart(op); - - SuperCheck(op); + OpStart(op,0,0,0,1); if(type) { // copy immediate to SR, stop the CPU and eat all remaining cycles. @@ -584,11 +649,12 @@ int OpStopReset(int op) ot("\n"); - ot(" mov r0,#1\n"); - ot(" str r0,[r7,#0x58] ;@ stopped\n"); + ot(" ldr r0,[r7,#0x58]\n"); + ot(" mov r5,#0 ;@ eat cycles\n"); + ot(" orr r0,r0,#1 ;@ stopped\n"); + ot(" str r0,[r7,#0x58]\n"); ot("\n"); - ot(" mov r5,#0 ;@ eat cycles\n"); Cycles = 4; ot("\n"); } @@ -597,17 +663,17 @@ int OpStopReset(int op) Cycles = 132; #if USE_RESET_CALLBACK ot(" str r4,[r7,#0x40] ;@ Save PC\n"); - ot(" mov r1,r9,lsr #28\n"); + ot(" mov r1,r10,lsr #28\n"); ot(" strb r1,[r7,#0x46] ;@ Save Flags (NZCV)\n"); ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n"); ot(" ldr r11,[r7,#0x90] ;@ ResetCallback\n"); ot(" tst r11,r11\n"); ot(" movne lr,pc\n"); ot(" bxne r11 ;@ call ResetCallback if it is defined\n"); - ot(" ldrb r9,[r7,#0x46] ;@ r9 = Load Flags (NZCV)\n"); + ot(" ldrb r10,[r7,#0x46] ;@ r10 = Load Flags (NZCV)\n"); ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n"); ot(" ldr r4,[r7,#0x40] ;@ Load PC\n"); - ot(" mov r9,r9,lsl #28\n"); + ot(" mov r10,r10,lsl #28\n"); ot("\n"); #endif }