--- M6502/perl.c 2007/07/30 17:32:41 29 +++ M6502/perl.c 2007/07/31 09:37:01 42 @@ -3,22 +3,55 @@ #include "M6502.h" #include "config.h" +#if DEBUGF +#define debugf(x) do { \ + PerlIO_stdoutf("#> "); \ + PerlIO_stdoutf x ; \ +} while (0) +#else +#define debugf(x) +#endif + static PerlInterpreter *my_perl; static M6502 *R; +#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 ); - -void update_R(M6502 *R) { - R->A = atoi( SvPV_nolen( get_sv("M6502::A", FALSE) ) ); - R->P = atoi( SvPV_nolen( get_sv("M6502::P", FALSE) ) ); - R->X = atoi( SvPV_nolen( get_sv("M6502::X", FALSE) ) ); - R->Y = atoi( SvPV_nolen( get_sv("M6502::Y", FALSE) ) ); - R->S = atoi( SvPV_nolen( get_sv("M6502::S", FALSE) ) ); - R->PC.W = atoi( SvPV_nolen( get_sv("M6502::PC", FALSE) ) ); - R->IPeriod = atoi( SvPV_nolen( get_sv("M6502::IPeriod", FALSE) ) ); +#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() *********************************/ @@ -38,30 +71,31 @@ PUSHMARK(SP); XPUSHs( sv_2mortal( newSViv( Addr ) ) ); PUTBACK; - count = call_pv("M6502::read", G_ARRAY ); + count = call_pv("Arch::read", G_ARRAY ); if ( count != 1 ) { printf("expect 1 return value, got %d", count); exit(1); } - printf("got %d values\n", count); + //debugf(("got %d values\n", count)); SPAGAIN; SV *sv; sv = POPs; byte = SvIV(sv); FREETMPS; LEAVE; - printf("Rd6502(%04x) = %02x\n", Addr, byte); + //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) { - printf("Wr6502(%04x,%02x)\n", Addr, Value); + debugf(("Wr6502(%04x,%02x)\n", Addr, Value)); dSP; ENTER; SAVETMPS; @@ -69,7 +103,7 @@ XPUSHs( sv_2mortal( newSViv( Addr ) ) ); XPUSHs( sv_2mortal( newSViv( Value ) ) ); PUTBACK; - call_pv("M6502::write", G_DISCARD ); + call_pv("Arch::write", G_DISCARD ); FREETMPS; LEAVE; } @@ -77,8 +111,8 @@ byte Op6502(register word Addr) { byte Op; Op = mem(Addr); - printf("Op6502(%04x,%02x)\n", Addr, Op); - dump_R; + debugf(("Op6502(%04x,%02x) PC:%04x\n", Addr, Op, R->PC.W)); + return Op; } /** Loop6502() ***********************************************/ @@ -88,10 +122,13 @@ /** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the **/ /** emulation loop. **/ /************************************ TO BE WRITTEN BY USER **/ + +int hw_int = INT_NONE; + byte Loop6502(register M6502 *R) { - printf("Loop6502\n"); + debugf(("Loop6502\n")); dump_R; - return INT_NONE; + return hw_int; } /** Patch6502() **********************************************/ @@ -102,7 +139,7 @@ /** or 0 if the opcode was truly illegal. **/ /************************************ TO BE WRITTEN BY USER **/ byte Patch6502(register byte Op,register M6502 *R) { - printf("Patch6502(%02x)\n", Op); + debugf(("Patch6502(%02x)\n", Op)); dump_R; return 0; } @@ -113,12 +150,11 @@ **/ 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, xs_init, 3, command_line, (char **)NULL)) { - printf("Failed to parse\n"); + printf("Failed to parse initial: %s\n", EMU_START ); return 0; } perl_run(my_perl); @@ -132,19 +168,27 @@ exit(1); } - update_R(R); - printf("reset CPU\n"); Reset6502(R); - printf("call M6502::init\n"); + printf("call Arch::init\n"); dSP; PUSHMARK(SP); - call_pv("M6502::init", G_DISCARD | G_NOARGS ); - - printf("run CPU\n"); - Run6502(R); + call_pv("Arch::init", G_DISCARD | G_NOARGS ); + int cycles = 1; + while ( cycles ) { + call_pv("Arch::cli", G_DISCARD | G_NOARGS ); + pull_R(R); + 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 %d cycles CPU run\n", cycles); + } } free(R); perl_destruct(my_perl);