[Noob] Adding code to VBL

News,announcements,programming,fixes,game patches & discussions.

Moderator: troed

Post Reply
User avatar
Badwolf
Posts: 2242
Joined: Tue Nov 19, 2019 12:09 pm

[Noob] Adding code to VBL

Post by Badwolf »

I'm a 68k ingenue and an utter system programming n00b, so please give me a hand with this one.

I know how to replace the VBL exception handler upon startup of my program, and restore it again upon exit but what I'd like to do is add a little bit of code to the existing VBL handler

I'm sure I had heard in the past that the trick is to point the exception vector to your code, do you thing, and then jump to the old handler's address, but I can't for the life of me make it work.

Am I barking up the wrong tree?

Code: Select all

... preamble ...

main:
    ; supexec hook_vbl
    pea hook_vbl
    move.w #38,-(sp)
    trap #14 ; xbios
    addq.l #6,sp

	; some code

    ; cconin (wait for key)
    move.w #1,-(sp)
    trap #1
    addq.l #2,sp
    
    ; supexec restore_vbl
    pea restore_vbl
    move.w #38,-(sp)
    trap #14 ; xbios
    addq.l #6,sp

    rts
; end of main



hook_vbl:
    move.w #$2700,sr ; disable interrupts
    move.l $70.w,old_vbl    ;save old vbl
    move.l #vbl,$70.w    ; insert my vbl
    move.w #$2300,sr    ; re-enable interrupts
    
    rts

restore_vbl:
    move.w #$2700,sr ; disable interrupts
    move.l (old_vbl),$70.w    ; restore old vbl
    move.w #$2300,sr    ; re-enable interrupts
    
    rts

vbl:
	; imaginary code here
    rte
    
old_vbl:
    ds.l 1
What do I put in 'vbl'? My naieve attempt with jmp (old_vbl) doesn't cut it.

Cheers,

BW
DFB1 Open source 50MHz 030 and TT-RAM accelerator for the Falcon
DSTB1 Open source 16Mhz 68k and AltRAM accelerator for the ST
Smalliermouse ST-optimised USB mouse adapter based on SmallyMouse2
FrontBench The Frontier: Elite 2 intro as a benchmark
troed
Moderator
Moderator
Posts: 911
Joined: Mon Aug 21, 2017 10:27 pm

Re: [Noob] Adding code to VBL

Post by troed »

Well ... yeah ;) You want to add your VBL routine to TOS VBL list, see $454 and $456: https://ftp.lip6.fr/pub/atari/Docs/hardware.txt

pseudo-code:

Code: Select all

p=*$456
while(*p != null)
  p++;
*p=&mycode
$454+=1
There's something about the memory allocated for the list that you need to look up though, I don't remember it off-hand. I _think_ it's that there's space allocated for 8 pointers to begin with, and if you're adding the ninth you need to allocate new memory and copy the existing list over and replace the pointer to it.

/Troed
czietz
Posts: 549
Joined: Sun Jan 14, 2018 1:02 pm

Re: [Noob] Adding code to VBL

Post by czietz »

I generally agree with troed, with one exception, though: Do not increment 'nvbls' (aka $454) by one when merely installing a routine. 'nvbls' holds the maximum number of slots available for the VBL handlers, not the number of slots currently in use. You would only change 'nvbls' if all slots are exhausted and you have allocated memory for a longer list.

See the Hitchhiker's Guide, p. 47-48: https://docs.dev-docs.org/htm/search.ph ... S+%28XBIOS

Advantages of installing a VBL handler this way: You don't have to take care of calling other handlers; the OS does that for you. You don't have to save registers; the OS does that for you.

PS: If you really want to hook the exception vector, a common pattern to call the previous handler is:

Code: Select all

move.l old_vbl,-(sp)
rts
If you hook a vector in a program that stays resident, be nice and follow the XBRA protocol.
User avatar
Badwolf
Posts: 2242
Joined: Tue Nov 19, 2019 12:09 pm

Re: [Noob] Adding code to VBL

Post by Badwolf »

troed wrote: Fri Apr 01, 2022 7:12 pm Well ... yeah ;) You want to add your VBL routine to TOS VBL list, see $454 and $456: https://ftp.lip6.fr/pub/atari/Docs/hardware.txt
...
/Troed
Well, umm, yeah, I mean could do it that way, obviously... ;-)

But seriously, that does seem a bit less hacky. Thanks. :)
czietz wrote: Fri Apr 01, 2022 7:42 pm I generally agree with troed, with one exception, though: Do not increment 'nvbls' (aka $454) by one when merely installing a routine. 'nvbls' holds the maximum number of slots available for the VBL handlers, not the number of slots currently in use. You would only change 'nvbls' if all slots are exhausted and you have allocated memory for a longer list.

See the Hitchhiker's Guide, p. 47-48: https://docs.dev-docs.org/htm/search.ph ... S+%28XBIOS
Aha, and thanks again! Knowing where to start looking is half the battle, I suppose. :)

Code: Select all

move.l old_vbl,-(sp)
rts
Sneaky!

I suppose it's like those people who can understand a foreign language but can't speak it -- I can see exactly what that's doing and why it works, but I would never have concocted that myself! :lol:

Thanks, guys. BTW, I'm just messing about seeing what sorts of things can be done and what can't. I haven't actually got a project in mind, but will go and look up XBRA.

Cheers,

BW
DFB1 Open source 50MHz 030 and TT-RAM accelerator for the Falcon
DSTB1 Open source 16Mhz 68k and AltRAM accelerator for the ST
Smalliermouse ST-optimised USB mouse adapter based on SmallyMouse2
FrontBench The Frontier: Elite 2 intro as a benchmark
czietz
Posts: 549
Joined: Sun Jan 14, 2018 1:02 pm

Re: [Noob] Adding code to VBL

Post by czietz »

The challenge is, as you're surely aware, that this is an interrupt and must not modify any registers, which precludes you from doing...

Code: Select all

move.l	old_vbl,a0
jmp	(a0)
(... which would be okay in a BIOS/XBIOS/GEMDOS trap, as they are allowed to clobber A0.)

Another approach one finds is patching in the jump at run-time. So you have something in you code like...

Code: Select all

jmp	0xDEADBEEF
... and modify the jump target in memory to point to the old handler. However, while the former methods can be made compatible to the XBRA protocol, the latter cannot, which is why I recommend to avoid it.
troed
Moderator
Moderator
Posts: 911
Joined: Mon Aug 21, 2017 10:27 pm

Re: [Noob] Adding code to VBL

Post by troed »

czietz wrote: Fri Apr 01, 2022 7:42 pmDo not increment 'nvbls' (aka $454) by one when merely installing a routine. 'nvbls' holds the maximum number of slots available for the VBL handlers, not the number of slots currently in use.
Ahh thanks - memory mixup there :)

/Troed
Post Reply

Return to “SOFTWARE PROGRAMMING & DISCUSSION”