Files
gb-emu/src/gbasm/assemble.c
Max Regan 6e2f4096a2 gb-emu: initial commit
Add a mostly non-functional Gameboy CPU and the skeleton
of a Gameboy assembler intended for unit tests.
2017-05-10 22:40:12 -07:00

40 lines
845 B
C

#include <stdlib.h>
#include "gbasm/assemble.h"
#include "gbasm/parser.h"
#include "gbasm/types.h"
#include "gbasm/errors.h"
#include "gbasm/opcodes.h"
#include "common.h"
#define GBASM_MAX_INSTS 1024
struct gb_asm_prog *prog;
int gbasm_assemble(char *program, struct emitter *emitter)
{
struct gbasm_parsed_inst insts[GBASM_MAX_INSTS];
int num_insts;
num_insts = gbasm_parse_buffer(program, insts, ARRAY_SIZE(insts));
if (num_insts < 0) {
return num_insts;
}
DEBUG_LOG("parsed %d instructions\n", num_insts);
for (int i = 0; i < num_insts; i++) {
DEBUG_LOG("emitting %s\n", insts[i].opcode);
const struct gbasm_op_info *info = gbasm_get_opcode_info(insts[i].opcode);
if (info == NULL) {
gbasm_unknown_opcode_error(&insts[i]);
}
info->check(&insts[i], info);
info->emit(emitter, &insts[i]);
}
return 0;
}