`EE3755            Verilog Homework 3  Due: TBA `
`///`

Estimated time to finish:

Prob.1 : 50Mins.

Prob.2 : 10Min.

Prob.3:  30Min.

Total : 90 Mins..

When you submit the Hw., Please write down how much time did you spend for each problem.

(No penalty for spending too little or too much time. Just want to know how long you spent.)

How to submit: Hard copy during the class.

Use “script “ command to take a snap shot of your program.

After run “script “

Use “cat” command to display your program.

Use “ncverilog” to run your program.

then stop the script.

` `
`Problem 1 : Rewrite or Modify the testbench program.`
`                   `
`            The program should test the number of ones in a.`
`            a value varies from 0 to 15.`
`            The output format may be this following:`
`                  Time, value of a at binary, value of p.`
`                  (time =  500, a = 0000 0000 0000 0000 0000 0000 0000 0000 ,p = 0; `
`                   time = 1500, a = 0000 0000 0000 0000 0000 0000 0000 0001 ,p = 1;`
` `
`          `
`            module pop_with_handshaking(p,ready,a,start,clk);`
` `
`   input [31:0] a;`
` `
`   input        start, clk;`
` `
`   output       p, ready;`
`   reg [5:0]    p;`
`   reg          ready;`
`   reg [31:0]   acopy;`
` `
`   initial ready = 1;`
`   always @( posedge clk )`
`     begin`
`        if( start )`
`          begin`
`             acopy = a;`
`             p = 0;`
`             ready = 0;`
`          end`
`        else if( !ready && acopy )`
` `
`          begin`
` `
`             p = p + acopy[0];`
` `
`             acopy = acopy >> 1;`
` `
`          end`
` `
`        else if( !ready && !acopy )`
` `
`          begin`
` `
`             ready = 1;//output is ready,also means the module is`
` `
`                      // ready to accept new input.`
` `
`          end`
` `
`     end`
` `
` `
` `
`endmodule`
` `
`// Solution...Modify this or Rewrite . `
`// Test bench or Stimulus Block`
`// (This is  not working)`
`// Giving this to help you.`
`module stimulus;`
` `
`reg clk;`
`// some declarations are wrong.//reg or wire..  `
`reg reset;`
`integer i;`
`wire [3:0] q;`
`wire [5:0] p1;`
`reg ready,start; `
`reg [31:0] a;`
` `
`pop_with_handshaking pp1(p1,ready,a,start,clk);`
` `
` `
` `
`initial`
` `
`            clk = 1'b0; //set clk to 0`
` `
`always`
` `
`            #5 clk = ~clk; // toggle clk every 5 time units`
` `
` `
`initial`
` begin`
`    `
`    for(i = 0; i<16 ;i= i+1) `
`     begin`
`       a = i[5:0];`
`       //should also give “start” and others.`
`       #500;`
`     end  `
`          `
`    #50000 \$finish;     `
`   `
`end`
` `
` `
`// Monitor the outputs`
` `
`initial`
`            \$monitor(\$time, " input a = %b  output p = %d",a,p1);`
`            //may use \$display(   )`
` `
`endmodule`
` `
`//`
` `
`//`
`reg reset;`
`integer i;`
`wire [3:0] q;`
`wire [5:0] p1;`
`wire ready;`
`reg  start; `
`reg [31:0] a;`
` `
`pop_with_handshaking pp1(p1,ready,a,start,clk);`
` `
` `
` `
`initial`
` `
`            clk = 1'b0; //set clk to 0`
` `
`always`
` `
`            #5 clk = ~clk; // toggle clk every 5 time units`
` `
` `
` `
` `
`initial`
` begin`
`    `
`    for(i = 0; i<16 ;i= i+1) `
`     begin`
`       a = i[5:0];`
`       start = 1;`
`       #500;`
`       start = 0;`
`       if(ready ==0)`
`   begin`
`   #500;`
`    end`
`    `
`       \$display(\$time," input a = %b output p = %d",a,p1);`
`     end  `
`          `
`    #50000 \$finish;     `
`   `
`end`
`//NOT A GOOD ONE BUT WORKING..`
`##########################`
`1000 input a = 00000000000000000000000000000000 output p =  0`
`                2000 input a = 00000000000000000000000000000001 output p =  1`
`                3000 input a = 00000000000000000000000000000010 output p =  1`
`                4000 input a = 00000000000000000000000000000011 output p =  2`
`                5000 input a = 00000000000000000000000000000100 output p =  1`
`                6000 input a = 00000000000000000000000000000101 output p =  2`
`                7000 input a = 00000000000000000000000000000110 output p =  2`
`                8000 input a = 00000000000000000000000000000111 output p =  3`
`                9000 input a = 00000000000000000000000000001000 output p =  1`
`               10000 input a = 00000000000000000000000000001001 output p =  2`
`               11000 input a = 00000000000000000000000000001010 output p =  2`
`               12000 input a = 00000000000000000000000000001011 output p =  3`
`               13000 input a = 00000000000000000000000000001100 output p =  2`
`               14000 input a = 00000000000000000000000000001101 output p =  3`
`               15000 input a = 00000000000000000000000000001110 output p =  3`
`               16000 input a = 00000000000000000000000000001111 output p =  4`
` `
` `
` `
` `

Problem 2: Convert the following numbers:

Hint (5/8) = 0.625.

a) Decimal  10 to 8-bit Binary:

00001010

b) Decimal -10     to 8-bit Binary:

11110110

c) Decimal 10.625  to Binary (as many bits as needed):

1010.101

d) Decimal -10.625  to IEEE 754 Single Precision (Show in hexadecimal):

C12A0000

Problem 3: Precision in IEEE 754.

1.    you have a floating point number:0.625.

Can you represent this in IEEE 754 single precision(easy one).

Yes.

2.    you have a floating point number: 0.123456789012345678901234567.

b1) can you represent this in IEEE 754 single precision without losing anything?

No.

It has only 24 bits(23 + 1 (hidden 1)) for mantissa

It can not represent very long digit number without losing precision.

b2) how many digits of the number can  the IEEE 754 single precision represent?

It will represent 7 digits.

Or It will precisely represent up to 6 digits.

(0.123457//if you run C program, it will round up. 0.12345678…->0.123457)

24 bits represents up to 16777216(2^24) -1.(part of 8 digit numbers //so 7 digits).

Or..log(2^24) ~=7.22 (So 7 digits).

when you are writing a program, assigning “a = 1.23456789” does not gain

anything if you use IEEE754 single.

And also adding 1234567 + 0.1234567 does not make any sense because of precision.

(Hint: C++ setw(),setprecision()

you don’t have to know the above commands.

Is setprecision(20) useful?).