/[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 28 by dpavlin, Mon Jul 30 15:52:22 2007 UTC revision 84 by dpavlin, Wed Aug 1 22:01:15 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 push_R(M6502 *R) {
28            dSP;
29            ENTER;
30            SAVETMPS;
31            PUSHMARK(SP);
32            XPUSHs( sv_2mortal( newSViv( R->A ) ) );
33            XPUSHs( sv_2mortal( newSViv( R->P ) ) );
34            XPUSHs( sv_2mortal( newSViv( R->X ) ) );
35            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;
49            FREETMPS;
50            LEAVE;
51    }
52    
53  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  
54          dump_R;          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("M6502::read", G_ARRAY | G_EVAL );
77            debugf(("got %d values\n", count));
78            SPAGAIN;
79            if (SvTrue(ERRSV)) {
80                    display_message("ERROR: %s", SvPV_nolen( ERRSV ) );
81                    exit(1);
82            }
83          if ( count != 1 ) {          if ( count != 1 ) {
84                  printf("expect 1 return value, got %d", count);                  printf("expect 1 return value, got %d", count);
85                  exit(1);                  exit(1);
86          }          }
         printf("got %d values\n", count);  
         SPAGAIN;  
87          SV *sv;          SV *sv;
88          sv = POPs;          sv = POPs;
89          byte = SvIV(sv);          byte = SvIV(sv);
90          FREETMPS;          FREETMPS;
91          LEAVE;          LEAVE;
92          printf("Rd6502(%04x) = %02x\n", Addr, byte);          debugf(("mem(%04x) = %02x\n", Addr, byte));
93          return byte;          return byte;
94  }  }
95    
96  byte Rd6502(register word Addr) {  byte Rd6502(register word Addr) {
97          byte Value;          byte Value;
98          Value = mem(Addr);  //      Value = mem(Addr);
99            Value = 0x42;
100            debugf(("Rd6502(%04x) = %02x\n", Addr, Value));
101          return Value;          return Value;
102  }  }
103    
104  void Wr6502(register word Addr,register byte Value) {  void Wr6502(register word Addr,register byte Value) {
105          printf("Wr6502(%04x,%02x)\n", Addr, Value);          debugf(("Wr6502(%04x,%02x)\n", Addr, Value));
106          dSP;          dSP;
107          ENTER;          ENTER;
108          SAVETMPS;          SAVETMPS;
# Line 69  void Wr6502(register word Addr,register Line 110  void Wr6502(register word Addr,register
110          XPUSHs( sv_2mortal( newSViv( Addr ) ) );          XPUSHs( sv_2mortal( newSViv( Addr ) ) );
111          XPUSHs( sv_2mortal( newSViv( Value ) ) );          XPUSHs( sv_2mortal( newSViv( Value ) ) );
112          PUTBACK;          PUTBACK;
113          call_pv("M6502::write", G_DISCARD );          call_pv("Arch::write", G_DISCARD );
114          FREETMPS;          FREETMPS;
115          LEAVE;          LEAVE;
116  }  }
# Line 77  void Wr6502(register word Addr,register Line 118  void Wr6502(register word Addr,register
118  byte Op6502(register word Addr) {  byte Op6502(register word Addr) {
119          byte Op;          byte Op;
120          Op = mem(Addr);          Op = mem(Addr);
121          printf("Op6502(%04x,%02x)\n", Addr, Op);          debugf(("Op6502(%04x,%02x) PC:%04x\n", Addr, Op, R->PC.W));
122          dump_R;          return Op;
123  }  }
124    
125  /** Loop6502() ***********************************************/  /** Loop6502() ***********************************************/
# Line 88  byte Op6502(register word Addr) { Line 129  byte Op6502(register word Addr) {
129  /** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the     **/  /** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the     **/
130  /** emulation loop.                                         **/  /** emulation loop.                                         **/
131  /************************************ TO BE WRITTEN BY USER **/  /************************************ TO BE WRITTEN BY USER **/
132    
133    int hw_int = INT_NONE;
134    
135  byte Loop6502(register M6502 *R) {  byte Loop6502(register M6502 *R) {
136          printf("Loop6502\n");          debugf(("Loop6502\n"));
137          dump_R;          dump_R;
138          return INT_NONE;          return hw_int;
139  }  }
140    
141  /** Patch6502() **********************************************/  /** Patch6502() **********************************************/
# Line 102  byte Loop6502(register M6502 *R) { Line 146  byte Loop6502(register M6502 *R) {
146  /** or 0 if the opcode was truly illegal.                   **/  /** or 0 if the opcode was truly illegal.                   **/
147  /************************************ TO BE WRITTEN BY USER **/  /************************************ TO BE WRITTEN BY USER **/
148  byte Patch6502(register byte Op,register M6502 *R) {  byte Patch6502(register byte Op,register M6502 *R) {
149          printf("Patch6502(%02x)\n", Op);          debugf(("Patch6502(%02x)\n", Op));
150          dump_R;          dump_R;
151            hw_int = INT_QUIT;
152          return 0;          return 0;
153  }  }
154    
155    void run_forever(void) {
156            printf("entered run_forever\n");
157    
158            R = malloc(sizeof(M6502));
159            if (!R) {
160                    printf("can't alloc %d bytes for M6502", sizeof(M6502));
161                    exit(1);
162            }
163    
164            printf("reset CPU\n");
165            Reset6502(R);
166    
167            printf("call Arch::init\n");
168            dSP;
169            PUSHMARK(SP);
170            call_pv("Arch::init", G_DISCARD | G_NOARGS );
171            FREETMPS;
172            LEAVE;
173    
174            int cycles = 1;
175            while ( cycles ) {
176                    dSP;
177                    PUSHMARK(SP);
178                    call_pv("Arch::cli", G_DISCARD | G_NOARGS );
179                    pull_R(R);
180                    FREETMPS;
181                    LEAVE;
182                    cycles = SvIV( get_sv("M6502::run_for", FALSE) );
183                    if ( cycles > 0 ) {
184                            printf("run CPU for %d cycles\n", cycles);
185                            dump_R;
186                            //Run6502(R);
187                            Exec6502(R, cycles);
188                            dump_R;
189                            push_R(R);
190                            printf("end of %d cycles CPU run\n", cycles);
191                    } else {
192                            printf("no cpu cycles set for run\n");
193                            cycles = 1; // never exit, prevents segfault
194                    }
195            }
196            free(R);
197    }
198    
199  /**  /**
200   * main code   * main code
201   *   *
202   **/   **/
203    
204  int main(int argc, char **argv) {  int main(int argc, char **argv) {
205          char *command_line[] = {"", "-e",          char *command_line[] = {"", "-e", EMU_START };
                 "use M6502; print \"Loaded M6502 module\n\";"};  
206          my_perl = perl_alloc();          my_perl = perl_alloc();
207          perl_construct(my_perl);          perl_construct(my_perl);
208          if (perl_parse(my_perl, NULL, 3, command_line, (char **)NULL)) {          if (perl_parse(my_perl, xs_init, 3, command_line, (char **)NULL)) {
209                  printf("Failed to parse\n");                  printf("Failed to parse initial: %s\n", EMU_START );
210                  return 0;                  return 0;
211          }          }
212          perl_run(my_perl);          perl_run(my_perl);
# Line 126  int main(int argc, char **argv) { Line 214  int main(int argc, char **argv) {
214                  printf("Failed to execute\n");                  printf("Failed to execute\n");
215                  return 0;                  return 0;
216          } else {          } else {
217                  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);  
   
218          }          }
         free(R);  
219          perl_destruct(my_perl);          perl_destruct(my_perl);
220          perl_free(my_perl);          perl_free(my_perl);
221          return 0;          return 0;

Legend:
Removed from v.28  
changed lines
  Added in v.84

  ViewVC Help
Powered by ViewVC 1.1.26