Page 1 of 1

Getting VBlank Status

Posted: Thu Oct 10, 2024 2:47 am
by Sarasaworks
I'm deep diving into VERA at the moment, but even with the documentation available, I'm not quite sure how to handle VBlank. Whether it matters that I'm working in 320x240, if I have to count lines, or if there's a flag.

I wrote the following C code, thinking I understand. However, given the resulting flicker, something isn't working correctly.

Code: Select all

#include <stdio.h>

typedef unsigned char u8;
typedef volatile unsigned char vu8;

#define ROM_BANK    (*(vu8*)0x9f60)

// KERNAL

// PETSCII

// VERA 
#define ADDRx_L     (*(vu8*)0x9f20)
#define ADDRx_M     (*(vu8*)0x9f21)
#define ADDRx_H     (*(vu8*)0x9f22)
#define DATA_0      (*(vu8*)0x9f23)
#define DATA_1      (*(vu8*)0x9f24)
#define CTRL        (*(vu8*)0x9f25)
#define IEN         (*(vu8*)0x9f26)
#define ISR         (*(vu8*)0x9f97)
#define DC_HSCALE   (*(vu8*)0x9f2a)
#define DC_VSCALE   (*(vu8*)0x9f2b)



void AddressSelect(u8 HighByte,u8 MidByte,u8 LowByte){
    ADDRx_L = LowByte;
    ADDRx_M = MidByte;
    ADDRx_H = HighByte;
}

void FixScreen(){
    asm("lda #$8f");
    asm("jsr $ffd2");
    asm("lda #$8e");
    asm("jsr $ffd2");
}

void ClearScreen(){
    asm("lda #$93");
    asm("jsr $ffd2");
}

void main(void){ 
    FixScreen();
    DC_HSCALE = 0x40;
    DC_VSCALE = 0x40;
    while(1){
        while(ISR & 0x01){
            printf("hello");
            ClearScreen();    
        }
    }
}

Re: Getting VBlank Status

Posted: Thu Oct 10, 2024 5:53 am
by unartic
It depends on what you want to achieve.

If you just want to wait for the vblank you can just use the assembly opcode:

wai

(https://github.com/X16Community/x16-doc ... sor.md#wai)

If you want to execute a piece of code on each vblank you have to setup your own isr.

Re: Getting VBlank Status

Posted: Fri Oct 11, 2024 5:13 am
by Sarasaworks
unartic wrote: Thu Oct 10, 2024 5:53 am If you want to execute a piece of code on each vblank you have to setup your own isr.
Unfortunately, creating my own ISR is beyond my scope at this phase. Even with videos, I'm left feeling overwhelmed and jaded.

That said, when dealing with the Gameboy (which, unlike it's console contemporaries, lacked a dedicated VBLANK register), I did a simple positioning check of the Vertical Scanline:

Code: Select all


       // DMG
        while(LY >= 145 && LY <= 255){
            // Do stuff in B&W
        }
  
After finding the VERA Scanline register, I implemented the same concept:

Code: Select all


        // X16
        while(SCANLINE_L >= 240 && SCANLINE_L <= 524){
	    // Do stuff  in Color
        }

While not as robust as writing your own ISR might be, this at least solves the problem of flicker and screen tear. It could also serve as a less daunting method for others who might be struggling in this area.