Thoughts on delays...

Let's talk about anything related to the 6502 microprocessor.
User avatar
cbmeeks
Posts: 1254
Joined: 17 Aug 2005
Location: Soddy-Daisy, TN USA
Contact:

Thoughts on delays...

Post by cbmeeks »

In my monitor, I'm needing more accurate timing. Mainly for controlled delays and dealing with the 65C51 transmit bug.

I'm currently just doing a silly loop and that works. But, obviously, the amount of time is different when I switch back to 1 MHz or 8 MHz.

What I'd like is a way of delaying an arbitrary amount of time. I'm not sure what resolution I would need. A resolution of a millisecond would be ideal but that might be pushing it on a 1 MHz machine. It doesn't really matter what it is as long as it's the same on all clock speeds and reasonably fast to allow granular delays.

My first guess was to use the timers on the VIA but I've never done that before. A RTC is not out of the question (or even a 555).

What are your suggestions?

Thanks!
Cat; the other white meat.
User avatar
Dr Jefyll
Posts: 3526
Joined: 11 Dec 2009
Location: Ontario, Canada
Contact:

Re: Thoughts on delays...

Post by Dr Jefyll »

A VIA timer uses the CPU clock for its timebase, which means it has the same problem as a software timing loop. But both the VIA and the software loop could be used in a way that accounts for whatever the clock rate happens to be. You could declare a value representing clock rate, and always refer to that value before setting up the timer or loop.

Remember a loop won't offer interrupt capability. But maybe that's not a priority for you.

Would an RTC offer enough resolution for your needs? I seem to recall they generally track time in increments no finer than 1/100 second. Other timer chips are available, though -- the 8254 being a very highly flexible example. And, unlike a VIA, it accepts its input clock on dedicated pins.

-- Jeff
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
User avatar
drogon
Posts: 1671
Joined: 14 Feb 2018
Location: Scotland
Contact:

Re: Thoughts on delays...

Post by drogon »

Some RTCs have a programmable clock output, also an 8-pin microcontroller (e.g. ATtiny) running off it's internal oscillator could be programmed to output - say - a 1Khz clock. you could simply poll that in software - however that would give you a delay from 1 to 2mS depending how you're sampling the output - that would be the minimum delay, for anything longer, counting pulses then it's more accurate, so for 100mS, then it would be a minimum of 100mS and a maximum of 101mS. Obviously interrupts would lengthen the delay, but that's usually not a concern. Or run the clock at 10Khz - still very "samplable" by a 1Mhz 6502.

Alternatively - what I did a while back, conditional assembly and copied wait code out of the Apple II monitor... On a 1Mhz Apple II that's good for 13µS to 166mS - although it's a bit chunky in the steps.

-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
whartung
Posts: 1004
Joined: 13 Dec 2003

Re: Thoughts on delays...

Post by whartung »

the problem is where getting the values from the RTC takes longer than the delay you're looking for.

If it's that important, a combination of the RTC and a VIA timer, or just loop counting. You can do a quick "benchmark" at startup to discern what values to use at runtime, and load constants in the system.

The monitor in the '265 does this, but it had two clock sources -- CLK and FCLK, FLCK is the "fast (XX Mhz)" clock, CLK is a 32768hz clock. It uses the slow clock to figure out how fast the fast clock is.

If you have access to a high enough resolution alternate clock (like an RTC), you could use that. But if you have to slurp in the RTC values via SPI, then getting the data can be "expensive" for high resolution timing.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8774
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Thoughts on delays...

Post by GARTHWILSON »

Remember that one of the VIA's features is that you can have T2 count pulses on PB6 and generate an interrupt when your predetermined number of pulses has been reached. Many RTCs have a square-wave output, and you could connect that, at least temporarily, to PB6, to compare T2 to a Φ2-locked T1 to see what the Φ2 frequency is. Once you know the Φ2 frequency (which you could also enter upon power-up, or tell it via a DIP switch, but maybe you want it automatic), routines can calculate what value to put in the T1 latches.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
User avatar
GaBuZoMeu
Posts: 660
Joined: 01 Mar 2017
Location: North-Germany

Re: Thoughts on delays...

Post by GaBuZoMeu »

Assuming° you are using the regular 1.8432 MHz clock for your 6551 and no external RxC than that pin (5 on DIL) reflects 16x RxC. Feeding this into PB6 of a VIA and use the pulse counting mode you can even generate an interrupt (out of the VIA) that would occur after transmission is done.


EDIT(1): with 1.8432 MHz the RxC should not exceed 115.2 kHz which can safely counted by a 1 or more MHz driven 6522.
User avatar
cbmeeks
Posts: 1254
Joined: 17 Aug 2005
Location: Soddy-Daisy, TN USA
Contact:

Re: Thoughts on delays...

Post by cbmeeks »

Thanks everyone for the suggestions!

That's a good idea on the ACIA clock. He stays the same no matter which main clock I have.
Cat; the other white meat.
User avatar
floobydust
Posts: 1394
Joined: 05 Mar 2013

Re: Thoughts on delays...

Post by floobydust »

There's quite a few different solutions that can be used to resolve the W65C51 xmit bug. None are truly ideal as all become a band-aid for a defective hardware device. Still, if you prefer to continue using the W65C51, then you can add some hardware to use the baud clock output and create an interrupt driven solution that will yield a timer/counter.

