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.
 
 
 
 
 

108 lines
2.3 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. #include "../util.h"
  8. #define DEFAULT_HEADER_OFFSET 0x7FF0
  9. #define DEFAULT_REGION "GG Export"
  10. /*
  11. Initialize default values in an AssemblerState object.
  12. */
  13. void state_init(AssemblerState *state)
  14. {
  15. state->header.offset = DEFAULT_HEADER_OFFSET;
  16. state->header.checksum = true;
  17. state->header.product_code = 0;
  18. state->header.version = 0;
  19. state->header.region = region_string_to_code(DEFAULT_REGION);
  20. state->header.rom_size = 0;
  21. state->optimizer = false;
  22. state->rom_size = 0;
  23. state->lines = NULL;
  24. state->includes = NULL;
  25. state->instructions = NULL;
  26. state->symtable = NULL;
  27. }
  28. /*
  29. Initialize an ASMSymbolTable and place it in *symtable_ptr.
  30. */
  31. void asm_symtable_init(ASMSymbolTable **symtable_ptr)
  32. {
  33. ASMSymbolTable *symtable;
  34. if (!(symtable = malloc(sizeof(ASMSymbolTable))))
  35. OUT_OF_MEMORY()
  36. for (size_t bucket = 0; bucket < SYMBOL_TABLE_BUCKETS; bucket++)
  37. symtable->buckets[bucket] = NULL;
  38. *symtable_ptr = symtable;
  39. }
  40. /*
  41. Deallocate an ASMLine list.
  42. */
  43. void asm_lines_free(ASMLine *line)
  44. {
  45. while (line) {
  46. ASMLine *temp = line->next;
  47. free(line->data);
  48. free(line);
  49. line = temp;
  50. }
  51. }
  52. /*
  53. Deallocate an ASMInclude list.
  54. */
  55. void asm_includes_free(ASMInclude *include)
  56. {
  57. while (include) {
  58. ASMInclude *temp = include->next;
  59. line_buffer_free(include->lines);
  60. free(include);
  61. include = temp;
  62. }
  63. }
  64. /*
  65. Deallocate an ASMInstruction list.
  66. */
  67. void asm_instructions_free(ASMInstruction *inst)
  68. {
  69. while (inst) {
  70. ASMInstruction *temp = inst->next;
  71. if (inst->symbol)
  72. free(inst->symbol);
  73. free(inst);
  74. inst = temp;
  75. }
  76. }
  77. /*
  78. Deallocate an ASMSymbolTable.
  79. */
  80. void asm_symtable_free(ASMSymbolTable *symtable)
  81. {
  82. if (!symtable)
  83. return;
  84. for (size_t bucket = 0; bucket < SYMBOL_TABLE_BUCKETS; bucket++) {
  85. ASMSymbol *sym = symtable->buckets[bucket], *temp;
  86. while (sym) {
  87. temp = sym->next;
  88. free(sym->symbol);
  89. free(sym);
  90. sym = temp;
  91. }
  92. }
  93. free(symtable);
  94. }