I'm attempting to emulate the NES PPU and I'm implementing its registers. In particular, reading from the PPU status register has the following effects:

Reading the status register will clear D7 mentioned above and also the address latch used by PPUSCROLL and PPUADDR. It does not clear the sprite 0 hit or overflow bit.

Source

The PPUSCROLL and PPUADDR registers are both write only. When writing to PPUSCROLL for example, the first write specifies and X-scroll position, and the second write specifies the Y-scroll position.

Is the latch the thing that determines the write state (x or y)? I just want to make sure I'm understanding this correctly.

Disclaimer: I don't own a NES, aI haven't programmed a NES, nor have I written a NES emulator. The following is just my understanding of the description.

A latch is a set of D-Flip-Flops that is used to temporarily store a value. So an address latch stores an address temporarily. Both PPUSCROLL and PPUADDR need to provide a 16-bit address (or x-y-value), so both would have two 8-bit latches which are filled in turn with 8-bit values when writing to them.

To select one of the 8-bit latches, one would need an address latch toggle. It looks like this toggle (another kind of flip-flop) is re-used to control both the PPUSCROLL and the PPUADDR latch pair. It's reset on reading either of them, and it's also reset by reading PPUSTATUS. The description you reference seems to use the term address latch to both to refer to the proper latches, and the latch toggle (which should be fixed IMHO).

So, yes, it's the thing that determines the write state (high byte or low byte for PPUADDR, x or y for PPUSTATUS).

A question that's not answered is what happens on the third write: Will the value appear again on the first latch (the toggle toggles again), or on the second latch (toggle stays at it is)? If you do have a real NES, it would be interesting to find out.

Latches are used to hold data temporarily. They're an essential building block of chips and they they were used to simplify interfacing between the CPU and other hardware as CPUs had data registers that couldn't hold a full address (for example 8 bit data registers vs 16 bit addresses for the 6502)

In software terminology (in computer / game console interfaces), it refers to a very common setup I'll try to explain.

Let's suppose you need to write a 16 bit address to a video register with the 6502:

lda #address&255
sta VIDEOREGISTER
lda #address/256
sta VIDEOREGISTER + 1

how would the hardware know when the address is complete? it needs a trigger to make a copy of the value; for example when VIDEOREGISTER + 1 is written.

To simplify the logic, it uses a latch:

lda #address&255
sta VIDEOREGISTER
lda #address/256
sta VIDEOREGISTER

The first write stores the first value, the second write get the second part and triggers the operation.

It is smaller to implement in hardware that way since it is essentially two set (8 bits each) of flip-flops chained with one another (like a shift register). Each write triggers the copy of flip-flop1 to flip-flop2 and the value written goes to flip-flop1. One the second write, you have both values needed "latched".

In this scenario, when you read the status register, it resets the cycle; so if one write was already done, it's gone, and the latch is ready to receive an address through 2 writes.

This is useful since you don't know the status of the hardware after a reset, where maybe the cpu did one write before and then all writes to that register would subsequently not work as expected.

If what is being emulated is SNES functionality: The SNES used the 65816, an extended 6502 that could do 24-bit addressing - but still came in a 40 pin case which obviously allows no space for 24 actual address lines, so a partially multiplexed address bus was used - the same 8 lines sometimes acted as the data bus, sometimes as the address bus. In order to have the 24 address lines available for actually addressing anything, one needed to store the contents of the 8 combined lines in a latch at the right time in the machine cycle, so they were available (at the output of the latch) while these lines were used as the data bus.

This kind of multiplexed interface is commonly found on 40-pin microprocessors in various forms, eg on the 8086/8088 and 8031 - note the ADx pins and the ALE (address latch enable. And yes, makes you think the designers were drunk at times...) pin on these.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.