2017-01-16 19:35:28 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
2018-04-01 21:40:49 +02:00
|
|
|
#include <stdarg.h>
|
2017-01-16 19:35:28 +01:00
|
|
|
|
2018-03-23 13:37:07 +01:00
|
|
|
#include <errno.h>
|
2017-01-16 19:35:28 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-03-26 12:36:47 +02:00
|
|
|
#include <gc/gc.h>
|
|
|
|
|
2017-01-16 19:35:28 +01:00
|
|
|
#include "bus.h"
|
2017-01-17 14:29:20 +01:00
|
|
|
#include "instruction.h"
|
|
|
|
#include "rk65c02.h"
|
2018-03-21 16:22:29 +01:00
|
|
|
#include "log.h"
|
2017-02-14 11:17:40 +01:00
|
|
|
#include "debug.h"
|
2017-01-16 19:35:28 +01:00
|
|
|
|
2017-01-26 13:11:00 +01:00
|
|
|
void rk65c02_exec(rk65c02emu_t *);
|
|
|
|
|
2018-04-10 10:12:27 +02:00
|
|
|
rk65c02emu_t
|
|
|
|
rk65c02_load_rom(const char *path, uint16_t load_addr, bus_t *b)
|
|
|
|
{
|
|
|
|
rk65c02emu_t e;
|
|
|
|
|
|
|
|
if (b == NULL) {
|
|
|
|
b = GC_MALLOC(sizeof(bus_t));
|
2018-08-05 23:45:44 +02:00
|
|
|
assert(b != NULL);
|
2018-04-10 10:12:27 +02:00
|
|
|
*b = bus_init_with_default_devs();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: normal error handling instead of assert would be preferred */
|
|
|
|
assert(bus_load_file(b, load_addr, path));
|
|
|
|
|
|
|
|
e = rk65c02_init(b);
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2017-01-18 17:18:19 +01:00
|
|
|
rk65c02emu_t
|
|
|
|
rk65c02_init(bus_t *b)
|
|
|
|
{
|
|
|
|
rk65c02emu_t e;
|
|
|
|
|
|
|
|
e.bus = b;
|
|
|
|
e.state = STOPPED;
|
2017-02-09 21:53:45 +01:00
|
|
|
e.stopreason = HOST;
|
2017-01-25 21:17:18 +01:00
|
|
|
e.regs.P = P_UNDEFINED|P_IRQ_DISABLE;
|
2017-01-31 00:31:04 +01:00
|
|
|
/* reset also clears the decimal flag */
|
|
|
|
e.regs.P &= ~P_DECIMAL;
|
2017-01-18 17:18:19 +01:00
|
|
|
|
2017-02-04 21:44:13 +01:00
|
|
|
e.irq = false;
|
|
|
|
|
2017-02-14 11:17:40 +01:00
|
|
|
e.bps_head = NULL;
|
2018-03-26 16:11:26 +02:00
|
|
|
e.trace = false;
|
2017-02-15 21:32:12 +01:00
|
|
|
e.trace_head = NULL;
|
2017-02-15 21:38:51 +01:00
|
|
|
e.runtime_disassembly = false;
|
2017-02-14 11:17:40 +01:00
|
|
|
|
2018-03-23 13:37:07 +01:00
|
|
|
rk65c02_log(LOG_DEBUG, "Initialized new emulator.");
|
2018-03-21 16:22:29 +01:00
|
|
|
|
2017-01-18 17:18:19 +01:00
|
|
|
return e;
|
|
|
|
}
|
2017-01-16 19:35:28 +01:00
|
|
|
|
2017-02-09 21:53:45 +01:00
|
|
|
void
|
|
|
|
rk65c02_assert_irq(rk65c02emu_t *e)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Clearly this is too simpleton'ish, because more than one device
|
|
|
|
* might want to assert the interrupt line (on hardware level it is
|
|
|
|
* active low, so can just be pulled down by any device connected
|
|
|
|
* to it.
|
|
|
|
*/
|
|
|
|
e->irq = true;
|
|
|
|
|
2018-04-13 11:25:30 +02:00
|
|
|
/*
|
|
|
|
* If the CPU was put to sleep by executing WAI instruction, resume
|
|
|
|
* operation.
|
|
|
|
*
|
|
|
|
* Whether interrupt will immediately be serviced, or not, depends
|
|
|
|
* on normal "interrupt disable" flag behaviour, so here we just
|
|
|
|
* need to start the CPU.
|
|
|
|
*/
|
2017-02-09 21:53:45 +01:00
|
|
|
if ((e->state == STOPPED) && (e->stopreason == WAI))
|
|
|
|
rk65c02_start(e);
|
|
|
|
}
|
|
|
|
|
2017-02-04 21:44:13 +01:00
|
|
|
void
|
|
|
|
rk65c02_irq(rk65c02emu_t *e)
|
|
|
|
{
|
|
|
|
/* push return address to the stack */
|
|
|
|
stack_push(e, e->regs.PC >> 8);
|
|
|
|
stack_push(e, e->regs.PC & 0xFF);
|
|
|
|
/* push processor status to the stack with break flag set */
|
|
|
|
stack_push(e, e->regs.P);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The IRQ disable is set, decimal flags is cleared _after_ pushing
|
|
|
|
* the P register to the stack.
|
|
|
|
*/
|
|
|
|
e->regs.P |= P_IRQ_DISABLE;
|
|
|
|
e->regs.P &= ~P_DECIMAL;
|
|
|
|
|
|
|
|
/* break flag is only saved to the stack, it is not present in the ISR... */
|
|
|
|
e->regs.P &= ~P_BREAK;
|
|
|
|
|
|
|
|
/* load address from IRQ vector into program counter */
|
|
|
|
e->regs.PC = bus_read_1(e->bus, VECTOR_IRQ);
|
|
|
|
e->regs.PC |= bus_read_1(e->bus, VECTOR_IRQ + 1) << 8;
|
|
|
|
}
|
|
|
|
|
2017-01-27 10:13:32 +01:00
|
|
|
/*
|
|
|
|
* Execute a single instruction within emulator.
|
|
|
|
*/
|
2017-01-16 19:35:28 +01:00
|
|
|
void
|
2017-01-26 13:11:00 +01:00
|
|
|
rk65c02_exec(rk65c02emu_t *e)
|
|
|
|
{
|
2017-01-16 19:35:28 +01:00
|
|
|
instruction_t i;
|
2017-01-18 17:18:19 +01:00
|
|
|
instrdef_t id;
|
2017-02-15 21:32:12 +01:00
|
|
|
uint16_t tpc; /* saved PC for tracing */
|
|
|
|
|
|
|
|
tpc = e->regs.PC;
|
2017-01-16 19:35:28 +01:00
|
|
|
|
2017-02-04 21:44:13 +01:00
|
|
|
if (e->irq && (!(e->regs.P & P_IRQ_DISABLE)))
|
|
|
|
rk65c02_irq(e);
|
|
|
|
|
2017-02-14 11:17:40 +01:00
|
|
|
/* XXX: handle watchpoints toos */
|
|
|
|
if (debug_PC_is_breakpoint(e)) {
|
|
|
|
e->state = STOPPED;
|
|
|
|
e->stopreason = BREAKPOINT;
|
2017-02-15 19:56:22 +01:00
|
|
|
return;
|
2017-02-14 11:17:40 +01:00
|
|
|
}
|
2017-01-26 13:11:00 +01:00
|
|
|
|
2017-02-15 21:38:51 +01:00
|
|
|
if (e->runtime_disassembly)
|
|
|
|
disassemble(e->bus, e->regs.PC);
|
2017-01-26 13:11:00 +01:00
|
|
|
|
|
|
|
i = instruction_fetch(e->bus, e->regs.PC);
|
|
|
|
id = instruction_decode(i.opcode);
|
|
|
|
|
2018-04-10 13:56:37 +02:00
|
|
|
assert(id.emul);
|
|
|
|
|
|
|
|
id.emul(e, &id, &i);
|
|
|
|
|
|
|
|
if (!instruction_modify_pc(&id))
|
|
|
|
program_counter_increment(e, &id);
|
2017-02-15 21:32:12 +01:00
|
|
|
|
|
|
|
if (e->trace)
|
|
|
|
debug_trace_savestate(e, tpc, &id, &i);
|
|
|
|
|
2017-01-26 13:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rk65c02_start(rk65c02emu_t *e) {
|
|
|
|
|
2017-02-02 14:43:44 +01:00
|
|
|
assert(e != NULL);
|
|
|
|
|
2017-01-18 17:18:19 +01:00
|
|
|
e->state = RUNNING;
|
|
|
|
while (e->state == RUNNING) {
|
2017-01-26 13:11:00 +01:00
|
|
|
rk65c02_exec(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rk65c02_step(rk65c02emu_t *e, uint16_t steps) {
|
|
|
|
|
|
|
|
uint16_t i = 0;
|
|
|
|
|
2017-02-02 14:43:44 +01:00
|
|
|
assert(e != NULL);
|
|
|
|
|
2017-01-26 13:11:00 +01:00
|
|
|
e->state = STEPPING;
|
|
|
|
while ((e->state == STEPPING) && (i < steps)) {
|
|
|
|
rk65c02_exec(e);
|
|
|
|
i++;
|
2017-01-16 19:35:28 +01:00
|
|
|
}
|
2017-02-03 22:21:43 +01:00
|
|
|
|
|
|
|
e->state = STOPPED;
|
|
|
|
e->stopreason = STEPPED;
|
2017-01-16 19:35:28 +01:00
|
|
|
}
|
2017-01-27 11:27:14 +01:00
|
|
|
|
2017-02-06 23:16:00 +01:00
|
|
|
void
|
|
|
|
rk65c02_dump_stack(rk65c02emu_t *e, uint8_t n)
|
|
|
|
{
|
|
|
|
uint16_t stackaddr;
|
|
|
|
|
|
|
|
stackaddr = STACK_END-n;
|
|
|
|
|
|
|
|
while (stackaddr <= STACK_END) {
|
|
|
|
|
|
|
|
if ((stackaddr == STACK_END-n) || !((stackaddr % 0x10)))
|
|
|
|
printf("stack %#02x: ", stackaddr);
|
|
|
|
|
|
|
|
printf("%#02x ", bus_read_1(e->bus, stackaddr));
|
|
|
|
|
|
|
|
stackaddr++;
|
|
|
|
|
|
|
|
if (!(stackaddr % 0x10))
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 11:27:14 +01:00
|
|
|
void
|
2017-02-15 21:32:12 +01:00
|
|
|
rk65c02_dump_regs(reg_state_t regs)
|
2017-01-27 11:27:14 +01:00
|
|
|
{
|
2018-03-23 13:37:07 +01:00
|
|
|
char *str;
|
|
|
|
|
|
|
|
str = rk65c02_regs_string_get(regs);
|
|
|
|
|
2018-03-24 00:14:41 +01:00
|
|
|
printf ("%s\n", str);
|
2018-03-23 13:37:07 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
rk65c02_regs_string_get(reg_state_t regs)
|
|
|
|
{
|
|
|
|
#define REGS_STR_LEN 50
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
/* XXX: string allocation to a separate utility function? */
|
2018-03-26 12:36:47 +02:00
|
|
|
str = GC_MALLOC(REGS_STR_LEN);
|
2018-08-05 23:45:44 +02:00
|
|
|
assert(str != NULL);
|
2018-03-23 13:37:07 +01:00
|
|
|
memset(str, 0, REGS_STR_LEN);
|
|
|
|
|
|
|
|
snprintf(str, REGS_STR_LEN, "A: %X X: %X Y: %X PC: %X SP: %X P: %c%c%c%c%c%c%c%c",
|
|
|
|
regs.A, regs.X, regs.Y, regs.PC, regs.SP,
|
|
|
|
(regs.P & P_NEGATIVE) ? 'N' : '-',
|
|
|
|
(regs.P & P_SIGN_OVERFLOW) ? 'V' : '-',
|
|
|
|
(regs.P & P_UNDEFINED) ? '1' : '-',
|
|
|
|
(regs.P & P_BREAK) ? 'B' : '-',
|
|
|
|
(regs.P & P_DECIMAL) ? 'D' : '-',
|
|
|
|
(regs.P & P_IRQ_DISABLE) ? 'I' : '-',
|
|
|
|
(regs.P & P_ZERO) ? 'Z' : '-',
|
|
|
|
(regs.P & P_CARRY) ? 'C' : '-');
|
|
|
|
|
|
|
|
return str;
|
2017-01-27 11:27:14 +01:00
|
|
|
}
|
2018-04-01 21:40:49 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
rk65c02_panic(rk65c02emu_t *e, const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
rk65c02_log(LOG_CRIT, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
e->state = STOPPED;
|
|
|
|
e->stopreason = EMUERROR;
|
|
|
|
|
|
|
|
/* TODO: run some UI callback. */
|
|
|
|
}
|
|
|
|
|