--- M6502/perl.c 2007/07/31 08:41:06 40 +++ M6502/perl.c 2007/08/01 22:25:37 86 @@ -3,25 +3,11 @@ #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 pull_R(M6502 *R) { +void pull_R(void) { R->A = SvIV( get_sv("M6502::A", FALSE) ); R->P = SvIV( get_sv("M6502::P", FALSE) ); R->X = SvIV( get_sv("M6502::X", FALSE) ); @@ -29,12 +15,17 @@ 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 + 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 push_R(M6502 *R) { +void push_R(void) { + debugf(("push_R")); dSP; ENTER; SAVETMPS; @@ -45,14 +36,26 @@ 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"); + debugf(("push_R called")); dump_R; FREETMPS; LEAVE; } +byte Debug6502(M6502 *R) { + dump_R; + return 1; // continue emulation +} + /** Rd6502()/Wr6502/Op6502() *********************************/ /** These functions are called when access to RAM occurs. **/ /** They allow to control memory access. Op6502 is the same **/ @@ -64,37 +67,43 @@ byte mem(word Addr) { byte byte; int count; + debugf(("mem(%04x)", Addr)); dSP; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs( sv_2mortal( newSViv( Addr ) ) ); PUTBACK; - count = call_pv("Arch::read", G_ARRAY ); + count = call_pv("M6502::read", G_ARRAY | G_EVAL ); + debugf(("got %d values", count)); + SPAGAIN; + if (SvTrue(ERRSV)) { + display_message("ERROR: %s", SvPV_nolen( ERRSV ) ); + exit(1); + } 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)); + debugf(("mem(%04x) = %02x", Addr, byte)); return byte; } byte Rd6502(register word Addr) { byte Value; - Value = mem(Addr); - debugf(("Rd6502(%04x) = %02x\n", Addr, Value)); +// Value = mem(Addr); + Value = 0x42; + debugf(("Rd6502(%04x) = %02x", Addr, Value)); return Value; } void Wr6502(register word Addr,register byte Value) { - debugf(("Wr6502(%04x,%02x)\n", Addr, Value)); + debugf(("Wr6502(%04x,%02x)", Addr, Value)); dSP; ENTER; SAVETMPS; @@ -110,7 +119,7 @@ byte Op6502(register word Addr) { byte Op; Op = mem(Addr); - debugf(("Op6502(%04x,%02x) PC:%04x\n", Addr, Op, R->PC.W)); + debugf(("Op6502(%04x,%02x) PC:%04x", Addr, Op, R->PC.W)); return Op; } @@ -121,10 +130,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) { - debugf(("Loop6502\n")); + debugf(("Loop6502")); dump_R; - return INT_NONE; + return hw_int; } /** Patch6502() **********************************************/ @@ -135,11 +147,56 @@ /** 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)); + debugf(("Patch6502(%02x)", 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; + 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; + 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 * @@ -158,31 +215,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); - } - - 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"); + run_forever(); } - free(R); perl_destruct(my_perl); perl_free(my_perl); return 0;