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

  ViewVC Help
Powered by ViewVC 1.1.26