-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmu_cpu.vhd
950 lines (836 loc) · 29.5 KB
/
vmu_cpu.vhd
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
library IEEE;
use IEEE.STD_LOGIC_1164.all;
package cpu is
type VmuCpuInstrOperands is record
immediate : natural;
direct : natural;
indAddress : natural;
indReg : std_logic_vector(1 downto 0);
bitPos : natural;
absolute : natural;
rel8S : integer;
rel16U : natural;
end record;
type VmuMem is array(natural range <>) of std_logic_vector(7 downto 0);
constant VmuSfrSize : integer := 128;
--type VmuSfrs is array(VmuSfrSize-1 downto 0) of std_logic_vector(7 downto 0);
subtype VmuSfrs is VmuMem(VmuSfrSize-1 downto 0);
constant VmuInstrMemSize : integer := 65536;
subtype VmuInstrMem is VmuMem(VmuInstrMemSize-1 downto 0);
constant VmuRomSize : integer := 65536;
subtype VmuRom is VmuMem(VmuRomSize-1 downto 0);
constant VmuRamBankSize : integer := 256;
constant VmuRamBankCount : integer := 3;
subtype VmuRamBank is VmuMem(VmuRamBankSize-1 downto 0);
type VmuRam is array(VmuRamBankCount-1 downto 0) of VmuRamBank;
constant VmuFlashBankSize : integer := 65536;
constant VmuFlashBankCount : integer := 2;
subtype VmuFlash is VmuMem(VmuFlashBankSize*VmuFlashBankCount-1 downto 0);
constant VmuXRamBankSize : integer := 128;
constant VmuXRamBankCount : integer := 3;
subtype VmuXRamBank is VmuMem(VmuXramBankSize-1 downto 0);
type VmuXRam is array(VmuXramBankCount-1 downto 0) of VmuXramBank;
constant VmuMemSegmentSize : integer := 128;
constant VmuMemSegmentGp1 : integer := 0;
constant VmuMemSegmentGp2 : integer := 1;
constant VmuMemSegmentSfr : integer := 2;
constant VmuMemSegmentXram : integer := 3;
constant VmuMemSegmentCount : integer := 4;
subtype VmuMemMap is VmuMem(VmuMemSegmentCount*VmuMemSegmentSize-1 downto 0);
end package;
library IEEE;
use IEEE.STD_LOGIC_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
use work.memBus.all;
use work.cpu.all;
use work.instrSet.all;
use work.VmuMemoryMappedRegister;
use work.memMap.all;
use work.sfr.all;
entity VmuCpu is
port (
clk : in std_logic;
reset : in std_logic;
memBusIn : inout VmuMemoryBusIn;
memBusOut : out VmuMemoryBusOut
);
end entity;
architecture behav of VmuCpu is
signal sfr : VmuSfrs;
signal rom : VmuRom;
signal ram : VmuRam;
signal flash : VmuFlash;
signal xRam : VmuXRam;
signal instrMem : VmuInstrMem;
signal memMap : VmuMemMap;
signal pc : natural;
signal nextPc : natural;
signal instr : std_logic_vector(23 downto 0);
signal opcode : natural;
signal operands : VmuCpuInstrOperands;
signal intReq : integer;
signal intMask : integer;
signal pswReg : std_logic_vector(7 downto 0);
signal extReg : std_logic_vector(7 downto 0);
function intToMemVec(num : integer) return std_logic_vector is
begin
return std_logic_vector(to_signed(num, 8));
end function;
function uintToMemVec(num : integer) return std_logic_vector is
begin
return std_logic_vector(to_unsigned(num, 8));
end function;
function memToSfr(addr : integer) return integer is
begin
return addr - ADDR_SFR_OFFSET;
end function;
begin
--Configure data memory map and address space.
memMap(VmuMemSegmentSize-1 downto 0)
<= ram(to_integer(unsigned(std_logic_vector'(""&sfr(memToSfr(ADDR_SFR_PSW))(SFR_PSW_RAMBK0_BIT)))))(VmuMemSegmentSize-1 downto 0);
memMap((VmuMemSegmentSfr*VmuMemSegmentSize)-1 downto VmuMemSegmentGp2*VmuMemSegmentSize)
<= ram(to_integer(unsigned(std_logic_vector'(""&sfr(memToSfr(ADDR_SFR_PSW))(SFR_PSW_RAMBK0_BIT)))))(VmuRamBankSize-1 downto VmuMemSegmentSize);
memMap((VmuMemSegmentXram*VmuMemSegmentSize)-1 downto VmuMemSegmentSfr*VmuMemSegmentSize)
<= sfr;
memMap((VmuMemSegmentXram+1)*VmuMemSegmentSize-1 downto VmuMemSegmentXram*VmuMemSegmentSize)
<= xRam(to_integer(unsigned(sfr(memToSfr(ADDR_SFR_XBNK))(2 downto 0))));
--Configure instruction memory map/source and address space (ROM when EXT is 0, Flash when 1).
instrMem(VmuInstrMemSize-1 downto 0) <= VmuInstrMem(flash(VmuInstrMemSize-1 downto 0)) when extReg(0) = '1' else
VmuInstrMem(rom(VmuInstrMemSize-1 downto 0));
--Fetch instruction (3 bytes is largest instruction size, extra data ignored for smaller instructions).
instr(23 downto 16) <= instrMem(pc);
instr(15 downto 8) <= instrMem(pc+1) when pc+1 < VmuInstrMemSize else
"00000000";
instr(7 downto 0) <= instrMem(pc+2) when pc+2 < VmuInstrMemSize else
"00000000";
--Extract opcode from the first byte of the instruction
opcode <= instrMap(to_integer(unsigned(instr(23 downto 16)))).opcode;
--Decode instruction and fetch operands.
process(instr) is
variable attr : VmuInstrAttr;
variable ops : VmuCpuInstrOperands;
variable shInstr : std_logic_vector(23 downto 0);
begin
attr := instrMap(opcode);
shInstr := instr;
if(attr.operands(0) = INSTR_ARG_TYPE_B3 and
attr.operands(1) = INSTR_ARG_TYPE_D9 and
attr.operands(2) = INSTR_ARG_TYPE_R8) then
ops.rel8S := to_integer(signed(shInstr(7 downto 0)));
ops.direct := to_integer(unsigned(shInstr(20) & instr(15 downto 8)));
ops.bitPos := to_integer(unsigned(shInstr(18 downto 16)));
elsif(attr.operands(0) = INSTR_ARG_TYPE_B3 and
attr.operands(1) = INSTR_ARG_TYPE_D9 and
attr.operands(2) = INSTR_ARG_TYPE_NONE) then
ops.direct := to_integer(unsigned(shInstr(12) & shInstr(7 downto 0)));
ops.bitPos := to_integer(unsigned(shInstr(10 downto 8)));
else
for i in 0 to 2 loop
case attr.operands(i) is
when INSTR_ARG_TYPE_R8 =>
ops.rel8S := to_integer(signed(shInstr(7 downto 0)));
shInstr := "00000000" & shInstr(23 downto 8);
when INSTR_ARG_TYPE_R16 =>
ops.rel16U := to_integer(unsigned(std_logic_vector'(shInstr(7 downto 0) & shInstr(23 downto 15))));
shInstr := "0000000000000000" & shInstr(23 downto 16);
when INSTR_ARG_TYPE_I8 =>
ops.immediate := to_integer(unsigned(shInstr(7 downto 0)));
shInstr := "00000000" & shInstr(23 downto 8);
when INSTR_ARG_TYPE_D9 =>
ops.direct := to_integer(unsigned(shInstr(8 downto 0)));
shInstr := "000000000" & shInstr(23 downto 9);
when INSTR_ARG_TYPE_N2 =>
ops.indReg := shInstr(1 downto 0);
shInstr := "00" & shInstr(23 downto 2);
when INSTR_ARG_TYPE_A12 =>
ops.absolute := to_integer(unsigned(shInstr(11 downto 0)));
shInstr := "000000000000" & shInstr(23 downto 12);
when INSTR_ARG_TYPE_A16 =>
ops.absolute := to_integer(unsigned(shInstr(15 downto 0)));
shInstr := "0000000000000000" & shInstr(23 downto 16);
when INSTR_ARG_TYPE_B3 =>
ops.bitPos := to_integer(unsigned(shInstr(2 downto 0)));
shInstr := "000" & shInstr(23 downto 3);
when others => null;
end case;
end loop;
end if;
operands <= ops;
end process;
--Fetch register-indirect operand.
operands.indAddress <= to_integer(unsigned(std_logic_vector'(operands.indReg(1) &
memMap(to_integer(unsigned(std_logic_vector'(operands.indReg &
sfr(memToSfr(ADDR_SFR_PSW))(SFR_PSW_IRBK1_BIT downto SFR_PSW_IRBK0_BIT))))))));
--Execute instruction with operands and opcode.
process(operands, opcode)
variable spInt : integer;
variable pendingPc : natural;
variable pcVector, vec16 : std_logic_vector(15 downto 0);
variable vec24 : std_logic_vector(23 downto 0);
variable vec1, vec2, vec3 : std_logic_vector(7 downto 0);
variable op1, op2, op3, op4, cin : integer;
begin
--Initialize intermediate local variables.
spInt := to_integer(unsigned(memMap(ADDR_SFR_SP)));
pendingPc := pc + instrMap(opcode).bytes;
pcVector := std_logic_vector(to_unsigned(pendingPc, 16));
if(memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) = '1') then
cin := 1;
else
cin := 0;
end if;
--Execute Instruction
case(opcode) is
when OPCODE_NOP => null;
when OPCODE_BR =>
pendingPc := pendingPc + operands.rel8S;
when OPCODE_LD =>
memMap(ADDR_SFR_ACC) <= memMap(operands.direct);
when OPCODE_LD_IND =>
memMap(ADDR_SFR_ACC) <= memMap(operands.indAddress);
when OPCODE_CALL =>
ram(0)(spInt+1) <= pcVector(7 downto 0);
ram(0)(spInt+2) <= pcVector(15 downto 8);
memMap(ADDR_SFR_SP) <= uintToMemVec(spInt+2);
pcVector(11 downto 0) := std_logic_vector(to_unsigned(operands.absolute, 12));
pendingPc := to_integer(unsigned(pcVector));
when OPCODE_CALLR =>
ram(0)(spInt+1) <= pcVector(7 downto 0);
ram(0)(spInt+2) <= pcVector(15 downto 8);
memMap(ADDR_SFR_SP) <= uintToMemVec(spInt+2);
pendingPc := pendingPc + (operands.rel16U mod 65536) - 1;
when OPCODE_BRF =>
pendingPc := pendingPc + (operands.rel16U mod 65536) - 1;
when OPCODE_ST =>
memMap(operands.direct) <= memMap(ADDR_SFR_ACC);
when OPCODE_ST_IND =>
memMap(operands.indAddress) <= memMap(ADDR_SFR_ACC);
when OPCODE_CALLF =>
ram(0)(spInt+1) <= pcVector(7 downto 0);
ram(0)(spInt+2) <= pcVector(15 downto 8);
memMap(ADDR_SFR_SP) <= uintToMemVec(spInt+2);
pendingPc := operands.absolute;
when OPCODE_JMPF =>
pendingPc := operands.absolute;
when OPCODE_MOV =>
memMap(operands.direct) <= uintToMemVec(operands.immediate);
when OPCODE_MOV_IND =>
memMap(operands.indAddress) <= uintToMemVec(operands.immediate);
when OPCODE_JMP =>
pcVector(11 downto 0) := std_logic_vector(to_unsigned(operands.absolute, 12));
pendingPc := to_integer(unsigned(pcVector));
when OPCODE_MUL =>
--Convert from vector, multiply, convert back,
vec24 := "00000000" & memMap(ADDR_SFR_ACC) & memMap(ADDR_SFR_C);
op1 := to_integer(unsigned(memMap(ADDR_SFR_B)));
op1 := op1 * to_integer(unsigned(vec24));
vec24 := std_logic_vector(to_unsigned(op1, 24));
--Write 24-bit result back as 3 separate bytes.
memMap(ADDR_SFR_C) <= vec24(7 downto 0);
memMap(ADDR_SFR_ACC) <= vec24(15 downto 8);
memMap(ADDR_SFR_B) <= vec24(23 downto 16);
--Update PSW
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
if(op1 > 65535) then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
when OPCODE_BEI =>
if(to_integer(unsigned(memMap(ADDR_SFR_ACC))) < operands.immediate) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
if(to_integer(unsigned(memMap(ADDR_SFR_ACC))) = operands.immediate) then
pendingPc := pendingPc + operands.rel8S;
end if;
end if;
when OPCODE_BE =>
if(memMap(ADDR_SFR_ACC) < memMap(operands.direct)) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
if(memMap(ADDR_SFR_ACC) = memMap(operands.direct)) then
pendingPc := pendingPc + operands.rel8S;
end if;
end if;
when OPCODE_BE_IND =>
if(memMap(ADDR_SFR_ACC) < memMap(operands.indAddress)) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
if(memMap(ADDR_SFR_ACC) = memMap(operands.indAddress)) then
pendingPc := pendingPc + operands.rel8S;
end if;
end if;
when OPCODE_DIV =>
--Fetch operands, convert to int, divide, convert back to vector.
op1 := to_integer(unsigned(memMap(ADDR_SFR_B)));
if(op1 /= 0) then --protect against divide-by-zero
vec16 := memMap(ADDR_SFR_ACC) & memMap(ADDR_SFR_C);
op2 := to_integer(unsigned(vec16));
op3 := op2 / op1;
op4 := op2 mod op1;
else
op3 := to_integer(unsigned(std_logic_vector'("11111111" & memMap(ADDR_SFR_C))));
op4 := 0;
end if;
vec16 := std_logic_vector(to_unsigned(op3, 16));
--Write result back as 16-bit quotient and 8-bit remainder.
memMap(ADDR_SFR_B) <= uintToMemVec(op4);
memMap(ADDR_SFR_ACC) <= vec16(15 downto 8);
memMap(ADDR_SFR_C) <= vec16(7 downto 0);
--Update PSW flags. Overflow active when remainder is present.
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
if(op4 /= 0) then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
when OPCODE_BNEI =>
if(to_integer(unsigned(memMap(ADDR_SFR_ACC))) < operands.immediate) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
if(to_integer(unsigned(memMap(ADDR_SFR_ACC))) /= operands.immediate) then
pendingPc := pc + operands.rel8S;
end if;
when OPCODE_BNE =>
if(to_integer(unsigned(memMap(ADDR_SFR_ACC))) < memMap(operands.direct)) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
if(to_integer(unsigned(memMap(ADDR_SFR_ACC))) /= memMap(operands.direct)) then
pendingPc := pendingPc + operands.rel8S;
end if;
when OPCODE_BNE_IND =>
if(to_integer(unsigned(memMap(ADDR_SFR_ACC))) < memMap(operands.indAddress)) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
if(to_integer(unsigned(memMap(ADDR_SFR_ACC))) /= memMap(operands.indAddress)) then
pendingPc := pendingPc + operands.rel8S;
end if;
when OPCODE_BPC =>
if(memMap(operands.direct)(operands.bitPos) = '1') then
memMap(operands.direct)(operands.bitPos) <= '0';
pendingPc := pendingPc + operands.rel8S;
end if;
when OPCODE_LDF => null; --not implementing flash read/programming yet!
when OPCODE_STF => null;
when OPCODE_DBNZ =>
op1 := to_integer(unsigned(memMap(operands.direct)))-1;
memMap(operands.direct) <= uintToMemVec(op1);
if(op1 /= 0) then
pendingPc := pendingPc + operands.rel8S;
end if;
when OPCODE_DBNZ_IND =>
op1 := to_integer(unsigned(memMap(operands.indAddress)))-1;
memMap(operands.direct) <= uintToMemVec(op1);
if(op1 /= 0) then
pendingPc := pendingPc + operands.rel8S;
end if;
when OPCODE_PUSH =>
spInt := spInt + 1;
ram(0)(spInt) <= memMap(operands.direct);
memMap(ADDR_SFR_SP) <= uintToMemVec(spInt);
when OPCODE_INC =>
op1 := to_integer(unsigned(memMap(operands.direct))) + 1;
memMap(operands.direct) <= uintToMemVec(op1);
when OPCODE_INC_IND =>
op1 := to_integer(unsigned(memMap(operands.indAddress))) + 1;
memMap(operands.indAddress) <= uintToMemVec(op1);
when OPCODE_BP =>
if(memMap(operands.direct)(operands.bitPos) = '1') then
pendingPc := pendingPc + operands.rel8S;
end if;
when OPCODE_POP =>
memMap(operands.direct) <= ram(0)(spInt);
memMap(ADDR_SFR_SP) <= uintToMemVec(spInt-1);
when OPCODE_DEC =>
op1 := to_integer(unsigned(memMap(operands.direct))) - 1;
memMap(operands.direct) <= uintToMemVec(op1);
when OPCODE_DEC_IND =>
op1 := to_integer(unsigned(memMap(operands.indAddress))) - 1;
memMap(operands.indAddress) <= uintToMemVec(op1);
when OPCODE_BZ =>
if(to_integer(unsigned(memMap(ADDR_SFR_ACC))) = 0) then
pendingPc := pendingPc + operands.rel8S;
end if;
when OPCODE_ADDI =>
op1 := to_integer(unsigned(memMap(ADDR_SFR_ACC)));
op2 := operands.immediate;
op3 := op1 + op2;
memMap(ADDR_SFR_ACC) <= uintToMemVec(op3);
--Carry
if(op3 > 255) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
--Overflow
vec1 := uintToMemVec(op1);
vec2 := uintToMemVec(op2);
vec3 := (not vec1 xor vec2) and (vec2 xor uintToMemVec(op3));
if(vec3(7) = '1') then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
--Aux carry
op1 := op1 mod 16;
op2 := op2 mod 16;
op3 := op1 + op2;
if(op3 > 15) then
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '0';
end if;
when OPCODE_ADD =>
op1 := to_integer(unsigned(memMap(ADDR_SFR_ACC)));
op2 := to_integer(unsigned(memMap(operands.direct)));
op3 := op1 + op2;
memMap(ADDR_SFR_ACC) <= uintToMemVec(op3);
--Carry
if(op3 > 255) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
--Overflow
vec1 := uintToMemVec(op1);
vec2 := uintToMemVec(op2);
vec3 := (not vec1 xor vec2) and (vec2 xor uintToMemVec(op3));
if(vec3(7) = '1') then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
--Aux carry
op1 := op1 mod 16;
op2 := op2 mod 16;
op3 := op1 + op2;
if(op3 > 15) then
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '0';
end if;
when OPCODE_ADD_IND =>
op1 := to_integer(unsigned(memMap(ADDR_SFR_ACC)));
op2 := to_integer(unsigned(memMap(operands.indAddress)));
op3 := op1 + op2;
memMap(ADDR_SFR_ACC) <= uintToMemVec(op3);
--Carry
if(op3 > 255) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
--Overflow
vec1 := uintToMemVec(op1);
vec2 := uintToMemVec(op2);
vec3 := (not vec1 xor vec2) and (vec2 xor uintToMemVec(op3));
if(vec3(7) = '1') then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
--Aux carry
op1 := op1 mod 16;
op2 := op2 mod 16;
op3 := op1 + op2;
if(op3 > 15) then
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '0';
end if;
when OPCODE_BN =>
if(memMap(operands.direct)(operands.bitPos) = '0') then
pendingPc := pendingPc + operands.rel8S;
end if;
when OPCODE_BNZ =>
if(to_integer(unsigned(memMap(ADDR_SFR_ACC))) /= 0) then
pendingPc := pendingPc + operands.rel8S;
end if;
when OPCODE_ADDCI =>
op1 := to_integer(unsigned(memMap(ADDR_SFR_ACC)));
op2 := operands.immediate + cin;
op3 := op1 + op2;
memMap(ADDR_SFR_ACC) <= uintToMemVec(op3);
--Carry
if(op3 > 255) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
--Overflow
vec1 := uintToMemVec(op1);
vec2 := uintToMemVec(op2);
vec3 := (not vec1 xor vec2) and (vec2 xor uintToMemVec(op3));
if(vec3(7) = '1') then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
--Aux carry
op1 := op1 mod 16;
op2 := op2 mod 16;
op3 := op1 + op2;
if(op3 > 15) then
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '0';
end if;
when OPCODE_ADDC =>
op1 := to_integer(unsigned(memMap(ADDR_SFR_ACC)));
op2 := to_integer(unsigned(memMap(operands.direct))) + cin;
op3 := op1 + op2;
memMap(ADDR_SFR_ACC) <= uintToMemVec(op3);
--Carry
if(op3 > 255) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
--Overflow
vec1 := uintToMemVec(op1);
vec2 := uintToMemVec(op2);
vec3 := (not vec1 xor vec2) and (vec2 xor uintToMemVec(op3));
if(vec3(7) = '1') then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
--Aux carry
op1 := op1 mod 16;
op2 := op2 mod 16;
op3 := op1 + op2;
if(op3 > 15) then
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '0';
end if;
when OPCODE_ADDC_IND =>
op1 := to_integer(unsigned(memMap(ADDR_SFR_ACC)));
op2 := to_integer(unsigned(memMap(operands.indAddress)))+1;
op3 := op1 + op2;
memMap(ADDR_SFR_ACC) <= uintToMemVec(op3);
--Carry
if(op3 > 255) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
--Overflow
vec1 := uintToMemVec(op1);
vec2 := uintToMemVec(op2);
vec3 := (not vec1 xor vec2) and (vec2 xor uintToMemVec(op3));
if(vec3(7) = '1') then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
--Aux carry
op1 := op1 mod 16;
op2 := op2 mod 16;
op3 := op1 + op2;
if(op3 > 15) then
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '0';
end if;
when OPCODE_RET =>
pcVector := ram(0)(spInt) & ram(0)(spInt-1);
memMap(ADDR_SFR_SP) <= uintToMemVec(spInt-2);
pendingPc := to_integer(unsigned(pcVector));
when OPCODE_SUBI =>
op1 := to_integer(unsigned(memMap(ADDR_SFR_ACC)));
op2 := operands.immediate;
op3 := op1 - op2;
memMap(ADDR_SFR_ACC) <= uintToMemVec(op3);
--Carry
if(op3 < 0) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
--Overflow
if(op2 < 1) then
if(255 + op2 >= op1) then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
else
if(op2 <= op1) then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
end if;
--Aux carry
op1 := op1 mod 16;
op2 := op2 mod 16;
op3 := op1 - op2;
if(op3 < 0) then
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '0';
end if;
when OPCODE_SUB =>
op1 := to_integer(unsigned(memMap(ADDR_SFR_ACC)));
op2 := to_integer(unsigned(memMap(operands.direct)));
op3 := op1 - op2;
memMap(ADDR_SFR_ACC) <= uintToMemVec(op3);
--Carry
if(op3 < 0) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
--Overflow
if(op2 < 1) then
if(255 + op2 >= op1) then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
else
if(op2 <= op1) then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
end if;
--Aux carry
op1 := op1 mod 16;
op2 := op2 mod 16;
op3 := op1 - op2;
if(op3 < 0) then
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '0';
end if;
when OPCODE_SUB_IND =>
op1 := to_integer(unsigned(memMap(ADDR_SFR_ACC)));
op2 := to_integer(unsigned(memMap(operands.indAddress)));
op3 := op1 - op2;
memMap(ADDR_SFR_ACC) <= uintToMemVec(op3);
--Carry
if(op3 < 0) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
--Overflow
if(op2 < 1) then
if(255 + op2 >= op1) then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
else
if(op2 <= op1) then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
end if;
--Aux carry
op1 := op1 mod 16;
op2 := op2 mod 16;
op3 := op1 - op2;
if(op3 < 0) then
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '0';
end if;
when OPCODE_NOT1 =>
memMap(operands.direct)(operands.bitPos) <= not memMap(operands.direct)(operands.bitPos);
when OPCODE_RETI =>
pcVector := ram(0)(spInt) & ram(0)(spInt-1);
memMap(ADDR_SFR_SP) <= uintToMemVec(spInt-2);
pendingPc := to_integer(unsigned(pcVector));
intMask <= intMask - 1;
when OPCODE_SUBCI =>
op1 := to_integer(unsigned(memMap(ADDR_SFR_ACC)));
op2 := operands.immediate;
op3 := op1 - op2 - cin;
memMap(ADDR_SFR_ACC) <= uintToMemVec(op3);
--Carry
if(op3 < 0) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
--Overflow
if(op2 < 1) then
if(255 + op2 >= op1) then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
else
if(op2 <= op1) then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
end if;
--Aux carry
op1 := op1 mod 16;
op2 := op2 mod 16;
op3 := op1 - op2;
if(op3 < 0) then
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '0';
end if;
when OPCODE_SUBC =>
op1 := to_integer(unsigned(memMap(ADDR_SFR_ACC)));
op2 := to_integer(unsigned(memMap(operands.direct)));
op3 := op1 - op2 - cin;
memMap(ADDR_SFR_ACC) <= uintToMemVec(op3);
--Carry
if(op3 < 0) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
--Overflow
if(op2 < 1) then
if(255 + op2 >= op1) then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
else
if(op2 <= op1) then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
end if;
--Aux carry
op1 := op1 mod 16;
op2 := op2 mod 16;
op3 := op1 - op2;
if(op3 < 0) then
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '0';
end if;
when OPCODE_SUBC_IND =>
op1 := to_integer(unsigned(memMap(ADDR_SFR_ACC)));
op2 := to_integer(unsigned(memMap(operands.indAddress)));
op3 := op1 - op2 - cin;
memMap(ADDR_SFR_ACC) <= uintToMemVec(op3);
--Carry
if(op3 < 0) then
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= '0';
end if;
--Overflow
if(op2 < 1) then
if(255 + op2 >= op1) then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
else
if(op2 <= op1) then
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_OV_BIT) <= '0';
end if;
end if;
--Aux carry
op1 := op1 mod 16;
op2 := op2 mod 16;
op3 := op1 - op2;
if(op3 < 0) then
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '1';
else
memMap(ADDR_SFR_PSW)(SFR_PSW_AC_BIT) <= '0';
end if;
when OPCODE_ROR =>
memMap(ADDR_SFR_ACC)(7 downto 0) <= memMap(ADDR_SFR_ACC)(0) & memMap(ADDR_SFR_ACC)(7 downto 1);
when OPCODE_LDC =>
op1 := to_integer(unsigned(memMap(ADDR_SFR_ACC)));
vec16 := memMap(ADDR_SFR_TRH) & memMap(ADDR_SFR_TRL);
op2 := to_integer(unsigned(vec16));
op3 := op1 + op2;
memMap(ADDR_SFR_ACC) <= instrMem(op3);
when OPCODE_XCH =>
memMap(ADDR_SFR_ACC) <= memMap(operands.direct);
memMap(operands.direct) <= memMap(ADDR_SFR_ACC);
when OPCODE_XCH_IND =>
memMap(ADDR_SFR_ACC) <= memMap(operands.direct);
memMap(operands.indAddress) <= memMap(ADDR_SFR_ACC);
when OPCODE_CLR1 =>
memMap(operands.direct)(operands.bitPos) <= '0';
when OPCODE_RORC =>
memMap(ADDR_SFR_ACC) <= memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) & memMap(ADDR_SFR_ACC)(7 downto 1);
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= memMap(ADDR_SFR_ACC)(0);
when OPCODE_ORI =>
memMap(ADDR_SFR_ACC) <= memMap(ADDR_SFR_ACC) or uintToMemVec(operands.immediate);
when OPCODE_OR =>
memMap(ADDR_SFR_ACC) <= memMap(ADDR_SFR_ACC) or memMap(operands.direct);
when OPCODE_OR_IND =>
memMap(ADDR_SFR_ACC) <= memMap(ADDR_SFR_ACC) or memMap(operands.indAddress);
when OPCODE_ROL =>
memMap(ADDR_SFR_ACC) <= memMap(ADDR_SFR_ACC)(6 downto 0) & memMap(ADDR_SFR_ACC)(7);
when OPCODE_ANDI =>
memMap(ADDR_SFR_ACC) <= memMap(ADDR_SFR_ACC) and uintToMemVec(operands.immediate);
when OPCODE_AND =>
memMap(ADDR_SFR_ACC) <= memMap(ADDR_SFR_ACC) and memMap(operands.direct);
when OPCODE_AND_IND =>
memMap(ADDR_SFR_ACC) <= memMap(ADDR_SFR_ACC) and memMap(operands.indAddress);
when OPCODE_SET1 =>
memMap(operands.direct)(operands.bitPos) <= '1';
when OPCODE_ROLC =>
memMap(ADDR_SFR_ACC) <= memMap(ADDR_SFR_ACC)(6 downto 0) & memMap(ADDR_SFR_ACC)(7);
memMap(ADDR_SFR_PSW)(SFR_PSW_CY_BIT) <= memMap(ADDR_SFR_ACC)(7);
when OPCODE_XORI =>
memMap(ADDR_SFR_ACC) <= memMap(ADDR_SFR_ACC) xor uintToMemVec(operands.immediate);
when OPCODE_XOR =>
memMap(ADDR_SFR_ACC) <= memMap(ADDR_SFR_ACC) xor memMap(operands.direct);
when OPCODE_XOR_IND =>
memMap(ADDR_SFR_ACC) <= memMap(ADDR_SFR_ACC) xor memMap(operands.indAddress);
when others => null;
end case;
--Commit new PC value for next clock cycle.
nextPc <= pendingPc;
--Handle firmware calls
end process;
--Trigger next processor cycle by setting the new PC value.
--Handle reset/CPU and register initialization.
process(clk, reset) is
begin
if(clk = '1' and clk'event) then
--Initialize CPU and registers to starting state
if(reset = '1') then
--Reset PC
pc <= 0;
--Initialize SFRs
--Fetch instructions from Flash
sfr(memToSfr(ADDR_SFR_EXT))(1) <= '1';
--Use RAM bank #1.
sfr(memToSfr(ADDR_SFR_PSW))(SFR_PSW_RAMBK0_BIT) <= '1';
--Disable low battery signal
sfr(memToSfr(ADDR_SFR_P7))(1) <= '1';
--Initialize Stack Pointer
sfr(memToSfr(ADDR_SFR_SP)) <= uintToMemVec(ADDR_STACK_BEGIN-1);
--Clear out all controller input pins
sfr(memToSfr(ADDR_SFR_P3)) <= "11111111";
else
pc <= nextPc;
end if;
end if;
end process;
end architecture;