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

sdm2
Posts: 33
Joined: Wed Feb 05, 2020 12:59 pm
Location: Poland

Re: Simplify long, repetitive code.

Post by sdm2 » Sat Sep 05, 2020 4:00 am

He has one more example, I don't know if I did it correctly:

New:

Code: Select all


	LDX LEV_Memory				;for example there is level no. 003 (#$03)
	BEQ Done
	CPX #129
	BCS Done
	LDA ITEMLEV_Temp_PPUpdateLO		;loads the value of the variable temp from the level no. 003
	STA ITEMGOLDLEV001_PPUpdateLO-1,X	;
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV001_PPUpdateHI,X          ;does not subtract because Hi is always one higher than LO (?)
Done:
	RTS

of course in RAM they are in this order:

ITEMGOLDLEV001_PPUpdateLO .ds 1
ITEMGOLDLEV001_PPUpdateHI .ds 1
ITEMGOLDLEV002_PPUpdateLO .ds 1
ITEMGOLDLEV002_PPUpdateHI .ds 1
ITEMGOLDLEV003_PPUpdateLO .ds 1
ITEMGOLDLEV003_PPUpdateHI .ds 1
(...)
ITEMGOLDLEV128_PPUpdateLO .ds 1
ITEMGOLDLEV128_PPUpdateHI .ds 1

edit: hmm i analyzed it, it won't work properly. LO must skip one additional value while writing, and so does HI.


Old:

Code: Select all

ITEM_GOLD_CLR_LEV_CHECK:

	LDA LEV_Memory
	CMP #1
	BNE NoITGCLC_001

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV001_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV001_PPUpdateHI

NoITGCLC_001:

	LDA LEV_Memory
	CMP #2
	BNE NoITGCLC_002

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV002_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV002_PPUpdateHI

NoITGCLC_002:

	LDA LEV_Memory
	CMP #3
	BNE NoITGCLC_003

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV003_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV003_PPUpdateHI

NoITGCLC_003:

	LDA LEV_Memory
	CMP #4
	BNE NoITGCLC_004

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV004_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV004_PPUpdateHI

NoITGCLC_004:

	LDA LEV_Memory
	CMP #5
	BNE NoITGCLC_005

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV005_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV005_PPUpdateHI

NoITGCLC_005:

	LDA LEV_Memory
	CMP #6
	BNE NoITGCLC_006

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV006_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV006_PPUpdateHI

NoITGCLC_006:

	LDA LEV_Memory
	CMP #7
	BNE NoITGCLC_007

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV007_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV007_PPUpdateHI

NoITGCLC_007:

	LDA LEV_Memory
	CMP #8
	BNE NoITGCLC_008

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV008_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV008_PPUpdateHI

NoITGCLC_008:

	LDA LEV_Memory
	CMP #9
	BNE NoITGCLC_009

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV009_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV009_PPUpdateHI

NoITGCLC_009:

	LDA LEV_Memory
	CMP #10
	BNE NoITGCLC_010

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV010_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV010_PPUpdateHI

NoITGCLC_010:

	LDA LEV_Memory
	CMP #11
	BNE NoITGCLC_011

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV011_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV011_PPUpdateHI

NoITGCLC_011:

	LDA LEV_Memory
	CMP #12
	BNE NoITGCLC_012

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV012_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV012_PPUpdateHI

NoITGCLC_012:

	LDA LEV_Memory
	CMP #13
	BNE NoITGCLC_013

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV013_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV013_PPUpdateHI

NoITGCLC_013:

	LDA LEV_Memory
	CMP #14
	BNE NoITGCLC_014

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV014_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV014_PPUpdateHI

NoITGCLC_014:

	LDA LEV_Memory
	CMP #15
	BNE NoITGCLC_015

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV015_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV015_PPUpdateHI

NoITGCLC_015:

	LDA LEV_Memory
	CMP #16
	BNE NoITGCLC_016

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV016_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV016_PPUpdateHI

NoITGCLC_016:

	LDA LEV_Memory
	CMP #17
	BNE NoITGCLC_017

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV017_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV017_PPUpdateHI

NoITGCLC_017:

	LDA LEV_Memory
	CMP #18
	BNE NoITGCLC_018

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV018_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV018_PPUpdateHI

NoITGCLC_018:

	LDA LEV_Memory
	CMP #19
	BNE NoITGCLC_019

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV019_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV019_PPUpdateHI

NoITGCLC_019:

	LDA LEV_Memory
	CMP #20
	BNE NoITGCLC_020

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV020_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV020_PPUpdateHI

NoITGCLC_020:

	LDA LEV_Memory
	CMP #21
	BNE NoITGCLC_021

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV021_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV021_PPUpdateHI

NoITGCLC_021:

	LDA LEV_Memory
	CMP #22
	BNE NoITGCLC_022

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV022_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV022_PPUpdateHI

NoITGCLC_022:

	LDA LEV_Memory
	CMP #23
	BNE NoITGCLC_023

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV023_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV023_PPUpdateHI

NoITGCLC_023:

	LDA LEV_Memory
	CMP #24
	BNE NoITGCLC_024

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV024_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV024_PPUpdateHI

NoITGCLC_024:

	LDA LEV_Memory
	CMP #25
	BNE NoITGCLC_025

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV025_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV025_PPUpdateHI

NoITGCLC_025:

	LDA LEV_Memory
	CMP #26
	BNE NoITGCLC_026

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV026_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV026_PPUpdateHI

NoITGCLC_026:

	LDA LEV_Memory
	CMP #27
	BNE NoITGCLC_027

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV027_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV027_PPUpdateHI

NoITGCLC_027:

	LDA LEV_Memory
	CMP #28
	BNE NoITGCLC_028

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV028_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV028_PPUpdateHI

NoITGCLC_028:

	LDA LEV_Memory
	CMP #29
	BNE NoITGCLC_029

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV029_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV029_PPUpdateHI

NoITGCLC_029:

	LDA LEV_Memory
	CMP #30
	BNE NoITGCLC_030

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV030_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV030_PPUpdateHI

NoITGCLC_030:

	LDA LEV_Memory
	CMP #31
	BNE NoITGCLC_031

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV031_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV031_PPUpdateHI

NoITGCLC_031:

	LDA LEV_Memory
	CMP #32
	BNE NoITGCLC_032

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV032_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV032_PPUpdateHI

NoITGCLC_032:

	LDA LEV_Memory
	CMP #33
	BNE NoITGCLC_033

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV033_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV033_PPUpdateHI

NoITGCLC_033:

	LDA LEV_Memory
	CMP #34
	BNE NoITGCLC_034

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV034_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV034_PPUpdateHI

NoITGCLC_034:

	LDA LEV_Memory
	CMP #35
	BNE NoITGCLC_035

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV035_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV035_PPUpdateHI

NoITGCLC_035:

	LDA LEV_Memory
	CMP #36
	BNE NoITGCLC_036

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV036_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV036_PPUpdateHI

NoITGCLC_036:

	LDA LEV_Memory
	CMP #37
	BNE NoITGCLC_037

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV037_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV037_PPUpdateHI

NoITGCLC_037:

	LDA LEV_Memory
	CMP #38
	BNE NoITGCLC_038

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV038_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV038_PPUpdateHI

NoITGCLC_038:

	LDA LEV_Memory
	CMP #39
	BNE NoITGCLC_039

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV039_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV039_PPUpdateHI

NoITGCLC_039:

	LDA LEV_Memory
	CMP #40
	BNE NoITGCLC_040

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV040_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV040_PPUpdateHI

NoITGCLC_040:

	LDA LEV_Memory
	CMP #41
	BNE NoITGCLC_041

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV041_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV041_PPUpdateHI

NoITGCLC_041:

	LDA LEV_Memory
	CMP #42
	BNE NoITGCLC_042

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV042_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV042_PPUpdateHI

NoITGCLC_042:

	LDA LEV_Memory
	CMP #43
	BNE NoITGCLC_043

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV043_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV043_PPUpdateHI

NoITGCLC_043:

	LDA LEV_Memory
	CMP #44
	BNE NoITGCLC_044

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV044_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV044_PPUpdateHI

NoITGCLC_044:

	LDA LEV_Memory
	CMP #45
	BNE NoITGCLC_045

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV045_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV045_PPUpdateHI

NoITGCLC_045:

	LDA LEV_Memory
	CMP #46
	BNE NoITGCLC_046

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV046_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV046_PPUpdateHI

NoITGCLC_046:

	LDA LEV_Memory
	CMP #47
	BNE NoITGCLC_047

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV047_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV047_PPUpdateHI

NoITGCLC_047:

	LDA LEV_Memory
	CMP #48
	BNE NoITGCLC_048

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV048_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV048_PPUpdateHI

NoITGCLC_048:

	LDA LEV_Memory
	CMP #49
	BNE NoITGCLC_049

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV049_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV049_PPUpdateHI

NoITGCLC_049:

	LDA LEV_Memory
	CMP #50
	BNE NoITGCLC_050

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV050_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV050_PPUpdateHI

NoITGCLC_050:

	LDA LEV_Memory
	CMP #51
	BNE NoITGCLC_051

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV051_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV051_PPUpdateHI

NoITGCLC_051:

	LDA LEV_Memory
	CMP #52
	BNE NoITGCLC_052

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV052_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV052_PPUpdateHI

NoITGCLC_052:

	LDA LEV_Memory
	CMP #53
	BNE NoITGCLC_053

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV053_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV053_PPUpdateHI

NoITGCLC_053:

	LDA LEV_Memory
	CMP #54
	BNE NoITGCLC_054

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV054_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV054_PPUpdateHI

NoITGCLC_054:

	LDA LEV_Memory
	CMP #55
	BNE NoITGCLC_055

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV055_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV055_PPUpdateHI

NoITGCLC_055:

	LDA LEV_Memory
	CMP #56
	BNE NoITGCLC_056

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV056_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV056_PPUpdateHI

NoITGCLC_056:

	LDA LEV_Memory
	CMP #57
	BNE NoITGCLC_057

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV057_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV057_PPUpdateHI

NoITGCLC_057:

	LDA LEV_Memory
	CMP #58
	BNE NoITGCLC_058

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV058_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV058_PPUpdateHI

NoITGCLC_058:

	LDA LEV_Memory
	CMP #59
	BNE NoITGCLC_059

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV059_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV059_PPUpdateHI

NoITGCLC_059:

	LDA LEV_Memory
	CMP #60
	BNE NoITGCLC_060

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV060_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV060_PPUpdateHI

NoITGCLC_060:

	LDA LEV_Memory
	CMP #61
	BNE NoITGCLC_061

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV061_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV061_PPUpdateHI

NoITGCLC_061:

	LDA LEV_Memory
	CMP #62
	BNE NoITGCLC_062

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV062_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV062_PPUpdateHI

NoITGCLC_062:

	LDA LEV_Memory
	CMP #63
	BNE NoITGCLC_063

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV063_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV063_PPUpdateHI

NoITGCLC_063:

	LDA LEV_Memory
	CMP #64
	BNE NoITGCLC_064

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV064_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV064_PPUpdateHI

NoITGCLC_064:

	LDA LEV_Memory
	CMP #65
	BNE NoITGCLC_065

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV065_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV065_PPUpdateHI

NoITGCLC_065:

	LDA LEV_Memory
	CMP #66
	BNE NoITGCLC_066

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV066_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV066_PPUpdateHI

NoITGCLC_066:

	LDA LEV_Memory
	CMP #67
	BNE NoITGCLC_067

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV067_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV067_PPUpdateHI

NoITGCLC_067:

	LDA LEV_Memory
	CMP #68
	BNE NoITGCLC_068

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV068_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV068_PPUpdateHI

NoITGCLC_068:

	LDA LEV_Memory
	CMP #69
	BNE NoITGCLC_069

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV069_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV069_PPUpdateHI

NoITGCLC_069:

	LDA LEV_Memory
	CMP #70
	BNE NoITGCLC_070

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV070_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV070_PPUpdateHI

NoITGCLC_070:

	LDA LEV_Memory
	CMP #71
	BNE NoITGCLC_071

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV071_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV071_PPUpdateHI

NoITGCLC_071:

	LDA LEV_Memory
	CMP #72
	BNE NoITGCLC_072

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV072_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV072_PPUpdateHI

NoITGCLC_072:

	LDA LEV_Memory
	CMP #73
	BNE NoITGCLC_073

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV073_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV073_PPUpdateHI

NoITGCLC_073:

	LDA LEV_Memory
	CMP #74
	BNE NoITGCLC_074

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV074_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV074_PPUpdateHI

NoITGCLC_074:

	LDA LEV_Memory
	CMP #75
	BNE NoITGCLC_075

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV075_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV075_PPUpdateHI

NoITGCLC_075:

	LDA LEV_Memory
	CMP #76
	BNE NoITGCLC_076

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV076_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV076_PPUpdateHI

NoITGCLC_076:

	LDA LEV_Memory
	CMP #77
	BNE NoITGCLC_077

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV077_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV077_PPUpdateHI

NoITGCLC_077:

	LDA LEV_Memory
	CMP #78
	BNE NoITGCLC_078

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV078_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV078_PPUpdateHI

NoITGCLC_078:

	LDA LEV_Memory
	CMP #79
	BNE NoITGCLC_079

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV079_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV079_PPUpdateHI

NoITGCLC_079:

	LDA LEV_Memory
	CMP #80
	BNE NoITGCLC_080

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV080_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV080_PPUpdateHI

NoITGCLC_080:

	LDA LEV_Memory
	CMP #81
	BNE NoITGCLC_081

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV081_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV081_PPUpdateHI

NoITGCLC_081:

	LDA LEV_Memory
	CMP #82
	BNE NoITGCLC_082

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV082_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV082_PPUpdateHI

NoITGCLC_082:

	LDA LEV_Memory
	CMP #83
	BNE NoITGCLC_083

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV083_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV083_PPUpdateHI

NoITGCLC_083:

	LDA LEV_Memory
	CMP #84
	BNE NoITGCLC_084

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV084_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV084_PPUpdateHI

NoITGCLC_084:

	LDA LEV_Memory
	CMP #85
	BNE NoITGCLC_085

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV085_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV085_PPUpdateHI

NoITGCLC_085:

	LDA LEV_Memory
	CMP #86
	BNE NoITGCLC_086

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV086_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV086_PPUpdateHI

NoITGCLC_086:

	LDA LEV_Memory
	CMP #87
	BNE NoITGCLC_087

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV087_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV087_PPUpdateHI

NoITGCLC_087:

	LDA LEV_Memory
	CMP #88
	BNE NoITGCLC_088

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV088_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV088_PPUpdateHI

NoITGCLC_088:

	LDA LEV_Memory
	CMP #89
	BNE NoITGCLC_089

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV089_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV089_PPUpdateHI

NoITGCLC_089:

	LDA LEV_Memory
	CMP #90
	BNE NoITGCLC_090

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV090_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV090_PPUpdateHI

NoITGCLC_090:

	LDA LEV_Memory
	CMP #91
	BNE NoITGCLC_091

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV091_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV091_PPUpdateHI

NoITGCLC_091:

	LDA LEV_Memory
	CMP #92
	BNE NoITGCLC_092

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV092_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV092_PPUpdateHI

NoITGCLC_092:

	LDA LEV_Memory
	CMP #93
	BNE NoITGCLC_093

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV093_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV093_PPUpdateHI

NoITGCLC_093:

	LDA LEV_Memory
	CMP #94
	BNE NoITGCLC_094

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV094_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV094_PPUpdateHI

NoITGCLC_094:

	LDA LEV_Memory
	CMP #95
	BNE NoITGCLC_095

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV095_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV095_PPUpdateHI

NoITGCLC_095:

	LDA LEV_Memory
	CMP #96
	BNE NoITGCLC_096

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV096_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV096_PPUpdateHI

NoITGCLC_096:

	LDA LEV_Memory
	CMP #97
	BNE NoITGCLC_097

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV097_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV097_PPUpdateHI

NoITGCLC_097:

	LDA LEV_Memory
	CMP #98
	BNE NoITGCLC_098

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV098_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV098_PPUpdateHI

NoITGCLC_098:

	LDA LEV_Memory
	CMP #99
	BNE NoITGCLC_099

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV099_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV099_PPUpdateHI

NoITGCLC_099:

	LDA LEV_Memory
	CMP #100
	BNE NoITGCLC_100

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV100_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV100_PPUpdateHI

NoITGCLC_100:

	LDA LEV_Memory
	CMP #101
	BNE NoITGCLC_101

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV101_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV101_PPUpdateHI

NoITGCLC_101:

	LDA LEV_Memory
	CMP #102
	BNE NoITGCLC_102

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV102_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV102_PPUpdateHI

NoITGCLC_102:

	LDA LEV_Memory
	CMP #103
	BNE NoITGCLC_103

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV103_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV103_PPUpdateHI

NoITGCLC_103:

	LDA LEV_Memory
	CMP #104
	BNE NoITGCLC_104

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV104_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV104_PPUpdateHI

NoITGCLC_104:

	LDA LEV_Memory
	CMP #105
	BNE NoITGCLC_105

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV105_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV105_PPUpdateHI

NoITGCLC_105:

	LDA LEV_Memory
	CMP #106
	BNE NoITGCLC_106

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV106_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV106_PPUpdateHI

NoITGCLC_106:

	LDA LEV_Memory
	CMP #107
	BNE NoITGCLC_107

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV107_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV107_PPUpdateHI

NoITGCLC_107:

	LDA LEV_Memory
	CMP #108
	BNE NoITGCLC_108

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV108_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV108_PPUpdateHI

NoITGCLC_108:

	LDA LEV_Memory
	CMP #109
	BNE NoITGCLC_109

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV109_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV109_PPUpdateHI

NoITGCLC_109:

	LDA LEV_Memory
	CMP #110
	BNE NoITGCLC_110

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV110_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV110_PPUpdateHI

NoITGCLC_110:

	LDA LEV_Memory
	CMP #111
	BNE NoITGCLC_111

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV111_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV111_PPUpdateHI

NoITGCLC_111:

	LDA LEV_Memory
	CMP #112
	BNE NoITGCLC_112

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV112_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV112_PPUpdateHI

NoITGCLC_112:

	LDA LEV_Memory
	CMP #113
	BNE NoITGCLC_113

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV113_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV113_PPUpdateHI

NoITGCLC_113:

	LDA LEV_Memory
	CMP #114
	BNE NoITGCLC_114

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV114_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV114_PPUpdateHI

NoITGCLC_114:

	LDA LEV_Memory
	CMP #115
	BNE NoITGCLC_115

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV115_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV115_PPUpdateHI

NoITGCLC_115:

	LDA LEV_Memory
	CMP #116
	BNE NoITGCLC_116

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV116_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV116_PPUpdateHI

NoITGCLC_116:

	LDA LEV_Memory
	CMP #117
	BNE NoITGCLC_117

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV117_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV117_PPUpdateHI

NoITGCLC_117:

	LDA LEV_Memory
	CMP #118
	BNE NoITGCLC_118

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV118_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV118_PPUpdateHI

NoITGCLC_118:

	LDA LEV_Memory
	CMP #119
	BNE NoITGCLC_119

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV119_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV119_PPUpdateHI

NoITGCLC_119:

	LDA LEV_Memory
	CMP #120
	BNE NoITGCLC_120

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV120_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV120_PPUpdateHI

NoITGCLC_120:

	LDA LEV_Memory
	CMP #121
	BNE NoITGCLC_121

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV121_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV121_PPUpdateHI

NoITGCLC_121:

	LDA LEV_Memory
	CMP #122
	BNE NoITGCLC_122

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV122_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV122_PPUpdateHI

NoITGCLC_122:

	LDA LEV_Memory
	CMP #123
	BNE NoITGCLC_123

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV123_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV123_PPUpdateHI

NoITGCLC_123:

	LDA LEV_Memory
	CMP #124
	BNE NoITGCLC_124

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV124_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV124_PPUpdateHI

NoITGCLC_124:

	LDA LEV_Memory
	CMP #125
	BNE NoITGCLC_125

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV125_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV125_PPUpdateHI

NoITGCLC_125:

	LDA LEV_Memory
	CMP #126
	BNE NoITGCLC_126

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV126_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV126_PPUpdateHI

NoITGCLC_126:

	LDA LEV_Memory
	CMP #127
	BNE NoITGCLC_127

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV127_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV127_PPUpdateHI

NoITGCLC_127:

	LDA LEV_Memory
	CMP #128
	BNE NoITGCLC_128

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV128_PPUpdateLO
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV128_PPUpdateHI

NoITGCLC_128:
	RTS

Fiskbit
Posts: 202
Joined: Sat Nov 18, 2017 9:15 pm

Re: Simplify long, repetitive code.

Post by Fiskbit » Sat Sep 05, 2020 7:48 am

It's common to split out components into separate tables:

ITEMGOLDLEV001_PPUpdateLO .ds 1
ITEMGOLDLEV002_PPUpdateLO .ds 1
ITEMGOLDLEV003_PPUpdateLO .ds 1
(...)
ITEMGOLDLEV128_PPUpdateLO .ds 1

ITEMGOLDLEV001_PPUpdateHI .ds 1
ITEMGOLDLEV002_PPUpdateHI .ds 1
ITEMGOLDLEV003_PPUpdateHI .ds 1
(...)
ITEMGOLDLEV128_PPUpdateHI .ds 1

Then you would write the code like this, which is basically the same as what you have, but I'm subtracting 1 from the HI table, too:

Code: Select all

	LDX LEV_Memory
	BEQ Done
	CPX #129
	BCS Done
	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV001_PPUpdateLO-1,X
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV001_PPUpdateHI-1,X
Done:
	RTS
If you were to leave both components in the same table, you could solve it by shifting the index left once, which multiples it by 2, which is the size of each element. That won't quite work in this case, though, because your 128th level is represented with #$80, which overflows to #$00 when shifted left, instead of #$7F, which would just barely fit. Ignoring that level 128 problem, that code looks like this:

Code: Select all

	LDA LEV_Memory
	BEQ Done
	CMP #129
	BCS Done
	ASL
	TAX  ; Edit: Forgot this instruction
	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV001_PPUpdateLO-2,X
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV001_PPUpdateHI-2,X
Done:
	RTS
Last edited by Fiskbit on Wed Sep 09, 2020 5:01 am, edited 1 time in total.

sdm2
Posts: 33
Joined: Wed Feb 05, 2020 12:59 pm
Location: Poland

Re: Simplify long, repetitive code.

Post by sdm2 » Sat Sep 05, 2020 8:26 am

Thanks. I haven't tested it yet, but it seems ok: ?

Code: Select all

	ldx LEV_Memory
	beq Done
	cpx #129
	bcs Done

	txa
	asl a
	tax

	lda ITEMLEV_Temp_PPUpdateLO
	sta ITEMGOLDLEV001_PPUpdateLO-2,x
	lda ITEMLEV_Temp_PPUpdateHI
	sta ITEMGOLDLEV001_PPUpdateHI-2,x
Done:
	rts
	

Code: Select all

	LDA LEV_Memory
	BEQ Done
	CMP #129
	BCS Done

	asl a
	tax

	LDA ITEMLEV_Temp_PPUpdateLO
	STA ITEMGOLDLEV001_PPUpdateLO-2,X
	LDA ITEMLEV_Temp_PPUpdateHI
	STA ITEMGOLDLEV001_PPUpdateHI-2,X

Done:
EDIT: Did not work. :/


Fiskbit wrote:
Sat Sep 05, 2020 7:48 am
It's common to split out components into separate tables
Only now will this sequence conflict with the addressing mode used in other code (LO, and HI as next byte - little endian).

ps. With LEV128 it's not a problem to add a manual check at the end.

sdm2
Posts: 33
Joined: Wed Feb 05, 2020 12:59 pm
Location: Poland

Re: Simplify long, repetitive code.

Post by sdm2 » Tue Sep 08, 2020 11:52 pm

EDIT:
Ok it's already working. I had badly defined variables in the ram.


In fact, the jump from "done" is not needed, because the program is only executed when the values in lev_memory are from 1 to 128 (when the code is executed, there is NEVER another value there ($01-$80).
So beq, cmp # 129 and doing a "Done" jump when the value is 0 doesn't make sense because it will never be done.

Code: Select all

function:

	lda LEV_Memory				;for example 01 is loaded
	asl a					;now is 02
	tax					;transfer to x

	lda ITEMLEV_Temp_PPUpdateLO		;load temp
	sta ITEMGOLDLEV001_PPUpdateLO-2,x	;store to $6000 with -2 (5FFE + increased by x = store to 6000)
	lda ITEMLEV_Temp_PPUpdateHI                 ;load temp hi
	sta ITEMGOLDLEV001_PPUpdateHI-2,x        ;store to $6001 with -2 (5FFF + x inc = store to 6001)

	RTS
	

Code: Select all

-2
-1
ITEMGOLDLEV001_PPUpdateLO	.ds 1	;$6000
ITEMGOLDLEV001_PPUpdateHI	.ds 1	;$6001
ITEMGOLDLEV002_PPUpdateLO	.ds 1	;$6002
ITEMGOLDLEV002_PPUpdateHI	.ds 1	;$6003
ITEMGOLDLEV003_PPUpdateLO	.ds 1	;$6004
ITEMGOLDLEV003_PPUpdateHI	.ds 1	;$6005...
(...)
also doesn't work. I don't know where the problem is.
Last edited by sdm2 on Tue Nov 17, 2020 6:23 am, edited 1 time in total.

calima
Posts: 1247
Joined: Tue Oct 06, 2015 10:16 am

Re: Simplify long, repetitive code.

Post by calima » Wed Sep 09, 2020 12:12 am

The point of separate LO and HI is that they are separate. You interleaved them. (that's not why the code doesn't work, but it's wasteful for you to go to the trouble...)

edit: at least 128*2 will overflow.
edit 2: The way you kept all the unneeded labels makes it look like it's an attempt at separating. Further, giving each their own label allows the assembler to reorder them; I don't know if your assembler does, but it's a risk.

Fiskbit
Posts: 202
Joined: Sat Nov 18, 2017 9:15 pm

Re: Simplify long, repetitive code.

Post by Fiskbit » Wed Sep 09, 2020 5:07 am

The code looks correct to me, aside from not yet handling level 128 correctly. Your bug may be elsewhere.

sdm2
Posts: 33
Joined: Wed Feb 05, 2020 12:59 pm
Location: Poland

Re: Simplify long, repetitive code.

Post by sdm2 » Tue Nov 17, 2020 6:56 am

I have a bit more complicated code that I'd like to simplify.

The "SetBlockFlags" function is executed when block 16x16 is destroyed. The PPU address is checked and if it is correct, the flag is lit, which then informs where the background elements should be cleaned in future (re-enetering map).

I divided the screen into 13 lines, each two bytes (2x8 bytes), i.e. 13x 16 bits. Each bit is responsible for one 16x16 background block.
At the moment of re-entering this level, the "UpdateNT_Block" function runs, which checks the lit bits and resets (store "Air" tile number) the destroyed background element (and resets the bytes in the ram memory where the collision map is (the same "Air" tiles number as saved in PPU)) - thanks to this, the previously destroyed wall will be remembered.

The program generally works fine, but is quite large.

"LEV_UpdateBlock_Linexxx" variables are temporary for calculations, after exiting a specific screen, they are transferred to eg "LEV001-256_UpdateBlock_Linexxx" - separate variables for each screen / level to remember information about damaged walls.
Attachments
setblockflags.txt
(40.87 KiB) Downloaded 15 times
updateblock.txt
(77.78 KiB) Downloaded 14 times
blockmap.png

unregistered
Posts: 1101
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: Simplify long, repetitive code.

Post by unregistered » Wed Nov 18, 2020 4:16 pm

sdm2 wrote:
Tue Nov 17, 2020 6:56 am
The program generally works fine, but is quite large.

"LEV_UpdateBlock_Linexxx" variables are temporary for calculations, after exiting a specific screen, they are transferred to eg "LEV001-256_UpdateBlock_Linexxx" - separate variables for each screen / level to remember information about damaged walls.
Note: I did NOT look at your attached code.

However, all these LEV_UpdateBlock_Linexxx variables being transferred to LEV001-256_UpdateBlock_Linexxx variables reminds me of the time when I too had tons of variables for things such as collision data for one screen.

The crucial help that I received caused me to understand that the code becomes MUCH simpler when we just use 1 large variable to hold an entire screen (my collision variable holds 2 entire screens worth of collision data). The trick that I was taught, and worked for me bc all of my collision variables were declared sequentially in one section of RAM.

This trick works in the asm6 assembler (have no clue if it’s possible in NESASM). Near the beginning of my code, right after the header, there’s this line:

Code: Select all

colTableW = colTable0
Therefore, the asm6 assembler finds colTable0’s address, set in the variables declaration section, and assigns that address to colTableW.

Then, I was able to use simple code like lda colTableW, x to load individual collision values from that giant variable.

And can use simple code like sta colTableW, x to store a collision value to anywhere inside that giant variable. You just need to set x accordingly. :)


Ooh, my W stands for “wide”, but feel free to use your own names; it’s your code. :)

...and feel free to use something, equivalent in NESASM, like this asm6 code:

Code: Select all

colTable0 .dsb ###
instead of all of the variables I imagine you’ve declared. :) I don’t know how much space your collision variables take so you can fill in ###.


