/[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 87 by dpavlin, Thu Aug 2 11:08:10 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 );  byte Debug6502(M6502 *R) {
   
 void update_R(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  
11          dump_R;          dump_R;
12            return 1; // continue emulation
13  }  }
14    
15  /** Rd6502()/Wr6502/Op6502() *********************************/  /** Rd6502()/Wr6502/Op6502() *********************************/
# Line 29  void update_R(M6502 *R) { Line 20  void update_R(M6502 *R) {
20  /** required if there is a #define FAST_RDOP.               **/  /** required if there is a #define FAST_RDOP.               **/
21  /************************************ TO BE WRITTEN BY USER **/  /************************************ TO BE WRITTEN BY USER **/
22    
23  byte Rd6502(register word Addr) {  byte mem(word Addr) {
24          byte Value;          byte byte;
         Value = 0x42;  
         printf("Rd6502(%04x,%02x)\n", Addr, Value);  
   
25          int count;          int count;
26            debugf(("mem(%04x)", Addr));
27          dSP;          dSP;
28          ENTER;          ENTER;
29          SAVETMPS;          SAVETMPS;
30          PUSHMARK(SP);          PUSHMARK(SP);
31          XPUSHs( sv_2mortal( newSViv( Value ) ) );          XPUSHs( sv_2mortal( newSViv( Addr ) ) );
32          PUTBACK;          PUTBACK;
33          count = call_pv("M6502::read", G_ARRAY );          count = call_pv("M6502::read", G_ARRAY | G_EVAL );
34            debugf(("got %d values", count));
35            SPAGAIN;
36            if (SvTRUE(ERRSV)) {
37                    printf("ERROR: %s", SvPV_nolen( ERRSV ) );
38                    exit(1);
39            }
40          if ( count != 1 ) {          if ( count != 1 ) {
41                  printf("expect 1 return value, got %d", count);                  printf("expect 1 return value, got %d", count);
42                  exit(1);                  exit(1);
43          }          }
         printf("got %d values\n", count);  
         SPAGAIN;  
44          SV *sv;          SV *sv;
45          sv = POPs;          sv = POPs;
46          Value = SvIV(sv);          byte = SvIV(sv);
 //      Value = savepv(SvPV_nolen(POPs));  
47          FREETMPS;          FREETMPS;
48          LEAVE;          LEAVE;
49          printf("Rd6502(%04x) = %02x\n", Addr, Value);          debugf(("mem(%04x) = %02x", Addr, byte));
50            return byte;
51    }
52    
53    byte Rd6502(register word Addr) {
54            byte Value;
55    //      Value = mem(Addr);
56            Value = 0x42;
57            debugf(("Rd6502(%04x) = %02x", Addr, Value));
58          return Value;          return Value;
59  }  }
60    
61  void Wr6502(register word Addr,register byte Value) {  void Wr6502(register word Addr,register byte Value) {
62          printf("Wr6502(%04x,%02x)\n", Addr, Value);          debugf(("Wr6502(%04x,%02x)", Addr, Value));
63          dSP;          dSP;
64          ENTER;          ENTER;
65          SAVETMPS;          SAVETMPS;
# Line 67  void Wr6502(register word Addr,register Line 67  void Wr6502(register word Addr,register
67          XPUSHs( sv_2mortal( newSViv( Addr ) ) );          XPUSHs( sv_2mortal( newSViv( Addr ) ) );
68          XPUSHs( sv_2mortal( newSViv( Value ) ) );          XPUSHs( sv_2mortal( newSViv( Value ) ) );
69          PUTBACK;          PUTBACK;
70          call_pv("M6502::write", G_DISCARD );          call_pv("Arch::write", G_DISCARD );
71          FREETMPS;          FREETMPS;
72          LEAVE;          LEAVE;
73  }  }
74    
75  byte Op6502(register word Addr) {  byte Op6502(register word Addr) {
76          byte Op;          byte Op;
77          Op = 0xff;          Op = mem(Addr);
78          printf("Op6502(%04x,%02x)\n", Addr, Op);          debugf(("Op6502(%04x,%02x) PC:%04x", Addr, Op, R->PC.W));
79          dump_R;          return Op;
80  }  }
81    
82  /** Loop6502() ***********************************************/  /** Loop6502() ***********************************************/
# Line 86  byte Op6502(register word Addr) { Line 86  byte Op6502(register word Addr) {
86  /** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the     **/  /** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the     **/
87  /** emulation loop.                                         **/  /** emulation loop.                                         **/
88  /************************************ TO BE WRITTEN BY USER **/  /************************************ TO BE WRITTEN BY USER **/
89    
90    int hw_int = INT_NONE;
91    
92  byte Loop6502(register M6502 *R) {  byte Loop6502(register M6502 *R) {
93          printf("Loop6502\n");          debugf(("Loop6502"));
94          dump_R;          dump_R;
95            return hw_int;
96  }  }
97    
98  /** Patch6502() **********************************************/  /** Patch6502() **********************************************/
# Line 99  byte Loop6502(register M6502 *R) { Line 103  byte Loop6502(register M6502 *R) {
103  /** or 0 if the opcode was truly illegal.                   **/  /** or 0 if the opcode was truly illegal.                   **/
104  /************************************ TO BE WRITTEN BY USER **/  /************************************ TO BE WRITTEN BY USER **/
105  byte Patch6502(register byte Op,register M6502 *R) {  byte Patch6502(register byte Op,register M6502 *R) {
106          printf("Patch6502(%02x)\n", Op);          debugf(("Patch6502(%02x)", Op));
107          dump_R;          dump_R;
108            hw_int = INT_QUIT;
109            return 0;
110    }
111    
112    void run_forever(void) {
113            printf("entered run_forever\n");
114    
115            R = malloc(sizeof(M6502));
116            if (!R) {
117                    printf("can't alloc %d bytes for M6502", sizeof(M6502));
118                    exit(1);
119            }
120    
121            printf("reset CPU\n");
122            Reset6502(R);
123    
124            printf("call Arch::init\n");
125            dSP;
126            PUSHMARK(SP);
127            call_pv("Arch::init", G_DISCARD | G_NOARGS );
128            FREETMPS;
129            LEAVE;
130    
131            int cycles = 1;
132            while ( cycles ) {
133                    dSP;
134                    PUSHMARK(SP);
135                    call_pv("Arch::cli", G_DISCARD | G_NOARGS );
136                    pull_R;
137                    FREETMPS;
138                    LEAVE;
139                    cycles = SvIV( get_sv("M6502::run_for", FALSE) );
140                    if ( cycles > 0 ) {
141                            printf("run CPU for %d cycles\n", cycles);
142                            dump_R;
143                            //Run6502(R);
144                            Exec6502(R, cycles);
145                            dump_R;
146                            push_R;
147                            printf("end of %d cycles CPU run\n", cycles);
148                    } else {
149                            printf("no cpu cycles set for run\n");
150                            cycles = 1; // never exit, prevents segfault
151                    }
152            }
153            free(R);
154  }  }
155    
156  /**  /**
# Line 109  byte Patch6502(register byte Op,register Line 159  byte Patch6502(register byte Op,register
159   **/   **/
160    
161  int main(int argc, char **argv) {  int main(int argc, char **argv) {
162          char *command_line[] = {"", "-e",          char *command_line[] = {"", "-e", EMU_START };
                 "use M6502; print \"Loaded M6502 module\n\";"};  
163          my_perl = perl_alloc();          my_perl = perl_alloc();
164          perl_construct(my_perl);          perl_construct(my_perl);
165          if (perl_parse(my_perl, NULL, 3, command_line, (char **)NULL)) {          if (perl_parse(my_perl, xs_init, 3, command_line, (char **)NULL)) {
166                  printf("Failed to parse\n");                  printf("Failed to parse initial: %s\n", EMU_START );
167                  return 0;                  return 0;
168          }          }
169          perl_run(my_perl);          perl_run(my_perl);
# Line 122  int main(int argc, char **argv) { Line 171  int main(int argc, char **argv) {
171                  printf("Failed to execute\n");                  printf("Failed to execute\n");
172                  return 0;                  return 0;
173          } else {          } else {
174                  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);  
   
175          }          }
         free(R);  
176          perl_destruct(my_perl);          perl_destruct(my_perl);
177          perl_free(my_perl);          perl_free(my_perl);
178          return 0;          return 0;

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

  ViewVC Help
Powered by ViewVC 1.1.26