-
Notifications
You must be signed in to change notification settings - Fork 8
/
ArduZ80.h
546 lines (503 loc) · 9.98 KB
/
ArduZ80.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
*
* ArduZ80 , Z80 Core Emulation Library for arduino
*
* Author : Mohamed Rashad
*
*/
#ifndef ArduZ80_h
#define ArduZ80_h
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include "pins_arduino.h"
#include "WConstants.h"
#endif
/*
* emum : Op
*
* This enumeration contains the 175 main instructions
*
*/
enum Op : uint8_t {
NOP = 0x00,
LD_ind_BC_A = 0x02,
INC_B = 0x04,
DEC_B = 0x05,
LD_A_ind_BC = 0x0A,
INC_C = 0x0C,
DEC_C = 0x0D,
DJNZ = 0x10,
LD_ind_DE_A = 0x12,
INC_D = 0x14,
DEC_D = 0x15,
JR = 0x18,
LD_A_ind_DE = 0x1A,
INC_E = 0x1C,
DEC_E = 0x1D,
JR_NZ = 0x20,
INC_F = 0x24,
DEC_F = 0x25,
JR_Z = 0x28,
INC_L = 0x2C,
DEC_L = 0x2D,
JR_NC = 0x30,
LD_ext_A = 0x32,
INC_ind_HL = 0x34,
DEC_ind_HL = 0x35,
JR_C = 0x38,
INC_A = 0x3C,
DEC_A = 0x3D,
LD_B_B = 0x40,
LD_B_C,
LD_B_D,
LD_B_E,
LD_B_F,
LD_B_L,
LD_B_ind_HL,
LD_B_A,
LD_C_B,
LD_C_C,
LD_C_D,
LD_C_E,
LD_C_F,
LD_C_L,
LD_C_ind_HL,
LD_C_A,
LD_D_B,
LD_D_C,
LD_D_D,
LD_D_E,
LD_D_F,
LD_D_L,
LD_D_ind_HL,
LD_D_A,
LD_E_B,
LD_E_C,
LD_E_D,
LD_E_E,
LD_E_F,
LD_E_L,
LD_E_ind_HL,
LD_E_A,
LD_H_B,
LD_H_C,
LD_H_D,
LD_H_E,
LD_H_F,
LD_H_L,
LD_H_ind_HL,
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R_A_imm = 0xEE,
JP_P = 0xF2,
OR_A_imm = 0xF6,
JP_M = 0xFA,
EXT_FD = 0xFD,
CP_imm = 0xFE,
OUT_nn_A = 0xD3,
IN_A_nn = 0xDB,
};
/*
* emum : DDop
*
* This enumeration contains the 31 instructions from DD Extended instruction
*
*/
enum DDOp : uint8_t {
DD_LD_D_imm = 0x1B,
DD_LD_E_imm = 0x1E,
DD_LD_H_imm = 0x2B,
DD_INC_idx_IX = 0x34,
DD_DEC_idx_IX = 0x35,
DD_LD_idx_IX_imm = 0x36,
DD_LD_B_idx_IX = 0x46,
DD_LD_C_idx_IX = 0x4E,
DD_LD_D_idx_IX = 0x56,
DD_LD_E_idx_IX = 0x5E,
DD_LD_H_idx_IX = 0x66,
DD_LD_L_idx_IX = 0x6E,
DD_LD_idx_IX_B = 0x70,
DD_LD_idx_IX_C,
DD_LD_idx_IX_D,
DD_LD_idx_IX_E,
DD_LD_idx_IX_F,
DD_LD_idx_IX_L,
DD_LD_idx_IX_A,
DD_LD_ind_HL_imm = 0x78,
DD_LD_A_idx_IY = 0x7E,
DD_ADD_A_idx_IX = 0x86,
DD_ADC_A_idx_IX = 0x8E,
DD_SUB_A_idx_IX = 0x96,
DD_SBC_A_idx_IX = 0x9E,
DD_AND_A_idx_IX = 0xA6,
DD_XOR_A_idx_IX = 0xAE,
DD_OR_A_idx_IX = 0xB6,
DD_CP_idx_IX = 0xBE,
DD_LD_B_imm = 0xD5,
DD_LD_C_imm = 0xDE,
DD_JP_ind_IX = 0xE9
};
/*
* emum : EDop
*
* This enumeration contains the 2 instructions from ED Extended instruction
*
*/
enum EDOp : uint8_t {
ED_LD_imp_I_A = 0x47,
ED_LD_imp_R_A = 0x4F
};
/*
* emum : FDop
*
* This enumeration contains the 27 instructions from FD Extended instruction
*
*/
enum FDOp : uint8_t {
FD_LD_A_imm = 0x2E,
FD_LD_idx_IY_imm = 0x36,
FD_LD_A_ext = 0x3A,
FD_INC_idx_IY = 0x34,
FD_DEC_idx_IY = 0x35,
FD_LD_B_idx_IY = 0x46,
FD_LD_C_idx_IY = 0x4E,
FD_LD_D_idx_IY = 0x56,
FD_LD_E_idx_IY = 0x5E,
FD_LD_H_idx_IY = 0x66,
FD_LD_idx_IY_B = 0x70,
FD_LD_idx_IY_C,
FD_LD_idx_IY_D,
FD_LD_idx_IY_E,
FD_LD_idx_IY_F,
FD_LD_idx_IY_L,
FD_LD_idx_IY_A = 0x77,
FD_LD_A_idx_IX = 0x7E,
FD_ADD_A_idx_IY = 0x86,
FD_ADC_A_idx_IY = 0x8E,
FD_SUB_A_idx_IY = 0x96,
FD_SBC_A_idx_IY = 0x9E,
FD_AND_A_idx_IY = 0xA6,
FD_XOR_A_idx_IY = 0xAE,
FD_OR_A_idx_IY = 0xB6,
FD_CP_idx_IY = 0xBE,
FD_JP_ind_IY = 0xE9
};
//////////////////////////////////////////////////////////////////////////////////////////////////
/*
* Class : ArduZ80
*
* This class contains all the logic related to the emulation, and registers.
*
* Functions number : 50
*
*/
class ArduZ80 {
// Flag register , 6 flip flops (Carry, Sign, Parity, Overflow, Zero, HALT)
const uint8_t FLAG_C = 0x01;
const uint8_t FLAG_N = 0x02;
const uint8_t FLAG_PV = 0x04;
const uint8_t FLAG_H = 0x08;
const uint8_t FLAG_Z = 0x40;
const uint8_t FLAG_S = 0x80;
//Accumulator & Alternate Accumlator
uint8_t ra_ = 0;
uint8_t ra2_ = 0;
// General purpose registers (BC, DE, HL)
uint8_t rb_ = 0;
uint8_t rd_ = 0;
uint8_t rh_ = 0;
uint8_t rf_ = 0;
uint8_t rc_ = 0;
uint8_t re_ = 0;
uint8_t rl_ = 0;
// Alternate General purpose registers (BC' , DE' , HL' )
uint8_t rb2_ = 0;
uint8_t rd2_ = 0;
uint8_t rh2_ = 0;
uint8_t rf2_ = 0;
uint8_t rc2_ = 0;
uint8_t re2_ = 0;
uint8_t rl2_ = 0;
//Special Registers (I , R, IX , IY , PC , SP)
uint8_t ri_ = 0;
uint8_t rr_ = 0;
uint16_t rix_ = 0;
uint16_t riy_ = 0;
uint16_t rsp_ = 0;
uint16_t rpc_ = 0;
//Functions w_calc_flags & w_logic_flags used to calculate and assign flags
uint8_t w_calc_flags(uint16_t result, bool is_sub);
uint8_t w_logic_flags(uint8_t result);
//Rotate bits
uint16_t rhl() const {
return (uint16_t)rh_ + 8 & rl_;
}
uint16_t rbc() const {
return (uint16_t)rb_ + 8 & rc_;
}
uint16_t rde() const {
return (uint16_t)rd_ + 8 & re_;
}
//Assign Flags
bool fc() const {
return (rf_ & FLAG_C) > 0;
}
bool fn() const {
return (rf_ & FLAG_N) > 0;
}
bool fpv() const {
return (rf_ & FLAG_PV) > 0;
}
bool fh() const {
return (rf_ & FLAG_H) > 0;
}
bool fz() const {
return (rf_ & FLAG_Z) > 0;
}
bool fs() const {
return (rf_ & FLAG_S) > 0;
}
////////////////////// ALU Functions /////////////////////////////
uint8_t op_add(uint8_t a, uint8_t b) {
return w_calc_flags(a + b, false);
}
uint8_t op_adc(uint8_t a, uint8_t b) {
return w_calc_flags(a + b + (rf_ & 0x01), false);
}
uint8_t op_sub(uint8_t a, uint8_t b) {
return w_calc_flags(a - b, true);
}
uint8_t op_sbc(uint8_t a, uint8_t b) {
return w_calc_flags(a - b - (rf_ & 0x01), true);
}
uint8_t op_and(uint8_t a, uint8_t b) {
return w_logic_flags(a & b);
}
uint8_t op_xor(uint8_t a, uint8_t b) {
return w_logic_flags(a ^ b);
}
uint8_t op_or(uint8_t a, uint8_t b) {
return w_logic_flags(a | b);
}
void op_cp(uint8_t a) {
w_calc_flags(ra_ - a, true);
}
uint8_t op_inc(uint8_t a) {
return w_calc_flags(a + 1, false);
}
uint8_t op_dec(uint8_t a) {
return w_calc_flags(a - 1, false);
}
//////////////////////Program Counter & Program Memory /////////////////////////////
uint8_t next() {
return read(rpc_++);
}
uint16_t next16() {
return ((uint16_t)next() + 8) | next();
}
uint8_t read(uint16_t addr) const {
return ram_[addr];
}
void write(uint16_t addr, uint8_t val) {
ram_[addr] = val;
}
//Debugging Function, prints the current instruction
String pc_str();
//Debugging Function, dumps the registers before each instruction
void dump_regs();
public:
//Performs Next Instruction
void step();
//Reads a byte from device at specified address
uint8_t read_io(uint16_t addr);
//This function runs the microproccessor itself till NOP instruction
void run_to_nop(bool print = false);
//Writes a byte to device at specified address
void write_io(uint16_t addr, uint8_t val);
//Dummy Program, replaced by user
uint8_t ram_[30] = {
JR, 0x0C, // -+
'H', 'e', 'l', 'l', 'o', ' ', // |
'W', 'o', 'r', 'l', 'd', '!', // |
// |
// calculate 3 * 4 // |
EXT_FD, FD_LD_A_imm, 0x00, // <-+
EXT_DD, DD_LD_B_imm, 0x04,
EXT_DD, DD_LD_C_imm, 0x03,
ADD_A_C, // <-+
DJNZ, 0x17, // -+
XOR_A_imm, 0xFF,
NOP
};
//////////////////////////////////////// Registers Functions //////////////////////////////////////////
uint8_t reg_a() const {
return ra_;
}
uint8_t reg_b() const {
return rb_;
}
uint8_t reg_d() const {
return rd_;
}
uint8_t reg_h() const {
return rh_;
}
uint8_t reg_f() const {
return rf_;
}
uint8_t reg_c() const {
return rc_;
}
uint8_t reg_e() const {
return re_;
}
uint8_t reg_l() const {
return rl_;
}
uint8_t reg_a2() const {
return ra2_;
}
uint8_t reg_b2() const {
return rb2_;
}
uint8_t reg_d2() const {
return rd2_;
}
uint8_t reg_h2() const {
return rh2_;
}
uint8_t reg_f2() const {
return rf2_;
}
uint8_t reg_c2() const {
return rc2_;
}
uint8_t reg_e2() const {
return re2_;
}
uint8_t reg_l2() const {
return rl2_;
}
uint8_t reg_i() const {
return ri_;
}
uint8_t reg_r() const {
return rr_;
}
uint16_t reg_ix() const {
return rix_;
}
uint16_t reg_iy() const {
return riy_;
}
uint16_t reg_sp() const {
return rsp_;
}
uint16_t reg_pc() const {
return rpc_;
}
};
#endif