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

Last updated at Posted at 2025-07-05

概要 

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

練習問題

atcoderを解け。
ABC 088 B - Card Game for Two
N枚のカードを二人で交互に取っていき、カードに書かれている数字の和の差を求める問題。

in

2
3 1

out

2

サンプルコード


module test;
	integer stdin = 'h8000_0000;
	integer n,
		a,
		b,
		c,
		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", n);
			pc <= 20;
		end
		20:
		begin
			ch = $fscanf(stdin, "%d", a);
			mem[p] = a;
			pc <= 30;
		end
		30:
		begin
			p = p + 1;
			if (p < n)
			begin
				pc <= 20;
			end
			else
			begin
				pc <= 40;
			end
		end
		40:
		begin
			for(i = 0; i < n; i = i + 1) 
			begin
				for(j = 0; j < n - i - 1; j = j + 1) 
				begin
					if (mem[j] < mem[j + 1]) 
					begin
						mem[j] <= mem[j + 1];
						mem[j + 1] <= mem[j];
					end
				end
			end
			pc <= 50;
		end
		50:
		begin
			//$display(" %d  %d", mem[0], mem[1]);
			for(i = 0; i < n; i = i + 1) 
			begin
				if (i % 2 == 0)
				begin
					b = b + mem[i];
				end
				else
				begin
					c = c + mem[i];
				end
			end
			pc <= 60;
		end
		60:
		begin
			$write("%d", b - c);
			pc <= 70;
		end
		70:
		begin
			$finish(0);
		end
		endcase
	end
	initial
	begin
		pc <= 10;
		v = 0;
		b = 0;
		c = 0;
		p = 0;
		mem[0] = 0;
	end
endmodule



実行結果

>vvp a.out
2
3 1
          2

以上。

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?