1

This Code works great and outputs the pixels as expected:

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;
}

but whenever I try to add the for loop and change the vram values from within that, I get white screen on the emulator, it compiles with no errors and even warnings, but I get white screen on the emulator.

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;
}

RealGas
  • 109
  • 1
  • 6
  • Can you show the assembly code for both programs? I wonder if the stack is not correctly set up, and in the first program the compiler did not need to use it. – user253751 Jul 01 '20 at 14:25
  • @user253751 I have not written a single line of assembly code and I have no Idea how can I show it. – RealGas Jul 01 '20 at 14:27
  • The compiler generates assembly code. It would be useful to learn how to look at it. – user253751 Jul 01 '20 at 14:31

1 Answers1

4

I ran into the same problem and found the solution: your display control pointer needs to be volatile. If you don't mark it as volatile, some unwanted optimizations happen at compile time that leave you with unexpected behavior.

*(volatile unsigned int *)0x04000000 = 0x0403;
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83