Jump to content
IGNORED

A programming tip for everybody


jbs30000

Recommended Posts

I'm making a game where I'm trying to use as little code as possible, and I thought of a great way to cut down on code for using the joystick.

Normally, when people check joysticks they use "if-then" statements. Well, from the information I got from the Stella Programmer's Guide I thought of a way to do it with less code. Here's a sample program that shows how to do it.

data Horizontal
0,0,0,0,0,1,1,1,0,255,255,255,0,0,0,0
end
data Vertical
0,0,0,0,0,1,255,0,0,1,255,0,0,1,255,0
end

player0:
%11111111
%11111111
%11111111
%11111111
end

player0x=30: player0y=30

el
temp1=SWCHA/16
player0x=player0x+Horizontal[temp1] : player0y=player0y+Vertical[temp1]
COLUP0=206: drawscreen: goto el

I'm guessing that everybody can figure out what I did, but just in case you have any questions, here's an explantion:

 

As noted in the above link, SWCHA holds direction(s), if any, that the joystick is being pushed.

Since player 0 is bits 4-7 I divide SWCHA by 16.

The values for which way the joystick is being pushed are:

0-4 N/A, 5-Down/Right, 6-Up/Right, 7-Right, 8-N/A, 9-Down/Left, 10-Up/Left, 11-Left, 12-N/A, 13-Down, 14-Up, 15-Joystick is not being pushed in any direction.

And finally, 255 is the same as -1, so in the horizontal data variable 255 means move left, and in the vertical data variable 255 means move up.

 

I hope that some of you find this useful.

Link to comment
Share on other sites

Actually, that way is worse. You actually save 35 bytes and at least 16 cycles by using the old way:

  if joy0left then player0x=player0x-1
 if joy0right then player0x=player0x+1
 if joy0up then player0y=player0y-1
 if joy0down then player0y=player0y+1

Edited by batari
Link to comment
Share on other sites

So you're saying that:

.L02 ;  if joy0up then player0y = player0y - 1

lda #$10
bit SWCHA
BNE .skipL02
.condpart0
DEC player0y
.skipL02
.L03 ;  if joy0down then player0y = player0y + 1

lda #$20
bit SWCHA
BNE .skipL03
.condpart1
INC player0y
.skipL03
.L04 ;  if joy0left then player0x = player0x - 1

bit SWCHA
BVS .skipL04
.condpart2
DEC player0x
.skipL04
.L05 ;  if joy0right then player0x = player0x + 1

bit SWCHA
BMI .skipL05
.condpart3
INC player0x
.skipL05

is shorter and faster than

.L04 ;  temp1 = SWCHA / 16

LDA SWCHA
lsr
lsr
lsr
lsr
STA temp1
.L05 ;  player0x = player0x + Horizontal[temp1]

LDA player0x
LDX temp1
CLC
ADC Horizontal,x
STA player0x
.L06 ;  player0y = player0y + Vertical[temp1]

LDA player0y
LDX temp1
CLC
ADC Vertical,x
STA player0y

Actually, counting the opcodes, there's 16 in my method, and 14 in the regular method. Not that much different in size, but then I know that doesn't mean much when it comes to speed.

Oh well, thought I found a good shortcut and wanted to share. My mistake.

Link to comment
Share on other sites

What takes up all the space is the data tables (38 bytes total, i.e. 32 bytes of data and 6 of overhead), not the code itself.

 

As for cycles, I just counted again and have a constant 44 in your code, and the old way (assuming up+down and left+right are not possible) takes 28-36.

 

Don't worry about it, as I've done the same sort of thing to try to save space and cycles, only to find that a few if-thens actually aren't all that bad.

Edited by batari
Link to comment
Share on other sites

Actually, while I don't know about speed, looking at the list files and looking at the actual machine code, it looks like my way produces less code:

My way:

 1551  f4c7				   .L04 		;  temp1 = SWCHA / 16
  1552  f4c7
  1553  f4c7		       ad 80 02 	      LDA	SWCHA
  1554  f4ca		       4a		      lsr
  1555  f4cb		       4a		      lsr
  1556  f4cc		       4a		      lsr
  1557  f4cd		       4a		      lsr
  1558  f4ce		       85 9c		      STA	temp1
  1559  f4d0				   .L05 		;  player0x = player0x + Horizontal[temp1]
  1560  f4d0
  1561  f4d0		       a5 80		      LDA	player0x
  1562  f4d2		       a6 9c		      LDX	temp1
  1563  f4d4		       18		      CLC
  1564  f4d5		       7d 92 f4 	      ADC	Horizontal,x
  1565  f4d8		       85 80		      STA	player0x
  1566  f4da				   .L06 		;  player0y = player0y + Vertical[temp1]
  1567  f4da
  1568  f4da		       a5 85		      LDA	player0y
  1569  f4dc		       a6 9c		      LDX	temp1
  1570  f4de		       18		      CLC
  1571  f4df		       7d a5 f4 	      ADC	Vertical,x
  1572  f4e2		       85 85		      STA	player0y

