Programming Light Gun

Started by MIRKOSOFT, January 19, 2010, 01:24 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

MIRKOSOFT

Hi!

I have light gun and I want to programming it.

In BASIC 7 it is simple, PEN(0) and PEN(1) are coordinates.

But what's not easy?

HOW TO DETECT THAT LIGHTGUN's FIRE-BUTTON WAS PRESSED?

Thanks for every help.

Miro
MIRKOSOFT of megabytes

Commodore 64 was great, Commodore 128 is bigger, better, faster and more!!!

http://www.mirkosoft.sk

MIRKOSOFT

... so, I found in manuals, books etc. ... that PEEK(56321) - control port1
IT WORKS NOT - everytime is value 255

I found also by indicate SHIFT key pressed
IT WORKS NOT - everytime not shifted...

So, how?
MIRKOSOFT of megabytes

Commodore 64 was great, Commodore 128 is bigger, better, faster and more!!!

http://www.mirkosoft.sk

Hydrophilic

I guess it depends on how light gun is made.  If no change when you peek($dc01), then it is not wired like standard trigger on joystick / paddle / mouse (these send signal as long as you hold them down).

My guess is it sends very short pulse on /PEN line of controller port 1... it is so quick a standard program may miss it, but it should be enough that VIC-II will sense /PEN and update light pen registers.

Try this BASIC program that I just made up!

10 COLLISION 90,3:REM GOSUB 90 WHEN /PEN ACTIVATES
20 DO:LOOP:REM DO NOTHING WHILE WAITING
30 END
90 ?PEN(0)-24,PEN(1)-50:RETURN

Remember COLLISION only works for 40-column VIC-IIe and not 80-column VDC (the VDC has no /IRQ output).

Another way without COLLISION is

(VIC-IIe)
IF PEEK(53273) AND 8 THEN POKE 53273,8: ?"TRIGGER"
(VDC)
IF PEEK(54784) AND 64 THEN ?"TRIGGER"

I'm not sure about VDC, maybe you need 255-PEEK(54784) AND 64... and I'm sure BDD would remind us not to PEEK the VDC!  Or easy way for VDC is ?PEN(4).

That's strange!  There is no PEN(5) for VIC trigger...

Oh yeah, for VDC, you probably want a background color different than standard black.

If none if these things work, maybe something is wrong?
I'm kupo for kupo nuts!

MIRKOSOFT

Hi!
I tried all possibilities you write and:
- light gun was detected when aim at screen, not when trigger was pressed in both VIC samples
-light gun works not with VDC, I tried more color-mixes
So, what with the trigger? Games working correctly, standard programming works not correctly...
The VDC is not so important, VIC is main target.
Oh, if it helps, I have model: Cheetah DEFENDER64.
Thank you also for explaining me your DIGI-program.
Miro
MIRKOSOFT of megabytes

Commodore 64 was great, Commodore 128 is bigger, better, faster and more!!!

http://www.mirkosoft.sk

MIRKOSOFT

I forgot - you wrote that it can be too short signal...
When I'm playing e.g. Operation Wolf, if I hold down the trigger, it makes like autofire... not single shot until I release trigger!
Can it be this?


Miro
MIRKOSOFT of megabytes

Commodore 64 was great, Commodore 128 is bigger, better, faster and more!!!

http://www.mirkosoft.sk

MIRKOSOFT

#5
Hi!

I tried to check $DC01 (56321) in assembler and:

-when I aim at screen it gives back value $EF (like fire), not $FF
It can be combined with INPUT/OUTPUT setting in $DC02 and $DC03, but don't know how
What do you think about?

Miro
MIRKOSOFT of megabytes

Commodore 64 was great, Commodore 128 is bigger, better, faster and more!!!

http://www.mirkosoft.sk

Hydrophilic

It sounds like device is working correctly: it gives value when pointed at the screen.  This is how light pens / guns are designed to work...

There is a light sensor inside and at exact moment it sees a bright spot (from raster drawing image on screen) it sends a quick signal to VIC-II by the /PEN line.  Of course the VIC-II knows the position it is drawing at that exact time, so it stores that position (that you read with PEN(0) and PEN(1)) and sets bit 3 in register that you can PEEK(53273).  If light pen interrupts have been enabled, VIC-II will also activate /IRQ at this time... this is how COLLISION works (it calls your subroutine when /IRQ from VIC-II occurs).

So it works when you point at screen, but when not pointed at screen, it will never see a "bright spot" from the raster of monitor and so it will never send signal on /PEN line.

Quote from: MIRKOSOFTI tried to check $DC01 (56321) in assembler and:

-when I aim at screen it gives back value $EF (like fire), not $FF

The /PEN line is also connected to PB4 of CIA#1 (see schematic of C128 in C128 Programmers Refernce Guide, page 726) which is the same line as joystick fire button.

Do I understand you correctly, that you get $EF when gun is pointed at screen and you press trigger?  Or do you get $EF when pointed at screen but not pressing trigger... that would be strange!

Quote from: MIRKOSOFT
When I'm playing e.g. Operation Wolf, if I hold down the trigger, it makes like autofire... not single shot until I release trigger!

