forked from stevehoover/warp-v
-
Notifications
You must be signed in to change notification settings - Fork 0
/
warp-v.tlv
1427 lines (1217 loc) · 57.7 KB
/
warp-v.tlv
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
\m4_TLV_version 1d: tl-x.org
\SV
// -----------------------------------------------------------------------------
// Copyright (c) 2017, Steven F. Hoover
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * The name Steven F. Hoover
// may not be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
// This code is mastered in https://github.com/stevehoover/warp-v.git
m4+makerchip_header(['
// A highly-parameterized CPU generator, configurable for:
// o An ISA of your choice, where the following ISAs are currently defined herein:
// - An uber-simple mini CPU for academic use
// - RISC-V (incomplete)
// o Pipeline staging (from 1 to 7 stages)
// o Architectural parameters, like memory size, etc.
// This file includes:
// o The configurable (esp. for pipeline depth) ISA-independent CPU logic.
// o ISA-specific decode and execute logic for mini-CPU, RISC-V, and a dummy
// (for diagrams).
// o A simple RISC-V assembler. (The Mini ISA needs no assembler as it uses strings for instructions.)
// o A tiny test program for each ISA
// Notes:
// o This code makes heavy use of macro preprocessing with M4 (https://www.gnu.org/software/m4/manual/m4.html),
// as well as "m4+" macros supported by Perl preprocessing. Neither of these are
// currently documented or supported for general use. This design is shared to illustrate
// the potential, not with the expectation that folks will evolve the design on their own.
// (If you are interested in doing so, please contact me at [email protected],
// and I would be happy to provide assistance.)
// o The preprocessed code is represented in the "Nav-TLV" tab. You can debug using
// Nav-TLV and find corresponding source lines by clicking Nav-TLV line numbers.
// o The "Diagram" may fail to generate due to the size of the design.
// The CPU
// The code is parameterized, using the M4 macro preprocessor, for adjustable pipeline
// depth.
//
// Overview:
// o One instruction traverses the single free-flowing CPU pipeline per cycle.
// o There is no branch or condition or target prediction.
// o Instructions are in-order, but the uarch supports loads that return their
// data out of order (though, they don't).
//
// Replays:
//
// The PC is redirected, and inflight instructions are squashed (their results are
// not committed) for:
// o jumps, which go to an absolute jump target address
// o unconditioned and true-conditioned branches, which go to branch target
// o instructions that consume a pending register, which replay instruction immediately
// (See "Loads", below.)
// o loads that write to a pending register, which replay instruction immediately
// (See "Loads", below.)
//
// Loads:
//
// Load instructions complete without writing their destination registers. Destination
// registers are instead marked "pending", and reads of pending registers are replayed.
// This could again result in a read of the same pending register and can repeat until
// the load returns. Writes to pending registers are also replayed, so there can be at
// most one oustanding load to any given register. This way, out-of-order loads are
// supported (though loads are implemented to have a fixed latency). A returning load
// reserves an instruction slot at the beginning of the pipeline to reserve a register
// write port. The returning load writes its result and clears the destination
// register's pending flag.
//
// To support L1 and L2 caches, it would be reasonable to delay register write (if
// necessary) to wait for L1 hits (extending the bypass window), and mark "pending"
// for L1 misses. Power could be saved by going idle on replay until load will return
// data.
//
// Bypass:
//
// Register bypass is provided if one instruction's result is not written to the
// register file in time for the next instruction's read. An additional bypass is
// provided for each additional cycle between read and write.
//
// Memory:
//
// The program is stored in its own instruction memory (for simplicity).
// Data memory is separate.
//
// ============
// Mini-CPU ISA
// ============
// A dirt-simple CPU for educational purposes.
// What's interesting about this CPU?
// o It's super small.
// o It's easy to play with an learn from.
// o Instructions are short, kind-of-readable strings, so no assembler is needed.
// They would map directly to a denser (~17-bit) encoding if desired.
// o The only instruction formats are op, load, and store.
// o Branch/Jump: There is no special format for control-flow instructions. Any
// instruction can write the PC (relative or absolute). A conditional branch
// will typically utilize a condition operation that provides a branch target or
// zero. The condition can be predicted as per traditional branch prediction
// (though there is no branch predictor in this example as it stands).
// ISA:
//
// Instructions are 5-character strings: "D=1o2"
//
// =: Appears in every instruction (just for readability).
// D, 2, 1: "a" - "h" for register values;
// "0" - "7" for immediate constants (sources, or "0" for unused dest);
// "P" for absolute dest PC (jump);
// "p" for relative dest PC (branch), PC = PC + 1 + result(signed).
//
// o: operator
// Op: (D = 1 o 2) (Eg: "c=a+b"):
// +, -, *, /: Arithmetic. *, / are unsigned.
// =, !, <, >, [, ]: Compare (D = (1 o r) ? all-1s : 0) (] is >=, [ is <=)
// (On booleans these are XNOR, XOR, !1&2, 1&!2, !1|2, 1|!2)
// &, |: Bitwise
// (Can be used on booleans as well as vectors.)
// (There are no operators for NAND and NOR and unary !.)
// ~: Extended constant (D = {1[2:0], 2[2:0]})
// ,: Combine (D = {1[11:6], 2[5:0]})
// ?: Conditional (D = 2 ? `0 : 1)
// Load (Eg: "c=a{b") (D = [1 + 2] (typically 1 would be an immediate offset):
// {: Load
// Store (Eg: "0=a}b") ([2] = 1):
// }: Store
//
// A full-width immediate load sequence, to load octal 2017 is:
// a=2~0
// b=1~7
// a=a,b
// A typical local conditional branch sequence is:
// a=0-6 // offset
// c=c-1 // decrementing loop counter
// p=a?c // branch by a (to PC+1-6) if c is non-negative (MSB==0)
// ==========
// RISC-V ISA
// ==========
// This design is an incomplete RISC-V implementation.
// Most instructions are characterized. The primary missing pieces are:
// o expressions for executing many instructions
// o ISA extensions
// o byte-level addressing
// The implementation is based on "The RISC-V Instruction Set Manual Vol. I: User-Level ISA," Version 2.2: https://riscv.org/specifications/
// =========
// DUMMY ISA
// =========
// This "ISA" can be selected to produce diagrams of the CPU without the ISA details.
// It is also useful as a starting point and reference for other ISAs, as it illustrates which signals are required.
// =========
// Libraries
// =========
m4_include(['pipeflow_tlv.m4'])
// =============
// Configuration
// =============
// This is where you configure the CPU.
m4_define(['M4_ISA'], RISCV) // MINI, RISCV, DUMMY, etc.
m4_define(['M4_TB'], 1) // 0 to disable testbench and instrumentation code.
// Define the implementation configuration, including pipeline depth and staging.
// Define the following:
// Stages:
// M4_PC_MUX_STAGE: Determining fetch PC.
// M4_FETCH_STAGE: Instruction fetch.
// M4_DECODE_STAGE: Instruction decode.
// M4_REG_RD_STAGE: Register file read.
// M4_EXECUTE_STAGE: Operation execution.
// M4_RESULT_STAGE: Select execution result.
// M4_BRANCH_TARGET_CALC_STAGE: Calculate branch target (generally EXECUTE, but some designs
// might produce offset from EXECUTE, then compute target).
// M4_MEM_WR_STAGE: Memory write.
// M4_REG_WR_STAGE: Register file write.
// Deltas (default to 0):
// M4_DELAY_BRANCH_TARGET_CALC: 1 to delay branch target calculation from its nominal (ISA-specific) value.
// Latencies (default to 0):
// M4_LD_RETURN_ALIGN: Alignment of load return pseudo-instruction into |mem pipeline.
// If |mem stages reflect nominal alignment w/ load instruction, this is the
// nominal load latency.
// Deltas (default to 0):
// M4 EXTRA_PRED_TAKEN_BUBBLE: 0 or 1. 0 aligns PC_MUX with BRANCH_TARGET_CALC.
// M4_EXTRA_JUMP_BUBBLE: 0 or 1. 0 aligns PC_MUX with EXECUTE for jumps.
// M4_EXTRA_BRANCH_BUBBLE: 0 or 1. 0 aligns PC_MUX with EXECUTE for branches.
// M4_EXTRA_REPLAY_BUBBLE: 0 or 1. 0 aligns PC_MUX with EXECUTE for replays.
// M4_BRANCH_PRED: {fallthrough, two_bit, ...}
// M4_DATA_MEM_WORDS: Number of data memory locations.
m4_case(['5-stage'],
['5-stage'], ['
// A reasonable 5-stage pipeline.
m4_defines(
(M4_PC_MUX_STAGE, -1),
(M4_FETCH_STAGE, 0),
(M4_DECODE_STAGE, 1),
(M4_REG_RD_STAGE, 1),
(M4_EXECUTE_STAGE, 2),
(M4_RESULT_STAGE, 2),
(M4_MEM_WR_STAGE, 3),
(M4_REG_WR_STAGE, 3),
(M4_EXTRA_PRED_TAKEN_BUBBLE, 1),
(M4_LD_RETURN_ALIGN, 4))
m4_define(['M4_BRANCH_PRED'], ['two_bit'])
m4_define_hier(M4_DATA_MEM_WORDS, 32)
'],
['1-stage'], ['
// No pipeline
m4_defines(
(M4_PC_MUX_STAGE, -1),
(M4_FETCH_STAGE, 0),
(M4_DECODE_STAGE, 0),
(M4_REG_RD_STAGE, 0),
(M4_EXECUTE_STAGE, 0),
(M4_RESULT_STAGE, 0),
(M4_MEM_WR_STAGE, 0),
(M4_REG_WR_STAGE, 0),
(M4_LD_RETURN_ALIGN, 1))
m4_define(['M4_BRANCH_PRED'], ['fallthrough'])
m4_define_hier(M4_DATA_MEM_WORDS, 32)
'],
['
// Deep pipeline
m4_defines(
(M4_PC_MUX_STAGE, 0),
(M4_FETCH_STAGE, 1),
(M4_DECODE_STAGE, 3),
(M4_REG_RD_STAGE, 4),
(M4_EXECUTE_STAGE, 5),
(M4_RESULT_STAGE, 5),
(M4_MEM_WR_STAGE, 5),
(M4_REG_WR_STAGE, 6),
(M4_EXTRA_PRED_TAKEN_BUBBLE, 1),
(M4_LD_RETURN_ALIGN, 7))
m4_define(['M4_BRANCH_PRED'], ['two_bit'])
m4_define_hier(M4_DATA_MEM_WORDS, 32)
']
)
// --------------------------
// ISA-Specific Configuration
// --------------------------
m4_case(M4_ISA, ['MINI'], ['
// Mini-CPU Configuration:
'], ['RISCV'], ['
// RISC-V Configuration:
// ISA options:
// Currently supported uarch variants:
// RV32I 2.0, w/ no ISA extensions.
// Machine width
m4_define_vector(['M4_WORD'], 32) // 32 or RV32X or 64 for RV64X.
// ISA extensions, 1, or 0 (following M4 boolean convention).
m4_defines(
(['M4_EXT_E'], 1),
(['M4_EXT_I'], 1),
(['M4_EXT_M'], 0),
(['M4_EXT_A'], 0),
(['M4_EXT_F'], 0),
(['M4_EXT_D'], 0),
(['M4_EXT_Q'], 0),
(['M4_EXT_L'], 0),
(['M4_EXT_C'], 0),
(['M4_EXT_B'], 0),
(['M4_EXT_J'], 0),
(['M4_EXT_T'], 0),
(['M4_EXT_P'], 0),
(['M4_EXT_V'], 0),
(['M4_EXT_N'], 0))
'], ['
// Dummy "ISA".
m4_define_hier(M4_DATA_MEM_WORDS, 4) // Override for narrow address.
']
)
// =====Done Defining Configuration=====
// Characterize ISA and apply configuration.
// Characterize the ISA, including:
// M4_NOMINAL_BR_TARGET_CALC_STAGE: An expression that will evaluate to the earliest stage at which the branch target
// can be available.
m4_case(M4_ISA, ['MINI'], ['
// Mini-CPU Characterization:
m4_define(['M4_NOMINAL_BRANCH_TARGET_CALC_STAGE'], ['M4_EXECUTE_STAGE'])
'], ['RISCV'], ['
// RISC-V Characterization:
m4_define(['M4_NOMINAL_BRANCH_TARGET_CALC_STAGE'], ['M4_DECODE_STAGE'])
'], ['DUMMY'], ['
// DUMMY Characterization:
m4_define(['M4_NOMINAL_BRANCH_TARGET_CALC_STAGE'], ['M4_EXECUTE_STAGE'])
']
)
// Supply defaults for extra cycles.
m4_defines(
(M4_DELAY_BRANCH_TARGET_CALC, 0),
(M4_EXTRA_JUMP_BUBBLE, 0),
(M4_EXTRA_BRANCH_BUBBLE, 0),
(M4_EXTRA_PRED_TAKEN_BUBBLE, 0),
(M4_EXTRA_REPLAY_BUBBLE, 0)
)
// Latencies, calculated from latency parameters:
m4_define(M4_REG_BYPASS_STAGES, m4_eval(M4_REG_WR_STAGE - M4_REG_RD_STAGE))
m4_define(M4_BRANCH_TARGET_CALC_STAGE, m4_eval(M4_NOMINAL_BRANCH_TARGET_CALC_STAGE + M4_DELAY_BRANCH_TARGET_CALC))
m4_define(M4_JUMP_BUBBLES, m4_eval(M4_EXECUTE_STAGE - M4_PC_MUX_STAGE + M4_EXTRA_JUMP_BUBBLE))
m4_define(M4_PRED_TAKEN_BUBBLES, m4_eval(M4_BRANCH_TARGET_CALC_STAGE - M4_PC_MUX_STAGE + M4_EXTRA_PRED_TAKEN_BUBBLE))
m4_define(M4_BRANCH_BUBBLES, m4_eval(M4_EXECUTE_STAGE - M4_PC_MUX_STAGE + M4_EXTRA_BRANCH_BUBBLE))
m4_define(M4_REPLAY_LATENCY, m4_eval(M4_EXECUTE_STAGE - M4_PC_MUX_STAGE + 1))
// ========================
// Check Legality of Config
// ========================
// (Not intended to be exhaustive.)
// Check stage order.
m4_define(['m4_stage_order'], ['
m4_ifelse($3, [''], [''], ['
m4_eval(M4_$1_STAGE > M4_$2_STAGE), 1, ['m4_errprint(['M4_$1_STAGE must precede M4_$2_STAGE.'])'],
['m4_stage_order(['m4_shift($*)'])'])
'])
'])
m4_stage_order(PC_MUX, FETCH, DECODE, REG_RD, EXECUTE, RESULT, REG_WR)
m4_stage_order(EXECUTE, MEM_WR)
// Check limit reg bypass
m4_ifelse(m4_eval(M4_REG_BYPASS_STAGES > 3), 1, ['m4_errprint(['Too many stages of register bypass (']M4_REG_BYPASS_STAGES['.'])'])
// ==================
// Default Parameters
// ==================
// These may be overridden by specific ISA.
m4_define(M4_BIG_ENDIAN, ['0'])
// =======================
// ISA-specific Parameters
// =======================
// Macros for ISA-specific code.
m4_define(M4_isa, m4_translit(M4_ISA, ['A-Z'], ['a-z'])) // A lower-case version of M4_ISA.
// For each ISA, define:
// m4_define_vector(['M4_INSTR'], XX) // (or, m4_define_vector_with_fields(...)) Instruction vector.
// m4_define_vector(['M4_ADDR'], XX) // An address.
// m4_define(['M4_BITS_PER_ADDR'], XX) // Each memory address holds XX bits.
// m4_define_vector(['M4_WORD'], XX) // Width of general-purpose registers.
// m4_define_hier(['M4_REGS'], XX) // General-purpose register file.
m4_case(M4_ISA,
['MINI'], ['
m4_define_vector_with_fields(M4_INSTR, 40, DEST_CHAR, 32, EQUALS_CHAR, 24, SRC1_CHAR, 16, OP_CHAR, 8, SRC2_CHAR, 0)
m4_define_vector(M4_ADDR, 12)
m4_define(['M4_BITS_PER_ADDR'], 12) // Each memory address holds 12 bits.
m4_define_vector(M4_WORD, 12)
m4_define_hier(M4_REGS, 8) // (Plural to avoid name conflict w/ SV "reg" keyword.)
m4_define(M4_NUM_INSTRS, 13) // (Must match program exactly.)
'],
['RISCV'], ['
// Definitions matching "The RISC-V Instruction Set Manual Vol. I: User-Level ISA", Version 2.2.
m4_define_vector(['M4_INSTR'], 32)
m4_define_vector(['M4_ADDR'], 30)
m4_define(['M4_BITS_PER_ADDR'], 8) // 8 for byte addressing.
m4_define_vector(['M4_WORD'], 32)
m4_define_hier(['M4_REGS'], 32, 1)
m4_define(M4_NUM_INSTRS, 11) // (Must match program exactly.)
'],
['DUMMY'], ['
m4_define_vector(M4_INSTR, 2)
m4_define_vector(M4_ADDR, 2)
m4_define(['M4_BITS_PER_ADDR'], 2)
m4_define_vector(M4_WORD, 2)
m4_define_hier(M4_REGS, 2)
m4_define(M4_NUM_INSTRS, 2) // (Must match program exactly.)
'])
// Computed ISA uarch Parameters (based on ISA-specific parameters).
m4_define(['M4_ADDRS_PER_WORD'], m4_eval(M4_WORD_CNT / M4_BITS_PER_ADDR))
m4_define(['M4_ADDRS_PER_INSTR'], m4_eval(M4_INSTR_CNT / M4_BITS_PER_ADDR))
m4_define_vector(['M4_PC'], M4_ADDR_HIGH, m4_width(m4_eval(M4_ADDRS_PER_INSTR - 1)))
m4_define_hier(M4_DATA_MEM_ADDRS, m4_eval(M4_DATA_MEM_WORDS_HIGH * M4_ADDRS_PER_WORD)) // Addressable data memory locations.
// ======================
// Code Generation Macros
// ======================
//
m4_case(M4_ISA, ['MINI'], ['
// An out-of-place correction for the fact that in Mini-CPU, instruction
// addresses are to different memory than data, and the memories have different widths.
m4_define_vector(['M4_PC'], 10, 0)
'], ['RISCV'], ['
// For each op5 value, we associate an instruction type.
// TODO:
// We construct M4_OP5_XXXXX_TYPE, and verify each instruction against that.
// Instruction fields are constructed and valid based on op5.
//...
// TO UPDATE:
// We construct localparam INSTR_TYPE_X_MASK as a mask, one bit per op5 indicating whether the op5 is of the type.
// Instantiated recursively for each instruction type.
// Initializes m4_instr_type_X_mask_expr which will build up a mask, one bit per op5.
m4_define(['m4_instr_types'],
['m4_ifelse(['$1'], [''], [''],
['m4_define(['m4_instr_type_$1_mask_expr'], ['0'])m4_instr_types(m4_shift($@))'])'])
// Instantiated recursively for each instruction type in \SV_plus context after characterizing each type.
// Declares localparam INSTR_TYPE_X_MASK as m4_instr_type_X_mask_expr.
m4_define(['m4_instr_types_sv'],
['m4_ifelse(['$1'], [''], [''],
['localparam INSTR_TYPE_$1_MASK = m4_instr_type_$1_mask_expr; m4_instr_types_sv(m4_shift($@))'])'])
// Instantiated recursively for each instruction type in \SV_plus context to decode instruction type.
// Creates "assign $$is_x_type = INSTR_TYPE_X_MASK[$raw_opcode[6:2]];" for each type.
m4_define(['m4_types_decode'],
['m4_ifelse(['$1'], [''], [''],
['['assign $$is_']m4_translit(['$1'], ['A-Z'], ['a-z'])['_type = INSTR_TYPE_$1_MASK[$raw_opcode[6:2]]; ']m4_types_decode(m4_shift($@))'])'])
// Instantiated for each op5 in \SV_plus context.
m4_define(['m4_op5'],
['m4_define(['M4_OP5_$1_TYPE'], $2)['localparam [4:0] OP5_$3 = 5'b$1;']m4_define(['m4_instr_type_$2_mask_expr'], m4_quote(m4_instr_type_$2_mask_expr)[' | (1 << 5'b$1)'])'])
// Return 1 if the given instruction is supported, [''] otherwise.
m4_define(['m4_instr_supported'],
['m4_ifelse(M4_EXT_$3, 1, ['m4_ifelse(M4_WORD_CNT, ['$2'], 1,
[''])'],
[''])'])
// Instantiated (in \SV_plus context) for each instruction.
m4_define_hide(['m4_instr'],
['// check instr type
m4_ifelse(M4_OP5_$4_TYPE, $1, [''],
['m4_errprint(['Instruction ']m4_argn($#, $@)[''s type ($1) is inconsistant with its op5 code ($4) of type ']M4_OP5_$4_TYPE[' on line ']m4___line__[' of file ']m4_FILE.m4_new_line)'])
// if instrs extension is supported and instr is for the right machine width, "
m4_ifelse(m4_instr_supported($@), 1, ['m4_show(['localparam [6:0] ']']m4_argn($#, $@)['['_INSTR_OPCODE = 7'b$4['']11;m4_instr$1(m4_shift($@))'])'],
[''])'])
m4_define(['m4_instr_func'],
['m4_instr_decode_expr($5, $3, $4, $6)[' localparam [2:0] $5_INSTR_FUNCT3 = 3'b']$4;'])
m4_define(['m4_instr_no_func'],
['m4_instr_decode_expr($4, $3, ['xxx'])'])
// Macros to create, for each instruction (xxx):
// o instructiton decode: $is_xxx_instr = ...; ...
// o result combining expr.: ({32{$is_xxx_instr}} & $xxx_rslt) | ...
// o $illegal computation: && ! $is_xxx_instr ...
// o $mnemonic: $is_xxx_instr ? "XXX" : ...
m4_define(['m4_decode_expr'], [''])
m4_define(['m4_rslt_mux_expr'], [''])
m4_define(['m4_illegal_instr_expr'], [''])
m4_define(['m4_mnemonic_expr'], [''])
m4_define_hide(
['m4_instr_decode_expr'],
['m4_define(
['m4_decode_expr'],
m4_dquote(m4_decode_expr['$is_']m4_translit($1, ['A-Z'], ['a-z'])['_instr = ($op5_funct3 ==? 8'b$2_$3);m4_plus_new_line ']))
m4_ifelse(['$4'], ['no_dest'],
[''],
['m4_define(
['m4_rslt_mux_expr'],
m4_dquote(m4_rslt_mux_expr[' |']['m4_plus_new_line ({']M4_WORD_CNT['{$is_']m4_translit($1, ['A-Z'], ['a-z'])['_instr}} & $']m4_translit($1, ['A-Z'], ['a-z'])['_rslt)']))'])
m4_define(
['m4_illegal_instr_expr'],
m4_dquote(m4_illegal_instr_expr[' && ! $is_']m4_translit($1, ['A-Z'], ['a-z'])['_instr']))
m4_define(
['m4_mnemonic_expr'],
m4_dquote(m4_mnemonic_expr['$is_']m4_translit($1, ['A-Z'], ['a-z'])['_instr ? "$1']m4_substr([' '], m4_len(['$1']))['" : ']))'])
// Unique to each instruction type.
// This includes assembler macros as follows. Fields are ordered rd, rs1, rs2, imm:
// I: m4_asm_ADDI(r4, r1, 0),
// R: m4_asm_ADD(r4, r1, r2), // optional 4th arg for funct7
// S: m4_asm_SW(r1, r2, 100), // Store r13 into [r10] + 4
// B: m4_asm_BLT(r1, r2, 1000), // Branch if r1 < r2 to PC + 13'b1000 (where lsb = 0)
m4_define(['m4_instrI'], ['m4_instr_func($@)m4_define(['m4_asm_$5'], ['m4_asm_instr_str(I, ['$5'], $']['@){12'b']m4_arg(3)[', m4_asm_reg(']m4_arg(2)['), $5_INSTR_FUNCT3, m4_asm_reg(']m4_arg(1)['), $5_INSTR_OPCODE}'])'])
m4_define(['m4_instrR'], ['m4_instr_func($@)m4_define(['m4_asm_$5'], ['m4_asm_instr_str(R, ['$5'], $']['@){7'b['']m4_ifelse(']m4_arg(4)[', [''], 0, ']m4_arg(4)['), m4_asm_reg(']m4_arg(3)['), m4_asm_reg(']m4_arg(2)['), $5_INSTR_FUNCT3, m4_asm_reg(']m4_arg(1)['), $5_INSTR_OPCODE}'])'])
m4_define(['m4_instrRI'], ['m4_instr_func($@)'])
m4_define(['m4_instrR4'], ['m4_instr_func($@)'])
m4_define(['m4_instrS'], ['m4_instr_func($@, ['no_dest'])m4_define(['m4_asm_$5'], ['m4_asm_instr_str(S, ['$5'], $']['@){m4_asm_imm_field(']m4_arg(3)[', 12, 11, 5), m4_asm_reg(']m4_arg(2)['), m4_asm_reg(']m4_arg(1)['), $5_INSTR_FUNCT3, m4_asm_imm_field(']m4_arg(3)[', 12, 4, 0), $5_INSTR_OPCODE}'])'])
m4_define(['m4_instrB'], ['m4_instr_func($@, ['no_dest'])m4_define(['m4_asm_$5'], ['m4_asm_instr_str(B, ['$5'], $']['@){m4_asm_imm_field(']m4_arg(3)[', 13, 12, 12), m4_asm_imm_field(']m4_arg(3)[', 13, 10, 5), m4_asm_reg(']m4_arg(2)['), m4_asm_reg(']m4_arg(1)['), $5_INSTR_FUNCT3, m4_asm_imm_field(']m4_arg(3)[', 13, 4, 1), m4_asm_imm_field(']m4_arg(3)[', 13, 11, 11), $5_INSTR_OPCODE}'])'])
m4_define(['m4_instrJ'], ['m4_instr_no_func($@)'])
m4_define(['m4_instrU'], ['m4_instr_no_func($@)'])
m4_define(['m4_instr_'], ['m4_instr_no_func($@)'])
// For each instruction type.
// Declare localparam[31:0] INSTR_TYPE_X_MASK, initialized to 0 that will be given a 1 bit for each op5 value of its type.
m4_define(['m4_instr_types_args'], ['I, R, RI, R4, S, B, J, U, _'])
m4_instr_types(m4_instr_types_args)
// Instruction fields (User ISA Manual 2.2, Fig. 2.2)
m4_define_fields(['M4_INSTR'], 32, FUNCT7, 25, RS2, 20, RS1, 15, FUNCT3, 12, RD, 7, OPCODE, 0)
//=========
// Specifically for assembler.
// An 20-bit immediate binary zero string.
m4_define(['m4_asm_imm_zero'], ['00000000000000000000'])
// Zero-extend to n bits. E.g. m4_asm_zero_ext(1001, 7) => 0001001
m4_define(['m4_asm_zero_ext'], ['m4_substr(m4_asm_imm_zero, 0, m4_eval($2 - m4_len($1)))$1'])
// Extract bits from a binary immediate value.
// m4_asm_imm_field(binary-imm, imm-length, max-bit, min-bit)
// E.g. m4_asm_imm_field(101011, 17, 7, 3) => 5'b00101
m4_define(['m4_asm_imm_field'], ['m4_eval($3 - $4 + 1)'b['']m4_substr(m4_asm_zero_ext($1, $2), m4_eval($2 - $3 - 1), m4_eval($3 - $4 + 1))'])
// Register operand.
m4_define(['m4_asm_reg'], ['m4_ifelse(m4_substr(['$1'], 0, 1), ['r'], [''], ['m4_errprint(['$1 passed to register field.'])'])5'd['']m4_substr(['$1'], 1)'])
// Specific asm instruction macros, in cases where the type-based defaults are insufficient.
m4_define(['m4_asm_ADD'], ['m4_asm_ADD_SUB($1, $2, $3)'])
m4_define(['m4_asm_SUB'], ['m4_asm_ADD_SUB($1, $2, $3, 0100000)'])
// For debug, a string for an asm instruction.
m4_define(['m4_asm_mem_expr'], [''])
// m4_asm_instr_str(<type>, <mnemonic>, <m4_asm-args>)
m4_define(['m4_asm_instr_str'], ['m4_pushdef(['m4_str'], ['($1) $2 m4_shift(m4_shift($@))'])m4_define(['m4_asm_mem_expr'],
m4_dquote(m4_asm_mem_expr[' "']m4_str['']m4_substr([' '], m4_len(m4_quote(m4_str)))['", ']))m4_popdef(['m4_str'])'])
//=========
'], ['DUMMY'], ['
'])
'])
\SV
/* verilator lint_on WIDTH */ // Let's be strict about bit widths.
//============================//
// //
// MINI-CPU //
// //
//============================//
\TLV mini_cnt10_prog()
\SV_plus
// /=====================\
// | Count to 10 Program |
// \=====================/
//
// (The program I wrote, within the model I wrote, within the program I wrote.)
// Add 1,2,3,...,10 (in that order).
// Store incremental results in memory locations 0..9. (1, 3, 6, 10, ...)
//
// Regs:
// b: cnt
// c: ten
// d: out
// e: tmp
// f: offset
// g: store addr
assign instrs = '{
"g=0~0", // store_addr = 0
"b=0~1", // cnt = 1
"c=1~2", // ten = 10
"d=0~0", // out = 0
"f=0-6", // offset = -6
"d=d+b", // -> out += cnt
"0=d}g", // store out at store_addr
"b=b+1", // cnt ++
"g=g+1", // store_addr++
"e=c-b", // tmp = 10 - cnt
"p=f?e", // ^- branch back if tmp >= 0
"e=0{c", // load the final value into tmp
"P=0-1" // TERMINATE by jumping to -1
};
\TLV mini_gen()
// No M4-generated code for mini.
// Decode logic for Mini-CPU.
// Context: within pipestage
// Inputs:
// $raw[11:0]
// Outputs:
// $ld
// $st
// $illegal
// $conditional_branch
// ...
\TLV mini_decode()
// Characters
$dest_char[7:0] = $raw[39:32];
/src[2:1]
$char[7:0] = (#src == 1) ? /instr$raw[M4_INSTR_SRC1_CHAR_RANGE] : /instr$raw[M4_INSTR_SRC2_CHAR_RANGE];
$op_char[7:0] = $raw[15:8];
// Dest
$dest_is_reg = ($dest_char >= "a" && $dest_char <= "h") || $returning_ld;
$dest_reg_valid = $dest_is_reg;
$fetch_instr_dest_reg[7:0] = $dest_char - "a";
$dest_reg[2:0] = $returning_ld ? $returning_ld_dest_reg : $fetch_instr_dest_reg[2:0];
$jump = $dest_char == "P";
$branch = $dest_char == "p";
$no_dest = $dest_char == "0";
$write_pc = $jump || $branch;
$dest_valid = $write_pc || $dest_is_reg;
$illegal_dest = !($dest_is_reg ||
(($branch || $jump || $no_dest) && ! $ld)); // Load must have reg dest.
/src[*]
// Src1
$is_reg = $char >= "a" && $char <= "h";
$reg_tmp[7:0] = $char - "a";
$reg[2:0] = $reg_tmp[2:0];
$is_imm = $char >= "0" && $char < "8";
$imm_tmp[7:0] = $char - "0";
$imm_value[11:0] = {9'b0, $imm_tmp[2:0]};
$illegal = !($is_reg || $is_imm);
// Opcode:
/op
$char[7:0] = /instr$op_char;
// Arithmetic
$add = $char == "+";
$sub = $char == "-";
$mul = $char == "*";
$div = $char == "/";
// Compare and bool (w/ 1 bit rslt)
$eq = $char == "=";
$ne = $char == "!";
$lt = $char == "<";
$gt = $char == ">";
$le = $char == "[";
$ge = $char == "]";
$and = $char == "&";
$or = $char == "|";
// Wide Immediate
$wide_imm = $char == "~";
$combine = $char == ",";
// Conditional
$conditional = $char == "?";
// Memory
$ld = $char == "{";
$st = $char == "}";
// Opcode classes:
$arith = $add || $sub || $mul || $div;
$compare = $eq || $ne || $lt || $gt || $le || $ge;
$bitwise = $and || $or;
$full = $arith || $bitwise || $wide_imm || $combine || $conditional;
//$op3 = $compare || $full;
$mem = $ld || $st;
$illegal = !($compare || $full || $mem);
$op_compare = /op$compare;
$op_full = /op$full;
$ld = /op$ld;
$spec_ld = $ld;
$st = /op$st;
$illegal = $illegal_dest || (| /src[*]$illegal) || /op$illegal;
// Branch instructions with a condition (that might be worth predicting).
//$branch_predict = $branch && /op$conditional;
$conditional_branch = $branch; // All branches (any instruction with "p" dest) is conditional (where condition is that result != 0).
// Execution unit logic for RISC-V.
// Context: pipeline
\TLV mini_exe(@_exe_stage, @_rslt_stage)
@M4_REG_RD_STAGE
/src[*]
$valid = /instr$valid_decode && ($is_reg || $is_imm);
?$valid
$value[M4_WORD_RANGE] = $is_reg ? $reg_value :
$imm_value;
// Note that some result muxing is performed in @_exe_stage, and the rest in @_rslt_stage.
@_exe_stage
$valid_exe = $valid_decode; // Execute if we decoded.
?$op_compare
$compare_rslt =
/op$eq ? /src[1]$value == /src[2]$value :
/op$ne ? /src[1]$value != /src[2]$value :
/op$lt ? /src[1]$value < /src[2]$value :
/op$gt ? /src[1]$value > /src[2]$value :
/op$le ? /src[1]$value <= /src[2]$value :
/op$ge ? /src[1]$value >= /src[2]$value :
1'b0;
?$op_full
$op_full_rslt[11:0] =
/op$add ? /src[1]$value + /src[2]$value :
/op$sub ? /src[1]$value - /src[2]$value :
/op$mul ? /src[1]$value * /src[2]$value :
/op$div ? /src[1]$value * /src[2]$value :
/op$and ? /src[1]$value & /src[2]$value :
/op$or ? /src[1]$value | /src[2]$value :
/op$wide_imm ? {6'b0, /src[1]$value[2:0], /src[2]$value[2:0]} :
/op$combine ? {/src[1]$value[5:0], /src[2]$value[5:0]} :
/op$conditional ? (/src[2]$value[11] ? 12'b0 : /src[1]$value) :
12'b0;
?$valid_st
$st_value[M4_WORD_RANGE] = /src[1]$value;
?$valid_ld_st
$addr[M4_ADDR_RANGE] = $ld ? (/src[1]$value + /src[2]$value) : /src[2]$value;
// Always predict taken; mispredict if jump or unconditioned branch or
// conditioned branch with positive condition.
?$valid_branch
$taken = $rslt != 12'b0;
@_rslt_stage
?$dest_valid
$rslt[11:0] =
$returning_ld ? $returning_ld_data :
$st ? /src[1]$value :
$op_full ? $op_full_rslt :
$op_compare ? {12{$compare_rslt}} :
12'b0;
// Jump (Dest = "P") and Branch (Dest = "p") Targets.
?$valid_jump
$jump_target[M4_PC_RANGE] = $rslt[M4_PC_RANGE];
@M4_BRANCH_TARGET_CALC_STAGE
?$branch
$branch_target[M4_PC_RANGE] = $Pc + M4_PC_CNT'b1 + $rslt[M4_PC_RANGE];
//============================//
// //
// RISC-V //
// //
//============================//
\TLV riscv_cnt10_prog()
\SV_plus
logic [40*8-1:0] instr_strs [0:M4_NUM_INSTRS];
// /=====================\
// | Count to 10 Program |
// \=====================/
//
// Add 1,2,3,...,10 (in that order).
// Store incremental results in memory locations 0..9. (1, 3, 6, 10, ...)
//
// Regs:
// 1: cnt
// 2: ten
// 3: out
// 4: tmp
// 5: offset
// 6: store addr
assign instrs = '{
m4_asm_ORI(r6, r0, 0), // store_addr = 0
m4_asm_ORI(r1, r0, 1), // cnt = 1
m4_asm_ORI(r2, r0, 1010), // ten = 10
m4_asm_ORI(r3, r0, 0), // out = 0
m4_asm_ADD(r3, r1, r3), // -> out += cnt
m4_asm_SW(r6, r3, 0), // store out at store_addr
m4_asm_ADDI(r1, r1, 1), // cnt ++
m4_asm_ADDI(r6, r6, 100), // store_addr++
m4_asm_BLT(r1, r2, 1111111110000), // ^- branch back if cnt < 10
m4_asm_LW(r4, r6, 0), // load the final value into tmp
m4_asm_BGE(r1, r2, 1111111010100) // TERMINATE by branching to -1
};
assign instr_strs = '{m4_asm_mem_expr "END "};
// M4-generated code.
\TLV riscv_gen()
// v---------------------
// Instruction characterization
// M4 ugliness for instruction characterization.
// For each opcode[6:2]
// (User ISA Manual 2.2, Table 19.1)
// Associate opcode[6:2] ([1:0] are 2'b11) with mnemonic and instruction type.
// Instruction type is not in the table, but there seems to be a single instruction type for each of these,
// so that is mapped here as well.
// op5(bits, type, mnemonic)
\SV_plus
m4_op5(00000, I, LOAD)
m4_op5(00001, I, LOAD_FP)
m4_op5(00010, _, CUSTOM_0)
m4_op5(00011, _, MISC_MEM)
m4_op5(00100, I, OP_IMM)
m4_op5(00101, U, AUIPC)
m4_op5(00110, I, OP_IMM_32)
m4_op5(00111, _, 48B1)
m4_op5(01000, S, STORE)
m4_op5(01001, S, STORE_FP)
m4_op5(01010, _, CUSTOM_1)
m4_op5(01011, RI, AMO) // (R-type, but rs2 = const for some, based on funct7 which doesn't exist for I-type?? R-type w/ ignored R2?)
m4_op5(01100, R, OP)
m4_op5(01101, U, LUI)
m4_op5(01110, R, OP_32)
m4_op5(01111, _, 64B)
m4_op5(10000, R4, MADD)
m4_op5(10001, R4, MSUB)
m4_op5(10010, R4, NMSUB)
m4_op5(10011, R4, NMADD)
m4_op5(10100, RI, OP_FP) // (R-type, but rs2 = const for some, based on funct7 which doesn't exist for I-type?? R-type w/ ignored R2?)
m4_op5(10101, _, RESERVED_1)
m4_op5(10110, _, CUSTOM_2_RV128)
m4_op5(10111, _, 48B2)
m4_op5(11000, B, BRANCH)
m4_op5(11001, I, JALR)
m4_op5(11010, _, RESERVED_2)
m4_op5(11011, J, JAL)
m4_op5(11100, _, SYSTEM)
m4_op5(11101, _, RESERVED_3)
m4_op5(11110, _, CUSTOM_3_RV128)
m4_op5(11111, _, 80B)
\SV_plus
// Not sure these are ever used.
m4_instr_types_sv(m4_instr_types_args)
\SV_plus
// Instruction characterization.
// (User ISA Manual 2.2, Table 19.2)
// instr(type, // (this is simply verified vs. op5)
// | bit-width,
// | | extension,
// | | | opcode[6:2], // (aka op5)
// | | | | func3, // (if applicable)
// | | | | | mnemonic)
m4_instr(U, 32, I, 01101, LUI)
m4_instr(U, 32, I, 00101, AUIPC)
m4_instr(J, 32, I, 11011, JAL)
m4_instr(I, 32, I, 11001, 000, JALR)
m4_instr(B, 32, I, 11000, 000, BEQ)
m4_instr(B, 32, I, 11000, 001, BNE)
m4_instr(B, 32, I, 11000, 100, BLT)
m4_instr(B, 32, I, 11000, 101, BGE)
m4_instr(B, 32, I, 11000, 110, BLTU)
m4_instr(B, 32, I, 11000, 111, BGEU)
m4_instr(I, 32, I, 00000, 000, LB)
m4_instr(I, 32, I, 00000, 001, LH)
m4_instr(I, 32, I, 00000, 010, LW)
m4_instr(I, 32, I, 00000, 100, LBU)
m4_instr(I, 32, I, 00000, 101, LHU)
m4_instr(S, 32, I, 01000, 000, SB)
m4_instr(S, 32, I, 01000, 001, SH)
m4_instr(S, 32, I, 01000, 010, SW)
m4_instr(I, 32, I, 00100, 000, ADDI)
m4_instr(I, 32, I, 00100, 010, SLTI)
m4_instr(I, 32, I, 00100, 011, SLTIU)
m4_instr(I, 32, I, 00100, 100, XORI)
m4_instr(I, 32, I, 00100, 110, ORI)
m4_instr(I, 32, I, 00100, 111, ANDI)
m4_instr(I, 32, I, 00100, 001, SLLI)
m4_instr(I, 32, I, 00100, 101, SRLI_SRAI) // Two instructions distinguished by an immediate bit, treated as a single instruction.
m4_instr(R, 32, I, 01100, 000, ADD_SUB) // Treated as a single instruction.
m4_instr(R, 32, I, 01100, 001, SLL)
m4_instr(R, 32, I, 01100, 010, SLT)
m4_instr(R, 32, I, 01100, 011, SLTU)
m4_instr(R, 32, I, 01100, 100, XOR)
m4_instr(R, 32, I, 01100, 101, SRL_SRA) // Treated as a single instruction.
m4_instr(R, 32, I, 01100, 110, OR)
m4_instr(R, 32, I, 01100, 111, AND)
//m4_instr(_, 32, I, 00011, 000, FENCE)
//m4_instr(_, 32, I, 00011, 001, FENCE_I)
//m4_instr(_, 32, I, 11100, 000, ECALL_EBREAK) // Two instructions distinguished by an immediate bit, treated as a single instruction.
//m4_instr(_, 32, I, 11100, 001, CSRRW)
//m4_instr(_, 32, I, 11100, 010, CSRRS)
//m4_instr(_, 32, I, 11100, 011, CSRRC)
//m4_instr(_, 32, I, 11100, 101, CSRRWI)
//m4_instr(_, 32, I, 11100, 110, CSRRSI)
//m4_instr(_, 32, I, 11100, 111, CSRRCI)
m4_instr(I, 64, I, 00000, 110, LWU)
m4_instr(I, 64, I, 00000, 011, LD)
m4_instr(S, 64, I, 01000, 011, SD)
m4_instr(I, 64, I, 00100, 001, SLLI)
m4_instr(I, 64, I, 00100, 101, SRLI_SRAI) // Two instructions distinguished by an immediate bit, treated as a single instruction.
m4_instr(I, 64, I, 00110, 000, ADDIW)
m4_instr(I, 64, I, 00110, 001, SLLIW)
m4_instr(I, 64, I, 00110, 101, SRLIW_SRAIW) // Two instructions distinguished by an immediate bit, treated as a single instruction.
m4_instr(R, 64, I, 01110, 000, ADDW_SUBW) // Two instructions distinguished by an immediate bit, treated as a single instruction.
m4_instr(R, 64, I, 01110, 001, SLLW)
m4_instr(R, 64, I, 01110, 101, SRLW_SRAW) // Two instructions distinguished by an immediate bit, treated as a single instruction.
m4_instr(R, 32, M, 01100, 000, MUL)
m4_instr(R, 32, M, 01100, 001, MULH)
m4_instr(R, 32, M, 01100, 010, MULHSU)
m4_instr(R, 32, M, 01100, 011, MULHU)
m4_instr(R, 32, M, 01100, 100, DIV)
m4_instr(R, 32, M, 01100, 101, DIVU)
m4_instr(R, 32, M, 01100, 110, REM)
m4_instr(R, 32, M, 01100, 111, REMU)
m4_instr(R, 64, M, 01110, 000, MULW)
m4_instr(R, 64, M, 01110, 100, DIVW)
m4_instr(R, 64, M, 01110, 101, DIVUW)
m4_instr(R, 64, M, 01110, 110, REMW)
m4_instr(R, 64, M, 01110, 111, REMUW)
// RV32A and RV64A
// NOT IMPLEMENTED. These are distinct in the func7 field.
// RV32F and RV64F
// NOT IMPLEMENTED.
// RV32D and RV64D
// NOT IMPLEMENTED.
// ^---------------------
// These are expanded in a separate TLV macro because multi-line expansion is a no-no for line tracking.
// This keeps the implications contained.
\TLV riscv_decode_expr()
m4_echo(m4_decode_expr)
\TLV riscv_rslt_mux_expr()
$rslt[M4_WORD_RANGE] =
$returning_ld ? $returning_ld_data :
M4_WORD_CNT'b0['']m4_echo(m4_rslt_mux_expr);
\TLV riscv_decode()
?$valid_decode
// =================================
// Extract fields of $raw (instruction) into $raw_<field>[x:0].
m4_into_fields(['M4_INSTR'], ['$raw'])
`BOGUS_USE($raw_funct7) // Delete once its used.
// Extract immediate fields into type-specific signals.
// (User ISA Manual 2.2, Fig. 2.4)
$raw_i_imm[31:0] = {{21{$raw[31]}}, $raw[30:20]};
$raw_s_imm[31:0] = {{21{$raw[31]}}, $raw[30:25], $raw[11:7]};
$raw_b_imm[31:0] = {{20{$raw[31]}}, $raw[7], $raw[30:25], $raw[11:8], 1'b0};
$raw_u_imm[31:0] = {$raw[31:12], {12{1'b0}}};
$raw_j_imm[31:0] = {{12{$raw[31]}}, $raw[19:12], $raw[20], $raw[30:21], 1'b0};
// Extract other type/instruction-specific fields.
$raw_shamt[6:0] = $raw[26:20];
$raw_aq = $raw[26];
$raw_rl = $raw[25];
$raw_rs3[4:0] = $raw[31:27];
$raw_rm[2:0] = $raw_funct3;
`BOGUS_USE($raw_shamt $raw_aq $raw_rl $raw_rs3 $raw_rm) // Avoid "unused" messages. Remove these as they become used.