/[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

Annotation of /M6502/perl.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 80 - (hide annotations)
Wed Aug 1 15:34:43 2007 UTC (16 years, 9 months ago) by dpavlin
File MIME type: text/plain
File size: 5745 byte(s)
move to XS compilation
1 dpavlin 24 #include <EXTERN.h>
2     #include <perl.h>
3     #include "M6502.h"
4     #include "config.h"
5    
6 dpavlin 34 #if DEBUGF
7 dpavlin 33 #define debugf(x) do { \
8 dpavlin 42 PerlIO_stdoutf("#> "); \
9 dpavlin 33 PerlIO_stdoutf x ; \
10     } while (0)
11     #else
12     #define debugf(x)
13     #endif
14    
15 dpavlin 24 static PerlInterpreter *my_perl;
16    
17     static M6502 *R;
18    
19 dpavlin 34 #if DUMP_R
20 dpavlin 26 #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 dpavlin 33 #else
22     #define dump_R
23     #endif
24 dpavlin 25
25 dpavlin 38 void pull_R(M6502 *R) {
26 dpavlin 33 R->A = SvIV( get_sv("M6502::A", FALSE) );
27 dpavlin 34 R->P = SvIV( get_sv("M6502::P", FALSE) );
28 dpavlin 38 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 dpavlin 74 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 dpavlin 38 printf("pull_R finished\n");
39 dpavlin 25 dump_R;
40 dpavlin 24 }
41    
42 dpavlin 38 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 dpavlin 74 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 dpavlin 38 PUTBACK;
61 dpavlin 39 call_pv("M6502::push_R", G_DISCARD );
62 dpavlin 38 printf("push_R called\n");
63     dump_R;
64     FREETMPS;
65     LEAVE;
66     }
67    
68 dpavlin 25 /** 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 dpavlin 28 byte mem(word Addr) {
77     byte byte;
78 dpavlin 27 int count;
79     dSP;
80     ENTER;
81     SAVETMPS;
82     PUSHMARK(SP);
83 dpavlin 28 XPUSHs( sv_2mortal( newSViv( Addr ) ) );
84 dpavlin 27 PUTBACK;
85 dpavlin 33 count = call_pv("Arch::read", G_ARRAY );
86 dpavlin 27 if ( count != 1 ) {
87     printf("expect 1 return value, got %d", count);
88     exit(1);
89     }
90 dpavlin 33 //debugf(("got %d values\n", count));
91 dpavlin 27 SPAGAIN;
92     SV *sv;
93     sv = POPs;
94 dpavlin 28 byte = SvIV(sv);
95 dpavlin 27 FREETMPS;
96     LEAVE;
97 dpavlin 34 //debugf(("mem(%04x) = %02x\n", Addr, byte));
98 dpavlin 28 return byte;
99     }
100    
101     byte Rd6502(register word Addr) {
102     byte Value;
103     Value = mem(Addr);
104 dpavlin 34 debugf(("Rd6502(%04x) = %02x\n", Addr, Value));
105 dpavlin 27 return Value;
106 dpavlin 25 }
107    
108     void Wr6502(register word Addr,register byte Value) {
109 dpavlin 33 debugf(("Wr6502(%04x,%02x)\n", Addr, Value));
110 dpavlin 27 dSP;
111     ENTER;
112     SAVETMPS;
113     PUSHMARK(SP);
114     XPUSHs( sv_2mortal( newSViv( Addr ) ) );
115     XPUSHs( sv_2mortal( newSViv( Value ) ) );
116     PUTBACK;
117 dpavlin 33 call_pv("Arch::write", G_DISCARD );
118 dpavlin 27 FREETMPS;
119     LEAVE;
120 dpavlin 25 }
121    
122     byte Op6502(register word Addr) {
123     byte Op;
124 dpavlin 28 Op = mem(Addr);
125 dpavlin 36 debugf(("Op6502(%04x,%02x) PC:%04x\n", Addr, Op, R->PC.W));
126 dpavlin 40 return Op;
127 dpavlin 25 }
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 dpavlin 42
137     int hw_int = INT_NONE;
138    
139 dpavlin 25 byte Loop6502(register M6502 *R) {
140 dpavlin 33 debugf(("Loop6502\n"));
141 dpavlin 25 dump_R;
142 dpavlin 42 return hw_int;
143 dpavlin 25 }
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 dpavlin 33 debugf(("Patch6502(%02x)\n", Op));
154 dpavlin 25 dump_R;
155 dpavlin 66 hw_int = INT_QUIT;
156 dpavlin 28 return 0;
157 dpavlin 25 }
158    
159 dpavlin 80 void run_forever(void) {
160     printf("entered run_forever\n");
161    
162     R = malloc(sizeof(M6502));
163     if (!R) {
164     printf("can't alloc %d bytes for M6502", sizeof(M6502));
165     exit(1);
166     }
167    
168     printf("reset CPU\n");
169     Reset6502(R);
170    
171     printf("call Arch::init\n");
172     dSP;
173     PUSHMARK(SP);
174     call_pv("Arch::init", G_DISCARD | G_NOARGS );
175     FREETMPS;
176     LEAVE;
177    
178     int cycles = 1;
179     while ( cycles ) {
180     dSP;
181     PUSHMARK(SP);
182     call_pv("Arch::cli", G_DISCARD | G_NOARGS );
183     pull_R(R);
184     FREETMPS;
185     LEAVE;
186     cycles = SvIV( get_sv("M6502::run_for", FALSE) );
187     if ( cycles > 0 ) {
188     printf("run CPU for %d cycles\n", cycles);
189     dump_R;
190     //Run6502(R);
191     Exec6502(R, cycles);
192     dump_R;
193     push_R(R);
194     printf("end of %d cycles CPU run\n", cycles);
195     } else {
196     printf("no cpu cycles set for run\n");
197     cycles = 1; // never exit, prevents segfault
198     }
199     }
200     free(R);
201     }
202    
203 dpavlin 25 /**
204     * main code
205     *
206     **/
207    
208 dpavlin 24 int main(int argc, char **argv) {
209 dpavlin 30 char *command_line[] = {"", "-e", EMU_START };
210 dpavlin 24 my_perl = perl_alloc();
211     perl_construct(my_perl);
212 dpavlin 29 if (perl_parse(my_perl, xs_init, 3, command_line, (char **)NULL)) {
213 dpavlin 31 printf("Failed to parse initial: %s\n", EMU_START );
214 dpavlin 24 return 0;
215     }
216     perl_run(my_perl);
217     if (SvTRUE(ERRSV)) {
218     printf("Failed to execute\n");
219     return 0;
220     } else {
221 dpavlin 80 run_forever();
222 dpavlin 24 }
223     perl_destruct(my_perl);
224     perl_free(my_perl);
225     return 0;
226     }

  ViewVC Help
Powered by ViewVC 1.1.26