2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * The Simulation Library.
22 * This Verilog library contains simple simulation models for the internal
23 * cells ($not, ...) generated by the frontends and used in most passes.
25 * This library can be used to verify the internal netlists as generated
26 * by the different frontends and passes.
28 * Note that memory can only be simulated when all $memrd and $memwr cells
29 * have been merged to stand-alone $mem cells (this is what the "memory_collect"
34 // --------------------------------------------------------
36 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
40 //- A bit-wise inverter. This corresponds to the Verilog unary prefix '~' operator.
44 parameter A_SIGNED = 0;
45 parameter A_WIDTH = 0;
46 parameter Y_WIDTH = 0;
48 input [A_WIDTH-1:0] A;
49 output [Y_WIDTH-1:0] Y;
52 if (A_SIGNED) begin:BLOCK1
53 assign Y = ~$signed(A);
62 // --------------------------------------------------------
64 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
68 //- A buffer. This corresponds to the Verilog unary prefix '+' operator.
72 parameter A_SIGNED = 0;
73 parameter A_WIDTH = 0;
74 parameter Y_WIDTH = 0;
76 input [A_WIDTH-1:0] A;
77 output [Y_WIDTH-1:0] Y;
80 if (A_SIGNED) begin:BLOCK1
81 assign Y = $signed(A);
89 // --------------------------------------------------------
91 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
95 //- An arithmetic inverter. This corresponds to the Verilog unary prefix '-' operator.
99 parameter A_SIGNED = 0;
100 parameter A_WIDTH = 0;
101 parameter Y_WIDTH = 0;
103 input [A_WIDTH-1:0] A;
104 output [Y_WIDTH-1:0] Y;
107 if (A_SIGNED) begin:BLOCK1
108 assign Y = -$signed(A);
109 end else begin:BLOCK2
116 // --------------------------------------------------------
118 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
122 //- A bit-wise AND. This corresponds to the Verilog '&' operator.
124 module \$and (A, B, Y);
126 parameter A_SIGNED = 0;
127 parameter B_SIGNED = 0;
128 parameter A_WIDTH = 0;
129 parameter B_WIDTH = 0;
130 parameter Y_WIDTH = 0;
132 input [A_WIDTH-1:0] A;
133 input [B_WIDTH-1:0] B;
134 output [Y_WIDTH-1:0] Y;
137 if (A_SIGNED && B_SIGNED) begin:BLOCK1
138 assign Y = $signed(A) & $signed(B);
139 end else begin:BLOCK2
146 // --------------------------------------------------------
148 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
152 //- A bit-wise OR. This corresponds to the Verilog '|' operator.
154 module \$or (A, B, Y);
156 parameter A_SIGNED = 0;
157 parameter B_SIGNED = 0;
158 parameter A_WIDTH = 0;
159 parameter B_WIDTH = 0;
160 parameter Y_WIDTH = 0;
162 input [A_WIDTH-1:0] A;
163 input [B_WIDTH-1:0] B;
164 output [Y_WIDTH-1:0] Y;
167 if (A_SIGNED && B_SIGNED) begin:BLOCK1
168 assign Y = $signed(A) | $signed(B);
169 end else begin:BLOCK2
176 // --------------------------------------------------------
178 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
182 //- A bit-wise XOR. This corresponds to the Verilog '^' operator.
184 module \$xor (A, B, Y);
186 parameter A_SIGNED = 0;
187 parameter B_SIGNED = 0;
188 parameter A_WIDTH = 0;
189 parameter B_WIDTH = 0;
190 parameter Y_WIDTH = 0;
192 input [A_WIDTH-1:0] A;
193 input [B_WIDTH-1:0] B;
194 output [Y_WIDTH-1:0] Y;
197 if (A_SIGNED && B_SIGNED) begin:BLOCK1
198 assign Y = $signed(A) ^ $signed(B);
199 end else begin:BLOCK2
206 // --------------------------------------------------------
208 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
212 //- A bit-wise XNOR. This corresponds to the Verilog '~^' operator.
214 module \$xnor (A, B, Y);
216 parameter A_SIGNED = 0;
217 parameter B_SIGNED = 0;
218 parameter A_WIDTH = 0;
219 parameter B_WIDTH = 0;
220 parameter Y_WIDTH = 0;
222 input [A_WIDTH-1:0] A;
223 input [B_WIDTH-1:0] B;
224 output [Y_WIDTH-1:0] Y;
227 if (A_SIGNED && B_SIGNED) begin:BLOCK1
228 assign Y = $signed(A) ~^ $signed(B);
229 end else begin:BLOCK2
236 // --------------------------------------------------------
238 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
240 //- $reduce_and (A, B, Y)
242 //- An AND reduction. This corresponds to the Verilog unary prefix '&' operator.
244 module \$reduce_and (A, Y);
246 parameter A_SIGNED = 0;
247 parameter A_WIDTH = 0;
248 parameter Y_WIDTH = 0;
250 input [A_WIDTH-1:0] A;
251 output [Y_WIDTH-1:0] Y;
254 if (A_SIGNED) begin:BLOCK1
255 assign Y = &$signed(A);
256 end else begin:BLOCK2
263 // --------------------------------------------------------
265 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
267 //- $reduce_or (A, B, Y)
269 //- An OR reduction. This corresponds to the Verilog unary prefix '|' operator.
271 module \$reduce_or (A, Y);
273 parameter A_SIGNED = 0;
274 parameter A_WIDTH = 0;
275 parameter Y_WIDTH = 0;
277 input [A_WIDTH-1:0] A;
278 output [Y_WIDTH-1:0] Y;
281 if (A_SIGNED) begin:BLOCK1
282 assign Y = |$signed(A);
283 end else begin:BLOCK2
290 // --------------------------------------------------------
292 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
294 //- $reduce_xor (A, B, Y)
296 //- A XOR reduction. This corresponds to the Verilog unary prefix '^' operator.
298 module \$reduce_xor (A, Y);
300 parameter A_SIGNED = 0;
301 parameter A_WIDTH = 0;
302 parameter Y_WIDTH = 0;
304 input [A_WIDTH-1:0] A;
305 output [Y_WIDTH-1:0] Y;
308 if (A_SIGNED) begin:BLOCK1
309 assign Y = ^$signed(A);
310 end else begin:BLOCK2
317 // --------------------------------------------------------
319 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
321 //- $reduce_xnor (A, B, Y)
323 //- A XNOR reduction. This corresponds to the Verilog unary prefix '~^' operator.
325 module \$reduce_xnor (A, Y);
327 parameter A_SIGNED = 0;
328 parameter A_WIDTH = 0;
329 parameter Y_WIDTH = 0;
331 input [A_WIDTH-1:0] A;
332 output [Y_WIDTH-1:0] Y;
335 if (A_SIGNED) begin:BLOCK1
336 assign Y = ~^$signed(A);
337 end else begin:BLOCK2
344 // --------------------------------------------------------
346 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
348 //- $reduce_bool (A, B, Y)
350 //- An OR reduction. This cell type is used instead of $reduce_or when a signal is
351 //- implicitly converted to a boolean signal, e.g. for operands of '&&' and '||'.
353 module \$reduce_bool (A, Y);
355 parameter A_SIGNED = 0;
356 parameter A_WIDTH = 0;
357 parameter Y_WIDTH = 0;
359 input [A_WIDTH-1:0] A;
360 output [Y_WIDTH-1:0] Y;
363 if (A_SIGNED) begin:BLOCK1
364 assign Y = !(!$signed(A));
365 end else begin:BLOCK2
372 // --------------------------------------------------------
374 module \$shl (A, B, Y);
376 parameter A_SIGNED = 0;
377 parameter B_SIGNED = 0;
378 parameter A_WIDTH = 0;
379 parameter B_WIDTH = 0;
380 parameter Y_WIDTH = 0;
382 input [A_WIDTH-1:0] A;
383 input [B_WIDTH-1:0] B;
384 output [Y_WIDTH-1:0] Y;
387 if (A_SIGNED) begin:BLOCK1
388 assign Y = $signed(A) << B;
389 end else begin:BLOCK2
396 // --------------------------------------------------------
398 module \$shr (A, B, Y);
400 parameter A_SIGNED = 0;
401 parameter B_SIGNED = 0;
402 parameter A_WIDTH = 0;
403 parameter B_WIDTH = 0;
404 parameter Y_WIDTH = 0;
406 input [A_WIDTH-1:0] A;
407 input [B_WIDTH-1:0] B;
408 output [Y_WIDTH-1:0] Y;
411 if (A_SIGNED) begin:BLOCK1
412 assign Y = $signed(A) >> B;
413 end else begin:BLOCK2
420 // --------------------------------------------------------
422 module \$sshl (A, B, Y);
424 parameter A_SIGNED = 0;
425 parameter B_SIGNED = 0;
426 parameter A_WIDTH = 0;
427 parameter B_WIDTH = 0;
428 parameter Y_WIDTH = 0;
430 input [A_WIDTH-1:0] A;
431 input [B_WIDTH-1:0] B;
432 output [Y_WIDTH-1:0] Y;
435 if (A_SIGNED) begin:BLOCK1
436 assign Y = $signed(A) <<< B;
437 end else begin:BLOCK2
444 // --------------------------------------------------------
446 module \$sshr (A, B, Y);
448 parameter A_SIGNED = 0;
449 parameter B_SIGNED = 0;
450 parameter A_WIDTH = 0;
451 parameter B_WIDTH = 0;
452 parameter Y_WIDTH = 0;
454 input [A_WIDTH-1:0] A;
455 input [B_WIDTH-1:0] B;
456 output [Y_WIDTH-1:0] Y;
459 if (A_SIGNED) begin:BLOCK1
460 assign Y = $signed(A) >>> B;
461 end else begin:BLOCK2
468 // --------------------------------------------------------
470 module \$shift (A, B, Y);
472 parameter A_SIGNED = 0;
473 parameter B_SIGNED = 0;
474 parameter A_WIDTH = 0;
475 parameter B_WIDTH = 0;
476 parameter Y_WIDTH = 0;
478 input [A_WIDTH-1:0] A;
479 input [B_WIDTH-1:0] B;
480 output [Y_WIDTH-1:0] Y;
483 if (B_SIGNED) begin:BLOCK1
484 assign Y = $signed(B) < 0 ? A << -B : A >> B;
485 end else begin:BLOCK2
492 // --------------------------------------------------------
494 module \$shiftx (A, B, Y);
496 parameter A_SIGNED = 0;
497 parameter B_SIGNED = 0;
498 parameter A_WIDTH = 0;
499 parameter B_WIDTH = 0;
500 parameter Y_WIDTH = 0;
502 input [A_WIDTH-1:0] A;
503 input [B_WIDTH-1:0] B;
504 output [Y_WIDTH-1:0] Y;
508 if (B_SIGNED) begin:BLOCK1
509 assign Y = A[$signed(B) +: Y_WIDTH];
510 end else begin:BLOCK2
511 assign Y = A[B +: Y_WIDTH];
517 // --------------------------------------------------------
519 module \$fa (A, B, C, X, Y);
523 input [WIDTH-1:0] A, B, C;
524 output [WIDTH-1:0] X, Y;
526 wire [WIDTH-1:0] t1, t2, t3;
528 assign t1 = A ^ B, t2 = A & B, t3 = C & t1;
529 assign Y = t1 ^ C, X = (t2 | t3) ^ (Y ^ Y);
533 // --------------------------------------------------------
535 module \$lcu (P, G, CI, CO);
539 input [WIDTH-1:0] P, G;
542 output reg [WIDTH-1:0] CO;
547 if (^{P, G, CI} !== 1'bx) begin
548 CO[0] = G[0] || (P[0] && CI);
549 for (i = 1; i < WIDTH; i = i+1)
550 CO[i] = G[i] || (P[i] && CO[i-1]);
556 // --------------------------------------------------------
558 module \$alu (A, B, CI, BI, X, Y, CO);
560 parameter A_SIGNED = 0;
561 parameter B_SIGNED = 0;
562 parameter A_WIDTH = 1;
563 parameter B_WIDTH = 1;
564 parameter Y_WIDTH = 1;
566 input [A_WIDTH-1:0] A;
567 input [B_WIDTH-1:0] B;
568 output [Y_WIDTH-1:0] X, Y;
571 output [Y_WIDTH-1:0] CO;
573 wire [Y_WIDTH-1:0] AA, BB;
576 if (A_SIGNED && B_SIGNED) begin:BLOCK1
577 assign AA = $signed(A), BB = BI ? ~$signed(B) : $signed(B);
578 end else begin:BLOCK2
579 assign AA = $unsigned(A), BB = BI ? ~$unsigned(B) : $unsigned(B);
583 // this is 'x' if Y and CO should be all 'x', and '0' otherwise
584 wire y_co_undef = ^{A, A, B, B, CI, CI, BI, BI};
587 assign Y = (AA + BB + CI) ^ {Y_WIDTH{y_co_undef}};
591 get_carry = (a&b) | (a&c) | (b&c);
596 assign CO[0] = get_carry(AA[0], BB[0], CI) ^ y_co_undef;
597 for (i = 1; i < Y_WIDTH; i = i+1) begin:BLOCK3
598 assign CO[i] = get_carry(AA[i], BB[i], CO[i-1]) ^ y_co_undef;
604 // --------------------------------------------------------
606 module \$lt (A, B, Y);
608 parameter A_SIGNED = 0;
609 parameter B_SIGNED = 0;
610 parameter A_WIDTH = 0;
611 parameter B_WIDTH = 0;
612 parameter Y_WIDTH = 0;
614 input [A_WIDTH-1:0] A;
615 input [B_WIDTH-1:0] B;
616 output [Y_WIDTH-1:0] Y;
619 if (A_SIGNED && B_SIGNED) begin:BLOCK1
620 assign Y = $signed(A) < $signed(B);
621 end else begin:BLOCK2
628 // --------------------------------------------------------
630 module \$le (A, B, Y);
632 parameter A_SIGNED = 0;
633 parameter B_SIGNED = 0;
634 parameter A_WIDTH = 0;
635 parameter B_WIDTH = 0;
636 parameter Y_WIDTH = 0;
638 input [A_WIDTH-1:0] A;
639 input [B_WIDTH-1:0] B;
640 output [Y_WIDTH-1:0] Y;
643 if (A_SIGNED && B_SIGNED) begin:BLOCK1
644 assign Y = $signed(A) <= $signed(B);
645 end else begin:BLOCK2
652 // --------------------------------------------------------
654 module \$eq (A, B, Y);
656 parameter A_SIGNED = 0;
657 parameter B_SIGNED = 0;
658 parameter A_WIDTH = 0;
659 parameter B_WIDTH = 0;
660 parameter Y_WIDTH = 0;
662 input [A_WIDTH-1:0] A;
663 input [B_WIDTH-1:0] B;
664 output [Y_WIDTH-1:0] Y;
667 if (A_SIGNED && B_SIGNED) begin:BLOCK1
668 assign Y = $signed(A) == $signed(B);
669 end else begin:BLOCK2
676 // --------------------------------------------------------
678 module \$ne (A, B, Y);
680 parameter A_SIGNED = 0;
681 parameter B_SIGNED = 0;
682 parameter A_WIDTH = 0;
683 parameter B_WIDTH = 0;
684 parameter Y_WIDTH = 0;
686 input [A_WIDTH-1:0] A;
687 input [B_WIDTH-1:0] B;
688 output [Y_WIDTH-1:0] Y;
691 if (A_SIGNED && B_SIGNED) begin:BLOCK1
692 assign Y = $signed(A) != $signed(B);
693 end else begin:BLOCK2
700 // --------------------------------------------------------
702 module \$eqx (A, B, Y);
704 parameter A_SIGNED = 0;
705 parameter B_SIGNED = 0;
706 parameter A_WIDTH = 0;
707 parameter B_WIDTH = 0;
708 parameter Y_WIDTH = 0;
710 input [A_WIDTH-1:0] A;
711 input [B_WIDTH-1:0] B;
712 output [Y_WIDTH-1:0] Y;
715 if (A_SIGNED && B_SIGNED) begin:BLOCK1
716 assign Y = $signed(A) === $signed(B);
717 end else begin:BLOCK2
724 // --------------------------------------------------------
726 module \$nex (A, B, Y);
728 parameter A_SIGNED = 0;
729 parameter B_SIGNED = 0;
730 parameter A_WIDTH = 0;
731 parameter B_WIDTH = 0;
732 parameter Y_WIDTH = 0;
734 input [A_WIDTH-1:0] A;
735 input [B_WIDTH-1:0] B;
736 output [Y_WIDTH-1:0] Y;
739 if (A_SIGNED && B_SIGNED) begin:BLOCK1
740 assign Y = $signed(A) !== $signed(B);
741 end else begin:BLOCK2
748 // --------------------------------------------------------
750 module \$ge (A, B, Y);
752 parameter A_SIGNED = 0;
753 parameter B_SIGNED = 0;
754 parameter A_WIDTH = 0;
755 parameter B_WIDTH = 0;
756 parameter Y_WIDTH = 0;
758 input [A_WIDTH-1:0] A;
759 input [B_WIDTH-1:0] B;
760 output [Y_WIDTH-1:0] Y;
763 if (A_SIGNED && B_SIGNED) begin:BLOCK1
764 assign Y = $signed(A) >= $signed(B);
765 end else begin:BLOCK2
772 // --------------------------------------------------------
774 module \$gt (A, B, Y);
776 parameter A_SIGNED = 0;
777 parameter B_SIGNED = 0;
778 parameter A_WIDTH = 0;
779 parameter B_WIDTH = 0;
780 parameter Y_WIDTH = 0;
782 input [A_WIDTH-1:0] A;
783 input [B_WIDTH-1:0] B;
784 output [Y_WIDTH-1:0] Y;
787 if (A_SIGNED && B_SIGNED) begin:BLOCK1
788 assign Y = $signed(A) > $signed(B);
789 end else begin:BLOCK2
796 // --------------------------------------------------------
798 module \$add (A, B, Y);
800 parameter A_SIGNED = 0;
801 parameter B_SIGNED = 0;
802 parameter A_WIDTH = 0;
803 parameter B_WIDTH = 0;
804 parameter Y_WIDTH = 0;
806 input [A_WIDTH-1:0] A;
807 input [B_WIDTH-1:0] B;
808 output [Y_WIDTH-1:0] Y;
811 if (A_SIGNED && B_SIGNED) begin:BLOCK1
812 assign Y = $signed(A) + $signed(B);
813 end else begin:BLOCK2
820 // --------------------------------------------------------
822 module \$sub (A, B, Y);
824 parameter A_SIGNED = 0;
825 parameter B_SIGNED = 0;
826 parameter A_WIDTH = 0;
827 parameter B_WIDTH = 0;
828 parameter Y_WIDTH = 0;
830 input [A_WIDTH-1:0] A;
831 input [B_WIDTH-1:0] B;
832 output [Y_WIDTH-1:0] Y;
835 if (A_SIGNED && B_SIGNED) begin:BLOCK1
836 assign Y = $signed(A) - $signed(B);
837 end else begin:BLOCK2
844 // --------------------------------------------------------
846 module \$mul (A, B, Y);
848 parameter A_SIGNED = 0;
849 parameter B_SIGNED = 0;
850 parameter A_WIDTH = 0;
851 parameter B_WIDTH = 0;
852 parameter Y_WIDTH = 0;
854 input [A_WIDTH-1:0] A;
855 input [B_WIDTH-1:0] B;
856 output [Y_WIDTH-1:0] Y;
859 if (A_SIGNED && B_SIGNED) begin:BLOCK1
860 assign Y = $signed(A) * $signed(B);
861 end else begin:BLOCK2
868 // --------------------------------------------------------
870 module \$macc (A, B, Y);
872 parameter A_WIDTH = 0;
873 parameter B_WIDTH = 0;
874 parameter Y_WIDTH = 0;
875 parameter CONFIG = 4'b0000;
876 parameter CONFIG_WIDTH = 4;
878 input [A_WIDTH-1:0] A;
879 input [B_WIDTH-1:0] B;
880 output reg [Y_WIDTH-1:0] Y;
882 // Xilinx XSIM does not like $clog2() below..
883 function integer my_clog2;
891 my_clog2 = my_clog2 + 1;
896 localparam integer num_bits = CONFIG[3:0] > 0 ? CONFIG[3:0] : 1;
897 localparam integer num_ports = (CONFIG_WIDTH-4) / (2 + 2*num_bits);
898 localparam integer num_abits = my_clog2(A_WIDTH) > 0 ? my_clog2(A_WIDTH) : 1;
900 function [2*num_ports*num_abits-1:0] get_port_offsets;
901 input [CONFIG_WIDTH-1:0] cfg;
905 get_port_offsets = 0;
906 for (i = 0; i < num_ports; i = i+1) begin
907 get_port_offsets[(2*i + 0)*num_abits +: num_abits] = cursor;
908 cursor = cursor + cfg[4 + i*(2 + 2*num_bits) + 2 +: num_bits];
909 get_port_offsets[(2*i + 1)*num_abits +: num_abits] = cursor;
910 cursor = cursor + cfg[4 + i*(2 + 2*num_bits) + 2 + num_bits +: num_bits];
915 localparam [2*num_ports*num_abits-1:0] port_offsets = get_port_offsets(CONFIG);
917 `define PORT_IS_SIGNED (0 + CONFIG[4 + i*(2 + 2*num_bits)])
918 `define PORT_DO_SUBTRACT (0 + CONFIG[4 + i*(2 + 2*num_bits) + 1])
919 `define PORT_SIZE_A (0 + CONFIG[4 + i*(2 + 2*num_bits) + 2 +: num_bits])
920 `define PORT_SIZE_B (0 + CONFIG[4 + i*(2 + 2*num_bits) + 2 + num_bits +: num_bits])
921 `define PORT_OFFSET_A (0 + port_offsets[2*i*num_abits +: num_abits])
922 `define PORT_OFFSET_B (0 + port_offsets[2*i*num_abits + num_abits +: num_abits])
925 reg [Y_WIDTH-1:0] tmp_a, tmp_b;
929 for (i = 0; i < num_ports; i = i+1)
934 for (j = 0; j < `PORT_SIZE_A; j = j+1)
935 tmp_a[j] = A[`PORT_OFFSET_A + j];
937 if (`PORT_IS_SIGNED && `PORT_SIZE_A > 0)
938 for (j = `PORT_SIZE_A; j < Y_WIDTH; j = j+1)
939 tmp_a[j] = tmp_a[`PORT_SIZE_A-1];
941 for (j = 0; j < `PORT_SIZE_B; j = j+1)
942 tmp_b[j] = A[`PORT_OFFSET_B + j];
944 if (`PORT_IS_SIGNED && `PORT_SIZE_B > 0)
945 for (j = `PORT_SIZE_B; j < Y_WIDTH; j = j+1)
946 tmp_b[j] = tmp_b[`PORT_SIZE_B-1];
948 if (`PORT_SIZE_B > 0)
949 tmp_a = tmp_a * tmp_b;
951 if (`PORT_DO_SUBTRACT)
956 for (i = 0; i < B_WIDTH; i = i+1) begin
961 `undef PORT_IS_SIGNED
962 `undef PORT_DO_SUBTRACT
970 // --------------------------------------------------------
972 module \$div (A, B, Y);
974 parameter A_SIGNED = 0;
975 parameter B_SIGNED = 0;
976 parameter A_WIDTH = 0;
977 parameter B_WIDTH = 0;
978 parameter Y_WIDTH = 0;
980 input [A_WIDTH-1:0] A;
981 input [B_WIDTH-1:0] B;
982 output [Y_WIDTH-1:0] Y;
985 if (A_SIGNED && B_SIGNED) begin:BLOCK1
986 assign Y = $signed(A) / $signed(B);
987 end else begin:BLOCK2
994 // --------------------------------------------------------
996 module \$mod (A, B, Y);
998 parameter A_SIGNED = 0;
999 parameter B_SIGNED = 0;
1000 parameter A_WIDTH = 0;
1001 parameter B_WIDTH = 0;
1002 parameter Y_WIDTH = 0;
1004 input [A_WIDTH-1:0] A;
1005 input [B_WIDTH-1:0] B;
1006 output [Y_WIDTH-1:0] Y;
1009 if (A_SIGNED && B_SIGNED) begin:BLOCK1
1010 assign Y = $signed(A) % $signed(B);
1011 end else begin:BLOCK2
1018 // --------------------------------------------------------
1019 `ifndef SIMLIB_NOPOW
1021 module \$pow (A, B, Y);
1023 parameter A_SIGNED = 0;
1024 parameter B_SIGNED = 0;
1025 parameter A_WIDTH = 0;
1026 parameter B_WIDTH = 0;
1027 parameter Y_WIDTH = 0;
1029 input [A_WIDTH-1:0] A;
1030 input [B_WIDTH-1:0] B;
1031 output [Y_WIDTH-1:0] Y;
1034 if (A_SIGNED && B_SIGNED) begin:BLOCK1
1035 assign Y = $signed(A) ** $signed(B);
1036 end else if (A_SIGNED) begin:BLOCK2
1037 assign Y = $signed(A) ** B;
1038 end else if (B_SIGNED) begin:BLOCK3
1039 assign Y = A ** $signed(B);
1040 end else begin:BLOCK4
1048 // --------------------------------------------------------
1050 module \$logic_not (A, Y);
1052 parameter A_SIGNED = 0;
1053 parameter A_WIDTH = 0;
1054 parameter Y_WIDTH = 0;
1056 input [A_WIDTH-1:0] A;
1057 output [Y_WIDTH-1:0] Y;
1060 if (A_SIGNED) begin:BLOCK1
1061 assign Y = !$signed(A);
1062 end else begin:BLOCK2
1069 // --------------------------------------------------------
1071 module \$logic_and (A, B, Y);
1073 parameter A_SIGNED = 0;
1074 parameter B_SIGNED = 0;
1075 parameter A_WIDTH = 0;
1076 parameter B_WIDTH = 0;
1077 parameter Y_WIDTH = 0;
1079 input [A_WIDTH-1:0] A;
1080 input [B_WIDTH-1:0] B;
1081 output [Y_WIDTH-1:0] Y;
1084 if (A_SIGNED && B_SIGNED) begin:BLOCK1
1085 assign Y = $signed(A) && $signed(B);
1086 end else begin:BLOCK2
1093 // --------------------------------------------------------
1095 module \$logic_or (A, B, Y);
1097 parameter A_SIGNED = 0;
1098 parameter B_SIGNED = 0;
1099 parameter A_WIDTH = 0;
1100 parameter B_WIDTH = 0;
1101 parameter Y_WIDTH = 0;
1103 input [A_WIDTH-1:0] A;
1104 input [B_WIDTH-1:0] B;
1105 output [Y_WIDTH-1:0] Y;
1108 if (A_SIGNED && B_SIGNED) begin:BLOCK1
1109 assign Y = $signed(A) || $signed(B);
1110 end else begin:BLOCK2
1117 // --------------------------------------------------------
1119 module \$slice (A, Y);
1121 parameter OFFSET = 0;
1122 parameter A_WIDTH = 0;
1123 parameter Y_WIDTH = 0;
1125 input [A_WIDTH-1:0] A;
1126 output [Y_WIDTH-1:0] Y;
1128 assign Y = A >> OFFSET;
1132 // --------------------------------------------------------
1134 module \$concat (A, B, Y);
1136 parameter A_WIDTH = 0;
1137 parameter B_WIDTH = 0;
1139 input [A_WIDTH-1:0] A;
1140 input [B_WIDTH-1:0] B;
1141 output [A_WIDTH+B_WIDTH-1:0] Y;
1147 // --------------------------------------------------------
1149 module \$mux (A, B, S, Y);
1151 parameter WIDTH = 0;
1153 input [WIDTH-1:0] A, B;
1155 output reg [WIDTH-1:0] Y;
1166 // --------------------------------------------------------
1168 module \$pmux (A, B, S, Y);
1170 parameter WIDTH = 0;
1171 parameter S_WIDTH = 0;
1173 input [WIDTH-1:0] A;
1174 input [WIDTH*S_WIDTH-1:0] B;
1175 input [S_WIDTH-1:0] S;
1176 output reg [WIDTH-1:0] Y;
1179 reg found_active_sel_bit;
1183 found_active_sel_bit = 0;
1184 for (i = 0; i < S_WIDTH; i = i+1)
1186 Y = found_active_sel_bit ? 'bx : B >> (WIDTH*i);
1187 found_active_sel_bit = 1;
1193 // --------------------------------------------------------
1194 `ifndef SIMLIB_NOLUT
1196 module \$lut (A, Y);
1198 parameter WIDTH = 0;
1201 input [WIDTH-1:0] A;
1204 wire lut0_out, lut1_out;
1207 if (WIDTH <= 1) begin:simple
1208 assign {lut1_out, lut0_out} = LUT;
1209 end else begin:complex
1210 \$lut #( .WIDTH(WIDTH-1), .LUT(LUT ) ) lut0 ( .A(A[WIDTH-2:0]), .Y(lut0_out) );
1211 \$lut #( .WIDTH(WIDTH-1), .LUT(LUT >> (2**(WIDTH-1))) ) lut1 ( .A(A[WIDTH-2:0]), .Y(lut1_out) );
1214 if (WIDTH > 0) begin:lutlogic
1216 casez ({A[WIDTH-1], lut0_out, lut1_out})
1219 3'b0??: Y = lut0_out;
1220 3'b1??: Y = lut1_out;
1230 // --------------------------------------------------------
1232 module \$sop (A, Y);
1234 parameter WIDTH = 0;
1235 parameter DEPTH = 0;
1236 parameter TABLE = 0;
1238 input [WIDTH-1:0] A;
1246 for (i = 0; i < DEPTH; i=i+1) begin
1248 for (j = 0; j < WIDTH; j=j+1) begin
1249 if (TABLE[2*WIDTH*i + 2*j + 0] && A[j]) match = 0;
1250 if (TABLE[2*WIDTH*i + 2*j + 1] && !A[j]) match = 0;
1258 // --------------------------------------------------------
1260 module \$tribuf (A, EN, Y);
1262 parameter WIDTH = 0;
1264 input [WIDTH-1:0] A;
1266 output [WIDTH-1:0] Y;
1268 assign Y = EN ? A : 'bz;
1272 // --------------------------------------------------------
1274 module \$assert (A, EN);
1278 `ifndef SIMLIB_NOCHECKS
1280 if (A !== 1'b1 && EN === 1'b1) begin
1281 $display("Assertion %m failed!");
1289 // --------------------------------------------------------
1291 module \$assume (A, EN);
1295 `ifndef SIMLIB_NOCHECKS
1297 if (A !== 1'b1 && EN === 1'b1) begin
1298 $display("Assumption %m failed!");
1306 // --------------------------------------------------------
1308 module \$predict (A, EN);
1314 // --------------------------------------------------------
1316 module \$equiv (A, B, Y);
1321 assign Y = (A !== 1'bx && A !== B) ? 1'bx : A;
1323 `ifndef SIMLIB_NOCHECKS
1325 if (A !== 1'bx && A !== B) begin
1326 $display("Equivalence failed!");
1334 // --------------------------------------------------------
1337 module \$sr (SET, CLR, Q);
1339 parameter WIDTH = 0;
1340 parameter SET_POLARITY = 1'b1;
1341 parameter CLR_POLARITY = 1'b1;
1343 input [WIDTH-1:0] SET, CLR;
1344 output reg [WIDTH-1:0] Q;
1346 wire [WIDTH-1:0] pos_set = SET_POLARITY ? SET : ~SET;
1347 wire [WIDTH-1:0] pos_clr = CLR_POLARITY ? CLR : ~CLR;
1351 for (i = 0; i < WIDTH; i = i+1) begin:bitslices
1352 always @(posedge pos_set[i], posedge pos_clr[i])
1355 else if (pos_set[i])
1363 // --------------------------------------------------------
1365 module \$dff (CLK, D, Q);
1367 parameter WIDTH = 0;
1368 parameter CLK_POLARITY = 1'b1;
1371 input [WIDTH-1:0] D;
1372 output reg [WIDTH-1:0] Q;
1373 wire pos_clk = CLK == CLK_POLARITY;
1375 always @(posedge pos_clk) begin
1381 // --------------------------------------------------------
1383 module \$dffe (CLK, EN, D, Q);
1385 parameter WIDTH = 0;
1386 parameter CLK_POLARITY = 1'b1;
1387 parameter EN_POLARITY = 1'b1;
1390 input [WIDTH-1:0] D;
1391 output reg [WIDTH-1:0] Q;
1392 wire pos_clk = CLK == CLK_POLARITY;
1394 always @(posedge pos_clk) begin
1395 if (EN == EN_POLARITY) Q <= D;
1400 // --------------------------------------------------------
1403 module \$dffsr (CLK, SET, CLR, D, Q);
1405 parameter WIDTH = 0;
1406 parameter CLK_POLARITY = 1'b1;
1407 parameter SET_POLARITY = 1'b1;
1408 parameter CLR_POLARITY = 1'b1;
1411 input [WIDTH-1:0] SET, CLR, D;
1412 output reg [WIDTH-1:0] Q;
1414 wire pos_clk = CLK == CLK_POLARITY;
1415 wire [WIDTH-1:0] pos_set = SET_POLARITY ? SET : ~SET;
1416 wire [WIDTH-1:0] pos_clr = CLR_POLARITY ? CLR : ~CLR;
1420 for (i = 0; i < WIDTH; i = i+1) begin:bitslices
1421 always @(posedge pos_set[i], posedge pos_clr[i], posedge pos_clk)
1424 else if (pos_set[i])
1434 // --------------------------------------------------------
1436 module \$adff (CLK, ARST, D, Q);
1438 parameter WIDTH = 0;
1439 parameter CLK_POLARITY = 1'b1;
1440 parameter ARST_POLARITY = 1'b1;
1441 parameter ARST_VALUE = 0;
1444 input [WIDTH-1:0] D;
1445 output reg [WIDTH-1:0] Q;
1446 wire pos_clk = CLK == CLK_POLARITY;
1447 wire pos_arst = ARST == ARST_POLARITY;
1449 always @(posedge pos_clk, posedge pos_arst) begin
1458 // --------------------------------------------------------
1460 module \$dlatch (EN, D, Q);
1462 parameter WIDTH = 0;
1463 parameter EN_POLARITY = 1'b1;
1466 input [WIDTH-1:0] D;
1467 output reg [WIDTH-1:0] Q;
1470 if (EN == EN_POLARITY)
1476 // --------------------------------------------------------
1479 module \$dlatchsr (EN, SET, CLR, D, Q);
1481 parameter WIDTH = 0;
1482 parameter EN_POLARITY = 1'b1;
1483 parameter SET_POLARITY = 1'b1;
1484 parameter CLR_POLARITY = 1'b1;
1487 input [WIDTH-1:0] SET, CLR, D;
1488 output reg [WIDTH-1:0] Q;
1490 wire pos_en = EN == EN_POLARITY;
1491 wire [WIDTH-1:0] pos_set = SET_POLARITY ? SET : ~SET;
1492 wire [WIDTH-1:0] pos_clr = CLR_POLARITY ? CLR : ~CLR;
1496 for (i = 0; i < WIDTH; i = i+1) begin:bitslices
1500 else if (pos_set[i])
1510 // --------------------------------------------------------
1512 module \$fsm (CLK, ARST, CTRL_IN, CTRL_OUT);
1514 parameter NAME = "";
1516 parameter CLK_POLARITY = 1'b1;
1517 parameter ARST_POLARITY = 1'b1;
1519 parameter CTRL_IN_WIDTH = 1;
1520 parameter CTRL_OUT_WIDTH = 1;
1522 parameter STATE_BITS = 1;
1523 parameter STATE_NUM = 1;
1524 parameter STATE_NUM_LOG2 = 1;
1525 parameter STATE_RST = 0;
1526 parameter STATE_TABLE = 1'b0;
1528 parameter TRANS_NUM = 1;
1529 parameter TRANS_TABLE = 4'b0x0x;
1532 input [CTRL_IN_WIDTH-1:0] CTRL_IN;
1533 output reg [CTRL_OUT_WIDTH-1:0] CTRL_OUT;
1535 wire pos_clk = CLK == CLK_POLARITY;
1536 wire pos_arst = ARST == ARST_POLARITY;
1538 reg [STATE_BITS-1:0] state;
1539 reg [STATE_BITS-1:0] state_tmp;
1540 reg [STATE_BITS-1:0] next_state;
1542 reg [STATE_BITS-1:0] tr_state_in;
1543 reg [STATE_BITS-1:0] tr_state_out;
1544 reg [CTRL_IN_WIDTH-1:0] tr_ctrl_in;
1545 reg [CTRL_OUT_WIDTH-1:0] tr_ctrl_out;
1550 input [31:0] tr_num;
1552 reg [STATE_NUM_LOG2-1:0] state_num;
1554 tr_pos = (2*STATE_NUM_LOG2+CTRL_IN_WIDTH+CTRL_OUT_WIDTH)*tr_num;
1555 tr_ctrl_out = TRANS_TABLE >> tr_pos;
1556 tr_pos = tr_pos + CTRL_OUT_WIDTH;
1557 state_num = TRANS_TABLE >> tr_pos;
1558 tr_state_out = STATE_TABLE >> (STATE_BITS*state_num);
1559 tr_pos = tr_pos + STATE_NUM_LOG2;
1560 tr_ctrl_in = TRANS_TABLE >> tr_pos;
1561 tr_pos = tr_pos + CTRL_IN_WIDTH;
1562 state_num = TRANS_TABLE >> tr_pos;
1563 tr_state_in = STATE_TABLE >> (STATE_BITS*state_num);
1564 tr_pos = tr_pos + STATE_NUM_LOG2;
1568 always @(posedge pos_clk, posedge pos_arst) begin
1570 state_tmp = STATE_TABLE[STATE_BITS*(STATE_RST+1)-1:STATE_BITS*STATE_RST];
1571 for (i = 0; i < STATE_BITS; i = i+1)
1572 if (state_tmp[i] === 1'bz)
1576 state_tmp = next_state;
1577 for (i = 0; i < STATE_BITS; i = i+1)
1578 if (state_tmp[i] === 1'bz)
1584 always @(state, CTRL_IN) begin
1585 next_state <= STATE_TABLE[STATE_BITS*(STATE_RST+1)-1:STATE_BITS*STATE_RST];
1588 // $display("Q: %b %b", state, CTRL_IN);
1589 for (i = 0; i < TRANS_NUM; i = i+1) begin
1591 // $display("T: %b %b -> %b %b [%d]", tr_state_in, tr_ctrl_in, tr_state_out, tr_ctrl_out, i);
1592 casez ({state, CTRL_IN})
1593 {tr_state_in, tr_ctrl_in}: begin
1594 // $display("-> %b %b <- MATCH", state, CTRL_IN);
1595 {next_state, CTRL_OUT} <= {tr_state_out, tr_ctrl_out};
1603 // --------------------------------------------------------
1604 `ifndef SIMLIB_NOMEM
1606 module \$memrd (CLK, EN, ADDR, DATA);
1608 parameter MEMID = "";
1609 parameter ABITS = 8;
1610 parameter WIDTH = 8;
1612 parameter CLK_ENABLE = 0;
1613 parameter CLK_POLARITY = 0;
1614 parameter TRANSPARENT = 0;
1617 input [ABITS-1:0] ADDR;
1618 output [WIDTH-1:0] DATA;
1621 if (MEMID != "") begin
1622 $display("ERROR: Found non-simulatable instance of $memrd!");
1629 // --------------------------------------------------------
1631 module \$memwr (CLK, EN, ADDR, DATA);
1633 parameter MEMID = "";
1634 parameter ABITS = 8;
1635 parameter WIDTH = 8;
1637 parameter CLK_ENABLE = 0;
1638 parameter CLK_POLARITY = 0;
1639 parameter PRIORITY = 0;
1642 input [WIDTH-1:0] EN;
1643 input [ABITS-1:0] ADDR;
1644 input [WIDTH-1:0] DATA;
1647 if (MEMID != "") begin
1648 $display("ERROR: Found non-simulatable instance of $memwr!");
1655 // --------------------------------------------------------
1657 module \$meminit (ADDR, DATA);
1659 parameter MEMID = "";
1660 parameter ABITS = 8;
1661 parameter WIDTH = 8;
1662 parameter WORDS = 1;
1664 parameter PRIORITY = 0;
1666 input [ABITS-1:0] ADDR;
1667 input [WORDS*WIDTH-1:0] DATA;
1670 if (MEMID != "") begin
1671 $display("ERROR: Found non-simulatable instance of $meminit!");
1678 // --------------------------------------------------------
1680 module \$mem (RD_CLK, RD_EN, RD_ADDR, RD_DATA, WR_CLK, WR_EN, WR_ADDR, WR_DATA);
1682 parameter MEMID = "";
1683 parameter signed SIZE = 4;
1684 parameter signed OFFSET = 0;
1685 parameter signed ABITS = 2;
1686 parameter signed WIDTH = 8;
1687 parameter signed INIT = 1'bx;
1689 parameter signed RD_PORTS = 1;
1690 parameter RD_CLK_ENABLE = 1'b1;
1691 parameter RD_CLK_POLARITY = 1'b1;
1692 parameter RD_TRANSPARENT = 1'b1;
1694 parameter signed WR_PORTS = 1;
1695 parameter WR_CLK_ENABLE = 1'b1;
1696 parameter WR_CLK_POLARITY = 1'b1;
1698 input [RD_PORTS-1:0] RD_CLK;
1699 input [RD_PORTS-1:0] RD_EN;
1700 input [RD_PORTS*ABITS-1:0] RD_ADDR;
1701 output reg [RD_PORTS*WIDTH-1:0] RD_DATA;
1703 input [WR_PORTS-1:0] WR_CLK;
1704 input [WR_PORTS*WIDTH-1:0] WR_EN;
1705 input [WR_PORTS*ABITS-1:0] WR_ADDR;
1706 input [WR_PORTS*WIDTH-1:0] WR_DATA;
1708 reg [WIDTH-1:0] memory [SIZE-1:0];
1711 reg [WR_PORTS-1:0] LAST_WR_CLK;
1712 reg [RD_PORTS-1:0] LAST_RD_CLK;
1714 function port_active;
1720 casez ({clk_enable, clk_polarity, last_clk, this_clk})
1721 4'b0???: port_active = 1;
1722 4'b1101: port_active = 1;
1723 4'b1010: port_active = 1;
1724 default: port_active = 0;
1730 for (i = 0; i < SIZE; i = i+1)
1731 memory[i] = INIT >>> (i*WIDTH);
1734 always @(RD_CLK, RD_ADDR, RD_DATA, WR_CLK, WR_EN, WR_ADDR, WR_DATA) begin
1735 `ifdef SIMLIB_MEMDELAY
1738 for (i = 0; i < RD_PORTS; i = i+1) begin
1739 if (!RD_TRANSPARENT[i] && RD_CLK_ENABLE[i] && RD_EN[i] && port_active(RD_CLK_ENABLE[i], RD_CLK_POLARITY[i], LAST_RD_CLK[i], RD_CLK[i])) begin
1740 // $display("Read from %s: addr=%b data=%b", MEMID, RD_ADDR[i*ABITS +: ABITS], memory[RD_ADDR[i*ABITS +: ABITS] - OFFSET]);
1741 RD_DATA[i*WIDTH +: WIDTH] <= memory[RD_ADDR[i*ABITS +: ABITS] - OFFSET];
1745 for (i = 0; i < WR_PORTS; i = i+1) begin
1746 if (port_active(WR_CLK_ENABLE[i], WR_CLK_POLARITY[i], LAST_WR_CLK[i], WR_CLK[i]))
1747 for (j = 0; j < WIDTH; j = j+1)
1748 if (WR_EN[i*WIDTH+j]) begin
1749 // $display("Write to %s: addr=%b data=%b", MEMID, WR_ADDR[i*ABITS +: ABITS], WR_DATA[i*WIDTH+j]);
1750 memory[WR_ADDR[i*ABITS +: ABITS] - OFFSET][j] = WR_DATA[i*WIDTH+j];
1754 for (i = 0; i < RD_PORTS; i = i+1) begin
1755 if ((RD_TRANSPARENT[i] || !RD_CLK_ENABLE[i]) && port_active(RD_CLK_ENABLE[i], RD_CLK_POLARITY[i], LAST_RD_CLK[i], RD_CLK[i])) begin
1756 // $display("Transparent read from %s: addr=%b data=%b", MEMID, RD_ADDR[i*ABITS +: ABITS], memory[RD_ADDR[i*ABITS +: ABITS] - OFFSET]);
1757 RD_DATA[i*WIDTH +: WIDTH] <= memory[RD_ADDR[i*ABITS +: ABITS] - OFFSET];
1761 LAST_RD_CLK <= RD_CLK;
1762 LAST_WR_CLK <= WR_CLK;
1768 // --------------------------------------------------------