/[VRac]/M6502/perl.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /M6502/perl.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 74 - (show annotations)
Tue Jul 31 23:48:19 2007 UTC (16 years, 9 months ago) by dpavlin
File MIME type: text/plain
File size: 5700 byte(s)
Implemented all R parts which are public in perl
1 #include <EXTERN.h>
2 #include <perl.h>
3 #include "M6502.h"
4 #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;
16
17 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 );
21 #else
22 #define dump_R
23 #endif
24
25 void pull_R(M6502 *R) {
26 R->A = SvIV( get_sv("M6502::A", FALSE) );
27 R->P = SvIV( get_sv("M6502::P", FALSE) );
28 R->X = SvIV( get_sv("M6502::X", FALSE) );
29 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 R->IRequest = SvIV( get_sv("M6502::IRequest", FALSE) );
34 R->IAutoReset = SvIV( get_sv("M6502::IAutoReset", FALSE) );
35 R->TrapBadOps = SvIV( get_sv("M6502::TrapBadOps", FALSE) );
36 R->Trap = SvIV( get_sv("M6502::Trap", FALSE) );
37 R->Trace = SvIV( get_sv("M6502::Trace", FALSE) );
38 printf("pull_R finished\n");
39 dump_R;
40 }
41
42 void push_R(M6502 *R) {
43 dSP;
44 ENTER;
45 SAVETMPS;
46 PUSHMARK(SP);
47 XPUSHs( sv_2mortal( newSViv( R->A ) ) );
48 XPUSHs( sv_2mortal( newSViv( R->P ) ) );
49 XPUSHs( sv_2mortal( newSViv( R->X ) ) );
50 XPUSHs( sv_2mortal( newSViv( R->Y ) ) );
51 XPUSHs( sv_2mortal( newSViv( R->S ) ) );
52 XPUSHs( sv_2mortal( newSViv( R->PC.W ) ) );
53 XPUSHs( sv_2mortal( newSViv( R->IPeriod ) ) );
54 XPUSHs( sv_2mortal( newSViv( R->ICount ) ) );
55 XPUSHs( sv_2mortal( newSViv( R->IRequest ) ) );
56 XPUSHs( sv_2mortal( newSViv( R->IAutoReset ) ) );
57 XPUSHs( sv_2mortal( newSViv( R->TrapBadOps ) ) );
58 XPUSHs( sv_2mortal( newSViv( R->Trap ) ) );
59 XPUSHs( sv_2mortal( newSViv( R->Trace ) ) );
60 PUTBACK;
61 call_pv("M6502::push_R", G_DISCARD );
62 printf("push_R called\n");
63 dump_R;
64 FREETMPS;
65 LEAVE;
66 }
67
68 /** Rd6502()/Wr6502/Op6502() *********************************/
69 /** These functions are called when access to RAM occurs. **/
70 /** They allow to control memory access. Op6502 is the same **/
71 /** as Rd6502, but used to read *opcodes* only, when many **/
72 /** checks can be skipped to make it fast. It is only **/
73 /** required if there is a #define FAST_RDOP. **/
74 /************************************ TO BE WRITTEN BY USER **/
75
76 byte mem(word Addr) {
77 byte byte;
78 int count;
79 dSP;
80 ENTER;
81 SAVETMPS;
82 PUSHMARK(SP);
83 XPUSHs( sv_2mortal( newSViv( Addr ) ) );
84 PUTBACK;
85 count = call_pv("Arch::read", G_ARRAY );
86 if ( count != 1 ) {
87 printf("expect 1 return value, got %d", count);
88 exit(1);
89 }
90 //debugf(("got %d values\n", count));
91 SPAGAIN;
92 SV *sv;
93 sv = POPs;
94 byte = SvIV(sv);
95 FREETMPS;
96 LEAVE;
97 //debugf(("mem(%04x) = %02x\n", Addr, byte));
98 return byte;
99 }
100
101 byte Rd6502(register word Addr) {
102 byte Value;
103 Value = mem(Addr);
104 debugf(("Rd6502(%04x) = %02x\n", Addr, Value));
105 return Value;
106 }
107
108 void Wr6502(register word Addr,register byte Value) {
109 debugf(("Wr6502(%04x,%02x)\n", Addr, Value));
110 dSP;
111 ENTER;
112 SAVETMPS;
113 PUSHMARK(SP);
114 XPUSHs( sv_2mortal( newSViv( Addr ) ) );
115 XPUSHs( sv_2mortal( newSViv( Value ) ) );
116 PUTBACK;
117 call_pv("Arch::write", G_DISCARD );
118 FREETMPS;
119 LEAVE;
120 }
121
122 byte Op6502(register word Addr) {
123 byte Op;
124 Op = mem(Addr);
125 debugf(("Op6502(%04x,%02x) PC:%04x\n", Addr, Op, R->PC.W));
126 return Op;
127 }
128
129 /** Loop6502() ***********************************************/
130 /** 6502 emulation calls this function periodically to **/
131 /** check if the system hardware requires any interrupts. **/
132 /** This function must return one of following values: **/
133 /** INT_NONE, INT_IRQ, INT_NMI, or INT_QUIT to exit the **/
134 /** emulation loop. **/
135 /************************************ TO BE WRITTEN BY USER **/
136
137 int hw_int = INT_NONE;
138
139 byte Loop6502(register M6502 *R) {
140 debugf(("Loop6502\n"));
141 dump_R;
142 return hw_int;
143 }
144
145 /** Patch6502() **********************************************/
146 /** Emulation calls this function when it encounters an **/
147 /** unknown opcode. This can be used to patch the code to **/
148 /** emulate BIOS calls, such as disk and tape access. The **/
149 /** function should return 1 if the exception was handled, **/
150 /** or 0 if the opcode was truly illegal. **/
151 /************************************ TO BE WRITTEN BY USER **/
152 byte Patch6502(register byte Op,register M6502 *R) {
153 debugf(("Patch6502(%02x)\n", Op));
154 dump_R;
155 hw_int = INT_QUIT;
156 return 0;
157 }
158
159 /**
160 * main code
161 *
162 **/
163
164 int main(int argc, char **argv) {
165 char *command_line[] = {"", "-e", EMU_START };
166 my_perl = perl_alloc();
167 perl_construct(my_perl);
168 if (perl_parse(my_perl, xs_init, 3, command_line, (char **)NULL)) {
169 printf("Failed to parse initial: %s\n", EMU_START );
170 return 0;
171 }
172 perl_run(my_perl);
173 if (SvTRUE(ERRSV)) {
174 printf("Failed to execute\n");
175 return 0;
176 } else {
177 R = malloc(sizeof(M6502));
178 if (!R) {
179 printf("can't alloc %d bytes for M6502", sizeof(M6502));
180 exit(1);
181 }
182
183 printf("reset CPU\n");
184 Reset6502(R);
185
186 printf("call Arch::init\n");
187 dSP;
188 PUSHMARK(SP);
189 call_pv("Arch::init", G_DISCARD | G_NOARGS );
190 FREETMPS;
191 LEAVE;
192
193 int cycles = 1;
194 while ( cycles ) {
195 dSP;
196 PUSHMARK(SP);
197 call_pv("Arch::cli", G_DISCARD | G_NOARGS );
198 pull_R(R);
199 FREETMPS;
200 LEAVE;
201 cycles = SvIV( get_sv("M6502::run_for", FALSE) );
202 if ( cycles > 0 ) {
203 printf("run CPU for %d cycles\n", cycles);
204 dump_R;
205 //Run6502(R);
206 Exec6502(R, cycles);
207 dump_R;
208 push_R(R);
209 printf("end of %d cycles CPU run\n", cycles);
210 } else {
211 printf("no cpu cycles set for run\n");
212 cycles = 1; // never exit, prevents segfault
213 }
214 }
215 }
216 free(R);
217 perl_destruct(my_perl);
218 perl_free(my_perl);
219 return 0;
220 }

  ViewVC Help
Powered by ViewVC 1.1.26