int Circuit::createSUBModule(const string &input1, const string &input2, const string &output, unsigned int numBits) { Node* node; // create input nodes for (unsigned int i = 0; i < numBits; ++i) { stringstream sstr; sstr << i; string name = input1 + "[" + sstr.str() + "]"; node = createNode(name); } for (unsigned int i = 0; i < numBits; ++i) { stringstream sstr; sstr << i; string name = input2 + "[" + sstr.str() + "]"; node = createNode(name); } // create output nodes for (unsigned int i = 0; i < numBits; ++i) { stringstream sstr; sstr << i; string name = output + "[" + sstr.str() + "]"; node = createNode(name); } // create one node and zero node Node* zeroNode = createNode("ZERO"); createZERONode(zeroNode); Node* oneNode = createNode("ONE"); createONENode(oneNode); // create c_out node Node* c_out = createNode("cout"); // create internal1 nodes for (unsigned int i = 0; i < numBits; ++i) { stringstream sstr; sstr << i; string name = output+"h"; name = name + "[" + sstr.str() + "]"; node = createNode(name); } // inverse the input2 for (unsigned int i = 0; i < numBits; ++i) { stringstream sstr; sstr << i; string name = output +"h"; name = name + "[" + sstr.str() + "]"; Node* hh = findNode(name); assert(hh != NULL); name = input2 + "[" + sstr.str() + "]"; Node* inNode2 = findNode(name); assert(inNode2 != NULL); createXOR3Node(zeroNode, oneNode, inNode2, hh); } // add input1 and the complemented input2 createADDModule(input1, output+"h", "ONE", output, "cout" ,numBits); // when you have implemented this function, // change 'return -1' to 'return 0' return 0; }
int Circuit::createSHIFTModule(const string &input, const string &output, unsigned int numBits, unsigned int numShift) { Node* node; // create input nodes for (unsigned int i = 0; i < numBits; ++i) { stringstream sstr; sstr << i; string name = input + "[" + sstr.str() + "]"; node = createNode(name); } // create output nodes for (unsigned int i = 0; i < numBits+numShift; ++i) { stringstream sstr; sstr << i; string name = output + "[" + sstr.str() + "]"; node = createNode(name); } // assign '0's to the least numShift bits Node* zeroNode = createNode("ZERO"); createZERONode(zeroNode); for (unsigned int i = 0; i < numShift; ++i) { stringstream sstr; sstr << i; string name = output + "[" + sstr.str() + "]"; Node* outNode = findNode(name); assert(outNode != NULL); createBUF1Node(zeroNode, outNode); } // assign inputs to the remaining numBits bits for (unsigned int i = numShift; i < numBits+numShift; ++i) { string name; // find input node[i-numShift] stringstream inStr; inStr << i-numShift; name = input + "[" + inStr.str() + "]"; Node* inNode = findNode(name); assert(inNode != NULL); // find output node[i] stringstream outStr; outStr << i; name = output + "[" + outStr.str() + "]"; Node* outNode = findNode(name); assert(outNode != NULL); // assign createBUF1Node(inNode, outNode); } return 0; }
int Circuit::createABSMIN5X3YModule(const string &input1, const string &input2, const string &output) { unsigned int ibits = 16; unsigned int obits = ibits + 3; string name; Node* node; stringstream sstr; // create primary inputs and outputs for (unsigned int i = 0; i < ibits; ++i) { sstr.str(""); sstr << i; name = input1 + "[" + sstr.str() + "]"; node = createNode(name); name = input2 + "[" + sstr.str() + "]"; node = createNode(name); } for (unsigned int i = 0; i < obits; ++i) { stringstream sstr; sstr << i; name = output + "[" + sstr.str() + "]"; node = createNode(name); } // 5x = (x << 2) + x // create (x << 2) const string x_shift("xs2"); createSHIFTModule(input1, x_shift, ibits, 2); // arithmetic extend (x << 2) and x to 19 bits arithmeticExtend(x_shift, ibits + 2, 1); arithmeticExtend(input1, ibits, 3); // compute (x << 2) + x into 5x node = findNode("ZERO"); if (node == NULL) { node = createNode("ZERO"); createZERONode(node); } node = createNode("not_used"); const string x_mult("5x"); createADDModule(input1, x_shift, "ZERO", x_mult, "not_used", obits); // 3y = (y << 1) + y const string y_shift("ys1"); createSHIFTModule(input2, y_shift, ibits, 1); // arithmetic extend (y << 1) and y to 19 bits arithmeticExtend(y_shift, ibits + 1, 2); arithmeticExtend(input2, ibits, 3); // compute (y << 1) + y into 3y node = findNode("ZERO"); if (node == NULL) { node = createNode("ZERO"); createZERONode(node); } node = createNode("not_used"); const string y_mult("3y"); createADDModule(input2, y_shift, "ZERO", y_mult, "not_used", obits); // Compute min(5x, 3y) const string min("min"); createMINModule(x_mult, y_mult, min, obits); // Compute abs(min) createABSModule(min, output, obits); return 0; }