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

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

  ViewVC Help
Powered by ViewVC 1.1.26