-
Notifications
You must be signed in to change notification settings - Fork 18
/
cpu.c
2003 lines (1717 loc) · 47.9 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
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
/*
* TamaLIB - A hardware agnostic Tamagotchi P1 emulation library
*
* Copyright (C) 2021 Jean-Christophe Rona <[email protected]>
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <avr/pgmspace.h>
#include "cpu.h"
#include "hw.h"
#include "hal.h"
//#include "rom_new.h"
#include "rom_12bit.h"
#define CPU_SPEED_RATIO 0
#define TICK_FREQUENCY 32768 // Hz
#define TIMER_1HZ_PERIOD 32768 // in ticks
#define TIMER_256HZ_PERIOD 128 // in ticks
#define MASK_4B 0xF00
#define MASK_6B 0xFC0
#define MASK_7B 0xFE0
#define MASK_8B 0xFF0
#define MASK_10B 0xFFC
#define MASK_12B 0xFFF
#define PCS (pc & 0xFF)
#define PCSL (pc & 0xF)
#define PCSH ((pc >> 4) & 0xF)
#define PCP ((pc >> 8) & 0xF)
#define PCB ((pc >> 12) & 0x1)
#define TO_PC(bank, page, step) ((step & 0xFF) | ((page & 0xF) << 8) | (bank & 0x1) << 12)
#define NBP ((np >> 4) & 0x1)
#define NPP (np & 0xF)
#define TO_NP(bank, page) ((page & 0xF) | (bank & 0x1) << 4)
#define XHL (x & 0xFF)
#define XL1 (x & 0xF)
#define XH1 ((x >> 4) & 0xF)
#define XP ((x >> 8) & 0xF)
#define YHL (y & 0xFF)
#define YL1 (y & 0xF)
#define YH1 ((y >> 4) & 0xF)
#define YP ((y >> 8) & 0xF)
#define M(n) get_memory(n)
#define SET_M(n, v) set_memory(n, v)
#define RQ(i) get_rq(i)
#define SET_RQ(i, v) set_rq(i, v)
#define SPL1 (sp & 0xF)
#define SPH1 ((sp >> 4) & 0xF)
#define FLAG_C (0x1 << 0)
#define FLAG_Z (0x1 << 1)
#define FLAG_D (0x1 << 2)
#define FLAG_I (0x1 << 3)
#define C !!(flags & FLAG_C)
#define Z !!(flags & FLAG_Z)
#define D !!(flags & FLAG_D)
#define I !!(flags & FLAG_I)
#define SET_C() {flags |= FLAG_C;}
#define CLEAR_C() {flags &= ~FLAG_C;}
#define SET_Z() {flags |= FLAG_Z;}
#define CLEAR_Z() {flags &= ~FLAG_Z;}
#define SET_D() {flags |= FLAG_D;}
#define CLEAR_D() {flags &= ~FLAG_D;}
#define SET_I() {flags |= FLAG_I;}
#define CLEAR_I() {flags &= ~FLAG_I;}
#define REG_CLK_INT_FACTOR_FLAGS 0xF00
#define REG_SW_INT_FACTOR_FLAGS 0xF01
#define REG_PROG_INT_FACTOR_FLAGS 0xF02
#define REG_SERIAL_INT_FACTOR_FLAGS 0xF03
#define REG_K00_K03_INT_FACTOR_FLAGS 0xF04
#define REG_K10_K13_INT_FACTOR_FLAGS 0xF05
#define REG_CLOCK_INT_MASKS 0xF10
#define REG_SW_INT_MASKS 0xF11
#define REG_PROG_INT_MASKS 0xF12
#define REG_SERIAL_INT_MASKS 0xF13
#define REG_K00_K03_INT_MASKS 0xF14
#define REG_K10_K13_INT_MASKS 0xF15
#define REG_PROG_TIMER_DATA_L 0xF24
#define REG_PROG_TIMER_DATA_H 0xF25
#define REG_PROG_TIMER_RELOAD_DATA_L 0xF26
#define REG_PROG_TIMER_RELOAD_DATA_H 0xF27
#define REG_K00_K03_INPUT_PORT 0xF40
#define REG_K10_K13_INPUT_PORT 0xF42
#define REG_K40_K43_BZ_OUTPUT_PORT 0xF54
#define REG_CPU_OSC3_CTRL 0xF70
#define REG_LCD_CTRL 0xF71
#define REG_LCD_CONTRAST 0xF72
#define REG_SVD_CTRL 0xF73
#define REG_BUZZER_CTRL1 0xF74
#define REG_BUZZER_CTRL2 0xF75
#define REG_CLK_WD_TIMER_CTRL 0xF76
#define REG_SW_TIMER_CTRL 0xF77
#define REG_PROG_TIMER_CTRL 0xF78
#define REG_PROG_TIMER_CLK_SEL 0xF79
#define INPUT_PORT_NUM 2
typedef struct {
//char *log;
u12_t code;
u12_t mask;
// u12_t shift_arg1;
// u12_t mask_arg1; // != 0 only if there are two arguments
u8_t cycles;
// void (*cb0)(u8_t arg0, u8_t arg1);
} op_t0;
typedef struct {
//char *log;
// u12_t code;
// u12_t mask;
// u12_t shift_arg1;
// u12_t mask_arg1; // != 0 only if there are two arguments
// u8_t cycles;
void (*cb1)(u8_t arg0, u8_t arg1);
} op_t1;
typedef struct {
u4_t states;
} input_port_t;
/* Registers */
static u13_t pc, next_pc;
static u12_t x, y;
static u4_t a, b;
static u5_t np;
static u8_t sp;
/* Flags */
static u4_t flags;
//static const u12_t *g_program = NULL;
static u4_t memory[MEMORY_SIZE];
//static u4_t io_memory[MEM_IO_SIZE];
static input_port_t inputs[INPUT_PORT_NUM] = {{0}};
//static u8_t maxNumber = 0;
/* Interrupts (in priority order) */
static interrupt_t interrupts[INT_SLOT_NUM] = {
{0x0, 0x0, 0, 0x0C}, // Prog timer
{0x0, 0x0, 0, 0x0A}, // Serial interface
{0x0, 0x0, 0, 0x08}, // Input (K10-K13)
{0x0, 0x0, 0, 0x06}, // Input (K00-K03)
{0x0, 0x0, 0, 0x04}, // Stopwatch timer
{0x0, 0x0, 0, 0x02}, // Clock timer
};
//static breakpoint_t *g_breakpoints = NULL;
static u32_t call_depth = 0;
static u32_t clk_timer_timestamp = 0; // in ticks
static u32_t prog_timer_timestamp = 0; // in ticks
static bool_t prog_timer_enabled = 0;
static u8_t prog_timer_data = 0;
static u8_t prog_timer_rld = 0;
static u32_t tick_counter = 0;
static u32_t ts_freq;
//static u8_t speed_ratio = 0;
static timestamp_t ref_ts;
/*
static state_t cpu_state = {
.pc = &pc,
.x = &x,
.y = &y,
.a = &a,
.b = &b,
.np = &np,
.sp = &sp,
.flags = &flags,
.tick_counter = &tick_counter,
.clk_timer_timestamp = &clk_timer_timestamp,
.prog_timer_timestamp = &prog_timer_timestamp,
.prog_timer_enabled = &prog_timer_enabled,
.prog_timer_data = &prog_timer_data,
.prog_timer_rld = &prog_timer_rld,
.call_depth = &call_depth,
.interrupts = interrupts,
.memory = memory,
}; */
void cpu_add_bp(breakpoint_t **list, u13_t addr)
{
/* breakpoint_t *bp;
bp = (breakpoint_t *) g_hal->malloc(sizeof(breakpoint_t));
if (!bp) {
g_hal->log(LOG_ERROR, "Cannot allocate memory for breakpoint 0x%04X!\n", addr);
return;
}
bp->addr = addr;
if (*list != NULL) {
bp->next = *list;
} else {
bp->next = NULL;
}
*list = bp; */
}
void cpu_free_bp(breakpoint_t **list)
{
/* breakpoint_t *bp = *list, *tmp;
while (bp != NULL) {
tmp = bp->next;
g_hal->free(bp);
bp = tmp;
}
*list = NULL; */
}
/*
void cpu_set_speed(u8_t speed)
{
speed_ratio = speed;
}*/
void cpu_get_state(cpu_state_t *cpustate)
{
cpustate->pc = pc;
cpustate->x = x;
cpustate->y = y;
cpustate->a = a;
cpustate->b = b;
cpustate->np = np;
cpustate->sp = sp;
cpustate->flags = flags;
cpustate->tick_counter = tick_counter;
cpustate->clk_timer_timestamp = clk_timer_timestamp;
cpustate->prog_timer_timestamp = prog_timer_timestamp;
cpustate->prog_timer_enabled = prog_timer_enabled;
cpustate->prog_timer_data = prog_timer_data;
cpustate->prog_timer_rld = prog_timer_rld;
cpustate->call_depth = call_depth;
cpustate->memory = (u4_t *)memory;
uint8_t i;
for(i=0;i<6;i++) {
cpustate->interrupts[i].factor_flag_reg = interrupts[i].factor_flag_reg;
cpustate->interrupts[i].mask_reg = interrupts[i].mask_reg;
cpustate->interrupts[i].triggered = interrupts[i].triggered;
cpustate->interrupts[i].vector = interrupts[i].vector;
}
}
void cpu_set_state(cpu_state_t *cpustate)
{
pc = cpustate->pc;
x = cpustate->x;
y = cpustate->y;
a = cpustate->a;
b = cpustate->b;
np = cpustate->np;
sp = cpustate->sp;
flags = cpustate->flags;
tick_counter = cpustate->tick_counter;
clk_timer_timestamp = cpustate->clk_timer_timestamp;
prog_timer_timestamp = cpustate->prog_timer_timestamp;
prog_timer_enabled = cpustate->prog_timer_enabled;
prog_timer_data = cpustate->prog_timer_data;
prog_timer_rld = cpustate->prog_timer_rld;
call_depth = cpustate->call_depth;
//memory = (u4_t *)cpustate->memory;
uint8_t i;
for(i=0;i<6;i++) {
interrupts[i].factor_flag_reg = cpustate->interrupts[i].factor_flag_reg;
interrupts[i].mask_reg = cpustate->interrupts[i].mask_reg;
interrupts[i].triggered = cpustate->interrupts[i].triggered;
interrupts[i].vector = cpustate->interrupts[i].vector;
}
}
u32_t cpu_get_depth(void)
{
return call_depth;
}
static void generate_interrupt(int_slot_t slot, u8_t bit)
{
/* Set the factor flag no matter what */
interrupts[slot].factor_flag_reg = interrupts[slot].factor_flag_reg | (0x1 << bit);
/* Trigger the INT only if not masked */
if (interrupts[slot].mask_reg & (0x1 << bit)) {
interrupts[slot].triggered = 1;
}
}
void cpu_set_input_pin(pin_t pin, pin_state_t state)
{
/* Set the I/O */
inputs[pin & 0x4].states = (inputs[pin & 0x4].states & ~(0x1 << (pin & 0x3))) | (state << (pin & 0x3));
/* Trigger the interrupt (TODO: handle relation register) */
if (state == PIN_STATE_LOW) {
switch ((pin & 0x4) >> 2) {
case 0:
generate_interrupt(INT_K00_K03_SLOT, pin & 0x3);
break;
case 1:
generate_interrupt(INT_K10_K13_SLOT, pin & 0x3);
break;
}
}
}
void cpu_sync_ref_timestamp(void)
{
ref_ts = g_hal->get_timestamp();
}
static u4_t get_io(u12_t n)
{
u4_t tmp;
switch (n) {
case REG_CLK_INT_FACTOR_FLAGS:
/* Interrupt factor flags (clock timer) */
tmp = interrupts[INT_CLOCK_TIMER_SLOT].factor_flag_reg;
interrupts[INT_CLOCK_TIMER_SLOT].factor_flag_reg = 0;
return tmp;
case REG_SW_INT_FACTOR_FLAGS:
/* Interrupt factor flags (stopwatch) */
tmp = interrupts[INT_STOPWATCH_SLOT].factor_flag_reg;
interrupts[INT_STOPWATCH_SLOT].factor_flag_reg = 0;
return tmp;
case REG_PROG_INT_FACTOR_FLAGS:
/* Interrupt factor flags (prog timer) */
tmp = interrupts[INT_PROG_TIMER_SLOT].factor_flag_reg;
interrupts[INT_PROG_TIMER_SLOT].factor_flag_reg = 0;
return tmp;
case REG_SERIAL_INT_FACTOR_FLAGS:
/* Interrupt factor flags (serial) */
tmp = interrupts[INT_SERIAL_SLOT].factor_flag_reg;
interrupts[INT_SERIAL_SLOT].factor_flag_reg = 0;
return tmp;
case REG_K00_K03_INT_FACTOR_FLAGS:
/* Interrupt factor flags (K00-K03) */
tmp = interrupts[INT_K00_K03_SLOT].factor_flag_reg;
interrupts[INT_K00_K03_SLOT].factor_flag_reg = 0;
return tmp;
case REG_K10_K13_INT_FACTOR_FLAGS:
/* Interrupt factor flags (K10-K13) */
tmp = interrupts[INT_K10_K13_SLOT].factor_flag_reg;
interrupts[INT_K10_K13_SLOT].factor_flag_reg = 0;
return tmp;
case REG_CLOCK_INT_MASKS:
/* Clock timer interrupt masks */
return interrupts[INT_CLOCK_TIMER_SLOT].mask_reg;
case REG_SW_INT_MASKS:
/* Stopwatch interrupt masks */
return interrupts[INT_STOPWATCH_SLOT].mask_reg & 0x3;
case REG_PROG_INT_MASKS:
/* Prog timer interrupt masks */
return interrupts[INT_PROG_TIMER_SLOT].mask_reg & 0x1;
case REG_SERIAL_INT_MASKS:
/* Serial interface interrupt masks */
return interrupts[INT_SERIAL_SLOT].mask_reg & 0x1;
case REG_K00_K03_INT_MASKS:
/* Input (K00-K03) interrupt masks */
return interrupts[INT_K00_K03_SLOT].mask_reg;
case REG_K10_K13_INT_MASKS:
/* Input (K10-K13) interrupt masks */
return interrupts[INT_K10_K13_SLOT].mask_reg;
case REG_PROG_TIMER_DATA_L:
/* Prog timer data (low) */
return prog_timer_data & 0xF;
case REG_PROG_TIMER_DATA_H:
/* Prog timer data (high) */
return (prog_timer_data >> 4) & 0xF;
case REG_PROG_TIMER_RELOAD_DATA_L:
/* Prog timer reload data (low) */
return prog_timer_rld & 0xF;
case REG_PROG_TIMER_RELOAD_DATA_H:
/* Prog timer reload data (high) */
return (prog_timer_rld >> 4) & 0xF;
case REG_K00_K03_INPUT_PORT:
/* Input port (K00-K03) */
return inputs[0].states;
case REG_K10_K13_INPUT_PORT:
/* Input port (K10-K13) */
return inputs[1].states;
case REG_K40_K43_BZ_OUTPUT_PORT:
/* Output port (R40-R43) */
//return io_memory[n - MEM_IO_ADDR_OFS];
return 0xf;
case REG_CPU_OSC3_CTRL:
/* CPU/OSC3 clocks switch, CPU voltage switch */
//return io_memory[n - MEM_IO_ADDR_OFS];
return 0;
case REG_LCD_CTRL:
/* LCD control */
//return io_memory[n - MEM_IO_ADDR_OFS];
return 0x8;
case REG_LCD_CONTRAST:
/* LCD contrast */
break;
case REG_SVD_CTRL:
/* SVD */
//return io_memory[n - MEM_IO_ADDR_OFS] & 0x7; // Voltage always OK
return 0;
case REG_BUZZER_CTRL1:
/* Buzzer config 1 */
//return memory[n - MEM_IO_ADDR_OFS];
return 0;
case REG_BUZZER_CTRL2:
/* Buzzer config 2 */
//return io_memory[n - MEM_IO_ADDR_OFS] & 0x3; // Buzzer ready
return 0;
case REG_CLK_WD_TIMER_CTRL:
/* Clock/Watchdog timer reset */
break;
case REG_SW_TIMER_CTRL:
/* Stopwatch stop/run/reset */
break;
case REG_PROG_TIMER_CTRL:
/* Prog timer stop/run/reset */
return !!prog_timer_enabled;
case REG_PROG_TIMER_CLK_SEL:
/* Prog timer clock selection */
break;
default:
break;
//g_hal->log(LOG_ERROR, "Read from unimplemented I/O 0x%03X - PC = 0x%04X\n", n, pc);
}
return 0;
}
static void set_io(u12_t n, u4_t v)
{
switch (n) {
case REG_CLOCK_INT_MASKS:
/* Clock timer interrupt masks */
/* Assume 1Hz timer INT enabled (0x8) */
interrupts[INT_CLOCK_TIMER_SLOT].mask_reg = v;
break;
case REG_SW_INT_MASKS:
/* Stopwatch interrupt masks */
/* Assume all INT disabled */
interrupts[INT_STOPWATCH_SLOT].mask_reg = v;
break;
case REG_PROG_INT_MASKS:
/* Prog timer interrupt masks */
/* Assume Prog timer INT enabled (0x1) */
interrupts[INT_PROG_TIMER_SLOT].mask_reg = v;
break;
case REG_SERIAL_INT_MASKS:
/* Serial interface interrupt masks */
/* Assume all INT disabled */
interrupts[INT_K10_K13_SLOT].mask_reg = v;
break;
case REG_K00_K03_INT_MASKS:
/* Input (K00-K03) interrupt masks */
/* Assume all INT disabled */
interrupts[INT_SERIAL_SLOT].mask_reg = v;
break;
case REG_K10_K13_INT_MASKS:
/* Input (K10-K13) interrupt masks */
/* Assume all INT disabled */
interrupts[INT_K10_K13_SLOT].mask_reg = v;
break;
case REG_PROG_TIMER_RELOAD_DATA_L:
/* Prog timer reload data (low) */
prog_timer_rld = v | (prog_timer_rld & 0xF0);
break;
case REG_PROG_TIMER_RELOAD_DATA_H:
/* Prog timer reload data (high) */
prog_timer_rld = (prog_timer_rld & 0xF) | (v << 4);
break;
case REG_K00_K03_INPUT_PORT:
/* Input port (K00-K03) */
/* Write not allowed */
break;
case REG_K40_K43_BZ_OUTPUT_PORT:
/* Output port (R40-R43) */
//g_hal->log(LOG_INFO, "Output/Buzzer: 0x%X\n", v);
hw_enable_buzzer(!(v & 0x8));
break;
case REG_CPU_OSC3_CTRL:
/* CPU/OSC3 clocks switch, CPU voltage switch */
/* Assume 32,768 OSC1 selected, OSC3 off, battery >= 3,1V (0x1) */
break;
case REG_LCD_CTRL:
/* LCD control */
break;
case REG_LCD_CONTRAST:
/* LCD contrast */
/* Assume medium contrast (0x8) */
break;
case REG_SVD_CTRL:
/* SVD */
/* Assume battery voltage always OK (0x6) */
break;
case REG_BUZZER_CTRL1:
/* Buzzer config 1 */
hw_set_buzzer_freq(v & 0x7);
break;
case REG_BUZZER_CTRL2:
/* Buzzer config 2 */
break;
case REG_CLK_WD_TIMER_CTRL:
/* Clock/Watchdog timer reset */
/* Ignore watchdog */
break;
case REG_SW_TIMER_CTRL:
/* Stopwatch stop/run/reset */
break;
case REG_PROG_TIMER_CTRL:
/* Prog timer stop/run/reset */
if (v & 0x2) {
prog_timer_data = prog_timer_rld;
}
if ((v & 0x1) && !prog_timer_enabled) {
prog_timer_timestamp = tick_counter;
}
prog_timer_enabled = v & 0x1;
break;
case REG_PROG_TIMER_CLK_SEL:
/* Prog timer clock selection */
/* Assume 256Hz, output disabled */
break;
default:
break;
//g_hal->log(LOG_ERROR, "Write 0x%X to unimplemented I/O 0x%03X - PC = 0x%04X\n", v, n, pc);
}
}
static void set_lcd(u12_t n, u4_t v)
{
u8_t i;
u8_t seg, com0;
seg = ((n & 0x7F) >> 1);
com0 = (((n & 0x80) >> 7) * 8 + (n & 0x1) * 4);
for (i = 0; i < 4; i++) {
hw_set_lcd_pin(seg, com0 + i, (v >> i) & 0x1);
}
}
/*
#define MEMORY_SIZE 1280 // 4096 x 4 bits (640 x 4 bits of RAM)
#define MEM_RAM_ADDR 0x000
#define MEM_RAM_SIZE 0x280 // 640
#define MEM_DISPLAY1_ADDR 0xE00 // 3584 0x280
#define MEM_DISPLAY1_ADDR_OFS 0xB80
#define MEM_DISPLAY1_SIZE 0x050 // 80
#define MEM_DISPLAY2_ADDR 0xE80 // 3712 0x2D0
#define MEM_DISPLAY2_ADDR_OFS 0xBB0
#define MEM_DISPLAY2_SIZE 0x050 // 80
#define MEM_IO_ADDR 0xF00 // 3840 0x320
#define MEM_IO_ADDR_OFS 0xBE0
#define MEM_IO_SIZE 0x080 // 128 0x3a0
*/
/*
u8_t cpu_get_max_number() {
return maxNumber;
}
*/
static u4_t get_memory(u12_t n)
{
u4_t res = 0;
if (n < MEM_RAM_SIZE) {
/* RAM */
//g_hal->log(LOG_MEMORY, "RAM - ");
//if (n > max_memory_addr_access) max_memory_addr_access = n;
if ((n & 0x1)==0) {
res = memory[n>>1] >> 4;
} else {
res = memory[n>>1] & 0b00001111;
}
} else if (n >= MEM_DISPLAY1_ADDR && n < (MEM_DISPLAY1_ADDR + MEM_DISPLAY1_SIZE)) {
/* Display Memory 1 */
//g_hal->log(LOG_MEMORY, "Display Memory 1 - ");
//res = memory[n - MEM_DISPLAY1_ADDR_OFS];
res = 0;
} else if (n >= MEM_DISPLAY2_ADDR && n < (MEM_DISPLAY2_ADDR + MEM_DISPLAY2_SIZE)) {
/* Display Memory 2 */
//g_hal->log(LOG_MEMORY, "Display Memory 2 - ");
//res = memory[n - MEM_DISPLAY2_ADDR_OFS];
res = 0;
} else if (n >= MEM_IO_ADDR && n < (MEM_IO_ADDR + MEM_IO_SIZE)) {
/* I/O Memory */
//g_hal->log(LOG_MEMORY, "I/O - ");
res = get_io(n);
} else {
//g_hal->log(LOG_ERROR, "Read from invalid memory address 0x%03X - PC = 0x%04X\n", n, pc);
return 0;
}
//g_hal->log(LOG_MEMORY, "Read 0x%X - Address 0x%03X - PC = 0x%04X\n", res, n, pc);
return res;
}
static void set_memory(u12_t n, u4_t v)
{
if (n < MEM_RAM_SIZE) {
/* RAM */
//g_hal->log(LOG_MEMORY, "RAM - ");
if ((n & 0x1)==0) {
memory[n>>1] = (memory[n>>1] & 0x0F) | (v << 4);
} else {
memory[n>>1] = (memory[n>>1] & 0xF0) | v;
}
//memory[n] = v;
} else if (n >= MEM_DISPLAY1_ADDR && n < (MEM_DISPLAY1_ADDR + MEM_DISPLAY1_SIZE)) {
/* Display Memory 1 */
set_lcd(n, v);
//memory[n - MEM_DISPLAY1_ADDR_OFS] = v;
//g_hal->log(LOG_MEMORY, "Display Memory 1 - ");
} else if (n >= MEM_DISPLAY2_ADDR && n < (MEM_DISPLAY2_ADDR + MEM_DISPLAY2_SIZE)) {
/* Display Memory 2 */
set_lcd(n, v);
//memory[n - MEM_DISPLAY2_ADDR_OFS] = v;
//g_hal->log(LOG_MEMORY, "Display Memory 2 - ");
} else if (n >= MEM_IO_ADDR && n < (MEM_IO_ADDR + MEM_IO_SIZE)) {
/* I/O Memory */
set_io(n, v);
//g_hal->log(LOG_MEMORY, "I/O - ");
} else {
//g_hal->log(LOG_ERROR, "Write 0x%X to invalid memory address 0x%03X - PC = 0x%04X\n", v, n, pc);
return;
}
//g_hal->log(LOG_MEMORY, "Write 0x%X - Address 0x%03X - PC = 0x%04X\n", v, n, pc);
}
/*
void cpu_refresh_hw(void)
{
static const struct range {
u12_t addr;
u12_t size;
} refresh_locs[] = {
{ MEM_DISPLAY1_ADDR, MEM_DISPLAY1_SIZE }, // Display Memory 1
{ MEM_DISPLAY2_ADDR, MEM_DISPLAY2_SIZE }, // Display Memory 2
{ REG_BUZZER_CTRL1, 1 }, // Buzzer frequency
{ REG_K40_K43_BZ_OUTPUT_PORT, 1 }, // Buzzer enabled
{ 0, 0 }, // end of list
};
for (int i = 0; refresh_locs[i].size != 0; i++) {
for (u12_t n = refresh_locs[i].addr; n < (refresh_locs[i].addr + refresh_locs[i].size); n++) {
set_memory(n, memory[n]);
}
}
}*/
static u4_t get_rq(u12_t rq)
{
switch (rq & 0x3) {
case 0x0: return a;
case 0x1: return b;
case 0x2: return M(x);
case 0x3: return M(y);
}
return 0;
}
static void set_rq(u12_t rq, u4_t v)
{
switch (rq & 0x3) {
case 0x0: a = v; break;
case 0x1: b = v; break;
case 0x2: SET_M(x, v); break;
case 0x3: SET_M(y, v); break;
}
}
/* Instructions */
static void op_pset_cb(u8_t arg0, u8_t arg1)
{
np = arg0;
}
static void op_jp_cb(u8_t arg0, u8_t arg1)
{
next_pc = arg0 | (np << 8);
}
static void op_jp_c_cb(u8_t arg0, u8_t arg1)
{
if (flags & FLAG_C) {
next_pc = arg0 | (np << 8);
}
}
static void op_jp_nc_cb(u8_t arg0, u8_t arg1)
{
if (!(flags & FLAG_C)) {
next_pc = arg0 | (np << 8);
}
}
static void op_jp_z_cb(u8_t arg0, u8_t arg1)
{
if (flags & FLAG_Z) {
next_pc = arg0 | (np << 8);
}
}
static void op_jp_nz_cb(u8_t arg0, u8_t arg1)
{
if (!(flags & FLAG_Z)) {
next_pc = arg0 | (np << 8);
}
}
static void op_jpba_cb(u8_t arg0, u8_t arg1)
{
next_pc = a | (b << 4) | (np << 8);
}
static void op_call_cb(u8_t arg0, u8_t arg1)
{
pc = (pc + 1) & 0x1FFF; // This does not actually change the PC register
SET_M(sp - 1, PCP);
SET_M(sp - 2, PCSH);
SET_M(sp - 3, PCSL);
sp = (sp - 3) & 0xFF;
next_pc = TO_PC(PCB, NPP, arg0);
call_depth++;
}
static void op_calz_cb(u8_t arg0, u8_t arg1)
{
pc = (pc + 1) & 0x1FFF; // This does not actually change the PC register
SET_M(sp - 1, PCP);
SET_M(sp - 2, PCSH);
SET_M(sp - 3, PCSL);
sp = (sp - 3) & 0xFF;
next_pc = TO_PC(PCB, 0, arg0);
call_depth++;
}
static void op_ret_cb(u8_t arg0, u8_t arg1)
{
next_pc = M(sp) | (M(sp + 1) << 4) | (M(sp + 2) << 8) | (PCB << 12);
sp = (sp + 3) & 0xFF;
call_depth--;
}
static void op_rets_cb(u8_t arg0, u8_t arg1)
{
next_pc = M(sp) | (M(sp + 1) << 4) | (M(sp + 2) << 8) | (PCB << 12);
sp = (sp + 3) & 0xFF;
next_pc = (pc + 1) & 0x1FFF;
call_depth--;
}
static void op_retd_cb(u8_t arg0, u8_t arg1)
{
next_pc = M(sp) | (M(sp + 1) << 4) | (M(sp + 2) << 8) | (PCB << 12);
sp = (sp + 3) & 0xFF;
SET_M(x, arg0 & 0xF);
SET_M(x + 1, (arg0 >> 4) & 0xF);
x = (x + 2) & 0xFFF;
call_depth--;
}
static void op_nop5_cb(u8_t arg0, u8_t arg1)
{
}
static void op_nop7_cb(u8_t arg0, u8_t arg1)
{
}
static void op_halt_cb(u8_t arg0, u8_t arg1)
{
g_hal->halt();
}
static void op_inc_x_cb(u8_t arg0, u8_t arg1)
{
x = (x + 1) & 0xFFF;
}
static void op_inc_y_cb(u8_t arg0, u8_t arg1)
{
y = (y + 1) & 0xFFF;
}
static void op_ld_x_cb(u8_t arg0, u8_t arg1)
{
x = arg0 | (XP << 8);
}
static void op_ld_y_cb(u8_t arg0, u8_t arg1)
{
y = arg0 | (YP << 8);
}
static void op_ld_xp_r_cb(u8_t arg0, u8_t arg1)
{
x = XHL | (RQ(arg0) << 8);
}
static void op_ld_xh_r_cb(u8_t arg0, u8_t arg1)
{
x = XL1 | (RQ(arg0) << 4) | (XP << 8);
}
static void op_ld_xl_r_cb(u8_t arg0, u8_t arg1)
{
x = RQ(arg0) | (XH1 << 4) | (XP << 8);
}
static void op_ld_yp_r_cb(u8_t arg0, u8_t arg1)
{
y = YHL | (RQ(arg0) << 8);
}
static void op_ld_yh_r_cb(u8_t arg0, u8_t arg1)
{
y = YL1 | (RQ(arg0) << 4) | (YP << 8);
}
static void op_ld_yl_r_cb(u8_t arg0, u8_t arg1)
{
y = RQ(arg0) | (YH1 << 4) | (YP << 8);
}
static void op_ld_r_xp_cb(u8_t arg0, u8_t arg1)
{
SET_RQ(arg0, XP);
}
static void op_ld_r_xh_cb(u8_t arg0, u8_t arg1)
{
SET_RQ(arg0, XH1);
}
static void op_ld_r_xl_cb(u8_t arg0, u8_t arg1)
{
SET_RQ(arg0, XL1);
}
static void op_ld_r_yp_cb(u8_t arg0, u8_t arg1)
{
SET_RQ(arg0, YP);
}
static void op_ld_r_yh_cb(u8_t arg0, u8_t arg1)
{
SET_RQ(arg0, YH1);
}
static void op_ld_r_yl_cb(u8_t arg0, u8_t arg1)
{
SET_RQ(arg0, YL1);
}
static void op_adc_xh_cb(u8_t arg0, u8_t arg1)
{
u8_t tmp;
tmp = XH1 + arg0 + C;
x = XL1 | ((tmp & 0xF) << 4)| (XP << 8);
if (tmp >> 4) { SET_C(); } else { CLEAR_C(); }
if (!(tmp & 0xF)) { SET_Z(); } else { CLEAR_Z(); }
}
static void op_adc_xl_cb(u8_t arg0, u8_t arg1)
{
u8_t tmp;
tmp = XL1 + arg0 + C;
x = (tmp & 0xF) | (XH1 << 4) | (XP << 8);
if (tmp >> 4) { SET_C(); } else { CLEAR_C(); }
if (!(tmp & 0xF)) { SET_Z(); } else { CLEAR_Z(); }
}
static void op_adc_yh_cb(u8_t arg0, u8_t arg1)
{
u8_t tmp;
tmp = YH1 + arg0 + C;
y = YL1 | ((tmp & 0xF) << 4)| (YP << 8);
if (tmp >> 4) { SET_C(); } else { CLEAR_C(); }
if (!(tmp & 0xF)) { SET_Z(); } else { CLEAR_Z(); }
}
static void op_adc_yl_cb(u8_t arg0, u8_t arg1)
{
u8_t tmp;
tmp = YL1 + arg0 + C;
y = (tmp & 0xF) | (YH1 << 4) | (YP << 8);
if (tmp >> 4) { SET_C(); } else { CLEAR_C(); }
if (!(tmp & 0xF)) { SET_Z(); } else { CLEAR_Z(); }
}
static void op_cp_xh_cb(u8_t arg0, u8_t arg1)
{
if (XH1 < arg0) { SET_C(); } else { CLEAR_C(); }
if (XH1 == arg0) { SET_Z(); } else { CLEAR_Z(); }
}
static void op_cp_xl_cb(u8_t arg0, u8_t arg1)
{
if (XL1 < arg0) { SET_C(); } else { CLEAR_C(); }
if (XL1 == arg0) { SET_Z(); } else { CLEAR_Z(); }
}
static void op_cp_yh_cb(u8_t arg0, u8_t arg1)
{
if (YH1 < arg0) { SET_C(); } else { CLEAR_C(); }
if (YH1 == arg0) { SET_Z(); } else { CLEAR_Z(); }
}
static void op_cp_yl_cb(u8_t arg0, u8_t arg1)
{
if (YL1 < arg0) { SET_C(); } else { CLEAR_C(); }
if (YL1 == arg0) { SET_Z(); } else { CLEAR_Z(); }
}
static void op_ld_r_i_cb(u8_t arg0, u8_t arg1)
{