This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
sequencer_rom.py
1000 lines (857 loc) · 36 KB
/
sequencer_rom.py
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
# Disable pylint's "your name is too short" warning.
# pylint: disable=C0103
# Disable protected access warnings
# pylint: disable=W0212
from nmigen import Signal, Module, Elaboratable
from nmigen.build import Platform
from consts import AluOp, AluFunc, BranchCond, MemAccessWidth
from consts import OpcodeFormat, SystemFunc, TrapCauseSelect
from consts import InstrReg, OpcodeSelect
from consts import NextPC, SeqMuxSelect, ConstSelect
class SequencerROM(Elaboratable):
"""ROM for the sequencer card state machine."""
def __init__(self):
# Control line
self.enable_sequencer_rom = Signal()
# Inputs: 9 + 11 decoder
# Since this is implemented by a ROM, the address lines
# must be stable in order for the outputs to start becoming
# stable. This means that if any input address depends on
# any output data combinatorically, there's a danger of
# going unstable. Therefore, all address lines must be
# registered, or come combinatorically from registered data.
self.memaddr_2_lsb = Signal(2)
self.branch_cond = Signal()
self._instr_phase = Signal(2)
# Only used on instruction phase 1 in BRANCH.
self.data_z_in_2_lsb0 = Signal()
self.imm0 = Signal()
self.rd0 = Signal()
self.rs1_0 = Signal()
# Instruction decoding
self.opcode_select = Signal(OpcodeSelect) # 4 bits
self._funct3 = Signal(3)
self._alu_func = Signal(4)
##############
# Outputs (66 bits total)
##############
# Raised on the last phase of an instruction.
self.set_instr_complete = Signal()
# Raised when the exception card should store trap data.
self.save_trap_csrs = Signal()
# CSR lines
self.csr_to_x = Signal()
self.z_to_csr = Signal()
# Memory
self.mem_rd = Signal(reset=1)
self.mem_wr = Signal()
# Bytes in memory word to write
self.mem_wr_mask = Signal(4)
self._next_instr_phase = Signal(2)
self._x_reg_select = Signal(InstrReg) # 2 bits
self._y_reg_select = Signal(InstrReg) # 2 bits
self._z_reg_select = Signal(InstrReg) # 2 bits
# -> X
self.x_mux_select = Signal(SeqMuxSelect)
self.reg_to_x = Signal()
# -> Y
self.y_mux_select = Signal(SeqMuxSelect)
self.reg_to_y = Signal()
# -> Z
self.z_mux_select = Signal(SeqMuxSelect)
self.alu_op_to_z = Signal(AluOp) # 4 bits
# -> PC
self.pc_mux_select = Signal(SeqMuxSelect)
# -> tmp
self.tmp_mux_select = Signal(SeqMuxSelect)
# -> csr_num
self._funct12_to_csr_num = Signal()
self._mepc_num_to_csr_num = Signal()
self._mcause_to_csr_num = Signal()
# -> memaddr
self.memaddr_mux_select = Signal(SeqMuxSelect)
# -> memdata
self.memdata_wr_mux_select = Signal(SeqMuxSelect)
self._const = Signal(ConstSelect) # select: 4 bits
self.enter_trap = Signal()
self.exit_trap = Signal()
# Signals for next registers
self.load_trap = Signal()
self.next_trap = Signal()
self.load_exception = Signal()
self.next_exception = Signal()
self.next_fatal = Signal()
def elaborate(self, _: Platform) -> Module:
"""Implements the logic of the sequencer card."""
m = Module()
# Defaults
m.d.comb += [
self._next_instr_phase.eq(0),
self.reg_to_x.eq(0),
self.reg_to_y.eq(0),
self.alu_op_to_z.eq(AluOp.NONE),
self.mem_rd.eq(0),
self.mem_wr.eq(0),
self.mem_wr_mask.eq(0),
self.csr_to_x.eq(0),
self.z_to_csr.eq(0),
self._funct12_to_csr_num.eq(0),
self._mepc_num_to_csr_num.eq(0),
self._mcause_to_csr_num.eq(0),
self._x_reg_select.eq(0),
self._y_reg_select.eq(0),
self._z_reg_select.eq(0),
self.enter_trap.eq(0),
self.exit_trap.eq(0),
self.save_trap_csrs.eq(0),
self.pc_mux_select.eq(SeqMuxSelect.PC),
self.memaddr_mux_select.eq(SeqMuxSelect.MEMADDR),
self.memdata_wr_mux_select.eq(SeqMuxSelect.MEMDATA_WR),
self.tmp_mux_select.eq(SeqMuxSelect.TMP),
self.x_mux_select.eq(SeqMuxSelect.X),
self.y_mux_select.eq(SeqMuxSelect.Y),
self.z_mux_select.eq(SeqMuxSelect.Z),
self._const.eq(0),
]
m.d.comb += [
self.load_trap.eq(0),
self.next_trap.eq(0),
self.load_exception.eq(0),
self.next_exception.eq(0),
self.next_fatal.eq(0),
]
with m.If(self.enable_sequencer_rom):
# Output control signals
with m.Switch(self.opcode_select):
with m.Case(OpcodeSelect.LUI):
self.handle_lui(m)
with m.Case(OpcodeSelect.AUIPC):
self.handle_auipc(m)
with m.Case(OpcodeSelect.OP_IMM):
self.handle_op_imm(m)
with m.Case(OpcodeSelect.OP):
self.handle_op(m)
with m.Case(OpcodeSelect.JAL):
self.handle_jal(m)
with m.Case(OpcodeSelect.JALR):
self.handle_jalr(m)
with m.Case(OpcodeSelect.BRANCH):
self.handle_branch(m)
with m.Case(OpcodeSelect.LOAD):
self.handle_load(m)
with m.Case(OpcodeSelect.STORE):
self.handle_store(m)
with m.Case(OpcodeSelect.CSRS):
self.handle_csrs(m)
with m.Case(OpcodeSelect.MRET):
self.handle_MRET(m)
with m.Case(OpcodeSelect.ECALL):
self.handle_ECALL(m)
with m.Case(OpcodeSelect.EBREAK):
self.handle_EBREAK(m)
with m.Default():
self.handle_illegal_instr(m)
return m
def next_instr(self, m: Module, next_pc: NextPC = NextPC.PC_PLUS_4):
"""Sets signals to advance to the next instruction.
next_pc is the signal to load the PC and MEMADDR registers with
at the end of the instruction cycle.
"""
m.d.comb += self.set_instr_complete.eq(1)
if next_pc == NextPC.PC_PLUS_4:
m.d.comb += self.pc_mux_select.eq(SeqMuxSelect.PC_PLUS_4)
m.d.comb += self.memaddr_mux_select.eq(SeqMuxSelect.PC_PLUS_4)
elif next_pc == NextPC.MEMADDR:
m.d.comb += self.pc_mux_select.eq(SeqMuxSelect.MEMADDR)
elif next_pc == NextPC.MEMADDR_NO_LSB:
m.d.comb += self.pc_mux_select.eq(SeqMuxSelect.MEMADDR_LSB_MASKED)
elif next_pc == NextPC.Z:
m.d.comb += self.pc_mux_select.eq(SeqMuxSelect.Z)
m.d.comb += self.memaddr_mux_select.eq(SeqMuxSelect.Z)
elif next_pc == NextPC.X:
m.d.comb += self.pc_mux_select.eq(SeqMuxSelect.X)
m.d.comb += self.memaddr_mux_select.eq(SeqMuxSelect.X)
def set_exception(self, m: Module, exc: ConstSelect, mtval: SeqMuxSelect, fatal: bool = True):
m.d.comb += self.load_exception.eq(1)
m.d.comb += self.next_exception.eq(1)
m.d.comb += self.next_fatal.eq(1 if fatal else 0)
m.d.comb += self._const.eq(exc)
m.d.comb += self.x_mux_select.eq(SeqMuxSelect.CONST)
m.d.comb += self.z_mux_select.eq(mtval)
if fatal:
m.d.comb += self.y_mux_select.eq(SeqMuxSelect.PC)
else:
m.d.comb += self.y_mux_select.eq(SeqMuxSelect.PC_PLUS_4)
# X -> MCAUSE, Y -> MEPC, Z -> MTVAL
m.d.comb += self.save_trap_csrs.eq(1)
m.d.comb += self.load_trap.eq(1)
m.d.comb += self.next_trap.eq(1)
m.d.comb += self._next_instr_phase.eq(0)
def handle_illegal_instr(self, m: Module):
self.set_exception(m, ConstSelect.EXC_ILLEGAL_INSTR,
mtval=SeqMuxSelect.INSTR)
def handle_lui(self, m: Module):
"""Adds the LUI logic to the given module.
rd <- r0 + imm
PC <- PC + 4
r0 -> X
imm -> Y
ALU ADD -> Z
Z -> rd
PC + 4 -> PC
PC + 4 -> memaddr
"""
m.d.comb += [
self.reg_to_x.eq(1),
self._x_reg_select.eq(InstrReg.ZERO),
self.y_mux_select.eq(SeqMuxSelect.IMM),
self.alu_op_to_z.eq(AluOp.ADD),
self._z_reg_select.eq(InstrReg.RD),
]
self.next_instr(m)
def handle_auipc(self, m: Module):
"""Adds the AUIPC logic to the given module.
rd <- PC + imm
PC <- PC + 4
PC -> X
imm -> Y
ALU ADD -> Z
Z -> rd
PC + 4 -> PC
PC + 4 -> memaddr
"""
m.d.comb += [
self.x_mux_select.eq(SeqMuxSelect.PC),
self.y_mux_select.eq(SeqMuxSelect.IMM),
self.alu_op_to_z.eq(AluOp.ADD),
self._z_reg_select.eq(InstrReg.RD),
]
self.next_instr(m)
def handle_op_imm(self, m: Module):
"""Adds the OP_IMM logic to the given module.
rd <- rs1 op imm
PC <- PC + 4
rs1 -> X
imm -> Y
ALU op -> Z
Z -> rd
PC + 4 -> PC
PC + 4 -> memaddr
"""
with m.If(~self._alu_func.matches(AluFunc.ADD, AluFunc.SUB, AluFunc.SLL, AluFunc.SLT,
AluFunc.SLTU, AluFunc.XOR, AluFunc.SRL, AluFunc.SRA, AluFunc.OR, AluFunc.AND)):
self.handle_illegal_instr(m)
with m.Else():
m.d.comb += [
self.reg_to_x.eq(1),
self._x_reg_select.eq(InstrReg.RS1),
self.y_mux_select.eq(SeqMuxSelect.IMM),
self._z_reg_select.eq(InstrReg.RD),
]
with m.Switch(self._alu_func):
with m.Case(AluFunc.ADD):
m.d.comb += self.alu_op_to_z.eq(AluOp.ADD)
with m.Case(AluFunc.SUB):
m.d.comb += self.alu_op_to_z.eq(AluOp.SUB)
with m.Case(AluFunc.SLL):
m.d.comb += self.alu_op_to_z.eq(AluOp.SLL)
with m.Case(AluFunc.SLT):
m.d.comb += self.alu_op_to_z.eq(AluOp.SLT)
with m.Case(AluFunc.SLTU):
m.d.comb += self.alu_op_to_z.eq(AluOp.SLTU)
with m.Case(AluFunc.XOR):
m.d.comb += self.alu_op_to_z.eq(AluOp.XOR)
with m.Case(AluFunc.SRL):
m.d.comb += self.alu_op_to_z.eq(AluOp.SRL)
with m.Case(AluFunc.SRA):
m.d.comb += self.alu_op_to_z.eq(AluOp.SRA)
with m.Case(AluFunc.OR):
m.d.comb += self.alu_op_to_z.eq(AluOp.OR)
with m.Case(AluFunc.AND):
m.d.comb += self.alu_op_to_z.eq(AluOp.AND)
self.next_instr(m)
def handle_op(self, m: Module):
"""Adds the OP logic to the given module.
rd <- rs1 op rs2
PC <- PC + 4
rs1 -> X
rs2 -> Y
ALU op -> Z
Z -> rd
PC + 4 -> PC
PC + 4 -> memaddr
"""
with m.If(~self._alu_func.matches(AluFunc.ADD, AluFunc.SUB, AluFunc.SLL, AluFunc.SLT,
AluFunc.SLTU, AluFunc.XOR, AluFunc.SRL, AluFunc.SRA, AluFunc.OR, AluFunc.AND)):
self.handle_illegal_instr(m)
with m.Else():
m.d.comb += [
self.reg_to_x.eq(1),
self._x_reg_select.eq(InstrReg.RS1),
self.reg_to_y.eq(1),
self._y_reg_select.eq(InstrReg.RS2),
self._z_reg_select.eq(InstrReg.RD),
]
with m.Switch(self._alu_func):
with m.Case(AluFunc.ADD):
m.d.comb += self.alu_op_to_z.eq(AluOp.ADD)
with m.Case(AluFunc.SUB):
m.d.comb += self.alu_op_to_z.eq(AluOp.SUB)
with m.Case(AluFunc.SLL):
m.d.comb += self.alu_op_to_z.eq(AluOp.SLL)
with m.Case(AluFunc.SLT):
m.d.comb += self.alu_op_to_z.eq(AluOp.SLT)
with m.Case(AluFunc.SLTU):
m.d.comb += self.alu_op_to_z.eq(AluOp.SLTU)
with m.Case(AluFunc.XOR):
m.d.comb += self.alu_op_to_z.eq(AluOp.XOR)
with m.Case(AluFunc.SRL):
m.d.comb += self.alu_op_to_z.eq(AluOp.SRL)
with m.Case(AluFunc.SRA):
m.d.comb += self.alu_op_to_z.eq(AluOp.SRA)
with m.Case(AluFunc.OR):
m.d.comb += self.alu_op_to_z.eq(AluOp.OR)
with m.Case(AluFunc.AND):
m.d.comb += self.alu_op_to_z.eq(AluOp.AND)
self.next_instr(m)
def handle_jal(self, m: Module):
"""Adds the JAL logic to the given module.
rd <- PC + 4, PC <- PC + imm
PC -> X
imm -> Y
ALU ADD -> Z
Z -> memaddr
---------------------
PC + 4 -> Z
Z -> rd
memaddr -> PC # This will zero the least significant bit
Note that because the immediate value for JAL has its least
significant bit set to zero by definition, and the PC is also
assumed to be aligned, there is no loss in generality to clear
the least significant bit when transferring memaddr to PC.
"""
with m.If(self._instr_phase == 0):
m.d.comb += [
self.x_mux_select.eq(SeqMuxSelect.PC),
self.y_mux_select.eq(SeqMuxSelect.IMM),
self.alu_op_to_z.eq(AluOp.ADD),
self.memaddr_mux_select.eq(SeqMuxSelect.Z),
self._next_instr_phase.eq(1),
]
with m.Else():
with m.If(self.memaddr_2_lsb[1] != 0):
self.set_exception(
m, ConstSelect.EXC_INSTR_ADDR_MISALIGN, mtval=SeqMuxSelect.MEMADDR)
with m.Else():
m.d.comb += [
self.z_mux_select.eq(SeqMuxSelect.PC_PLUS_4),
self._z_reg_select.eq(InstrReg.RD),
]
self.next_instr(m, NextPC.MEMADDR_NO_LSB)
def handle_jalr(self, m: Module):
"""Adds the JALR logic to the given module.
rd <- PC + 4, PC <- (rs1 + imm) & 0xFFFFFFFE
rs1 -> X
imm -> Y
ALU ADD -> Z
Z -> memaddr
---------------------
PC + 4 -> Z
Z -> rd
memaddr -> PC # This will zero the least significant bit
"""
with m.If(self._instr_phase == 0):
m.d.comb += [
self.reg_to_x.eq(1),
self._x_reg_select.eq(InstrReg.RS1),
self.y_mux_select.eq(SeqMuxSelect.IMM),
self.alu_op_to_z.eq(AluOp.ADD),
self.memaddr_mux_select.eq(SeqMuxSelect.Z),
self._next_instr_phase.eq(1),
]
with m.Else():
with m.If(self.memaddr_2_lsb[1] != 0):
self.set_exception(
m, ConstSelect.EXC_INSTR_ADDR_MISALIGN, mtval=SeqMuxSelect.MEMADDR_LSB_MASKED)
with m.Else():
m.d.comb += [
self.z_mux_select.eq(SeqMuxSelect.PC_PLUS_4),
self._z_reg_select.eq(InstrReg.RD),
]
self.next_instr(m, NextPC.MEMADDR_NO_LSB)
def handle_branch(self, m: Module):
"""Adds the BRANCH logic to the given module.
cond <- rs1 - rs2 < 0, rs1 - rs2 == 0
if f(cond):
PC <- PC + imm
else:
PC <- PC + 4
rs1 -> X
rs2 -> Y
ALU SUB -> Z, cond
--------------------- cond == 1
PC -> X
imm/4 -> Y (imm for cond == 1, 4 otherwise)
ALU ADD -> Z
Z -> PC
Z -> memaddr
--------------------- cond == 0
PC + 4 -> PC
PC + 4 -> memaddr
"""
with m.If(self._instr_phase == 0):
m.d.comb += [
self.reg_to_x.eq(1),
self._x_reg_select.eq(InstrReg.RS1),
self.reg_to_y.eq(1),
self._y_reg_select.eq(InstrReg.RS2),
self.alu_op_to_z.eq(AluOp.SUB),
self._next_instr_phase.eq(1),
]
with m.Elif(self._instr_phase == 1):
with m.If(~self._funct3.matches(BranchCond.EQ, BranchCond.NE,
BranchCond.LT, BranchCond.GE,
BranchCond.LTU, BranchCond.GEU)):
self.handle_illegal_instr(m)
with m.Else():
with m.If(self.branch_cond):
m.d.comb += self.y_mux_select.eq(SeqMuxSelect.IMM)
with m.Else():
m.d.comb += self._const.eq(ConstSelect.SHAMT_4)
m.d.comb += self.y_mux_select.eq(SeqMuxSelect.CONST)
m.d.comb += [
self.x_mux_select.eq(SeqMuxSelect.PC),
self.alu_op_to_z.eq(AluOp.ADD),
]
with m.If(self.data_z_in_2_lsb0):
self.next_instr(m, NextPC.Z)
with m.Else():
m.d.comb += self._next_instr_phase.eq(2)
m.d.comb += self.tmp_mux_select.eq(SeqMuxSelect.Z)
with m.Else():
self.set_exception(
m, ConstSelect.EXC_INSTR_ADDR_MISALIGN, mtval=SeqMuxSelect.TMP)
def handle_load(self, m: Module):
"""Adds the LOAD logic to the given module.
Note that byte loads are byte-aligned, half-word loads
are 16-bit aligned, and word loads are 32-bit aligned.
Attempting to load unaligned will lead to undefined
behavior.
Operation is to load 32 bits from a 32-bit aligned
address, and then perform at most two shifts to get
the desired behavior: a shift left to get the most
significant byte into the leftmost position, then a
shift right to zero or sign extend the value.
For example, for loading a half-word starting at
address A where A%4=0, we first load the full 32
bits at that address, resulting in XYHL, where X and
Y are unwanted and H and L are the half-word we want
to load. Then we shift left by 16: HL00. And finally
we shift right by 16, either signed or unsigned
depending on whether we are doing an LH or an LHU:
ssHL / 00HL.
addr <- rs1 + imm
rd <- data at addr, possibly sign-extended
PC <- PC + 4
If we let N be addr%4, then:
instr N shift1 shift2
--------------------------
LB 0 SLL 24 SRA 24
LB 1 SLL 16 SRA 24
LB 2 SLL 8 SRA 24
LB 3 SLL 0 SRA 24
LBU 0 SLL 24 SRL 24
LBU 1 SLL 16 SRL 24
LBU 2 SLL 8 SRL 24
LBU 3 SLL 0 SRL 24
LH 0 SLL 16 SRA 16
LH 2 SLL 0 SRA 16
LHU 0 SLL 16 SRL 16
LHU 2 SLL 0 SRL 16
LW 0 SLL 0 SRA 0
(all other N are misaligned accesses)
Where there is an SLL 0, the machine cycle
could be skipped, but in the interests of
simpler logic, we will not do that.
rs1 -> X
imm -> Y
ALU ADD -> Z
Z -> memaddr
---------------------
memdata -> X
shamt1 -> Y
ALU SLL -> Z
Z -> rd
---------------------
rd -> X
shamt2 -> Y
ALU SRA/SRL -> Z
Z -> rd
PC + 4 -> PC
PC + 4 -> memaddr
"""
with m.If(self._instr_phase == 0):
m.d.comb += [
self.reg_to_x.eq(1),
self._x_reg_select.eq(InstrReg.RS1),
self.y_mux_select.eq(SeqMuxSelect.IMM),
self.alu_op_to_z.eq(AluOp.ADD),
self.memaddr_mux_select.eq(SeqMuxSelect.Z),
self._next_instr_phase.eq(1),
]
with m.Elif(self._instr_phase == 1):
# Check for exception conditions first
with m.If(self._funct3.matches(MemAccessWidth.H, MemAccessWidth.HU) &
self.memaddr_2_lsb[0]):
self.set_exception(
m, ConstSelect.EXC_LOAD_ADDR_MISALIGN, mtval=SeqMuxSelect.MEMADDR)
with m.Elif((self._funct3 == MemAccessWidth.W) &
(self.memaddr_2_lsb != 0)):
self.set_exception(
m, ConstSelect.EXC_LOAD_ADDR_MISALIGN, mtval=SeqMuxSelect.MEMADDR)
with m.Elif(~self._funct3.matches(MemAccessWidth.B, MemAccessWidth.BU,
MemAccessWidth.H, MemAccessWidth.HU,
MemAccessWidth.W)):
self.handle_illegal_instr(m)
with m.Else():
m.d.comb += [
self.mem_rd.eq(1),
self.x_mux_select.eq(SeqMuxSelect.MEMDATA_RD),
self.y_mux_select.eq(SeqMuxSelect.CONST),
self.alu_op_to_z.eq(AluOp.SLL),
self._z_reg_select.eq(InstrReg.RD),
self._next_instr_phase.eq(2),
]
with m.Switch(self._funct3):
with m.Case(MemAccessWidth.B, MemAccessWidth.BU):
with m.Switch(self.memaddr_2_lsb):
with m.Case(0):
m.d.comb += self._const.eq(
ConstSelect.SHAMT_24)
with m.Case(1):
m.d.comb += self._const.eq(
ConstSelect.SHAMT_16)
with m.Case(2):
m.d.comb += self._const.eq(ConstSelect.SHAMT_8)
with m.Case(3):
m.d.comb += self._const.eq(ConstSelect.SHAMT_0)
with m.Case(MemAccessWidth.H, MemAccessWidth.HU):
with m.Switch(self.memaddr_2_lsb):
with m.Case(0):
m.d.comb += self._const.eq(
ConstSelect.SHAMT_16)
with m.Case(2):
m.d.comb += self._const.eq(ConstSelect.SHAMT_0)
with m.Case(MemAccessWidth.W):
m.d.comb += self._const.eq(ConstSelect.SHAMT_0)
with m.Else():
m.d.comb += [
self.reg_to_x.eq(1),
self._x_reg_select.eq(InstrReg.RD),
self.y_mux_select.eq(SeqMuxSelect.CONST),
self._z_reg_select.eq(InstrReg.RD),
]
with m.Switch(self._funct3):
with m.Case(MemAccessWidth.B):
m.d.comb += [
self._const.eq(ConstSelect.SHAMT_24),
self.alu_op_to_z.eq(AluOp.SRA),
]
with m.Case(MemAccessWidth.BU):
m.d.comb += [
self._const.eq(ConstSelect.SHAMT_24),
self.alu_op_to_z.eq(AluOp.SRL),
]
with m.Case(MemAccessWidth.H):
m.d.comb += [
self._const.eq(ConstSelect.SHAMT_16),
self.alu_op_to_z.eq(AluOp.SRA),
]
with m.Case(MemAccessWidth.HU):
m.d.comb += [
self._const.eq(ConstSelect.SHAMT_16),
self.alu_op_to_z.eq(AluOp.SRL),
]
with m.Case(MemAccessWidth.W):
m.d.comb += [
self._const.eq(ConstSelect.SHAMT_0),
self.alu_op_to_z.eq(AluOp.SRL),
]
self.next_instr(m)
def handle_store(self, m: Module):
"""Adds the STORE logic to the given module.
Note that byte stores are byte-aligned, half-word stores
are 16-bit aligned, and word stores are 32-bit aligned.
Attempting to stores unaligned will lead to undefined
behavior.
addr <- rs1 + imm
data <- rs2
PC <- PC + 4
rs1 -> X
imm -> Y
ALU ADD -> Z
Z -> memaddr
---------------------
rs2 -> X
shamt -> Y
ALU SLL -> Z
Z -> wrdata
-> wrmask
---------------------
PC + 4 -> PC
PC + 4 -> memaddr
"""
with m.If(self._instr_phase == 0):
m.d.comb += [
self.reg_to_x.eq(1),
self._x_reg_select.eq(InstrReg.RS1),
self.y_mux_select.eq(SeqMuxSelect.IMM),
self.alu_op_to_z.eq(AluOp.ADD),
self.memaddr_mux_select.eq(SeqMuxSelect.Z),
self._next_instr_phase.eq(1),
]
with m.Elif(self._instr_phase == 1):
# Check for exception conditions first
with m.If((self._funct3 == MemAccessWidth.H) & self.memaddr_2_lsb[0]):
self.set_exception(
m, ConstSelect.EXC_STORE_AMO_ADDR_MISALIGN, mtval=SeqMuxSelect.MEMADDR)
with m.Elif((self._funct3 == MemAccessWidth.W) & (self.memaddr_2_lsb != 0)):
self.set_exception(
m, ConstSelect.EXC_STORE_AMO_ADDR_MISALIGN, mtval=SeqMuxSelect.MEMADDR)
with m.Elif(~self._funct3.matches(MemAccessWidth.B,
MemAccessWidth.H,
MemAccessWidth.W)):
self.handle_illegal_instr(m)
with m.Else():
m.d.comb += [
self.reg_to_x.eq(1),
self._x_reg_select.eq(InstrReg.RS2),
self.y_mux_select.eq(SeqMuxSelect.CONST),
self.alu_op_to_z.eq(AluOp.SLL),
self.memdata_wr_mux_select.eq(SeqMuxSelect.Z),
self._next_instr_phase.eq(2),
]
with m.Switch(self._funct3):
with m.Case(MemAccessWidth.B):
with m.Switch(self.memaddr_2_lsb):
with m.Case(0):
m.d.comb += self._const.eq(ConstSelect.SHAMT_0)
with m.Case(1):
m.d.comb += self._const.eq(ConstSelect.SHAMT_8)
with m.Case(2):
m.d.comb += self._const.eq(
ConstSelect.SHAMT_16)
with m.Case(3):
m.d.comb += self._const.eq(
ConstSelect.SHAMT_24)
with m.Case(MemAccessWidth.H):
with m.Switch(self.memaddr_2_lsb):
with m.Case(0):
m.d.comb += self._const.eq(ConstSelect.SHAMT_0)
with m.Case(2):
m.d.comb += self._const.eq(
ConstSelect.SHAMT_16)
with m.Case(MemAccessWidth.W):
m.d.comb += self._const.eq(ConstSelect.SHAMT_0)
with m.Else():
with m.Switch(self._funct3):
with m.Case(MemAccessWidth.B):
with m.Switch(self.memaddr_2_lsb):
with m.Case(0):
m.d.comb += self.mem_wr_mask.eq(0b0001)
with m.Case(1):
m.d.comb += self.mem_wr_mask.eq(0b0010)
with m.Case(2):
m.d.comb += self.mem_wr_mask.eq(0b0100)
with m.Case(3):
m.d.comb += self.mem_wr_mask.eq(0b1000)
with m.Case(MemAccessWidth.H):
with m.Switch(self.memaddr_2_lsb):
with m.Case(0):
m.d.comb += self.mem_wr_mask.eq(0b0011)
with m.Case(2):
m.d.comb += self.mem_wr_mask.eq(0b1100)
with m.Case(MemAccessWidth.W):
m.d.comb += self.mem_wr_mask.eq(0b1111)
m.d.comb += self.mem_wr.eq(1)
self.next_instr(m)
def handle_csrs(self, m: Module):
"""Adds the SYSTEM (CSR opcodes) logic to the given module.
Some points of interest:
* Attempts to write a read-only register
result in an illegal instruction exception.
* Attempts to access a CSR that doesn't exist
result in an illegal instruction exception.
* Attempts to write read-only bits to a read/write CSR
are ignored.
Because we're building this in hardware, which is
expensive, we're not implementing any CSRs that aren't
strictly necessary. The documentation for the misa, mvendorid,
marchid, and mimpid registers state that they can return zero if
unimplemented. This implies that unimplemented CSRs still
exist.
The mhartid, because we only have one HART, can just return zero.
"""
with m.Switch(self._funct3):
with m.Case(SystemFunc.CSRRW):
self.handle_CSRRW(m)
with m.Case(SystemFunc.CSRRWI):
self.handle_CSRRWI(m)
with m.Case(SystemFunc.CSRRS):
self.handle_CSRRS(m)
with m.Case(SystemFunc.CSRRSI):
self.handle_CSRRSI(m)
with m.Case(SystemFunc.CSRRC):
self.handle_CSRRC(m)
with m.Case(SystemFunc.CSRRCI):
self.handle_CSRRCI(m)
with m.Default():
self.handle_illegal_instr(m)
def handle_CSRRW(self, m: Module):
m.d.comb += self._funct12_to_csr_num.eq(1)
with m.If(self._instr_phase == 0):
with m.If(self.rd0):
m.d.comb += [
self._x_reg_select.eq(InstrReg.ZERO),
self.reg_to_x.eq(1),
]
with m.Else():
m.d.comb += [
self.csr_to_x.eq(1)
]
m.d.comb += [
self._y_reg_select.eq(InstrReg.RS1),
self.reg_to_y.eq(1),
self.alu_op_to_z.eq(AluOp.Y),
self.z_to_csr.eq(1),
self.tmp_mux_select.eq(SeqMuxSelect.X),
self._next_instr_phase.eq(1),
]
with m.Else():
m.d.comb += [
self.z_mux_select.eq(SeqMuxSelect.TMP),
self._z_reg_select.eq(InstrReg.RD),
]
self.next_instr(m)
def handle_CSRRWI(self, m: Module):
m.d.comb += self._funct12_to_csr_num.eq(1)
with m.If(self._instr_phase == 0):
with m.If(self.rd0):
m.d.comb += [
self._x_reg_select.eq(InstrReg.ZERO),
self.reg_to_x.eq(1),
]
with m.Else():
m.d.comb += [
self.csr_to_x.eq(1)
]
m.d.comb += [
self.y_mux_select.eq(SeqMuxSelect.IMM),
self.alu_op_to_z.eq(AluOp.Y),
self.z_to_csr.eq(1),
self.tmp_mux_select.eq(SeqMuxSelect.X),
self._next_instr_phase.eq(1),
]
with m.Else():
m.d.comb += [
self.z_mux_select.eq(SeqMuxSelect.TMP),
self._z_reg_select.eq(InstrReg.RD),
]
self.next_instr(m)
def handle_CSRRS(self, m: Module):
m.d.comb += self._funct12_to_csr_num.eq(1)
with m.If(self._instr_phase == 0):
m.d.comb += [
self.csr_to_x.eq(1),
self._y_reg_select.eq(InstrReg.RS1),
self.reg_to_y.eq(1),
self.alu_op_to_z.eq(AluOp.OR),
self.z_to_csr.eq(~self.rs1_0),
self.tmp_mux_select.eq(SeqMuxSelect.X),
self._next_instr_phase.eq(1),
]
with m.Else():
m.d.comb += [
self.z_mux_select.eq(SeqMuxSelect.TMP),
self._z_reg_select.eq(InstrReg.RD),
]
self.next_instr(m)
def handle_CSRRSI(self, m: Module):
m.d.comb += self._funct12_to_csr_num.eq(1)
with m.If(self._instr_phase == 0):
m.d.comb += [
self.csr_to_x.eq(1),
self.y_mux_select.eq(SeqMuxSelect.IMM),
self.alu_op_to_z.eq(AluOp.OR),
self.z_to_csr.eq(~self.imm0),
self.tmp_mux_select.eq(SeqMuxSelect.X),
self._next_instr_phase.eq(1),
]
with m.Else():
m.d.comb += [
self.z_mux_select.eq(SeqMuxSelect.TMP),
self._z_reg_select.eq(InstrReg.RD),
]
self.next_instr(m)
def handle_CSRRC(self, m: Module):
m.d.comb += self._funct12_to_csr_num.eq(1)
with m.If(self._instr_phase == 0):
m.d.comb += [
self.csr_to_x.eq(1),
self._y_reg_select.eq(InstrReg.RS1),
self.reg_to_y.eq(1),
self.alu_op_to_z.eq(AluOp.AND_NOT),
self.z_to_csr.eq(~self.rs1_0),
self.tmp_mux_select.eq(SeqMuxSelect.X),
self._next_instr_phase.eq(1),
]
with m.Else():
m.d.comb += [
self.z_mux_select.eq(SeqMuxSelect.TMP),
self._z_reg_select.eq(InstrReg.RD),
]
self.next_instr(m)
def handle_CSRRCI(self, m: Module):
m.d.comb += self._funct12_to_csr_num.eq(1)
with m.If(self._instr_phase == 0):
m.d.comb += [
self.csr_to_x.eq(1),
self.y_mux_select.eq(SeqMuxSelect.IMM),
self.alu_op_to_z.eq(AluOp.AND_NOT),
self.z_to_csr.eq(~self.imm0),
self.tmp_mux_select.eq(SeqMuxSelect.X),
self._next_instr_phase.eq(1),
]
with m.Else():
m.d.comb += [
self.z_mux_select.eq(SeqMuxSelect.TMP),
self._z_reg_select.eq(InstrReg.RD),
]
self.next_instr(m)
def handle_MRET(self, m: Module):
m.d.comb += [
self._mepc_num_to_csr_num.eq(1),
self.csr_to_x.eq(1),
self.exit_trap.eq(1),
]
self.next_instr(m, NextPC.X)
def handle_ECALL(self, m: Module):
"""Handles the ECALL instruction.
Note that normally, ECALL is used from a lower privelege mode, which stores
the PC of the instruction in the appropriate lower EPC CSR (e.g. SEPC or UEPC).
This allows interrupts to be handled during the call, because we're in a higher
privelege level. However, in machine mode, there is no higher privelege level,
so we have no choice but to disable interrupts for an ECALL.
"""
self.set_exception(
m, ConstSelect.EXC_ECALL_FROM_MACH_MODE, mtval=SeqMuxSelect.PC, fatal=False)
def handle_EBREAK(self, m: Module):
"""Handles the EBREAK instruction.
Note that normally, EBREAK is used from a lower privelege mode, which stores
the PC of the instruction in the appropriate lower EPC CSR (e.g. SEPC or UEPC).
This allows interrupts to be handled during the call, because we're in a higher
privelege level. However, in machine mode, there is no higher privelege level,
so we have no choice but to disable interrupts for an EBREAK.
"""
self.set_exception(
m, ConstSelect.EXC_BREAKPOINT, mtval=SeqMuxSelect.PC, fatal=False)