Simplify long, repetitive code.

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

User avatar
OmegaMax
Posts: 83
Joined: Wed Sep 21, 2016 8:55 am
Location: Calgary.Alberta,Canada

Re: Simplify long, repetitive code.

Post by OmegaMax » Thu Jul 13, 2017 1:52 pm

Oziphantom wrote:Yeah but Garth, they are having trouble making an array of Words, lets keep the -1, stack push tricks to latter yeah ;)
Agreed and that's why I used your code example to help him.

Garth
Posts: 148
Joined: Wed Nov 30, 2016 4:45 pm
Location: Southern California
Contact:

Re: Simplify long, repetitive code.

Post by Garth » Thu Jul 13, 2017 3:08 pm

What I presented is really basic stuff, not rocket science. Make the table say,

Code: Select all

LvlFuncPtrTableLo
   .byte  LOW(LEV01-1), LOW(LEV02-1), LOW(LEV03-1, <etc.>
LvlFuncPtrTableHi
   .byte  HIGH(LEV01-1), HIGH(LEV02-1), HIGH(LEV03-1), <etc.>
Using the RTS for a jump simplifies things by eliminating the need for a variable and removing it from the constant battle for ZP space (since it was suggested to put it there). It also makes the program more efficient.
http://WilsonMinesCo.com/ lots of 6502 resources

User avatar
rainwarrior
Posts: 7639
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Simplify long, repetitive code.

Post by rainwarrior » Thu Jul 13, 2017 3:18 pm

Garth wrote:It also makes the program more efficient.
Depends what you mean by "efficient". Using RTS takes 1 more cycle vs an indirect jump, but it saves some bytes of code at least?

Code: Select all

STA zp +3
STA zp +3
JMP (addr) +5

PHA +3
PHA +3
RTS +6
The JMP pointer does not need to be on the zeropage, either (JMP's indirection is always from an absolute address, ZP is irrelevant to that instruction), though that would cost another 2 cycles from the STAs.
Last edited by rainwarrior on Thu Jul 13, 2017 3:25 pm, edited 1 time in total.

User avatar
OmegaMax
Posts: 83
Joined: Wed Sep 21, 2016 8:55 am
Location: Calgary.Alberta,Canada

Re: Simplify long, repetitive code.

Post by OmegaMax » Thu Jul 13, 2017 3:25 pm

This is becoming "use my way" instead,you left an example Garth so if sdm chooses to use it that's fine also.

Garth
Posts: 148
Joined: Wed Nov 30, 2016 4:45 pm
Location: Southern California
Contact:

Re: Simplify long, repetitive code.

Post by Garth » Thu Jul 13, 2017 3:30 pm

Depends what you mean by "efficient".
It saves several bytes; and if the indirect address is not in ZP, it's slightly faster, too.
This is becoming "use my way" instead of helping another programmer.
I believe it's always helpful to present a more elegant solution.
http://WilsonMinesCo.com/ lots of 6502 resources

User avatar
OmegaMax
Posts: 83
Joined: Wed Sep 21, 2016 8:55 am
Location: Calgary.Alberta,Canada

Re: Simplify long, repetitive code.

Post by OmegaMax » Thu Jul 13, 2017 3:39 pm

I believe when your new the code Oziphantom showed is easier to grasp,that's why I choose his code to help sdm.Now that he understands and has it working he can explore other ways if he chooses,it's not really necessary though but it's up to him.

sdm
Posts: 288
Joined: Tue Apr 11, 2006 4:08 am
Location: Poland

Re: Simplify long, repetitive code.

Post by sdm » Mon Jul 17, 2017 5:13 am

I would still like to ask for help with a slightly different, large code. Rule similar, but slightly different. Each bit in the "GOLD_xx_xx_UnLock" byte slot is responsible for the collected (1) or not (0) gold. I tried to make the simplified code as above in posts, but this time instead of jump (JMP) to the label are operations on different bits. And now I have a problem, because I do not know if any one can do it in a similar way: /

Collecting the "GOLD" item can be random, there is no pre-assigned order of collection order (one big map consisting of 64-nametables)

GOLD_Checker - is the variable that depends on which "map" we enter, the number of this map is loaded (Specify: the value with the map number is uploaded exactly in the "collision" of the gold with the hero, and after that time is again zero)

Code: Select all

GOLD_Checker		.ds 1
GOLD_01_08_UnLock	.ds 1
GOLD_09_16_UnLock	.ds 1
GOLD_17_24_UnLock	.ds 1
GOLD_25_32_UnLock	.ds 1
GOLD_33_40_UnLock	.ds 1
GOLD_41_48_UnLock	.ds 1
GOLD_49_56_UnLock	.ds 1
GOLD_57_64_UnLock	.ds 1



GOLD_Check:

	LDA GOLD_Checker
	CMP #1
	BNE NoGC_01

	LDA GOLD_01_08_UnLock
	ORA #%00000001
	STA GOLD_01_08_UnLock

NoGC_01:
	LDA GOLD_Checker
	CMP #2
	BNE NoGC_02

	LDA GOLD_01_08_UnLock
	ORA #%00000010
	STA GOLD_01_08_UnLock

NoGC_02:
	LDA GOLD_Checker
	CMP #3
	BNE NoGC_03

	LDA GOLD_01_08_UnLock
	ORA #%00000100
	STA GOLD_01_08_UnLock

NoGC_03:
	LDA GOLD_Checker
	CMP #4
	BNE NoGC_04

	LDA GOLD_01_08_UnLock
	ORA #%00001000
	STA GOLD_01_08_UnLock

NoGC_04:
	LDA GOLD_Checker
	CMP #5
	BNE NoGC_05

	LDA GOLD_01_08_UnLock
	ORA #%00010000
	STA GOLD_01_08_UnLock

NoGC_05:
	LDA GOLD_Checker
	CMP #6
	BNE NoGC_06

	LDA GOLD_01_08_UnLock
	ORA #%00100000
	STA GOLD_01_08_UnLock

NoGC_06:
	LDA GOLD_Checker
	CMP #7
	BNE NoGC_07

	LDA GOLD_01_08_UnLock
	ORA #%01000000
	STA GOLD_01_08_UnLock

NoGC_07:
	LDA GOLD_Checker
	CMP #8
	BNE NoGC_08

	LDA GOLD_01_08_UnLock
	ORA #%10000000
	STA GOLD_01_08_UnLock

NoGC_08:
	LDA GOLD_Checker
	CMP #9
	BNE NoGC_09

	LDA GOLD_09_16_UnLock
	ORA #%00000001
	STA GOLD_09_16_UnLock

NoGC_09:
	LDA GOLD_Checker
	CMP #10
	BNE NoGC_10

	LDA GOLD_09_16_UnLock
	ORA #%00000010
	STA GOLD_09_16_UnLock

NoGC_10:
	LDA GOLD_Checker
	CMP #11
	BNE NoGC_11

	LDA GOLD_09_16_UnLock
	ORA #%00000100
	STA GOLD_09_16_UnLock

NoGC_11:
	LDA GOLD_Checker
	CMP #12
	BNE NoGC_12

	LDA GOLD_09_16_UnLock
	ORA #%00001000
	STA GOLD_09_16_UnLock

NoGC_12:
	LDA GOLD_Checker
	CMP #13
	BNE NoGC_13

	LDA GOLD_09_16_UnLock
	ORA #%00010000
	STA GOLD_09_16_UnLock

NoGC_13:
	LDA GOLD_Checker
	CMP #14
	BNE NoGC_14

	LDA GOLD_09_16_UnLock
	ORA #%00100000
	STA GOLD_09_16_UnLock

NoGC_14:
	LDA GOLD_Checker
	CMP #15
	BNE NoGC_15

	LDA GOLD_09_16_UnLock
	ORA #%01000000
	STA GOLD_09_16_UnLock

NoGC_15:
	LDA GOLD_Checker
	CMP #16
	BNE NoGC_16

	LDA GOLD_09_16_UnLock
	ORA #%10000000
	STA GOLD_09_16_UnLock

NoGC_16:
	LDA GOLD_Checker
	CMP #17
	BNE NoGC_17

	LDA GOLD_17_24_UnLock
	ORA #%00000001
	STA GOLD_17_24_UnLock

NoGC_17:
	LDA GOLD_Checker
	CMP #18
	BNE NoGC_18

	LDA GOLD_17_24_UnLock
	ORA #%00000010
	STA GOLD_17_24_UnLock

NoGC_18:
	LDA GOLD_Checker
	CMP #19
	BNE NoGC_19

	LDA GOLD_17_24_UnLock
	ORA #%00000100
	STA GOLD_17_24_UnLock

NoGC_19:
	LDA GOLD_Checker
	CMP #20
	BNE NoGC_20

	LDA GOLD_17_24_UnLock
	ORA #%00001000
	STA GOLD_17_24_UnLock

NoGC_20:
	LDA GOLD_Checker
	CMP #21
	BNE NoGC_21

	LDA GOLD_17_24_UnLock
	ORA #%00010000
	STA GOLD_17_24_UnLock

NoGC_21:
	LDA GOLD_Checker
	CMP #22
	BNE NoGC_22

	LDA GOLD_17_24_UnLock
	ORA #%00100000
	STA GOLD_17_24_UnLock

NoGC_22:
	LDA GOLD_Checker
	CMP #23
	BNE NoGC_23

	LDA GOLD_17_24_UnLock
	ORA #%01000000
	STA GOLD_17_24_UnLock

NoGC_23:
	LDA GOLD_Checker
	CMP #24
	BNE NoGC_24

	LDA GOLD_17_24_UnLock
	ORA #%10000000
	STA GOLD_17_24_UnLock

NoGC_24:
	LDA GOLD_Checker
	CMP #25
	BNE NoGC_25

	LDA GOLD_25_32_UnLock
	ORA #%00000001
	STA GOLD_25_32_UnLock

NoGC_25:
	LDA GOLD_Checker
	CMP #26
	BNE NoGC_26

	LDA GOLD_25_32_UnLock
	ORA #%00000010
	STA GOLD_25_32_UnLock

NoGC_26:
	LDA GOLD_Checker
	CMP #27
	BNE NoGC_27

	LDA GOLD_25_32_UnLock
	ORA #%00000100
	STA GOLD_25_32_UnLock

NoGC_27:
	LDA GOLD_Checker
	CMP #28
	BNE NoGC_28

	LDA GOLD_25_32_UnLock
	ORA #%00001000
	STA GOLD_25_32_UnLock

NoGC_28:
	LDA GOLD_Checker
	CMP #29
	BNE NoGC_29

	LDA GOLD_25_32_UnLock
	ORA #%00010000
	STA GOLD_25_32_UnLock

NoGC_29:
	LDA GOLD_Checker
	CMP #30
	BNE NoGC_30

	LDA GOLD_25_32_UnLock
	ORA #%00100000
	STA GOLD_25_32_UnLock

NoGC_30:
	LDA GOLD_Checker
	CMP #31
	BNE NoGC_31

	LDA GOLD_25_32_UnLock
	ORA #%01000000
	STA GOLD_25_32_UnLock

NoGC_31:
	LDA GOLD_Checker
	CMP #32
	BNE NoGC_32

	LDA GOLD_25_32_UnLock
	ORA #%10000000
	STA GOLD_25_32_UnLock

NoGC_32:
	LDA GOLD_Checker
	CMP #33
	BNE NoGC_33

	LDA GOLD_33_40_UnLock
	ORA #%00000001
	STA GOLD_33_40_UnLock

NoGC_33:
	LDA GOLD_Checker
	CMP #34
	BNE NoGC_34

	LDA GOLD_33_40_UnLock
	ORA #%00000010
	STA GOLD_33_40_UnLock

NoGC_34:
	LDA GOLD_Checker
	CMP #35
	BNE NoGC_35

	LDA GOLD_33_40_UnLock
	ORA #%00000100
	STA GOLD_33_40_UnLock

NoGC_35:
	LDA GOLD_Checker
	CMP #36
	BNE NoGC_36

	LDA GOLD_33_40_UnLock
	ORA #%00001000
	STA GOLD_33_40_UnLock

NoGC_36:
	LDA GOLD_Checker
	CMP #37
	BNE NoGC_37

	LDA GOLD_33_40_UnLock
	ORA #%00010000
	STA GOLD_33_40_UnLock

NoGC_37:
	LDA GOLD_Checker
	CMP #38
	BNE NoGC_38

	LDA GOLD_33_40_UnLock
	ORA #%00100000
	STA GOLD_33_40_UnLock

NoGC_38:
	LDA GOLD_Checker
	CMP #39
	BNE NoGC_39

	LDA GOLD_33_40_UnLock
	ORA #%01000000
	STA GOLD_33_40_UnLock

NoGC_39:
	LDA GOLD_Checker
	CMP #40
	BNE NoGC_40

	LDA GOLD_33_40_UnLock
	ORA #%10000000
	STA GOLD_33_40_UnLock

NoGC_40:
	LDA GOLD_Checker
	CMP #41
	BNE NoGC_41

	LDA GOLD_41_48_UnLock
	ORA #%00000001
	STA GOLD_41_48_UnLock

NoGC_41:
	LDA GOLD_Checker
	CMP #42
	BNE NoGC_42

	LDA GOLD_41_48_UnLock
	ORA #%00000010
	STA GOLD_41_48_UnLock

NoGC_42:
	LDA GOLD_Checker
	CMP #43
	BNE NoGC_43

	LDA GOLD_41_48_UnLock
	ORA #%00000100
	STA GOLD_41_48_UnLock

NoGC_43:
	LDA GOLD_Checker
	CMP #44
	BNE NoGC_44

	LDA GOLD_41_48_UnLock
	ORA #%00001000
	STA GOLD_41_48_UnLock

NoGC_44:
	LDA GOLD_Checker
	CMP #45
	BNE NoGC_45

	LDA GOLD_41_48_UnLock
	ORA #%00010000
	STA GOLD_41_48_UnLock

NoGC_45:
	LDA GOLD_Checker
	CMP #46
	BNE NoGC_46

	LDA GOLD_41_48_UnLock
	ORA #%00100000
	STA GOLD_41_48_UnLock

NoGC_46:
	LDA GOLD_Checker
	CMP #47
	BNE NoGC_47

	LDA GOLD_41_48_UnLock
	ORA #%01000000
	STA GOLD_41_48_UnLock

NoGC_47:
	LDA GOLD_Checker
	CMP #48
	BNE NoGC_48

	LDA GOLD_41_48_UnLock
	ORA #%10000000
	STA GOLD_41_48_UnLock

NoGC_48:
	LDA GOLD_Checker
	CMP #49
	BNE NoGC_49

	LDA GOLD_49_56_UnLock
	ORA #%00000001
	STA GOLD_49_56_UnLock

NoGC_49:
	LDA GOLD_Checker
	CMP #50
	BNE NoGC_50

	LDA GOLD_49_56_UnLock
	ORA #%00000010
	STA GOLD_49_56_UnLock

NoGC_50:
	LDA GOLD_Checker
	CMP #51
	BNE NoGC_51

	LDA GOLD_49_56_UnLock
	ORA #%00000100
	STA GOLD_49_56_UnLock

NoGC_51:
	LDA GOLD_Checker
	CMP #52
	BNE NoGC_52

	LDA GOLD_49_56_UnLock
	ORA #%00001000
	STA GOLD_49_56_UnLock

NoGC_52:
	LDA GOLD_Checker
	CMP #53
	BNE NoGC_53

	LDA GOLD_49_56_UnLock
	ORA #%00010000
	STA GOLD_49_56_UnLock

NoGC_53:
	LDA GOLD_Checker
	CMP #54
	BNE NoGC_54

	LDA GOLD_49_56_UnLock
	ORA #%00100000
	STA GOLD_49_56_UnLock

NoGC_54:
	LDA GOLD_Checker
	CMP #55
	BNE NoGC_55

	LDA GOLD_49_56_UnLock
	ORA #%01000000
	STA GOLD_49_56_UnLock

NoGC_55:
	LDA GOLD_Checker
	CMP #56
	BNE NoGC_56

	LDA GOLD_49_56_UnLock
	ORA #%10000000
	STA GOLD_49_56_UnLock

NoGC_56:
	LDA GOLD_Checker
	CMP #57
	BNE NoGC_57

	LDA GOLD_57_64_UnLock
	ORA #%00000001
	STA GOLD_57_64_UnLock

NoGC_57:
	LDA GOLD_Checker
	CMP #58
	BNE NoGC_58

	LDA GOLD_57_64_UnLock
	ORA #%00000010
	STA GOLD_57_64_UnLock

NoGC_58:
	LDA GOLD_Checker
	CMP #59
	BNE NoGC_59

	LDA GOLD_57_64_UnLock
	ORA #%00000100
	STA GOLD_57_64_UnLock

NoGC_59:
	LDA GOLD_Checker
	CMP #60
	BNE NoGC_60

	LDA GOLD_57_64_UnLock
	ORA #%00001000
	STA GOLD_57_64_UnLock

NoGC_60:
	LDA GOLD_Checker
	CMP #61
	BNE NoGC_61

	LDA GOLD_57_64_UnLock
	ORA #%00010000
	STA GOLD_57_64_UnLock

NoGC_61:
	LDA GOLD_Checker
	CMP #62
	BNE NoGC_62

	LDA GOLD_57_64_UnLock
	ORA #%00100000
	STA GOLD_57_64_UnLock

NoGC_62:
	LDA GOLD_Checker
	CMP #63
	BNE NoGC_63

	LDA GOLD_57_64_UnLock
	ORA #%01000000
	STA GOLD_57_64_UnLock

NoGC_63:
	LDA GOLD_Checker
	CMP #64
	BNE NoGC_64

	LDA GOLD_57_64_UnLock
	ORA #%10000000
	STA GOLD_57_64_UnLock

NoGC_64:
	RTS



User avatar
thefox
Posts: 3141
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Re: Simplify long, repetitive code.

Post by thefox » Mon Jul 17, 2017 6:52 am

Something like this:

- A = GOLD_Checker-1. This is your 0-based level index 0..63.
- A/8 (range 0..7) tells you which byte to modify. You can divide by 8 by shifting right (LSR) 3 times.
- A & %111 (range 0..7) tells you which bit to modify (& = bitwise AND). You can use a lookup table with 8 elements (%1, %10, %100, ..., %10000000) to retrieve a mask for ORA.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi

User avatar
tokumaru
Posts: 11440
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Simplify long, repetitive code.

Post by tokumaru » Mon Jul 17, 2017 9:08 am

Here's one way to do it:

Code: Select all

BitMasks:

	.byte %00000001
	.byte %00000010
	.byte %00000100
	.byte %00001000
	.byte %00010000
	.byte %00100000
	.byte %01000000
	.byte %10000000

;************************

	sec
	lda GOLD_Checker
	sbc #$01
	tay
	and #%00000111
	tax
	tya
	lsr
	lsr
	lsr
	tay
	lda GOLD_01_08_UnLock, y
	ora BitMasks, x
	sta GOLD_01_08_UnLock, y
EDIT: Whenever you find yourself doing lots of comparisons against consecutive numbers, you should probably be using indexing instead. And when you're indexing stuff, the bits in the index usually have meanings that you can exploit, so when you realize what the bit pattern of the index/address is, you'll be able to find/manipulate the target data after a few bit manipulations rather than tons of comparisons for specific cases. For example, the way that the layout of a name table address is %0010NNYY YYXXXXX (NN = name table, XXXXX = horizontal tile coordinate, YYYYY = vertical tile coordinate) or an attribute table address is %0010NN11 11YYYXXX (NN = name table, XXX = horizontal 32x32 block coordinate, YYY = vertical 32x32 block coordinate) while the highest unused X and Y bits can be combined to index bit masks for manipulating the 16x16 blocks inside the 32x32 blocks (the lower X and Y bits are discarded for good, since you can't set palettes for individual tiles).

Post Reply