-
Notifications
You must be signed in to change notification settings - Fork 157
/
zipcore.v
6122 lines (5445 loc) · 156 KB
/
zipcore.v
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
////////////////////////////////////////////////////////////////////////////////
//
// Filename: zipcore.v
// {{{
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core
//
// Purpose:
//
// Creator: Dan Gisselquist, Ph.D.
// Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
// }}}
// Copyright (C) 2015-2024, Gisselquist Technology, LLC
// {{{
// This program is free software (firmware): you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
// target there if the PDF file isn't present.) If not, see
// <http://www.gnu.org/licenses/> for a copy.
// }}}
// License: GPL, v3, as defined and found on www.gnu.org,
// {{{
// http://www.gnu.org/licenses/gpl.html
//
////////////////////////////////////////////////////////////////////////////////
//
`default_nettype none
// }}}
module zipcore #(
// {{{
parameter ADDRESS_WIDTH=30, // 32-b word addr width
parameter [31:0] RESET_ADDRESS=32'h010_0000,
parameter OPT_MPY = 0,
parameter [0:0] OPT_SHIFTS = 1,
parameter [0:0] OPT_DIV = 1,
parameter [0:0] IMPLEMENT_FPU = 0,
parameter [0:0] OPT_EARLY_BRANCHING = 1,
parameter [0:0] OPT_CIS = 1'b1,
parameter [0:0] OPT_SIM = 1'b0,
parameter [0:0] OPT_DISTRIBUTED_REGS = 1'b1,
parameter [0:0] OPT_PIPELINED = 1'b1,
parameter [0:0] OPT_PIPELINED_BUS_ACCESS = (OPT_PIPELINED),
parameter [0:0] OPT_LOCK=1,
parameter [0:0] OPT_DCACHE = 1,
parameter [0:0] OPT_USERMODE = 1'b1,
parameter [0:0] OPT_LOWPOWER = 1'b0,
parameter [0:0] OPT_CLKGATE = 1'b1,
parameter [0:0] OPT_START_HALTED = 1'b1,
parameter [0:0] OPT_DBGPORT = 1'b1,
parameter [0:0] OPT_TRACE_PORT = 1'b0,
parameter [0:0] OPT_PROFILER = 1'b0,
localparam AW=ADDRESS_WIDTH
`ifdef FORMAL
, parameter F_LGDEPTH=4
`endif
// }}}
) (
// {{{
input wire i_clk, i_reset, i_interrupt,
output wire o_clken,
// Debug interface
// {{{
input wire i_halt, i_clear_cache,
input wire [4:0] i_dbg_wreg,
input wire i_dbg_we,
input wire [31:0] i_dbg_data,
input wire [4:0] i_dbg_rreg,
//
output wire o_dbg_stall,
output wire [31:0] o_dbg_reg,
output reg [2:0] o_dbg_cc,
output wire o_break,
// }}}
// Instruction fetch interface
// {{{
output wire o_pf_new_pc,
output wire o_clear_icache,
output wire o_pf_ready,
output wire [(AW+1):0] o_pf_request_address,
input wire i_pf_valid, i_pf_illegal,
input wire [31:0] i_pf_instruction,
input wire [(AW+1):0] i_pf_instruction_pc,
// }}}
// Memory unit interface
// {{{
output wire o_clear_dcache,
output wire o_mem_ce,
output wire o_bus_lock,
output wire [2:0] o_mem_op,
output wire [31:0] o_mem_addr,
output wire [31:0] o_mem_data,
output wire [AW+1:0] o_mem_lock_pc,
output wire [4:0] o_mem_reg,
input wire i_mem_busy, i_mem_rdbusy,
i_mem_pipe_stalled, i_mem_valid,
input wire i_bus_err,
input wire [4:0] i_mem_wreg,
input wire [31:0] i_mem_result,
// }}}
// Accounting outputs ... to help us count stalls and usage
// {{{
output wire o_op_stall,
output wire o_pf_stall,
output wire o_i_count,
// }}}
// Debug data for on-line/live tracing
// {{{
output wire [31:0] o_debug,
//}}}
// (Optional) Profiler data
// {{{
output wire o_prof_stb,
output wire [AW+1:0] o_prof_addr,
output wire [31:0] o_prof_ticks
// }}}
// }}}
);
// Local parameter definitions
// {{{
// Verilator lint_off UNUSED
localparam [0:0] OPT_MEMPIPE = OPT_PIPELINED_BUS_ACCESS;
localparam [(AW-1):0] RESET_BUS_ADDRESS = RESET_ADDRESS[AW+1:2];
localparam [3:0] CPU_CC_REG = 4'he,
CPU_PC_REG = 4'hf;
localparam [3:0] CPU_SUB_OP = 4'h0,// also a compare instruction
CPU_AND_OP = 4'h1,// also a test instruction
CPU_BREV_OP= 4'h8,
CPU_MOV_OP = 4'hd;
localparam CPU_CLRDCACHE_BIT=15, // Set to clr D-cache,auto clears
CPU_CLRICACHE_BIT=14, // Set to clr I-cache,auto clears
CPU_PHASE_BIT = 13, // Set on last half of a CIS
CPU_FPUERR_BIT = 12, // Set on floating point error
CPU_DIVERR_BIT = 11, // Set on divide error
CPU_BUSERR_BIT = 10, // Set on bus error
CPU_TRAP_BIT = 9, // User TRAP has taken place
CPU_ILL_BIT = 8, // Illegal instruction
CPU_BREAK_BIT = 7,
CPU_STEP_BIT = 6, // Will step one (or two CIS) ins
CPU_GIE_BIT = 5,
CPU_SLEEP_BIT = 4;
// Verilator lint_on UNUSED
// }}}
// Register declarations
// {{{
// The distributed RAM style comment is necessary on the
// SPARTAN6 with XST to prevent XST from oversimplifying the register
// set and in the process ruining everything else. It basically
// optimizes logic away, to where it no longer works. The logic
// as described herein will work, this just makes sure XST implements
// that logic.
//
// (* ram_style = "distributed" *)
reg [31:0] regset [0:(OPT_USERMODE)? 31:15];
// Condition codes
// (BUS, TRAP,ILL,BREAKEN,STEP,GIE,SLEEP ), V, N, C, Z
reg [3:0] flags, iflags;
wire [15:0] w_uflags, w_iflags;
reg break_en, user_step, sleep, r_halted;
wire break_pending, trap, gie, ubreak, pending_interrupt,
stepped;
wire step;
wire ill_err_u;
reg ill_err_i;
reg ibus_err_flag;
wire ubus_err_flag;
wire idiv_err_flag, udiv_err_flag;
// Verilator coverage_off
wire ifpu_err_flag, ufpu_err_flag;
// Verilator coverage_on
wire ihalt_phase, uhalt_phase;
// The master chip enable
wire master_ce, master_stall;
//
//
// PIPELINE STAGE #1 :: Prefetch
// Variable declarations
//
// {{{
reg [(AW+1):0] pf_pc;
reg new_pc;
wire clear_pipeline;
reg dcd_stalled;
// wire pf_cyc, pf_stb, pf_we, pf_stall, pf_ack, pf_err;
// wire [(AW-1):0] pf_addr;
// wire pf_valid, pf_gie, pf_illegal;
wire pf_gie;
`ifdef FORMAL
wire [31:0] f_dcd_insn_word;
wire f_dcd_insn_gie;
wire f_dcd_insn_is_pipeable;
reg [31:0] f_op_insn_word;
reg [31:0] f_alu_insn_word;
`endif
assign clear_pipeline = new_pc;
// }}}
//
// PIPELINE STAGE #2 :: Instruction Decode
// Variable declarations
//
//
// {{{
wire [3:0] dcd_opn;
wire dcd_ce, dcd_phase;
wire [4:0] dcd_A, dcd_B, dcd_R, dcd_preA, dcd_preB;
wire dcd_Acc, dcd_Bcc, dcd_Apc, dcd_Bpc, dcd_Rcc, dcd_Rpc;
wire [3:0] dcd_F;
wire dcd_wR, dcd_rA, dcd_rB,
dcd_ALU, dcd_M, dcd_DIV, dcd_FP,
dcd_wF, dcd_gie, dcd_break, dcd_lock,
dcd_pipe, dcd_ljmp;
wire dcd_valid;
wire [AW+1:0] dcd_pc /* verilator public_flat */;
wire [31:0] dcd_I;
wire dcd_zI; // true if dcd_I == 0
wire dcd_A_stall, dcd_B_stall, dcd_F_stall;
wire dcd_illegal;
wire dcd_early_branch, dcd_early_branch_stb;
wire [(AW+1):0] dcd_branch_pc;
wire dcd_sim;
wire [22:0] dcd_sim_immv;
wire prelock_stall, last_lock_insn;
wire cc_invalid_for_dcd;
wire pending_sreg_write;
// }}}
//
//
// PIPELINE STAGE #3 :: Read Operands
// Variable declarations
//
//
//
// {{{
// Now, let's read our operands
reg op_valid /* verilator public_flat */,
op_valid_mem, op_valid_alu;
reg op_valid_div, op_valid_fpu;
wire op_stall;
wire [3:0] op_opn;
wire [4:0] op_R;
reg op_Rcc;
wire [4:0] op_Aid, op_Bid;
wire op_rA, op_rB;
reg [31:0] r_op_Av, r_op_Bv;
wire [AW+1:0] op_pc;
wire [31:0] w_op_Av, w_op_Bv, op_Av, op_Bv;
reg [31:0] w_pcB_v, w_pcA_v;
reg [31:0] w_op_BnI;
wire op_wR;
reg op_wF;
wire op_gie;
wire [3:0] op_Fl;
reg [6:0] r_op_F;
wire [7:0] op_F;
wire op_ce, op_phase, op_pipe;
reg r_op_break;
wire w_op_valid;
wire op_lowpower_clear;
wire [8:0] w_cpu_info;
// Some pipeline control wires
reg op_illegal;
wire op_break;
wire op_lock;
wire op_sim /* verilator public_flat */;
wire [22:0] op_sim_immv /* verilator public_flat */;
wire alu_sim /* verilator public_flat */;
wire [22:0] alu_sim_immv /* verilator public_flat */;
// }}}
//
//
// PIPELINE STAGE #4 :: ALU / Memory
// Variable declarations
//
//
// {{{
wire [(AW+1):0] alu_pc;
reg [4:0] alu_reg;
reg r_alu_pc_valid, mem_pc_valid;
wire alu_pc_valid;
wire alu_phase;
wire alu_ce /* verilator public_flat */, alu_stall;
wire [31:0] alu_result;
wire [3:0] alu_flags;
wire alu_valid, alu_busy;
wire set_cond;
reg alu_wR, alu_wF;
wire alu_gie, alu_illegal;
wire mem_ce, mem_stalled;
wire div_ce, div_error, div_busy, div_valid;
wire [31:0] div_result;
wire [3:0] div_flags;
// Verilator coverage_off
wire fpu_ce, fpu_error, fpu_busy, fpu_valid;
wire [31:0] fpu_result;
wire [3:0] fpu_flags;
// Verilator coverage_on
reg adf_ce_unconditional;
reg dbgv, dbg_clear_pipe;
reg [31:0] dbg_val;
reg [31:0] debug_pc;
reg r_dbg_stall;
assign div_ce = (op_valid_div)&&(adf_ce_unconditional)&&(set_cond);
assign fpu_ce = (IMPLEMENT_FPU)&&(op_valid_fpu)&&(adf_ce_unconditional)&&(set_cond);
// }}}
//
//
// PIPELINE STAGE #5 :: Write-back
// Variable declarations
//
// {{{
wire wr_write_pc, wr_write_cc,
wr_write_scc, wr_write_ucc;
reg wr_reg_ce, wr_flags_ce;
reg [3:0] wr_flags;
reg [2:0] wr_index;
wire [4:0] wr_reg_id;
reg [31:0] wr_gpreg_vl, wr_spreg_vl;
wire w_switch_to_interrupt, w_release_from_interrupt;
reg [(AW+1):0] ipc;
wire [(AW+1):0] upc;
reg last_write_to_cc;
wire cc_write_hold;
reg r_clear_icache;
reg pfpcset;
reg [2:0] pfpcsrc;
wire w_clken;
// }}}
// }}}
////////////////////////////////////////////////////////////////////////
//
// MASTER: clock enable.
// {{{
////////////////////////////////////////////////////////////////////////
//
//
assign master_ce = (!i_halt || alu_phase)
&&(!cc_write_hold)&&(!o_break)&&(!sleep);
// }}}
////////////////////////////////////////////////////////////////////////
//
// Calculate stall conditions
// {{{
////////////////////////////////////////////////////////////////////////
//
//
// PIPELINE STAGE #1 :: Prefetch
// These are calculated externally, within the prefetch module.
//
// PIPELINE STAGE #2 :: Instruction Decode
// {{{
always @(*)
if (OPT_PIPELINED)
dcd_stalled = (dcd_valid && op_stall);
else
dcd_stalled = (!master_ce)
// Can't step forward when we need to be halted
||(ill_err_i)||(ibus_err_flag)||(idiv_err_flag)
||((dcd_valid || op_valid) && !dcd_early_branch)
||(alu_busy)||(div_busy)||(fpu_busy)||(i_mem_busy);
// }}}
//
// PIPELINE STAGE #3 :: Read Operands
// {{{
generate if (OPT_PIPELINED)
begin : GEN_OP_STALL
reg r_cc_invalid_for_dcd;
reg r_pending_sreg_write;
// cc_invalid_for_dcd
// {{{
initial r_cc_invalid_for_dcd = 1'b0;
always @(posedge i_clk)
if (clear_pipeline)
r_cc_invalid_for_dcd <= 1'b0;
else if ((alu_ce || mem_ce)&&(set_cond)&&(op_valid) &&((op_wF)
||((op_wR)&&(op_R[4:0] == { op_gie, CPU_CC_REG }))))
// If an instruction in the pipeline will write to the
// CC register, then we can't be allowed to release
// any instruction depending upon the CC register
// (for other than conditional execution) into the
// pipeline.
r_cc_invalid_for_dcd <= 1'b1;
else if (r_cc_invalid_for_dcd)
// While the pipeline is busy, keep r_cc_invalid_for_dcd
// high.
r_cc_invalid_for_dcd <= ((alu_busy)||(i_mem_rdbusy)||(div_busy)||(fpu_busy));
else
r_cc_invalid_for_dcd <= 1'b0;
assign cc_invalid_for_dcd = r_cc_invalid_for_dcd;
// }}}
// pending_sreg_write
// {{{
// Used to determine if we need to stall for flags to be ready
initial r_pending_sreg_write = 1'b0;
always @(posedge i_clk)
if (clear_pipeline)
r_pending_sreg_write <= 1'b0;
else if (((adf_ce_unconditional)||(mem_ce))
&&(set_cond)&&(!op_illegal)
&&(op_wR)
&&(op_R[3:1] == 3'h7)
&&(op_R[4:0] != { gie, 4'hf }))
r_pending_sreg_write <= 1'b1;
else if ((!i_mem_rdbusy)&&(!alu_busy))
r_pending_sreg_write <= 1'b0;
assign pending_sreg_write = r_pending_sreg_write;
// }}}
assign op_stall = (op_valid)&&(
// {{{
// Only stall if we're loaded w/valid insns and the
// next stage is accepting our instruction
(!adf_ce_unconditional)&&(!mem_ce)
)
||(dcd_valid)&&(
// If we are halted, then accepting anything
// into the Op stage might accept a register
// that then gets modified by the debugging
// interface so as to be invalid.
i_halt
// Stall if we need to wait for an operand A
// to be ready to read
|| (dcd_A_stall)
// Likewise for B, also includes logic
// regarding immediate offset (register must
// be in register file if we need to add to
// an immediate)
||(dcd_B_stall)
// Or if we need to wait on flags to work on the
// CC register
||(dcd_F_stall)
);
// }}}
assign op_ce = ((dcd_valid)||(dcd_illegal)||(dcd_early_branch))&&(!op_stall);
end else begin : NO_OP_STALLS // !OPT_PIPELINED
// {{{
assign op_stall = 1'b0; // (o_break)||(pending_interrupt);
assign op_ce = ((dcd_valid)||(dcd_early_branch))&&(!op_stall);
assign pending_sreg_write = 1'b0;
assign cc_invalid_for_dcd = 1'b0;
// verilator coverage_off
// Verilator lint_off UNUSED
wire pipe_unused;
assign pipe_unused = &{ 1'b0, cc_invalid_for_dcd,
pending_sreg_write };
// Verilator lint_on UNUSED
// verilator coverage_on
// }}}
end endgenerate
// BUT ... op_ce is too complex for many of the data operations. So
// let's make their circuit enable code simpler. In particular, if
// op_ doesn't need to be preserved, we can change it all we want
// ... right? The clear_pipeline code, for example, really only needs
// to determine whether op_valid is true.
// assign op_change_data_ce = (!op_stall);
// }}}
//
// PIPELINE STAGE #4 :: ALU / Memory
// {{{
// 1. Basic stall is if the previous stage is valid and the next is
// busy.
// 2. Also stall if the prior stage is valid and the master clock enable
// is de-selected
// 3. Stall if someone on the other end is writing the CC register,
// since we don't know if it'll put us to sleep or not.
// 4. Last case: Stall if we would otherwise move a break instruction
// through the ALU. Break instructions are not allowed through
// the ALU.
generate if (OPT_PIPELINED)
begin : GEN_ALU_STALL
// alu_stall, alu_ce
// {{{
assign alu_stall = (((master_stall)||(i_mem_rdbusy))&&(op_valid_alu)) //Case 1&2
||(wr_reg_ce)&&(wr_write_cc);
// assign // alu_ce = (master_ce)&&(op_valid_alu)&&(!alu_stall)
// &&(!clear_pipeline)&&(!op_illegal)
// &&(!pending_sreg_write)
// &&(!alu_sreg_stall);
assign alu_ce = (adf_ce_unconditional)&&(op_valid_alu);
// verilator coverage_off
// Verilator lint_off unused
wire unused_alu_stall = &{ 1'b0, alu_stall };
// Verilator lint_on unused
// verilator coverage_on
// }}}
end else begin : NO_ALU_STALLS
// alu_stall, alu_ce
// {{{
assign alu_stall = (master_stall);
//assign alu_ce = (master_ce)&&(op_valid_alu)
// &&(!clear_pipeline)
// &&(!alu_stall);
assign alu_ce = (adf_ce_unconditional)&&(op_valid_alu);
// verilator coverage_off
// Verilator lint_off unused
wire unused_alu_stall = &{ 1'b0, alu_stall };
// Verilator lint_on unused
// verilator coverage_on
// }}}
end endgenerate
//
// mem_ce
// {{{
// Note: if you change the conditions for mem_ce, you must also change
// alu_pc_valid.
//
assign mem_ce = (master_ce)&&(op_valid_mem)&&(!mem_stalled)
&&(!clear_pipeline);
// }}}
// mem_stalled
// {{{
generate if (OPT_PIPELINED_BUS_ACCESS)
begin : GEN_PIPELINE_MEM_STALL
// {{{
assign mem_stalled = (master_stall)||((op_valid_mem)&&(
(i_mem_pipe_stalled)
||(i_bus_err)||(div_error)
||(!op_pipe && i_mem_busy)
// Stall waiting for flags to be valid
// Or waiting for a write to the PC register
// Or CC register, since that can change the
// PC as well
||((wr_reg_ce)
&&((wr_write_pc)||(wr_write_cc)))));
// }}}
end else if (OPT_PIPELINED)
begin : GEN_MEM_STALL
// {{{
assign mem_stalled = (master_stall)||((op_valid_mem)&&(
(i_bus_err)||(div_error)||(i_mem_busy)
// Stall waiting for flags to be valid
// Or waiting for a write to the PC register
// Or CC register, since that can change the
// PC as well
||((wr_reg_ce)
&&((wr_write_pc)||(wr_write_cc)))));
// }}}
end else begin : NO_MEM_STALL
// {{{
assign mem_stalled = master_stall || i_mem_busy;
// }}}
end endgenerate
// }}}
// }}}
//
// Master stall condition
// {{{
assign master_stall = (!master_ce)||(!op_valid)||(ill_err_i)
||(step && stepped)
||(ibus_err_flag)||(idiv_err_flag)
||(pending_interrupt && !o_bus_lock)&&(!alu_phase)
||(alu_busy)||(div_busy)||(fpu_busy)||(op_break)
||((OPT_PIPELINED)&&(
prelock_stall
||((i_mem_busy)&&(op_illegal))
||((i_mem_busy)&&(op_valid_div))
||(alu_illegal)||(o_break)));
// }}}
//
// (Everything but memory) stall condition
// {{{
// ALU, DIV, or FPU CE ... equivalent to the OR of all three of these
always @(*)
if (OPT_PIPELINED)
adf_ce_unconditional =
(!master_stall)&&(!op_valid_mem)&&(!i_mem_rdbusy)
&&(!i_mem_busy || !op_wR
|| op_R[4:0] != { gie, CPU_CC_REG });
else
adf_ce_unconditional = (!master_stall)&&(op_valid)&&(!op_valid_mem);
// }}}
// }}}
////////////////////////////////////////////////////////////////////////
//
// PIPELINE STAGE #1 :: Instruction fetch
// {{{
////////////////////////////////////////////////////////////////////////
//
//
assign o_pf_ready = (!dcd_stalled && !dcd_phase);
assign o_pf_new_pc = (new_pc)||((dcd_early_branch_stb)&&(!clear_pipeline));
assign o_pf_request_address = ((dcd_early_branch_stb)&&(!clear_pipeline))
? dcd_branch_pc:pf_pc;
assign pf_gie = gie;
// }}}
////////////////////////////////////////////////////////////////////////
//
// PIPELINE STAGE #2 :: Instruction Decode
// {{{
////////////////////////////////////////////////////////////////////////
//
//
assign dcd_ce =((OPT_PIPELINED)&&(!dcd_valid))||(!dcd_stalled);
wire [6:0] dcd_full_R, dcd_full_A, dcd_full_B;
idecode #(
// {{{
.ADDRESS_WIDTH(AW),
.OPT_MPY((OPT_MPY!=0)? 1'b1:1'b0),
.OPT_SHIFTS(OPT_SHIFTS),
.OPT_PIPELINED(OPT_PIPELINED),
.OPT_EARLY_BRANCHING(OPT_EARLY_BRANCHING),
.OPT_DIVIDE(OPT_DIV),
.OPT_FPU(IMPLEMENT_FPU),
.OPT_LOCK(OPT_LOCK),
.OPT_OPIPE(OPT_PIPELINED_BUS_ACCESS),
.OPT_USERMODE(OPT_USERMODE),
.OPT_SIM(OPT_SIM),
.OPT_CIS(OPT_CIS),
.OPT_LOWPOWER(OPT_LOWPOWER)
// }}}
) instruction_decoder(
// {{{
.i_clk(i_clk),
.i_reset(i_reset||clear_pipeline||o_clear_icache),
.i_ce(dcd_ce),
.i_stalled(dcd_stalled),
.i_instruction(i_pf_instruction), .i_gie(pf_gie),
.i_pc(i_pf_instruction_pc), .i_pf_valid(i_pf_valid),
.i_illegal(i_pf_illegal),
.o_valid(dcd_valid), .o_phase(dcd_phase),
.o_illegal(dcd_illegal), .o_pc(dcd_pc),
.o_dcdR(dcd_full_R), .o_dcdA(dcd_full_A),
.o_dcdB(dcd_full_B),
.o_preA(dcd_preA), .o_preB(dcd_preB),
.o_I(dcd_I), .o_zI(dcd_zI), .o_cond(dcd_F),
.o_wF(dcd_wF), .o_op(dcd_opn),
.o_ALU(dcd_ALU), .o_M(dcd_M), .o_DV(dcd_DIV),
.o_FP(dcd_FP), .o_break(dcd_break), .o_lock(dcd_lock),
.o_wR(dcd_wR), .o_rA(dcd_rA), .o_rB(dcd_rB),
.o_early_branch(dcd_early_branch),
.o_early_branch_stb(dcd_early_branch_stb),
.o_branch_pc(dcd_branch_pc), .o_ljmp(dcd_ljmp),
.o_pipe(dcd_pipe),
.o_sim(dcd_sim), .o_sim_immv(dcd_sim_immv)
`ifdef FORMAL
, .f_insn_word(f_dcd_insn_word),
.f_insn_gie(f_dcd_insn_gie),
.f_insn_is_pipeable(f_dcd_insn_is_pipeable)
`endif
// }}}
);
assign { dcd_Rcc, dcd_Rpc, dcd_R } = (!OPT_LOWPOWER || dcd_valid || !OPT_PIPELINED) ? dcd_full_R : 7'h0;
assign { dcd_Acc, dcd_Apc, dcd_A } = (!OPT_LOWPOWER || dcd_valid || !OPT_PIPELINED) ? dcd_full_A : 7'h0;
assign { dcd_Bcc, dcd_Bpc, dcd_B } = (!OPT_LOWPOWER || dcd_valid || !OPT_PIPELINED) ? dcd_full_B : 7'h0;
assign dcd_gie = pf_gie;
// }}}
////////////////////////////////////////////////////////////////////////
//
// PIPELINE STAGE #3 :: Read Operands (Registers)
// {{{
////////////////////////////////////////////////////////////////////////
//
//
// op_pipe
// {{{
generate if (OPT_PIPELINED_BUS_ACCESS)
begin : GEN_OP_PIPE // Memory pipelining
reg r_op_pipe;
// To be a pipeable operation, there must be
// two valid adjacent instructions
// Both must be memory instructions
// Both must be writes, or both must be reads
// Both operations must be to the same identical address,
// or at least a single (one) increment above that
// address
//
// However ... we need to know this before this clock, hence
// this is calculated in the instruction decoder.
initial r_op_pipe = 1'b0;
always @(posedge i_clk)
if ((OPT_LOWPOWER && i_reset)||(clear_pipeline)||(i_halt))
r_op_pipe <= 1'b0;
else if (op_ce)
r_op_pipe <= (dcd_pipe)&&(op_valid_mem)&&(!OPT_LOWPOWER || !dcd_illegal);
else if ((wr_reg_ce)&&(wr_reg_id == op_Bid[4:0]))
r_op_pipe <= 1'b0;
else if (mem_ce) // Clear us any time an op_ is clocked in
r_op_pipe <= 1'b0;
assign op_pipe = r_op_pipe;
`ifdef FORMAL
always @(*)
if (OPT_LOWPOWER && !op_valid_mem)
assert(!r_op_pipe);
`endif
end else begin : NO_OP_PIPE
assign op_pipe = 1'b0;
end endgenerate
// }}}
// Read from our register set
// {{{
generate if (OPT_DISTRIBUTED_REGS)
begin : GEN_DISTRIBUTED_REGS
// {{{
if (OPT_USERMODE)
begin : GEN_FULL_REGSET
assign w_op_Av = (!OPT_LOWPOWER || dcd_valid || !OPT_PIPELINED) ? regset[dcd_A] : 32'h0;
assign w_op_Bv = (!OPT_LOWPOWER || dcd_valid || !OPT_PIPELINED) ? regset[dcd_B] : 32'h0;
end else begin : GEN_NO_USERREGS
assign w_op_Av = (!OPT_LOWPOWER || dcd_valid || !OPT_PIPELINED) ? regset[dcd_A[3:0]] : 32'h0;
assign w_op_Bv = (!OPT_LOWPOWER || dcd_valid || !OPT_PIPELINED) ? regset[dcd_B[3:0]] : 32'h0;
end
// verilator coverage_off
// verilator lint_off UNUSED
wire unused_prereg_addrs;
assign unused_prereg_addrs = &{ 1'b0, dcd_preA, dcd_preB };
// verilator lint_on UNUSED
// verilator coverage_on
// }}}
end else begin : GEN_BLOCKRAM
// {{{
reg [31:0] pre_rewrite_value, pre_op_Av, pre_op_Bv;
reg pre_rewrite_flag_A, pre_rewrite_flag_B;
always @(posedge i_clk)
if (dcd_ce)
begin
pre_rewrite_flag_A <= (wr_reg_ce)&&(dcd_preA == wr_reg_id);
pre_rewrite_flag_B <= (wr_reg_ce)&&(dcd_preB == wr_reg_id);
pre_rewrite_value <= wr_gpreg_vl;
end else if (OPT_PIPELINED && dcd_valid)
begin
pre_rewrite_flag_A <= (wr_reg_ce)&&(dcd_A == wr_reg_id);
pre_rewrite_flag_B <= (wr_reg_ce)&&(dcd_B == wr_reg_id);
pre_rewrite_value <= wr_gpreg_vl;
end
if (OPT_USERMODE)
begin : GEN_FULL_REGSET
always @(posedge i_clk)
if (dcd_ce || (OPT_PIPELINED && dcd_valid))
pre_op_Av <= regset[dcd_ce ? dcd_preA : dcd_A];
always @(posedge i_clk)
if (dcd_ce || (OPT_PIPELINED && dcd_valid))
pre_op_Bv <= regset[dcd_ce ? dcd_preB : dcd_B];
end else begin : GEN_NO_USERREGS
always @(posedge i_clk)
if (dcd_ce || (OPT_PIPELINED && dcd_valid))
pre_op_Av <= regset[dcd_ce ? dcd_preA[3:0] : dcd_A[3:0]];
always @(posedge i_clk)
if (dcd_ce || (OPT_PIPELINED && dcd_valid))
pre_op_Bv <= regset[dcd_ce ? dcd_preB[3:0] : dcd_B[3:0]];
end
assign w_op_Av = (pre_rewrite_flag_A) ? pre_rewrite_value : pre_op_Av;
assign w_op_Bv = (pre_rewrite_flag_B) ? pre_rewrite_value : pre_op_Bv;
// }}}
end endgenerate
// }}}
assign w_cpu_info = {
// {{{
1'b1,
(OPT_MPY >0)? 1'b1:1'b0,
(OPT_DIV >0)? 1'b1:1'b0,
(IMPLEMENT_FPU >0)? 1'b1:1'b0,
OPT_PIPELINED,
1'b0,
(OPT_EARLY_BRANCHING > 0)? 1'b1:1'b0,
OPT_PIPELINED_BUS_ACCESS,
OPT_CIS
};
// }}}
always @(*)
begin
w_pcA_v = 0;
if ((!OPT_USERMODE)||(dcd_A[4] == dcd_gie))
w_pcA_v[(AW+1):0] = { dcd_pc[AW+1:2], 2'b00 };
else
w_pcA_v[(AW+1):0] = { upc[(AW+1):2], uhalt_phase, 1'b0 };
end
// Op register addresses
// {{{
generate if (OPT_PIPELINED)
begin : OP_REG_ADVANEC
// {{{
reg [4:0] r_op_R;
reg [4:0] r_op_Aid, r_op_Bid;
reg r_op_rA, r_op_rB;
initial r_op_R = 0;
initial r_op_Aid = 0;
initial r_op_Bid = 0;
initial r_op_rA = 0;
initial r_op_rB = 0;
initial op_Rcc = 0;
always @(posedge i_clk)
begin
if (op_ce && (!OPT_LOWPOWER || w_op_valid))
begin
r_op_R <= dcd_R;
r_op_Aid <= dcd_A;
if ((dcd_rB)&&(!dcd_early_branch)&&(!dcd_illegal))
r_op_Bid <= dcd_B;
r_op_rA <= (dcd_rA)&&(!dcd_early_branch)&&(!dcd_illegal);
r_op_rB <= (dcd_rB)&&(!dcd_early_branch)&&(!dcd_illegal);
op_Rcc <= (dcd_Rcc)&&(dcd_wR)&&(dcd_R[4]==dcd_gie);
end
if (op_lowpower_clear)
begin
r_op_rA <= 1'b0;
r_op_rB <= 1'b0;
end
end
assign op_R = r_op_R;
assign op_Aid = r_op_Aid;
assign op_Bid = r_op_Bid;
assign op_rA = r_op_rA;
assign op_rB = r_op_rB;
// }}}
end else begin : OP_REG_COPY
// {{{
assign op_R = dcd_R;
assign op_Aid = dcd_A;
assign op_Bid = dcd_B;
assign op_rA = dcd_rA;
assign op_rB = dcd_rB;
always @(*)
op_Rcc = (dcd_Rcc)&&(dcd_wR)&&(dcd_R[4]==dcd_gie);
// }}}
end endgenerate
// }}}
// r_op_Av -- The registered value of the op A register
// {{{
reg [2:0] avsrc;
reg [2:0] bvsrc;
reg [1:0] bisrc;
always @(*)
begin
avsrc = 3'b000;
if (!OPT_PIPELINED || op_ce)
begin
if (dcd_Apc)
avsrc = 3'b101;
else if (dcd_Acc)
avsrc = 3'b110;
else
avsrc = 3'b111;
end
if (OPT_PIPELINED && wr_reg_ce)
begin
if (!op_ce && wr_reg_id == op_Aid && op_rA)
avsrc = 3'b100;
else if (op_ce && wr_reg_id == dcd_A)
avsrc = 3'b100;
end
end
// 44313
initial r_op_Av = 0;
always @(posedge i_clk)
begin
if (avsrc[2])
case(avsrc[1:0])
2'b00: r_op_Av <= wr_gpreg_vl;
2'b01: r_op_Av <= w_pcA_v;
2'b10: r_op_Av <= { w_cpu_info, w_op_Av[22:16], (dcd_A[4])?w_uflags:w_iflags };
2'b11: r_op_Av <= w_op_Av;
endcase
if (op_lowpower_clear)
r_op_Av <= 0;
end
// }}}
// w_op_B -- The registered value of the op B register
// {{{
always @(*)
begin
w_pcB_v = 0;
if ((!OPT_USERMODE)||(dcd_B[4] == dcd_gie))
w_pcB_v[(AW+1):0] = { dcd_pc[AW+1:2], 2'b00 };
else
w_pcB_v[(AW+1):0] = { upc[(AW+1):2], uhalt_phase, 1'b0 };
end
always @(*)
if (!dcd_rB)
bisrc = 0;
else if ((OPT_PIPELINED)&&(wr_reg_ce)&&(wr_reg_id == dcd_B))
bisrc = 1;
else if (dcd_Bcc)
bisrc = 2;
else
bisrc = 3;
always @(*)
case(bisrc[1:0])
2'b00: w_op_BnI = 0;
2'b01: w_op_BnI = wr_gpreg_vl;
2'b10: w_op_BnI = { w_cpu_info, w_op_Bv[22:16],
(dcd_B[4]) ? w_uflags : w_iflags };
2'b11: w_op_BnI = w_op_Bv;
endcase
always @(*)
begin
bvsrc = 0;
if ((!OPT_PIPELINED)||(op_ce))
begin
if ((dcd_Bpc)&&(dcd_rB))
bvsrc = 3'b100;
else
bvsrc = 3'b101;
end else if ((OPT_PIPELINED)&&(op_rB)
&&(wr_reg_ce)&&(op_Bid == wr_reg_id))
bvsrc = 3'b110;
end
initial r_op_Bv = 0;
always @(posedge i_clk)
begin
if (bvsrc[2]) casez(bvsrc[1:0])
2'b00: r_op_Bv <= w_pcB_v + { dcd_I[29:0], 2'b00 };
2'b01: r_op_Bv <= w_op_BnI + dcd_I;
2'b1?: r_op_Bv <= wr_gpreg_vl;
endcase
if (op_lowpower_clear)
r_op_Bv <= 0;
end
// }}}
// op_F
// {{{