General lightgun information
Why do I need to calibrate my lightgun?Calibration of a lightgun (or lightpen) is required for various reasons. The main reason is that there are lot's of production tolerances in the positioning of the sensor inside the gun. So in order to compensate for a misaligned sensor behind the lense, the computer must know the created offset in the measured position in order to know where the user is pointing the gun at. So the computer draws a large white square of other image in the center of the screen. Then the user presses the trigger and the computer detect the signal of the gun. It is cruacial that the user is pointing the gun at the correct location for a good calibration. Now the computer knows the offset (or error) in the position for X and Y. This offset is simply subtracted from the measured position during the game. That is why yu will hit your targets if you calibrate your gun.
But there is more. Not only the location of the sensor behind the lens is responsible for the offset in the X and Y position also the time delay in the circuitry of the gun. Keep in mind that the lightgun signal are very fast. Fast is a relative concept, but consider that the electronics are cheap, the cable is long and not shielded. All in all the lightgun design is not optimised for performance, it is optimized for profit and profit only. If not then the gun would be to expensive and nobody would buy it. As long as you don't forget to add a calibrate routine in your program there is no problem with the gun being cheap. Well back to the fast signals. The length of a video line is 64uSec (pal), for 320 pixels + borders this means that for every uSec of delay you shift approx 8 pixels to the right. Well actually that would be 4 lightpen counts because the lightgun resolution is not 320 counts but 160. The Y has the full resolution of 200 pixels and delays in the circuitry do not affect the Y position. An X offset of 25 (that would be 50 pixels or approx 16% of you screens width) is considered normal. And no problem at all, because the lightgun/pens position is a full byte (255 is max value) this means that the theoretical allowable error may be up to 95 (PAL) and that would be a very bad aligned gun with a very long cable combined with a user that does not know how to aim at the calibration target.
When you spend a lot of components you can compensate for the delays in the cable and the circuitry itself, but there is absolutely no need, the C64 electronics can compensate without any problems for the offset error. But is there is sombody out there (for a brief moment I thought about it, using a precise delay to shift the X signal to the prefect position of the next line, nobody will notice that the Y position would be 1 pixels off) who thinks he/she is smart and wants to build a lightgun you do need need to calibrate then what would be the point, the available games still have the calibrate routines and some pop-up even if you do not need them and that never changes (so that's why I did not do that).
How does a Stack light rifle, Magnum light phaser, Cheetah defender, etc. (but not the Gun stick or Nintendo zapper) lightgun/pen work?First of all it is not a real gun, it does not shoot anything. It shoots in the same way as your photocamera shoot pictures, by capturing light. It is a single pixel optical sensor and nothing more. It can only work with a CRT monitor and that must be a 50Hz or 60Hz type. (which is determined by the type of computer connected to it (PAL=50Hz or NTSC=60Hz). The lightgun sensor is much quicker then our eyes. And that's the only reason why it can work. Because it is able to detect the small flash of the phosfor pixels of your TV screen at the moment that the picture is drawn. So the lightgun is able to capture the moment of the writing of the pixel the lightgun/pen is pointing at. Because the computer knows what pixel is written, the computer knows which pixel (or set of pixels) the gun/pen is looking at once the signal of the lightgun/pen goes active. The computer saves this X,Yposition in a register for every first lightpen signal of each frame. Every other signal of the lightgun during the same frame is ignored.
There is only one small catch... it doesn't work on black (or dark colours). Because the flash of a pixel being drawn onto the screen is the brightest when the pixel is white and there is nothing drawn onto the screen when a pixel is black. So a black pixel doesn't emit light and therefore cannot be detected by the lightsensor of the lightgun/pen. Depending on the sensitivity of the lightgun/pen other colours then white might work or not. But white simply works the best. You may improve the sensitivity of your lightgun/pen by changing the brightness setting of your CRT monitor to the maximum value. Therefore the monitor will emit the most amount of light and this makes it easier for the sensor to detect it. If you want to write a game where you need to use a lot of dark coloured target that you want to hit (or want to increase the overal sensitivity to all colours), then you can use a small trick. When the trigger is pulled, make the entire screen white. You may using the screen blanking flag of the vic and set the background and border colour to white for the duration of a single frame. It does produce a tiny flicker/flash but it works. This trick is also used by the game "Operation Wolf" (which is a very good lightgun game for the C64). But as long as you (the programmer) choose your targets colour(s) wisely, then you should not require this trick.
Now how does this lightgun sensor signal looks like on a scope? Well first of all, you'd most likely see more then one pulse. Because the light sensor will be activated for every bright pixel that is in the field of view of the sensor.
On the first image you'll see that the lightgun is pointing to a bright set of pixel as it is activated (goes low) and then for the remaining 19.8mSec is does nothing (high). The frame takes 20mSec to draw to the screen (do not pay too much attention to the scope's measured period it should say 20mSec). This signal is an active low signal (the signal is the low level of the logical line the remaining time the signal is high carrying no information).
The second image shows that there are multiple lines inside the lightgun field of view. As the pulses are separated approx. 64uSec from each other, which is the timing of a PAL video line. I do not know why my scope says 61uSec, appearantly the C64 I'm using does not have that accurate timing ?!?! And practically, it does not matter at all, as long as the timing values are withing the PAL limits it can connect to the TV's input as display the signal.
Why does my keyboard respond badly when a lightgun/pen is connected?Not all lightguns/pen cause this problem but some do and that can be very anoying. Because the only way to solve it (without modifying the device) is to disconnect it and then plug it in when you need it. This problem lies in the fact that the keyboard shares IO-lines with the joystick ports. Normally this is not a problem, because when you do no use the joystick the IO-lines are not affected. A joystick consists of switches being closed when you move in a certain direction, closing these switches will short an IO-line to ground, which the computer can detect. But when the computer is scanning the keyboard while your are using the joystick then conflicts occur. The computer cannot determine if the signals changed because of a keypress or by joystick movement. This problem is less noticeble with a joystick connected to port#2. This is (most likely) one of the reasons why many programmers chose to use port#2 in their games. Which made it confusing which port to use in case of singleplayer games, resulting in Commodore users constanly swapping the joystick between port#1 and #2 depending on the game being played. The only proper sollution... by a second joystick and connect them both.
While a joystick is only a set of switches, a lightgun/pen sensor is much more sophisticated. Some designs mimic the switch approach by using open-collector circuits to drive the lightgun/pen signal. These will cause no problems if you leave the lightgun/pen untouched/pointing away from the screen (or lightbulbs), the keyboard IO-line connected to pin-6 of the joystick port#1 (while pressing the F1 key) looks like the image below (left). But the "better" or "high-end" sensors have a push-pull configuration (creates a better digital signal) but makes it very difficult for the keyboard matrix to drive the line low when a key is pressed. This is shown in the image below (middle). If you point the lightgun to the screen and push te F1 key, the the signal looks like the image below (right). You can see that the sensor prevents the IO-line from going low, but get's low enough when the sensor drives it low. Resulting is strange behaviour of the keyboard while pressing the F1 key . Therefore if the keyboard wants to pull a line low while the sensor keeps pulling it high then we have a conflict. This does not cause damage (because the sensor can't supply much current) but it is enough to stop the keyboard from working properly.
Signals of IO-pin ($DC01 bit4, connected to joystick port#1, pin-6), while pressing the F1 key.
Left: normal operation (no lightgun/pen connected or lightgun/pen pointing away from screen or modulated light sources
Middle: conflict with lightgun/pen sensor, some keys do not respond
Right: conflict with lightgun/sensor resulting is
Fixing the problem
However, for those people who are capable of handling a soldering iron AND are willing to modify there lightgun/pen, there is a fix this problem.
All we need to do, is to prevent the sensor signal from being pulled high. Using a small schottky diode. This diode allows the sensor to pull the signal down but prevent's it from pulling it up. Reducing the push-pull ouput stage of the sensor to a open-collector stage. Regarding the signal quality of the lightgun/pen, this will not affect accuracy at all, because the computer is only looking for the fast going falling edge (1->0 transition) which is unaffected by the diode. The rising edge (0->1 transition) speed is determined by the pull-up resistor in the C64 which doesn't need to be superfast at all.
Below you see a simplified representation a joystick port input line. The most right image shows the conflict problem being solved by adding a simple diode (preferably a schottky diode, because this has the lowest voltage drop). The diode prevents the push pull driver from pulling the PIN-6 line high, but enables it to go low. Just like the joystick, the image on the left. If you do not want to modify your lightgun/pen, then you can also add a small adapter that sits between the C64 and the lightgun/pen.
Please keep in mind that there allways be potential problems reading the keyboard when using the Joystick/Lightgun/Lightpen simultaneously. However most of these can be avoided by chosing the right keys in your program, keys that are not affected by the connected input device. So proper "monkey testing" of your game will always be required.
Using a lightgun/pen on a 100Hz televisionTechnically this doesn't work, simply because the speed of the image drawn onto the CRT is double the speed of the PAL computer sending the signal to the TV. However... there is a possibillity that there might be a game written that supports this. These kind of televisions weren't available in the good old days of the commodore. And now these are becoming obsolete, but there are still people around who want to play their C64 lightgun/pen games on these televisions. Depending on the frame buffer used in the television, the image is drawn in sync, but twice as fast. This means that the X and Y register of the gun will be halve the value for the same position on the screen compared to a normal 50Hz TV/monitor. No image a game where you will never be able to get the lightgun/pen position to move beyond the halve maximum value, this will mean that you wil never be able to hit anything on the right side of your screen.
Theoretically it should be possible to compensate for this, but it requires writing a dedicated to the usage of 100Hz TV's, also it will have half the lightgun/pen resolution, so accuracy will be reduced with 50%. Which isn't a big issue considering that aiming to a single pixel was an impossible challenge anyway. However how this works in practice still needs to be properly tested.
One thing is clear, you simply can't achieve the perfect score on operation wolf using a 100Hz TV.
Using a lightgun/pen on a plasma or LCD televisionTechnically this doesn't work, simply because the image is drawn not in the same way as a CRT monitor does. Therefore the image lacks th dlash of the pixels when they are drawn, making it impossible for the lightgun/pen to detect it. For more modern systems then the C64 there were guns designed that work on a different principle but still were called lightguns. This is confusing. The reason why these guns can work with any kind of TVmonitor is the fact that they don't look at the monitor, they use a small box that emits light. The gun contains a much more sophisticated sensor that can track the position of the emitted light. When calibrated properly, the tracked position is then linked to a positon of a crosshair on the screen. The WII-mote is such a controller (http://en.wikipedia.org/wiki/Wii_Remote) and technically you could say that this controller contains a tiny high speed video camera. To hide the light from the user, the light is infrared, which humans simply can't see.
Is it possible to convert a WII controller so that we can use it on a C64 or other 8-bit computer?Yes, this is possible. It isn't really difficult, but I haven't had the time to do this, yet. It would involve removing the camera from the WII controller, then using a small microcontroller the camera would be read and it's position must be converted into a signal send to the joystick port#1. However, this signal must be synchronized with the video signal coming from the VIC chip of the C64. Which would simply be a matter of sensing the H and V sync signals. So this system would have and extra lightbar underneath your TV/monitor/projectionscreen and a cable from the gun going to the composite video output and the joystickport#1.