⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rom.cpp

📁 一个gba的模拟器源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    asm jmp sprcmp_ret
sprcmp_notEqu:
    asm jl sprcmp_greater
sprcmp_less:
    asm mov eax,-1
    asm jmp sprcmp_ret
sprcmp_greater:
    asm mov eax,1
sprcmp_ret:
}*/

void RenderLine()
{
    if (lcdY>=144) return;
    if (!displayEnabled)
    {
        if (scaleFact==1)
        {
            char *ptr0=ScreenLine[lcdY];
            int value=Filter[0x7fff]|(Filter[0x7fff]<<16);
            asm mov edi,ptr0
            asm mov eax,value
            asm mov ecx,80
            asm rep stosd
        }
        else
        {
            char *ptr0=ScreenLine[lcdY<<1];
            char *ptr1=ScreenLine[(lcdY<<1)+1];
            int value=Filter[0x7fff]|(Filter[0x7fff]<<16);
            asm mov edi,ptr0
            asm mov eax,value
            asm mov ecx,160
            asm rep stosd
            asm mov edi,ptr1
            asm mov eax,value
            asm mov ecx,160
            asm rep stosd
        }
        return;
    }
    unsigned short v[160],bc[160];
    char bpri[160];
//    memset(v,0,320);
//    memset(bc,0,320);
//    if (hiRam[0x40]&1)
    {
        int bkty=((hiRam[0x42]+lcdY)&0xff)>>3;
        int bkyo=(hiRam[0x42]+lcdY)&7;
        int bktx=hiRam[0x43]>>3;
        int bkxo=hiRam[0x43]&7;
        int vidRamBase=((hiRam[0x40]&8)?0x1c00:0x1800)+(bkty<<5);
        int tileBase=((hiRam[0x40]&16)?0:0x1000);
        for (int i=0;i<160;)
        {
            int ti=vidRam[vidRamBase+bktx];
            int pn=vidRam[vidRamBase+0x2000+bktx];
            int attr=pn&0xf8;
            pn&=7;
            if ((!(hiRam[0x40]&16))&&(ti>=0x80))
                ti=-(0x100-ti);
            int taddr=tileBase+(ti*16);
            if (attr&8) taddr+=0x2000;
            int v0=vidRam[taddr+(((attr&0x40)?(7-bkyo):bkyo)<<1)];
            int v1=vidRam[taddr+(((attr&0x40)?(7-bkyo):bkyo)<<1)+1];
            for (;(bkxo<8)&&(i<160);)
            {
                int c;
                if (attr&0x20)
                {
                    c=(v0&(0x80>>(7-bkxo)))?1:0;
                    c|=(v1&(0x80>>(7-bkxo)))?2:0;
                }
                else
                {
                    c=(v0&(0x80>>bkxo))?1:0;
                    c|=(v1&(0x80>>bkxo))?2:0;
                }
                bpri[i]=((attr&0x80)&&(c));
                bc[i]=c;
                v[i]=bgPal[(pn<<2)+c];
//                r[i]=(bgPal[(pn<<2)+c]&31)<<3;
//                g[i]=((bgPal[(pn<<2)+c]>>5)&31)<<3;
//                b[i]=((bgPal[(pn<<2)+c]>>10)&31)<<3;
                bkxo++; i++;
            }
            bkxo=0; bktx=(bktx+1)&31;
        }
    }
    static int wy,wyr;
    if (lcdY==0) { wy=hiRam[0x4a]; wyr=0; }
    if ((wy<=lcdY)&&(hiRam[0x4b]<=166)&&
        (hiRam[0x40]&0x20))
    {
        int wty=wyr>>3;
        int wyo=wyr&7;
        wyr++;
        int wtx=0;
        int wxo=0;
        for (int i=(hiRam[0x4b]-7);i<160;)
        {
            if (i<0)
            {
                wxo-=i;
                i=0;
                continue;
            }
            int ti=vidRam[((hiRam[0x40]&0x40)?0x1c00:0x1800)+
                (wty<<5)+wtx];
            int pn=vidRam[((hiRam[0x40]&0x40)?0x3c00:0x3800)+
                (wty<<5)+wtx];
            int attr=pn&0xf8;
            pn&=7;
            if ((!(hiRam[0x40]&16))&&(ti>=0x80))
                ti=-(0x100-ti);
            int taddr=((hiRam[0x40]&16)?0:0x1000)+(ti*16);
            if (attr&8) taddr+=0x2000;
            int v0=vidRam[taddr+(((attr&0x40)?(7-wyo):wyo)<<1)];
            int v1=vidRam[taddr+(((attr&0x40)?(7-wyo):wyo)<<1)+1];
            for (;(wxo<8)&&(i<160);)
            {
                int c;
                if (attr&0x20)
                {
                    c=(v0&(0x80>>(7-wxo)))?1:0;
                    c|=(v1&(0x80>>(7-wxo)))?2:0;
                }
                else
                {
                    c=(v0&(0x80>>wxo))?1:0;
                    c|=(v1&(0x80>>wxo))?2:0;
                }
                bc[i]=c;
                v[i]=bgPal[(pn<<2)+c];
//                r[i]=(bgPal[(pn<<2)+c]&31)<<3;
//                g[i]=((bgPal[(pn<<2)+c]>>5)&31)<<3;
//                b[i]=((bgPal[(pn<<2)+c]>>10)&31)<<3;
                wxo++; i++;
            }
            wxo=0; wtx++;
        }
    }
    if (hiRam[0x40]&2)
    {
        char s[40];
/*        asm lea eax,s
        asm lea ebx,oam
        asm mov ecx,40
       sprCopyLoop:
        asm mov edx,[ebx]
        asm mov [eax],edx
        asm mov [eax+4],cl
        asm add ebx,4
        asm add eax,8
        asm loop sprCopyLoop
        qsort(s,40,8,sprcmp);*/
        if (hiRam[0x40]&4)
        {
            int i,j,k,bit;
            for (i=0,j=0;i<40;i++)
            {
                if ((oam[i<<2]-16)>lcdY)
                    s[i]=0;
                else if ((oam[i<<2]-16+15)<lcdY)
                    s[i]=0;
                else if (oam[(i<<2)+1]>=168)
                {
                    j++;
                    s[i]=0;
                }
                else if (j>=10)
                    s[i]=0;
                else
                {
                    s[i]=1;
                    j++;
                }
            }
            for (i=39;i>=0;i--)
            {
                if (!s[i])
                    continue;
                int saddr=(oam[(i<<2)+2]&(~1))<<4;
                if (oam[(i<<2)+3]&0x40)
                    saddr+=(15+((oam[i<<2]-16)-lcdY))<<1;
                else
                    saddr+=(lcdY-(oam[i<<2]-16))<<1;
                if (oam[(i<<2)+3]&8)
                    saddr+=0x2000;
                int v0=vidRam[saddr];
                int v1=vidRam[saddr+1];
                int pn=(oam[(i<<2)+3]&7)|(cgb?0:((oam[(i<<2)+3]>>4)&1));
                bit=(oam[(i<<2)+3]&0x20)?0:7;
                for (j=oam[(i<<2)+1]-8,k=0;(j<160)&&(k<8);j++,k++,bit+=(oam[(i<<2)+3]&0x20)?1:-1)
                {
                    if (j<0) continue;
                    if (bpri[j]) continue;
                    if ((oam[(i<<2)+3]&0x80)&&(bc[j])) continue;
                    int c=(v0&(1<<bit))?1:0;
                    c|=(v1&(1<<bit))?2:0;
                    if (c==0) continue;
                    v[j]=objPal[(pn<<2)+c];
//                    r[j]=(objPal[(pn<<2)+c]&31)<<3;
//                    g[j]=((objPal[(pn<<2)+c]>>5)&31)<<3;
//                    b[j]=((objPal[(pn<<2)+c]>>10)&31)<<3;
                }
            }
        }
        else
        {
            int i,j,k,bit;
            for (i=0,j=0;i<40;i++)
            {
                if ((oam[i<<2]-16)>lcdY)
                    s[i]=0;
                else if ((oam[i<<2]-16+7)<lcdY)
                    s[i]=0;
                else if (oam[(i<<2)+1]>=168)
                {
                    j++;
                    s[i]=0;
                }
                else if (j>=10)
                    s[i]=0;
                else
                {
                    s[i]=1;
                    j++;
                }
            }
            for (i=39;i>=0;i--)
            {
                if (!s[i])
                    continue;
                int saddr=oam[(i<<2)+2]<<4;
                if (oam[(i<<2)+3]&0x40)
                    saddr+=(7+((oam[i<<2]-16)-lcdY))<<1;
                else
                    saddr+=(lcdY-(oam[i<<2]-16))<<1;
                if (oam[(i<<2)+3]&8)
                    saddr+=0x2000;
                int v0=vidRam[saddr];
                int v1=vidRam[saddr+1];
                int pn=(oam[(i<<2)+3]&7)|(cgb?0:((oam[(i<<2)+3]>>4)&1));
                bit=(oam[(i<<2)+3]&0x20)?0:7;
                for (j=oam[(i<<2)+1]-8,k=0;(j<160)&&(k<8);j++,k++,bit+=(oam[(i<<2)+3]&0x20)?1:-1)
                {
                    if (j<0) continue;
                    if (bpri[j]) continue;
                    if ((oam[(i<<2)+3]&0x80)&&(bc[j])) continue;
                    int c=(v0&(1<<bit))?1:0;
                    c|=(v1&(1<<bit))?2:0;
                    if (c==0) continue;
                    v[j]=objPal[(pn<<2)+c];
//                    r[j]=(objPal[(pn<<2)+c]&31)<<3;
//                    g[j]=((objPal[(pn<<2)+c]>>5)&31)<<3;
//                    b[j]=((objPal[(pn<<2)+c]>>10)&31)<<3;
                }
            }
        }
    }
    if (scaleFact==1)
    {
        char *ptr0=ScreenLine[lcdY];
        asm mov edi,ptr0
        asm lea esi,v
        asm mov ecx,80
        asm rep movsd
    }
    else
    {
        char *ptr0=ScreenLine[lcdY<<1];
        char *ptr1=ScreenLine[(lcdY<<1)+1];
        asm mov edi,ptr0
        asm mov edx,ptr1
        asm lea esi,v
        asm mov ecx,160
       copyLoop:
        asm mov bx,[esi]
        asm mov ax,bx
        asm shl eax,16
        asm mov ax,bx
        asm add esi,2
        asm mov [edi],eax
        asm add edi,4
        asm mov [edx],eax
        asm add edx,4
        asm loop copyLoop
    }
/*    for (int i=0;i<160;i++)
    {
        if (scaleFact==1)
        {
            ScreenLine[lcdY][i<<1]=r[i]&0xff;
            ScreenLine[lcdY][(i<<1)+1]=r[i]>>8;
//            ScreenLine[lcdY][(i<<2)+2]=r[i];
        }
        else
        {
            ScreenLine[lcdY<<1][i<<3]=b[i];
            ScreenLine[lcdY<<1][(i<<3)+1]=g[i];
            ScreenLine[lcdY<<1][(i<<3)+2]=r[i];
            ScreenLine[(lcdY<<1)+1][i<<3]=b[i];
            ScreenLine[(lcdY<<1)+1][(i<<3)+1]=g[i];
            ScreenLine[(lcdY<<1)+1][(i<<3)+2]=r[i];
            ScreenLine[lcdY<<1][(i<<3)+4]=b[i];
            ScreenLine[lcdY<<1][(i<<3)+5]=g[i];
            ScreenLine[lcdY<<1][(i<<3)+6]=r[i];
            ScreenLine[(lcdY<<1)+1][(i<<3)+4]=b[i];
            ScreenLine[(lcdY<<1)+1][(i<<3)+5]=g[i];
            ScreenLine[(lcdY<<1)+1][(i<<3)+6]=r[i];
        }
    }*/
}

