--- M6502/perl.c 2007/07/30 17:32:41 29 +++ M6502/perl.c 2007/07/30 21:53:04 35 @@ -3,15 +3,28 @@ #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; +#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 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->A = SvIV( get_sv("M6502::A", FALSE) ); + printf("--%d", get_sv("M6502::A", FALSE) ); + R->P = SvIV( 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) ) ); @@ -38,30 +51,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 +83,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,7 +91,7 @@ byte Op6502(register word Addr) { byte Op; Op = mem(Addr); - printf("Op6502(%04x,%02x)\n", Addr, Op); + debugf(("Op6502(%04x,%02x)\n", Addr, Op)); dump_R; } @@ -89,7 +103,7 @@ /** emulation loop. **/ /************************************ TO BE WRITTEN BY USER **/ byte Loop6502(register M6502 *R) { - printf("Loop6502\n"); + debugf(("Loop6502\n")); dump_R; return INT_NONE; } @@ -102,7 +116,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 +127,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 +145,18 @@ 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 ); + call_pv("Arch::init", G_DISCARD | G_NOARGS ); + update_R(R); printf("run CPU\n"); Run6502(R); - + printf("end of CPU run\n"); } free(R); perl_destruct(my_perl);