An emulator, assembler, and disassembler for the Sega Game Gear
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

200 lines
4.2 KiB

  1. /* Copyright (C) 2014-2015 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. Initialize an ASMSymbolTable and place it in *symtable_ptr.
  39. */
  40. void asm_symtable_init(ASMSymbolTable **symtable_ptr)
  41. {
  42. *symtable_ptr = hash_table_NEW(ASMSymbol, symbol, next);
  43. }
  44. /*
  45. Create and return a new ASMDefineTable.
  46. */
  47. ASMDefineTable* asm_deftable_new()
  48. {
  49. return hash_table_NEW(ASMDefine, name, next);
  50. }
  51. /*
  52. Deallocate an ASMLine list.
  53. */
  54. void asm_lines_free(ASMLine *line)
  55. {
  56. while (line) {
  57. ASMLine *temp = line->next;
  58. free(line->data);
  59. free(line);
  60. line = temp;
  61. }
  62. }
  63. /*
  64. Deallocate an ASMInclude list.
  65. */
  66. void asm_includes_free(ASMInclude *include)
  67. {
  68. while (include) {
  69. ASMInclude *temp = include->next;
  70. line_buffer_free(include->lines);
  71. free(include);
  72. include = temp;
  73. }
  74. }
  75. /*
  76. Deallocate an ASMInstruction list.
  77. */
  78. void asm_instructions_free(ASMInstruction *inst)
  79. {
  80. while (inst) {
  81. ASMInstruction *temp = inst->next;
  82. free(inst->bytes);
  83. free(inst->symbol);
  84. free(inst);
  85. inst = temp;
  86. }
  87. }
  88. /*
  89. Deallocate an ASMData list.
  90. */
  91. void asm_data_free(ASMData *data)
  92. {
  93. while (data) {
  94. ASMData *temp = data->next;
  95. free(data->bytes);
  96. free(data);
  97. data = temp;
  98. }
  99. }
  100. /*
  101. Callback function for freeing an ASMSymbol.
  102. */
  103. static void free_asm_symbol(HashNode *node)
  104. {
  105. free(((ASMSymbol*) node)->symbol);
  106. free(node);
  107. }
  108. /*
  109. Deallocate an ASMSymbolTable.
  110. */
  111. void asm_symtable_free(ASMSymbolTable *symtable)
  112. {
  113. hash_table_free(symtable, free_asm_symbol);
  114. }
  115. /*
  116. Callback function for freeing an ASMDefine.
  117. */
  118. static void free_asm_define(HashNode *node)
  119. {
  120. free(((ASMDefine*) node)->name);
  121. free(node);
  122. }
  123. /*
  124. Deallocate an ASMDefineTable.
  125. */
  126. void asm_deftable_free(ASMDefineTable *deftable)
  127. {
  128. hash_table_free(deftable, free_asm_define);
  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);
  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(const ASMDefineTable *tab, const char *key)
  151. {
  152. return (ASMDefine*) hash_table_find(tab, key);
  153. }
  154. /*
  155. Insert an ASMDefine into the define table.
  156. This doesn't check for duplicate keys, so you must do that beforehand.
  157. */
  158. void asm_deftable_insert(ASMDefineTable *tab, ASMDefine *define)
  159. {
  160. hash_table_insert(tab, (HashNode*) define);
  161. }
  162. #ifdef DEBUG_MODE
  163. /*
  164. DEBUG FUNCTION: Print out an ASMLine list to stdout.
  165. */
  166. void asm_lines_print(const ASMLine *line)
  167. {
  168. DEBUG("Dumping ASMLines:")
  169. while (line) {
  170. DEBUG("- %-40.*s [%s:%02zu]", (int) line->length, line->data,
  171. line->filename, line->original->lineno)
  172. line = line->next;
  173. }
  174. }
  175. #endif