-
Notifications
You must be signed in to change notification settings - Fork 7
/
CPU.c
334 lines (227 loc) · 8.69 KB
/
CPU.c
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//
// CPU.c
// The Omega Project
// https://github.com/h5n1xp/Omega
//
// Created by Matt Parsons on 02/02/2019.
// Copyright © 2019 Matt Parsons. All rights reserved.
// <[email protected]>
//
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <stdio.h>
#include "CPU.h"
#include "m68k.h"
#include "Memory.h"
#include "CIA.h"
unsigned char* _emulatorMemory;
//#define NOSLOWRAM
//#define CHIPTOP 0x3FFFF // for 256K Chip
//#define CHIPTOP 0x7FFFF // for 512K
//#define CHIPTOP 0xFFFFF // for 1Meg
//#define CHIPTOP 0x1FFFFF // for 2Meg
/* Called when the CPU pulses the RESET line */
void cpu_pulse_reset(void){
uint32_t pc = m68k_get_reg(NULL, M68K_REG_PC);
uint32_t sp = m68k_get_reg(NULL, M68K_REG_SP);
printf("PC:%0x6\n",pc);
printf("SP:%0x6\n",sp);
printf("Machine reset...\n");
//clear RAM (don't wipe long at address 0 as that signals a Guru occured)
for(int i=4;i<0x1FFFFF;i++){
low16Meg[i]=0;
}
ChipsetInit();
m68k_set_reg(M68K_REG_PC, 0xF80002); //start executing at the ROM + 2
}
void cpu_init(){
//set CIA IDs
CIAInit(&CIAA, 32776);
CIAInit(&CIAB, 40960);
m68k_init();
m68k_set_cpu_type(M68K_CPU_TYPE_68000); //Pretend to be an A500 for now...
m68k_set_reg(M68K_REG_PC, 0xF80002); //start executing at the ROM + 2
}
void cpu_execute(){
/*
int* dis = &disass;
uint32_t pc = m68k_get_reg(NULL, M68K_REG_PC);
uint32_t A0 = m68k_get_reg(NULL, M68K_REG_A0);
uint32_t A1 = m68k_get_reg(NULL, M68K_REG_A1);
uint32_t A2 = m68k_get_reg(NULL, M68K_REG_A2);
uint32_t A3 = m68k_get_reg(NULL, M68K_REG_A3);
uint32_t A4 = m68k_get_reg(NULL, M68K_REG_A4);
uint32_t A5 = m68k_get_reg(NULL, M68K_REG_A5);
uint32_t A6 = m68k_get_reg(NULL, M68K_REG_A6);
uint32_t A7 = m68k_get_reg(NULL, M68K_REG_A7);
uint32_t D0 = m68k_get_reg(NULL, M68K_REG_D0);
uint32_t D1 = m68k_get_reg(NULL, M68K_REG_D1);
uint32_t D2 = m68k_get_reg(NULL, M68K_REG_D2);
uint32_t D3 = m68k_get_reg(NULL, M68K_REG_D3);
uint32_t D4 = m68k_get_reg(NULL, M68K_REG_D4);
uint32_t D5 = m68k_get_reg(NULL, M68K_REG_D5);
uint32_t D6 = m68k_get_reg(NULL, M68K_REG_D6);
uint32_t D7 = m68k_get_reg(NULL, M68K_REG_D7);
if(pc==0xFC6CFC){
// printf("Trap\n");
//disass =1;
}
uint32_t* guru = low16Meg;
*/
m68k_execute(16);
}
void checkInterrupt(Chipset_t* chipset){
if(chipset->intenar & 0x4000){ // if master interrupt switch is enabled
uint16_t intMask = chipset->intreqr & chipset->intenar; //only set the int level, if bits are enabled
if(intMask !=0){
m68k_end_timeslice();
if(intMask & 8192){ // External int
m68k_set_irq(6);
}else if(intMask & 4096){ // Disk sync
m68k_set_irq(5);
}else if(intMask & 2048){ // Serial receive buffer full
m68k_set_irq(5);
}else if(intMask & 1024){ // Audio 3
m68k_set_irq(4);
}else if(intMask & 512){ // Audio 2
m68k_set_irq(4);
}else if(intMask & 256){ // Audio 1
m68k_set_irq(4);
}else if(intMask & 128){ // Audio 0
m68k_set_irq(4);
}else if(intMask & 64){ // Blitter finished
m68k_set_irq(3);
}else if(intMask & 32){ // VBL
m68k_set_irq(3);
}else if(intMask & 16){ // Copper
m68k_set_irq(3);
}else if(intMask & 8){ // Ports, IO and timers
m68k_set_irq(2);
}else if(intMask & 4){ // Software int
m68k_set_irq(1);
}else if(intMask & 2){ // Disk block finished
m68k_set_irq(1);
}else if(intMask & 1){ // Serial Transmit buffer empty
m68k_set_irq(1);
}
}else{
m68k_set_irq(0); // no interupt pending
}
}else{
m68k_set_irq(0); // no interupt if master INT disabled
}
}
//Emulator functions
unsigned int cpu_read_byte(unsigned int address){
//If Address > 0xFFFFFF (the low16Meg), then access the BCM2837 chip
//Not needed if running on a real RaspberryPi. just allow the CPU to access the memory.
//maskout 32bit address for now
address &=0xFFFFFF;
return chipReadByte(address);
}
unsigned int cpu_read_word(unsigned int address){
//If Address > 0xFFFFFF (the low16Meg), then access the BCM2837 chip
//Not needed if running on a real RaspberryPi. just allow the CPU to access the memory.
return chipReadWord(address);
}
unsigned int cpu_read_long(unsigned int address){
//If Address > 0xFFFFFF (the low16Meg), then access the BCM2837 chip
//Not needed if running on a real RaspberryPi. just allow the CPU to access the memory.
//maskout 32bit address for now
address &=0xFFFFFF;
return chipReadLong(address);
}
void cpu_write_byte(unsigned int address,unsigned int value){
//If Address > 0xFFFFFF (the low16Meg), then access the BCM2837 chip
//Not needed if running on a real RaspberryPi. just allow the CPU to access the memory.
chipWriteByte(address, value);
}
void cpu_write_word(unsigned int address,unsigned int value){
//If Address > 0xFFFFFF (the low16Meg), then access the BCM2837 chip
//Not needed if running on a real RaspberryPi. just allow the CPU to access the memory.
chipWriteWord(address, value);
}
void cpu_write_long(unsigned int address,unsigned int value){
//If Address > 0xFFFFFF (the low16Meg), then access the BCM2837 chip
//Not needed if running on a real RaspberryPi. just allow the CPU to access the memory.
chipWriteLong(address, value);
}
// Called when the CPU acknowledges an interrupt
int cpu_irq_ack(int level)
{
return level;
/*
switch(level)
{
case 0:
return 0;//nmi_device_ack();
case 1:
return 0;// input_device_ack();
case 2:
return 0;// output_device_ack();
}
*/
return M68K_INT_ACK_SPURIOUS;
}
unsigned int cpu_read_word_dasm(unsigned int address){
if(address > 16777216)
printf("Disassembler attempted to read word from address %x", address);
return cpu_read_word(address);
}
unsigned int cpu_read_long_dasm(unsigned int address){
if(address > 16777216)
printf("Dasm attempted to read long from address %x", address);
return cpu_read_long(address);
}
/* Disassembler */
void make_hex(char* buff, unsigned int pc, unsigned int length){
char* ptr = buff;
for(;length>0;length -= 2)
{
sprintf(ptr, "%04x", cpu_read_word_dasm(pc));
pc += 2;
ptr += 4;
if(length > 2)
*ptr++ = ' ';
}
}
void disassemble_program(){
unsigned int pc;
unsigned int instr_size;
char buff[100];
char buff2[100];
pc = cpu_read_long_dasm(4);
while(pc <= 0x16e)
{
instr_size = m68k_disassemble(buff, pc, M68K_CPU_TYPE_68000);
make_hex(buff2, pc, instr_size);
printf("%03x: %-20s: %s\n", pc, buff2, buff);
pc += instr_size;
}
fflush(stdout);
}
void cpu_instr_callback(){
// The following code will print out instructions as they are executed
static char buff[100];
static char buff2[100];
static unsigned int pc;
static unsigned int instr_size;
pc = m68k_get_reg(NULL, M68K_REG_PC);
instr_size = m68k_disassemble(buff, pc, M68K_CPU_TYPE_68000);
if(disass<1){
return;
}
make_hex(buff2, pc, instr_size);
//fprintf(globalFD,"E %03x: %-20s: %s\n", pc, buff2, buff);
printf("E %03x: %-20s: %s\n", pc, buff2, buff);
fflush(stdout);
//SDL_Delay(75);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// //
// //
// //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////