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.
 
 
 
 
 

116 lines
2.6 KiB

  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. #pragma once
  4. #include <stdbool.h>
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "hash_table.h"
  8. #include "inst_args.h"
  9. #include "../assembler.h"
  10. #define DEFAULT_HEADER_OFFSET 0x7FF0
  11. #define DEFAULT_REGION 6 // GG Export
  12. #define DEFAULT_DECLSIZE 0xC // 32 KB
  13. /* Structs */
  14. struct ASMLine {
  15. char *data;
  16. size_t length;
  17. const Line *original;
  18. const char *filename;
  19. bool is_label;
  20. struct ASMLine *next;
  21. };
  22. typedef struct ASMLine ASMLine;
  23. struct ASMInclude {
  24. LineBuffer *lines;
  25. struct ASMInclude *next;
  26. };
  27. typedef struct ASMInclude ASMInclude;
  28. typedef struct {
  29. size_t offset;
  30. size_t length;
  31. } ASMLocation;
  32. struct ASMInstruction {
  33. ASMLocation loc;
  34. uint8_t *bytes;
  35. char *symbol;
  36. const ASMLine *line;
  37. struct ASMInstruction *next;
  38. };
  39. typedef struct ASMInstruction ASMInstruction;
  40. struct ASMData {
  41. ASMLocation loc;
  42. uint8_t *bytes;
  43. struct ASMData *next;
  44. };
  45. typedef struct ASMData ASMData;
  46. struct ASMSymbol {
  47. uint16_t offset;
  48. char *symbol;
  49. const ASMLine *line;
  50. struct ASMSymbol *next;
  51. };
  52. typedef struct ASMSymbol ASMSymbol;
  53. struct ASMDefine {
  54. ASMArgImmediate value;
  55. char *name;
  56. const ASMLine *line;
  57. struct ASMDefine *next;
  58. };
  59. typedef struct ASMDefine ASMDefine;
  60. typedef HashTable ASMSymbolTable;
  61. typedef HashTable ASMDefineTable;
  62. typedef struct {
  63. size_t offset;
  64. bool checksum;
  65. uint32_t product_code;
  66. uint8_t version;
  67. uint8_t region;
  68. uint8_t rom_size;
  69. } ASMHeaderInfo;
  70. typedef struct {
  71. ASMHeaderInfo header;
  72. bool cross_blocks;
  73. size_t rom_size;
  74. ASMLine *lines;
  75. ASMInclude *includes;
  76. ASMInstruction *instructions;
  77. ASMData *data;
  78. ASMSymbolTable *symtable;
  79. } AssemblerState;
  80. /* Functions */
  81. void state_init(AssemblerState*);
  82. void state_free(AssemblerState*);
  83. void asm_symtable_init(ASMSymbolTable**);
  84. ASMDefineTable* asm_deftable_new();
  85. void asm_lines_free(ASMLine*);
  86. void asm_includes_free(ASMInclude*);
  87. void asm_instructions_free(ASMInstruction*);
  88. void asm_data_free(ASMData*);
  89. void asm_symtable_free(ASMSymbolTable*);
  90. void asm_deftable_free(ASMDefineTable*);
  91. const ASMSymbol* asm_symtable_find(const ASMSymbolTable*, const char*);
  92. void asm_symtable_insert(ASMSymbolTable*, ASMSymbol*);
  93. const ASMDefine* asm_deftable_find(const ASMDefineTable*, const char*, size_t);
  94. void asm_deftable_insert(ASMDefineTable*, ASMDefine*);
  95. bool asm_deftable_remove(ASMDefineTable*, const char*, size_t);
  96. void asm_lines_print(const ASMLine*);