3 |
#include "M6502.h" |
#include "M6502.h" |
4 |
#include "config.h" |
#include "config.h" |
5 |
|
|
6 |
|
#if DEBUGF |
7 |
|
#define debugf(x) do { \ |
8 |
|
PerlIO_stdoutf("#> "); \ |
9 |
|
PerlIO_stdoutf x ; \ |
10 |
|
} while (0) |
11 |
|
#else |
12 |
|
#define debugf(x) |
13 |
|
#endif |
14 |
|
|
15 |
static PerlInterpreter *my_perl; |
static PerlInterpreter *my_perl; |
16 |
|
|
17 |
static M6502 *R; |
static M6502 *R; |
18 |
|
|
19 |
|
#if DUMP_R |
20 |
#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 ); |
21 |
|
#else |
22 |
|
#define dump_R |
23 |
|
#endif |
24 |
|
|
25 |
|
void pull_R(M6502 *R) { |
26 |
|
R->A = SvIV( get_sv("M6502::A", FALSE) ); |
27 |
|
R->P = SvIV( get_sv("M6502::P", FALSE) ); |
28 |
|
R->X = SvIV( get_sv("M6502::X", FALSE) ); |
29 |
|
R->Y = SvIV( get_sv("M6502::Y", FALSE) ); |
30 |
|
R->S = SvIV( get_sv("M6502::S", FALSE) ); |
31 |
|
R->PC.W = SvIV( get_sv("M6502::PC", FALSE) ); |
32 |
|
R->IPeriod = SvIV( get_sv("M6502::IPeriod", FALSE) ); |
33 |
|
R->IRequest = SvIV( get_sv("M6502::IRequest", FALSE) ); |
34 |
|
R->IAutoReset = SvIV( get_sv("M6502::IAutoReset", FALSE) ); |
35 |
|
R->TrapBadOps = SvIV( get_sv("M6502::TrapBadOps", FALSE) ); |
36 |
|
R->Trap = SvIV( get_sv("M6502::Trap", FALSE) ); |
37 |
|
R->Trace = SvIV( get_sv("M6502::Trace", FALSE) ); |
38 |
|
printf("pull_R finished\n"); |
39 |
|
dump_R; |
40 |
|
} |
41 |
|
|
42 |
void update_R(M6502 *R) { |
void push_R(M6502 *R) { |
43 |
R->A = atoi( SvPV_nolen( get_sv("M6502::A", FALSE) ) ); |
dSP; |
44 |
R->P = atoi( SvPV_nolen( get_sv("M6502::P", FALSE) ) ); |
ENTER; |
45 |
R->X = atoi( SvPV_nolen( get_sv("M6502::X", FALSE) ) ); |
SAVETMPS; |
46 |
R->Y = atoi( SvPV_nolen( get_sv("M6502::Y", FALSE) ) ); |
PUSHMARK(SP); |
47 |
R->S = atoi( SvPV_nolen( get_sv("M6502::S", FALSE) ) ); |
XPUSHs( sv_2mortal( newSViv( R->A ) ) ); |
48 |
R->PC.W = atoi( SvPV_nolen( get_sv("M6502::PC", FALSE) ) ); |
XPUSHs( sv_2mortal( newSViv( R->P ) ) ); |
49 |
R->IPeriod = atoi( SvPV_nolen( get_sv("M6502::IPeriod", FALSE) ) ); |
XPUSHs( sv_2mortal( newSViv( R->X ) ) ); |
50 |
// ICount IRequest IAutoReset TrapBadOps Trap Trace |
XPUSHs( sv_2mortal( newSViv( R->Y ) ) ); |
51 |
|
XPUSHs( sv_2mortal( newSViv( R->S ) ) ); |
52 |
|
XPUSHs( sv_2mortal( newSViv( R->PC.W ) ) ); |
53 |
|
XPUSHs( sv_2mortal( newSViv( R->IPeriod ) ) ); |
54 |
|
XPUSHs( sv_2mortal( newSViv( R->ICount ) ) ); |
55 |
|
XPUSHs( sv_2mortal( newSViv( R->IRequest ) ) ); |
56 |
|
XPUSHs( sv_2mortal( newSViv( R->IAutoReset ) ) ); |
57 |
|
XPUSHs( sv_2mortal( newSViv( R->TrapBadOps ) ) ); |
58 |
|
XPUSHs( sv_2mortal( newSViv( R->Trap ) ) ); |
59 |
|
XPUSHs( sv_2mortal( newSViv( R->Trace ) ) ); |
60 |
|
PUTBACK; |
61 |
|
call_pv("M6502::push_R", G_DISCARD ); |
62 |
|
printf("push_R called\n"); |
63 |
dump_R; |
dump_R; |
64 |
|
FREETMPS; |
65 |
|
LEAVE; |
66 |
} |
} |
67 |
|
|
68 |
/** Rd6502()/Wr6502/Op6502() *********************************/ |
/** Rd6502()/Wr6502/Op6502() *********************************/ |
82 |
PUSHMARK(SP); |
PUSHMARK(SP); |
83 |
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
84 |
PUTBACK; |
PUTBACK; |
85 |
count = call_pv("M6502::read", G_ARRAY ); |
count = call_pv("Arch::read", G_ARRAY ); |
86 |
if ( count != 1 ) { |
if ( count != 1 ) { |
87 |
printf("expect 1 return value, got %d", count); |
printf("expect 1 return value, got %d", count); |
88 |
exit(1); |
exit(1); |
89 |
} |
} |
90 |
printf("got %d values\n", count); |
//debugf(("got %d values\n", count)); |
91 |
SPAGAIN; |
SPAGAIN; |
92 |
SV *sv; |
SV *sv; |
93 |
sv = POPs; |
sv = POPs; |
94 |
byte = SvIV(sv); |
byte = SvIV(sv); |
95 |
FREETMPS; |
FREETMPS; |
96 |
LEAVE; |
LEAVE; |
97 |
printf("Rd6502(%04x) = %02x\n", Addr, byte); |
//debugf(("mem(%04x) = %02x\n", Addr, byte)); |
98 |
return byte; |
return byte; |
99 |
} |
} |
100 |
|
|
101 |
byte Rd6502(register word Addr) { |
byte Rd6502(register word Addr) { |
102 |
byte Value; |
byte Value; |
103 |
Value = mem(Addr); |
Value = mem(Addr); |
104 |
|
debugf(("Rd6502(%04x) = %02x\n", Addr, Value)); |
105 |
return Value; |
return Value; |
106 |
} |
} |
107 |
|
|
108 |
void Wr6502(register word Addr,register byte Value) { |
void Wr6502(register word Addr,register byte Value) { |
109 |
printf("Wr6502(%04x,%02x)\n", Addr, Value); |
debugf(("Wr6502(%04x,%02x)\n", Addr, Value)); |
110 |
dSP; |
dSP; |
111 |
ENTER; |
ENTER; |
112 |
SAVETMPS; |
SAVETMPS; |
114 |
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
115 |
XPUSHs( sv_2mortal( newSViv( Value ) ) ); |
XPUSHs( sv_2mortal( newSViv( Value ) ) ); |
116 |
PUTBACK; |
PUTBACK; |
117 |
call_pv("M6502::write", G_DISCARD ); |
call_pv("Arch::write", G_DISCARD ); |
118 |
FREETMPS; |
FREETMPS; |
119 |
LEAVE; |
LEAVE; |
120 |
} |
} |
122 |
byte Op6502(register word Addr) { |
byte Op6502(register word Addr) { |
123 |
byte Op; |
byte Op; |
124 |
Op = mem(Addr); |
Op = mem(Addr); |
125 |
printf("Op6502(%04x,%02x)\n", Addr, Op); |
debugf(("Op6502(%04x,%02x) PC:%04x\n", Addr, Op, R->PC.W)); |
126 |
dump_R; |
return Op; |
127 |
} |
} |
128 |
|
|
129 |
/** Loop6502() ***********************************************/ |
/** Loop6502() ***********************************************/ |
133 |
/** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the **/ |
/** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the **/ |
134 |
/** emulation loop. **/ |
/** emulation loop. **/ |
135 |
/************************************ TO BE WRITTEN BY USER **/ |
/************************************ TO BE WRITTEN BY USER **/ |
136 |
|
|
137 |
|
int hw_int = INT_NONE; |
138 |
|
|
139 |
byte Loop6502(register M6502 *R) { |
byte Loop6502(register M6502 *R) { |
140 |
printf("Loop6502\n"); |
debugf(("Loop6502\n")); |
141 |
dump_R; |
dump_R; |
142 |
return INT_NONE; |
return hw_int; |
143 |
} |
} |
144 |
|
|
145 |
/** Patch6502() **********************************************/ |
/** Patch6502() **********************************************/ |
150 |
/** or 0 if the opcode was truly illegal. **/ |
/** or 0 if the opcode was truly illegal. **/ |
151 |
/************************************ TO BE WRITTEN BY USER **/ |
/************************************ TO BE WRITTEN BY USER **/ |
152 |
byte Patch6502(register byte Op,register M6502 *R) { |
byte Patch6502(register byte Op,register M6502 *R) { |
153 |
printf("Patch6502(%02x)\n", Op); |
debugf(("Patch6502(%02x)\n", Op)); |
154 |
dump_R; |
dump_R; |
155 |
|
hw_int = INT_QUIT; |
156 |
return 0; |
return 0; |
157 |
} |
} |
158 |
|
|
159 |
|
void run_forever(void) { |
160 |
|
printf("entered run_forever\n"); |
161 |
|
|
162 |
|
R = malloc(sizeof(M6502)); |
163 |
|
if (!R) { |
164 |
|
printf("can't alloc %d bytes for M6502", sizeof(M6502)); |
165 |
|
exit(1); |
166 |
|
} |
167 |
|
|
168 |
|
printf("reset CPU\n"); |
169 |
|
Reset6502(R); |
170 |
|
|
171 |
|
printf("call Arch::init\n"); |
172 |
|
dSP; |
173 |
|
PUSHMARK(SP); |
174 |
|
call_pv("Arch::init", G_DISCARD | G_NOARGS ); |
175 |
|
FREETMPS; |
176 |
|
LEAVE; |
177 |
|
|
178 |
|
int cycles = 1; |
179 |
|
while ( cycles ) { |
180 |
|
dSP; |
181 |
|
PUSHMARK(SP); |
182 |
|
call_pv("Arch::cli", G_DISCARD | G_NOARGS ); |
183 |
|
pull_R(R); |
184 |
|
FREETMPS; |
185 |
|
LEAVE; |
186 |
|
cycles = SvIV( get_sv("M6502::run_for", FALSE) ); |
187 |
|
if ( cycles > 0 ) { |
188 |
|
printf("run CPU for %d cycles\n", cycles); |
189 |
|
dump_R; |
190 |
|
//Run6502(R); |
191 |
|
Exec6502(R, cycles); |
192 |
|
dump_R; |
193 |
|
push_R(R); |
194 |
|
printf("end of %d cycles CPU run\n", cycles); |
195 |
|
} else { |
196 |
|
printf("no cpu cycles set for run\n"); |
197 |
|
cycles = 1; // never exit, prevents segfault |
198 |
|
} |
199 |
|
} |
200 |
|
free(R); |
201 |
|
} |
202 |
|
|
203 |
/** |
/** |
204 |
* main code |
* main code |
205 |
* |
* |
218 |
printf("Failed to execute\n"); |
printf("Failed to execute\n"); |
219 |
return 0; |
return 0; |
220 |
} else { |
} else { |
221 |
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); |
|
|
|
|
222 |
} |
} |
|
free(R); |
|
223 |
perl_destruct(my_perl); |
perl_destruct(my_perl); |
224 |
perl_free(my_perl); |
perl_free(my_perl); |
225 |
return 0; |
return 0; |