An emulator, assembler, and disassembler for the Sega Game Gear
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

239 wiersze
6.5 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. /*
  16. Parse an instruction encoded in line into an ASMInstruction object.
  17. On success, return NULL and store the instruction in *inst_ptr. On failure,
  18. return an ErrorInfo object; *inst_ptr is not modified.
  19. */
  20. static ErrorInfo* parse_instruction(
  21. const ASMLine *line, ASMInstruction **inst_ptr, size_t offset)
  22. {
  23. // TODO
  24. return error_info_create(line, ET_PARSER, ED_PARSE_SYNTAX);
  25. }
  26. /*
  27. Tokenize ASMLines into ASMInstructions.
  28. NULL is returned on success and an ErrorInfo object is returned on failure.
  29. state->instructions, state->data, and state->symtable may or may not be
  30. modified regardless of success.
  31. */
  32. static ErrorInfo* tokenize(AssemblerState *state)
  33. {
  34. size_t size = state->rom_size ? state->rom_size : ROM_SIZE_MAX;
  35. const ASMLine **overlap_table = calloc(size, sizeof(const ASMLine*));
  36. if (!overlap_table)
  37. OUT_OF_MEMORY()
  38. // TODO: fill overlap table for header with pointers to a dummy object
  39. ErrorInfo *ei = NULL;
  40. ASMInstruction dummy = {.next = NULL}, *inst, *prev = &dummy;
  41. const ASMLine *line = state->lines, *origin = NULL;
  42. size_t offset = 0;
  43. while (line) {
  44. if (IS_LOCAL_DIRECTIVE(line)) {
  45. if (IS_DIRECTIVE(line, DIR_ORIGIN)) {
  46. if (!DIRECTIVE_HAS_ARG(line, DIR_ORIGIN)) {
  47. ei = error_info_create(line, ET_PREPROC, ED_PP_NO_ARG);
  48. goto cleanup;
  49. }
  50. uint32_t arg;
  51. if (!parse_uint32_t(&arg, line, DIR_ORIGIN)) {
  52. ei = error_info_create(line, ET_PREPROC, ED_PP_BAD_ARG);
  53. goto cleanup;
  54. }
  55. offset = arg;
  56. origin = line;
  57. }
  58. else {
  59. // TODO
  60. ei = error_info_create(line, ET_PREPROC, ED_PP_UNKNOWN);
  61. goto cleanup;
  62. }
  63. }
  64. else if (IS_LABEL(line)) {
  65. // TODO: add to symbol table
  66. }
  67. else {
  68. if ((ei = parse_instruction(line, &inst, offset)))
  69. goto cleanup;
  70. // TODO: bounded check on range [offset, offset + inst->length) against overlap table
  71. // if clash, use error with current line,
  72. // then table line (if not header),
  73. // then origin line (if non-null)
  74. offset += inst->length;
  75. prev->next = inst;
  76. prev = inst;
  77. }
  78. line = line->next;
  79. }
  80. cleanup:
  81. state->instructions = dummy.next;
  82. free(overlap_table);
  83. return ei;
  84. }
  85. /*
  86. Resolve default placeholder values in assembler state, such as ROM size.
  87. On success, no new heap objects are allocated. On error, an ErrorInfo
  88. object is returned.
  89. */
  90. static ErrorInfo* resolve_defaults(AssemblerState *state)
  91. {
  92. if (!state->rom_size) {
  93. state->rom_size = ROM_SIZE_MIN;
  94. // TODO: use highest instruction too
  95. if (state->header.rom_size != INVALID_SIZE_CODE) {
  96. size_t decl_size = size_code_to_bytes(state->header.rom_size);
  97. if (decl_size > state->rom_size)
  98. state->rom_size = decl_size;
  99. }
  100. }
  101. if (state->header.rom_size == INVALID_SIZE_CODE)
  102. state->header.rom_size = size_bytes_to_code(state->rom_size);
  103. return NULL;
  104. }
  105. /*
  106. Resolve symbol placeholders in instructions such as jumps and branches.
  107. On success, no new heap objects are allocated. On error, an ErrorInfo
  108. object is returned.
  109. */
  110. static ErrorInfo* resolve_symbols(AssemblerState *state)
  111. {
  112. // TODO
  113. (void) state;
  114. return NULL;
  115. }
  116. /*
  117. Convert finalized ASMInstructions and ASMData into a binary data block.
  118. This function should never fail.
  119. */
  120. static void serialize_binary(AssemblerState *state, uint8_t *binary)
  121. {
  122. // TODO
  123. for (size_t i = 0; i < state->rom_size; i++)
  124. binary[i] = 'X';
  125. }
  126. /*
  127. Assemble the z80 source code in the source code buffer into binary data.
  128. If successful, return the size of the assembled binary data and change
  129. *binary_ptr to point to the assembled ROM data buffer. *binary_ptr must be
  130. free()'d when finished.
  131. If an error occurred, return 0 and update *ei_ptr to point to an ErrorInfo
  132. object which can be shown to the user with error_info_print(). The
  133. ErrorInfo object must be destroyed with error_info_destroy() when finished.
  134. In either case, only one of *binary_ptr and *ei_ptr is modified.
  135. */
  136. size_t assemble(const LineBuffer *source, uint8_t **binary_ptr, ErrorInfo **ei_ptr)
  137. {
  138. AssemblerState state;
  139. ErrorInfo *error_info;
  140. size_t retval = 0;
  141. state_init(&state);
  142. if ((error_info = preprocess(&state, source)))
  143. goto error;
  144. asm_symtable_init(&state.symtable);
  145. #ifdef DEBUG_MODE
  146. asm_lines_print(state.lines);
  147. #endif
  148. if ((error_info = tokenize(&state)))
  149. goto error;
  150. if ((error_info = resolve_defaults(&state)))
  151. goto error;
  152. if ((error_info = resolve_symbols(&state)))
  153. goto error;
  154. uint8_t *binary = malloc(sizeof(uint8_t) * state.rom_size);
  155. if (!binary)
  156. OUT_OF_MEMORY()
  157. serialize_binary(&state, binary);
  158. *binary_ptr = binary;
  159. retval = state.rom_size;
  160. goto cleanup;
  161. error:
  162. *ei_ptr = error_info;
  163. cleanup:
  164. state_free(&state);
  165. return retval;
  166. }
  167. /*
  168. Assemble the z80 source code at the input path into a binary file.
  169. Return true if the operation was a success and false if it was a failure.
  170. Errors are printed to STDOUT; if the operation was successful then nothing
  171. is printed.
  172. */
  173. bool assemble_file(const char *src_path, const char *dst_path)
  174. {
  175. LineBuffer *source = read_source_file(src_path, true);
  176. if (!source)
  177. return false;
  178. uint8_t *binary;
  179. ErrorInfo *error_info;
  180. size_t size = assemble(source, &binary, &error_info);
  181. line_buffer_free(source);
  182. if (!size) {
  183. error_info_print(error_info, stderr);
  184. error_info_destroy(error_info);
  185. return false;
  186. }
  187. bool success = write_binary_file(dst_path, binary, size);
  188. free(binary);
  189. return success;
  190. }