Experiment 7

Tasks and Functions

CPE114L
3Q1920
Drill7_1
module task1 (temp_a, temp_b, temp_c, temp_d);
	input [7:0] temp_a, temp_c;
	output [7:0] temp_b, temp_d;
	reg [7:0] temp_b, temp_d;
	initial temp_b=0;
	task convert;
		input [7:0] temp_in;
		output [7:0] temp_out;
		begin
		temp_out = ((9*temp_in)/5)+32;
		end
	endtask
	always@(temp_a or temp_b or temp_c or temp_d)
	begin
		convert (temp_a,temp_b);
		convert (temp_c,temp_d);
	end
endmodule

module drill7_1;
	reg [7:0]temp_a, temp_c;
	wire [7:0]temp_b, temp_d;
	task1 t1(temp_a,temp_b,temp_c,temp_d);
	
	initial begin
		#1 temp_a=8'd0; temp_c=8'd32;
		#2 $display("Temperature A in Celsius = %d",temp_a);
		#2 $display("Temperature A in Fahrenheit = %d", temp_b);
		#2 $display("Temperature B in Celsius = %d",temp_c);
		#2 $display("Temperature B in Fahrenheit = %d", temp_d);
	end
endmodule


Drill7_2
module drill7_2;
	function automatic [31:0] calFactorial;
	input [7:0] number;
	begin
		if (number == 1) begin
			calFactorial=1;
		end
		else begin
			calFactorial=number*calFactorial(number-1);
		end
	end
	endfunction
	
	initial begin
		$display("Factorial of 1:%d", calFactorial(1));
		$display("Factorial of 2:%d", calFactorial(2));
		$display("Factorial of 16:%d", calFactorial(16));
	end
endmodule


Drill7_3
primitive mem(D,A,B,C);
	input A,B,C;
	output D;
	table
	 // A B C : D
		0 0 0 : 1;
		0 0 1 : 0;
		0 1 0 : 0;
		0 1 1 : 1;
		1 0 0 : 1;
		1 0 1 : 0;
		1 1 0 : 1;
		1 1 1 : 1;
	endtable
endprimitive

module drill7_3;
	integer fileno;
	reg [255:0] mem[0:255];
	reg A,B,C;
	wire D;
	integer x;
	reg [255:0] data;
	initial x=0;
	
	mem (D,A,B,C);
	initial begin
//Disregard older syntax shown to you during discussion
//Use the following syntax instead
//file handle is fileno, and “memory.txt” is the file to be opened
//memory.txt is expected to be saved within the current working directory of
//drill7_3.vl.
		fileno=$fopen("memory.txt");
		A=1'B0; B=1'B0; C=1'B0;
		#1 $display("Saving truth table to file...");
		#1 $fdisplay(fileno,"%b%b%b%b",A,B,C,D);
		#1 A=1'B0; B=1'B0; C=1'B1;
		#1 $fdisplay(fileno,"%b%b%b%b",A,B,C,D);
		#1 A=1'B0; B=1'B1; C=1'B0;
		#1 $fdisplay(fileno,"%b%b%b%b",A,B,C,D);
		#1 A=1'B0; B=1'B1; C=1'B1;
		#1 $fdisplay(fileno,"%b%b%b%b",A,B,C,D);
		#1 A=1'B1; B=1'B0; C=1'B0;
		#1 $fdisplay(fileno,"%b%b%b%b",A,B,C,D);
		#1 A=1'B1; B=1'B0; C=1'B1;
		#1 $fdisplay(fileno,"%b%b%b%b",A,B,C,D);
		#1 A=1'B1; B=1'B1; C=1'B0;
		#1 $fdisplay(fileno,"%b%b%b%b",A,B,C,D);
		#1 A=1'B1; B=1'B1; C=1'B1;
		#1 $fdisplay(fileno,"%b%b%b%b",A,B,C,D);
		#1 $fclose(fileno);
		#1 $display("Retrieving truth table from file..");
		#1 $readmemb("memory.txt",mem);
		#1 while (x!=8)
			begin
				data=mem[x];
				$display("%b %b %b : %b", data[3], data[2], data[1], data[0]);
				x=x+1;
			end
	end
endmodule


Drill7_4
module drill7_4;

