Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc-extra / emu / rrgb / z80.c
blob4fc3a26ca6ef4ae67f4e50e72b3897c9419958f6
1 /* Emulation of the Z80 CPU with hooks into the other parts of Z81.
2 * Copyright (C) 1994 Ian Collier.
3 * Z81 changes (C) 1995 Russell Marks.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <math.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <time.h>
24 #include "z80.h"
25 #include "debugger.h"
26 #include "parse_map.h"
27 #include "disgb.h"
29 pmfunction fun_first;
31 unsigned short breakpoints[5];
32 unsigned short callback_stack[100];
34 int callback_stack_pos;
35 extern unsigned long tstates;
37 void push_callback( unsigned short addr )
39 pmfunction walk;
40 int done = 0;
42 walk=fun_first;
43 while ((walk!=NULL)&&(!done)) {
44 if (walk->addr==addr) {
45 walk->start = tstates;
46 walk->num_calls++;
47 done=1;
49 else
50 walk = walk->next;
52 if (!done) {
53 /* New one */
54 walk=fun_first;
55 fun_first=malloc(sizeof(mfunction));
56 fun_first->next=walk;
57 fun_first->num_calls=1;
58 fun_first->tstates=0;
59 fun_first->addr=addr;
60 fun_first->start=tstates;
62 callback_stack[callback_stack_pos++] = addr;
65 void pop_callback(void)
67 pmfunction walk;
69 if (callback_stack_pos > 0) {
70 callback_stack_pos--;
71 walk=fun_first;
72 while (walk) {
73 if (walk->addr==callback_stack[callback_stack_pos]) {
74 walk->tstates+=tstates- walk->start;
75 walk = NULL;
77 else
78 walk = walk->next;
83 int mainloop(int flags)
85 unsigned char a, f, b, c, d, e, h, l;
86 unsigned char r, i, iff1, iff2, im;
87 unsigned short pc;
88 unsigned short sp;
89 extern unsigned long tstates;
90 unsigned int radjust;
91 unsigned char intsample;
92 unsigned char op;
93 int states_until_timerint;
94 unsigned long last_tstates;
95 unsigned startTime = tstates;
96 int cpuRunning = 1;
97 clock_t max_run_time = clock() + CLOCKS_PER_SEC*30;
98 unsigned run_time_check_mod = 0;
100 int j, runOne = 0;
102 struct sregs regs =
104 &a, &f, &b, &c, &d, &e, &h, &l, &pc, &sp, &cpuRunning
106 a = f = b = c = d = e = h = l = i = r = iff1 = iff2 = im = 0;
107 fun_first = NULL;
108 sp = pc = 0;
109 sp = 0xdfff;
110 pc = 0x100; /* GB start address */
111 tstates = 0;
112 last_tstates=0;
113 callback_stack_pos = 0;
114 states_until_timerint = 40000;
116 for (j = 0; j < 5; j++)
117 breakpoints[j] = 0;
119 if (flags&DSTARTDEBUG)
120 breakpoints[0] = 0x100;
122 while (cpuRunning) {
123 if (runOne) {
124 if (enterDebugger(&regs))
125 runOne = 1;
126 else
127 runOne = 0;
129 j = 0;
130 while (j < 5) {
131 if (breakpoints[j] == pc) {
132 if (enterDebugger(&regs))
133 runOne = 1;
134 break;
136 j++;
138 op = fetch(pc);
139 pc++;
140 switch (op) {
141 #include "z80ops.c"
142 default:
143 printf("\nrrgb: warning - invalid opcode %02x at %04x.\n", op, pc - 1);
144 enterDebugger(&regs);
145 break;
147 if (flags&DTIMERINT) {
148 states_until_timerint-=tstates-last_tstates;
149 last_tstates=tstates;
150 if (states_until_timerint<0) {
151 states_until_timerint=40000;
152 push2(pc);
153 pc=0x50;
156 if (flags&DLIMITEDRUN && (++run_time_check_mod) == 100) {
157 if (clock() > max_run_time) {
158 printf("Error: Maximum runtime exceeded\n");
159 cpuRunning = 0;
161 run_time_check_mod = 0;
164 return 0;