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);
15 int reset_exec6502(void);
16 int decode6502(unsigned char inst);
17 int execute6502(void);
18 int test_and_set_exec(void);
19 int test_and_set_intr(void);
21 int init_6502core(void);
24 void pc_set(unsigned short addr);
25 unsigned short pc_get(void);
26 void pc_move(int offset);
27 void report_exec_err(void);
31 int disas_inst(unsigned short addr);
32 void dump_6502(int full);
33 extern int debug_mode;
34 extern unsigned short break_point;
35 extern int dbg_log_msg;
36 extern int critical_error;
39 static unsigned char cpu_data_buffer;
40 static unsigned short cpu_addr_buffer;
43 * clock execution function array.
44 * max execution cycle is 7.
45 * the last element must be NULL.
47 clock_func_t *execute_func;
49 static unsigned long clock_cnt;
52 int get_clock_cnt(void) {
63 //dprint("%d\n", clock_cnt);
71 unsigned char load_memory(unsigned short addr) {
76 cpu_data_buffer = get_bus_data();
81 dprint("load: @%04x = %02x\n", addr, cpu_data_buffer);
82 return cpu_data_buffer;
85 void store_memory(unsigned short addr, unsigned char data) {
87 dprint("store: @%04x = %02x\n", addr, data);
95 cpu_addr_buffer = addr;
96 cpu_data_buffer = data;
100 * load address in the memory.
101 * loading 2 bytes takes 2 cycles.
102 * the parameter "cycle" means first or second read.
104 unsigned short load_addr(unsigned short addr, int cycle) {
105 unsigned short byte = load_memory(addr);
107 ///NES=little endian. lower byte first, higher byte second.
109 cpu_addr_buffer = ((cpu_addr_buffer & 0xff00) | byte);
111 cpu_addr_buffer = ((cpu_addr_buffer & 0x00ff) | (byte << 8));
112 return cpu_addr_buffer;
115 static int reset_handler2(void) {
117 ret = reset_exec6502();
119 fprintf(stderr, "cpu reset failure.\n");
120 critical_error = TRUE;
124 if (test_and_set_intr()) {
125 execute_func = fetch_and_decode_inst;
130 static int reset_handler1(void) {
134 fprintf(stderr, "cpu reset failure.\n");
135 critical_error = TRUE;
138 execute_func = reset_handler2;
142 void reset_cpu(void) {
143 execute_func = reset_handler1;
146 static int nmi_handler(void) {
152 fprintf(stderr, "cpu nmi handling failure.\n");
155 critical_error = TRUE;
159 //last cycle goes to next execute cycle
160 if (test_and_set_intr()) {
163 execute_func = fetch_and_decode_inst;
168 static int decode_inst(void) {
171 ret = decode6502(cpu_data_buffer);
176 static int fetch_and_decode_inst(void) {
180 //if nmi occurred, no further execution on the current instruction.
182 execute_func = nmi_handler;
183 return execute_func();
189 if (break_point == pc) {
210 fprintf(stderr, "cpu decode instruction failure.\n");
213 critical_error = TRUE;
219 execute_func = execute_inst;
225 static int execute_inst(void) {
228 //dprint("execute\n");
230 //execute the instruction
239 critical_error = TRUE;
244 //last cycle goes to next execute cycle
245 if (test_and_set_exec()) {
246 execute_func = fetch_and_decode_inst;
251 void set_cpu_addr_buf(unsigned short addr) {
252 cpu_addr_buffer = addr;
255 unsigned short get_cpu_addr_buf(void) {
256 return cpu_addr_buffer;
259 void set_cpu_data_buf(unsigned char data) {
260 cpu_data_buffer = data;
263 unsigned char get_cpu_data_buf(void) {
264 return cpu_data_buffer;
270 ret = init_6502core();
275 ret = register_clock_hander(clock_cpu);