/[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 40 by dpavlin, Tue Jul 31 08:41:06 2007 UTC revision 85 by dpavlin, Wed Aug 1 22:02:09 2007 UTC
# Line 3  Line 3 
3  #include "M6502.h"  #include "M6502.h"
4  #include "config.h"  #include "config.h"
5    
 #if DEBUGF  
 #define debugf(x)  do {         \  
         PerlIO_stdoutf x ;      \  
 } while (0)  
 #else  
 #define debugf(x)  
 #endif  
   
6  static PerlInterpreter *my_perl;  static PerlInterpreter *my_perl;
7    
8  static M6502 *R;  static M6502 *R;
9    
 #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  
   
10  void pull_R(M6502 *R) {  void pull_R(M6502 *R) {
11          R->A = SvIV( get_sv("M6502::A", FALSE) );          R->A = SvIV( get_sv("M6502::A", FALSE) );
12          R->P = SvIV( get_sv("M6502::P", FALSE) );          R->P = SvIV( get_sv("M6502::P", FALSE) );
# Line 29  void pull_R(M6502 *R) { Line 15  void pull_R(M6502 *R) {
15          R->S = SvIV( get_sv("M6502::S", FALSE) );          R->S = SvIV( get_sv("M6502::S", FALSE) );
16          R->PC.W = SvIV( get_sv("M6502::PC", FALSE) );          R->PC.W = SvIV( get_sv("M6502::PC", FALSE) );
17          R->IPeriod = SvIV( get_sv("M6502::IPeriod", FALSE) );          R->IPeriod = SvIV( get_sv("M6502::IPeriod", FALSE) );
18          // ICount IRequest IAutoReset TrapBadOps Trap Trace          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");          printf("pull_R finished\n");
24          dump_R;          dump_R;
25  }  }
# Line 45  void push_R(M6502 *R) { Line 35  void push_R(M6502 *R) {
35          XPUSHs( sv_2mortal( newSViv( R->Y ) ) );          XPUSHs( sv_2mortal( newSViv( R->Y ) ) );
36          XPUSHs( sv_2mortal( newSViv( R->S ) ) );          XPUSHs( sv_2mortal( newSViv( R->S ) ) );
37          XPUSHs( sv_2mortal( newSViv( R->PC.W ) ) );          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;          PUTBACK;
46          call_pv("M6502::push_R", G_DISCARD );          call_pv("M6502::push_R", G_DISCARD );
47          printf("push_R called\n");          printf("push_R called\n");
# Line 53  void push_R(M6502 *R) { Line 50  void push_R(M6502 *R) {
50          LEAVE;          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() *********************************/
59  /** These functions are called when access to RAM occurs.   **/  /** These functions are called when access to RAM occurs.   **/
60  /** They allow to control memory access. Op6502 is the same **/  /** They allow to control memory access. Op6502 is the same **/
# Line 64  void push_R(M6502 *R) { Line 66  void push_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)", 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("Arch::read", G_ARRAY );          count = call_pv("M6502::read", G_ARRAY | G_EVAL );
77            debugf(("got %d values", 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          }          }
         //debugf(("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          //debugf(("mem(%04x) = %02x\n", Addr, byte));          debugf(("mem(%04x) = %02x", 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          debugf(("Rd6502(%04x) = %02x\n", Addr, Value));          Value = 0x42;
100            debugf(("Rd6502(%04x) = %02x", 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          debugf(("Wr6502(%04x,%02x)\n", Addr, Value));          debugf(("Wr6502(%04x,%02x)", Addr, Value));
106          dSP;          dSP;
107          ENTER;          ENTER;
108          SAVETMPS;          SAVETMPS;
# Line 110  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          debugf(("Op6502(%04x,%02x) PC:%04x\n", Addr, Op, R->PC.W));          debugf(("Op6502(%04x,%02x) PC:%04x", Addr, Op, R->PC.W));
122          return Op;          return Op;
123  }  }
124    
# Line 121  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          debugf(("Loop6502\n"));          debugf(("Loop6502"));
137          dump_R;          dump_R;
138          return INT_NONE;          return hw_int;
139  }  }
140    
141  /** Patch6502() **********************************************/  /** Patch6502() **********************************************/
# Line 135  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          debugf(("Patch6502(%02x)\n", Op));          debugf(("Patch6502(%02x)", 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   *   *
# Line 158  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);  
                 }  
   
                 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");  
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.40  
changed lines
  Added in v.85

  ViewVC Help
Powered by ViewVC 1.1.26