Final note: With just my first code example, it doesn’t matter that your code know how big colTableW is, just its starting address is necessary bc your countless collision variables have already been declared sequentially; so the nes already has that space reserved. You just need to remember the size of your collision variable... if it just holds one screen and, therefore, is 240 bytes large, a nice loop can be created if you start the data at say $0610:

Code: Select all

  ldx #$10
— ;code to determine what collision value to write
  ;goes here
  sta colTableW, x
  inx
  bne —
That simple loop will write your entire screen worth of collision values. The cpx isn’t needed bc when x becomes 00, your entire screen-worth of collision values will be drawn.


edit: If writing values to “byte sized” sections of the colTableW is preferable:

Code: Select all

  ldx #$07
— ;code to determine what collision value to write
  ;goes here
  sta colTableW, x
  sta colTableW+8, x
  sta colTableW+16, x
  sta colTableW+24, x
  ;...etc :)
  dex
  bpl —
The cpx here is unnecessary again bc after it writes byte0 to each of the 8byte sections, dex will change x to #$ff and the negative flag will be set and the loop will quit. :) Though your code for this loop will probably need to be more complex (why write the same value to every 8byte section in your collision table?)... but, it’s your game so experiment away. :)

final-edit.

sdm2
Posts: 33
Joined: Wed Feb 05, 2020 12:59 pm
Location: Poland

