2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

windowsでiverilog その175

Posted at

概要 

windowsでiverilogやってみた。
74181なverilog見つけたので、やってみた。

74181は、ALU

TTL集積回路として実装された4ビット・スライス演算装置(Arithmetic Logic Unit; ALU)です。

参考にしたページ

サンプルコード


module alu(output wire f, output wire co_x, input wire a, input wire b, input wire ci_x, input wire [3:0] s, input wire m);
	wire g;
	wire h;
	assign g = !(a | (b & s[0]) | (!b & s[1]));
	assign h = !((!b & s[2] & a) | (a & s[3] & b));
	assign co_x = (g | (h & ci_x));
	assign f = !(!m & ci_x) ^ g ^ h;
endmodule

module alu74181 #(parameter WIDTH = 4) (output wire [WIDTH - 1:0] f, output wire co_x, input wire [WIDTH - 1:0] a, input wire [WIDTH - 1:0] b, input wire ci_x, input wire [3:0] s, input wire m);
	genvar i;
	wire [WIDTH:0] c_x;
	assign c_x[0] = ci_x;
	assign co_x = c_x[WIDTH];
	generate
		for (i = 0; i < WIDTH; i = i + 1)
		begin
			alu alu_ins(.f(f[i]), .co_x(c_x[i + 1]), .a(a[i]), .b(b[i]), .ci_x(c_x[i]), .s(s), .m(m));
		end
	endgenerate
endmodule

module test;
	wire [3:0] f;
	reg [3:0] a;
	reg [3:0] b;
	reg cy;
	reg [3:0] s;
	reg m;
	task check_alu(input [3:0] ai, input [3:0] bi, input c, input [3:0] si, input mi);
	begin
		a = ai;
		b = bi;
		cy = c;
		s = si;
		m = mi;
	#1 
		$display("a=%d b=%d cy=%d s=%d m=%d f=%d", a, b, cy, s, m, f);
	end
	endtask
	alu74181 u(.f(f), .a(a), .b(b), .ci_x(cy), .s(s), .m(m));
	initial 
	begin
		$display("ADD");
		check_alu('h0, 'h0, 1, 'h9, 0);
		check_alu('h1, 'h1, 1, 'h9, 0);
		check_alu('h3, 'h3, 1, 'h9, 0);
		check_alu('h7, 'h7, 1, 'h9, 0);
		$display("SUB");
		check_alu('h9, 'h0, 0, 'h6, 0);
		check_alu('h9, 'h3, 0, 'h6, 0);
		check_alu('h9, 'h6, 0, 'h6, 0);
		$display("XOR");
		check_alu('h0, 'h0, 0, 6, 1);
		check_alu('h0, 'h1, 0, 6, 1);
		check_alu('h1, 'h0, 0, 6, 1);
		check_alu('h1, 'h1, 0, 6, 1);
		$display("AND");
		check_alu('h0, 'h0, 0, 'hb, 1);
		check_alu('h0, 'h1, 0, 'hb, 1);
		check_alu('h1, 'h0, 0, 'hb, 1);
		check_alu('h1, 'h1, 0, 'hb, 1);
		$display("OR");
		check_alu('h0, 'h0, 0, 'he, 1);
		check_alu('h0, 'h1, 0, 'he, 1);
		check_alu('h1, 'h0, 0, 'he, 1);
		check_alu('h1, 'h1, 0, 'he, 1);
	end
endmodule





実行結果

>vvp a.out
ADD
a= 0 b= 0 cy=1 s= 9 m=0 f= 0
a= 1 b= 1 cy=1 s= 9 m=0 f= 2
a= 3 b= 3 cy=1 s= 9 m=0 f= 6
a= 7 b= 7 cy=1 s= 9 m=0 f=14
SUB
a= 9 b= 0 cy=0 s= 6 m=0 f= 9
a= 9 b= 3 cy=0 s= 6 m=0 f= 6
a= 9 b= 6 cy=0 s= 6 m=0 f= 3
XOR
a= 0 b= 0 cy=0 s= 6 m=1 f= 0
a= 0 b= 1 cy=0 s= 6 m=1 f= 1
a= 1 b= 0 cy=0 s= 6 m=1 f= 1
a= 1 b= 1 cy=0 s= 6 m=1 f= 0
AND
a= 0 b= 0 cy=0 s=11 m=1 f= 0
a= 0 b= 1 cy=0 s=11 m=1 f= 0
a= 1 b= 0 cy=0 s=11 m=1 f= 0
a= 1 b= 1 cy=0 s=11 m=1 f= 1
OR
a= 0 b= 0 cy=0 s=14 m=1 f= 0
a= 0 b= 1 cy=0 s=14 m=1 f= 1
a= 1 b= 0 cy=0 s=14 m=1 f= 1
a= 1 b= 1 cy=0 s=14 m=1 f= 1

以上。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?