Delay loops in asembly
Delay loops in asembly
I am trying to add HD44780 character display to my 6502 project.
Currently I do not have any timer which could be used for convenient time keeping and implementation of delay loops - it will be added with the next I/O board. Until then I need some workaround, because initialization routine of HD44780 requires several waiting periods, before busy flag becomes accessible.
Is there a easy way to calculate how much time simple loop takes to complete on specific 6502 CPU, clocked with specific frequency? And then execute that loop as many times as necessary? Simply speaking i need an equivalent of delay_us() and delay_ms() functions.
Currently I do not have any timer which could be used for convenient time keeping and implementation of delay loops - it will be added with the next I/O board. Until then I need some workaround, because initialization routine of HD44780 requires several waiting periods, before busy flag becomes accessible.
Is there a easy way to calculate how much time simple loop takes to complete on specific 6502 CPU, clocked with specific frequency? And then execute that loop as many times as necessary? Simply speaking i need an equivalent of delay_us() and delay_ms() functions.
Re: Delay loops in asembly
Is there an easy way? Well, you need to count cycles. You can do that by hand, looking up cycle counts in documentation, or you can run a suitable simulator like visual6502, or (possibly) Kowalski's emulator.
Or, perhaps someone has already done the work... here's a usenet posting:
To convert from cycles to milliseconds, you need to know the clock frequency of your 6502.
Or, perhaps someone has already done the work... here's a usenet posting:
Quote:
For my usual loop:
Code: Select all
ldy #ycnt ; (2 cycles)
ldx #xcnt ; (2 cycles)
delay dex ; (2 cycles)
bne delay ; (3 cycles in loop, 2 cycles at end)
dey ; (2 cycles)
bne delay ; (3 cycles in loop, 2 cycles at end)-
JenniferDigital
- Posts: 92
- Joined: 25 May 2015
Re: Delay loops in asembly
Don't forget that the times required for the HD44780 and compatibles, as far as I can remember are minimum times, so it's ok to be a few cycles over. Also, there's the busy flag you can use after initialisation. That will allow your code to run as fast as the display will allow.
Last edited by JenniferDigital on Wed Aug 29, 2018 6:08 pm, edited 1 time in total.
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Delay loops in asembly
Atlantis wrote:
I am trying to add HD44780 character display to my 6502 project.
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?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: Delay loops in asembly
DigitalDunc wrote:
Don't forget that the times required for the HD44780 and compatibles are as far as I can remember are minimum times, so it's ok to be a few cycles over. Also, there's the busy flag ypu can use after initialisation. That will allow your code to run as fast as the display will allow.
- BigDumbDinosaur
- Posts: 9425
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
Re: Delay loops in asembly
BigEd wrote:
or you can run a suitable simulator like visual6502, or (possibly) Kowalski's emulator.
x86? We ain't got no x86. We don't NEED no stinking x86!
Re: Delay loops in asembly
BigEd wrote:
... to count cycles... you can run a suitable simulator like visual6502, or (possibly) Kowalski's emulator..
Re: Delay loops in asembly
BigEd wrote:
Or, perhaps someone has already done the work...
Code: Select all
{ } ; delay 9*(256*A+Y)+8 cycles
{ } ; assumes that the BCS does not cross a page boundary
{ } ;
{ 8000 C0 01 } .1 CPY # 1 ; 2
{ 8002 88 } DEY ; 2
{ 8003 E9 00 } SBC # 0 ; 2
{ 8005 D0 F9 } BCS =.1 ; 3
Also, a good exercise for beginners is to analyze this routine and understand how it works (what, for example, is the purpose of the CPY?), and verify that the cycle count formula is correct.
- barrym95838
- Posts: 2056
- Joined: 30 Jun 2013
- Location: Sacramento, CA, USA
Re: Delay loops in asembly
dclxvi wrote:
Code: Select all
{ } ; delay 9*(256*A+Y)+8 cycles
{ } ; assumes that the BCS does not cross a page boundary
{ } ;
{ 8000 C0 01 } .1 CPY # 1 ; 2
{ 8002 88 } DEY ; 2
{ 8003 E9 00 } SBC # 0 ; 2
{ 8005 D0 F9 } BCS =.1 ; 3
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!
Mike B. (about me) (learning how to github)
Mike B. (about me) (learning how to github)
- BigDumbDinosaur
- Posts: 9425
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
Re: Delay loops in asembly
BigEd wrote:
BigEd wrote:
... to count cycles... you can run a suitable simulator like visual6502, or (possibly) Kowalski's emulator..
x86? We ain't got no x86. We don't NEED no stinking x86!
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Delay loops in asembly
barrym95838 wrote:
That level of efficiency causes my brain to do a little happy dance. Nicely done, sir!
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?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: Delay loops in asembly
BigDumbDinosaur wrote:
I think Daryl's latest revision (1.2.14a) addressed the cycle count inaccuracies.
Re: Delay loops in asembly
GARTHWILSON wrote:
before someone faxed us an ap. note about this. (This was before the internet.)
Like that time my kid first saw a payphone and wondered what it was. LOL
Cat; the other white meat.
- BigDumbDinosaur
- Posts: 9425
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
Re: Delay loops in asembly
cbmeeks wrote:
GARTHWILSON wrote:
before someone faxed us an ap. note about this. (This was before the internet.)
Like that time my kid first saw a payphone and wondered what it was. LOL
x86? We ain't got no x86. We don't NEED no stinking x86!
OT: telephony nostalgia
Hey, if you don't know how to operate the round thing, you can always dial by hook-flashing... defeats dial-locks too! So I hear.
In fact, that could be a useful application for a delay loop.
In fact, that could be a useful application for a delay loop.