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 |
void update_C_R(void) { |
void update_C_R(void) { |
54 |
LEAVE; |
LEAVE; |
55 |
} |
} |
56 |
|
|
57 |
|
/** Debug6502() **********************************************/ |
58 |
|
|
59 |
|
byte Debug6502(M6502 *R) { |
60 |
|
dump_R; |
61 |
|
return 1; // continue emulation |
62 |
|
} |
63 |
|
|
64 |
|
/** Rd6502()/Wr6502/Op6502() *********************************/ |
65 |
|
/** These functions are called when access to RAM occurs. **/ |
66 |
|
/** They allow to control memory access. Op6502 is the same **/ |
67 |
|
/** as Rd6502, but used to read *opcodes* only, when many **/ |
68 |
|
/** checks can be skipped to make it fast. It is only **/ |
69 |
|
/** required if there is a #define FAST_RDOP. **/ |
70 |
|
/************************************ TO BE WRITTEN BY USER **/ |
71 |
|
|
72 |
|
byte mem(word Addr) { |
73 |
|
byte byte; |
74 |
|
int count; |
75 |
|
debugf(("mem(%04x)", Addr)); |
76 |
|
dSP; |
77 |
|
ENTER; |
78 |
|
SAVETMPS; |
79 |
|
PUSHMARK(SP); |
80 |
|
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
81 |
|
PUTBACK; |
82 |
|
count = call_pv("M6502::_read", G_ARRAY | G_EVAL ); |
83 |
|
debugf(("got %d values", count)); |
84 |
|
SPAGAIN; |
85 |
|
if (SvTRUE(ERRSV)) { |
86 |
|
printf("ERROR: %s", SvPV_nolen( ERRSV ) ); |
87 |
|
exit(1); |
88 |
|
} |
89 |
|
if ( count != 1 ) { |
90 |
|
printf("expect 1 return value, got %d", count); |
91 |
|
exit(1); |
92 |
|
} |
93 |
|
SV *sv; |
94 |
|
sv = POPs; |
95 |
|
byte = SvIV(sv); |
96 |
|
FREETMPS; |
97 |
|
LEAVE; |
98 |
|
debugf(("mem(%04x) = %02x", Addr, byte)); |
99 |
|
return byte; |
100 |
|
} |
101 |
|
|
102 |
|
byte Rd6502(register word Addr) { |
103 |
|
byte Value; |
104 |
|
Value = mem(Addr); |
105 |
|
// Value = 0x42; |
106 |
|
debugf(("Rd6502(%04x) = %02x", Addr, Value)); |
107 |
|
return Value; |
108 |
|
} |
109 |
|
|
110 |
|
void Wr6502(register word Addr,register byte Value) { |
111 |
|
debugf(("Wr6502(%04x,%02x)", Addr, Value)); |
112 |
|
dSP; |
113 |
|
ENTER; |
114 |
|
SAVETMPS; |
115 |
|
PUSHMARK(SP); |
116 |
|
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
117 |
|
XPUSHs( sv_2mortal( newSViv( Value ) ) ); |
118 |
|
PUTBACK; |
119 |
|
call_pv("M6502::_write", G_DISCARD ); |
120 |
|
FREETMPS; |
121 |
|
LEAVE; |
122 |
|
} |
123 |
|
|
124 |
|
byte Op6502(register word Addr) { |
125 |
|
byte Op; |
126 |
|
Op = mem(Addr); |
127 |
|
debugf(("Op6502(%04x,%02x) PC:%04x", Addr, Op, R->PC.W)); |
128 |
|
return Op; |
129 |
|
} |
130 |
|
|
131 |
|
/** Loop6502() ***********************************************/ |
132 |
|
/** 6502 emulation calls this function periodically to **/ |
133 |
|
/** check if the system hardware requires any interrupts. **/ |
134 |
|
/** This function must return one of following values: **/ |
135 |
|
/** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the **/ |
136 |
|
/** emulation loop. **/ |
137 |
|
/************************************ TO BE WRITTEN BY USER **/ |
138 |
|
|
139 |
|
int hw_int = INT_NONE; |
140 |
|
|
141 |
|
byte Loop6502(register M6502 *R) { |
142 |
|
debugf(("Loop6502")); |
143 |
|
dump_R; |
144 |
|
return hw_int; |
145 |
|
} |
146 |
|
|
147 |
|
/** Patch6502() **********************************************/ |
148 |
|
/** Emulation calls this function when it encounters an **/ |
149 |
|
/** unknown opcode. This can be used to patch the code to **/ |
150 |
|
/** emulate BIOS calls, such as disk and tape access. The **/ |
151 |
|
/** function should return 1 if the exception was handled, **/ |
152 |
|
/** or 0 if the opcode was truly illegal. **/ |
153 |
|
/************************************ TO BE WRITTEN BY USER **/ |
154 |
|
byte Patch6502(register byte Op,register M6502 *R) { |
155 |
|
debugf(("Patch6502(%02x)", Op)); |
156 |
|
dump_R; |
157 |
|
hw_int = INT_QUIT; |
158 |
|
return 0; |
159 |
|
} |
160 |
|
|
161 |
|
/*************************************************************/ |
162 |
|
|
163 |
int |
int |
164 |
reset (void) { |
reset (void) { |
165 |
debugf(("M6502::reset called")); |
debugf(("M6502::reset called")); |
167 |
debugf(("allocating space for R")); |
debugf(("allocating space for R")); |
168 |
R = malloc(sizeof(M6502)); |
R = malloc(sizeof(M6502)); |
169 |
if (!R) { |
if (!R) { |
170 |
PerlIO_stderrf("can't alloc %d bytes for M6502", sizeof(M6502)); |
PerlIO_stdoutf("can't alloc %d bytes for M6502", sizeof(M6502)); |
171 |
exit(1); |
exit(1); |
172 |
} |
} |
173 |
} |
} |
178 |
return 1; |
return 1; |
179 |
} |
} |
180 |
|
|
181 |
|
void exec(int cycles) { |
182 |
|
debugf(("exec for %d cycles", cycles)); |
183 |
|
|
184 |
|
if (!R) reset(); |
185 |
|
|
186 |
|
update_C_R(); |
187 |
|
Exec6502(R, cycles); |
188 |
|
update_perl_R(); |
189 |
|
debugf(("end of %d cycles CPU run\n", cycles)); |
190 |
|
} |
191 |
|
|
192 |
int set_debug(int state) { |
int set_debug(int state) { |
193 |
debug = state; |
debug = state; |
194 |
return debug; |
return debug; |