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 x ; \ |
9 |
|
} while (0) |
10 |
|
#else |
11 |
|
#define debugf(x) |
12 |
|
#endif |
13 |
|
|
14 |
static PerlInterpreter *my_perl; |
static PerlInterpreter *my_perl; |
15 |
|
|
16 |
static M6502 *R; |
static M6502 *R; |
17 |
|
|
18 |
|
#if DUMP_R |
19 |
#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 ); |
20 |
|
#else |
21 |
|
#define dump_R |
22 |
|
#endif |
23 |
|
|
24 |
void update_R(M6502 *R) { |
void update_R(M6502 *R) { |
25 |
R->A = atoi( SvPV_nolen( get_sv("M6502::A", FALSE) ) ); |
R->A = SvIV( get_sv("M6502::A", FALSE) ); |
26 |
R->P = atoi( SvPV_nolen( get_sv("M6502::P", FALSE) ) ); |
printf("--%d", get_sv("M6502::A", FALSE) ); |
27 |
|
R->P = SvIV( get_sv("M6502::P", FALSE) ); |
28 |
R->X = atoi( SvPV_nolen( get_sv("M6502::X", FALSE) ) ); |
R->X = atoi( SvPV_nolen( get_sv("M6502::X", FALSE) ) ); |
29 |
R->Y = atoi( SvPV_nolen( get_sv("M6502::Y", FALSE) ) ); |
R->Y = atoi( SvPV_nolen( get_sv("M6502::Y", FALSE) ) ); |
30 |
R->S = atoi( SvPV_nolen( get_sv("M6502::S", FALSE) ) ); |
R->S = atoi( SvPV_nolen( get_sv("M6502::S", FALSE) ) ); |
42 |
/** required if there is a #define FAST_RDOP. **/ |
/** required if there is a #define FAST_RDOP. **/ |
43 |
/************************************ TO BE WRITTEN BY USER **/ |
/************************************ TO BE WRITTEN BY USER **/ |
44 |
|
|
45 |
|
byte mem(word Addr) { |
46 |
|
byte byte; |
47 |
|
int count; |
48 |
|
dSP; |
49 |
|
ENTER; |
50 |
|
SAVETMPS; |
51 |
|
PUSHMARK(SP); |
52 |
|
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
53 |
|
PUTBACK; |
54 |
|
count = call_pv("Arch::read", G_ARRAY ); |
55 |
|
if ( count != 1 ) { |
56 |
|
printf("expect 1 return value, got %d", count); |
57 |
|
exit(1); |
58 |
|
} |
59 |
|
//debugf(("got %d values\n", count)); |
60 |
|
SPAGAIN; |
61 |
|
SV *sv; |
62 |
|
sv = POPs; |
63 |
|
byte = SvIV(sv); |
64 |
|
FREETMPS; |
65 |
|
LEAVE; |
66 |
|
//debugf(("mem(%04x) = %02x\n", Addr, byte)); |
67 |
|
return byte; |
68 |
|
} |
69 |
|
|
70 |
byte Rd6502(register word Addr) { |
byte Rd6502(register word Addr) { |
71 |
byte Value; |
byte Value; |
72 |
Value = 0x42; |
Value = mem(Addr); |
73 |
printf("Rd6502(%04x,%02x)\n", Addr, Value); |
debugf(("Rd6502(%04x) = %02x\n", Addr, Value)); |
74 |
|
return Value; |
75 |
} |
} |
76 |
|
|
77 |
void Wr6502(register word Addr,register byte Value) { |
void Wr6502(register word Addr,register byte Value) { |
78 |
printf("Wr6502(%04x,%02x)\n", Addr, Value); |
debugf(("Wr6502(%04x,%02x)\n", Addr, Value)); |
79 |
|
dSP; |
80 |
|
ENTER; |
81 |
|
SAVETMPS; |
82 |
|
PUSHMARK(SP); |
83 |
|
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
84 |
|
XPUSHs( sv_2mortal( newSViv( Value ) ) ); |
85 |
|
PUTBACK; |
86 |
|
call_pv("Arch::write", G_DISCARD ); |
87 |
|
FREETMPS; |
88 |
|
LEAVE; |
89 |
} |
} |
90 |
|
|
91 |
byte Op6502(register word Addr) { |
byte Op6502(register word Addr) { |
92 |
byte Op; |
byte Op; |
93 |
Op = 0xff; |
Op = mem(Addr); |
94 |
printf("Op6502(%04x,%02x)\n", Addr, Op); |
debugf(("Op6502(%04x,%02x)\n", Addr, Op)); |
95 |
dump_R; |
dump_R; |
96 |
} |
} |
97 |
|
|
103 |
/** emulation loop. **/ |
/** emulation loop. **/ |
104 |
/************************************ TO BE WRITTEN BY USER **/ |
/************************************ TO BE WRITTEN BY USER **/ |
105 |
byte Loop6502(register M6502 *R) { |
byte Loop6502(register M6502 *R) { |
106 |
printf("Loop6502\n"); |
debugf(("Loop6502\n")); |
107 |
dump_R; |
dump_R; |
108 |
|
return INT_NONE; |
109 |
} |
} |
110 |
|
|
111 |
/** Patch6502() **********************************************/ |
/** Patch6502() **********************************************/ |
116 |
/** or 0 if the opcode was truly illegal. **/ |
/** or 0 if the opcode was truly illegal. **/ |
117 |
/************************************ TO BE WRITTEN BY USER **/ |
/************************************ TO BE WRITTEN BY USER **/ |
118 |
byte Patch6502(register byte Op,register M6502 *R) { |
byte Patch6502(register byte Op,register M6502 *R) { |
119 |
printf("Patch6502(%02x)\n", Op); |
debugf(("Patch6502(%02x)\n", Op)); |
120 |
dump_R; |
dump_R; |
121 |
|
return 0; |
122 |
} |
} |
123 |
|
|
124 |
/** |
/** |
127 |
**/ |
**/ |
128 |
|
|
129 |
int main(int argc, char **argv) { |
int main(int argc, char **argv) { |
130 |
char *command_line[] = {"", "-e", |
char *command_line[] = {"", "-e", EMU_START }; |
|
"use M6502; print \"Loaded M6502 module\n\";"}; |
|
131 |
my_perl = perl_alloc(); |
my_perl = perl_alloc(); |
132 |
perl_construct(my_perl); |
perl_construct(my_perl); |
133 |
if (perl_parse(my_perl, NULL, 3, command_line, (char **)NULL)) { |
if (perl_parse(my_perl, xs_init, 3, command_line, (char **)NULL)) { |
134 |
printf("Failed to parse\n"); |
printf("Failed to parse initial: %s\n", EMU_START ); |
135 |
return 0; |
return 0; |
136 |
} |
} |
137 |
perl_run(my_perl); |
perl_run(my_perl); |
145 |
exit(1); |
exit(1); |
146 |
} |
} |
147 |
|
|
|
update_R(R); |
|
|
|
|
148 |
printf("reset CPU\n"); |
printf("reset CPU\n"); |
149 |
Reset6502(R); |
Reset6502(R); |
150 |
|
|
151 |
printf("call M6502::init\n"); |
printf("call Arch::init\n"); |
152 |
dSP; |
dSP; |
153 |
PUSHMARK(SP); |
PUSHMARK(SP); |
154 |
call_pv("M6502::init", G_DISCARD | G_NOARGS ); |
call_pv("Arch::init", G_DISCARD | G_NOARGS ); |
155 |
|
|
156 |
|
update_R(R); |
157 |
printf("run CPU\n"); |
printf("run CPU\n"); |
158 |
Run6502(R); |
Run6502(R); |
159 |
|
|