Ian wrote:How does the screen flash method work?
It's quite ingenious. Old-fashioned CRT light guns worked by having a single photodiode in the gun with a lens to focus light on it. So imagine a single-pixel sensor looking for a spike in brightness. A CRT scans line by line from top to bottom and the phosphors that are illuminated are of course brightest at the moment they are irradiated by the electron beam and then quickly decay. This means that if you look at a CRT in slow motion, you can see the current pixel being drawn. See
this video for an example.
Now, the system actually knows exactly where the raster beam is at all times because the CRT beam position is controlled by the video signal. Makes sense if you think about it: CRT TVs were analog devices that output exactly what was fed to them without any buffering. How rapidly the beam goes back and forth and up and down is controlled by horizontal and vertical sync signals that implicitly define the resolution being displayed. On old 8- and 16-bit systems, CPU cycles per scanline were known, and there were V-blank and H-blank (per line) interrupts. So the CPU could deduce at any point where the raster beam was.
When you press the trigger on the lightgun, the game sees that as it would any other controller button press. Except for a lightgun game, it displays a white screen for one frame. The next frame will therefore be brighter than the last one and the gun's sensor can then look for the spike in brightness. When the beam crosses the field of view of the lightgun's sensor, the light gun sends a signal back to the game system (another digital input, just like a button press), and the game records the beam position at that point (because it knows where the beam is). That is the (x,y) coordinate that the gun must have been pointed at! The gun's aim is deduced by the CPU based on the timing of when the lightgun's single-pixel sensor saw a brightness spike. This is why lightguns don't work on LCD and LED displays. I think there are even some newer CRTs where they fail to work.
Exactly how this is done depends on the system. On the Sega Genesis, there was a hardware "HV counter" that could be read at any given time and gave the current vertical and horizontal position of the raster beam during the active frame. This could also be latched. Peripherals like the Menacer light gun were able to cause the HV counter to be latched at the time the trigger was pressed, generate a CPU interrupt, and then the CPU could read the counter value at the time it was latched. See page 35
here.
I'm not sure how Model 3 communicates raster beam timing. It's clear that the trigger is simply read by the PowerPC as a digital input and the screen is flashed for a frame. But then the X, Y position is read out directly. I assume that something similar to the HV counter exists and is piped into whatever logic implements that input register directly. It's clearly not done by the CPU.
Here's a good video about lightguns and light pens.