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 |
byte Debug6502(M6502 *R) { |
|
#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) { |
|
|
R->A = SvIV( get_sv("M6502::A", FALSE) ); |
|
|
R->P = SvIV( get_sv("M6502::P", FALSE) ); |
|
|
R->X = SvIV( get_sv("M6502::X", FALSE) ); |
|
|
R->Y = SvIV( get_sv("M6502::Y", FALSE) ); |
|
|
R->S = SvIV( get_sv("M6502::S", FALSE) ); |
|
|
R->PC.W = SvIV( get_sv("M6502::PC", FALSE) ); |
|
|
R->IPeriod = SvIV( get_sv("M6502::IPeriod", FALSE) ); |
|
|
// ICount IRequest IAutoReset TrapBadOps Trap Trace |
|
|
printf("pull_R finished\n"); |
|
11 |
dump_R; |
dump_R; |
12 |
} |
return 1; // continue emulation |
|
|
|
|
void push_R(M6502 *R) { |
|
|
dSP; |
|
|
ENTER; |
|
|
SAVETMPS; |
|
|
PUSHMARK(SP); |
|
|
XPUSHs( sv_2mortal( newSViv( R->A ) ) ); |
|
|
XPUSHs( sv_2mortal( newSViv( R->P ) ) ); |
|
|
XPUSHs( sv_2mortal( newSViv( R->X ) ) ); |
|
|
XPUSHs( sv_2mortal( newSViv( R->Y ) ) ); |
|
|
XPUSHs( sv_2mortal( newSViv( R->S ) ) ); |
|
|
XPUSHs( sv_2mortal( newSViv( R->PC.W ) ) ); |
|
|
PUTBACK; |
|
|
call_pv("M6502::push_R", G_DISCARD ); |
|
|
printf("push_R called\n"); |
|
|
dump_R; |
|
|
FREETMPS; |
|
|
LEAVE; |
|
13 |
} |
} |
14 |
|
|
15 |
/** Rd6502()/Wr6502/Op6502() *********************************/ |
/** Rd6502()/Wr6502/Op6502() *********************************/ |
23 |
byte mem(word Addr) { |
byte mem(word Addr) { |
24 |
byte byte; |
byte byte; |
25 |
int count; |
int count; |
26 |
|
debugf(("mem(%04x)", Addr)); |
27 |
dSP; |
dSP; |
28 |
ENTER; |
ENTER; |
29 |
SAVETMPS; |
SAVETMPS; |
30 |
PUSHMARK(SP); |
PUSHMARK(SP); |
31 |
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
32 |
PUTBACK; |
PUTBACK; |
33 |
count = call_pv("Arch::read", G_ARRAY ); |
count = call_pv("M6502::read", G_ARRAY | G_EVAL ); |
34 |
|
debugf(("got %d values", count)); |
35 |
|
SPAGAIN; |
36 |
|
if (SvTRUE(ERRSV)) { |
37 |
|
printf("ERROR: %s", SvPV_nolen( ERRSV ) ); |
38 |
|
exit(1); |
39 |
|
} |
40 |
if ( count != 1 ) { |
if ( count != 1 ) { |
41 |
printf("expect 1 return value, got %d", count); |
printf("expect 1 return value, got %d", count); |
42 |
exit(1); |
exit(1); |
43 |
} |
} |
|
//debugf(("got %d values\n", count)); |
|
|
SPAGAIN; |
|
44 |
SV *sv; |
SV *sv; |
45 |
sv = POPs; |
sv = POPs; |
46 |
byte = SvIV(sv); |
byte = SvIV(sv); |
47 |
FREETMPS; |
FREETMPS; |
48 |
LEAVE; |
LEAVE; |
49 |
//debugf(("mem(%04x) = %02x\n", Addr, byte)); |
debugf(("mem(%04x) = %02x", Addr, byte)); |
50 |
return byte; |
return byte; |
51 |
} |
} |
52 |
|
|
53 |
byte Rd6502(register word Addr) { |
byte Rd6502(register word Addr) { |
54 |
byte Value; |
byte Value; |
55 |
Value = mem(Addr); |
// Value = mem(Addr); |
56 |
debugf(("Rd6502(%04x) = %02x\n", Addr, Value)); |
Value = 0x42; |
57 |
|
debugf(("Rd6502(%04x) = %02x", Addr, Value)); |
58 |
return Value; |
return Value; |
59 |
} |
} |
60 |
|
|
61 |
void Wr6502(register word Addr,register byte Value) { |
void Wr6502(register word Addr,register byte Value) { |
62 |
debugf(("Wr6502(%04x,%02x)\n", Addr, Value)); |
debugf(("Wr6502(%04x,%02x)", Addr, Value)); |
63 |
dSP; |
dSP; |
64 |
ENTER; |
ENTER; |
65 |
SAVETMPS; |
SAVETMPS; |
75 |
byte Op6502(register word Addr) { |
byte Op6502(register word Addr) { |
76 |
byte Op; |
byte Op; |
77 |
Op = mem(Addr); |
Op = mem(Addr); |
78 |
debugf(("Op6502(%04x,%02x) PC:%04x\n", Addr, Op, R->PC.W)); |
debugf(("Op6502(%04x,%02x) PC:%04x", Addr, Op, R->PC.W)); |
79 |
return Op; |
return Op; |
80 |
} |
} |
81 |
|
|
86 |
/** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the **/ |
/** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the **/ |
87 |
/** emulation loop. **/ |
/** emulation loop. **/ |
88 |
/************************************ TO BE WRITTEN BY USER **/ |
/************************************ TO BE WRITTEN BY USER **/ |
89 |
|
|
90 |
|
int hw_int = INT_NONE; |
91 |
|
|
92 |
byte Loop6502(register M6502 *R) { |
byte Loop6502(register M6502 *R) { |
93 |
debugf(("Loop6502\n")); |
debugf(("Loop6502")); |
94 |
dump_R; |
dump_R; |
95 |
return INT_NONE; |
return hw_int; |
96 |
} |
} |
97 |
|
|
98 |
/** Patch6502() **********************************************/ |
/** Patch6502() **********************************************/ |
103 |
/** or 0 if the opcode was truly illegal. **/ |
/** or 0 if the opcode was truly illegal. **/ |
104 |
/************************************ TO BE WRITTEN BY USER **/ |
/************************************ TO BE WRITTEN BY USER **/ |
105 |
byte Patch6502(register byte Op,register M6502 *R) { |
byte Patch6502(register byte Op,register M6502 *R) { |
106 |
debugf(("Patch6502(%02x)\n", Op)); |
debugf(("Patch6502(%02x)", Op)); |
107 |
dump_R; |
dump_R; |
108 |
|
hw_int = INT_QUIT; |
109 |
return 0; |
return 0; |
110 |
} |
} |
111 |
|
|
112 |
|
void run_forever(void) { |
113 |
|
printf("entered run_forever\n"); |
114 |
|
|
115 |
|
R = malloc(sizeof(M6502)); |
116 |
|
if (!R) { |
117 |
|
printf("can't alloc %d bytes for M6502", sizeof(M6502)); |
118 |
|
exit(1); |
119 |
|
} |
120 |
|
|
121 |
|
printf("reset CPU\n"); |
122 |
|
Reset6502(R); |
123 |
|
|
124 |
|
printf("call Arch::init\n"); |
125 |
|
dSP; |
126 |
|
PUSHMARK(SP); |
127 |
|
call_pv("Arch::init", G_DISCARD | G_NOARGS ); |
128 |
|
FREETMPS; |
129 |
|
LEAVE; |
130 |
|
|
131 |
|
int cycles = 1; |
132 |
|
while ( cycles ) { |
133 |
|
dSP; |
134 |
|
PUSHMARK(SP); |
135 |
|
call_pv("Arch::cli", G_DISCARD | G_NOARGS ); |
136 |
|
pull_R; |
137 |
|
FREETMPS; |
138 |
|
LEAVE; |
139 |
|
cycles = SvIV( get_sv("M6502::run_for", FALSE) ); |
140 |
|
if ( cycles > 0 ) { |
141 |
|
printf("run CPU for %d cycles\n", cycles); |
142 |
|
dump_R; |
143 |
|
//Run6502(R); |
144 |
|
Exec6502(R, cycles); |
145 |
|
dump_R; |
146 |
|
push_R; |
147 |
|
printf("end of %d cycles CPU run\n", cycles); |
148 |
|
} else { |
149 |
|
printf("no cpu cycles set for run\n"); |
150 |
|
cycles = 1; // never exit, prevents segfault |
151 |
|
} |
152 |
|
} |
153 |
|
free(R); |
154 |
|
} |
155 |
|
|
156 |
/** |
/** |
157 |
* main code |
* main code |
158 |
* |
* |
171 |
printf("Failed to execute\n"); |
printf("Failed to execute\n"); |
172 |
return 0; |
return 0; |
173 |
} else { |
} else { |
174 |
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"); |
|
175 |
} |
} |
|
free(R); |
|
176 |
perl_destruct(my_perl); |
perl_destruct(my_perl); |
177 |
perl_free(my_perl); |
perl_free(my_perl); |
178 |
return 0; |
return 0; |