0

I have coded a chip-8 emulator.Whatever I do, it seems that I cannot show any pixels on the screen.The weird thing is that I have checked the code, top-bottom for 2 days already, and there does not seem to be any problem.It reads the .rom file into memory, and fetches the OP code correctly.

Here is the source code:

                SDL_SetRenderDrawColor( renderer, 0, 0, 0, SDL_ALPHA_OPAQUE );
                SDL_RenderClear(renderer);
                uint32_t pixels[(WINDOW_WIDTH / 10) * (WINDOW_HEIGHT / 10)];
                uint16_t i;
                for(i = 0; i < 64*32; i++){
                    pixels[i] = (0x00FFFFFF * display[i]) | 0xFF000000;
                }
                //upload the pixels to the texture
                SDL_UpdateTexture(tex,NULL,pixels, 64 * sizeof(uint32_t));
                //Now get the texture to the screen
                SDL_RenderCopy(renderer,tex,NULL,NULL);
                SDL_RenderPresent(renderer); // Update screen
                ch8.drawF = false;

uint16_t x = ch8->V[((ch8->opcode & 0x0F00) >> 8)];
                uint16_t y = ch8->V[((ch8->opcode & 0x00F0) >> 4)];
                uint8_t n = (ch8->opcode & 0x000F);
                for(i = 0; i < n; i++) {
                    uint8_t pixel= memory[ch8->I.word + i];
                    for(j = 0; j < 8; j++) {
                        if((pixel & (0x80 >> j)) != 0){
                            if(display[x + j  + ((y + i) * 64)] == 1) {
                                ch8->V[0xF] = 1;
                            }
                            display[x + j  + ((y + i) * 64)] ^= 1;
                        }
                    }
                }
J.Doe
  • 21
  • 2
  • 6
  • Perhaps something went wrong before what you're looking at. Try to simplify the situation, test upstream conditions, and come back stating what you found. Even if it does not solve the issue, it will make the situation clearer and hopefully help get an answer. – Stéphane Gourichon Sep 12 '17 at 10:33
  • I have tested for 2 days straight, and doublechecked the code tens of times.I have even doublechecked the code based on other's code.Still not working.I find it weird that if I just make a mock array with pixels, and put it on the texture , then try to render the texture, still no pixel on the screen! – J.Doe Sep 12 '17 at 10:46
  • I tried to compiled your code from https://pastebin.com/kxYjn9AT , but it has `assignment from incompatible pointer type` in `ch8->SP = (memory + 0x0EA0)`. Indeed compiler cannot convert ‘uint8_t* {aka unsigned char*}’ to ‘uint16_t* {aka short unsigned int*}’. Perhaps your code is messing with memory big time. Anyway you should be much more specific. For a start, have you tried a trivial program that opens a window and sets some pixels, then build from that? – Stéphane Gourichon Sep 12 '17 at 12:15
  • I have compiled the code, and I do not get such an compiler error, not even a warning,using -Wall.It cannot have anything to do with SP.I have tried a mock ROM with only draw opcode, that has no call opcode, so SP is not even used and it still did not work.Even more, I have tested the stack functionality of the emulator,with call and return opcodes and everything seems to be fine.I have made a mock array of pixels, just before the call to SDL_UpdateTexture, and it still did not work. – J.Doe Sep 12 '17 at 12:26
  • edit: I have made a trivial program that opens a window and sets some pixels,and it still does not want to draw the pixels. https://pastebin.com/a68BTuEz – J.Doe Sep 12 '17 at 12:38
  • it 100% has to do with the SDL drawing part,because it does not work, even at simple examples.I don't get what I'm doing wrong in drawing pixels to a texture and then rendering it on screen. – J.Doe Sep 12 '17 at 13:43
  • I had fixed it.It had to do with the init function, I had missread the API and the functions return values. – J.Doe Sep 12 '17 at 14:25
  • Can you then post an answer (below, not a comment) to your own question with the faulty line and the corrected one? Have a nice day. – Stéphane Gourichon Sep 12 '17 at 16:03

1 Answers1

0

So basically, the problem was at init() function.I was initially using, SDL_CreateWindow and SDL_CreateRenderer,but now I'm using ,SDL_CreateWindowAndRenderer, which takes pointers to pointers of SDL_Window and SDL_Renderer instead of a pointer to a char and a pointer to a window.

Also there were 3 problems I fixed. 1.I was adding + 0x200 to NNN opcodes,because at firstly I thought that the NNN in ROM's are relative to 0, so I removed +0x200 from each XNNN opcode.Also I forgot a * at SDL_Texture* tex, its supposed to be SDL_Texture** tex, I was merely changing the address the local pointer was poiting too... 2.at opcode 2NNN, instead of (ch8->SP) = ch8->opcode & 0x0FFF; its(ch8->SP) = ch8->PC.word; 3.at opcode FX65 its i <= ((ch8->opcode & 0x0F00) >> 8)

Basically, the differences between SDL_CreateWindowAndRenderer and SDL_CreateWindow&SDL_CreateRenderer had me confused, I should had check'd the documentation first.

Now I only need to make the emulator only redraw the changed pixels, then make the emulator play sound.

J.Doe
  • 21
  • 2
  • 6