[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: 6502 Delay Loops using Applesoft
In article <02b1803a-1471-40cb-a69f-37dbab5811e2@o23g2000prh.googlegroups.com>,
Allen Bong <allenbsf6502@gmail.com> wrote:
>Has anyone written a gereral delay loop calculating program in
>Applesoft, to calculate the constants required to gererate the
>required time delays in 6502 codes?
The handful of times I've needed timing-critical delays (in an audio player
way back in the day, and more recently in some hardware bit-banging code),
it was easy enough to do them manually. The 6502 datasheet tells you how
many cycles each instruction needs to execute:
http://archive.6502.org/datasheets/rockwell_r650x_r651x.pdf
(go to page 10; it's the number in the lower right corner for each
instruction)
The Apple II runs at approximately 1 MHz (it's actually a smidge faster than
that), so one cycle takes about one microsecond. The shortest run time for
any instruction is two cycles, or about two microseconds. For short delays
(small multiples of 2 us), a block of NOPs will do. For longer delays, the
math to calculate how long a loop will take is fairly simple. Consider
this:
LDY #10
]1 DEY
BNE [1
The first two instructions take 2 cycles each. The branch takes 3 cycles if
the branch is taken to the same page, 4 if the branch is taken to a
different page, or 2 if the branch isn't taken. Assuming that the code is
all on the same page (which it will be most of the time, but you'd want to
verify where the assembler decides to place your code), LDY executes once,
DEY executes 10 times, and BNE is taken nine times and not taken once.
2 LDY
10*2 DEY
9*3 BNE taken
+ 2 BNE not taken
=====
51 cycles total
For nested loops, you'd want to calculate the runtime for the inner loop and
plug that into your calculation for the outer loop.
I suppose if I had a regular need for calculating delay loops, I might've
knocked together an app to do that. As shown above, though, they're simple
enough to put together by hand as you need them.
_/_
/ v \ Scott Alfter (remove the obvious to send mail)
(IIGS( http://alfter.us/ Top-posting!
\_^_/ rm -rf /bin/laden >What's the most annoying thing on Usenet?
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---