Skip to content

Commit 9043b99

Browse files
committed
Add field typing and ROM struct declaration
1 parent 8e848fe commit 9043b99

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

structs.asm

+42-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
; MIT License
33
;
4-
; Copyright (c) 2018 Eldred Habert
4+
; Copyright (c) 2018-2019 Eldred Habert
55
; Originally hosted at https://github.com/ISSOtm/rgbds-structs
66
;
77
; Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -60,7 +60,9 @@ field_name_from_id: MACRO
6060
FIELD_ID_STR equs "{\1}"
6161
STRUCT_FIELD equs STRCAT("{STRUCT_NAME}_field", STRSUB("{FIELD_ID_STR}", 2, STRLEN("{FIELD_ID_STR}") - 1))
6262
STRUCT_FIELD_NAME equs "{STRUCT_FIELD}_name"
63-
STRUCT_FIELD_SIZE equs "{STRUCT_FIELD}_size"
63+
STRUCT_FIELD_TYPE equs "{STRUCT_FIELD}_type"
64+
STRUCT_FIELD_NBEL equs "{STRUCT_FIELD}_nb_el" ; Number of elements
65+
STRUCT_FIELD_SIZE equs "{STRUCT_FIELD}_size" ; sizeof(type) * nb_el
6466
ENDM
6567

6668

@@ -77,7 +79,7 @@ STRUCT_FIELD_NAME equs "\"\3\""
7779
PURGE STRUCT_FIELD_NAME
7880

7981
; Set field offset
80-
STRUCT_FIELD \2 \1
82+
STRUCT_FIELD \2 (\1)
8183
; Alias this in a human-comprehensive manner
8284
STRUCT_FIELD_NAME equs "{STRUCT_NAME}_\3"
8385
STRUCT_FIELD_NAME = STRUCT_FIELD
@@ -86,9 +88,15 @@ STRUCT_FIELD_NAME = STRUCT_FIELD
8688
CURRENT_RS RB 0
8789
STRUCT_FIELD_SIZE = CURRENT_RS - STRUCT_FIELD
8890

91+
; Set properties
92+
STRUCT_FIELD_NBEL = \1
93+
STRUCT_FIELD_TYPE equs STRSUB("\2", 2, 1)
94+
8995
PURGE FIELD_ID_STR
9096
PURGE STRUCT_FIELD
9197
PURGE STRUCT_FIELD_NAME
98+
PURGE STRUCT_FIELD_TYPE
99+
PURGE STRUCT_FIELD_NBEL
92100
PURGE STRUCT_FIELD_SIZE
93101
PURGE CURRENT_RS
94102

@@ -114,29 +122,54 @@ longs: MACRO
114122
ENDM
115123

116124

117-
; dstruct struct_type, var_name
118-
; Allocates space for a struct in memory (primarily RAM)
125+
; dstruct struct_type, var_name[, ...]
126+
; Allocates space for a struct in memory
127+
; If no further arguments are supplied, the space is simply allocated (using `ds`)
128+
; Otherwise, the data is written to memory using the appropriate types
129+
; For example, a struct defined with `bytes 1, Field1` and `words 3, Field2` would have four extra arguments, one byte then three words.
119130
dstruct: MACRO
120131
NB_FIELDS equs "\1_nb_fields"
121132
IF !DEF(NB_FIELDS)
122133
FAIL "Struct \1 isn't defined!"
123134
ENDC
124135
STRUCT_NAME equs "\1" ; Target this struct for `field_name_from_id`
136+
VAR_NAME equs "\2"
125137

126-
\2:: ; Declare the struct's root
138+
VAR_NAME:: ; Declare the struct's root
127139

128140
FIELD_ID = 0
129141
REPT NB_FIELDS
130142

131143
field_name_from_id FIELD_ID
132-
FIELD_NAME equs STRCAT("\2_", STRUCT_FIELD_NAME)
144+
FIELD_NAME equs STRCAT("{VAR_NAME}_", STRUCT_FIELD_NAME)
133145
FIELD_NAME::
134-
ds STRUCT_FIELD_SIZE
146+
IF _NARG == 2 ; RAM definition, no data
147+
ds STRUCT_FIELD_SIZE
148+
ELSE
149+
TMP equs STRCAT("\{", STRCAT("{STRUCT_FIELD_TYPE}", "\}")) ; Temp var for double deref because "{{STRUCT_FIELD_TYPE}}" is a syntax error
150+
DATA_TYPE equs STRCAT("D", TMP)
151+
PURGE TMP
152+
SHIFT_FIELDS equs ""
153+
REPT STRUCT_FIELD_NBEL
154+
DATA_TYPE \3
155+
SHIFT
156+
; Stupid hack because RGBDS saves the macro arguments when entering REPT blocks
157+
TMP equs "{SHIFT_FIELDS}\n\tSHIFT"
158+
PURGE SHIFT_FIELDS
159+
SHIFT_FIELDS equs "{TMP}"
160+
PURGE TMP
161+
ENDR
162+
SHIFT_FIELDS
163+
PURGE SHIFT_FIELDS
164+
PURGE DATA_TYPE
165+
ENDC
135166

136167
; Clean up vars for next iteration
137168
PURGE FIELD_ID_STR
138169
PURGE STRUCT_FIELD
139170
PURGE STRUCT_FIELD_NAME
171+
PURGE STRUCT_FIELD_TYPE
172+
PURGE STRUCT_FIELD_NBEL
140173
PURGE STRUCT_FIELD_SIZE
141174
PURGE FIELD_NAME
142175

@@ -152,5 +185,6 @@ sizeof_\2 = sizeof_\1
152185
; Clean up
153186
PURGE NB_FIELDS
154187
PURGE STRUCT_NAME
188+
PURGE VAR_NAME
155189
PURGE FIELD_ID
156190
ENDM

0 commit comments

Comments
 (0)