aboutsummaryrefslogtreecommitdiff
path: root/6502.c
blob: 0f9c95435873e0f0749909f6abd13bf376972c1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*

    6502 - 6502 emulation

    Copyright (C) 2025  Ian Cowburn <ianc@noddybox.co.uk>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, see <http://www.gnu.org/licenses/>

    -------------------------------------------------------------------------

    6502

*/
#include <stdlib.h>
#include <string.h>

#include "6502.h"


/* ---------------------------------------- MACROS
*/
#define PRIV                    cpu->priv
#define MAX_PER_CALLBACK        10
#define TRUE                    1
#define FALSE                   0

#define CALLBACK(r,d)	do					\
			{					\
			int f;					\
								\
			for(f = 0; f < MAX_PER_CALLBACK; f++)	\
			    if (PRIV->callback[r][f])		\
				PRIV->last_cb &=		\
				    PRIV->callback[r][f](cpu,d);\
			} while(0)

/* ---------------------------------------- PRIVATE DATA AND TYPES
*/
struct C6502Private
{
    C6502Val            cycles;
    C6502Val            timer[e6502_NO_TIMERS];
    C6502ReadMemory     mread;
    C6502WriteMemory    mwrite;
    C6502ReadMemory     disread;
    C6502Callback       callback[e6502_NO_CALLBACK][MAX_PER_CALLBACK];
    C6502Label          *labels;
    int                 last_cb;
};

/* ---------------------------------------- PRIVATE FUNCTIONS
*/
static void InitTables()
{
    static int init=FALSE;

    if (init)
    	return;

    init=TRUE;

    /* TODO */
}

static void C6502_CheckInterrupt(C6502 *cpu)
{
    /* TODO */
}


/* ---------------------------------------- INTERFACES
*/
C6502     *C6502Init(C6502ReadMemory read_memory,
                     C6502WriteMemory write_memory,
                     C6502ReadMemory read_for_disassem)
{
    C6502 *cpu;
    int f;
    int r;

    InitTables();

    cpu = malloc(sizeof *cpu);

    if (cpu)
    {
    	PRIV = malloc(sizeof *cpu->priv);

	if (PRIV)
	{
	    PRIV->mread = read_memory;
	    PRIV->mwrite = write_memory;
	    PRIV->disread = read_for_disassem;

	    for(f = 0; f < eC6502_NO_CALLBACK; f++)
		for(r = 0; r < MAX_PER_CALLBACK; r++)
		    PRIV->callback[f][r] = NULL;

	    C6502Reset(cpu);
	}
	else
	{
	    free(cpu);
	    cpu = NULL;
	}
    }

    return cpu;
}


void Z80Reset(Z80 *cpu)
{
    PRIV->cycle = 0;
    PRIV->timer[e6502_TIMER_1] = 0;
    PRIV->timer[e6502_TIMER_2] = 0;
    PRIV->timer[e6502_TIMER_3] = 0;

    cpu->PC = 0;

    /* TODO */

}


C6502Val C6502Cycles(C6502 *cpu)
{
    return PRIV->cycle;
}


void C6502ResetCycles(C6502 *cpu, C6502Val cycles)
{
    PRIV->cycle = cycles;
}


C6502Val C6502GetTimer(C6502 *cpu, C6502Timer timer)
{
    return PRIV->timer[timer];
}


void C6502SetTimer(C6502 *cpu, C6502Timer timer, C6502Val cycles)
{
    PRIV->timer[timer] = cycles;
}


int C6502LodgeCallback(C6502 *cpu,
                       C6502CallbackReason reason,
                       C6502Callback callback)
{
    int f;

    for(f = 0; f < MAX_PER_CALLBACK; f++)
    {
    	if (!PRIV->callback[reason][f])
	{
	    PRIV->callback[reason][f] = callback;
	    return TRUE;
	}
    }

    return FALSE;
}


void C6502RemoveCallback(C6502 *cpu,
                         C6502CallbackReason reason,
                         C6502Callback callback)
{
    int f;

    for(f = 0; f < MAX_PER_CALLBACK; f++)
    {
    	if (PRIV->callback[reason][f] == callback)
	{
	    PRIV->callback[reason][f] = NULL;
	}
    }
}


void C6502Interrupt(C6502 *cpu)
{
    PRIV->raise = TRUE;
    PRIV->nmi = FALSE;
}


void C6502NMI(C6502 *cpu)
{
    PRIV->raise = TRUE;
    PRIV->nmi = TRUE;
}


int C6502SingleStep(C6502 *cpu)
{
    C6502_CheckInterrupt(cpu);

    CALLBACK(eC6502_Instruction, PRIV->cycle);

    INC_R;

    opcode=FETCH_BYTE;

    C6502_Decode(cpu,opcode);

    return PRIV->last_cb;
}


void C6502Exec(C6502 *cpu)
{
    while (C6502SingleStep(cpu));
}


void C6502SetLabels(C6502 *cpu, C6502Label labels[])
{
    PRIV->labels = labels;
}


const char *C6502Disassemble(C6502 *cpu, C6502Word *pc)
{
#ifdef ENABLE_DISASSEM
    C6502Byte C6502_Dis_FetchByte(C6502 *cpu, C6502Word *pc);
    static char s[80];
    C6502Word opc,npc;
    C6502Byte op;
    int f;

    opc=*pc;
    op=C6502_Dis_FetchByte(cpu,pc);
    dis_opcode_z80[op](cpu,op,pc);
    npc=*pc;

    strcpy(s,C6502_Dis_Printf("%-5s",C6502_Dis_GetOp()));
    strcat(s,C6502_Dis_Printf("%-40s ;",C6502_Dis_GetArg()));

    for(f=0;f<5 && opc!=npc;f++)
    {
#ifdef ENABLE_ARRAY_MEMORY
	strcat(s,C6502_Dis_Printf(" %.2x",(int)C6502_MEMORY[opc++]));
#else
	strcat(s,C6502_Dis_Printf(" %.2x",(int)PRIV->disread(cpu,opc++)));
#endif
    }

    if (opc!=npc)
	for(f=1;f<3;f++)
	    s[strlen(s)-f]='.';

    return s;
#else
    (*pc)+=4;
    return "NO DISASSEMBLER";
#endif
}


/* END OF FILE */