 |
|
| View previous topic :: View next topic |
| Author |
Message |
Newbie
Joined: 23 Jun 2012 Posts: 29 Location: Canada
|
Posted: Sat Jun 23, 2012 4:21 am Post subject: Confused about keyboard and timer interrupts |
|
|
Hi,
I guess I fit a common profile (at least here): I am "getting back to my roots" by trying to code a c64 game in asm (a.k.a. revisiting an incomplete childhood dream). This is my first experiment (I'm using KickAssembler and Vice), and so far I'm having a blast.. However, here is the outline of a problem I can't seem to solve by myself:
(a) I have an event that must occur at regular intervals (e.g. every second)
(b) I also want to react on certain key presses, and have the key event continue to trigger as long as the key is held
My current solution is as follow:
(1) I set the key autorepeat mode:
(2) I set an interrupt handler:
| Code: |
sei
lda #<interrupt_handler
sta $0314
lda #>interrupt_handler
sta $0315
cli
|
(3) In this handler, I keep a counter, which, every time it reaches 60, runs the required timing event (a) (i.e., every second, since the interrupt is called every 60th of a sec); this seems to be working rather well (although I don't know if it's a very accurate way to handle a timer)
(4) In this same interrupt (directly following the previous code), I also call, at smaller intervals, the GETIN kernal routine ($ffe4), and that's where my problems begin.
Every time a certain key is detected, some additional code is run. I noticed that the "time" (or rather complexity) of this code seems to be important, because with the experiments I made, when it is below a certain threshold, everything runs fine. But when it's over it, strange things start to happen, and my program basically crashes.
This thread seems to agree with this "theory":
http://www.lemon64.com/forum/viewtopic.php?t=38309
I see recommendations that events of this type should be "run off the IRQ", but I don't understand enough about interrupts for this to be really meaningful. I seem to understand that the mechanism I'm currently using is the timer signal from the CIA chip (not sure about this), and that there other types of interrupt I could be using (the raster interrupt in particular is often mentioned). Ideally, I would require an event triggered simply on key presses, but I don't know if such a thing is possible, or simple enough to do.
Thanks in advance for any help! |
|
| Back to top |
|
 |
Forum Junkie

Joined: 17 Jun 2006 Posts: 527
|
Posted: Sat Jun 23, 2012 5:48 am Post subject: Re: Confused about keyboard and timer interrupts |
|
|
| cjauvin wrote: | | Every time a certain key is detected, some additional code is run. I noticed that the "time" (or rather complexity) of this code seems to be important, because with the experiments I made, when it is below a certain threshold, everything runs fine. But when it's over it, strange things start to happen, and my program basically crashes. |
What does this additional code do? And how long does it take to run? I bet it takes more than a frame to execute?
By default, the IRQ is set up to run once a frame. Your 60 count will work, on NTSC.
Probably what you should be doing is ready the key, store the result. Then "off" the irq do something when the key is set.
I am betting right now your off IRQ code is jmp *?
Instead do something like:
key .byte 0
here lda key
beq here
cmp #special key
bne here
jsr specialkeyhandling
jmp here
Or something along those lines. _________________
|
|
| Back to top |
|
 |
Über Groupie


Joined: 12 Jun 2012 Posts: 310 Location: Wuppertal, FRG
|
Posted: Sat Jun 23, 2012 8:35 am Post subject: Re: Confused about keyboard and timer interrupts |
|
|
| THEWIZ wrote: | | By default, the IRQ is set up to run once a frame. Your 60 count will work, on NTSC. |
The normal Timer-IRQ is not set up to run once a frame, but to run every 60th of a second and should be the same on PAL/NTSC (the standard IRQ is no rasterinterrupt!).
Some guys in another forum told me a couple of weeks ago if you use the standard IRQ and don't change the timer values you don't have to worry about PAL/NTSC differencies.
So the counter by cjauvin should also work on PAL. |
|
| Back to top |
|
 |
Groupie in Training

Joined: 17 Mar 2006 Posts: 103
|
Posted: Sat Jun 23, 2012 1:02 pm Post subject: |
|
|
For an action type game relying on keys you'd be way better off scanning the keyboard yourself.
But if e.g. it's just an RPG or something where you press certain keys singularly then letting the OS do it should be fine.
The timer IRQ does odd bits of system housekeeping too. The deficiency of the OS keyscan is that it's subject to ghosting and lost keys due to either multiple keypresses or someone using the joystick.
You can solve multi key presses and mostly eliminate joystick interference if you scan for individual keys yourself. |
|
| Back to top |
|
 |
Newbie
Joined: 23 Jun 2012 Posts: 29 Location: Canada
|
Posted: Sat Jun 23, 2012 4:15 pm Post subject: |
|
|
Many thanks guys! THEWIZ's solution was easy to implement, and seems to be working very well.. I learned something! |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Tip: Get C64 Forever for super-comfy C64 emulation with pre-installed games, demos and other goodies!
Powered by phpBB © 2001, 2005 phpBB Group
|