An emulator, assembler, and disassembler for the Sega Game Gear
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

335 řádky
9.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 "assembler.h"
  5. #include "assembler/directives.h"
  6. #include "assembler/errors.h"
  7. #include "assembler/io.h"
  8. #include "assembler/parse_util.h"
  9. #include "assembler/preprocessor.h"
  10. #include "assembler/state.h"
  11. #include "logging.h"
  12. #include "rom.h"
  13. #include "util.h"
  14. #define IS_LABEL(line) (line->data[line->length - 1] == ':')
  15. /* Sentinel values for overlap table */
  16. const ASMLine header_sentinel, bounds_sentinel;
  17. /*
  18. Add a given line, representing a label, to the symbol table.
  19. Return NULL on success and an ErrorInfo object on failure (in the case of
  20. duplicate labels).
  21. */
  22. static ErrorInfo* add_label_to_table(
  23. ASMSymbolTable *symtable, const ASMLine *line, size_t offset)
  24. {
  25. char *symbol = strndup(line->data, line->length - 1);
  26. if (!symbol)
  27. OUT_OF_MEMORY()
  28. const ASMSymbol *current = asm_symtable_find(symtable, symbol);
  29. if (current) {
  30. ErrorInfo *ei = error_info_create(line, ET_LAYOUT, ED_LYT_DUPE_LABELS);
  31. error_info_append(ei, current->line);
  32. return ei;
  33. }
  34. ASMSymbol *label = malloc(sizeof(ASMSymbol));
  35. if (!label)
  36. OUT_OF_MEMORY()
  37. label->offset = offset;
  38. label->symbol = symbol;
  39. label->line = line;
  40. asm_symtable_insert(symtable, label);
  41. return NULL;
  42. }
  43. /*
  44. Parse data encoded in a line into an ASMData object.
  45. On success, return NULL and store the instruction in *data_ptr. On failure,
  46. return an ErrorInfo object; *data_ptr is not modified.
  47. */
  48. static ErrorInfo* parse_data(
  49. const ASMLine *line, ASMData **data_ptr, size_t offset)
  50. {
  51. // TODO
  52. return error_info_create(line, ET_PARSER, ED_PARSE_SYNTAX);
  53. }
  54. /*
  55. Parse an instruction encoded in a line into an ASMInstruction object.
  56. On success, return NULL and store the instruction in *inst_ptr. On failure,
  57. return an ErrorInfo object; *inst_ptr is not modified.
  58. */
  59. static ErrorInfo* parse_instruction(
  60. const ASMLine *line, ASMInstruction **inst_ptr, size_t offset)
  61. {
  62. // TODO
  63. return error_info_create(line, ET_PARSER, ED_PARSE_SYNTAX);
  64. }
  65. /*
  66. Check if the given location overlaps with any existing objects.
  67. On success, return NULL and add the location to the overlap table.
  68. On failure, return an ErrorInfo object.
  69. */
  70. static ErrorInfo* check_layout(
  71. const ASMLine **overlap_table, size_t size, const ASMLocation *loc,
  72. const ASMLine *line, const ASMLine *origin)
  73. {
  74. const ASMLine *clash = NULL;
  75. if (loc->offset + loc->length >= size) {
  76. clash = &bounds_sentinel;
  77. } else {
  78. for (size_t i = 0; i < loc->length; i++) {
  79. if (overlap_table[loc->offset + i]) {
  80. clash = overlap_table[loc->offset + i];
  81. break;
  82. }
  83. }
  84. }
  85. if (clash) {
  86. ErrorInfo *ei = error_info_create(line, ET_LAYOUT,
  87. (clash == &header_sentinel) ? ED_LYT_OVERLAP_HEAD :
  88. (clash == &bounds_sentinel) ? ED_LYT_BOUNDS : ED_LYT_OVERLAP);
  89. if (origin)
  90. error_info_append(ei, origin);
  91. if (clash != &header_sentinel && clash != &bounds_sentinel)
  92. error_info_append(ei, clash);
  93. return ei;
  94. }
  95. for (size_t i = 0; i < loc->length; i++)
  96. overlap_table[loc->offset + i] = line;
  97. return NULL;
  98. }
  99. /*
  100. Tokenize ASMLines into ASMInstructions.
  101. NULL is returned on success and an ErrorInfo object is returned on failure.
  102. state->instructions, state->data, and state->symtable may or may not be
  103. modified regardless of success.
  104. */
  105. static ErrorInfo* tokenize(AssemblerState *state)
  106. {
  107. size_t size = state->rom_size ? state->rom_size : ROM_SIZE_MAX;
  108. const ASMLine **overlap_table = calloc(size, sizeof(const ASMLine*));
  109. if (!overlap_table)
  110. OUT_OF_MEMORY()
  111. ErrorInfo *ei = NULL;
  112. ASMInstruction dummy_inst = {.next = NULL}, *inst, *prev_inst = &dummy_inst;
  113. ASMData dummy_data = {.next = NULL}, *data, *prev_data = &dummy_data;
  114. const ASMLine *line = state->lines, *origin = NULL;
  115. size_t offset = 0;
  116. for (size_t i = 0; i < HEADER_SIZE; i++)
  117. overlap_table[state->header.offset + i] = &header_sentinel;
  118. while (line) {
  119. if (IS_LABEL(line)) {
  120. if ((ei = add_label_to_table(state->symtable, line, offset)))
  121. goto cleanup;
  122. }
  123. else if (IS_LOCAL_DIRECTIVE(line)) {
  124. if (IS_DIRECTIVE(line, DIR_ORIGIN)) {
  125. if (!DIRECTIVE_HAS_ARG(line, DIR_ORIGIN)) {
  126. ei = error_info_create(line, ET_PREPROC, ED_PP_NO_ARG);
  127. goto cleanup;
  128. }
  129. uint32_t arg;
  130. if (!dparse_uint32_t(&arg, line, DIR_ORIGIN)) {
  131. ei = error_info_create(line, ET_PREPROC, ED_PP_BAD_ARG);
  132. goto cleanup;
  133. }
  134. offset = arg;
  135. origin = line;
  136. }
  137. else {
  138. if ((ei = parse_data(line, &data, offset)))
  139. goto cleanup;
  140. offset += data->loc.length;
  141. prev_data->next = data;
  142. prev_data = data;
  143. if ((ei = check_layout(overlap_table, size, &data->loc, line, origin)))
  144. goto cleanup;
  145. }
  146. }
  147. else {
  148. if ((ei = parse_instruction(line, &inst, offset)))
  149. goto cleanup;
  150. offset += inst->loc.length;
  151. prev_inst->next = inst;
  152. prev_inst = inst;
  153. if ((ei = check_layout(overlap_table, size, &inst->loc, line, origin)))
  154. goto cleanup;
  155. }
  156. line = line->next;
  157. }
  158. cleanup:
  159. state->instructions = dummy_inst.next;
  160. state->data = dummy_data.next;
  161. free(overlap_table);
  162. return ei;
  163. }
  164. /*
  165. Resolve default placeholder values in assembler state, such as ROM size.
  166. On success, no new heap objects are allocated. On error, an ErrorInfo
  167. object is returned.
  168. */
  169. static ErrorInfo* resolve_defaults(AssemblerState *state)
  170. {
  171. if (!state->rom_size) {
  172. state->rom_size = ROM_SIZE_MIN;
  173. // TODO: use highest instruction too
  174. if (state->header.rom_size != INVALID_SIZE_CODE) {
  175. size_t decl_size = size_code_to_bytes(state->header.rom_size);
  176. if (decl_size > state->rom_size)
  177. state->rom_size = decl_size;
  178. }
  179. }
  180. if (state->header.rom_size == INVALID_SIZE_CODE)
  181. state->header.rom_size = size_bytes_to_code(state->rom_size);
  182. return NULL;
  183. }
  184. /*
  185. Resolve symbol placeholders in instructions such as jumps and branches.
  186. On success, no new heap objects are allocated. On error, an ErrorInfo
  187. object is returned.
  188. */
  189. static ErrorInfo* resolve_symbols(AssemblerState *state)
  190. {
  191. // TODO
  192. (void) state;
  193. return NULL;
  194. }
  195. /*
  196. Convert finalized ASMInstructions and ASMData into a binary data block.
  197. This function should never fail.
  198. */
  199. static void serialize_binary(AssemblerState *state, uint8_t *binary)
  200. {
  201. // TODO
  202. for (size_t i = 0; i < state->rom_size; i++)
  203. binary[i] = 'X';
  204. }
  205. /*
  206. Assemble the z80 source code in the source code buffer into binary data.
  207. If successful, return the size of the assembled binary data and change
  208. *binary_ptr to point to the assembled ROM data buffer. *binary_ptr must be
  209. free()'d when finished.
  210. If an error occurred, return 0 and update *ei_ptr to point to an ErrorInfo
  211. object which can be shown to the user with error_info_print(). The
  212. ErrorInfo object must be destroyed with error_info_destroy() when finished.
  213. In either case, only one of *binary_ptr and *ei_ptr is modified.
  214. */
  215. size_t assemble(const LineBuffer *source, uint8_t **binary_ptr, ErrorInfo **ei_ptr)
  216. {
  217. AssemblerState state;
  218. ErrorInfo *error_info;
  219. size_t retval = 0;
  220. state_init(&state);
  221. if ((error_info = preprocess(&state, source)))
  222. goto error;
  223. asm_symtable_init(&state.symtable);
  224. #ifdef DEBUG_MODE
  225. asm_lines_print(state.lines);
  226. #endif
  227. if ((error_info = tokenize(&state)))
  228. goto error;
  229. if ((error_info = resolve_defaults(&state)))
  230. goto error;
  231. if ((error_info = resolve_symbols(&state)))
  232. goto error;
  233. uint8_t *binary = malloc(sizeof(uint8_t) * state.rom_size);
  234. if (!binary)
  235. OUT_OF_MEMORY()
  236. serialize_binary(&state, binary);
  237. *binary_ptr = binary;
  238. retval = state.rom_size;
  239. goto cleanup;
  240. error:
  241. *ei_ptr = error_info;
  242. cleanup:
  243. state_free(&state);
  244. return retval;
  245. }
  246. /*
  247. Assemble the z80 source code at the input path into a binary file.
  248. Return true if the operation was a success and false if it was a failure.
  249. Errors are printed to STDOUT; if the operation was successful then nothing
  250. is printed.
  251. */
  252. bool assemble_file(const char *src_path, const char *dst_path)
  253. {
  254. LineBuffer *source = read_source_file(src_path, true);
  255. if (!source)
  256. return false;
  257. uint8_t *binary;
  258. ErrorInfo *error_info;
  259. size_t size = assemble(source, &binary, &error_info);
  260. line_buffer_free(source);
  261. if (!size) {
  262. error_info_print(error_info, stderr);
  263. error_info_destroy(error_info);
  264. return false;
  265. }
  266. bool success = write_binary_file(dst_path, binary, size);
  267. free(binary);
  268. return success;
  269. }