-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemu.c
3203 lines (2831 loc) · 75.1 KB
/
emu.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
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <string.h>
#include "system.h"
#define H8_DEBUG_PRINT_FETCH 0
#define H8_DEBUG_PRINT_REGISTERS 0
#define H8_ERROR(a) \
{ \
system->error_code = a; \
system->error_line = __LINE__; \
}
#define H8_OP(a) void a(h8_system_t *system)
typedef void (*H8_OP_T)(h8_system_t*);
/** A macro to unroll opcodes that take a Rd in AL into 16 functions */
#define H8_UNROLL(op) \
op(0, system->cpu.regs[0].byte.rh) \
op(1, system->cpu.regs[1].byte.rh) \
op(2, system->cpu.regs[2].byte.rh) \
op(3, system->cpu.regs[3].byte.rh) \
op(4, system->cpu.regs[4].byte.rh) \
op(5, system->cpu.regs[5].byte.rh) \
op(6, system->cpu.regs[6].byte.rh) \
op(7, system->cpu.regs[7].byte.rh) \
op(8, system->cpu.regs[0].byte.rl) \
op(9, system->cpu.regs[1].byte.rl) \
op(a, system->cpu.regs[2].byte.rl) \
op(b, system->cpu.regs[3].byte.rl) \
op(c, system->cpu.regs[4].byte.rl) \
op(d, system->cpu.regs[5].byte.rl) \
op(e, system->cpu.regs[6].byte.rl) \
op(f, system->cpu.regs[7].byte.rl)
/**
* Updates the zero and negative flags based on a given value, and sets
* overflow flag to 0.
*/
void ccr_zn(h8_system_t *system, signed val)
{
system->cpu.ccr.flags.z = val == 0;
system->cpu.ccr.flags.n = val < 0;
system->cpu.ccr.flags.v = 0;
}
void ccr_v(h8_system_t *system, signed a, signed b, signed c)
{
system->cpu.ccr.flags.v = ((a >= 0 && b >= 0 && c < 0) ||
(a < 0 && b < 0 && c >= 0));
}
/**
* =============================================================================
* Assembly function implementations
* =============================================================================
*/
#define H8_MOV_OP(name, type, hcbit) \
/** Moves a type from one location to another */ \
type mov_##name(h8_system_t *system, type dst, const type src) \
{ \
H8_UNUSED(dst); \
ccr_zn(system, src.i); \
return src; \
}
H8_MOV_OP(b, h8_byte_t, 4)
H8_MOV_OP(w, h8_word_t, 12)
H8_MOV_OP(l, h8_long_t, 28)
#define H8_ADD_OP(name, type, hcbit) \
type add_##name(h8_system_t *system, type dst, const type src) \
{ \
type result; \
result.i = dst.i + src.i; \
system->cpu.ccr.flags.c = result.u < src.u; \
ccr_zn(system, result.i); \
ccr_v(system, dst.i, src.i, result.i); \
system->cpu.ccr.flags.h = (result.u & (1 << hcbit)) != (dst.u & (1 << hcbit)); \
return result; \
}
H8_ADD_OP(b, h8_byte_t, 4)
H8_ADD_OP(w, h8_word_t, 12)
H8_ADD_OP(l, h8_long_t, 28)
/** Add immediate without modifying status bits */
void adds_l(h8_system_t *system, h8_long_t *dst, const unsigned src)
{
H8_UNUSED(system);
dst->u = dst->u + src;
}
#define H8_SUB_OP(name, type, hcbit) \
type sub_##name(h8_system_t *system, type dst, const type src) \
{ \
type result; \
result.i = dst.i - src.i; \
system->cpu.ccr.flags.c = src.u > dst.u; \
ccr_zn(system, result.i); \
ccr_v(system, dst.i, -src.i, result.i); \
system->cpu.ccr.flags.h = (result.u & (1 << hcbit)) != (dst.u & (1 << hcbit)); \
return result; \
}
H8_SUB_OP(b, h8_byte_t, 4)
H8_SUB_OP(w, h8_word_t, 12)
H8_SUB_OP(l, h8_long_t, 28)
/** Subtract immediate without modifying status bits */
void subs_l(h8_system_t *system, h8_long_t *dst, const unsigned src)
{
H8_UNUSED(system);
dst->i = dst->i - src;
}
static void daa_b(h8_system_t *system, h8_byte_t *dst)
{
h8_u8 adjust = 0;
h8_bool carry_out = 0;
if ((dst->u & 0x0F) > 9 || system->cpu.ccr.flags.h)
adjust |= 0x06;
if (((dst->u & 0xF0) >> 4) > 9 || system->cpu.ccr.flags.c)
{
adjust |= 0x60;
carry_out = 1;
}
dst->u += adjust;
system->cpu.ccr.flags.c = carry_out;
system->cpu.ccr.flags.h = ((adjust & 0x06) != 0);
ccr_zn(system, dst->i);
}
static void extu_w(h8_system_t *system, h8_word_t *dst)
{
dst->h.u = 0;
system->cpu.ccr.flags.n = 0;
system->cpu.ccr.flags.z = dst->u == 0;
system->cpu.ccr.flags.v = 0;
}
static void extu_l(h8_system_t *system, h8_long_t *dst)
{
dst->h.u = 0;
system->cpu.ccr.flags.n = 0;
system->cpu.ccr.flags.z = dst->u == 0;
system->cpu.ccr.flags.v = 0;
}
static void exts_w(h8_system_t *system, h8_word_t *dst)
{
dst->h.u = (dst->l.u & B10000000) ? 0xFF : 0x00;
system->cpu.ccr.flags.n = 0; /** @todo was this right? */
system->cpu.ccr.flags.z = dst->u == 0;
system->cpu.ccr.flags.v = 0;
}
static void exts_l(h8_system_t *system, h8_long_t *dst)
{
dst->h.u = (dst->c.u & B10000000) ? 0xFFFF : 0x0000;
system->cpu.ccr.flags.n = (dst->c.u & B10000000) ? 1 : 0;
system->cpu.ccr.flags.z = dst->u == 0;
system->cpu.ccr.flags.v = 0;
}
#define H8_ROTL_OP(name, type, carry) \
static void rotl_##name(h8_system_t *system, type *dst) \
{ \
h8_bool msb = (dst->u & carry) ? 1 : 0; \
dst->u <<= 1; \
dst->u |= msb; \
system->cpu.ccr.flags.c = msb; \
ccr_zn(system, dst->i); \
}
H8_ROTL_OP(b, h8_byte_t, 0x80)
H8_ROTL_OP(w, h8_word_t, 0x8000)
H8_ROTL_OP(l, h8_long_t, 0x80000000)
#define H8_ROTR_OP(name, type, carry) \
static void rotr_##name(h8_system_t *system, type *dst) \
{ \
unsigned lsb = (dst->u & 1) ? carry : 0; \
system->cpu.ccr.flags.c = (dst->u & 1); \
dst->u >>= 1; \
dst->u |= lsb; \
ccr_zn(system, dst->i); \
}
H8_ROTR_OP(b, h8_byte_t, 0x80)
H8_ROTR_OP(w, h8_word_t, 0x8000)
H8_ROTR_OP(l, h8_long_t, 0x80000000)
#define H8_ROTXL_OP(name, type, carry) \
static void rotxl_##name(h8_system_t *system, type *dst) \
{ \
h8_bool msb = (dst->u & carry) ? 1 : 0; \
dst->u <<= 1; \
dst->u |= system->cpu.ccr.flags.c; \
system->cpu.ccr.flags.c = msb; \
ccr_zn(system, dst->i); \
}
H8_ROTXL_OP(b, h8_byte_t, 0x80)
H8_ROTXL_OP(w, h8_word_t, 0x8000)
H8_ROTXL_OP(l, h8_long_t, 0x80000000)
#define H8_ROTXR_OP(name, type, carry) \
static void rotxr_##name(h8_system_t *system, type *dst) \
{ \
h8_bool lsb = (dst->u & 1); \
dst->u >>= 1; \
dst->u |= (system->cpu.ccr.flags.c ? carry : 0); \
system->cpu.ccr.flags.c = lsb; \
ccr_zn(system, dst->i); \
}
H8_ROTXR_OP(b, h8_byte_t, 0x80)
H8_ROTXR_OP(w, h8_word_t, 0x8000)
H8_ROTXR_OP(l, h8_long_t, 0x80000000)
#define H8_SHAL_OP(name, type, carry) \
static void shal_##name(h8_system_t *system, type *dst) \
{ \
system->cpu.ccr.flags.c = (dst->u & carry) ? 1 : 0; \
dst->u <<= 1; \
ccr_zn(system, dst->i); \
}
H8_SHAL_OP(b, h8_byte_t, 0x80)
H8_SHAL_OP(w, h8_word_t, 0x8000)
H8_SHAL_OP(l, h8_long_t, 0x80000000)
#define H8_SHAR_OP(name, type, carry) \
static void shar_##name(h8_system_t *system, type *dst) \
{ \
type sign; \
sign.u = dst->u & carry; \
system->cpu.ccr.flags.c = (dst->u & 1) ? 1 : 0; \
dst->u >>= 1; \
dst->u = sign.u | (dst->u & ~carry); \
ccr_zn(system, dst->i); \
}
H8_SHAR_OP(b, h8_byte_t, 0x80)
H8_SHAR_OP(w, h8_word_t, 0x8000)
H8_SHAR_OP(l, h8_long_t, 0x80000000)
#define H8_SHL_OP(name, type, carry, direction, op) \
static void shl##direction##_##name(h8_system_t *system, type *dst) \
{ \
system->cpu.ccr.flags.c = (dst->u & carry) ? 1 : 0; \
dst->u op 1; \
ccr_zn(system, dst->i); \
}
H8_SHL_OP(b, h8_byte_t, 0x80, l, <<=)
H8_SHL_OP(w, h8_word_t, 0x8000, l, <<=)
H8_SHL_OP(l, h8_long_t, 0x80000000, l, <<=)
H8_SHL_OP(b, h8_byte_t, 0x01, r, >>=)
H8_SHL_OP(w, h8_word_t, 0x0001, r, >>=)
H8_SHL_OP(l, h8_long_t, 0x00000001, r, >>=)
/**
* Register IO functions
*/
#define H8_IO_DUMMY_IN H8_UNUSED(system); H8_UNUSED(byte);
#define H8_IO_DUMMY_OUT H8_UNUSED(system); H8_UNUSED(byte); H8_UNUSED(value);
H8_IN(pdr1i)
{
unsigned i;
for (i = 0; i < 3; i++)
{
if (system->pdr1_in[i].device && system->pdr1_in[i].func)
{
byte->u &= ~(1 << i);
byte->u |= (system->pdr1_in[i].func(system->pdr1_in[i].device) & 0x01) << i;
}
}
}
H8_OUT(pdr1o)
{
unsigned i;
for (i = 0; i < 3; i++)
if (system->pdr1_out[i].device && system->pdr1_out[i].func)
system->pdr1_out[i].func(system->pdr1_out[i].device, (value.u >> i) & 1);
*byte = value;
}
H8_IN(pdr3i)
{
unsigned i;
for (i = 0; i < 3; i++)
{
if (system->pdr3_in[i].device && system->pdr3_in[i].func)
{
byte->u &= ~(1 << i);
byte->u |= (system->pdr3_in[i].func(system->pdr3_in[i].device) & 0x01) << i;
}
}
}
H8_OUT(pdr3o)
{
unsigned i;
for (i = 0; i < 3; i++)
if (system->pdr3_out[i].device && system->pdr3_out[i].func)
system->pdr3_out[i].func(system->pdr3_out[i].device, (value.u >> i) & 1);
*byte = value;
}
/**
* 8.3.1 Port Data Register 8 (PDR8)
*/
H8_IN(pdr8i)
{
unsigned i;
for (i = 0; i < 3; i++)
{
if (system->pdr8_in[i].device && system->pdr8_in[i].func)
{
byte->u &= ~(1 << (i + 2));
byte->u |= (system->pdr8_in[i].func(system->pdr8_in[i].device) & 0x01) << (i + 2);
}
}
}
H8_OUT(pdr8o)
{
unsigned i;
for (i = 0; i < 3; i++)
if (system->pdr8_out[i].device && system->pdr8_out[i].func)
system->pdr8_out[i].func(system->pdr8_out[i].device, (value.u >> (i + 2)) & 1);
*byte = value;
}
H8_IN(pdr9i)
{
unsigned i;
for (i = 0; i < 4; i++)
{
if (system->pdr9_in[i].device && system->pdr9_in[i].func)
{
byte->u &= ~(1 << i);
byte->u |= (system->pdr9_in[i].func(system->pdr9_in[i].device) & 0x01) << i;
}
}
}
H8_OUT(pdr9o)
{
unsigned i;
for (i = 0; i < 4; i++)
if (system->pdr9_out[i].device && system->pdr9_out[i].func)
system->pdr9_out[i].func(system->pdr9_out[i].device, (value.u >> i) & 1);
*byte = value;
}
/**
* 8.5.1 Port Data Register B (PDRB)
*/
H8_IN(pdrbi)
{
unsigned i;
for (i = 0; i < 6; i++)
{
if (system->pdrb_in[i].device && system->pdrb_in[i].func)
{
byte->u &= ~(1 << i);
byte->u |= (system->pdrb_in[i].func(system->pdrb_in[i].device) & 1) << i;
}
}
}
H8_OUT(pdrbo)
{
/* Read-only */
H8_IO_DUMMY_OUT
}
/**
* 15.3.5 SS Status Register (SSSR)
* F0E4
*/
H8_IN(sssri)
{
system->vmem.parts.io1.ssu.sssr.flags.tend = 1;
system->vmem.parts.io1.ssu.sssr.flags.tdre = 1;
system->vmem.parts.io1.ssu.sssr.flags.rdrf = 1;
*byte = system->vmem.parts.io1.ssu.sssr.raw;
}
H8_OUT(sssro)
{
h8_sssr_t *sssr = (h8_sssr_t*)byte;
const h8_sssr_t *sssr_val = (h8_sssr_t*)&value;
H8_UNUSED(system);
if (!sssr_val->flags.ce)
sssr->flags.ce = 0;
if (!sssr_val->flags.rdrf)
sssr->flags.rdrf = 0;
if (!sssr_val->flags.tdre)
sssr->flags.tdre = 0;
if (!sssr_val->flags.tend)
sssr->flags.tend = 0;
if (!sssr_val->flags.orer)
sssr->flags.orer = 0;
}
/**
* 15.3.6 SS Receive Data Register (SSRDR)
* F0E9
*/
H8_IN(ssrdri)
{
unsigned i;
for (i = 0; i < system->device_count; i++)
if (system->devices[i].ssu_in)
system->devices[i].ssu_in(&system->devices[i], byte);
}
H8_OUT(ssrdro)
{
/* Read-only */
H8_IO_DUMMY_OUT
}
/**
* 15.3.7 SS Transmit Data Register (SSTDR)
* F0EB
* @todo SSMR controls flip-on-store
*/
H8_OUT(sstdro)
{
unsigned i;
for (i = 0; i < system->device_count; i++)
if (system->devices[i].ssu_out)
system->devices[i].ssu_out(&system->devices[i], byte, value);
}
/**
* 15.3.8 SS Shift Register (SSTRSR)
* ????
*/
H8_IN(adsri)
{
/** @todo A/DC simply assumes result is done when status is read */
system->vmem.parts.io2.adc.adsr.flags.adsf = 0;
*byte = system->vmem.parts.io2.adc.adsr.raw;
}
H8_IN(adrrhi)
{
system->vmem.parts.io2.adc.adrr.raw.h.u = 0xFF;
*byte = system->vmem.parts.io2.adc.adrr.raw.h;
}
H8_IN(adrrli)
{
system->vmem.parts.io2.adc.adrr.raw.l.u = 0xC0;
*byte = system->vmem.parts.io2.adc.adrr.raw.l;
}
static H8_IN_T reg_ins[0x160] =
{
/* IO region 1 (0xF020) */
/* 0xF020 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF030 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF040 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF050 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF060 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF070 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF080 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF090 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF0A0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF0B0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF0C0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF0D0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF0E0 */
NULL, NULL, NULL, NULL, sssri, NULL, NULL, NULL,
NULL, ssrdri, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF0F0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* IO region 2 (0xFF80) */
/* 0xFF80 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xFF90 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xFFA0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xFFB0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, adrrhi, adrrli, NULL, adsri,
/* 0xFFC0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xFFD0 */
NULL, NULL, NULL, NULL, pdr1i, NULL, pdr3i, NULL,
NULL, NULL, NULL, pdr8i, pdr9i, NULL, pdrbi, NULL,
/* 0xFFE0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xFFF0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
};
static H8_OUT_T reg_outs[0x160] =
{
/* IO region 1 (0xF020) */
/* 0xF020 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF030 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF040 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF050 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF060 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF070 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF080 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF090 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF0A0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF0B0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF0C0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF0D0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xF0E0 */
NULL, NULL, NULL, NULL, sssro, NULL, NULL, NULL,
NULL, ssrdro, NULL, sstdro, NULL, NULL, NULL, NULL,
/* 0xF0F0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* IO region 2 (0xFF80) */
/* 0xFF80 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xFF90 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xFFA0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xFFB0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xFFC0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xFFD0 */
NULL, NULL, NULL, NULL, pdr1o, NULL, pdr3o, NULL,
NULL, NULL, NULL, pdr8o, pdr9o, NULL, pdrbo, NULL,
/* 0xFFE0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xFFF0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
};
unsigned h8_read(const h8_system_t *system, void *buffer,
const unsigned address, unsigned size)
{
if (address >= sizeof(system->vmem))
return 0;
else
{
unsigned max_size = sizeof(system->vmem) - address;
if (size > max_size)
size = max_size;
memcpy(buffer, &system->vmem.raw[address], size);
return size;
}
}
unsigned h8_write(h8_system_t *system, const void *buffer,
const unsigned address, const unsigned size,
const h8_bool force)
{
if ((address >= 0xf780 && address < 0xff80) || force)
{
memcpy(&system->vmem.raw[address], buffer, size);
return size;
}
else
return 0;
}
void *h8_find(h8_system_t *system, unsigned address)
{
#if H8_PROFILING
/*if (address >= 0xf780 && address < 0xff80)*/
system->reads[address & 0xFFFF] += 1;
#endif
return &system->vmem.raw[address & 0xFFFF];
}
static H8_IN_T h8_register_in(h8_system_t *system, unsigned address)
{
if (address >= H8_MEMORY_REGION_IO1 &&
address < H8_MEMORY_REGION_IO1 + sizeof(system->vmem.parts.io1))
{
#if H8_DEBUG_PRINT_REGISTERS
if (!reg_ins[address - H8_MEMORY_REGION_IO1])
printf("Better not implement %04X input, BOY\n", address);
#endif
return reg_ins[address - H8_MEMORY_REGION_IO1];
}
else if (address >= H8_MEMORY_REGION_IO2 &&
address < H8_MEMORY_REGION_IO2 + sizeof(system->vmem.parts.io2))
{
#if H8_DEBUG_PRINT_REGISTERS
if (!reg_ins[address - H8_MEMORY_REGION_IO2 + sizeof(system->vmem.parts.io1)])
printf("Better not implement %04X input, BOY\n", address);
#endif
return reg_ins[address - H8_MEMORY_REGION_IO2 + sizeof(system->vmem.parts.io1)];
}
else
return NULL;
}
static H8_OUT_T h8_register_out(h8_system_t *system, unsigned address)
{
if (address >= H8_MEMORY_REGION_IO1 &&
address < H8_MEMORY_REGION_IO1 + sizeof(system->vmem.parts.io1))
{
#if H8_DEBUG_PRINT_REGISTERS
if (!reg_outs[address - H8_MEMORY_REGION_IO1])
printf("Better not implement %04X output, BOY\n", address);
#endif
return reg_outs[address - H8_MEMORY_REGION_IO1];
}
else if (address >= H8_MEMORY_REGION_IO2 &&
address < H8_MEMORY_REGION_IO2 + sizeof(system->vmem.parts.io2))
{
#if H8_DEBUG_PRINT_REGISTERS
if (!reg_outs[address - H8_MEMORY_REGION_IO2 + sizeof(system->vmem.parts.io1)])
printf("Better not implement %04X output, BOY\n", address);
#endif
return reg_outs[address - H8_MEMORY_REGION_IO2 + sizeof(system->vmem.parts.io1)];
}
else
return NULL;
}
/**
* @brief h8_byte_in Reads a byte in after any necessary IO input function.
* @param system
* @param address
* @return The byte after IO
*/
static h8_byte_t h8_byte_in(h8_system_t *system, unsigned address)
{
H8_IN_T in = h8_register_in(system, address);
h8_byte_t *byte = h8_find(system, address);
system->vmem.raw[0xF7B5].u = (system->vmem.raw[0xF7B5].u | 1) & ~(1 << 4);
system->vmem.raw[0xF7C4].u = 1;
if (in)
in(system, byte);
return *byte;
}
static void h8_byte_out(h8_system_t *system, const unsigned address,
const h8_byte_t value)
{
H8_OUT_T out = h8_register_out(system, address);
h8_byte_t *byte = h8_find(system, address);
if (out)
out(system, byte, value);
else
*byte = value;
}
static h8_byte_t h8_read_b(h8_system_t *system, const unsigned address)
{
return h8_byte_in(system, address);
}
static void h8_write_b(h8_system_t *system, const unsigned address,
const h8_byte_t value)
{
h8_byte_out(system, address, value);
}
h8_byte_t h8_peek_b(h8_system_t *system, const unsigned address)
{
return *(h8_byte_t*)h8_find(system, address);
}
void h8_poke_b(h8_system_t *system, const unsigned address,
const h8_byte_t val)
{
*(h8_byte_t*)h8_find(system, address) = val;
}
/**
* Reads a word from explicit big-endian memory to native endianness.
*/
static h8_word_t h8_read_w(h8_system_t *system, unsigned address)
{
h8_word_t w;
w.h = h8_byte_in(system, address);
w.l = h8_byte_in(system, address + 1);
return w;
}
/**
* Writes a 16-bit word from native endianness to explicit big-endian memory.
*/
static void h8_write_w(h8_system_t *system, unsigned address, h8_word_t val)
{
h8_byte_out(system, address, val.h);
h8_byte_out(system, address + 1, val.l);
}
h8_word_t h8_peek_w(h8_system_t *system, const unsigned address)
{
h8_word_t w;
w.h = h8_peek_b(system, address);
w.l = h8_peek_b(system, address + 1);
return w;
}
void h8_poke_w(h8_system_t *system, const unsigned address,
const h8_word_t val)
{
h8_poke_b(system, address, val.h);
h8_poke_b(system, address + 1, val.l);
}
/**
* Reads a long in native endianness.
*/
static h8_long_t h8_read_l(h8_system_t *system, const unsigned address)
{
h8_long_t l;
l.a = h8_byte_in(system, address);
l.b = h8_byte_in(system, address + 1);
l.c = h8_byte_in(system, address + 2);
l.d = h8_byte_in(system, address + 3);
return l;
}
static void h8_write_l(h8_system_t *system, const unsigned address,
const h8_long_t val)
{
h8_byte_out(system, address, val.a);
h8_byte_out(system, address + 1, val.b);
h8_byte_out(system, address + 2, val.c);
h8_byte_out(system, address + 3, val.d);
}
h8_long_t h8_peek_l(h8_system_t *system, const unsigned address)
{
h8_long_t l;
l.a = h8_peek_b(system, address);
l.b = h8_peek_b(system, address + 1);
l.c = h8_peek_b(system, address + 2);
l.d = h8_peek_b(system, address + 3);
return l;
}
void h8_poke_l(h8_system_t *system, const unsigned address,
const h8_long_t val)
{
h8_poke_b(system, address, val.a);
h8_poke_b(system, address + 1, val.b);
h8_poke_b(system, address + 2, val.c);
h8_poke_b(system, address + 3, val.d);
}
/**
* Returns a pointer to a direct register accessed as a byte.
* reg immediate is a 3-bit register number and 1 bit to specify RL or RH.
*/
static h8_byte_t *rd_b(h8_system_t *system, unsigned reg)
{
if (reg & 0x8)
return &system->cpu.regs[reg & 0x7].byte.rl;
else
return &system->cpu.regs[reg & 0x7].byte.rh;
}
/**
* Returns a pointer to a direct register accessed as a word.
* reg immediate is a 3-bit register number and 1 bit to specify E or R.
*/
static h8_word_t *rd_w(h8_system_t *system, unsigned reg)
{
if (reg & 0x8)
return &system->cpu.regs[reg & 0x7].word.e;
else
return &system->cpu.regs[reg & 0x7].word.r;
}
/**
* Returns a pointer to a direct register accessed as a long.
*/
static h8_long_t *rd_l(h8_system_t *system, unsigned reg)
{
return &system->cpu.regs[reg & 0x7].er;
}
typedef unsigned h8_aptr;
#define H8_APTR_MASK 0x0000FFFF
/** General register, accessed as an address */
static h8_aptr er(h8_system_t *system, unsigned ers)
{
return rd_l(system, ers)->u & H8_APTR_MASK;
}
/** General register, accessed as an address with post-increment */
static h8_aptr erpi_b(h8_system_t *system, unsigned ers)
{
h8_aptr temp = rd_l(system, ers)->l.u;
rd_l(system, ers)->u += 1;
return temp;
}
/** General register, accessed as an address with post-increment */
static h8_aptr erpi_w(h8_system_t *system, unsigned ers)
{
h8_aptr temp = rd_l(system, ers)->l.u;
rd_l(system, ers)->u += 2;
return temp;
}
/** General register, accessed as an address with post-increment */
static h8_aptr erpi_l(h8_system_t *system, unsigned ers)
{
h8_aptr temp = rd_l(system, ers)->l.u;
rd_l(system, ers)->u += 4;
return temp;
}
/** General register, accessed as an address with pre-decrement */
static h8_aptr erpd_b(h8_system_t *system, unsigned ers)
{
rd_l(system, ers)->u -= 1;
return rd_l(system, ers)->u;
}
static h8_aptr erpd_w(h8_system_t *system, unsigned ers)
{
rd_l(system, ers)->u -= 2;
return rd_l(system, ers)->u;
}
static h8_aptr erpd_l(h8_system_t *system, unsigned ers)
{
rd_l(system, ers)->u -= 4;
return rd_l(system, ers)->u;
}
/** General register, accessed as an address with 16-bit displacement */
static h8_aptr erd16(h8_system_t *system, unsigned ers, signed d)
{
return (h8_aptr)((h8_s32)(rd_l(system, ers)->u) + d);
}
/** General register, accessed as an address with 24-bit displacement */
static h8_aptr erd24(h8_system_t *system, unsigned ers, signed d)
{
return (h8_aptr)((h8_s32)(rd_l(system, ers)->u) + d);
}
/** 8-bit absolute address */
static h8_aptr aa8(const h8_byte_t aa)
{
return 0xFF00 | aa.u;
}
/** 16-bit absolute address */
static h8_aptr aa16(const h8_word_t aa)
{
return aa.u;
}
/** 24-bit absolute address */
static h8_aptr aa24(const h8_long_t aa)
{
return aa.u & H8_APTR_MASK;
}
#define H8_RS_RD(name, type) \
static void rs_rd_##name(h8_system_t *system, type rs, type *rd, \
type(*action)(h8_system_t*, type, const type)) \
{ \
*rd = action(system, *rd, rs); \
}
H8_RS_RD(b, h8_byte_t)
H8_RS_RD(w, h8_word_t)
H8_RS_RD(l, h8_long_t)
#define H8_MS_RD(name, type) \
static void ms_rd_##name(h8_system_t *system, unsigned ms, type *rd, \
type(*action)(h8_system_t*, type, const type)) \
{ \
type ms_val = h8_read_##name(system, ms); \
*rd = action(system, *rd, ms_val); \
}
H8_MS_RD(b, h8_byte_t)
H8_MS_RD(w, h8_word_t)
H8_MS_RD(l, h8_long_t)
#define H8_RS_MD(name, type) \
static void rs_md_##name(h8_system_t *system, const type rs, unsigned md, \
type(*action)(h8_system_t*, type, const type)) \
{ \
type md_val = h8_peek_##name(system, md); \
h8_write_##name(system, md, action(system, md_val, rs)); \
}
H8_RS_MD(b, h8_byte_t)
H8_RS_MD(w, h8_word_t)
H8_RS_MD(l, h8_long_t)