1
0

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 その198

Last updated at Posted at 2025-07-05

概要 

windowsでiverilogやってみた。
練習問題やってみた。

練習問題

atcoderを解け。
ABC 085 B - Kagami Mochi
N枚の整数値が与えられ、それらの重複しない要素の数を数える問題です。

in

10 8 8 6

out

3

サンプルコード


module test;
	integer stdin = 'h8000_0000;
	integer n,
		a,
		b,
		c,
		d,
		ch,
		i,
		j,
		v,
		p;
	reg [7:0] pc;
	reg [7:0] mem[0:9];
	always #1
	begin
		case(pc)
		10:
		begin
			ch = $fscanf(stdin, "%d%d%d%d", a, b, c, d);
			pc <= 20;
		end
		20:
		begin
			//$display("%d%d%d%d", a, b, c, d);
			pc <= 30;
		end
		30:
		begin
			p = 0;
			for(i = 0; i < n; i = i + 1) 
			begin
				if (mem[i] == a)
				begin
					p = 1;
				end
			end
			if (p == 0)
			begin
				mem[n] <= a;
				n = n + 1;
				//$display("%d", n);
			end
			pc <= 40;
		end
		40:
		begin
			p = 0;
			for(i = 0; i < n; i = i + 1) 
			begin
				if (mem[i] == b)
				begin
					p = 1;
				end
			end
			if (p == 0)
			begin
				mem[n] <= b;
				n = n + 1;
				//$display("%d", n);
			end
			pc <= 50;
		end
		50:
		begin
			p = 0;
			for(i = 0; i < n; i = i + 1) 
			begin
				if (mem[i] == c)
				begin
					p = 1;
				end
			end
			if (p == 0)
			begin
				mem[n] <= c;
				n = n + 1;
				//$display("%d", n);
			end
			pc <= 60;
		end
		60:
		begin
			p = 0;
			for(i = 0; i < n; i = i + 1) 
			begin
				if (mem[i] == d)
				begin
					p = 1;
				end
			end
			if (p == 0)
			begin
				mem[n] <= d;
				n = n + 1;
				//$display("%d", n);
			end
			pc <= 70;
		end
		70:
		begin
			$write("%d", n);
			pc <= 80;
		end
		80:
		begin
			$finish(0);
		end
		endcase
	end
	initial
	begin
		pc <= 10;
		n = 0;
		mem[0] = 0;
	end
endmodule




実行結果

>vvp a.out
10 8 8 6
          3

以上。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?