A) Module listing ============================================================ // File : round.v module test; reg [11:0] x; // Simple truncation wire [7:0] trunc = x[11:4]; // Round-to-plus-infinity // Increment the integer when the fraction is not equal to zero. wire [7:0] plus_inf = (x[3:0] == 4'h0)? x[11:4] : x[11:4] + 8'h01; // Round-to-zero // Increment the integer when a negative number and the fraction is not equal to zero. wire [7:0] zero = (x[11] & (x[3:0] != 4'h0))? x[11:4] + 8'h01 : x[11:4]; // Up-magnitude round // Increment the integer when a positive number and the fraction is not equal to zero. wire [7:0] up_mag = (~x[11] & (x[3:0] != 4'h0))? x[11:4] + 8'h01 : x[11:4]; // 2's Complement round // Add fractional number 4'h8 to original number before using integer wire [11:0] temp = x[11:0] + 12'h008; wire [7:0] twos_comp = temp[11:4]; // Convergent round // Increment the integer when; a) fraction is greater than 4'h8, // or b) fraction = 4'h8 & integer is odd (lsb = 1). reg [7:0] converg; always @( x ) if (x[3:0] > 4'h8) converg = x[11:4] + 8'h01; else if ((x[3:0] == 4'h8) & (x[4])) converg = x[11:4] + 8'h01; else converg = x[11:4]; // display input and outputs initial begin x = 12'h000; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h001; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h800; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h801; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h011; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h811; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h008; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h018; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h808; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h818; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h019; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h819; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h028; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h828; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h029; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); x = 12'h829; #10; $display("x = %h trunc = %h plus_inf = %h zero = %h up_mag = %h twos_comp = %h converg = %h", x,trunc,plus_inf,zero,up_mag,twos_comp,converg); $finish; end endmodule // test module ============================================================ B) Simulation results: Compiling source file "round.v" Highest level modules: test x = 000 trunc = 00 plus_inf = 00 zero = 00 up_mag = 00 twos_comp = 00 converg = 00 x = 001 trunc = 00 plus_inf = 01 zero = 00 up_mag = 01 twos_comp = 00 converg = 00 x = 800 trunc = 80 plus_inf = 80 zero = 80 up_mag = 80 twos_comp = 80 converg = 80 x = 801 trunc = 80 plus_inf = 81 zero = 81 up_mag = 80 twos_comp = 80 converg = 80 x = 011 trunc = 01 plus_inf = 02 zero = 01 up_mag = 02 twos_comp = 01 converg = 01 x = 811 trunc = 81 plus_inf = 82 zero = 82 up_mag = 81 twos_comp = 81 converg = 81 x = 008 trunc = 00 plus_inf = 01 zero = 00 up_mag = 01 twos_comp = 01 converg = 00 x = 018 trunc = 01 plus_inf = 02 zero = 01 up_mag = 02 twos_comp = 02 converg = 02 x = 808 trunc = 80 plus_inf = 81 zero = 81 up_mag = 80 twos_comp = 81 converg = 80 x = 818 trunc = 81 plus_inf = 82 zero = 82 up_mag = 81 twos_comp = 82 converg = 82 x = 019 trunc = 01 plus_inf = 02 zero = 01 up_mag = 02 twos_comp = 02 converg = 02 x = 819 trunc = 81 plus_inf = 82 zero = 82 up_mag = 81 twos_comp = 82 converg = 82 x = 028 trunc = 02 plus_inf = 03 zero = 02 up_mag = 03 twos_comp = 03 converg = 02 x = 828 trunc = 82 plus_inf = 83 zero = 83 up_mag = 82 twos_comp = 83 converg = 82 x = 029 trunc = 02 plus_inf = 03 zero = 02 up_mag = 03 twos_comp = 03 converg = 03 x = 829 trunc = 82 plus_inf = 83 zero = 83 up_mag = 82 twos_comp = 83 converg = 83 L114 "round.v": $finish at simulation time 160 161 simulation events -- ****************************************************************** Tom Balph tomb@phxtc.sps.mot.com Motorola SPS /MS - ASU02/ rgd210@email.sps.mot.com Phoenix Technology Center 602-755-2504 7755 S. Research Drive, Suite 110 602-755-2551 (FAX) Tempe, AZ 85284 USA