Emulating NES CPU and PPU on PIC32, too slow?

Let's talk about anything related to the 6502 microprocessor.
sburrow
Posts: 833
Joined: 09 Oct 2021
Location: Texas

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by sburrow »

barnacle wrote:
So I'd try it just as you've drawn it and see if it works for you.
Thank you for this. I know it's been a few days now, but you have inspired me to go ahead with some breadboarding to see if I'm satisfied. I'm leery of using the op-amp because of a few reasons: 1) I don't have higher voltages on the board, and I can only assure up to about 3.7V from a battery at any moment, and 2) I honestly haven't learned much about op-amps and still do not know exactly how they work.

I'll keep y'all updated. But thank you for the 'go ahead' so that I can start testing :)

Chad
User avatar
Dr Jefyll
Posts: 3525
Joined: 11 Dec 2009
Location: Ontario, Canada
Contact:

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by Dr Jefyll »

barnacle wrote:
what that output load does to the linearity of your DACs
Interesting point, Neil -- I myself might've overlooked the linearity issue entirely. :roll: But my experience with DACs is minimal, so I decided to pay a furtive visit to Wikipedia!

In the section entitled Current Mode, the WP page says, "Since the output impedance is independent of digital code, the analog output may equally well be taken as a current into a virtual ground," which to me suggests we needn't worry about the load seen by the output of the R-2R resistor ladder. (Thus Chad's 100K resistors could perhaps be made smaller.) But apparently (per Fig 5 in one of the references) the impedance of the gates which drive the inputs of the ladder can be a concern. Comments, anyone? And, Chad, can you tell us where AUDIO-LEFT-A and the other eleven signals come from? Do they come straight from the PIC? (I'm hoping they originate from a device belonging to a logic family with strong output drive.)

-- Jeff
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
sburrow
Posts: 833
Joined: 09 Oct 2021
Location: Texas

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by sburrow »

Dr Jefyll wrote:
And, Chad, can you tell us where AUDIO-LEFT-A and the other eleven signals come from? Do they come straight from the PIC? (I'm hoping they originate from a device belonging to a logic family with strong output drive.)
They come directly from the PIC32. As far as how much drive they have between 15mA and 25mA on any individual pin. Not a whole lot, but I haven't really had issues with it so far.

I'm reading your response and Neil's, and digesting it still. I went and watched an EEV Blog tutorial video on Op Amps just a bit ago, and I am learning more about that now, thus understanding more.

Thank you Jeff!

Chad
User avatar
Dr Jefyll
Posts: 3525
Joined: 11 Dec 2009
Location: Ontario, Canada
Contact:

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by Dr Jefyll »

Well, even 15 mA implies the pin's output impedance is somewhere very roughly in the neighborhood of 30 ohms (would need more info to get a proper estimate). Ideally we want the pin to have an impedance of zero, but it is only driving a resistor of 20K anyway. In effect, we've changed that resistor to 20,030 ohms, which I think is tolerable given that the DAC has pretty poor resolution to begin with (ie, only 6 bits).

Getting back to those 100K resistors that mix the Left DAC & Right DAC outputs into "Mono," reducing those 100K values would increase the amplitude going into the potentiometer and the power amp... at the expense of an increase in crosstalk, or "potential for signal to go back up the other leg," as Neil put it. But it may not be a big problem if a small amount of the Left signal can be heard on the Right output, and vice versa.

-- Jeff
Attachments
AudioChannelMixer1.png
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
barnacle
Posts: 1831
Joined: 19 Jan 2004
Location: Potsdam, DE
Contact:

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by barnacle »

Dr Jefyll wrote:
the analog output may equally well be taken as a current into a virtual ground," which to me suggests we needn't worry about the load seen by the output of the R-2R resistor ladder.
I think a poor choice of words on Wiki's part, perhaps... it's current into a virtual earth but it needs that high load impedance. So using an opamp - there are lots of rail-rail 5v opamps out there; frequency response isn't going to be a factor here - to both provide that virtual earth and the channel mixing is an obvious 'two birds, one stone' solution.
Screenshot from 2025-05-05 11-18-01.png
This is an as-yet untested 3-bit ADC to provide 8-level gray-scale video at VGA frequencies, so the op-amp is a bit more enthusiastic, but you can see it's a standard r-2r ADC driving a high input impedance video amplifier.

Neil
sburrow
Posts: 833
Joined: 09 Oct 2021
Location: Texas

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by sburrow »

Dr Jefyll wrote:
But it may not be a big problem if a small amount of the Left signal can be heard on the Right output, and vice versa.
I am certainly not looking for perfect audio quality here :) But being able to differentiate between left and right on the headphones would be nice. Perhaps an amount between 10K and 100K, whatever I end up being happy with?

[ And thank you for the monochrome schematic, I forgot! ]
barnacle wrote:
This is an as-yet untested 3-bit ADC to provide 8-level gray-scale video at VGA frequencies, so the op-amp is a bit more enthusiastic, but you can see it's a standard r-2r ADC driving a high input impedance video amplifier.
I see you are feeding it directly to the VGA output. I have done *many* VGA circuits and have never needed any type of amplification, I think the most I had one time was using a 74HC' latch. As in, I get what you have and don't doubt you, but I think that an 8 ohm speaker would need more "umph" than VGA. Am I wrong?

Thank you both for this help.

Chad
barnacle
Posts: 1831
Joined: 19 Jan 2004
Location: Potsdam, DE
Contact:

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by barnacle »

Oops, I forgot to monochrome that screen capture. Sorry.

The VGA circuit is more complex than that, since it also provides for independent 1-bit RGB options... there's a digital buffer off-screen to the left, to keep the two options separate.

Most video circuits require 1v into a 75ohm load impedance, around ten milliwatts. Ten milliwatts average power is respectably loud for a single small speaker (probably a 1W rating); each time you increase the power by a factor of ten, the volume is perceptually doubled. Your eight ohm speaker will pull three-fifths of an amp at five volts, but you have the audio amplifier to handle that. I wouldn't use a direct drive from a logic part - 20-40mA max - to drive the speaker, but I'd use at least a small transistor, or as you have done, an amplifier chip. Class D makes it power efficient.

Neil
User avatar
Dr Jefyll
Posts: 3525
Joined: 11 Dec 2009
Location: Ontario, Canada
Contact:

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by Dr Jefyll »

barnacle wrote:
I'd use at least a small transistor, or as you have done, an amplifier chip. Class D makes it power efficient.
Agree on that, and in general with most of Neil's points. But a misunderstanding may be lurking in regard to a remark in the final paragraph of this post. Unless I'm somehow misreading, the drift is that, if the output of the R-2R ladder is attached to a load whose impedance is inappropriate, then linearity will suffer... which I take to mean the analog output won't always be proportional to the digital input. For this particular DAC, at least, I'm not convinced that's true.

My key assumption is that none of the inputs to the R-2R network is ever left floating. Instead, every input is always connected either to a low impedance source of the Reference Voltage or to a low impedance source of 0 volts. As a result, the output impedance of the network will never vary, regardless of what combination of inputs is high or low. In these conditions, the output impedance of Chad's network will always be 10K. If the six-bit input is binary 000000 then the network's output behaves like an imaginary low impedance source of 0 volts with a 10K resistor in series. When we increment the inputs to successively higher values, the behavior is as if the source has proportionally increased its voltage (and we still have a 10K resistor in series).

We can infer the rest. If the load connected to the network output (ie, downstream of said resistor) is infinity ohms then the network output voltage will ramp linearly all the way from zero volts up to the maximum (and the output current will be zero). At the other extreme, if the network output attaches to ground (virtual or otherwise :) ) then it's the current that ramps up while voltage remains at zero. The results of intermediate load values are also easily predictable.

