Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. ; MIT License
  3. ;
  4. ; Copyright (c) 2018 Eldred Habert
  5. ; Originally hosted at https://github.com/ISSOtm/rgbds-structs
  6. ;
  7. ; Permission is hereby granted, free of charge, to any person obtaining a copy
  8. ; of this software and associated documentation files (the "Software"), to deal
  9. ; in the Software without restriction, including without limitation the rights
  10. ; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. ; copies of the Software, and to permit persons to whom the Software is
  12. ; furnished to do so, subject to the following conditions:
  13. ;
  14. ; The above copyright notice and this permission notice shall be included in all
  15. ; copies or substantial portions of the Software.
  16. ;
  17. ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. ; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. ; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. ; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. ; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. ; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. ; SOFTWARE.
  24.  
  25. if !def(STRUCT_INC)
  26. STRUCT_INC set 1
  27.  
  28. ; struct struct_name
  29. ; Begins a struct declaration
  30. struct: MACRO
  31.     IF DEF(NB_FIELDS)
  32.         FAIL "Please close struct definitions using `end_struct`"
  33.     ENDC
  34.  
  35. STRUCT_NAME equs "\1"
  36.  
  37. NB_FIELDS = 0
  38.     RSRESET
  39. ENDM
  40.  
  41. ; end_struct
  42. ; Ends a struct declaration
  43. end_struct: MACRO
  44. ; Set nb of fields
  45. STRUCT_NB_FIELDS equs "{STRUCT_NAME}_nb_fields"
  46. STRUCT_NB_FIELDS = NB_FIELDS
  47.     PURGE STRUCT_NB_FIELDS
  48.  
  49. ; Set size of struct
  50. STRUCT_SIZEOF equs "sizeof_{STRUCT_NAME}"
  51. STRUCT_SIZEOF RB 0
  52.     PURGE STRUCT_SIZEOF
  53.  
  54.     PURGE NB_FIELDS
  55.     PURGE STRUCT_NAME
  56. ENDM
  57.  
  58.  
  59. ; field_name_from_id field_id
  60. ; For internal use, please do not use externally
  61. field_name_from_id: MACRO
  62. FIELD_ID_STR equs "{\1}"
  63. STRUCT_FIELD equs STRCAT("{STRUCT_NAME}_field", STRSUB("{FIELD_ID_STR}", 2, STRLEN("{FIELD_ID_STR}") - 1))
  64. STRUCT_FIELD_NAME equs "{STRUCT_FIELD}_name"
  65. STRUCT_FIELD_SIZE equs "{STRUCT_FIELD}_size"
  66. ENDM
  67.  
  68.  
  69. ; new_field nb_elems, rs_type, field_name
  70. ; For internal use, please do not use externally
  71. new_field: MACRO
  72.     IF !DEF(STRUCT_NAME) || !DEF(NB_FIELDS)
  73.         FAIL "Please start defining a struct, using `define_struct`"
  74.     ENDC
  75.  
  76.     field_name_from_id NB_FIELDS
  77. ; Set field name
  78. STRUCT_FIELD_NAME equs "\"\3\""
  79.     PURGE STRUCT_FIELD_NAME
  80.  
  81. ; Set field offset
  82. STRUCT_FIELD \2 \1
  83. ; Alias this in a human-comprehensive manner
  84. STRUCT_FIELD_NAME equs "{STRUCT_NAME}_\3"
  85. STRUCT_FIELD_NAME = STRUCT_FIELD
  86.  
  87. ; Calculate field size
  88. CURRENT_RS RB 0
  89. STRUCT_FIELD_SIZE = CURRENT_RS - STRUCT_FIELD
  90.  
  91.     PURGE FIELD_ID_STR
  92.     PURGE STRUCT_FIELD
  93.     PURGE STRUCT_FIELD_NAME
  94.     PURGE STRUCT_FIELD_SIZE
  95.     PURGE CURRENT_RS
  96.  
  97. NB_FIELDS = NB_FIELDS + 1
  98. ENDM
  99.  
  100. ; bytes nb_bytes, field_name
  101. ; Defines a field of N bytes
  102. field: MACRO
  103.     new_field \1, RB, \2
  104. ENDM
  105.  
  106.  
  107. ; dstruct struct_type, var_name
  108. ; Allocates space for a struct in memory (primarily RAM)
  109. dstruct: MACRO
  110. NB_FIELDS equs "\1_nb_fields"
  111.     IF !DEF(NB_FIELDS)
  112.         FAIL "Struct \1 isn't defined!"
  113.     ENDC
  114. STRUCT_NAME equs "\1" ; Target this struct for `field_name_from_id`
  115.  
  116. \2:: ; Declare the struct's root
  117.  
  118. FIELD_ID = 0
  119.     REPT NB_FIELDS
  120.  
  121.         field_name_from_id FIELD_ID
  122. FIELD_NAME equs STRCAT(".", STRUCT_FIELD_NAME)
  123. FIELD_NAME::
  124.         ds STRUCT_FIELD_SIZE
  125.  
  126.         ; Clean up vars for next iteration
  127.         PURGE FIELD_ID_STR
  128.         PURGE STRUCT_FIELD
  129.         PURGE STRUCT_FIELD_NAME
  130.         PURGE STRUCT_FIELD_SIZE
  131.         PURGE FIELD_NAME
  132.  
  133. FIELD_ID = FIELD_ID + 1
  134.     ENDR
  135.  
  136.  
  137.     ; Define variable's properties from struct's
  138. \2_nb_fields = NB_FIELDS
  139. sizeof_\2 = sizeof_\1
  140.  
  141.  
  142.     ; Clean up
  143.     PURGE NB_FIELDS
  144.     PURGE STRUCT_NAME
  145.     PURGE FIELD_ID
  146. ENDM
  147.  
  148. ; dstruct_init struct_type, var_name
  149. ; Starts the initialization of a struct in ROM
  150. dstruct_init: MACRO
  151. NB_FIELDS equs "\1_nb_fields"
  152.     IF !DEF(NB_FIELDS)
  153.         FAIL "Struct \1 isn't defined!"
  154.     ENDC
  155. STRUCT_NAME equs "\1"
  156. VAR_NAME equs "\2"
  157.  
  158. VAR_NAME::
  159.  
  160. FIELD_COUNT = NB_FIELDS
  161. FIELD_ID = 0
  162. ENDM
  163.  
  164. ; dfield field_name, data_type, field_value
  165. ; Defines the data for the next field of the current struct initialization
  166. dfield: MACRO
  167.     field_name_from_id FIELD_ID
  168.     IF STRUCT_FIELD_NAME != "\1"
  169.         FAIL "Struct field name \1 does not match!"
  170.     ENDC
  171. DATA_TYPE equs "\2"
  172. FIELD_SIZE equ STRUCT_FIELD_SIZE
  173. FIELD_NAME equs STRCAT(STRCAT("{VAR_NAME}", "."), STRUCT_FIELD_NAME)
  174. FIELD_NAME::
  175.     REPT _NARG - 2
  176.         DATA_TYPE \3
  177.         SHIFT
  178.     ENDR
  179. FIELD_NAME_END equs STRCAT("{FIELD_NAME}", "_end")
  180. FIELD_NAME_END:
  181. FIELD_DIFF equ FIELD_NAME_END - FIELD_NAME
  182.     IF FIELD_SIZE != FIELD_DIFF
  183.         FAIL "Struct {VAR_NAME} field {{STRUCT_FIELD_NAME}} is not correct size of {FIELD_SIZE} bytes! Found {FIELD_DIFF} instead."
  184.     ENDC
  185.  
  186. PURGE DATA_TYPE
  187. PURGE FIELD_ID_STR
  188. PURGE STRUCT_FIELD
  189. PURGE STRUCT_FIELD_NAME
  190. PURGE STRUCT_FIELD_SIZE
  191. PURGE FIELD_NAME
  192. PURGE FIELD_NAME_END
  193. PURGE FIELD_SIZE
  194. PURGE FIELD_DIFF
  195.  
  196. FIELD_ID = FIELD_ID + 1
  197. ENDM
  198.  
  199. ; dstruct_end
  200. ; Finalizes the current struct initialization
  201. dstruct_end: MACRO
  202.     IF FIELD_ID < FIELD_COUNT
  203.         field_name_from_id FIELD_ID
  204.         FAIL "Struct {VAR_NAME} is missing fields! Expected {{STRUCT_FIELD_NAME}} next."
  205.     ENDC
  206.     PURGE NB_FIELDS
  207.     PURGE STRUCT_NAME
  208.     PURGE VAR_NAME
  209.     PURGE FIELD_ID
  210.     PURGE FIELD_COUNT
  211. ENDM
  212.  
  213. ENDC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement