1

此代码效果很好,并按预期输出像素:

int main()
{
    *(unsigned int *)0x04000000 = 0x0403;
    unsigned short *Screen = (unsigned short *)0x06000000;
    Screen[120 + 80 * 240] = 0x001F;
    Screen[136 + 80 * 240] = 0x03E0;
    Screen[120 + 96 * 240] = 0x7C00;
    Screen[100] = 0x7C00;

    while (1);

    return 0;
}

但是每当我尝试添加 for 循环并从中更改 vram 值时,我在模拟器上得到白屏,它编译时没有错误甚至警告,但我在模拟器上得到白屏。

int main()
{
    *(unsigned int *)0x04000000 = 0x0403;
    unsigned short *Screen = (unsigned short *)0x06000000;
    
    Screen[120 + 80 * 240] = 0x001F;
    Screen[136 + 80 * 240] = 0x03E0;
    Screen[120 + 96 * 240] = 0x7C00;
    Screen[100] = 0x7C00;
    
    int i;

    for (i = 100; i < 110; i++)
    {
        Screen[i] = 0x7C00;
    }

    while (1);
    
    return 0;
}

4

1 回答 1

4

我遇到了同样的问题并找到了解决方案:您的显示控制指针需要是volatile. 如果您不将其标记为volatile,则在编译时会发生一些不需要的优化,从而导致您出现意外行为。

*(volatile unsigned int *)0x04000000 = 0x0403;
于 2021-10-12T21:27:42.637 回答