This works only when gun is pointed at screen, correct?

Because the VIC-IIe draws screen 60 or 50 times per second (depending on NTSC or PAL video standard), then you can detect trigger many times per second, and thus shoot many bullets per second (if game will allow).  However, the time trigger (/PEN line) is activate will probably be very short time, I think for a few microseconds when light sensor sees "bright spot" from raster on monitor.  So if you use BASIC program like

DO:?JOY(1)AND128:LOOP

then you may always see 0 because /PEN line is too quick for BASIC.

Quote from: MIRKOSOFTI tried to check $DC01 (56321) in assembler and:

-when I aim at screen it gives back value $EF (like fire), not $FF
ML is much faster, however you may be lucky this works.  The instruction LDA $DC01 takes 4 microseconds to execute (slow CPU speed), but the raster spot should be much smaller than a character width, and character width is only 1 microsecond.  In other words, this may work for your device but not others.  The PEEK(53273) should work with every device because VIC-II is much faster than CPU.

Quote from: MIRKOSOFTlight gun was detected when aim at screen, not when trigger was pressed in both VIC samples

So my programs work only when pointed at screen... at least they work :)  Is this the same with your game... only shoot when gun is pointed at screen?

Quote from: MIRKOSOFTlight gun works not with VDC, I tried more color-mixes
The C128 P.R.G. does not say if LP of VDC is active high or low so I am not sure if code should be ?PEEK(54784)AND64 or should be ?255-PEEK(54784)AND64 ... did you try both?  Did you try ?PEN(4) also?  If PEN(4) does not work, even with different background color, then maybe the device will not work with VDC...

Quote from: MIRKOSOFTOh, if it helps, I have model: Cheetah DEFENDER64.
Thank you also for explaining me your DIGI-program.
It may help other people, but I do not have any light gun or light pen... this is all theory from my studies of CBM computers... which is why I was so happy my program worked at all!!  You are welcome for explanation of DIGI-program, I hope you make a cool game or something!
I'm kupo for kupo nuts!

MIRKOSOFT

Hi!
First I want to answer your questions in last reply, after I'll try anything and then report results.
1. $EF value I get without pressing triegger
2. In games only when I point at screen and autofire is only when I hold down the trigger.
3. I tried all possibilieties of VDC you wrote. Without using 255-... was always detected active and PEN(4) also always active.
Thank you very much
Miro
MIRKOSOFT of megabytes

Commodore 64 was great, Commodore 128 is bigger, better, faster and more!!!

http://www.mirkosoft.sk

MIRKOSOFT

Hi!
So, I tried possibilities you wrote...
Ever I can say that enough pointing at screen detects ligh gun activity.
But how to scan if trigger is pressed?
Don't understand, 'cause in games when I hold down trigger not, at the time is no fire...


Miro
MIRKOSOFT of megabytes

Commodore 64 was great, Commodore 128 is bigger, better, faster and more!!!

http://www.mirkosoft.sk

Hydrophilic

I am sorry, but I do not understand
Quote from: MIRKOSOFTEver I can say that enough pointing at screen detects ligh gun activity.
So you can detect with PEEK(53273) when pointing at screen when pressing trigger?  Can you detect with PEEK(53273) when pointing at screen when NOT pressing trigger?

I assume you can not detect with PEEK(53273) when not pointing at screen (regardless if you press trigger or not)

What about peek port 1 with ML.  Here is a ML program to try:

.1300 LDA #0
.1302 STA $FF00
.1305 SEI
.1306 LDA $DC01
.1309 STA $0400
.130C JMP $1306

Note this program will not stop!  You must press STOP+RESTORE or /RESET to regain control.

Anyway, it will set top-left character of VIC-II screen with value from joystick port 1.  Do you see any change of top-left character when pressing trigger when NOT pointed at screen?  If you see change of character, then there should be another line active other than /PEN (joystick fire)...

You should see change of character when gun is pointed at screen, but do you see character change every time gun is pointed at screen (not pressing trigger), or do you only see change when gun is pointed at screen and you are pressing trigger?

Quote from: MIRKOSOFTDon't understand, 'cause in games when I hold down trigger not, at the time is no fire...

Again, I am sorry but I do not understand...

When you hold trigger, is there fire in game when pointed at screen?

When you hold trigger, is there fire in game when not pointed at screen?

When not holding trigger, can you make game fire by simply pointing gun at screen?
I'm kupo for kupo nuts!

MIRKOSOFT

First, I'm sorry for my bad English.


I tried your routine and character was never changed.


I try to write again what you did not understand:
When I pointed to screen value is changed, pressing trigger do nothing.
When I pointed not to screen, value is not changed.
---
In games, when I press trigger and point to screen, e.g. Operation Wolf, I can fire on enemies and also aim the targets.
When I point to screen and don't press trigger, I can only aim the targets, not to kill them.
When I point not to screen, light gun activity is not detected.


Don't know to write it better... my English is not so good.


And, thank you for your patience, your help is great, only solution is found not yet, but I believe that if you'll help me, we'll to find it.


Cheers,


Miro

MIRKOSOFT of megabytes

