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

  ViewVC Help
Powered by ViewVC 1.1.26