1 |
#include <EXTERN.h> |
2 |
#include <perl.h> |
3 |
#include "M6502.h" |
4 |
#include "config.h" |
5 |
|
6 |
static PerlInterpreter *my_perl; |
7 |
|
8 |
static M6502 *R; |
9 |
|
10 |
#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 ); |
11 |
|
12 |
void update_R(M6502 *R) { |
13 |
R->A = atoi( SvPV_nolen( get_sv("M6502::A", FALSE) ) ); |
14 |
R->P = atoi( SvPV_nolen( get_sv("M6502::P", FALSE) ) ); |
15 |
R->X = atoi( SvPV_nolen( get_sv("M6502::X", FALSE) ) ); |
16 |
R->Y = atoi( SvPV_nolen( get_sv("M6502::Y", FALSE) ) ); |
17 |
R->S = atoi( SvPV_nolen( get_sv("M6502::S", FALSE) ) ); |
18 |
R->PC.W = atoi( SvPV_nolen( get_sv("M6502::PC", FALSE) ) ); |
19 |
R->IPeriod = atoi( SvPV_nolen( get_sv("M6502::IPeriod", FALSE) ) ); |
20 |
// ICount IRequest IAutoReset TrapBadOps Trap Trace |
21 |
dump_R; |
22 |
} |
23 |
|
24 |
/** Rd6502()/Wr6502/Op6502() *********************************/ |
25 |
/** These functions are called when access to RAM occurs. **/ |
26 |
/** They allow to control memory access. Op6502 is the same **/ |
27 |
/** as Rd6502, but used to read *opcodes* only, when many **/ |
28 |
/** checks can be skipped to make it fast. It is only **/ |
29 |
/** required if there is a #define FAST_RDOP. **/ |
30 |
/************************************ TO BE WRITTEN BY USER **/ |
31 |
|
32 |
byte mem(word Addr) { |
33 |
byte byte; |
34 |
int count; |
35 |
dSP; |
36 |
ENTER; |
37 |
SAVETMPS; |
38 |
PUSHMARK(SP); |
39 |
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
40 |
PUTBACK; |
41 |
count = call_pv("M6502::read", G_ARRAY ); |
42 |
if ( count != 1 ) { |
43 |
printf("expect 1 return value, got %d", count); |
44 |
exit(1); |
45 |
} |
46 |
printf("got %d values\n", count); |
47 |
SPAGAIN; |
48 |
SV *sv; |
49 |
sv = POPs; |
50 |
byte = SvIV(sv); |
51 |
FREETMPS; |
52 |
LEAVE; |
53 |
printf("Rd6502(%04x) = %02x\n", Addr, byte); |
54 |
return byte; |
55 |
} |
56 |
|
57 |
byte Rd6502(register word Addr) { |
58 |
byte Value; |
59 |
Value = mem(Addr); |
60 |
return Value; |
61 |
} |
62 |
|
63 |
void Wr6502(register word Addr,register byte Value) { |
64 |
printf("Wr6502(%04x,%02x)\n", Addr, Value); |
65 |
dSP; |
66 |
ENTER; |
67 |
SAVETMPS; |
68 |
PUSHMARK(SP); |
69 |
XPUSHs( sv_2mortal( newSViv( Addr ) ) ); |
70 |
XPUSHs( sv_2mortal( newSViv( Value ) ) ); |
71 |
PUTBACK; |
72 |
call_pv("M6502::write", G_DISCARD ); |
73 |
FREETMPS; |
74 |
LEAVE; |
75 |
} |
76 |
|
77 |
byte Op6502(register word Addr) { |
78 |
byte Op; |
79 |
Op = mem(Addr); |
80 |
printf("Op6502(%04x,%02x)\n", Addr, Op); |
81 |
dump_R; |
82 |
} |
83 |
|
84 |
/** Loop6502() ***********************************************/ |
85 |
/** 6502 emulation calls this function periodically to **/ |
86 |
/** check if the system hardware requires any interrupts. **/ |
87 |
/** This function must return one of following values: **/ |
88 |
/** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the **/ |
89 |
/** emulation loop. **/ |
90 |
/************************************ TO BE WRITTEN BY USER **/ |
91 |
byte Loop6502(register M6502 *R) { |
92 |
printf("Loop6502\n"); |
93 |
dump_R; |
94 |
return INT_NONE; |
95 |
} |
96 |
|
97 |
/** Patch6502() **********************************************/ |
98 |
/** Emulation calls this function when it encounters an **/ |
99 |
/** unknown opcode. This can be used to patch the code to **/ |
100 |
/** emulate BIOS calls, such as disk and tape access. The **/ |
101 |
/** function should return 1 if the exception was handled, **/ |
102 |
/** or 0 if the opcode was truly illegal. **/ |
103 |
/************************************ TO BE WRITTEN BY USER **/ |
104 |
byte Patch6502(register byte Op,register M6502 *R) { |
105 |
printf("Patch6502(%02x)\n", Op); |
106 |
dump_R; |
107 |
return 0; |
108 |
} |
109 |
|
110 |
/** |
111 |
* main code |
112 |
* |
113 |
**/ |
114 |
|
115 |
int main(int argc, char **argv) { |
116 |
char *command_line[] = {"", "-e", EMU_START }; |
117 |
my_perl = perl_alloc(); |
118 |
perl_construct(my_perl); |
119 |
if (perl_parse(my_perl, xs_init, 3, command_line, (char **)NULL)) { |
120 |
printf("Failed to parse\n"); |
121 |
return 0; |
122 |
} |
123 |
perl_run(my_perl); |
124 |
if (SvTRUE(ERRSV)) { |
125 |
printf("Failed to execute\n"); |
126 |
return 0; |
127 |
} else { |
128 |
R = malloc(sizeof(M6502)); |
129 |
if (!R) { |
130 |
printf("can't alloc %d bytes for M6502", sizeof(M6502)); |
131 |
exit(1); |
132 |
} |
133 |
|
134 |
update_R(R); |
135 |
|
136 |
printf("reset CPU\n"); |
137 |
Reset6502(R); |
138 |
|
139 |
printf("call M6502::init\n"); |
140 |
dSP; |
141 |
PUSHMARK(SP); |
142 |
call_pv("M6502::init", G_DISCARD | G_NOARGS ); |
143 |
|
144 |
printf("run CPU\n"); |
145 |
Run6502(R); |
146 |
|
147 |
} |
148 |
free(R); |
149 |
perl_destruct(my_perl); |
150 |
perl_free(my_perl); |
151 |
return 0; |
152 |
} |