-
Notifications
You must be signed in to change notification settings - Fork 19
/
cpu.h
395 lines (335 loc) · 8.54 KB
/
cpu.h
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* cpu.h
*
* (C) Copyright 2014 Ulrich Hecht
*
* This file is part of CASCADE. CASCADE is almost free software; you can
* redistribute it and/or modify it under the terms of the Cascade Public
* License 1.0. Read the file "LICENSE" for details.
*/
#ifndef _CPU_H
#define _CPU_H
#include <stdint.h>
#include "debug.h"
#include "state.h"
#include "ring.h"
#ifdef LATENCY
#include <sys/time.h>
#endif
#define CPU_CMD_NONE 0
#define CPU_CMD_EXIT 1
#define CPU_CMD_TOGGLE_ECHO 2
#define CPU_CMD_RATE 3
#define CPU_CMD_SAVE 4
#define CPU_CMD_LOAD 5
#define CPU_CMD_RESET 6
#define CPU_CMD_FRESET 7
#define CPU_CMD_RECORD 8
#define CPU_CMD_PLAY 9
#define CPU_CMD_STOP_RECPLAY 10
#define CPU_CMD_LOAD_ROM 11
/* documented in 272238 C-52 */
#define PSW_ST (1<<0)
#define PSW_INTE (1<<1)
#define PSW_PTSE (1<<2)
#define PSW_C (1<<3)
#define PSW_VT (1<<4)
#define PSW_V (1<<5)
#define PSW_N (1<<6)
#define PSW_Z (1<<7)
#define EVENT_INVALID 0
#define EVENT_KEYDOWN 1
#define EVENT_KEYUP 2
#define EVENT_SERIALRX 3
#define EVENT_SERIALRXBIT 4
#define EVENT_SERIALSTAT 5
#define EVENT_EEPROMREAD 6
// recorded event structure
struct Event {
uint64_t cycles;
int type;
int value;
};
extern uint32_t debug_level;
extern uint32_t debug_level_unabridged;
class Serial;
class Keypad;
class Lcd;
class Eeprom;
class Interface;
class UI;
class Hsio;
class Hints;
class Cpu {
friend class Keypad;
public:
Cpu(UI *ui);
~Cpu();
void reset();
void setRomSize(size_t size);
void setMappedRamSize(size_t size);
bool loadRom(const char* name);
bool loadExtendedRom(const char *name);
void enableRecording(const char *rname);
void disableRecording();
void enableReplaying(const char *rname);
void setSerial(Interface *iface, bool expect_echo);
void setWatchpoint(uint16_t lo, uint16_t hi);
void setMaximumCycles(uint64_t max);
void setDebugTrigger(uint32_t trigger, uint32_t level);
int emulate(void);
void dumpMem();
void setSlowDown(float factor);
void recordEvent(int type, int value);
struct Event retrieveEvent(int type);
int retrieveEventValue(int type);
inline uint64_t getCycles() {
return cycles;
}
inline bool isReplaying() {
return replaying;
}
inline uint32_t getClock() {
return oclock;
}
bool is_hiscan;
int serDivToBaud(uint16_t divisor) {
return getClock() / 2 / (divisor & 0x7fff) / 8;
}
uint16_t serBaudToDiv(int baud) {
return (getClock() / 2 / baud / 8) | 0x8000;
}
void sync(bool exact);
void sendCommand(int cmd);
Lcd *getLcd() {
return lcd;
}
void stop() {
emulation_stopped = true;
}
void resume() {
emulation_stopped = false;
}
protected:
inline uint32_t virtToPhys(uint16_t addr, int fetch) {
if (addr < 0xc000)
return addr;
else
return virtToPhysSlow(addr, fetch);
}
virtual uint8_t ioRead8(uint16_t addr);
virtual void ioWrite8(uint16_t addr, uint8_t value);
uint8_t mappedRead8(uint16_t addr, int fetch);
uint8_t memRead8Bus(uint16_t addr, int fetch);
typedef uint8_t (Cpu::*memReader)(uint16_t addr);
inline uint8_t memRead8(uint16_t addr) {
switch (addr) {
case 0 ... 1: return 0;
case 0x18 ... 0xff:
case 0x2000 ... 0xbfff:
return memRead8Ram(addr);
case 0xc000 ... 0xffff:
return memRead8Mapped(addr);
default:
return memRead8Slow(addr);
}
}
uint8_t memRead8Ram(uint16_t addr) {
#ifndef NDEBUG
if (watchpoint_lo && watchpoint_lo <= addr && watchpoint_hi >= addr)
DEBUG(WARN, "%04X/%08X: WATCH READ %04X: %02X\n", opc, virtToPhys(opc, 1), addr, ram[addr]);
#endif
return ram[addr];
}
uint8_t memRead8Null(uint16_t addr) {
return 0;
}
uint8_t memRead8Slow(uint16_t addr) {
return memRead8Bus(addr, 0);
}
uint8_t memRead8Mapped(uint16_t addr) {
return data_ptr[addr - 0xc000];
}
inline uint16_t memRead16(uint16_t addr) {
return memRead8(addr) | (memRead8(addr + 1) << 8);
}
inline uint32_t memRead32(uint16_t addr) {
return memRead16(addr) | (memRead16(addr + 2) << 16);
}
inline uint8_t fetch(void) {
if (pc >= 0xc000)
return code_ptr[pc++ - 0xc000];
else
return memRead8Bus(pc++, 1);
}
inline uint8_t peek(int offset) {
return memRead8Bus(pc + offset, 1);
}
inline uint16_t fetch16(void) {
return (uint16_t)fetch() | ((uint16_t)fetch() << 8);
}
inline uint16_t peek16(int offset) {
return (uint16_t)peek(offset) | ((uint16_t)peek(offset + 1) << 8);
}
typedef void (Cpu::*memWriter)(uint16_t addr, uint8_t value);
inline void memWrite8(uint16_t addr, uint8_t value) {
switch (addr) {
case 0 ... 0x17:
case 0x200 ... 0x2ff:
ioWrite8(addr, value);
break;
case 0x18 ... 0xff:
case 0x2000 ... 0xbfff:
memWrite8Ram(addr, value);
break;
case 0xc000 ... 0xffff:
memWrite8Mapped(addr, value);
break;
default:
memWrite8Slow(addr, value);
break;
}
}
void memWrite8Slow(uint16_t addr, uint8_t value);
void memWrite8Mapped(uint16_t addr, uint8_t value) {
DEBUG(MEM, "WRITE %02X -> %04X\n", value, addr);
data_ptr[addr - 0xc000] = value;
}
void memWrite8Ram(uint16_t addr, uint8_t value) {
DEBUG(MEM, "WRITE %02X -> %04X\n", value, addr);
#ifndef NDEBUG
if (watchpoint_lo && addr >= watchpoint_lo && addr <= watchpoint_hi)
DEBUG(WARN, "%04X/%08X: WATCH %04X: %02X -> %02X\n", opc, virtToPhys(opc, 1), addr, memRead8(addr), value);
#endif
ram[addr] = value;
}
inline void memWrite16(uint16_t addr, uint16_t value) {
memWrite8(addr, value & 0xff);
memWrite8(addr + 1, value >> 8);
}
inline void memWrite32(uint16_t addr, uint32_t value) {
memWrite16(addr, value & 0xffff);
memWrite16(addr + 2, value >> 16);
}
inline void push16(uint16_t word) {
uint16_t new_sp = memRead16(0x18) - 2;
memWrite16(0x18, new_sp);
memWrite16(new_sp, word);
}
inline uint16_t pop16(void) {
uint16_t sp = memRead16(0x18);
uint16_t val = memRead16(sp);
memWrite16(0x18, sp + 2);
return val;
}
inline uint16_t indexedAddr(void) {
uint8_t imm8 = fetch();
uint16_t target;
if (imm8 & 1) {
target = fetch16() + memRead16(imm8 & 0xfe);
cycle(1);
}
else {
target = fetch() + memRead16(imm8);
}
return target;
}
uint32_t virtToPhysSlow(uint16_t addr, int fetch);
inline void cycle(int c) {
cycles += c;
}
inline void cycleShift(int c, int shift) {
cycles += c + (shift? shift : 1);
}
inline void cycleRM3(int c, uint16_t addr) {
cycles += c;
if (addr >= 0x200)
cycles += 3;
}
inline void cycleRM2(int c, uint16_t addr) {
cycles += c;
if (addr >= 0x200)
cycles += 2;
}
void resetTiming();
const char *disassemble();
private:
inline uint16_t getTimer1() {
return (uint16_t)(getCycles() / 8) + timer1_offset;
}
bool loadSaveState(const char *name, bool write);
bool loadSaveState(statefile_t fp, bool write);
uint32_t clock, oclock;
#ifndef NDEBUG
uint16_t watchpoint_lo, watchpoint_hi;
uint32_t trigger;
uint32_t trigger_level;
uint8_t *mem_profile;
#endif
const uint8_t *rom;
uint8_t *ram;
const uint8_t *exrom;
#ifdef LATENCY
struct latency_t {
uint16_t min;
uint16_t max;
uint32_t total;
uint32_t count;
uint8_t cycles;
uint8_t opcode;
uint8_t _reserved[2];
};
struct latency_t *mem_latency;
struct timeval tv, tv2;
bool do_latency;
#endif
uint8_t *mapped_ram;
const uint8_t *code_ptr;
uint8_t *data_ptr;
uint16_t pc;
uint16_t opc; /* PC at start of insn */
uint8_t psw;
uint8_t code_hi, code_lo;
uint8_t data_hi, data_lo;
uint8_t wsr;
uint8_t wsr1;
uint8_t int_mask, int_mask1;
uint16_t ptssel, ptssrv;
uint8_t ad_command;
uint64_t cycles;
uint64_t end_cycles;
uint8_t ioc0, ioc1, ios0, ios1;
uint16_t last_ios1_read;
Lcd *lcd;
Serial *serial;
uint8_t ioport1, ioport2;
uint16_t ad_result;
uint16_t timer1_offset;
uint32_t timer2;
int32_t timer2_inc_factor;
Eeprom* eeprom;
uint32_t starttime;
uint32_t oldtime;
uint32_t nowtime;
uint64_t oldcycles;
uint64_t next_sampling;
uint64_t next_lcd_update;
uint64_t next_event_pumping;
float slowdown;
Hsio *hsi;
Keypad *keypad;
UI *ui;
Hints *hints;
// event recording/replaying
bool recording, replaying;
char *record_file_name;
statefile_t record_file;
struct Event current_event;
uint32_t rom_size;
uint32_t exrom_size;
char *rom_name;
char *exrom_name;
Ring<int> *cmd_queue;
bool emulation_stopped;
};
#endif