--- M6502/perl.c 2007/07/30 17:56:13 30 +++ M6502/perl.c 2007/08/01 21:46:23 83 @@ -7,18 +7,52 @@ static M6502 *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 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) ); + R->IRequest = SvIV( get_sv("M6502::IRequest", FALSE) ); + R->IAutoReset = SvIV( get_sv("M6502::IAutoReset", FALSE) ); + R->TrapBadOps = SvIV( get_sv("M6502::TrapBadOps", FALSE) ); + R->Trap = SvIV( get_sv("M6502::Trap", FALSE) ); + R->Trace = SvIV( get_sv("M6502::Trace", FALSE) ); + printf("pull_R finished\n"); + dump_R; +} -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) ) ); - // ICount IRequest IAutoReset TrapBadOps Trap Trace +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 ) ) ); + XPUSHs( sv_2mortal( newSViv( R->IPeriod ) ) ); + XPUSHs( sv_2mortal( newSViv( R->ICount ) ) ); + XPUSHs( sv_2mortal( newSViv( R->IRequest ) ) ); + XPUSHs( sv_2mortal( newSViv( R->IAutoReset ) ) ); + XPUSHs( sv_2mortal( newSViv( R->TrapBadOps ) ) ); + XPUSHs( sv_2mortal( newSViv( R->Trap ) ) ); + XPUSHs( sv_2mortal( newSViv( R->Trace ) ) ); + PUTBACK; + call_pv("M6502::push_R", G_DISCARD ); + printf("push_R called\n"); dump_R; + FREETMPS; + LEAVE; +} + +byte Debug6502(M6502 *R) { + dump_R; + return 1; // continue emulation } /** Rd6502()/Wr6502/Op6502() *********************************/ @@ -32,36 +66,38 @@ byte mem(word Addr) { byte byte; int count; + debugf(("mem(%04x)\n", Addr)); dSP; ENTER; SAVETMPS; 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 +105,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 +113,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 +124,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,11 +141,56 @@ /** 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; + hw_int = INT_QUIT; return 0; } +void run_forever(void) { + printf("entered run_forever\n"); + + R = malloc(sizeof(M6502)); + if (!R) { + 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 ); + FREETMPS; + LEAVE; + + int cycles = 1; + while ( cycles ) { + dSP; + PUSHMARK(SP); + call_pv("Arch::cli", G_DISCARD | G_NOARGS ); + pull_R(R); + FREETMPS; + LEAVE; + cycles = SvIV( get_sv("M6502::run_for", FALSE) ); + if ( cycles > 0 ) { + 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); + } else { + printf("no cpu cycles set for run\n"); + cycles = 1; // never exit, prevents segfault + } + } + free(R); +} + /** * main code * @@ -117,7 +201,7 @@ 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); @@ -125,27 +209,8 @@ printf("Failed to execute\n"); return 0; } else { - R = malloc(sizeof(M6502)); - if (!R) { - printf("can't alloc %d bytes for M6502", sizeof(M6502)); - exit(1); - } - - update_R(R); - - printf("reset CPU\n"); - Reset6502(R); - - printf("call M6502::init\n"); - dSP; - PUSHMARK(SP); - call_pv("M6502::init", G_DISCARD | G_NOARGS ); - - printf("run CPU\n"); - Run6502(R); - + run_forever(); } - free(R); perl_destruct(my_perl); perl_free(my_perl); return 0;