You can then leverage the timer/counter for accurate and consistent delays, albeit your resolution will be tied to the baud clock itself. A jiffy clock should be easy enough to implement and would result in a 10ms timing interval. You need to determine what resolution of timing you need for your application (beyond the delay timing for the W65C51 transmit). Just realize that the interrupt service routine will spend some number of clock cycles there to handle the interrupt. The slower the CPU clock, the greater percentage of the CPU's time will be spent there. The other concern is the CPU time processing the delay and any added time due to execution of instructions, which are dependent on the CPU clock.

For the W65C51, a timing delay between sending characters at 19.2K baud would be around 521 microseconds. I would probably increase that a bit for a safety margin. Also note that this assumes 8- data bits, 1-stop bit and no parity.
User avatar
cbmeeks
Posts: 1254
Joined: 17 Aug 2005
Location: Soddy-Daisy, TN USA
Contact:

Re: Thoughts on delays...

Post by cbmeeks »

I'm only using the 65C51 because that's all I have at the moment. I'm building small modules to plug into my SBC so that I can write drivers for them. I'll finish the ACIA driver before I move on to the NXP. By then, I'll just have a PCB made. Unless I want to try wire wrapping it. I suppose I could...but I just wanted to get "something out the door" and the ACIA fit that bill.

Thanks
Cat; the other white meat.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8774
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Thoughts on delays...

Post by GARTHWILSON »

GaBuZoMeu wrote:
Assuming° you are using the regular 1.8432 MHz clock for your 6551 and no external RxC than that pin (5 on DIL) reflects 16x RxC. Feeding this into PB6 of a VIA and use the pulse counting mode you can even generate an interrupt (out of the VIA) that would occur after transmission is done.

EDIT(1): with 1.8432 MHz the RxC should not exceed 115.2 kHz which can safely counted by a 1 or more MHz driven 6522.

This looks to be the best idea yet! From the VIA's data sheet, you write to the T2 low-byte latch, and that value gets transferred to the low-byte counter when you write to the high byte. For each byte, store the byte in the 51's transmit data register, then store the desired value (0 in this case, with STZ) to the high byte of the T2 counter to re-start the counter and to re-enable its interrupt. If the number of bits per frame remains constant, or if you just want to use the maximum for everything, the T2 low-byte latch value will be the same for all bit rates. Thanks, GaBuZoMeu!

Edit: Pin 5 is for 16x the bit rate, and 16 times 115.2kbps is 1.8432MHz. 9600bps is the fastest standard RS-232 speed a '22 running at 1MHz would be able to dependably count.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
User avatar
GaBuZoMeu
Posts: 660
Joined: 01 Mar 2017
Location: North-Germany

Re: Thoughts on delays...

Post by GaBuZoMeu »

Thank you, Garth.

It looks like the 6522 could not count all pulses once their frequency is above PHI2/4 (assuming a duty cycle of 50:50). If that is true, 19200 baud would be (x16 = 307200 Hz) just a bit too fast. But this only means some pulses weren't count or the other way: it took a few more pulses to count down to zero. This causes the delay to be a little longer than necessary. If this is not a big deal... :wink:


edited: typos
whartung
Posts: 1004
Joined: 13 Dec 2003

Re: Thoughts on delays...

Post by whartung »

cbmeeks wrote:
I'm only using the 65C51 because that's all I have at the moment. I'm building small modules to plug into my SBC so that I can write drivers for them. I'll finish the ACIA driver before I move on to the NXP. By then, I'll just have a PCB made. Unless I want to try wire wrapping it. I suppose I could...but I just wanted to get "something out the door" and the ACIA fit that bill.
Then tweak your cycle counting loops when you up your system clock, and rebuild. That's, what, 1? 2 lines of code?

That's your simplest, cheapest, most effective tack.

Especially if you're just going to yank the thing out as soon as practical.
Last edited by whartung on Fri Feb 08, 2019 5:37 pm, edited 1 time in total.
DerTrueForce
Posts: 483
Joined: 04 Jun 2016
Location: Australia

Re: Thoughts on delays...

Post by DerTrueForce »

The 28L92 has a counter/timer that can be driven from the 3.6864 MHz baudrate clock. It can go very slowly, especially if you use the divide-by-16 clock setting.
But if you need the delay for a 6551, that's going to be of no use to you, because the 28L92 would replace the ACIA completely.
Chromatix
Posts: 1462
Joined: 21 May 2018

Re: Thoughts on delays...

Post by Chromatix »

Given that the 6522 can use an external clock source for its timers, and a 32kHz oscillator is often useful with an RTC (if you don't want to rely on a bare crystal), you could send the 32kHz signal to the 6522 as well. That would give you an independent timing source with sub-millisecond precision, while the RTC could be relied on to keep time to within 1 second.
User avatar
BillO
Posts: 1038
Joined: 12 Dec 2008
Location: Canada

Re: Thoughts on delays...

Post by BillO »

Solution 1) As a part of your start-up fire off a one-shot (ls121, ls122 or ls123) and count the number of loops until the one-shot resets. Use this to calculate your timing. This would take 2 pins on your 6522.

Solution 2) PM me a mailing address and I will send you one of the 'magical' 65C51 chips I have. I recently ran one of these at 20mHz and as long as your /CE PD is tight, it should work for you too. I just ask that you pay for the shipping once you receive it. One caveat - I will be out of the country until March 4th and unable to send until then.
Bill
Post Reply