extern int debugStartPC,debug;
void DebugWarn(int wn)
{
    debugStartPC=previouspc;
    run=0;
    debug=1;
    DebugWnd->Show();
    DebugWnd->Update();
    debugWarnMsg=wn;
}

extern int lcdXOfs,lcdYOfs;
void RedrawLCD()
{
    LCDCanvas->Draw(lcdXOfs,lcdYOfs,bmp);
}

extern int debugRunToCursor,debugCursorAddr;
extern int debugCodeBreakCount,debugCodeBreak[256];
extern int debugDataBreak;
extern int debugStepOver,debugBeginStepOver,endStepOver;
extern int debugStepGoal,debugStepA7,debugEndStepOver;
extern int ignoreStack; 
extern unsigned short oldPC;
void DebugChecks()
{
    int bp,i;
    if (debugRunToCursor)
    {
        if (regPC==debugCursorAddr)
        {
            run=0;
            debug=1;
            debugStartPC=regPC;
            debugRunToCursor=0;
            DebugWnd->Show();
            DebugWnd->Update();
            return;
        }
    }
    for (i=0,bp=0;i<debugCodeBreakCount;i++)
    {
        if (regPC==debugCodeBreak[i])
            bp=1;
    }
    if (bp)
    {
        run=0;
        debug=1;
        debugStartPC=regPC;
        DebugWnd->Show();
        DebugWnd->Update();
        return;
    }
    if (debugDataBreak)
    {
        run=0;
        debug=1;
        debugStartPC=oldPC;
        DebugWnd->Show();
        DebugWnd->Update();
        return;
    }
    if (debugStepOver)
    {
        if (debugBeginStepOver)
        {
            debugBeginStepOver=0;
            if (endStepOver)
            {
                run=0;
                debug=1;
                debugStartPC=regPC;
                debugEndStepOver=1;
                DebugWnd->Show();
                DebugWnd->Update();
                return;
            }
        }
        if (debugStepOver&&(regPC==debugStepGoal)&&
            ((regSP==debugStepA7)||(ignoreStack)))
        {
            run=0;
            debug=1;
            debugStartPC=regPC;
            DebugWnd->Show();
            DebugWnd->Update();
            debugEndStepOver=1;
            return;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -