[SOLVED] Problem with PORTB in 6522

Building your first 6502-based project? We'll help you get started here.
claw
Posts: 20
Joined: 01 Mar 2023

[SOLVED] Problem with PORTB in 6522

Post by claw »

Hello,

I have build Ben Eater's circuit of 6502.
I have written a LCD program for 4bit mode and it works. But along with that, I am trying to toggle PORTB, pin0 and it is not working.
Can anyone please look into the code and point out what is wrong?

Thanks
Attachments
delay_timer.S
(2 KiB) Downloaded 131 times
Last edited by claw on Thu Mar 02, 2023 5:29 am, edited 1 time in total.
John West
Posts: 383
Joined: 03 Sep 2002

Re: Problem with PORTB in 6522

Post by John West »

You have print_msg exiting directly to loop, skipping the write to DDRB (and also the write to PORTA). Port B will never switch to an output.
User avatar
drogon
Posts: 1671
Joined: 14 Feb 2018
Location: Scotland
Contact:

Re: Problem with PORTB in 6522

Post by drogon »

claw wrote:
Hello,

I have build Ben Eater's circuit of 6502.
I have written a LCD program for 4bit mode and it works. But along with that, I am trying to toggle PORTB, pin0 and it is not working.
Can anyone please look into the code and point out what is wrong?

Thanks
your print_msg loop jumps past the initialiser for the toggle loop, so DDRB is never initialised.

What you could do is change the

reset:

code to initialise BOTH ports, so

Code: Select all

reset:
  lda #$FF
  sta DDRA
  sta DDRB
Also, what's the clock speed?

Your delay_loop will execute 256 (outer) * 256 (inner) = 65536 times. The inner loop will take 5 clock cycles per loop, so that's 256 * 5 cycles, the outer loop will add another 5 clock cycles per execution of the inner loop, so you're looking at 256 * 5 + 256 * 5 = about 1.6 million clock cycles give or take. At 1Mhz, well that's just over 1.5 seconds, but in the Khz range of the Ben eater thing? A very very long time. Try reducing the ldy and ldx instructions to single digit numbers...

-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
claw
Posts: 20
Joined: 01 Mar 2023

Re: Problem with PORTB in 6522

Post by claw »

Thanks very much for your inputs. It works now.
I am wondering how I overlooked this trivial mistake.

And yes, the clock is 1Mhz. I am just experimenting with the delay.
I will switch to the timers for the delay shortly. However, on a side note, the delay is not 1.6 seconds as you have mentioned!.
It blinks as if the delay is around 250 to 500ms! - but that is not the topic for my concern!!!

Thanks for your time guys.
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: Problem with PORTB in 6522

Post by barrym95838 »

claw wrote:
It blinks as if the delay is around 250 to 500ms!
I took a quick stab at it and arrived at ~ 327 ms @ 1 MHz. Now, back to our regularly scheduled program ...
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)
claw
Posts: 20
Joined: 01 Mar 2023

Re: Problem with PORTB in 6522

Post by claw »

Thanks Mike.
I think your calculations should be correct.

I am actually surprised to see lots of activity in this forum! I thought forums are dead in this age.

I am looking forward to tinker more on the 6502 and custom SBCs!
User avatar
BigDumbDinosaur
Posts: 9428
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

Re: Problem with PORTB in 6522

Post by BigDumbDinosaur »

claw wrote:
Thanks very much for your inputs. It works now.
I am wondering how I overlooked this trivial mistake.

Even experienced programmers can make mistakes like that. Long, long ago, I chased a bug in a program for the better part of an afternoon, a frustration that was not soon forgotten. The mistake was a classic head-slapper: a TXA instruction that should have been TAX. Yep! Nothing more than a boneheaded typo. And, no, I'm not dyslexic. :D

The bad thing about assembly language is it’s quite unforgiving. A worse thing about assembly language is you can end up drowning in a bowl full of spaghetti. The good thing about assembly language (aside from speed of execution) is it forces you to be attentive to the details. In my experience, the best high-level programmers are those who started at the bottom, so to speak, meaning writing assembly language programs. They are the ones who have learned to pay attention to the tedious minutia in a way that both coaxes best performance from a system and reduces the tendency for bugs to erupt.
x86?  We ain't got no x86.  We don't NEED no stinking x86!
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Problem with PORTB in 6522

Post by GARTHWILSON »

