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