Commodore 64 was great, Commodore 128 is bigger, better, faster and more!!!

http://www.mirkosoft.sk

MIRKOSOFT

#11
...longer time ago I wanted to detect that SHIFT LOCK was pressed (no L/R SHIFT), answer was written:

"Normally, you set one CIA port to output and the other CIA port to input for scanning the keys. This is the usual way, and it's the right way. Once you have detected that the left shift key is pressed, you need to do something that is "electrically wrong": Program both bits of the key matrix to OUTPUT and set the A side to 0, while you set the B side to 1. In essence, you're programming a short-circuit.

Now comes the electrical knowledge: Even when programmed to output, the CIA port-registers always reflect the true status of the physical pins. If programmed to 1, but pulled to GND on the outside, you read a 0. The behaviour of the "1"-bit is what lets you determine the status of the shift lock key: If the bit that you've programmed to "1" reads "0", then the shift lock key is pressed. If both bits read the value that you have written, then the normal left-shift key is pressed.
This procedure sounds much like the "killer-poke", because you're essentially programming a short-circuit, but it's not. The CIA's port bits are operated close to, but within limits using this technique."

And code what I done was:

test: lda #$fd //test l.shift(lock)
sta $dc00
lda $c001
and #$80
bne test
lda #$02 //set data registers to OUTPUT
sta $dc02
lda #$80
sta $dc03
lda #$00 //set A to 0
sta $dc00
lda #$ff //set B to 1
sta $dc01
lda $dc01 //test if bit is 1
and #$80
bne test //if yes test l.shift(lock) back
lda #$00    // set B to INPUT
sta $dc03
lda #$ff    // set A to OUTPUT
sta $dc02
rts


Can this have st. to do with scan of trigger?
Thank you for help and patience.

Miro
MIRKOSOFT of megabytes

Commodore 64 was great, Commodore 128 is bigger, better, faster and more!!!

http://www.mirkosoft.sk

Hydrophilic

Quote from: MIRKOSOFTI tried your routine and character was never changed.

I don't know what is happening.  You have gun plugged into port#1 (closest to front of C128) ?  Not port#2 (beside power switch) ?

Because if you use port#1, and there is no change when you run the .1300 program, then the trigger must not be connected to the joystick lines... that would be strange, but I guess it is possible.

The only other input lines on port#1 are the POTX and POTY lines.  I always thought they should be used to add extra buttons to joystick, but I never heard of any device that used POTX/Y as the only button... Anyway, you can check if POTX or POTY lines are used with simple program:

DO:?POT(1)AND255,POT(2)AND255:LOOP

This will normally print 255 255 all the time.  See if you get different values with your gun...

Quote from: MIRKOSOFTIn games, when I press trigger and point to screen, e.g. Operation Wolf, I can fire on enemies and also aim the targets.
When I point to screen and don't press trigger, I can only aim the targets, not to kill them.
When I point not to screen, light gun activity is not detected.


Don't know to write it better... my English is not so good.

Your English is not very bad; I understand now.  So the gun must be sending /PEN signals all the time gun is pointed to screen, then sending a different signal when you press trigger.  A normal device, like joystick, paddle, or mouse, will simply ground one of the CIA lines (and you would see character change when running .1300 program).  If you run .1300 program, do you see character change when using joystick or mouse?  You are running .1300 program on VIC screen, yes?

Quote from: MIRKOSOFTlonger time ago I wanted to detect that SHIFT LOCK was pressed (no L/R SHIFT), answer was written...

Program both bits of the key matrix to OUTPUT...

Can this have st. to do with scan of trigger?

Setting port to Output Mode then reading port makes it more difficult to detect signal.  With this method it is not possible to detect Left Shift, but Shift Lock is so powerful, it can still be detected (so you can tell the keys apart).  But for the light gun, we don't want to make detecting signals more difficult, we want to make them easier!

One other possibility is maybe gun will not send trigger signal unless a signal is sent by C128 to gun... this is not normal for CBM controllers like joystick, but some devices do this... your gun does not seem normal to my way of thinking, so maybe you are right: maybe you must set a line to output and send "0" then device will send trigger signal.  In that case, there are 4 lines to test (bits 0 to 3) assuming it does not use /PEN line (on bit 4).  $dc03 controls direction of port#1, and is usually $00 for all inputs.  I hope you can see the idea here, there are many possibilities you could try and it would take a while to explain or write a program now...
I'm kupo for kupo nuts!

MIRKOSOFT

DO:?POT(1)AND255,POT(2)AND255:LOOP


- YOU'RE GENIUS!!!!!!!!!!!!!!!!!!!!!


WHEN I RUN THIS PROGRAM AND PRESS TRIGGER SECOND VALUE CHANGES FROM 255 TO 2!!!!!


I'M HAPPY. THAT'S WHAT I WANTED AND I'M GRATEFUL, VERY VERY GRATEFUL THANK YOU AGAIN AGAIN.


THANK YOU FOR YOUR PATIENCE!!!!
Cheers,


Miro

MIRKOSOFT of megabytes

Commodore 64 was great, Commodore 128 is bigger, better, faster and more!!!

http://www.mirkosoft.sk