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.
 
 
 
 
 

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