diff --git a/src/Makefile b/src/Makefile index c599e38..f6797f4 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ #CLI=rk65c02cli #CLI_OBJS=rk65c02cli.o -LIB_OBJS=rk65c02.o bus.o instruction.o emulation.o debug.o device_ram.o device_fb.o device_serial.o log.o +LIB_OBJS=rk65c02.o bus.o instruction.o emulation.o debug.o device_ram.o device_fb.o device_serial.o log.o assembler.o LIB_SO=librk65c02.so LIB_STATIC=librk65c02.a diff --git a/src/assembler.c b/src/assembler.c new file mode 100644 index 0000000..d3e4e81 --- /dev/null +++ b/src/assembler.c @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include "bus.h" +#include "rk65c02.h" +#include "log.h" +#include "assembler.h" +#include "instruction.h" + +assembler_t +assemble_init(bus_t *b, uint16_t pc) +{ + assembler_t asmblr; + + asmblr.bus = b; + asmblr.pc = pc; + + return asmblr; +} + +bool +assemble_single_implied(assembler_t *a, const char *mnemonic) +{ + return assemble_single(a, mnemonic, IMPLIED, 0, 0); +} + +bool +assemble_single(assembler_t *a, const char *mnemonic, addressing_t mode, uint8_t op1, uint8_t op2) +{ + uint8_t *asmbuf; + uint8_t bsize; + bool rv; + + rv = assemble_single_buf(&asmbuf, &bsize, mnemonic, mode, op1, op2); + if (rv == false) + return rv; + + rv = bus_load_buf(a->bus, a->pc, asmbuf, bsize); + a->pc += bsize; + + return rv; +} + +bool +assemble_single_buf_implied(uint8_t **buf, uint8_t *bsize, const char *mnemonic) +{ + return assemble_single_buf(buf, bsize, mnemonic, IMPLIED, 0, 0); +} + + +bool +assemble_single_buf(uint8_t **buf, uint8_t *bsize, const char *mnemonic, addressing_t mode, uint8_t op1, uint8_t op2) +{ + instrdef_t id; + uint8_t opcode; + bool found; + + found = false; + opcode = 0; + + /* find the opcode for given mnemonic and addressing mode */ + while (opcode <= 0xFF) { /* this is stupid */ + id = instruction_decode(opcode); + if ((strcmp(mnemonic, id.mnemonic) == 0) && (id.mode == mode)) { + found = true; + break; + } + opcode++; + } + + if (!found) { + rk65c02_log(LOG_ERROR, + "Couldn't find opcode for mnemonic %s mode %x.", + mnemonic, mode); + return false; + } + + *bsize = id.size; + *buf = GC_MALLOC(id.size); + if(*buf == NULL) { + rk65c02_log(LOG_ERROR, "Error allocating assembly buffer."); + return false; + } + + /* fill the buffer */ + memset(*buf, 0, id.size); + (*buf)[0] = opcode; + /* XXX */ + if (id.size > 1) + (*buf)[1] = op1; + if (id.size > 2) + (*buf)[2] = op2; + + return found; +} + diff --git a/src/assembler.h b/src/assembler.h new file mode 100644 index 0000000..e12d426 --- /dev/null +++ b/src/assembler.h @@ -0,0 +1,22 @@ +#ifndef _ASSEMBLER_H_ +#define _ASSEMBLER_H_ + +#include "instruction.h" +#include "rk65c02.h" + +struct assembler { + bus_t *bus; + uint16_t pc; +}; + +typedef struct assembler assembler_t; + +bool assemble_single_buf_implied(uint8_t **, uint8_t *, const char *); +bool assemble_single_buf(uint8_t **, uint8_t *, const char *, addressing_t, uint8_t, uint8_t); + +assembler_t assemble_init(bus_t *, uint16_t); +bool assemble_single(assembler_t *, const char *, addressing_t, uint8_t, uint8_t); +bool assemble_single_implied(assembler_t *, const char *); + +#endif /* _ASSEMBLER_H_ */ + diff --git a/src/instruction.c b/src/instruction.c index ba9ba6b..d52f7d7 100644 --- a/src/instruction.c +++ b/src/instruction.c @@ -135,93 +135,6 @@ instruction_string_get(instruction_t *i) return str; } -assembler_t -assemble_init(bus_t *b, uint16_t pc) -{ - assembler_t asmblr; - - asmblr.bus = b; - asmblr.pc = pc; - - return asmblr; -} - -bool -assemble_single_implied(assembler_t *a, const char *mnemonic) -{ - return assemble_single(a, mnemonic, IMPLIED, 0, 0); -} - -bool -assemble_single(assembler_t *a, const char *mnemonic, addressing_t mode, uint8_t op1, uint8_t op2) -{ - uint8_t *asmbuf; - uint8_t bsize; - bool rv; - - rv = assemble_single_buf(&asmbuf, &bsize, mnemonic, mode, op1, op2); - if (rv == false) - return rv; - - rv = bus_load_buf(a->bus, a->pc, asmbuf, bsize); - a->pc += bsize; - - return rv; -} - -bool -assemble_single_buf_implied(uint8_t **buf, uint8_t *bsize, const char *mnemonic) -{ - return assemble_single_buf(buf, bsize, mnemonic, IMPLIED, 0, 0); -} - - -bool -assemble_single_buf(uint8_t **buf, uint8_t *bsize, const char *mnemonic, addressing_t mode, uint8_t op1, uint8_t op2) -{ - instrdef_t id; - uint8_t opcode; - bool found; - - found = false; - opcode = 0; - - /* find the opcode for given mnemonic and addressing mode */ - while (opcode <= 0xFF) { /* this is stupid */ - id = instruction_decode(opcode); - if ((strcmp(mnemonic, id.mnemonic) == 0) && (id.mode == mode)) { - found = true; - break; - } - opcode++; - } - - if (!found) { - rk65c02_log(LOG_ERROR, - "Couldn't find opcode for mnemonic %s mode %x.", - mnemonic, mode); - return false; - } - - *bsize = id.size; - *buf = GC_MALLOC(id.size); - if(*buf == NULL) { - rk65c02_log(LOG_ERROR, "Error allocating assembly buffer."); - return false; - } - - /* fill the buffer */ - memset(*buf, 0, id.size); - (*buf)[0] = opcode; - /* XXX */ - if (id.size > 1) - (*buf)[1] = op1; - if (id.size > 2) - (*buf)[2] = op2; - - return found; -} - void disassemble(bus_t *b, uint16_t addr) { diff --git a/src/instruction.h b/src/instruction.h index 6504116..8b358ba 100644 --- a/src/instruction.h +++ b/src/instruction.h @@ -41,13 +41,6 @@ struct instrdef { typedef struct instrdef instrdef_t; -struct assembler { - bus_t *bus; - uint16_t pc; -}; - -typedef struct assembler assembler_t; - instruction_t instruction_fetch(bus_t *, uint16_t); instrdef_t instruction_decode(uint8_t); void instruction_print(instruction_t *); @@ -64,11 +57,4 @@ void program_counter_increment(rk65c02emu_t *, instrdef_t *); bool instruction_modify_pc(instrdef_t *); void program_counter_branch(rk65c02emu_t *, int8_t); -bool assemble_single_buf_implied(uint8_t **, uint8_t *, const char *); -bool assemble_single_buf(uint8_t **, uint8_t *, const char *, addressing_t, uint8_t, uint8_t); - -assembler_t assemble_init(bus_t *, uint16_t); -bool assemble_single(assembler_t *, const char *, addressing_t, uint8_t, uint8_t); -bool assemble_single_implied(assembler_t *, const char *); - #endif /* _INSTRUCTION_H_ */ diff --git a/test/test_assemble.c b/test/test_assemble.c index 0f18e04..d9315d2 100644 --- a/test/test_assemble.c +++ b/test/test_assemble.c @@ -7,6 +7,7 @@ #include "bus.h" #include "rk65c02.h" +#include "assembler.h" #include "instruction.h" #include "utils.h" diff --git a/test/test_debug.c b/test/test_debug.c index eb93f58..1ae4199 100644 --- a/test/test_debug.c +++ b/test/test_debug.c @@ -9,6 +9,7 @@ #include "bus.h" #include "rk65c02.h" +#include "assembler.h" #include "instruction.h" #include "debug.h" #include "utils.h" diff --git a/test/test_emulation.c b/test/test_emulation.c index 07517c1..fed334a 100644 --- a/test/test_emulation.c +++ b/test/test_emulation.c @@ -6,6 +6,7 @@ #include "bus.h" #include "rk65c02.h" +#include "assembler.h" #include "instruction.h" #include "debug.h" #include "log.h"