I count 29 bytes of code (commands and data)

Old way:

  1534  f4a1				   .L02 		;  if joy0up then player0y = player0y - 1
  1535  f4a1
  1536  f4a1		       a9 10		      lda	#$10
  1537  f4a3		       2c 80 02 	      bit	SWCHA
  1538  f4a6		       d0 02		      BNE	.skipL02
  1539  f4a8				   .condpart0
  1540  f4a8		       c6 85		      DEC	player0y
  1541  f4aa				   .skipL02
  1542  f4aa				   .L03 		;  if joy0down then player0y = player0y + 1
  1543  f4aa
  1544  f4aa		       a9 20		      lda	#$20
  1545  f4ac		       2c 80 02 	      bit	SWCHA
  1546  f4af		       d0 02		      BNE	.skipL03
  1547  f4b1				   .condpart1
  1548  f4b1		       e6 85		      INC	player0y
  1549  f4b3				   .skipL03
  1550  f4b3				   .L04 		;  if joy0left then player0x = player0x - 1
  1551  f4b3
  1552  f4b3		       2c 80 02 	      bit	SWCHA
  1553  f4b6		       70 02		      BVS	.skipL04
  1554  f4b8				   .condpart2
  1555  f4b8		       c6 80		      DEC	player0x
  1556  f4ba				   .skipL04
  1557  f4ba				   .L05 		;  if joy0right then player0x = player0x + 1
  1558  f4ba
  1559  f4ba		       2c 80 02 	      bit	SWCHA
  1560  f4bd		       30 02		      BMI	.skipL05
  1561  f4bf				   .condpart3
  1562  f4bf		       e6 80		      INC	player0x
  1563  f4c1				   .skipL05

I count 32 bytes.

Like I said, I don't know about speed, but it seems like like my version produces slightly less machine code. OK, it's only 3 bytes, but I'm curious where you got your 35 byte figure from.

Link to comment
Share on other sites

  • 1 year later...

;)

 

 

 

;left player only...
LDA SWCHA	;4
ASL		  ;2
BCS Not_Right;2 @8
INC player0x ;5
Not_Right:
ASL		  ;2
BCS Not_Left ;2 @13
DEC player0x ;5
Not_Left:
ASL		  ;2
BCS Not_Down ;2 @18
INC player0y ;5
Not_Down:
ASL		  ;2
BCS Not_Up   ;2 @23
DEC player0y ;5
Not_Up:
;23 bytes finished @24 (min)
;add 4 cycles for each direction used
;soo...between 24 and 32.

 

The INC's and DEC's are cut down to 2 cycles each if you preload the sprite's coordinates to the X and Y registers...if you are doing further adjustments to them (such as checking for screen boundries).

 

 

 

 

;both players version...
LDA SWCHA	;4
LDX #$01	 ;2 player #
Stick_Loop:
ASL		  ;2
BCS Not_Right;2 @10/35
INC player0x,x;6
Not_Right:
ASL		  ;2
BCS Not_Left ;2 @15/40
DEC player0x,x;6
Not_Left:
ASL		  ;2
BCS Not_Down ;2 @20/45
INC player0y,x;6
Not_Down:
ASL		  ;2
BCS Not_Up   ;2 @25/50
DEC player0y,x;6
Not_Up:
DEX		  ;2
BPL Stick_Loop;2 @31/55
;28 bytes finished @55 cycles (min)
;add 5 cycles for each direction used
;soo...between 55 and 75.

Link to comment
Share on other sites

Did you see Robert M's version of this:

 

http://www.randomterrain.com/atari-2600-me...ands.html#swcha

Wow, a thread I posted two years ago.

 

Anyway, no, I haven't seen that code, so thank you for the link.

I did a similar routine as well. I'd say "great minds think alike," but it's debatable whether this is a "great idea." I mean, *I* thought it was, but not everyone agreed. ;) Oh, and someone pointed out that the data tables could be squeezed down to save ROM.

 

Here's one post I did that uses this technique:

 

http://www.atariage.com/forums/index.php?s...howtopic=120461

 

And here's another thread where it was talked about:

 

http://www.atariage.com/forums/index.php?s...howtopic=121930

 

Michael

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...