4-bit carry look-ahead adder

Scroll down to browse the source code.

Share with your network:

bin dec
hex step
b'/* Copyright (c) 2014, Fortylines LLC\n All rights reserved.\n\n Redistribution and use in source and binary forms, with or without\n modification, are permitted provided that the following conditions are met:\n\n 1. Redistributions of source code must retain the above copyright notice,\n this list of conditions and the following disclaimer.\n 2. Redistributions in binary form must reproduce the above copyright\n notice, this list of conditions and the following disclaimer in the\n documentation and/or other materials provided with the distribution.\n\n THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED\n TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR\n CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\n EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\n PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;\n OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\n WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR\n OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n/*\n This file implements a 4-bit carry look-ahead adder bundled with an exhaustive\n testbench.\n\n Simulate with iverilog:\n\n $ iverilog 4-bit-carry-look-ahead-adder.v\n $ ./a.out\n\n*/\n\nmodule sum(\n output result,\n input left, right, carry_in\n);\n assign result = left ^ right ^ carry_in;\nendmodule // sum\n\n\nmodule carry_block_4bit(\n output [3:0] carry,\n input [3:0] left, right,\n input carry_in\n);\n\n wire [3:0] gen, prp; /* generate and propagate */\n\n assign gen[0] = left[0] & right[0];\n assign prp[0] = left[0] ^ right[0];\n assign gen[1] = left[1] & right[1];\n assign prp[1] = left[1] ^ right[1];\n assign gen[2] = left[2] & right[2];\n assign prp[2] = left[2] ^ right[2];\n assign gen[3] = left[3] & right[3];\n assign prp[3] = left[3] ^ right[3];\n assign carry[0] = gen[0] | (prp[0] & carry_in);\n assign carry[1] = gen[1] | prp[1]\n & (gen[0] | (prp[0] & carry_in));\n assign carry[2] = gen[2] | prp[2]\n & (gen[1] | prp[1] & (gen[0] | (prp[0] & carry_in)));\n assign carry[3] = gen[3] | prp[3]\n & (gen[2] | prp[2] & (gen[1] | prp[1] & (gen[0] | (prp[0] & carry_in))));\n\nendmodule // carry_block_4bit\n\n\nmodule carry_lookahead_adder_4bit(\n output [3:0] sum,\n output carry_out,\n input [3:0] left, right,\n input carry_in\n);\n wire [4:1] carry;\n\n carry_block_4bit b0(carry[4:1], left[3:0], right[3:0], carry_in);\n sum a0(sum[0], left[0], right[0], carry_in);\n sum a1(sum[1], left[1], right[1], carry[1]);\n sum a2(sum[2], left[2], right[2], carry[2]);\n sum a3(sum[3], left[3], right[3], carry[3]);\n assign carry_out = carry[4];\n\nendmodule // carry_lookahead_adder\n\n\nmodule test_adder_4bit;\n // inputs\n reg [3:0] left;\n reg [3:0] right;\n reg carry_in;\n // outputs\n wire [3:0] sum;\n wire cary_out;\n\n carry_lookahead_adder_4bit adder(\n .sum(sum),\n .carry_out(carry_out),\n .left(left),\n .right(right),\n .carry_in(carry_in)\n );\n\n initial begin\n left = 0;\n right = 0;\n carry_in = 0;\n\n #50; // wait for reset to complete\n\n // exhaustive testing, going through all combinations.\n while (left < 15) begin\n while (right < 15) begin\n #10 right = right + 1;\n end\n #10 left = left + 1; right = right + 1;\n end\n while (right < 15) begin\n #10 right = right + 1;\n end\n #10 left = left + 1; right = right + 1; carry_in = 1;\n while (left < 15) begin\n while (right < 15) begin\n #10 right = right + 1;\n end\n #10 left = left + 1; right = right + 1;\n end\n while (right < 15) begin\n #10 right = right + 1;\n end\n\n end // initial begin\n\n initial begin\n $monitor("time=", $time,,\n "left=%b right=%b carry_in=%b : sum=%b carry_out=%b",\n left, right, carry_in, sum, carry_out);\n /* The following statement will (by default) create a dump.vcd\n when the testbench is executed by iverilog. */\n $dumpvars(0, test_adder_4bit);\n end\n\nendmodule // test_adder_4bit\n'