From 49b70f0e1fdc79f8448c11b1f6b43cc3f2d41c9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kujawa?= Date: Thu, 19 Jan 2017 10:59:35 +0100 Subject: [PATCH] Make instruction data read more flexible and split from emulation of particular instruction. --- src/emulation.c | 6 ++++- src/instruction.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++ src/instruction.h | 1 + 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/emulation.c b/src/emulation.c index c1bb55f..fc090e6 100644 --- a/src/emulation.c +++ b/src/emulation.c @@ -8,7 +8,11 @@ emul_lda(rk65c02emu_t *e, instruction_t *i) instrdef_t id; id = instruction_decode(i->opcode); - e->regs.A = i->op1; + e->regs.A = instruction_data_read_1(e, &id, i); + + + /* adjust status flags */ + } diff --git a/src/instruction.c b/src/instruction.c index b3f4849..d583de1 100644 --- a/src/instruction.c +++ b/src/instruction.c @@ -137,3 +137,62 @@ instruction_decode(uint8_t opcode) return id; } +uint8_t +instruction_data_read_1(rk65c02emu_t *e, instrdef_t *id, instruction_t *i) +{ + uint8_t rv; /* data read from the bus */ +// uint8_t ziaddr; /* indirect zero page address */ + uint16_t iaddr; /* indirect address */ + + rv = 0; + + switch (id->mode) { + case ACCUMULATOR: + rv = e->regs.A; + break; + case IMMEDIATE: + rv = i->op1; + break; + case ZP: + rv = bus_read_1(e->bus, i->op1); + break; + case ZPX: + /* XXX: wraps around zero page? */ + rv = bus_read_1(e->bus, i->op1 + e->regs.X); + break; + case ZPY: + rv = bus_read_1(e->bus, i->op1 + e->regs.Y); + break; + case IZP: + iaddr = bus_read_1(e->bus, i->op1); + iaddr |= (bus_read_1(e->bus, i->op1 + 1) << 8); + rv = bus_read_1(e->bus, iaddr); + break; + case IZPX: +/* ziaddr = bus_read_1(e->bus, i->op1 + e->regs.X); + ziaddr |= (bus_read_1(e->bus, i->op1 + e->regs.X + 1) << 8); + rv = bus_read_1(e->bus, ziaddr); + break; +*/ + case IZPY: +/* + iaddr = bus_read_1(e->bus, i->op1 + e->regs.Y); + iaddr |= (bus_read_1(e->bus, i->op1 + e->regs.Y + 1) << 8); + rv = bus_read_1(e->bus, ziaddr); +*/ + case RELATIVE: + case ABSOLUTE: + case ABSOLUTEX: + case ABSOLUTEY: + case IABSOLUTE: + case IABSOLUTEX: + + default: + printf("unhandled addressing mode for opcode %x\n", + i->opcode); + break; + } + + return rv; +} + diff --git a/src/instruction.h b/src/instruction.h index 3038820..4075cae 100644 --- a/src/instruction.h +++ b/src/instruction.h @@ -43,6 +43,7 @@ instruction_t instruction_fetch(bus_t *, uint16_t); instrdef_t instruction_decode(uint8_t); void instruction_print(instruction_t *); void disassemble(bus_t *, uint16_t); +uint8_t instruction_data_read_1(rk65c02emu_t *, instrdef_t *, instruction_t *); //void instruction_execute(rk65c02emu_t *, instruction_t *); #endif /* _INSTRUCTION_H_ */