9 //typedef int (clock_func_t) (void);
10 static int fetch_and_decode_inst(void);
11 static int decode_inst(void);
12 static int execute_inst(void);
13 static int reset_handler1(void);
16 int reset_exec6502(void);
17 int decode6502(unsigned char inst);
18 int execute6502(void);
19 int test_and_set_exec(void);
20 int test_and_set_intr(void);
22 int init_6502core(void);
26 void pc_set(unsigned short addr);
27 unsigned short pc_get(void);
28 void pc_move(int offset);
29 void report_exec_err(void);
30 void d1_set(int on_off);
31 void d2_set(int on_off);
32 void d3_set(int on_off);
33 unsigned int get_nmi_cnt(void);
37 int disas_inst(unsigned short addr);
38 void dump_6502(int full);
39 extern int debug_mode;
40 extern unsigned short break_point;
41 extern unsigned long break_counter_point;
42 extern unsigned char break_nmi_point;
43 extern int critical_error;
46 static unsigned char cpu_data_buffer;
47 static unsigned short cpu_addr_buffer;
49 static void (*dump_6502_level2)(int);
50 static void (*dump_6502_level3_load)(unsigned short, unsigned char);
51 static void (*dump_6502_level3_store)(unsigned short, unsigned char);
52 static int (*d1_disas)(unsigned short);
55 * clock execution function array.
56 * max execution cycle is 7.
57 * the last element must be NULL.
59 clock_func_t *execute_func;
61 static unsigned long clock_cnt;
64 unsigned long get_clock_cnt(void) {
68 void reset_clock_cnt(void) {
79 if (get_reset_pin()) {
80 execute_func = reset_handler1;
82 return execute_func();
90 int register_cpu_clock(void) {
91 return register_clock_hander(clock_cpu, CPU_DEVIDER);
94 int unregister_cpu_clock(void) {
95 return unregister_clock_hander(clock_cpu);
98 unsigned char load_memory(unsigned short addr) {
103 cpu_data_buffer = get_bus_data();
106 dump_6502_level3_load (addr, cpu_data_buffer);
109 return cpu_data_buffer;
112 void store_memory(unsigned short addr, unsigned char data) {
113 dump_6502_level3_store (addr, data);
121 cpu_data_buffer = data;
125 * load address in the memory.
126 * loading 2 bytes takes 2 cycles.
127 * the parameter "cycle" means first or second read.
129 unsigned short load_addr(unsigned short addr, int cycle) {
130 unsigned short byte = load_memory(addr);
132 ///NES=little endian. lower byte first, higher byte second.
134 cpu_addr_buffer = ((cpu_addr_buffer & 0xff00) | byte);
136 cpu_addr_buffer = ((cpu_addr_buffer & 0x00ff) | (byte << 8));
137 return cpu_addr_buffer;
140 static int reset_handler2(void) {
142 ret = reset_exec6502();
144 fprintf(stderr, "cpu reset failure.\n");
145 critical_error = TRUE;
149 if (test_and_set_intr()) {
150 execute_func = fetch_and_decode_inst;
155 static int reset_handler1(void) {
159 fprintf(stderr, "cpu reset failure.\n");
160 critical_error = TRUE;
163 execute_func = reset_handler2;
167 void reset_cpu(void) {
171 static int nmi_handler(void) {
177 fprintf(stderr, "cpu nmi handling failure.\n");
180 critical_error = TRUE;
184 //last cycle goes to next execute cycle
185 if (test_and_set_intr()) {
188 execute_func = fetch_and_decode_inst;
193 static int decode_inst(void) {
196 ret = decode6502(cpu_data_buffer);
201 static int fetch_and_decode_inst(void) {
205 //if nmi occurred, no further execution on the current instruction.
207 execute_func = nmi_handler;
208 return execute_func();
212 dump_6502_level2(TRUE);
215 if (break_point == pc) {
218 if ((break_nmi_point == get_nmi_cnt()) && (break_counter_point == clock_cnt)) {
230 //if bus not ready, do nothing.
240 fprintf(stderr, "cpu decode instruction failure.\n");
243 critical_error = TRUE;
249 execute_func = execute_inst;
252 //dprint("%d\n", clock_cnt);
258 static int execute_inst(void) {
261 //dprint("execute\n");
263 //execute the instruction
272 critical_error = TRUE;
277 //last cycle goes to next execute cycle
278 if (test_and_set_exec()) {
279 execute_func = fetch_and_decode_inst;
284 void set_cpu_addr_buf(unsigned short addr) {
285 cpu_addr_buffer = addr;
288 unsigned short get_cpu_addr_buf(void) {
289 return cpu_addr_buffer;
292 void set_cpu_data_buf(unsigned char data) {
293 cpu_data_buffer = data;
296 unsigned char get_cpu_data_buf(void) {
297 return cpu_data_buffer;
301 static void null_dump_6502 (int param) {}
302 static void null_load_store (unsigned short addr, unsigned char data) {}
303 static int null_disas(unsigned short addr) {return 0;}
305 static void dump_load (unsigned short addr, unsigned char data) {
308 dprint("load: @%04x = %02x\n", addr, data);
310 static void dump_store (unsigned short addr, unsigned char data) {
312 dprint("store: @%04x = %02x\n", addr, data);
318 ret = init_6502core();
323 ret = register_cpu_clock();
339 /*------for debug.c-----*/
340 void d1_set(int on_off) {
342 d1_disas = disas_inst;
345 d1_disas = null_disas;
349 void d2_set(int on_off) {
351 dump_6502_level2 = dump_6502;
354 dump_6502_level2 = null_dump_6502;
358 void d3_set(int on_off) {
360 dump_6502_level3_load = dump_load;
361 dump_6502_level3_store = dump_store;
364 dump_6502_level3_load = null_load_store;
365 dump_6502_level3_store = null_load_store;