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

  ViewVC Help
Powered by ViewVC 1.1.26