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 |
void update_R(M6502 *R) { |
#define dump_R |
23 |
R->A = atoi( SvPV_nolen( get_sv("M6502::A", FALSE) ) ); |
#endif |
24 |
R->P = atoi( SvPV_nolen( get_sv("M6502::P", FALSE) ) ); |
|
25 |
R->X = atoi( SvPV_nolen( get_sv("M6502::X", FALSE) ) ); |
void pull_R(M6502 *R) { |
26 |
R->Y = atoi( SvPV_nolen( get_sv("M6502::Y", FALSE) ) ); |
R->A = SvIV( get_sv("M6502::A", FALSE) ); |
27 |
R->S = atoi( SvPV_nolen( get_sv("M6502::S", FALSE) ) ); |
R->P = SvIV( get_sv("M6502::P", FALSE) ); |
28 |
R->PC.W = atoi( SvPV_nolen( get_sv("M6502::PC", FALSE) ) ); |
R->X = SvIV( get_sv("M6502::X", FALSE) ); |
29 |
R->IPeriod = atoi( SvPV_nolen( get_sv("M6502::IPeriod", FALSE) ) ); |
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 |
// ICount IRequest IAutoReset TrapBadOps Trap Trace |
// ICount IRequest IAutoReset TrapBadOps Trap Trace |
34 |
|
printf("pull_R finished\n"); |
35 |
dump_R; |
dump_R; |
36 |
} |
} |
37 |
|
|
38 |
|
void push_R(M6502 *R) { |
39 |
|
dSP; |
40 |
|
ENTER; |
41 |
|
SAVETMPS; |
42 |
|
PUSHMARK(SP); |
43 |
|
XPUSHs( sv_2mortal( newSViv( R->A ) ) ); |
44 |
|
XPUSHs( sv_2mortal( newSViv( R->P ) ) ); |
45 |
|
XPUSHs( sv_2mortal( newSViv( R->X ) ) ); |
46 |
|
XPUSHs( sv_2mortal( newSViv( R->Y ) ) ); |
47 |
|
XPUSHs( sv_2mortal( newSViv( R->S ) ) ); |
48 |
|
XPUSHs( sv_2mortal( newSViv( R->PC.W ) ) ); |
49 |
|
PUTBACK; |
50 |
|
call_pv("M6502::push_R", G_DISCARD ); |
51 |
|
printf("push_R called\n"); |
52 |
|
dump_R; |
53 |
|
FREETMPS; |
54 |
|
LEAVE; |
55 |
|
} |
56 |
|
|
57 |
/** Rd6502()/Wr6502/Op6502() *********************************/ |
/** Rd6502()/Wr6502/Op6502() *********************************/ |
58 |
/** These functions are called when access to RAM occurs. **/ |
/** These functions are called when access to RAM occurs. **/ |
59 |
/** They allow to control memory access. Op6502 is the same **/ |
/** They allow to control memory access. Op6502 is the same **/ |
71 |
PUSHMARK(SP); |
PUSHMARK(SP); |
72 |
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
73 |
PUTBACK; |
PUTBACK; |
74 |
count = call_pv("M6502::read", G_ARRAY ); |
count = call_pv("Arch::read", G_ARRAY ); |
75 |
if ( count != 1 ) { |
if ( count != 1 ) { |
76 |
printf("expect 1 return value, got %d", count); |
printf("expect 1 return value, got %d", count); |
77 |
exit(1); |
exit(1); |
78 |
} |
} |
79 |
printf("got %d values\n", count); |
//debugf(("got %d values\n", count)); |
80 |
SPAGAIN; |
SPAGAIN; |
81 |
SV *sv; |
SV *sv; |
82 |
sv = POPs; |
sv = POPs; |
83 |
byte = SvIV(sv); |
byte = SvIV(sv); |
84 |
FREETMPS; |
FREETMPS; |
85 |
LEAVE; |
LEAVE; |
86 |
printf("Rd6502(%04x) = %02x\n", Addr, byte); |
//debugf(("mem(%04x) = %02x\n", Addr, byte)); |
87 |
return byte; |
return byte; |
88 |
} |
} |
89 |
|
|
90 |
byte Rd6502(register word Addr) { |
byte Rd6502(register word Addr) { |
91 |
byte Value; |
byte Value; |
92 |
Value = mem(Addr); |
Value = mem(Addr); |
93 |
|
debugf(("Rd6502(%04x) = %02x\n", Addr, Value)); |
94 |
return Value; |
return Value; |
95 |
} |
} |
96 |
|
|
97 |
void Wr6502(register word Addr,register byte Value) { |
void Wr6502(register word Addr,register byte Value) { |
98 |
printf("Wr6502(%04x,%02x)\n", Addr, Value); |
debugf(("Wr6502(%04x,%02x)\n", Addr, Value)); |
99 |
dSP; |
dSP; |
100 |
ENTER; |
ENTER; |
101 |
SAVETMPS; |
SAVETMPS; |
103 |
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
104 |
XPUSHs( sv_2mortal( newSViv( Value ) ) ); |
XPUSHs( sv_2mortal( newSViv( Value ) ) ); |
105 |
PUTBACK; |
PUTBACK; |
106 |
call_pv("M6502::write", G_DISCARD ); |
call_pv("Arch::write", G_DISCARD ); |
107 |
FREETMPS; |
FREETMPS; |
108 |
LEAVE; |
LEAVE; |
109 |
} |
} |
111 |
byte Op6502(register word Addr) { |
byte Op6502(register word Addr) { |
112 |
byte Op; |
byte Op; |
113 |
Op = mem(Addr); |
Op = mem(Addr); |
114 |
printf("Op6502(%04x,%02x)\n", Addr, Op); |
debugf(("Op6502(%04x,%02x) PC:%04x\n", Addr, Op, R->PC.W)); |
115 |
dump_R; |
return Op; |
116 |
} |
} |
117 |
|
|
118 |
/** Loop6502() ***********************************************/ |
/** Loop6502() ***********************************************/ |
122 |
/** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the **/ |
/** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the **/ |
123 |
/** emulation loop. **/ |
/** emulation loop. **/ |
124 |
/************************************ TO BE WRITTEN BY USER **/ |
/************************************ TO BE WRITTEN BY USER **/ |
125 |
|
|
126 |
|
int hw_int = INT_NONE; |
127 |
|
|
128 |
byte Loop6502(register M6502 *R) { |
byte Loop6502(register M6502 *R) { |
129 |
printf("Loop6502\n"); |
debugf(("Loop6502\n")); |
130 |
dump_R; |
dump_R; |
131 |
return INT_NONE; |
return hw_int; |
132 |
} |
} |
133 |
|
|
134 |
/** Patch6502() **********************************************/ |
/** Patch6502() **********************************************/ |
139 |
/** or 0 if the opcode was truly illegal. **/ |
/** or 0 if the opcode was truly illegal. **/ |
140 |
/************************************ TO BE WRITTEN BY USER **/ |
/************************************ TO BE WRITTEN BY USER **/ |
141 |
byte Patch6502(register byte Op,register M6502 *R) { |
byte Patch6502(register byte Op,register M6502 *R) { |
142 |
printf("Patch6502(%02x)\n", Op); |
debugf(("Patch6502(%02x)\n", Op)); |
143 |
dump_R; |
dump_R; |
144 |
return 0; |
return 0; |
145 |
} |
} |
150 |
**/ |
**/ |
151 |
|
|
152 |
int main(int argc, char **argv) { |
int main(int argc, char **argv) { |
153 |
char *command_line[] = {"", "-e", |
char *command_line[] = {"", "-e", EMU_START }; |
|
"use M6502; print \"Loaded M6502 module\n\";"}; |
|
154 |
my_perl = perl_alloc(); |
my_perl = perl_alloc(); |
155 |
perl_construct(my_perl); |
perl_construct(my_perl); |
156 |
if (perl_parse(my_perl, NULL, 3, command_line, (char **)NULL)) { |
if (perl_parse(my_perl, xs_init, 3, command_line, (char **)NULL)) { |
157 |
printf("Failed to parse\n"); |
printf("Failed to parse initial: %s\n", EMU_START ); |
158 |
return 0; |
return 0; |
159 |
} |
} |
160 |
perl_run(my_perl); |
perl_run(my_perl); |
168 |
exit(1); |
exit(1); |
169 |
} |
} |
170 |
|
|
|
update_R(R); |
|
|
|
|
171 |
printf("reset CPU\n"); |
printf("reset CPU\n"); |
172 |
Reset6502(R); |
Reset6502(R); |
173 |
|
|
174 |
printf("call M6502::init\n"); |
printf("call Arch::init\n"); |
175 |
dSP; |
dSP; |
176 |
PUSHMARK(SP); |
PUSHMARK(SP); |
177 |
call_pv("M6502::init", G_DISCARD | G_NOARGS ); |
call_pv("Arch::init", G_DISCARD | G_NOARGS ); |
178 |
|
FREETMPS; |
179 |
printf("run CPU\n"); |
LEAVE; |
180 |
Run6502(R); |
|
181 |
|
int cycles = 1; |
182 |
|
while ( cycles ) { |
183 |
|
dSP; |
184 |
|
PUSHMARK(SP); |
185 |
|
call_pv("Arch::cli", G_DISCARD | G_NOARGS ); |
186 |
|
pull_R(R); |
187 |
|
FREETMPS; |
188 |
|
LEAVE; |
189 |
|
cycles = SvIV( get_sv("M6502::run_for", FALSE) ); |
190 |
|
printf("run CPU for %d cycles\n", cycles); |
191 |
|
dump_R; |
192 |
|
//Run6502(R); |
193 |
|
Exec6502(R, cycles); |
194 |
|
dump_R; |
195 |
|
push_R(R); |
196 |
|
printf("end of %d cycles CPU run\n", cycles); |
197 |
|
} |
198 |
} |
} |
199 |
free(R); |
free(R); |
200 |
perl_destruct(my_perl); |
perl_destruct(my_perl); |