All this to say, I think the 100K resistors in Chad's circuit could be reduced in value substantially. This'll ameliorate the gain loss which Neil noted, and IMO the linearity of the DACs won't be impaired.

-- Jeff
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
sburrow
Posts: 833
Joined: 09 Oct 2021
Location: Texas

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by sburrow »

Dr Jefyll wrote:
All this to say, I think the 100K resistors in Chad's circuit could be reduced in value substantially. This'll ameliorate the gain loss which Neil noted, and IMO the linearity of the DACs won't be impaired.
So, some updates. See attached picture for the mini-circuit I'm working with right now.

I bought some LM386's and they finally came in the mail. Not wanting to do eBay for something like that again, stick to Mouser! Anyways, I set up the Gain=200 circuit from the datasheet, and fed it an audio signal from the desktop computer to test, at 5V power supply. [ Mind you, all of this is on a *breadboard*, so I don't really trust it much. ]

I replaced the pot with an in-line resistor and a 10K resistor to ground. When the in-line resistor is 0 ohms it's loud enough, but lots of distortion. Like "I want to blow the speakers" kind of thing. Makes sense. When the in-line resistor is 10K ohms, it's still loud, a bit better, still lots of noise. When the in-line resistor is 100K ohms, it's "loud" but full of static. Like, I can hear the music in the background, but the hum over-rides it a lot.

