EE 3755 Homework 2 SOLUTION // Problem 0: // Write down your name and your account here // Your Name : ####### // Your Account: ####### // //Problem 1:(10pts) //Look at the memory module below and answer //The memory unit contains data and operators. // What will be the equation? //Answer : 3+4 +5 = //Data Memory Module //Look at it and give an answer for the problem 1. module memory(); reg [8:0] mem [7:0]; initial begin mem[0] = 3+256; //tagged data for 3 mem[1] = 0; //operator code for + mem[2] = 4+256; //tagged data for 4 mem[3] = 0; //operator code for + mem[4] = 5+256; //tagged data for 5 mem[5] = 2; //operator code for = mem[6] = 2; mem[7] = 2; end endmodule //You don't have to understand this code. //This module memory_read_s will read 1 bit data from memory. module memory_read_s(v,ck); input ck; output v; reg [8:0] local_v, index; reg [3:0] cnt; memory m(); reg v; initial index = 0; initial cnt = 0; always @(ck) begin local_v = m.mem[index]; v = local_v[cnt]; cnt = cnt+1; if(cnt == 9) begin cnt = 0; index = index +1; end end endmodule // ////////////////////////////////////////////////////// //////You don¡¯t have to read code below. This is a part of the testbench. ///// Another data memory for the testbench ///// you don't have to know module memory2(); reg [8:0] mem [7:0]; initial begin mem[0] = 5+256; //tagged data for 3 mem[1] = 1; //operator code for - mem[2] = 4+256; //tagged data for 4 mem[3] = 0; //operator code for + mem[4] = 1+256; //tagged data for 5 mem[5] = 2; //operator code for = mem[6] = 2; mem[7] = 2; end endmodule //Read data from memory2 module memory_read_s2(v,ck); input ck; output v; reg [8:0] local_v, index; reg [3:0] cnt; memory2 m(); reg v; initial index = 0; initial cnt = 0; always @(ck) begin local_v = m.mem[index]; v = local_v[cnt]; cnt = cnt+1; if(cnt == 9) begin cnt = 0; index = index +1; end end endmodule // //Problem 2 Complete the module so it can perform the required functionality.(65pts) module serial_taggeddata_adder(result,v,ck); input v,ck; output result; reg[8:0] local_data1,local_data2,result,temp_result; // you will save the incoming bit stream into the registers. // or you can declare more regs and save the value on them. reg[3:0] cnt; reg[2:0] operator;//add : 1 //subtract : 2 parameter op_add = 1; parameter op_sub = 2; initial temp_result = 0; initial operator = 3; // initial cnt = 0; always @(ck) #1 begin /// Write down your code here /// It should contain this code: $display("result output = %d\n",result); /// And this : #1 $stop; //Why #1 before $stop? //Answer: you give time to the simulator to do //something before it stops. /// The code length will be less than 30 lines. /// My code length is about 20 lines. /// There will be no penalty for a little bit longer code /// as long as your program produces right answer /// SOLUTION: local_data1[cnt] = v;//save incoming bit stream into local_data1 cnt = cnt+1; if(cnt == 9) begin cnt = 0; // if(local_data1 >= 256) $display("data = %d\n",local_data1-256); // if(local_data1 < 256) $display("data op = %d\n",local_data1); local_data2 = local_data1; if(local_data2 == 0) operator = op_add;//add if(local_data2 == 1) operator = op_sub;//sub if(local_data2 == 2) //"=" begin result = temp_result; $display("result output = %d\n",result); #1 $stop; end if(operator == 3) temp_result = local_data2-256; if(local_data2 != 0 && local_data2 !=1) begin if(operator != 3) begin if(operator == op_add) temp_result = temp_result + (local_data2-256);//add if(operator == op_sub) temp_result = temp_result - (local_data2-256);//sub end end end end endmodule ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////Do not modify this test_all module//////////////////////////////////////////////////// module test_all(); integer i,j; reg ck; wire [3:0] v1; wire vs,vs2; wire same; wire [8:0] result2,result; initial ck = 0; initial begin for(i = 1; i<= 80; i = i+ 1) begin #10; ck = ~ck; end end memory_read_s m_r_s(vs,ck); serial_taggeddata_adder s_a_a(result,vs,ck); memory_read_s2 m_r_s2(vs2,ck); serial_taggeddata_adder s_a_a_2(result2,vs2,ck); initial $monitor($time, " result = %d,result2 = %d",result,result2); endmodule // Problem 3: Convert the following numbers. (15pts) // 3.1) Decimal 8 to 8-bit Binary: (2pts) // Write your answer here: // 00001000 // 3.2) Decimal -8 to 8-bit Binary (2pts) // Write your answer here: // 11111000 // 3.3) Decimal 8.875 to Binary (as many bits as needed) (3pts) // Write your answer here: // 1000.111 // 3.4) Decimal -8.875 to IEEE 754 Single Precision (8pts) // (Show in hexadecimal): // Write your answer here: C10E0000