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

Legend:
Removed from v.30  
changed lines
  Added in v.83

  ViewVC Help
Powered by ViewVC 1.1.26