/[VRac]/M6502/M6502.xs
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/M6502.xs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 87 by dpavlin, Thu Aug 2 11:08:10 2007 UTC revision 197 by dpavlin, Sun Apr 13 00:44:30 2008 UTC
# Line 7  Line 7 
7  #include "M6502.h"  #include "M6502.h"
8  #include "config.h"  #include "config.h"
9    
10  M6502 *R;  M6502 *R = NULL;
11  int debug = 0;  int debug = 0;
12    
13    // same as memory size
14    #define CACHE_SIZE 0xffff
15    byte opCache[CACHE_SIZE];
16    
17    #define CALLBACK_READ_SKIP      0x00
18    #define CALLBACK_READ_ONCE      0x01
19    #define CALLBACK_READ_ALWAYS    0x02
20    #define CALLBACK_READ_MASK      0x0f
21    #define CALLBACK_WRITE_SKIP     0x00
22    #define CALLBACK_WRITE_ONCE     0x10
23    #define CALLBACK_WRITE_ALWAYS   0x20
24    #define CALLBACK_WRITE_MASK     0xf0
25    byte perlCallBack[CACHE_SIZE];
26    
27  void update_C_R(void) {  void update_C_R(void) {
28          R->A = SvIV( get_sv("M6502::A", FALSE) );          R->A = SvIV( get_sv("M6502::A", FALSE) );
29          R->P = SvIV( get_sv("M6502::P", FALSE) );          R->P = SvIV( get_sv("M6502::P", FALSE) );
# Line 54  void update_perl_R(void) { Line 68  void update_perl_R(void) {
68          LEAVE;          LEAVE;
69  }  }
70    
71    /** Debug6502() **********************************************/
72    
73    byte Debug6502(M6502 *R) {
74            dump_R;
75            return 1; // continue emulation
76    }
77    
78    /** Rd6502()/Wr6502/Op6502() *********************************/
79    /** These functions are called when access to RAM occurs.   **/
80    /** They allow to control memory access. Op6502 is the same **/
81    /** as Rd6502, but used to read *opcodes* only, when many   **/
82    /** checks can be skipped to make it fast. It is only       **/
83    /** required if there is a #define FAST_RDOP.               **/
84    /************************************ TO BE WRITTEN BY USER **/
85    
86    byte mem(register word Addr) {
87    
88            if ( perlCallBack[Addr] & CALLBACK_READ_MASK == CALLBACK_READ_SKIP )
89                    return opCache[Addr];
90            if ( perlCallBack[Addr] & CALLBACK_READ_MASK == CALLBACK_READ_ONCE )
91                    perlCallBack[Addr] = perlCallBack[Addr] & CALLBACK_WRITE_MASK | CALLBACK_READ_SKIP;
92    
93            byte byte;
94            int count;
95            debugf(("mem(%04x)", Addr));
96            dSP;
97            ENTER;
98            SAVETMPS;
99            PUSHMARK(SP);
100            XPUSHs( sv_2mortal( newSViv( Addr ) ) );
101            PUTBACK;
102            count = call_pv("M6502::_read", G_ARRAY | G_EVAL );
103            debugf(("got %d values", count));
104            SPAGAIN;
105            if (SvTRUE(ERRSV)) {
106                    printf("ERROR: %s", SvPV_nolen( ERRSV ) );
107                    exit(1);
108            }
109            if ( count != 1 ) {
110                    printf("expect 1 return value, got %d", count);
111                    exit(1);
112            }
113            SV *sv;
114            sv = POPs;
115            byte = SvIV(sv);
116            FREETMPS;
117            LEAVE;
118            debugf(("mem(%04x) = %02x", Addr, byte));
119            opCache[Addr] = byte;
120            return byte;
121    }
122    
123    byte Rd6502(register word Addr) {
124            byte Value;
125            Value = mem(Addr);
126            debugf(("Rd6502(%04x) = %02x", Addr, Value));
127            return Value;
128    }
129    
130    void Wr6502(register word Addr,register byte Value) {
131            debugf(("Wr6502(%04x,%02x)", Addr, Value));
132            if ( perlCallBack[Addr] & CALLBACK_WRITE_MASK == CALLBACK_WRITE_SKIP && opCache[Addr] == Value ) {
133                    debugf(("skipped perl callback, same value"));
134                    return;
135            }
136            opCache[Addr] = Value;
137            if ( perlCallBack[Addr] & CALLBACK_WRITE_MASK == CALLBACK_WRITE_SKIP ) return;
138            if ( perlCallBack[Addr] & CALLBACK_WRITE_MASK == CALLBACK_WRITE_ONCE )
139                    perlCallBack[Addr] = perlCallBack[Addr] & CALLBACK_READ_MASK | CALLBACK_WRITE_SKIP;
140            dSP;
141            ENTER;
142            SAVETMPS;
143            PUSHMARK(SP);
144            XPUSHs( sv_2mortal( newSViv( Addr ) ) );
145            XPUSHs( sv_2mortal( newSViv( Value ) ) );
146            PUTBACK;
147            call_pv("M6502::_write", G_DISCARD );
148            FREETMPS;
149            LEAVE;
150    }
151    
152    byte Op6502(register word Addr) {
153            byte Op;
154            Op = mem(Addr);
155            debugf(("Op6502(%04x,%02x) PC:%04x", Addr, Op, R->PC.W));
156            return Op;
157    }
158    
159    /** Loop6502() ***********************************************/
160    /** 6502 emulation calls this function periodically to      **/
161    /** check if the system hardware requires any interrupts.   **/
162    /** This function must return one of following values:      **/
163    /** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the     **/
164    /** emulation loop.                                         **/
165    /************************************ TO BE WRITTEN BY USER **/
166    
167    int hw_int = INT_NONE;
168    
169    byte Loop6502(register M6502 *R) {
170            debugf(("Loop6502"));
171            dump_R;
172            return hw_int;
173    }
174    
175    /** Patch6502() **********************************************/
176    /** Emulation calls this function when it encounters an     **/
177    /** unknown opcode. This can be used to patch the code to   **/
178    /** emulate BIOS calls, such as disk and tape access. The   **/
179    /** function should return 1 if the exception was handled,  **/
180    /** or 0 if the opcode was truly illegal.                   **/
181    /************************************ TO BE WRITTEN BY USER **/
182    byte Patch6502(register byte Op,register M6502 *R) {
183            debugf(("Patch6502(%02x)", Op));
184            dump_R;
185            hw_int = INT_QUIT;
186            return 0;
187    }
188    
189    /*************************************************************/
190    
191  int  int
192  reset (void) {  reset (void) {
193          debugf(("M6502::reset called"));          debugf(("M6502::reset called"));
# Line 61  reset (void) { Line 195  reset (void) {
195                  debugf(("allocating space for R"));                  debugf(("allocating space for R"));
196                  R = malloc(sizeof(M6502));                  R = malloc(sizeof(M6502));
197                  if (!R) {                  if (!R) {
198                          PerlIO_stderrf("can't alloc %d bytes for M6502", sizeof(M6502));                          PerlIO_stdoutf("can't alloc %d bytes for M6502", sizeof(M6502));
199                          exit(1);                          exit(1);
200                  }                  }
201                    memset( opCache, 0, CACHE_SIZE );
202                    memset( perlCallBack, CALLBACK_READ_ALWAYS | CALLBACK_WRITE_ALWAYS, CACHE_SIZE );
203          }          }
204          Reset6502(R);          Reset6502(R);
205          debugf(("Reset6502 over"));          debugf(("Reset6502 over"));
# Line 72  reset (void) { Line 208  reset (void) {
208          return 1;          return 1;
209  }  }
210    
211    int exec(int cycles) {
212            int left;
213            debugf(("exec for %d cycles", cycles));
214    
215            if (!R) reset();
216    
217            update_C_R();
218            left = Exec6502(R, cycles);
219            update_perl_R();
220            debugf(("end of %d cycles CPU run\n", cycles));
221            return left;
222    }
223    
224  int set_debug(int state) {  int set_debug(int state) {
225          debug = state;          debug = state;
226          return debug;          return debug;
# Line 81  int get_debug(void) { Line 230  int get_debug(void) {
230          return debug;          return debug;
231  }  }
232    
233    /* FIXME somehow check if Addr will fit in int on current platform */
234    void set_read_callback(int Addr) {
235            perlCallBack[Addr] == perlCallBack[Addr] & CALLBACK_WRITE_MASK | CALLBACK_READ_ALWAYS;
236    }
237    
238    void set_write_callback(int Addr) {
239            perlCallBack[Addr] == perlCallBack[Addr] & CALLBACK_READ_MASK | CALLBACK_WRITE_ALWAYS;
240    }
241    
242    /* we fake here, since we will need to call perl at least once to get initial value... */
243    int disable_all_callbacks(void) {
244            memset( perlCallBack, CALLBACK_READ_ONCE | CALLBACK_WRITE_ONCE, CACHE_SIZE );
245            return perlCallBack[0];
246    }
247    
248    
249  MODULE = M6502          PACKAGE = M6502  MODULE = M6502          PACKAGE = M6502
250    
251  PROTOTYPES: DISABLE  PROTOTYPES: DISABLE
# Line 100  update_C_R() Line 265  update_C_R()
265  void  void
266  update_perl_R()  update_perl_R()
267    
268    int
269    exec(int cycles)
270    
271    void
272    set_read_callback(int Addr)
273    
274    void
275    set_write_callback(int Addr)
276    
277    int
278    disable_all_callbacks()
279    

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

  ViewVC Help
Powered by ViewVC 1.1.26