Re: Simplify long, repetitive code.

Post by sdm2 » Thu Nov 19, 2020 1:53 am

Thanks. Only my method as I wrote, "one flag = one 16x16 background block" must stay, because I use these flags for another purpose, so changing the code rule is not an option. The code must work as in the attachment.
unregistered wrote:
Wed Nov 18, 2020 4:16 pm
You just need to remember the size of your collision variable... if it just holds one screen and, therefore, is 240 bytes large
So one screen / level of information about the collision with the background takes 240 bytes? In my code, one screen is 26 bytes (13x 16bits) x 256 screens = 6656 bytes (I don't count the screen border blocks, these are solid). I am not a good programmer, it is possible to use less memory for this purpose. But it seems to me that one bit per one 16x16 block of the screen is probably not bad.

Attached is a sample test game that uses this code.
Attachments
kosmity_atakujo_openmap.nes
(512.02 KiB) Downloaded 4 times
Last edited by sdm2 on Tue Nov 24, 2020 8:35 am, edited 1 time in total.

unregistered
Posts: 1101
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: Simplify long, repetitive code.

Post by unregistered » Thu Nov 19, 2020 9:58 am

sdm2 wrote:
Thu Nov 19, 2020 1:53 am
So one screen / level of information about the collision with the background takes 240 bytes? In my code, one screen is 26 bytes (13x 16bits) x 256 screens = 6656 bytes (I don't count the screen border blocks, these are solid).
Yes, is CAN be smaller, if 1 bit per 16bit screen block is efficient for you, that’s excellent! :D

In our platformer, we currently need 7 5 multiple collision bits for each 16bit block. Each 8bit block can be solid or passthroughable or other things... so the extra bits are needed for our game. But, since you don’t need all that, it’s much simpler! :)


edit: sry, 2 of those collision bits are where we have stored the palette for each 16bit metatile. That simplifies things since we had 3 unused collision bits for each 16bit metatile.

final-edit: Forgot that it’s a bit more complex. :oops: Honestly, “multiple” seems to be the best choice of words.

Post Reply