I then tested to see if the LM386 would work at 3.3V, and it does seem to do so! I don't see much of a difference in output really or distortion, so that's nice. The breadboard is SUPER annoying, and not wanting to play nicely, ever. But I did get it to work when I held some wires and pressed something else down at the same time.

So, I think I'm going to assume what Jeff is talking about. The 100K ohm resistors can be reduced a lot, and a little bit of feedback on the other channels will just have to be as is. I'm not looking for absolute clarity, just some distinction between left and right, and I think I can still achieve that with some 10K ohms. I will test this theory out more fully later, but now I have to run to the college's graduation ceremony!

Thank you everyone!

Chad
Attachments
LM386.png
LM386.png (20.01 KiB) Viewed 2372 times
sburrow
Posts: 833
Joined: 09 Oct 2021
Location: Texas

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by sburrow »

I was able to spend a bit more time with this this morning. Here are some findings:

1) The LM386 does not want to run at 3.3V. I had previously thought it did, when I pushed some wires, etc. Me pushing those wires probably connected VCC to 5V instead of 3.3V. When I pull the ground from the LM1117-3.3, it makes the 5V turn into 4.3V or so, which is suitable for the LM386, and so it runs. I connect the ground pin to ground, VCC turns into 3.3V and the LM386 shuts down. Could be the breadboard still, but I'm suspecting it simply doesn't want to work outside of it's 4V minimum spec.

2) I got the sound quality to be drastically improved. I put in some capacitors on the power rail and that helped a lot. I also removed the gain cap to make it Gain=20 and that improved the sound quality a lot as well. If I raise the volume from the desktop, it starts to get scratchy, probably because it's 'blowing out the speakers'. It sounds like it at least.

3) Using the in-line 100K resistor works, but it is very faint unless you crank up the volume, and thus making it more scratchy. 10K in-line seems great. I then connected two different channels at once using the 10K resistors and they seemed to be working great. I could distinctly hear each channel, and if I lowered the volume of one channel the other one was heard more clearly. So I think 10K 'mixer' resistors seem fine enough for this project. This test is far from perfect, but it's proving a point: The higher the in-line resistor, the more faint the signal, the more scratchy the speaker will sound when at audible levels.

Alright, I have learned some stuff! Now to apply it :) Thank you everyone, I appreciate your input and guidance.

Chad
barnacle
Posts: 1831
Joined: 19 Jan 2004
Location: Potsdam, DE
Contact:

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by barnacle »

A quick look at the LM368 datasheet suggests that you may be confusing things... A gain of 200 on a 5v supply implies a maximum input of around 25mV peak-peak (or 250mV for gain 20). You should be getting between zero and five volts from that DAC; even with the potential divider losses we've discussed I would expect you're massively overdriving the amplifier and that's one reason it's sounding so bad - are you able to sling a scope probe across the DAC output? Also, are you using a capacitor to ground to reconstruct the signal?