claw wrote:
I thought forums are dead in this age.
How so?  Most of the forums I'm on are very active.
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?
claw
Posts: 20
Joined: 01 Mar 2023

Re: Problem with PORTB in 6522

Post by claw »

GARTHWILSON wrote:
claw wrote:
I thought forums are dead in this age.
How so?  Most of the forums I'm on are very active.
May be the ones I was on are dead (Dead in the sense, users left for some other platforms like reddit or something called discord which I don't even understand how to operate!)

I hope this forum is focusing on 6502 computer - I myself am starting on this topic though (started couple of weeks back to be honest).
I tried to build Ben Eater's 8-bit computer couple of years back (almost 60% complete). But due to the breadboard quality issue, it's still pending. Do you guys have any suggestions on good generic SBC forums where they discuss things like Ben Eater's 8-bit computer?
Since I learnt a lesson due to breadboard quality. This time for 6502, I ordered busboard BB830 ones which did not cause any problem till now.

Thanks.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Problem with PORTB in 6522

Post by GARTHWILSON »

claw wrote:
GARTHWILSON wrote:
claw wrote:
I thought forums are dead in this age.
How so?  Most of the forums I'm on are very active.
May be the ones I was on are dead (Dead in the sense, users left for some other platforms like reddit or something called discord which I don't even understand how to operate!)
I've read a few things on reddit (when linked by someone else), but I got on discord at someone's recommendation (or maybe it was a request), and like you, I can't figure it out either; and then the log-in process is very long, giving me three captchas ("Select all the pictures with crosswalks," etc.).  One forum I'm on that has been in a coma for probably 15 years, because of spammers, is Phil Pemberton's 6502ag Yahoo group, which has been migrated to groups.io since Yahoo closed Yahoo groups a few years ago, but all the old messages have not been migrated over yet.  It's a listserv though, and I have most of them in email.  In an effort to stir interest, I post a couple of times a year, but almost no one else does.

Quote:
I hope this forum is focusing on 6502 computer - I myself am starting on this topic though (started couple of weeks back to be honest).
I tried to build Ben Eater's 8-bit computer couple of years back (almost 60% complete). But due to the breadboard quality issue, it's still pending. Do you guys have any suggestions on good generic SBC forums where they discuss things like Ben Eater's 8-bit computer?

I would say 6502.org and this forum are the world hub of all things 6502.  So you've come to the right place.  I have my own very extensive mostly-6502 site (linked below), as do a few others here; but 6502.org is the hub. :D

Quote:
Since I learnt a lesson due to breadboard quality. This time for 6502, I ordered busboard BB830 ones which did not cause any problem till now.

They make a good product.  I've used ones from Global Specialties too, all American ones with a lifetime warranty, and never had any connection problems.  Nevertheless, I don't do this computer stuff on breadboards.  I did make my first CMOS 65c02 computer on breadboards and didn't have any problems, in 1986 or '7, but that was with 2MHz parts and 74HC logic which is pretty tame as far as edge rates go.
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
BigDumbDinosaur
Posts: 9428
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

Re: Problem with PORTB in 6522

Post by BigDumbDinosaur »

claw wrote:
GARTHWILSON wrote:
claw wrote:
I thought forums are dead in this age.
How so?  Most of the forums I'm on are very active.
May be the ones I was on are dead (Dead in the sense, users left for some other platforms like reddit or something called discord which I don't even understand how to operate!)
Most forums die due to inadequate moderation that allows off-topic crud to creep in. Around here, diligent moderation has kept spammers out. Also, we try to avoid getting into hot-button things, such as politics and “my computer is better/faster/cheaper/prettier than yours” flame wars.

Quote:
I hope this forum is focusing on 6502 computer...

This place isn't called 6502.org for nothing. We try to keep chatter about Intel, Motorola and Zilog MPUs down to a dull roar. :D

Quote:
Do you guys have any suggestions on good generic SBC forums where they discuss things like Ben Eater's 8-bit computer?

I’m not aware of any such forums—this one is arguably the most useful when it comes to getting help with a 6502 unit.

A problem that has been noted—which results in people coming here seeking help—is that Mr. Eater glosses over the practical aspects of constructing a working unit and in some cases, omits information that the builder needs to complete a successful build. There is more to it than rounding up some parts and wiring things together. Usually when someone comes here looking for help after trying to build a Ben Eater unit we suggest they visit Garth’s 6502 website so they can read his 6502 primer and get some of the gaps in their knowledge filled in.

Quote:
Since I learnt a lesson due to breadboard quality. This time for 6502, I ordered busboard BB830 ones which did not cause any problem till now.

Poor-quality breadboard has sabotaged many a project. Even use of a premium-quality breadboard doesn’t guarantee anything. Those long wires can toss a ton of inductance into the circuit and prevent things from working. Things such as switching noise and ground-bounce can make what appears to be an otherwise correctly-built unit unstable or DOA.

Oops! Garth’s post appeared right before mine. So you get to read essentially the same thing twice. :D
x86?  We ain't got no x86.  We don't NEED no stinking x86!
claw
Posts: 20
Joined: 01 Mar 2023

Re: [SOLVED] Problem with PORTB in 6522

Post by claw »

Thanks very much for your insights, Mr. Garth and Mr. Dinosaur.

I agree that it is frustrating sometimes when Ben glosses over few things in his videos. But nevertheless, I started this hobby after seeing Ben's videos. Thanks for the 6502 primer. I will definitely go through it (given the experience you guys have on this field).

Also on the breadboard part, most frustrating is such parts like the DIP switches which smaller leads popping out randomly!

While watching Ben's videos, I was under the impression that those are the only information I have apart from the datasheets. But now, I see that this site is a gold mine and many experienced engineers both in profession and age :)
User avatar
drogon
Posts: 1671
Joined: 14 Feb 2018
Location: Scotland
Contact:

Re: Problem with PORTB in 6522

Post by drogon »

barrym95838 wrote:
claw wrote:
It blinks as if the delay is around 250 to 500ms!
I took a quick stab at it and arrived at ~ 327 ms @ 1 MHz. Now, back to our regularly scheduled program ...
Well I'm intrigues now - obviously my back of an envelope calculations were an order of magnitude out, so let me see if I can get it right.. Just because:

This is the code:

Code: Select all

delay_loop:
 ldy #$ff
delloop1:
 ldx #$ff
xloop1:
 dex
 bne xloop1
 dey
 bne delloop1
 rts
So the inner loop is:

Code: Select all

xloop1:
    dex
    bne  xloop1
The dex is 2 cycles and the bne is 3 cycles, (but only 2 on the very last one which I never bothered counting before). X in initialised at 255, so the loop count in 255, so that's 254 loops of 5 cycles and one loop of 4 cycles = 1274 cycles.

The outer loop also runs 255 times, so the inner loop will run 255 times 1274 cycles = 324870 cycles.

The outer loop also has an overhead - same as the inner loop which is 254 * 5 + 4 = 1274 cycles.

And that needs to be added (not multiplied which I did before) to the grand total giving 324870 cycles or about 1/3 of a second at 1Mhz.

(plus a tiny overhead for jsr/rts)

Phew!

FWIW: I've used the WAIT routine out of the Apple II monitor in the past for a "calibrated" delay - not really needed here but maybe worthwhile knowing about.

-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Problem with PORTB in 6522

Post by BigEd »

BigDumbDinosaur wrote:
Quote:
Do you guys have any suggestions on good generic SBC forums where they discuss things like Ben Eater's 8-bit computer?
I’m not aware of any such forums—this one is arguably the most useful when it comes to getting help with a 6502 unit.
I'd mention the retrobrewcomputers forum and the VCFederation forums, as well as the Stardot forums, as being active, well-moderated, and adjacent to these interests. You'll get similar advice over on anycpu.org forums, although there are far fewer people there.
claw
Posts: 20
Joined: 01 Mar 2023

Re: Problem with PORTB in 6522

Post by claw »

drogon wrote:
And that needs to be added (not multiplied which I did before) to the grand total giving 324870 cycles or about 1/3 of a second at 1Mhz.
-Gordon
I should say you have got a lot of patience. One that I always try to acquire from your generation. And I suppose your username seems to be an anagram of your name? or is it a conincidence? :)
BigEd wrote:
I'd mention the retrobrewcomputers forum and the VCFederation forums, as well as the Stardot forums, as being active, well-moderated, and adjacent to these interests. You'll get similar advice over on anycpu.org forums, although there are far fewer people there.
I went through these. vcfed is something like a collectors forum! stardot and retrobrew looks similar to this site. I feel retrobrewcomputers has some really interesting stuff though! thank you.
Post Reply