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.
 
 
 
 
 

107 lines
2.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. #pragma once
  4. #include <stdbool.h>
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "../assembler.h"
  8. #define DEFAULT_HEADER_OFFSET 0x7FF0
  9. #define DEFAULT_REGION 6 // GG Export
  10. #define DEFAULT_DECLSIZE 0xC // 32 KB
  11. #define MAX_SYMBOL_SIZE 256
  12. #define SYMBOL_TABLE_BUCKETS 128
  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. typedef struct {
  54. ASMSymbol *buckets[SYMBOL_TABLE_BUCKETS];
  55. } ASMSymbolTable;
  56. typedef struct {
  57. size_t offset;
  58. bool checksum;
  59. uint32_t product_code;
  60. uint8_t version;
  61. uint8_t region;
  62. uint8_t rom_size;
  63. } ASMHeaderInfo;
  64. typedef struct {
  65. ASMHeaderInfo header;
  66. bool cross_blocks;
  67. size_t rom_size;
  68. ASMLine *lines;
  69. ASMInclude *includes;
  70. ASMInstruction *instructions;
  71. ASMData *data;
  72. ASMSymbolTable *symtable;
  73. } AssemblerState;
  74. /* Functions */
  75. void state_init(AssemblerState*);
  76. void state_free(AssemblerState*);
  77. void asm_symtable_init(ASMSymbolTable**);
  78. void asm_lines_free(ASMLine*);
  79. void asm_includes_free(ASMInclude*);
  80. void asm_instructions_free(ASMInstruction*);
  81. void asm_data_free(ASMData*);
  82. void asm_symtable_free(ASMSymbolTable*);
  83. const ASMSymbol* asm_symtable_find(const ASMSymbolTable*, const char*);
  84. void asm_symtable_insert(ASMSymbolTable*, ASMSymbol*);
  85. #ifdef DEBUG_MODE
  86. void asm_lines_print(const ASMLine*);
  87. #endif