--- M6502/perl.c 2007/07/30 13:29:57 24 +++ M6502/perl.c 2007/07/31 08:41:06 40 @@ -3,22 +3,154 @@ #include "M6502.h" #include "config.h" +#if DEBUGF +#define debugf(x) do { \ + PerlIO_stdoutf x ; \ +} while (0) +#else +#define debugf(x) +#endif + static PerlInterpreter *my_perl; static M6502 *R; -void Reset6502(M6502 *R) { - R->PC.W = atoi( SvPV_nolen( get_sv("M6502::PC", TRUE) ) ); - printf("PC: %04x\n", R->PC.W); +#if DUMP_R +#define dump_R printf("# PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x\n", R->PC.W, R->A, R->P, R->X, R->Y, R->S ); +#else +#define dump_R +#endif + +void pull_R(M6502 *R) { + R->A = SvIV( get_sv("M6502::A", FALSE) ); + R->P = SvIV( get_sv("M6502::P", FALSE) ); + R->X = SvIV( get_sv("M6502::X", FALSE) ); + R->Y = SvIV( get_sv("M6502::Y", FALSE) ); + R->S = SvIV( get_sv("M6502::S", FALSE) ); + R->PC.W = SvIV( get_sv("M6502::PC", FALSE) ); + R->IPeriod = SvIV( get_sv("M6502::IPeriod", FALSE) ); + // ICount IRequest IAutoReset TrapBadOps Trap Trace + printf("pull_R finished\n"); + dump_R; +} + +void push_R(M6502 *R) { + dSP; + ENTER; + SAVETMPS; + PUSHMARK(SP); + XPUSHs( sv_2mortal( newSViv( R->A ) ) ); + XPUSHs( sv_2mortal( newSViv( R->P ) ) ); + XPUSHs( sv_2mortal( newSViv( R->X ) ) ); + XPUSHs( sv_2mortal( newSViv( R->Y ) ) ); + XPUSHs( sv_2mortal( newSViv( R->S ) ) ); + XPUSHs( sv_2mortal( newSViv( R->PC.W ) ) ); + PUTBACK; + call_pv("M6502::push_R", G_DISCARD ); + printf("push_R called\n"); + dump_R; + FREETMPS; + LEAVE; } +/** Rd6502()/Wr6502/Op6502() *********************************/ +/** These functions are called when access to RAM occurs. **/ +/** They allow to control memory access. Op6502 is the same **/ +/** as Rd6502, but used to read *opcodes* only, when many **/ +/** checks can be skipped to make it fast. It is only **/ +/** required if there is a #define FAST_RDOP. **/ +/************************************ TO BE WRITTEN BY USER **/ + +byte mem(word Addr) { + byte byte; + int count; + dSP; + ENTER; + SAVETMPS; + PUSHMARK(SP); + XPUSHs( sv_2mortal( newSViv( Addr ) ) ); + PUTBACK; + count = call_pv("Arch::read", G_ARRAY ); + if ( count != 1 ) { + printf("expect 1 return value, got %d", count); + exit(1); + } + //debugf(("got %d values\n", count)); + SPAGAIN; + SV *sv; + sv = POPs; + byte = SvIV(sv); + FREETMPS; + LEAVE; + //debugf(("mem(%04x) = %02x\n", Addr, byte)); + return byte; +} + +byte Rd6502(register word Addr) { + byte Value; + Value = mem(Addr); + debugf(("Rd6502(%04x) = %02x\n", Addr, Value)); + return Value; +} + +void Wr6502(register word Addr,register byte Value) { + debugf(("Wr6502(%04x,%02x)\n", Addr, Value)); + dSP; + ENTER; + SAVETMPS; + PUSHMARK(SP); + XPUSHs( sv_2mortal( newSViv( Addr ) ) ); + XPUSHs( sv_2mortal( newSViv( Value ) ) ); + PUTBACK; + call_pv("Arch::write", G_DISCARD ); + FREETMPS; + LEAVE; +} + +byte Op6502(register word Addr) { + byte Op; + Op = mem(Addr); + debugf(("Op6502(%04x,%02x) PC:%04x\n", Addr, Op, R->PC.W)); + return Op; +} + +/** Loop6502() ***********************************************/ +/** 6502 emulation calls this function periodically to **/ +/** check if the system hardware requires any interrupts. **/ +/** This function must return one of following values: **/ +/** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the **/ +/** emulation loop. **/ +/************************************ TO BE WRITTEN BY USER **/ +byte Loop6502(register M6502 *R) { + debugf(("Loop6502\n")); + dump_R; + return INT_NONE; +} + +/** Patch6502() **********************************************/ +/** Emulation calls this function when it encounters an **/ +/** unknown opcode. This can be used to patch the code to **/ +/** emulate BIOS calls, such as disk and tape access. The **/ +/** function should return 1 if the exception was handled, **/ +/** or 0 if the opcode was truly illegal. **/ +/************************************ TO BE WRITTEN BY USER **/ +byte Patch6502(register byte Op,register M6502 *R) { + debugf(("Patch6502(%02x)\n", Op)); + dump_R; + return 0; +} + +/** + * main code + * + **/ + int main(int argc, char **argv) { - char *command_line[] = {"", "-e", - "use M6502; print \"Loaded M6502 module\n\";"}; + char *command_line[] = {"", "-e", EMU_START }; my_perl = perl_alloc(); perl_construct(my_perl); - if (perl_parse(my_perl, NULL, 3, command_line, (char **)NULL)) { - printf("Failed to parse\n"); + if (perl_parse(my_perl, xs_init, 3, command_line, (char **)NULL)) { + printf("Failed to parse initial: %s\n", EMU_START ); return 0; } perl_run(my_perl); @@ -31,9 +163,24 @@ printf("can't alloc %d bytes for M6502", sizeof(M6502)); exit(1); } + printf("reset CPU\n"); Reset6502(R); + printf("call Arch::init\n"); + dSP; + PUSHMARK(SP); + call_pv("Arch::init", G_DISCARD | G_NOARGS ); + + pull_R(R); + int cycles = SvIV( get_sv("M6502::run_for", FALSE) ); + printf("run CPU for %d cycles\n", cycles); + dump_R; + //Run6502(R); + Exec6502(R, cycles); + dump_R; + push_R(R); + printf("end of CPU run\n"); } free(R); perl_destruct(my_perl);