Author Topic: Keeping the Crystal Timers going  (Read 12746 times)

0 Members and 3 Guests are viewing this topic.

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Keeping the Crystal Timers going
« on: April 20, 2011, 12:56:46 am »
I want to work on a timer that triggers interrupts about every 1/100000 of a second.  I know how to set the timer speed, so suppose I have the counter set to 1 and divisor set to CPU speed / 16 (That is, 937500 HZ in fast mode).

But I'm a little lost for the rest of the steps.  What do I need to do to make sure that my interrupt routine will always run every 1/100000 of a second?  Assume the routine starts at Start_Routine, and the routine ends at End_Routine.


Start_Routine:

;Code
;Code
;Code

End_Routine

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Keeping the Crystal Timers going
« Reply #1 on: April 20, 2011, 01:09:21 am »
This is kind of a bad idea in general, because you won't be able to respond to an interrupt in 16 clock cycles.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Keeping the Crystal Timers going
« Reply #2 on: April 20, 2011, 10:20:01 am »
This is kind of a bad idea in general, because you won't be able to respond to an interrupt in 16 clock cycles.

Oh, did I get my computations wrong?  I'm trying to get an interrupt every 150 cycles or so
« Last Edit: April 20, 2011, 10:20:34 am by Hot_Dog »

Offline z80man

  • Casio Traitor
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 977
  • Rating: +85/-3
    • View Profile
Re: Keeping the Crystal Timers going
« Reply #3 on: April 20, 2011, 10:57:38 am »
I wouldn't trust this as I've never tried it before but what I got for setting the timers at cpu speed /16 was this:
Code: [Select]
ld A,$88
out ($30),A

List of stuff I need to do before September:
1. Finish the Emulator of the Casio Prizm (in active development)
2. Finish the the SH3 asm IDE/assembler/linker program (in active development)
3. Create a partial Java virtual machine  for the Prizm (not started)
4. Create Axe for the Prizm with an Axe legacy mode (in planning phase)
5. Develop a large set of C and asm libraries for the Prizm (some progress)
6. Create an emulator of the 83+ for the Prizm (not started)
7. Create a well polished game that showcases the ability of the Casio Prizm (not started)

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Keeping the Crystal Timers going
« Reply #4 on: April 20, 2011, 11:08:38 am »
I wouldn't trust this as I've never tried it before but what I got for setting the timers at cpu speed /16 was this:
Code: [Select]
ld A,$88
out ($30),A

Granted, but like I mentioned in the first post I know how to set the timers. 

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Keeping the Crystal Timers going
« Reply #5 on: April 20, 2011, 05:16:58 pm »
If you want an interrupt every 150 cycles. You would want to output a 9 with your timer speed being cpu / 16.

And to make sure it keeps going, you need to output to port $31 and maybe $32. The problem with outputting to $32 is that you are doing your interrupt so fast that those extra 20 clock cycles of output are going to add up quick. However, don't miss an interrupt or else $32 is going to loop to $FF.

So for code, this is my recommendation:
Code: [Select]
start:
ex af, af'
ld a, 3 ;3 because you want interrupts, and you want it to loop
out ($31), a
exx

;don't check for which interrupt caused
;only have timers enabled

;do something quick

exx
ex af, af'
ret
end:

And remember, keep the interrupt quick, you've already used 44 of your 150 t-states. Too many more and the real program is going to stop all together.
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Keeping the Crystal Timers going
« Reply #6 on: April 20, 2011, 06:25:43 pm »
So you're saying that 9, not 1, should be outputted to the counter, right?

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Keeping the Crystal Timers going
« Reply #7 on: April 20, 2011, 06:59:00 pm »
Yes. Output 9 to port ($32). You want 150 t-states. So 9*16 is 144 which is about what you want. Outputting a 1 will only give you 16 t-states. And since RET takes 10, that leaves you with 6 ;)
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Keeping the Crystal Timers going
« Reply #8 on: April 20, 2011, 07:05:19 pm »
I may be wrong, but couldn't you get the exact number of cycles you wanted (up to 255 or maybe 256) by outputting the desired number of cycles to the counter port and setting the timer speed to the CPU clock speed?

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Keeping the Crystal Timers going
« Reply #9 on: April 20, 2011, 07:08:48 pm »
I may be wrong, but couldn't you get the exact number of cycles you wanted (up to 255 or maybe 256) by outputting the desired number of cycles to the counter port and setting the timer speed to the CPU clock speed?

If that's so, that's preferable.  And in that case I really misunderstood the wikiti article :(

Offline FloppusMaximus

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 290
  • Rating: +57/-5
    • View Profile
Re: Keeping the Crystal Timers going
« Reply #10 on: April 20, 2011, 09:23:36 pm »
Well, in general, you can't have an event timed more precisely than one instruction (i.e., 4 to 23 clock cycles), except by writing code to explicitly delay for that long.  Interrupts can't "fire" in the middle of an instruction, only between one instruction and the next.

If you use the auto-repeating mode, though, the timer keeps ticking after it has expired, so you can use it for accurate long-term timekeeping.  (I.e., you'll still be off by 4 to 23 clock cycles each time, but the errors won't accumulate.)  If you want to do this, you do have to be sure that you acknowledge the interrupt (by writing a value to port 31/34/37) every time the timer expires, and before it underflows.  Is that what you were originally asking about, Hot_Dog?

In answer to Runer112's implied question about whether you can count up to 256: no, the timers don't really work right if you set the initial value to zero.  But if you want an interrupt every 256 clock cycles, you can switch to (say) the CPU/2 mode and set the initial value to 128.

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Keeping the Crystal Timers going
« Reply #11 on: April 20, 2011, 09:29:31 pm »
Quote
Is that what you were originally asking about, Hot_Dog?

Well basically I wanted to know how to keep the interrupt routine going.  I read on wikiti that there were things you had to do if you wanted the interrupt routine to run on a consistent basis, but there were some concepts in the article that I could not understand


Offline FloppusMaximus

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 290
  • Rating: +57/-5
    • View Profile
Re: Keeping the Crystal Timers going
« Reply #12 on: April 20, 2011, 10:46:16 pm »
If you wanted to use the auto-repeat mode as I've described (i.e., timer keeps running and fires every N ticks, regardless of what the CPU is doing), then you'd want to:
- select the desired frequency (ticks per second) using port 30
- set port 31 to 3
- set port 32 to the desired number of ticks per interrupt period

Then, each time a timer interrupt occurs, you'd write 3 to port 31 again.  You wouldn't touch ports 30 or 32 after that, except to turn the timer off when your program finishes, and perhaps you might want to read port 32 to determine precisely how much time has elapsed.  If port 31 bit 2 ever gets set, you're probably doing something wrong.

If you wanted to use the non-auto-repeat mode (i.e., timer fires N ticks after the previous interrupt was acknowledged), then you'd want to:
- select the desired frequency using port 30
- set port 31 to 2
- set port 32 to the desired number of ticks until the next interrupt

and do that again each time an interrupt occurs (though you could omit the re-initialization of port 30 if you like.)

I think that's basically all there is to it.  There's also some weirdness (seemingly a hardware bug) where the programmable timers won't generate interrupts if you turn off regular timer interrupts (port 3 bit 1) and the CPU is halted.  Don't do both of those things at once, and you should be fine.

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Keeping the Crystal Timers going
« Reply #13 on: August 15, 2011, 03:39:11 pm »
I forgot to thank you, Floppus.  This information is just what I needed!