Page 1 of 2
[SOLVED] Problem with PORTB in 6522
Posted: Wed Mar 01, 2023 2:36 pm
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
Re: Problem with PORTB in 6522
Posted: Wed Mar 01, 2023 3:13 pm
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.
Re: Problem with PORTB in 6522
Posted: Wed Mar 01, 2023 3:24 pm
by drogon
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
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
Re: Problem with PORTB in 6522
Posted: Thu Mar 02, 2023 3:08 am
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.
Re: Problem with PORTB in 6522
Posted: Thu Mar 02, 2023 3:23 am
by barrym95838
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 ...
Re: Problem with PORTB in 6522
Posted: Thu Mar 02, 2023 4:45 am
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!
Re: Problem with PORTB in 6522
Posted: Thu Mar 02, 2023 5:20 am
by BigDumbDinosaur
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. 
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.
Re: Problem with PORTB in 6522
Posted: Thu Mar 02, 2023 5:28 am
by GARTHWILSON
I thought forums are dead in this age.
How so? Most of the forums I'm on are very active.
Re: Problem with PORTB in 6522
Posted: Thu Mar 02, 2023 7:16 am
by claw
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.
Re: Problem with PORTB in 6522
Posted: Thu Mar 02, 2023 8:02 am
by GARTHWILSON
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.
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.
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.
Re: Problem with PORTB in 6522
Posted: Thu Mar 02, 2023 8:08 am
by BigDumbDinosaur
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.
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. 
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.
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. 
Re: [SOLVED] Problem with PORTB in 6522
Posted: Thu Mar 02, 2023 9:40 am
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

Re: Problem with PORTB in 6522
Posted: Thu Mar 02, 2023 11:41 am
by drogon
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:
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
Re: Problem with PORTB in 6522
Posted: Thu Mar 02, 2023 12:28 pm
by BigEd
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.
Re: Problem with PORTB in 6522
Posted: Thu Mar 02, 2023 12:56 pm
by claw
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?
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.