An emulator, assembler, and disassembler for the Sega Game Gear
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

state.c 4.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Copyright (C) 2014-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. Released under the terms of the MIT License. See LICENSE for details. */
  3. #include <stdlib.h>
  4. #include "state.h"
  5. #include "io.h"
  6. #include "../logging.h"
  7. /*
  8. Initialize default values in an AssemblerState object.
  9. */
  10. void state_init(AssemblerState *state)
  11. {
  12. state->header.offset = DEFAULT_HEADER_OFFSET;
  13. state->header.checksum = true;
  14. state->header.product_code = 0;
  15. state->header.version = 0;
  16. state->header.region = DEFAULT_REGION;
  17. state->header.rom_size = DEFAULT_DECLSIZE;
  18. state->cross_blocks = false;
  19. state->rom_size = 0;
  20. state->lines = NULL;
  21. state->includes = NULL;
  22. state->instructions = NULL;
  23. state->data = NULL;
  24. state->symtable = NULL;
  25. }
  26. /*
  27. Deallocate the contents of an AssemblerState object.
  28. */
  29. void state_free(AssemblerState *state)
  30. {
  31. asm_lines_free(state->lines);
  32. asm_includes_free(state->includes);
  33. asm_instructions_free(state->instructions);
  34. asm_data_free(state->data);
  35. asm_symtable_free(state->symtable);
  36. }
  37. /*
  38. Callback function for freeing an ASMSymbol.
  39. */
  40. static void free_asm_symbol(ASMSymbol *node)
  41. {
  42. free(node->symbol);
  43. free(node);
  44. }
  45. /*
  46. Initialize an ASMSymbolTable and place it in *symtable_ptr.
  47. */
  48. void asm_symtable_init(ASMSymbolTable **symtable_ptr)
  49. {
  50. *symtable_ptr = hash_table_NEW(ASMSymbol, symbol, next, free_asm_symbol);
  51. }
  52. /*
  53. Callback function for freeing an ASMDefine.
  54. */
  55. static void free_asm_define(ASMDefine *node)
  56. {
  57. free(node->name);
  58. free(node);
  59. }
  60. /*
  61. Create and return a new ASMDefineTable.
  62. */
  63. ASMDefineTable* asm_deftable_new()
  64. {
  65. return hash_table_NEW(ASMDefine, name, next, free_asm_define);
  66. }
  67. /*
  68. Deallocate an ASMLine list.
  69. */
  70. void asm_lines_free(ASMLine *line)
  71. {
  72. while (line) {
  73. ASMLine *temp = line->next;
  74. free(line->data);
  75. free(line);
  76. line = temp;
  77. }
  78. }
  79. /*
  80. Deallocate an ASMInclude list.
  81. */
  82. void asm_includes_free(ASMInclude *include)
  83. {
  84. while (include) {
  85. ASMInclude *temp = include->next;
  86. line_buffer_free(include->lines);
  87. free(include);
  88. include = temp;
  89. }
  90. }
  91. /*
  92. Deallocate an ASMInstruction list.
  93. */
  94. void asm_instructions_free(ASMInstruction *inst)
  95. {
  96. while (inst) {
  97. ASMInstruction *temp = inst->next;
  98. free(inst->bytes);
  99. free(inst->symbol);
  100. free(inst);
  101. inst = temp;
  102. }
  103. }
  104. /*
  105. Deallocate an ASMData list.
  106. */
  107. void asm_data_free(ASMData *data)
  108. {
  109. while (data) {
  110. ASMData *temp = data->next;
  111. free(data->bytes);
  112. free(data);
  113. data = temp;
  114. }
  115. }
  116. /*
  117. Deallocate an ASMSymbolTable.
  118. */
  119. void asm_symtable_free(ASMSymbolTable *symtable)
  120. {
  121. hash_table_free(symtable);
  122. }
  123. /*
  124. Deallocate an ASMDefineTable.
  125. */
  126. void asm_deftable_free(ASMDefineTable *deftable)
  127. {
  128. hash_table_free(deftable);
  129. }
  130. /*
  131. Search for a key in the symbol table.
  132. Return the corresponding symbol on success and NULL on failure.
  133. */
  134. const ASMSymbol* asm_symtable_find(const ASMSymbolTable *tab, const char *key)
  135. {
  136. return (ASMSymbol*) hash_table_find(tab, key, -1);
  137. }
  138. /*
  139. Insert a symbol into the table.
  140. This doesn't check for duplicate keys, so you must do that beforehand.
  141. */
  142. void asm_symtable_insert(ASMSymbolTable *tab, ASMSymbol *symbol)
  143. {
  144. hash_table_insert(tab, (HashNode*) symbol);
  145. }
  146. /*
  147. Search for a key in the define table.
  148. Return the corresponding ASMDefine on success and NULL on failure.
  149. */
  150. const ASMDefine* asm_deftable_find(
  151. const ASMDefineTable *tab, const char *key, size_t size)
  152. {
  153. return (ASMDefine*) hash_table_find(tab, key, size);
  154. }
  155. /*
  156. Insert an ASMDefine into the define table.
  157. This doesn't check for duplicate keys, so you must do that beforehand.
  158. */
  159. void asm_deftable_insert(ASMDefineTable *tab, ASMDefine *define)
  160. {
  161. hash_table_insert(tab, (HashNode*) define);
  162. }
  163. /*
  164. Remove an ASMDefine from the define table.
  165. Return true if the node was removed, or false if it was not found.
  166. */
  167. bool asm_deftable_remove(
  168. ASMDefineTable *tab, const char *key, size_t size)
  169. {
  170. return hash_table_remove(tab, key, size);
  171. }
  172. /*
  173. @TRACE_LEVEL
  174. Print out an ASMLine list to stdout.
  175. */
  176. void asm_lines_print(const ASMLine *line)
  177. {
  178. TRACE("Dumping ASMLines:")
  179. while (line) {
  180. TRACE("- %-40.*s [%s:%02zu]", (int) line->length, line->data,
  181. line->filename, line->original->lineno)
  182. line = line->next;
  183. }
  184. }