/[VRac]/M6502/perl.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /M6502/perl.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 27 by dpavlin, Mon Jul 30 15:45:03 2007 UTC revision 81 by dpavlin, Wed Aug 1 15:53:54 2007 UTC
# Line 3  Line 3 
3  #include "M6502.h"  #include "M6502.h"
4  #include "config.h"  #include "config.h"
5    
6    #if DEBUGF
7    #define debugf(x)  do {         \
8            PerlIO_stdoutf("#> ");  \
9            PerlIO_stdoutf x ;      \
10    } while (0)
11    #else
12    #define debugf(x)
13    #endif
14    
15  static PerlInterpreter *my_perl;  static PerlInterpreter *my_perl;
16    
17  static M6502 *R;  static M6502 *R;
18    
19    #if DUMP_R
20  #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 );  #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 );
21    #else
22    #define dump_R
23    #endif
24    
25    void pull_R(M6502 *R) {
26            R->A = SvIV( get_sv("M6502::A", FALSE) );
27            R->P = SvIV( get_sv("M6502::P", FALSE) );
28            R->X = SvIV( get_sv("M6502::X", FALSE) );
29            R->Y = SvIV( get_sv("M6502::Y", FALSE) );
30            R->S = SvIV( get_sv("M6502::S", FALSE) );
31            R->PC.W = SvIV( get_sv("M6502::PC", FALSE) );
32            R->IPeriod = SvIV( get_sv("M6502::IPeriod", FALSE) );
33            R->IRequest = SvIV( get_sv("M6502::IRequest", FALSE) );
34            R->IAutoReset = SvIV( get_sv("M6502::IAutoReset", FALSE) );
35            R->TrapBadOps = SvIV( get_sv("M6502::TrapBadOps", FALSE) );
36            R->Trap = SvIV( get_sv("M6502::Trap", FALSE) );
37            R->Trace = SvIV( get_sv("M6502::Trace", FALSE) );
38            printf("pull_R finished\n");
39            dump_R;
40    }
41    
42    void push_R(M6502 *R) {
43            dSP;
44            ENTER;
45            SAVETMPS;
46            PUSHMARK(SP);
47            XPUSHs( sv_2mortal( newSViv( R->A ) ) );
48            XPUSHs( sv_2mortal( newSViv( R->P ) ) );
49            XPUSHs( sv_2mortal( newSViv( R->X ) ) );
50            XPUSHs( sv_2mortal( newSViv( R->Y ) ) );
51            XPUSHs( sv_2mortal( newSViv( R->S ) ) );
52            XPUSHs( sv_2mortal( newSViv( R->PC.W ) ) );
53            XPUSHs( sv_2mortal( newSViv( R->IPeriod ) ) );
54            XPUSHs( sv_2mortal( newSViv( R->ICount ) ) );
55            XPUSHs( sv_2mortal( newSViv( R->IRequest ) ) );
56            XPUSHs( sv_2mortal( newSViv( R->IAutoReset ) ) );
57            XPUSHs( sv_2mortal( newSViv( R->TrapBadOps ) ) );
58            XPUSHs( sv_2mortal( newSViv( R->Trap ) ) );
59            XPUSHs( sv_2mortal( newSViv( R->Trace ) ) );
60            PUTBACK;
61            call_pv("M6502::push_R", G_DISCARD );
62            printf("push_R called\n");
63            dump_R;
64            FREETMPS;
65            LEAVE;
66    }
67    
68  void update_R(M6502 *R) {  byte Debug6502(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  
69          dump_R;          dump_R;
70            return 1; // continue emulation
71  }  }
72    
73  /** Rd6502()/Wr6502/Op6502() *********************************/  /** Rd6502()/Wr6502/Op6502() *********************************/
# Line 29  void update_R(M6502 *R) { Line 78  void update_R(M6502 *R) {
78  /** required if there is a #define FAST_RDOP.               **/  /** required if there is a #define FAST_RDOP.               **/
79  /************************************ TO BE WRITTEN BY USER **/  /************************************ TO BE WRITTEN BY USER **/
80    
81  byte Rd6502(register word Addr) {  byte mem(word Addr) {
82          byte Value;          byte byte;
         Value = 0x42;  
         printf("Rd6502(%04x,%02x)\n", Addr, Value);  
   
83          int count;          int count;
84          dSP;          dSP;
85          ENTER;          ENTER;
86          SAVETMPS;          SAVETMPS;
87          PUSHMARK(SP);          PUSHMARK(SP);
88          XPUSHs( sv_2mortal( newSViv( Value ) ) );          XPUSHs( sv_2mortal( newSViv( Addr ) ) );
89          PUTBACK;          PUTBACK;
90          count = call_pv("M6502::read", G_ARRAY );          count = call_pv("Arch::read", G_ARRAY );
91          if ( count != 1 ) {          if ( count != 1 ) {
92                  printf("expect 1 return value, got %d", count);                  printf("expect 1 return value, got %d", count);
93                  exit(1);                  exit(1);
94          }          }
95          printf("got %d values\n", count);          //debugf(("got %d values\n", count));
96          SPAGAIN;          SPAGAIN;
97          SV *sv;          SV *sv;
98          sv = POPs;          sv = POPs;
99          Value = SvIV(sv);          byte = SvIV(sv);
 //      Value = savepv(SvPV_nolen(POPs));  
100          FREETMPS;          FREETMPS;
101          LEAVE;          LEAVE;
102          printf("Rd6502(%04x) = %02x\n", Addr, Value);          //debugf(("mem(%04x) = %02x\n", Addr, byte));
103            return byte;
104    }
105    
106    byte Rd6502(register word Addr) {
107            byte Value;
108            Value = mem(Addr);
109            debugf(("Rd6502(%04x) = %02x\n", Addr, Value));
110          return Value;          return Value;
111  }  }
112    
113  void Wr6502(register word Addr,register byte Value) {  void Wr6502(register word Addr,register byte Value) {
114          printf("Wr6502(%04x,%02x)\n", Addr, Value);          debugf(("Wr6502(%04x,%02x)\n", Addr, Value));
115          dSP;          dSP;
116          ENTER;          ENTER;
117          SAVETMPS;          SAVETMPS;
# Line 67  void Wr6502(register word Addr,register Line 119  void Wr6502(register word Addr,register
119          XPUSHs( sv_2mortal( newSViv( Addr ) ) );          XPUSHs( sv_2mortal( newSViv( Addr ) ) );
120          XPUSHs( sv_2mortal( newSViv( Value ) ) );          XPUSHs( sv_2mortal( newSViv( Value ) ) );
121          PUTBACK;          PUTBACK;
122          call_pv("M6502::write", G_DISCARD );          call_pv("Arch::write", G_DISCARD );
123          FREETMPS;          FREETMPS;
124          LEAVE;          LEAVE;
125  }  }
126    
127  byte Op6502(register word Addr) {  byte Op6502(register word Addr) {
128          byte Op;          byte Op;
129          Op = 0xff;          Op = mem(Addr);
130          printf("Op6502(%04x,%02x)\n", Addr, Op);          debugf(("Op6502(%04x,%02x) PC:%04x\n", Addr, Op, R->PC.W));
131          dump_R;          return Op;
132  }  }
133    
134  /** Loop6502() ***********************************************/  /** Loop6502() ***********************************************/
# Line 86  byte Op6502(register word Addr) { Line 138  byte Op6502(register word Addr) {
138  /** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the     **/  /** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the     **/
139  /** emulation loop.                                         **/  /** emulation loop.                                         **/
140  /************************************ TO BE WRITTEN BY USER **/  /************************************ TO BE WRITTEN BY USER **/
141    
142    int hw_int = INT_NONE;
143    
144  byte Loop6502(register M6502 *R) {  byte Loop6502(register M6502 *R) {
145          printf("Loop6502\n");          debugf(("Loop6502\n"));
146          dump_R;          dump_R;
147            return hw_int;
148  }  }
149    
150  /** Patch6502() **********************************************/  /** Patch6502() **********************************************/
# Line 99  byte Loop6502(register M6502 *R) { Line 155  byte Loop6502(register M6502 *R) {
155  /** or 0 if the opcode was truly illegal.                   **/  /** or 0 if the opcode was truly illegal.                   **/
156  /************************************ TO BE WRITTEN BY USER **/  /************************************ TO BE WRITTEN BY USER **/
157  byte Patch6502(register byte Op,register M6502 *R) {  byte Patch6502(register byte Op,register M6502 *R) {
158          printf("Patch6502(%02x)\n", Op);          debugf(("Patch6502(%02x)\n", Op));
159          dump_R;          dump_R;
160            hw_int = INT_QUIT;
161            return 0;
162    }
163    
164    void run_forever(void) {
165            printf("entered run_forever\n");
166    
167            R = malloc(sizeof(M6502));
168            if (!R) {
169                    printf("can't alloc %d bytes for M6502", sizeof(M6502));
170                    exit(1);
171            }
172    
173            printf("reset CPU\n");
174            Reset6502(R);
175    
176            printf("call Arch::init\n");
177            dSP;
178            PUSHMARK(SP);
179            call_pv("Arch::init", G_DISCARD | G_NOARGS );
180            FREETMPS;
181            LEAVE;
182    
183            int cycles = 1;
184            while ( cycles ) {
185                    dSP;
186                    PUSHMARK(SP);
187                    call_pv("Arch::cli", G_DISCARD | G_NOARGS );
188                    pull_R(R);
189                    FREETMPS;
190                    LEAVE;
191                    cycles = SvIV( get_sv("M6502::run_for", FALSE) );
192                    if ( cycles > 0 ) {
193                            printf("run CPU for %d cycles\n", cycles);
194                            dump_R;
195                            //Run6502(R);
196                            Exec6502(R, cycles);
197                            dump_R;
198                            push_R(R);
199                            printf("end of %d cycles CPU run\n", cycles);
200                    } else {
201                            printf("no cpu cycles set for run\n");
202                            cycles = 1; // never exit, prevents segfault
203                    }
204            }
205            free(R);
206  }  }
207    
208  /**  /**
# Line 109  byte Patch6502(register byte Op,register Line 211  byte Patch6502(register byte Op,register
211   **/   **/
212    
213  int main(int argc, char **argv) {  int main(int argc, char **argv) {
214          char *command_line[] = {"", "-e",          char *command_line[] = {"", "-e", EMU_START };
                 "use M6502; print \"Loaded M6502 module\n\";"};  
215          my_perl = perl_alloc();          my_perl = perl_alloc();
216          perl_construct(my_perl);          perl_construct(my_perl);
217          if (perl_parse(my_perl, NULL, 3, command_line, (char **)NULL)) {          if (perl_parse(my_perl, xs_init, 3, command_line, (char **)NULL)) {
218                  printf("Failed to parse\n");                  printf("Failed to parse initial: %s\n", EMU_START );
219                  return 0;                  return 0;
220          }          }
221          perl_run(my_perl);          perl_run(my_perl);
# Line 122  int main(int argc, char **argv) { Line 223  int main(int argc, char **argv) {
223                  printf("Failed to execute\n");                  printf("Failed to execute\n");
224                  return 0;                  return 0;
225          } else {          } else {
226                  R = malloc(sizeof(M6502));                  run_forever();
                 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);  
   
227          }          }
         free(R);  
228          perl_destruct(my_perl);          perl_destruct(my_perl);
229          perl_free(my_perl);          perl_free(my_perl);
230          return 0;          return 0;

Legend:
Removed from v.27  
changed lines
  Added in v.81

  ViewVC Help
Powered by ViewVC 1.1.26