-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrocode
1306 lines (1111 loc) · 26.6 KB
/
microcode
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
# Microcode for the FISC2 CPU. (c) 2020 Warren Toomey, GPL3
#
# First up, we have the definitions of the control line combinations.
# A leading @ on a line means that this control line is active low.
# ALU operations (4 bits, 3:0).
# These ones are set as levels at clock-start
# and go straight to the ALU
A+B = 0000 # uses carry-in
A-B = 0001 # uses carry-in
A&B = 0002
A|B = 0003
A^B = 0004
A<<B = 0005
A>>B = 0006
A*B = 0007 # low 8-bit result
A/B = 0008
A%B = 0009
A+0 = 000A # uses carry-in
A-Bcomp = 000B # no carry-in, sets flags, outputs B's value
Zero = 000C
A+1 = 000D
A-1 = 000E
~A = 000F
# Address bus writers (2 bits, 5:4)
# These ones are set as levels at clock-start
PCwrite = 0000
ARwrite = 0010
SPwrite = 0020
DRwrite = 0030
# Stack operation (2 bits, 7:6)
# These ones are set as levels at clock-start
Stkincr = 0000
Stkdecr = 0040
Stkhold = 0080
PCincr = 00C0
# Data bus writers (4 bits, 11:8)
# These ones are set as levels at clock-start
MEMwrite = 0000
ADhiwrite = 0100
ADlowrite = 0200
UARTwrite = 0300
Awrite = 0400
Olowrite = 0500
Ohiwrite = 0600
ARincr = 0700
uSreset = 0800
DbWr9 = 0900 # unused
DbWrA = 0A00 # unused
DbWrB = 0B00 # unused
DbWrC = 0C00 # unused
DbWrD = 0D00 # unused
DbWrE = 0E00 # unused
DbWrF = 0F00 # unused
# Data bus readers (4 bits, 15:12)
# These ones are set as levels at clock-start
Noread = 0000
DRincr = 1000
SPhiread = 2000
SPloread = 3000
ARhiread = 4000
ARloread = 5000
DRhiread = 6000
DRloread = 7000
# These one act as clock edges mid-cycle
Bankread = 8000
Aread = 9000
Bread = A000
IRread = B000
MEMread = C000
Oread = D000
UARTread = E000
Jmpena = F000
# The following lists control lines which needs to be asserted always, unless
# one or more of the other control lines are named. We also use this to
# prevent two or more of the list from being asserted at the same time.
assert A+B unless A-B A&B A|B A^B A<<B A>>B A*B A/B A%B A+0 A-Bcomp \
Zero A+1 A-1 ~A
assert PCwrite unless ARwrite SPwrite DRwrite
assert Stkhold unless Stkincr Stkdecr PCincr
assert MEMwrite unless ADhiwrite ADlowrite UARTwrite Awrite Olowrite \
Ohiwrite ARincr uSreset DbWr9 DbWrA DbWrB DbWrC DbWrD DbWrE DbWrF
assert Noread unless DRincr SPhiread SPloread ARhiread ARloread DRhiread \
DRloread Bankread Aread Bread IRread MEMread Oread UARTread Jmpena
# This line, if given, is placed at position zero for each microinstruction.
# The purpose is to load the IR with the instruction and increment the PC.
#
START := MEMwrite IRread PCincr
# Definitions of useful macros
# Perform an ALU operation on two bytes in memory
define ALUB_WW(OP):
MEMwrite DRloread PCincr # Get the destination address
MEMwrite DRhiread PCincr
MEMwrite ARloread PCincr # Get the source address
MEMwrite ARhiread PCincr
ARwrite MEMwrite Bread # Load the source byte
DRwrite MEMwrite OP Oread # Perform the operation
DRwrite Olowrite MEMread # and write it to the destination
uSreset
# Perform an ALU operation on two words in memory
define ALUW_WW(OP):
MEMwrite DRloread PCincr # Load the DR
MEMwrite DRhiread PCincr
MEMwrite ARloread PCincr # Load the AR
MEMwrite ARhiread PCincr
ARwrite MEMwrite Bread # B= Mem[ AR ]
DRwrite MEMwrite OP Oread # O= Mem[ DR ] + B
DRwrite Olowrite MEMread # Mem[ DR ]= O
ARincr DRincr # Increment AR and DR
ARwrite MEMwrite Bread # B= Mem[ AR ]
DRwrite MEMwrite OP Oread # O= Mem[ DR ] + B
DRwrite Olowrite MEMread # Mem[ DR ]= O
uSreset
# Perform an ALU operation on two words that DR/AR already point to
define ALUW_DRAR(OP):
ARwrite MEMwrite Bread # B= Mem[ AR ]
DRwrite MEMwrite OP Oread # O= Mem[ DR ] + B
DRwrite Olowrite MEMread # Mem[ DR ]= O
ARincr DRincr # Increment AR and DR
ARwrite MEMwrite Bread # B= Mem[ AR ]
DRwrite MEMwrite OP Oread # O= Mem[ DR ] + B
DRwrite Olowrite MEMread # Mem[ DR ]= O
uSreset
# Load DR and AR from the instruction
define LOAD_DRAR():
MEMwrite DRloread PCincr
MEMwrite DRhiread PCincr
MEMwrite ARloread PCincr
MEMwrite ARhiread PCincr
# Load AR from the instruction
define LOAD_AR():
MEMwrite ARloread PCincr
MEMwrite ARhiread PCincr
# Load DR from the instruction
define LOAD_DR():
MEMwrite DRloread PCincr
MEMwrite DRhiread PCincr
# Clear carry
define CLC():
Zero Oread
# Conditional jump, comparing bytes at the AR and DR addresses.
# We don't have to encode the comparison as the opcode number
# does this.
define CJMPB_WW():
MEMwrite DRloread PCincr
MEMwrite DRhiread PCincr
DRwrite MEMwrite Aread # Get destination value
MEMwrite ARloread PCincr
MEMwrite ARhiread PCincr
ARwrite MEMwrite Bread # Get source value
MEMwrite ARloread PCincr
MEMwrite ARhiread PCincr # Get jump address
Zero Oread
Awrite A-Bcomp Oread
Ohiwrite ARwrite Jmpena
uSreset
# Conditional jump on existing flags
define CJMP_FLAGS():
MEMwrite ARloread PCincr
MEMwrite ARhiread PCincr
Ohiwrite ARwrite Jmpena
uSreset
# Conditional jump, comparing X and Y.
define CJMP_XY():
MEMwrite ARloread PCincr
MEMwrite ARhiread PCincr
Zero Oread
Awrite A-Bcomp Oread
Ohiwrite ARwrite Jmpena
uSreset
# ALU Operations on X and Y
define ALUB_XY(OP):
Awrite OP Oread
Olowrite Aread
uSreset
# ALU Operations on a memory location and Y
define ALUB_WY(OP):
MEMwrite ARloread PCincr
MEMwrite ARhiread PCincr
ARwrite MEMwrite OP Oread
ARwrite Olowrite MEMread
uSreset
# ALU operations on a byte destination at SP+offset and Y
define ALUB_SY(OP):
A-Bcomp Oread # Temporarily store B
Olowrite ARloread # in ARlo
Zero Oread # Add SP+offset into DR
MEMwrite Bread PCincr
SPwrite ADlowrite A+B Oread
Olowrite DRloread
MEMwrite Bread PCincr
SPwrite ADhiwrite A+B Oread
Olowrite DRhiread
ARwrite ADlowrite Bread # Get B back
DRwrite MEMwrite OP Oread # Do the ALU operation
Olowrite DRwrite MEMread # And save it
uSreset
# 00-0F instructions: special purpose
# No operation
00 nop:
uSreset
## 01-0F: Load/store and general data movement instructions
# Load memory location with byte constant $XX.
01 ldb_wb:
LOAD_AR() # Get the address into AR
CLC() # Ensure carry is clear
MEMwrite A+0 Oread PCincr # Load const into Oreg
ARwrite Olowrite MEMread # Write to memory location
uSreset
# Load memory location with word constant $XXXX.
02 ldw_ww:
LOAD_AR() # Get the address into AR
CLC() # Ensure carry is clear
MEMwrite A+0 Oread PCincr # Load const into Oreg
ARwrite Olowrite MEMread # Write to memory location
ARincr DRincr # Increment AR and DR
MEMwrite A+0 Oread PCincr # Load const hi bits into Oreg
ARwrite Olowrite MEMread # Write to memory location
uSreset
# Move 8-bit byte from source to destination
03 movb_ww:
LOAD_DRAR()
ARwrite MEMwrite Aread # A= Mem[ AR ]
DRwrite Awrite MEMread
uSreset
# Move 16-bit word from source to destination
04 movw_ww:
LOAD_DRAR()
ARwrite MEMwrite Aread # A= Mem[ AR ]
DRwrite Awrite MEMread # Mem[ DR ] = A
ARincr DRincr # Increment AR and DR
ARwrite MEMwrite Aread # A= Mem[ AR+1 ]
DRwrite Awrite MEMread # Mem[ DR+1 ] = A
uSreset
# Output byte from memory to UART
05 out_w:
LOAD_AR() # Get the address into AR
ARwrite MEMwrite UARTread
uSreset
# Output byte constant to UART
06 out_b:
MEMwrite UARTread PCincr
uSreset
# Clear byte in memory
07 clrb_w:
LOAD_AR() # Get the address into AR
CLC()
ARwrite Olowrite MEMread
uSreset
# Clear word in memory
08 clrw_w:
LOAD_AR() # Get the address into AR
CLC()
ARwrite Olowrite MEMread
CLC()
ARincr DRincr
ARwrite Olowrite MEMread
uSreset
# Compare byte at dest with byte at source
09 cmpb_ww:
LOAD_DRAR()
ARwrite MEMwrite Bread # B= Mem[ AR ]
CLC()
DRwrite MEMwrite A-Bcomp Oread
uSreset
# Compare 16-bit word value at dest with word at source
0A cmpw_ww:
LOAD_DRAR()
CLC()
ARwrite MEMwrite Bread
DRwrite MEMwrite A-B Oread
ARincr DRincr
ARwrite MEMwrite Bread
DRwrite MEMwrite A-Bcomp Oread
uSreset
# Clear the carry bit
0E clc:
CLC()
uSreset
# Load the memory bank register with constant
0F bank_b:
MEMwrite Bankread PCincr
uSreset
# 10-1F: 8-bit ALU operations, two memory operands
# Add with no carry
10 addb_ww:
CLC()
ALUB_WW(A+B)
# Subtract with no carry
11 subb_ww:
CLC()
ALUB_WW(A-B)
# Add with carry
12 adcb_ww:
ALUB_WW(A+B)
# Subtract with carry
13 sbcb_ww:
ALUB_WW(A-B)
# AND
14 andb_ww:
ALUB_WW(A&B)
# OR
15 orb_ww:
ALUB_WW(A|B)
# XOR
16 xorb_ww:
ALUB_WW(A^B)
# Logical shift left
17 lslb_ww:
ALUB_WW(A<<B)
# Arithmetic shift right
18 asrb_ww:
ALUB_WW(A>>B)
# Multiply
19 mulb_ww:
ALUB_WW(A*B)
# Divide
1A divb_ww:
ALUB_WW(A/B)
# Modulo
1B modb_ww:
ALUB_WW(A%B)
# Increment
1C incb_w:
LOAD_DR()
DRwrite MEMwrite A+1 Oread
DRwrite Olowrite MEMread
uSreset
# Decrement
1D decb_w:
LOAD_DR()
DRwrite MEMwrite A-1 Oread
DRwrite Olowrite MEMread
uSreset
# Read from UART to memory
1E in_w:
LOAD_AR() # Get the address into AR
ARwrite UARTwrite MEMread
uSreset
# 20-2F: 16-bit ALU operations, two memory operands
# Add with no carry
20 addw_ww:
CLC()
ALUW_WW(A+B)
# Subtract with no carry
21 subw_ww:
CLC()
ALUW_WW(A-B)
# Add with carry
22 adcw_ww:
ALUW_WW(A+B)
# Subtract with carry
23 sbcw_ww:
ALUW_WW(A-B)
# AND
24 andw_ww:
ALUW_WW(A&B)
# OR
25 orw_ww:
ALUW_WW(A|B)
# XOR
26 xorw_ww:
ALUW_WW(A^B)
# Logical shift left: NOTYET
27 lslw_ww:
MEMwrite DRloread PCincr
MEMwrite DRhiread PCincr
MEMwrite ARloread PCincr
MEMwrite ARhiread PCincr
ARwrite MEMwrite Bread
DRwrite MEMwrite A<<B Oread
DRwrite Olowrite MEMread
uSreset
# Arithmetic shift right: NOTYET
28 asrw_ww:
MEMwrite DRloread PCincr
MEMwrite DRhiread PCincr
MEMwrite ARloread PCincr
MEMwrite ARhiread PCincr
ARwrite MEMwrite Bread
DRwrite MEMwrite A>>B Oread
DRwrite Olowrite MEMread
uSreset
# Increment:
2C incw_w:
LOAD_DR()
DRwrite MEMwrite A+1 Oread
DRwrite Olowrite MEMread
ARincr DRincr
DRwrite MEMwrite A+0 Oread
DRwrite Olowrite MEMread
uSreset
# Decrement:
2D decw_w:
LOAD_DR()
DRwrite MEMwrite A-1 Oread
DRwrite Olowrite MEMread
CLC()
ARincr DRincr
Olowrite Bread
DRwrite MEMwrite A-B Oread
DRwrite Olowrite MEMread
CLC() uSreset
# 30-37: Jumps on 8-bit comparisons
# Jump if dest equals source
30 jeqb_www:
CJMPB_WW()
# Jump if dest less than source
31 jltb_www:
CJMPB_WW()
# Jump if dest not equal to source
32 jneb_www:
CJMPB_WW()
# Jump if dest less than or equal to source
33 jleb_www:
CJMPB_WW()
# Jump if dest greater than or equal to source
34 jgeb_www:
CJMPB_WW()
# Jump if dest greater than source
35 jgtb_www:
CJMPB_WW()
# Jump if UART not ready to transmit
36 jnt_w:
LOAD_AR() # Get the jump address in AR
ARwrite Jmpena uSreset
# Jump if no UART input receiveable
37 jnr_w:
LOAD_AR() # Get the jump address in AR
ARwrite Jmpena uSreset
# Jump to subroutine starting at given address.
# Push the return address on the stack.
# A gets destroyed. Must be X0 or X8 as jump if zero.
38 jsr_w:
LOAD_AR() # Get the jump address in AR
Stkdecr
PCwrite ADhiwrite Aread # Load PChi into A
SPwrite Awrite MEMread # Save it on the stack
Stkdecr
PCwrite ADlowrite Aread # Load PClo into A
SPwrite Awrite MEMread # Save it on the stack
CLC()
Ohiwrite ARwrite Jmpena # Copy AR to PC
uSreset
## Push and pop instructions. Choosing suitable mnemonics is hard
# Push a byte constant on the stack. Destroys A register.
39 push_b:
Stkdecr
MEMwrite Aread PCincr
SPwrite Awrite MEMread
uSreset
# Push a byte in memory on the stack. Destroys A register.
3A pushb_w:
Stkdecr
LOAD_AR()
ARwrite MEMwrite Aread
SPwrite Awrite MEMread
uSreset
# Push a word in memory on the stack.
# We have to push the high byte first to ensure
# the word on the stack is stored little endian
3B pushw_w:
Stkdecr
LOAD_AR()
ARwrite MEMwrite Aread
ARincr DRincr
ARwrite MEMwrite Bread
Awrite A-Bcomp Oread
SPwrite Olowrite MEMread
Stkdecr
SPwrite Awrite MEMread
uSreset
# Push an address on the stack. We have to push the high
# byte first to ensure the address on the stack is
# stored little endian
3C pusha_w:
Stkdecr
MEMwrite Aread PCincr
MEMwrite Bread PCincr
Awrite A-Bcomp Oread
SPwrite Olowrite MEMread
Stkdecr
SPwrite Awrite MEMread
uSreset
# Pop a byte from the stack to memory
3D popb_w:
LOAD_AR() # Get the jump address in AR
SPwrite MEMwrite Aread
Awrite ARwrite MEMread
Stkincr uSreset
# Push an address on the stack. We need to
# push the high byte first so that the
# address ends up little endian
3E pushw_a:
Stkdecr
MEMwrite Aread PCincr
MEMwrite Bread PCincr
A-Bcomp Oread
SPwrite Olowrite MEMread
Stkdecr
SPwrite Awrite MEMread
Stkdecr
uSreset
# Conditional jumps on existing flags
# Jump if equal to zero
40 jeq_w:
CJMP_FLAGS()
# Jump if less than zero
41 jlt_w:
CJMP_FLAGS()
# Jump if not equal to zero
42 jne_w:
CJMP_FLAGS()
# Jump if less than or equal to zero
43 jle_w:
CJMP_FLAGS()
# Jump if greater than or equal to zero
44 jge_w:
CJMP_FLAGS()
# Jump if greater than zero
45 jgt_w:
CJMP_FLAGS()
# Absolute jump. Has to be at X0 or X8
# so that we do a jump if zero.
# Always jump to $XXXX
48 jmp_w:
CLC()
CJMP_FLAGS()
# Return from a subroutine to the address
# previously stored on the stack.
# Must be X0 as jump if zero.
50 rts:
SPwrite MEMwrite ARloread # Get low byte of address
Stkincr
SPwrite MEMwrite ARhiread # Get high byte of address
Stkincr
CLC()
Ohiwrite ARwrite Jmpena # Copy AR to PC
uSreset
## Stack instructions
# Move a byte from a constant offset on the
# stack into a memory location
51 movb_wS:
LOAD_DR() # Get destination address
CLC() # Clear carry
SPwrite ADlowrite Bread # Get SPlo into B
MEMwrite A+B Oread PCincr # Calculate SPlo+offset
Olowrite ARloread # and store in ARlo
SPwrite ADhiwrite Bread # Get SPhi into B
MEMwrite A+B Oread PCincr # Calculate SPhi+offset
Olowrite ARhiread # and store in ARhi
ARwrite MEMwrite Aread # Load byte at that address
DRwrite Awrite MEMread # and store in the destination
uSreset
# Move a word from a constant offset on the
# stack into a memory location
52 movw_wS:
LOAD_DR() # Get destination address
CLC() # Clear carry
SPwrite ADlowrite Bread # Get SPlo into B
MEMwrite A+B Oread PCincr # Calculate SPlo+offset
Olowrite ARloread # and store in ARlo
SPwrite ADhiwrite Bread # Get SPhi into B
MEMwrite A+B Oread PCincr # Calculate SPhi+offset
Olowrite ARhiread # and store in ARhi
ARwrite MEMwrite Aread # Load byte at that address
DRwrite Awrite MEMread # and store in the destination
ARincr DRincr
ARwrite MEMwrite Aread # Load byte at next address
DRwrite Awrite MEMread # and store in the destination
uSreset
# Move a word from a memory location to
# a constant offset on the stack
54 movw_Sw:
CLC() # Clear carry
SPwrite ADlowrite Bread # Get SPlo into B
MEMwrite A+B Oread PCincr # Calculate SPlo+offset
Olowrite DRloread # and store in DRlo
SPwrite ADhiwrite Bread # Get SPhi into B
MEMwrite A+B Oread PCincr # Calculate SPhi+offset
Olowrite DRhiread # and store in DRhi
LOAD_AR() # Get source address
ARwrite MEMwrite Aread # Load byte at that address
DRwrite Awrite MEMread # and store in the destination
ARincr DRincr
ARwrite MEMwrite Aread # Load byte at next address
DRwrite Awrite MEMread # and store in the destination
uSreset
## Pointer instructions
# Move byte value at pointer into location in memory
58 movb_wp:
LOAD_DRAR() # Get dest and src address
ARwrite MEMwrite Aread # Get ptr low byte
ARincr
ARwrite MEMwrite ARhiread # Get ptr high byte into AR
Awrite ARloread # Get ptr low byte into AR
ARwrite MEMwrite Aread # Get the byte
DRwrite Awrite MEMread # and write to destination
uSreset
# Move byte value in memory to address pointed to
59 movb_pw:
LOAD_DRAR() # Get dest and src address
DRwrite MEMwrite Aread # Get ptr low byte
DRincr
DRwrite MEMwrite DRhiread # Get ptr high byte into DR
Awrite DRloread # Get ptr low byte into DR
ARwrite MEMwrite Aread # Get the byte
DRwrite Awrite MEMread # and write to destination
uSreset
# Move word value at pointer into location in memory
5A movw_wp:
LOAD_DRAR() # Get dest and src address
ARwrite MEMwrite Aread # Get ptr low byte
ARincr
ARwrite MEMwrite ARhiread # Get ptr high byte into AR
Awrite ARloread # Get ptr low byte into AR
ARwrite MEMwrite Aread # Get the low byte
DRwrite Awrite MEMread # and write to destination
ARincr DRincr
ARwrite MEMwrite Aread # Get the high byte
DRwrite Awrite MEMread # and write to destination
uSreset
# Move word value in memory to address pointed to
5B movw_pw:
LOAD_DRAR() # Get dest and src address
DRwrite MEMwrite Aread # Get ptr low byte
DRincr
DRwrite MEMwrite DRhiread # Get ptr high byte into DR
Awrite DRloread # Get ptr low byte into DR
ARwrite MEMwrite Aread # Get the low byte
DRwrite Awrite MEMread # and write to destination
ARincr DRincr
ARwrite MEMwrite Aread # Get the high byte
DRwrite Awrite MEMread # and write to destination
uSreset
# Instructions that deal with the real internal registers
# Load X with constant $XX
80 ldb_xb:
MEMwrite Aread PCincr
uSreset
# Load Y with constant $XX
81 ldb_yb:
MEMwrite Bread PCincr
uSreset
# Store X to memory
82 mov_wx:
LOAD_AR()
ARwrite Awrite MEMread
uSreset
# Store Y to memory
83 mov_wy:
LOAD_AR()
Awrite A-Bcomp Oread
ARwrite Olowrite MEMread
uSreset
# Load X from memory
84 mov_xw:
LOAD_AR()
ARwrite MEMwrite Aread
uSreset
# Load Y from memory
85 mov_yw:
LOAD_AR()
ARwrite MEMwrite Bread
uSreset
# Write X to the UART
86 out_x:
Awrite UARTread
uSreset
# Write Y to the UART
87 out_y:
Awrite A-Bcomp Oread
Olowrite UARTread
uSreset
# Read X from the UART
88 in_x:
UARTwrite Aread
uSreset
# Read Y from the UART
89 in_y:
UARTwrite Bread
uSreset
# Push X to the stack.
8A push_x:
Stkdecr
SPwrite Awrite MEMread
uSreset
# Push Y to the stack.
8B push_y:
Stkdecr
Awrite A-Bcomp Oread
SPwrite Olowrite MEMread
uSreset
# Pop X from the stack.
8C pop_x:
SPwrite MEMwrite Aread
Stkincr
uSreset
# Pop B from the stack.
8D pop_y:
SPwrite MEMwrite Bread
Stkincr
uSreset
# Initialise the Stack Pointer
8E ldw_sw:
MEMwrite SPloread PCincr
MEMwrite SPhiread PCincr
uSreset
# X/Y ALU operations
# Add with no carry
90 add_xy:
CLC() # Clear the carry
ALUB_XY(A+B)
# Subtract with no carry
91 sub_xy:
CLC() # Clear the carry
ALUB_XY(A-B)
# Add with carry
92 adc_xy:
ALUB_XY(A+B)
# Subtract with carry
93 sbc_xy:
ALUB_XY(A-B)
94 and_xy:
ALUB_XY(A&B)
95 or_xy:
ALUB_XY(A|B)
96 xor_xy:
ALUB_XY(A^B)
97 lsl_xy:
ALUB_XY(A<<B)
98 asr_xy:
ALUB_XY(A>>B)
# Multiply X by Y. Store the low 8-bits
# in Y, and the high 8-bits in X.
99 mul_xy:
Awrite A*B Oread
Olowrite Bread
Ohiwrite Aread
uSreset
9A div_xy:
ALUB_XY(A/B)
9B mod_xy:
ALUB_XY(A%B)
# Increment X if carry is set
9C cinc_x:
ALUB_XY(A+0)
# Clear X register
9D clr_x:
CLC()
Olowrite Aread
uSreset
# Increment X register
9E inc_x:
CLC()
ALUB_XY(A+1)
# Decrement X register
9F dec_x:
CLC()
ALUB_XY(A-1)
# Negate X register
A0 neg_x:
ALUB_XY(~A)
# Compare X and Y
A1 cmp_xy:
CLC()
Awrite A-Bcomp Oread
uSreset
# Copy X to Y
A2 mov_xy:
Awrite Bread
uSreset
# Copy a byte from a pointer on the stack
# to the X register
A3 mov_xP:
CLC() # Clear carry
SPwrite ADlowrite Bread # Get SPlo into B
MEMwrite A+B Oread # Add on the offset
Olowrite ARloread PCincr # Save into ARlo
SPwrite ADhiwrite Bread # Get SPhi into B
MEMwrite A+B Oread # Add on the offset
Olowrite ARhiread PCincr # Save into ARhi
ARwrite MEMwrite DRloread # Get pointer low byte
ARincr
ARwrite MEMwrite DRhiread # Get pointer high byte
DRwrite MEMwrite Aread # Get the byte, finally
uSreset
# Copy a byte from the X register
# to a pointer on the stack
A4 mov_Px:
CLC() # Clear carry
SPwrite ADlowrite Bread # Get SPlo into B
MEMwrite A+B Oread # Add on the offset
Olowrite ARloread PCincr # Save into ARlo
SPwrite ADhiwrite Bread # Get SPhi into B
MEMwrite A+B Oread # Add on the offset
Olowrite ARhiread PCincr # Save into ARhi
ARwrite MEMwrite DRloread # Get pointer low byte
ARincr
ARwrite MEMwrite DRhiread # Get pointer high byte
DRwrite MEMread Awrite # Send the byte, finally
uSreset
# Copy a byte from a SP+offset into the Y register
A5 mov_yS:
Zero Oread # Add SP+offset into DR
MEMwrite Bread PCincr
SPwrite ADlowrite A+B Oread
Olowrite DRloread
MEMwrite Bread PCincr
SPwrite ADhiwrite A+B Oread
Olowrite DRhiread
DRwrite MEMwrite Bread
uSreset
# Copy a byte from a SP+offset into the X register
A6 mov_xS:
CLC() # Add SP+offset into DR
MEMwrite Bread PCincr
SPwrite ADlowrite A+B Oread
Olowrite DRloread
MEMwrite Bread PCincr
SPwrite ADhiwrite A+B Oread
Olowrite DRhiread
DRwrite MEMwrite Aread
uSreset
# Increment the word at SP+offset
A7 incw_S:
CLC() # Add SP+offset into DR
MEMwrite Bread PCincr
SPwrite ADlowrite A+B Oread
Olowrite DRloread
MEMwrite Bread PCincr
SPwrite ADhiwrite A+B Oread
Olowrite DRhiread
DRwrite MEMwrite A+1 Oread
DRwrite Olowrite MEMread
ARincr DRincr
DRwrite MEMwrite A+0 Oread
DRwrite Olowrite MEMread
uSreset
# Copy byte from address+rY offset to X
A8 mov_xi:
CLC()
MEMwrite A+B Oread PCincr # Add lo addr byte and Y
Olowrite DRloread
MEMwrite A+0 Oread PCincr # Inc hi addr byte if needed
Olowrite DRhiread
DRwrite MEMwrite Aread
uSreset
# Copy byte from X to address+rY offset
A9 mov_ix:
CLC()
MEMwrite A+B Oread PCincr # Add lo addr byte and Y
Olowrite DRloread
MEMwrite A+0 Oread PCincr # Inc hi addr byte if needed
Olowrite DRhiread
DRwrite Awrite MEMread
uSreset