Visual 2A03 Update

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Visual 2A03 Update

Post by Quietust »

Over the past several weeks, I've gone through nearly all of the transistor definitions for Visual 2A03 and rearranged them to properly model signal propagation order, so that when a node changes state and (de)activates multiple transistors, it should now update them in a proper nearest-to-furthest order (instead of always top-to-bottom and left-to-right, due to how they were originally generated from the vectorized layer images).

I've posted an alternate version of the simulator using these updated definitions, since I don't know whether all of the changes will have been beneficial - the original simulator already had some very specific areas updated in this manner (like the Sprite DMA logic, as mentioned in another thread), and while this bulk change may have fixed several other areas that had similar issues, it may have also broken some others, so if anybody's got any test programs to run against the simulator, I would be very interested in seeing the results.

Note that there are 3 rather large nodes whose transistors I have not yet reordered due to the sheer quantity: reset (308), clk1out (76), and apu_clk1 (539).
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
Sour
Posts: 891
Joined: Sun Feb 07, 2016 6:16 pm

Re: Visual 2A03 Update

Post by Sour »

Thanks for taking the time to do this! (I can't imagine how long this might have taken...)

I integrated the new definitions in Visual NES, and both Donkey Kong & Ballon Fight display their title screens, and the sprite RAM and palette RAM on both is correct. So at the very least, sprite DMA is fixed, and nothing too major seems to have broken. (I think my previous mention of palette RAM writes not working properly in Visual NES was just me misinterpreting the fact that these games do not initialize all of palette ram, but only some entries for their title screens)

However, sprites still aren't showing up properly in visual nes. I've done some testing by manipulating the OAM buffer in the 2a03 and waiting for the next frame. These are all 2C02-side issues (and for all I know, they might be my code's fault?), but might as well mention them here, in case they might be helpful:
-It looks like any sprite below scanline ~8 or so will not show up. e.g if I display sprite index 0 at x=0, y=0, it displays fine. But at y=5, the bottom ~5 rows are cut off.
-Increasing the tile index beyond 0 seems to break things progressively. tile index 0 displays as normal, but tile index 1 is missing its first row, tile index 2 is missing the first 2 rows, and so on. So effectively anything above tile index 7 never displays anything on the screen.
-Setting the X value seems to work as expected (displays properly at any X value on the first row)
-Attributes all appear to work (palette selection, horizontal & vertical mirroring)

So basically, setting y >= 8 or tile index >= 8 on a sprite will effectively hide the sprite, but everything other than that appears to be working as expected.

Another bug I've found is that the background color is somehow determined by the contents of palette ram at $1F, instead of $00, but everything else seems to be colored normally. In case it might be important, I generate the screen's output by reading the 5 "pal_d#_out" signals every time "hpos" changes.

I'll try running some of blargg's tests in the background and see if any of them fail unexpectedly.
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Visual 2A03 Update

Post by Quietust »

Sour wrote: Fri May 01, 2020 2:03 pm I integrated the new definitions in Visual NES, and both Donkey Kong & Ballon Fight display their title screens, and the sprite RAM and palette RAM on both is correct. So at the very least, sprite DMA is fixed, and nothing too major seems to have broken. (I think my previous mention of palette RAM writes not working properly in Visual NES was just me misinterpreting the fact that these games do not initialize all of palette ram, but only some entries for their title screens)
That's good to know.
Sour wrote: Fri May 01, 2020 2:03 pm However, sprites still aren't showing up properly in visual nes. (etc.)
Considering that I haven't touched Visual 2C02 yet, it's not surprising that there are still bugs there. When I get time, I'll start updating it too, but it might take a while.
Sour wrote: Fri May 01, 2020 2:03 pm I'll try running some of blargg's tests in the background and see if any of them fail unexpectedly.
I'm primarily interested in the APU tests, since those are the most likely to reveal simulation glitches in the 2A03.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
Sour
Posts: 891
Joined: Sun Feb 07, 2016 6:16 pm

Re: Visual 2A03 Update

Post by Sour »

Quietust wrote: Fri May 01, 2020 4:06 pmI'm primarily interested in the APU tests, since those are the most likely to reveal simulation glitches in the 2A03.
I've run through the tests labelled "apu_test" and "test_apu_2" on the Wiki (http://wiki.nesdev.com/w/index.php/Emulator_tests#APU)

apu_test: All pass, except:
3- irq_flag: Error: Writing $00 or $80 to $4017 shouldn't affect flag (Failed #6)
7- dmc_basics: Error: There should be a one-byte buffer that's filled immediately if empty

test_apu_2: All pass, except: test_2, test_6

This appears to be similar to the test results I got a few years ago: http://forums.nesdev.com/viewtopic.php? ... 30#p186599
(although the specific set of tests by blargg that I ran that time was the "blargg_apu_2005.07.30" set, which I think is older?)
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Visual 2A03 Update

Post by Quietust »

Sour wrote: Fri May 01, 2020 6:35 pm apu_test: All pass, except:
3- irq_flag: Error: Writing $00 or $80 to $4017 shouldn't affect flag (Failed #6)
7- dmc_basics: Error: There should be a one-byte buffer that's filled immediately if empty

test_apu_2: All pass, except: test_2, test_6

This appears to be similar to the test results I got a few years ago: http://forums.nesdev.com/viewtopic.php? ... 30#p186599
(although the specific set of tests by blargg that I ran that time was the "blargg_apu_2005.07.30" set, which I think is older?)
To be clear, are any of those failures new compared to the standard version of the simulator? If so, I'll need to go through and re-fix them.

In the meantime, I've started working on the Visual 2C02, accessible here. Do note that I'm only processed about 15% of the nodes (which might sound like a lot, but I did most of the easy ones first so the rest will take a lot longer), so I wouldn't bother updating Visual NES just yet.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
Sour
Posts: 891
Joined: Sun Feb 07, 2016 6:16 pm

Re: Visual 2A03 Update

Post by Sour »

Quietust wrote: Sat May 02, 2020 11:44 amTo be clear, are any of those failures new compared to the standard version of the simulator? If so, I'll need to go through and re-fix them.
I've gone through a number of tests again, so far I have not found any test that previously passed that now fails.

"sprite_ram" (which failed before), now passes, which isn't too surprising.
The "02-immediate" CPU instruction test now gives the same result as when I attempted to disable decimal mode in my code: http://forums.nesdev.com/viewtopic.php? ... 30#p186672
(so presumably some of the "illegal" opcodes might not be working properly still, but no difference compared to before, it looks like)
Also tested blargg's "full_palette" demo, and it looks like it's displaying properly, so the timing on that is good, too (had not tested this one before)

double_2007_read and dma_2007_read still have the same result as before (and fail vs what blargg noted as good results), so it sounds like the DMC channel (or the DMA logic for it) may have one or more issues (between this and the dmc_basics test I mentioned yesterday.)
oam_read still fails (same result as before), but turns out this does not use DMA at all, so I guess that's expected.

So overall, sprite DMA is fixed, decimal mode is disabled, and everything else seems to be identical to before.

Edit: Also, I didn't run the sprdma_and_dmc_dma test since that would take about 1 hour and a half to complete, but if you're interested in the result, let me know.

RE: The 2C02, thank you for working on that, too! Take however long it takes, I'll update the definitions on my end once you're done (happy to update it and run some test roms whenever you need)


(In case anybody wants it, this is an updated version of Visual NES with the 2A03 definitions updated and the ability to search/highlight nodes by their name added): https://www.mesen.ca/VisualNes_v0.2.zip)
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Visual 2A03 Update

Post by Quietust »

Somehow, I managed to get through nearly all of the 2C02 (except for pclk1/pclk2), though in the process I somehow managed to introduce a rather severe OAM corruption bug when writing to $2003. Of course, considering that writing nonzero values to $2003 is known to cause corruption, but it's not supposed to be this bad.

If necessary, I'll start over from the beginning and check for regressions more frequently, so I can at least figure out which change caused the problem.

Feel free to try out the alternate version, but be aware that I might make additional unannounced updates to it.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
Sour
Posts: 891
Joined: Sun Feb 07, 2016 6:16 pm

Re: Visual 2A03 Update

Post by Sour »

That was fast!

I tried out the new 2C02 definitions, but they seem to make things worse for sprites (in visual nes at least) - I can no longer get any sprite to show up on the screen by manipulating sprite RAM.

However, the new definitions also trigger the "infinite loop" warning (line 36 in chipsim.js) on my end. This doesn't seem to be triggering in Visual 2C02, so this is probably caused by a bug/difference in my code. I tried stripping out the CPU from the simulation, and it didn't help, either. I'll have to try tracing both simulations to see where they diverge and figure out what's causing the infinite loop to happen.
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Visual 2A03 Update

Post by Quietust »

I've done a round of updates to reorder the transistors driven by pclk0 (edit: and also pclk1), and with an update to the builtin test program, I've confirmed that all 8 sprite render units appear to be functional, at least with 8x8 sprites at Y=0 on the first few scanlines (the X counters and CHR shift registers all get filled in, and I got all 8 of them to activate in sequence). This was actually an improvement over what I started with, where only the first 3 or 4 sprite units were loading their tile data successfully.

As it turns out, the OAM corruption on write to $2004 was due to timing, and adding an additional 1-tick delay to my test program was sufficient to avoid it. I do still get some minor corruption when enabling rendering mid-scanline, though - the sprite pointer jumps from $00 to $104, and OAM $00-$07 gets copied to $20-$27.

Edit: I also finally added some "fake" logic to render the video output - rather than trying to parse the NTSC waveform, it just takes the "+++pal_dX_out" bits (at X coordinates 4 thru 259) and draws them using the PlayChoice-10 palette.

Edit: I just discovered another missing transistor in the 2A03, which I've added as "t14634b". It's located in the vicinity of the DPCM logic, so it may well have been responsible for some of the failed tests (though only possibly due to timing glitches, since it merely results in node 11466 being driven low a few nanoseconds sooner).

I've been wanting to retrace the Visual 2A03 from scratch, just because I made a lot of mistakes my first time through (mainly from developing my techniques) - maybe I should consider starting it now, rather than waiting for better layer images.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
Sour
Posts: 891
Joined: Sun Feb 07, 2016 6:16 pm

Re: Visual 2A03 Update

Post by Sour »

The latest update to the 2c02 definitions seems to have fixed the sprites, and they're now able to appear on various lines, but it seems like any scanline with sprites on them end up being corrupted to some extent. I've managed to fix the endless loop at bootup in Visual NES, but scanlines with a sprite also cause an endless loop (once per scanline), at least when running donkey kong. (Balloon fight also has similar issues, but like DK, it now displays the menu's sprite at the correct location)

As usual I can't claim this is a bug with the Visual 2C02 over my own code, but maybe this trace gives you a clue?

Code: Select all

recalcNodeList iteration: 99  (9 nodes): 
  1394 (pixel_color2), 
  1391 (pixel_color1),
  1398 (pixel_color3), 
  1388 (pixel_color0),
  7577 (selected_attr1),
  7578 (selected_attr0),
  7579 (selected_pat1),
  7580 (selected_pat0),
  1407 (+/spr_pixel_opaque_and_not_hidden_behind_bg_pixel), 
  
recalcNodeList iteration: 100  (10 nodes): 
  61 (/exp_out0) , 
  1406 (spr_pixel_transparent) , 
  1394 (pixel_color2) , 
  7578 (selected_attr0) , 
  1391 (pixel_color1) , 
  7579 (selected_pat1) , 
  1398 (pixel_color3) , 
  7577 (selected_attr1) , 
  1388 (pixel_color0) , 
  7580 (selected_pat0) , 
  
recalcNodeList iteration: 101  (4 nodes): 
  7367 (spr_pixel_opaque) , 
  1408 (+spr_pixel_opaque_and_not_hidden_behind_bg_pixel) , 
  61 (/exp_out0) , 
  1406 (spr_pixel_transparent) , 
  
recalcNodeList iteration: 102  (11 nodes): 
  7577 (selected_attr1) , 
  1524 (+spr_out_attr1) , 
  7578 (selected_attr0) , 
  1519 (+spr_out_attr0) , 
  7579 (selected_pat1) , 
  1514 (+spr_out_pat1) , 
  7580 (selected_pat0) , 
  1512 (+spr_out_pat0) , 
  1407 (+/spr_pixel_opaque_and_not_hidden_behind_bg_pixel) , 
  7367 (spr_pixel_opaque) , 
  1408 (+spr_pixel_opaque_and_not_hidden_behind_bg_pixel) , 
And then it loops back to the first step (so these 4 steps repeat forever)

As is (when breaking out of the loop after 1000 iterations), DK now toggles between these 2 frames (3rd image is from a test rom that shows all 64 sprites on the screen):
sprites.png
I also tried running the dmc_basics test again to see if the missing transistor affected it, but it gave the same error as before (I don't think this test is meant to be very timing-sensitive, so that's probably not surprising)
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Visual 2A03 Update

Post by Quietust »

With a bit of experimenting, I've figured out what's going on - the loop happens when the PPU starts drawing Sprite pixels on top of a blank Background. It was very briefly shorting the Background and Sprite pixel data together, and since Ground "wins" in NMOS, the sprite pixels were becoming blank and making the PPU decide to draw the Background instead; once that decision finished, the Sprite and Background lines were disconnected from each other, allowing the Sprite pixels to become nontransparent again and switch the PPU back to drawing the Sprite.

Moving transistor t9895 to get updated before t10712-t10715 (contrary to the physical signal propagation order) seems to have fixed it - sadly, this means that my transistor reordering technique is not universally correct, but it does seem to work most of the time.

I've also gone througyh and reordered all of the transistors driven by the APU clock in the updated Visual 2A03. Sprite DMA and the Triangle channel (which were both sensitive to timing glitches) appear to still both be functional.

Additionally, I've updated both Visual 2A03 and Visual 2C02 to pop up an alert box whenever something starts to loop, so it'll at least be more obvious when these sorts of problems happen in the future.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
Sour
Posts: 891
Joined: Sun Feb 07, 2016 6:16 pm

Re: Visual 2A03 Update

Post by Sour »

The updated definitions didn't help, unfortunately.
I'll try building a simple sprite + bg pattern in memory in Visual 2C02 and the same in Visual NES to compare and post back once I've figured out if the issue is common to both or if it's just a bug in visual nes.

Edit: Had not noticed you also said you updated the 2A03 too, I'll update those and retest for regressions on my end when I have a chance.
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Visual 2A03 Update

Post by Quietust »

New feature for Visual 2A03: audio output display. There are now 5 boxes to the right of the memory table which display waveforms for each of the builtin sound channels (square 0, square 1, triangle, noise, PCM); the first 4 are scaled vertically by a factor of 3 (so that they aren't extremely tiny), while PCM is unscaled (and is much taller as a result).

They essentially work the same as Visual 2C02's video waveform display.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
Alyosha_TAS
Posts: 173
Joined: Wed Jun 15, 2016 11:49 am

Re: Visual 2A03 Update

Post by Alyosha_TAS »

Wow more work on this is super exciting! I wish you both luck in working out the bugs here, great work so far!
Post Reply