Here is a sample solutions to Assignment 0: Hamming Distance Between Two 8-Bit Binary Integers.
// 1-bit Full Adder module fa(s, co, a, b, ci); output s, co; input a, b, ci; wire gen, p, prop; xor summer(s, a, b, ci); and (gen, a, b); or (p, a, b); and (prop, p, ci); or (co, gen, prop); endmodule // 8-bit HAM module ham(h, a, b); output [3:0] h; input [7:0] a, b; wire [7:0] d; wire c1; wire [1:0] s1, s2, c2; wire [2:0] t; xor (d[0], a[0], b[0]); xor (d[1], a[1], b[1]); xor (d[2], a[2], b[2]); fa one(s1[0], s1[1], d[0], d[1], d[2]); xor (d[3], a[3], b[3]); xor (d[4], a[4], b[4]); xor (d[5], a[5], b[5]); fa two(s2[0], s2[1], d[3], d[4], d[5]); xor (d[6], a[6], b[6]); xor (d[7], a[7], b[7]); fa twe(t[0], c1, s1[0], s2[0], d[6]); fa faw(t[1], t[2], s1[1], s2[1], c1); fa fyv(h[0], c2[0], t[0], d[7], 0); fa six(h[1], c2[1], t[1], 0, c2[0]); fa svn(h[2], h[3], t[2], 0, c2[1]); endmodule // REFerence 8-bit HAM module refham(h, a, b); output [3:0] h; input [7:0] a, b; wire [7:0] d; assign d = a ^ b; assign h = d[0] + d[1] + d[2] + d[3] + d[4] + d[5] + d[6] + d[7]; endmodule // TEST BENCH module testbench; reg [7:0] a, b; integer correct = 0; integer failed = 0; wire [3:0] h, href; ham uut(h, a, b); refham oracle(href, a, b); initial begin a=0; repeat (256) begin b=0; repeat (256) #1 begin if (h != href) begin $display("Wrong: refham(%d,%d)=%d, but ham was %d", a, b, href, h); failed = failed + 1; end else begin correct = correct + 1; end b = b + 1; end a = a + 1; end $display("All cases tested; %d correct, %d failed", correct, failed); $finish; end endmodule
The C program that generated this page was written by Hank Dietz using the CGIC library to implement the CGI interface.
Advanced Computer Architecture.