Neil
User avatar
Dr Jefyll
Posts: 3525
Joined: 11 Dec 2009
Location: Ontario, Canada
Contact:

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by Dr Jefyll »

I agree on the "massively overdriving" point. In my last post I mentioned the gain loss problem... but, even in the context of that other power-amp IC (the PAM8202AAD), it's uncertain whether said problem really is a problem. And with the LM386, a gain of 200 is actually excessive.

As for the "super annoying" breadboard, it sounds as if you need a better quality product. I'm no expert myself, but Radical Brad has more than a little experience with breadboards ( :shock: ), and the ones he buys are from Twin Industries.

-- Jeff
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
sburrow
Posts: 833
Joined: 09 Oct 2021
Location: Texas

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by sburrow »

barnacle wrote:
A gain of 200 on a 5v supply implies a maximum input of around 25mV peak-peak (or 250mV for gain 20).
OH! Well, that makes sense! Hm!

I just pulled out the scope, and yeah, I'm reaching into the 500mV range constantly, but rarely all the way to 1V. Well there ya have it :) Thank you for that insight.
Dr Jefyll wrote:
As for the "super annoying" breadboard, it sounds as if you need a better quality product. I'm no expert myself, but Radical Brad has more than a little experience with breadboards ( :shock: ), and the ones he buys are from Twin Industries.
I don't think I got the ones like Brad, but I did get the ones Ben Eater recommended. And they were still junk, honestly. Yeah, maybe I just don't know the principles of breadboards, but I have had so little success with them in the past. I'm not wanting to figure out why they don't work well for me, I'm just aggravated that I had to use one at all given my past experiences.

Did you know, some people just do not like olives. WHAT?! I suppose if they are forced to eat them, or mix them with other foods, cover them in sugar, whatever to get them to eat it they might eat it. But in the end, they still don't like olives. *shrug* Same with me and breadboards. Nothing will cure me of my dislike of the things, nothing. When my PCB's act *flawlessly* and my breadboards change signals when I wave my hand near it or shift my legs under the desk (literally!), then I'm just not interested in breadboards. :)

Thank you both! I have a plan to not use the LM386, nor the PAM8320, but instead the TPA2000D1. But now that you told me about input voltage max, I'm going to feed it directly from the R2R DAC, before the polarized capacitor, not after. Thank you for saving me there :)

Thank you both. I hope to have some new news within a couple of weeks!

Chad
barnacle
Posts: 1831
Joined: 19 Jan 2004
Location: Potsdam, DE
Contact:

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by barnacle »

That's why I made those prototyping cards on a breadboard layout (but also able to accommodate SOIC as well as DIP)...

Neil
sburrow
Posts: 833
Joined: 09 Oct 2021
Location: Texas

Re: Emulating NES CPU and PPU on PIC32, too slow?

Post by sburrow »

Hello everyone! I have updates!

See attached pictures. I now have a fully portable yet 'switchable' NES emulator!

Long story short, I made a new PCB with buttons on it and with places for a speaker, battery, and LCD screen. I then altered my code to display on either the VGA display or the LCD display, switchable via menu or by detecting the presence of controllers.

This is capable of 4 players at once, since I have the handheld itself, two Genesis controllers, and then a USB controller. Speed wise the LCD is just as fast as the VGA, so that's good, that is full speed at 20 FPS essentially. Having USB plugged in slows it down by about 10%, and some games like Mario 3 are just a tiny bit slow for some reason, but overall it is functioning very well.

Next up is to modify this external case, a lot. Obviously I did 'bodges' on it, haha. Lots of little optimizations there needed, but having a 3D printer now helps a lot with that.

I don't have my code or anything else on GitHub at the moment, still making everything *just perfect*.

There's the update, another success! Thanks everyone.

Chad
Attachments
20250611_152359.jpg
20250611_150903.jpg
Post Reply