I can provide the zip files later.
CS-UY
2
2
1
4
â Homework 2
Jeff Epstein
Introduction
Complete the following exercises. When you finish, submit your solutions on Gradescope.
Submit your code files exactly as named in the questions.
Problems
1. If you havenât yet done so, complete the Verilog tutorial, which you can find on Classes, in the Resources
section, in the âguidesâ folder. You donât have to submit anything: this is just to familiarize you with
the tools.
You may want to look at the Verilog cheat sheet and reference manual, also available on Classes.
2. Download and extract the enclosed files from hw2a.zip:
⢠full_adder_nodelay.v â a full one-bit adder module
⢠four_bit_adder_subtractor.v â a (broken) four-bit ripple-carry adder/subtractor, which uses
the adder in the above file
⢠adder_subtractor_test.v â a test bench that will verify the behavior of the above module
To run the test bench, compile adder_subtractor_test.v and run the resulting binary in the simu-
lator. View the resulting waveform in GTKWave. The following commands will do it:
iverilog -o adder_subtractor_test.vvp adder_subtractor_test.v
vvp adder_subtractor_test.vvp
gtkwave adder_subtractor_test.vcd
The four_bit_adder_subtractor module should be able to add and subtract four bit numbers. As
provided, however, it can currently only add.
The four_bit_adder_subtractor module takes three inputs:
⢠A â the first input (four bit)
⢠B â the second input (four bit)
⢠Op â the operation (one bit); this will be 0 for addition and 1 for subtraction. Initially, it is
ignored within the module.
And it yields two outputs:
⢠S â the sum (four bit)
⢠Cout â the carry out (one bit)
1
When you run the Verilog simulator, the test bench will notify you if the adder produces unexpected
results. Initially, you should see the following output:
When subtracting 1 and 1, got 2, but expected 0
When subtracting 2 and
5
, got 7, but expected –
3
When subtracting 4 and -4, got 0, but expected -8
When subtracting 7 and 7, got -2, but expected 0
(The numbers might seem a little funny: the test bench claims that 4 minus â4 should yield â8. This
is because in a 4-bit 2âs complement number, itâs impossible to represent 8, the mathemtically correct
solution. The bit pattern 1000 will be interpreted as â8.)
By modifying the four_bit_adder_subtractor.v file, make it support both addition and subtraction.
Do not modify the other files. Your solution must be consistent with the addition algorithm we
discussed in class. You may not use Verilogâs built-in arithmetic operators. You may use the conditional
operator (?:) as a multiplexer. You can use all the basic gates (&, |, ^, â¼). If youâve done it correctly,
the test bench shouldnât print out any unexpected results.
Submit your corrected four_bit_adder_subtractor.v file, as well as a screenshot (named
four_bit_adder_subtractor or four_bit_adder_subtractor ) showing the output of
GTKWave when viewing the waveform of ports on the corrected four_bit_adder_subtractor module.
Make sure that your waveform doesnât contain any red (unknown) values.
3. Write a combinatorial Verilog module binary_to_gray. Its input, B, is an unsigned 4-bit binary
number. Its 4-bit output, G, is the corresponding gray code. Use only Verilogâs low-level logical
operations (operators |, &, ^, â¼) in your answer.
Your module with strt like this:
module binary_to_gray(B, G);
Be sure to declare your ports as input or output; declare any additional wires you may need; and use
assign statements to connect ports to gates.
One way to convert from binary to gray code is to observe the following rules:
⢠The MSB (most significant bit) of the output is equal to the MSB of the input.
⢠Bit i of the output is obtained by XOR-ing together bits i and i + 1 of the input (i.e. that bit,
and the one to its left).
Put your code in a file named binary_to_gray.v. Test your code using the binary_to_gray_tb.v
test bench file provided in hw2b.zip. Note that the test bench does not automatically verify the test
cases; youâll need to look at the waveform in GTKWave in order to determine if your output is correct.
The correct gray codes are given in binary on the Wikipedia page linked above.
Submit your source file (named binary_to_gray.v) and a screenshot of GTKWave showing the wave-
forms of your moduleâs ports (named binary_to_gray or binary_to_gray ). Make sure that
your waveform doesnât contain any red (unknown) values.
4. Download the file hw2c.zip.
Using the ALU circuit diagram that we discussed in class as a guide, implement a combinatorial 4-
bit Arithmetic Logic Unit in Verilog. Your module must be functionally identical to the unit design
presented in class: it must support a 3-bit operation input that selects one of the following operations:
A & B, A | B, A + B, A & â¼B, A | â¼B, A – B, A < B. The inputs and output of the unit should be
4-bit values.
2
https://en.wikipedia.org/wiki/Gray_code
A circuit diagram of the ALU is available in the zip file, as well as in the class slides.
In your implementation, do not use the high-level Verilog operators +, -, <, etc. Instead, implement the ALUâs logic as given in the diagram, using low-level gate logic (operators |, &, ^, â¼) and an adder. Use the included four_bit_adder module to provide the adder functionality. You should not need more than one instance of this adder. You may use the conditional operator (?:) as a multiplexer. You can nest several instances of the conditional operator to simulate a larger multiplexer.
Use the included alu.v to get started. Test your code using the included Verilog testbench alu_tb.v.
Submit your completed alu.v.
Hint: to create an instance of four_bit_adder inside your module, you can use syntax similar to the
following. Below, weâre creating an instance of four_bit_adder named the_adder; the name does not
matter. We create a 4-bit wire named adder_sum and a 1-bit wire named adder_Cout for the sum and
carry out of the adder, respectively, which we connect to the adder. You are responsible for providing
reasonable values for A, B, and carry in.
wire [3:0] adder_sum;
wire adder_Cout;
four_bit_adder the_adder(A, B, 0, adder_sum , adder_Cout );
Hint: you can use the ?: syntax to select from one of several options. For example:
// X will be 3 when Y is 0, and 4 otherwise.
assign X = (Y==0) ? 3 : 4;
// Z will be 3 when Y is 0, and 4 when Y is 2, and 5 otherwise.
assign Z = (Y==0) ? 3 : (Y==2) ? 4 : 5;
You already know the OR, AND, and XOR operations as applied to logical (i.e. true/false) inputs. For
example, True and False == False.
These operations can also apply to pairs of n-bit binary numbers a and b. In this case, the numbers will
be treated as a sequence of bits a(nâ1)…0 and b(nâ1)…0. The result of a op b will be the sequence of bits
arising from applying operation op to pairs of bits from each operand, i.e. anâ1 op bnâ1, anâ2 op bnâ2, …,
a0 op b0. When these operations are applied to sequences of bits, they are called bitwise operations.
In C and C++, the & operator is bitwise AND, | is bitwise OR, ^ is bitwise XOR, and â¼ is bitwise
NOT. (Distinguish these bitwise operators from their logical/boolean counterparts: && for logical AND; ||
for logical OR; and ! for logical NOT.)
For example, letâs say we want to calculate 2510 | 510, i.e. the bitwise OR of 25 and 5. That is, in decimal:
2 5
| 5
?
First, letâs convert both numbers to binary. Here, we write 8 bits for each number:
0 0 0 1 1 0 0 1 = 2510
| 0 0 0 0 0 1 0 1 = 510
?
To calculate the answer, we apply the OR operation to the two bits in each column, yielding:
3
0 0 0 1 1 0 0 1 = 2510
| 0 0 0 0 0 1 0 1 = 510
0 0 0 1 1 1 0 1
That value, 000111012, can be converted into decimal:
0 0 0 1 1 0 0 1 = 2510
| 0 0 0 0 0 1 0 1 = 510
0 0 0 1 1 1 0 1 = 2910
Therefore 2510 | 510 = 2910.
The unary NOT operation can also be applied bitwise, in which case each of the bits of the input will be
inverted. For example, to calculate â¼ 2510, we convert the value into binary, yielding 000110012. Note that
weâve written the number with 8 bits. Now we invert each bit, giving us 111001102, which is 23010.
For the following questions, use the C/C++ bit-twiddling operators &, |, ^, and â¼. You may also find
the operators << and >> helpful.
You donât need a compiler to verify your work. You should be able to complete the functions based on
your own calculations.
The following diagram, showing the terminology used to name individual bits in an 8-bit representation
of the decimal number 41, may help:
5. Complete the following C function, which will return a value equal to its parameter, with the third
least significant bit flipped on. That is, flipOnBit3(90) should return 94, but flipOnBit3(45) should
return 45, since the third least significant bit is already on in 45.
unsigned int flipOnBit3(unsigned int x) {
return YOUR CODE HERE;
}
Your solution should be one line. Submit your answer in a plain text file named hw2.txt. Number
your answer with the question number.
6. Complete the following C function, which will return a value equal to its parameter, with the second
least significant bit toggled, i.e. if the second least significant bit is on, turn it off, and if it is off, turn
it on. That is, toggleBit2(90) should return 88, but toggleBit2(45) should return 47.
Hint: for this question, you probably want to use the xor operator (^).
unsigned int toggleBit2(unsigned int x) {
return YOUR CODE HERE;
}
Your solution should be one line. Submit your answer in a plain text file named hw2.txt. Number
your answer with the question number.
4
https://en.wikipedia.org/wiki/Bitwise_operations_in_C#Bitwise_operators
7. Complete the following C function, which will return a value equal to its parameter, with the fifth least
significant bit flipped off. That is, flipOffBit5(90) should return 74, but flipOffBit5(45) should
return 45, since the fifth least significant bit is already off in 45.
Do not make any assumptions about the size of an unsigned int. That is, your solution should work
equally well running on an architecture where unsigned ints are 16-bit, 32-bit, or 64-bit.
unsigned int flipOffBit5(unsigned int x) {
return YOUR CODE HERE;
}
Your solution should be one line. Submit your answer in a plain text file named hw2.txt. Number
your answer with the question number.
8. Your friend Rupert often confuses the C/C++ logical AND operator (&&) with the bitwise AND
operator (&). In frustration, he decides to abandon the logical version, and use the bitwise form
exclusively. Because C and C++ use zero to represent false, and any nonzero value to represent true,
he opines, using the bitwise operator in place of the logical operator will produce equivalent results.
Is Rupert correct to make this substitution? Justify your answer with examples.
5
We provide professional writing services to help you score straight A’s by submitting custom written assignments that mirror your guidelines.
Get result-oriented writing and never worry about grades anymore. We follow the highest quality standards to make sure that you get perfect assignments.
Our writers have experience in dealing with papers of every educational level. You can surely rely on the expertise of our qualified professionals.
Your deadline is our threshold for success and we take it very seriously. We make sure you receive your papers before your predefined time.
Someone from our customer support team is always here to respond to your questions. So, hit us up if you have got any ambiguity or concern.
Sit back and relax while we help you out with writing your papers. We have an ultimate policy for keeping your personal and order-related details a secret.
We assure you that your document will be thoroughly checked for plagiarism and grammatical errors as we use highly authentic and licit sources.
Still reluctant about placing an order? Our 100% Moneyback Guarantee backs you up on rare occasions where you aren’t satisfied with the writing.
You don’t have to wait for an update for hours; you can track the progress of your order any time you want. We share the status after each step.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
From brainstorming your paper's outline to perfecting its grammar, we perform every step carefully to make your paper worthy of A grade.
Hire your preferred writer anytime. Simply specify if you want your preferred expert to write your paper and we’ll make that happen.
Get an elaborate and authentic grammar check report with your work to have the grammar goodness sealed in your document.
You can purchase this feature if you want our writers to sum up your paper in the form of a concise and well-articulated summary.
You don’t have to worry about plagiarism anymore. Get a plagiarism report to certify the uniqueness of your work.
Join us for the best experience while seeking writing assistance in your college life. A good grade is all you need to boost up your academic excellence and we are all about it.
We create perfect papers according to the guidelines.
We seamlessly edit out errors from your papers.
We thoroughly read your final draft to identify errors.
Work with ultimate peace of mind because we ensure that your academic work is our responsibility and your grades are a top concern for us!
Dedication. Quality. Commitment. Punctuality
Here is what we have achieved so far. These numbers are evidence that we go the extra mile to make your college journey successful.
We have the most intuitive and minimalistic process so that you can easily place an order. Just follow a few steps to unlock success.
We understand your guidelines first before delivering any writing service. You can discuss your writing needs and we will have them evaluated by our dedicated team.
We write your papers in a standardized way. We complete your work in such a way that it turns out to be a perfect description of your guidelines.
We promise you excellent grades and academic excellence that you always longed for. Our writers stay in touch with you via email.