task borderopen;
begin
	$display("****************************************************");
	repeat(8)
	$display("***                                              ***");
end
endtask

task borderclose;
begin
	repeat(8)
	$display("***                                              ***");
	$display("****************************************************");
end
endtask

	reg [3:0]x;
	initial begin
		$monitor("\t\t\t",x);
		#1 borderopen;
		#1 $display("\t This is a set of random values\n");
		repeat (4)
		#1 x=$random;
		#1 borderclose;
		#1 $stop;
		#1 borderopen;
		#1 $display("\tThis is a new set of random values\n");
		repeat (4)
		#1 x=$random;
		#1 borderclose;
		#1 $stop;
		#20 $finish;
	end
endmodule


Exercise7_1
module operation(inX, X, n, disp, Q);
	input [3:0] inX;
	output reg [3:0] Q;
	input X;
	input [3:0] n;
	reg [3:0] ntmp;
	reg tmpN;
	integer fileHandle;
	reg [255:0] mem [0:255];
	output reg [255:0] disp;

	initial Q=0;

	always @(X, n, inX) begin
		ntmp = n;
		if(X==1) begin
			Q = inX;
			fileHandle = $fopen("exercise7_1.txt");
			while(ntmp!=0) begin
				Q = Q + 1;
				ntmp = ntmp-1;
			end
			disp="Do Increment and then Save  |";
			$fwrite(fileHandle, "%b", Q);
			$fclose(fileHandle);
		end
		else begin
			mem[0] = 0;
			disp="Do Read, Rotate, and Update |";
			$readmemb("exercise7_1.txt", mem, mem[0], mem[3]);
			Q = mem[0];
			repeat(ntmp) begin
				tmpN = Q[3];
				Q = Q << 1;
				Q[0] = tmpN;
			end
			fileHandle = $fopen("exercise7_1.txt");
			$fwrite(fileHandle, "%b", Q);
			$fclose(fileHandle);
		end
	end
endmodule

module exercise7_1;
	reg [3:0] inX;
	reg X;
	reg [3:0] n;
	wire [3:0] Q;
	wire [255:0] disp;

	operation tb(inX, X, n, disp, Q);

	initial begin
	X=1; n=1; inX=2;
	$display("       _________________________________________________________________________ ");
	$display("      |                                                                         |");
	$display("      |                           4-Bit Circulation                             |");
	$display("      |                                                                         |");
	$display("      |       Operations:                                                       |");
	$display("      |       If X=0, Do Read from txt file then rotate to left it n times      |");
	$display("      |       If X=1, Do Increment data it n times and save to txt file         |");
	$display("      |       Legend: D - Decimal, B - Binary                                   |");
	$display("      |_________________________________________________________________________|");
	$display("      |   |   |                  |                |                             |");
	$display("      | X | n |  inX(D)   inX(B) | Out(D) Out(B)  |             Msg             |");
	$monitor("      | %b |%d |   %d       %b  |  %d     %b   | %0s", X, n, inX, inX, Q, Q, disp);
	#2 X=0; n=3;
	#2 X=1; n=5; inX=4;
	#2 X=0; n=3;
	#2 X=1; n=5; inX=6;
	#2 X=0; n=1;
	#2 $display("      |___|___|__________________|________________|_____________________________|");
	#2 $finish;
	
	end
endmodule


Exercise7_2
module exercise7_2();
function automatic [31:0] product;
	input [7:0] num1;
	input [7:0] num2;
 
	begin
  
		if(num2 > 0)
		begin 
			product = num1 + product(num1, num2-1);
			end
		else
		begin
			product = 0;
		end  
	end
endfunction
 
	initial begin
		$display("    ____________________________________________________ ");
		$display("   |                                                    |");
		$display("   |       Multiplication using Recursive Addition      |");
		$display("   |____________________________________________________|");
		$display("   |                  |              |                  |");
		$display("   |   Multiplicand   |  Multiplier  |     Product      |");
		$display("   |        5         |      4       |%d", product(5,4), "        |");
		$display("   |        7         |      2       |%d", product(7,2), "        |");
		$display("   |        8         |      3       |%d", product(8,3), "        | ");
		$display("   |__________________|______________|__________________|");
		$display("");
	end
endmodule 


Laboratory Report: Click here to download the file