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

Last updated at Posted at 2025-07-07

概要 

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

練習問題

atcoderを解け。
ABC 049 C - Daydream
文字列Sが、'dream','dreamer','erase','eraser'の組み合わせで作れるかどうかを判定する。

投入するソース

erasedream

期待値

YES

サンプルコード


module test;
	integer stdin = 'h8000_0000;
	integer a,
		b,
		c,
		ch,
		v,
		p;
	reg [7:0] pc;
	reg [7:0] str[0:19];
	logic [6:0][7:0] str0 = "remaerd";
	logic [5:0][7:0] str1 = "resare";
	logic [4:0][7:0] str2 = "maerd";
	logic [4:0][7:0] str3 = "esare";
	always #1
	begin
		case(pc)
		10:
		begin
			ch = $fgetc(stdin);
			pc <= 20;
		end
		20:
		begin
			str[v] = ch;
			if (ch == 'ha)
			begin
				a = 0;
				b = 0;
				p = 7;
				pc <= 30;
			end
			else
			begin
				v++;
				pc <= 10;
			end
		end
		30:
		begin
			//$display("%s %s", str[a], str0[b]);
			if (str[a] == str0[b])
			begin
				a = a + 1;
				b = b + 1;
				if (b == p)
				begin
					//$display("--ok0");
					if (a == v)
					begin
						pc <= 150;
					end
					else
					begin
						b = 0;
						p = 7;
						pc <= 30;
					end
				end
				else
				begin
					pc <= 30;
				end
			end
			else
			begin
				//$display("--pass0");
				a = a - b;
				b = 0;
				p = 6;
				pc <= 40;
			end
		end
		40:
		begin
			//$display("%s %s", str[a], str1[b]);
			if (str[a] == str1[b])
			begin
				a = a + 1;
				b = b + 1;
				if (b == p)
				begin
					//$display("--ok1");
					if (a == v)
					begin
						pc <= 150;
					end
					else
					begin
						b = 0;
						p = 7;
						pc <= 30;
					end
				end
				else
				begin
					pc <= 40;
				end
			end
			else
			begin
				//$display("--pass1");
				a = a - b;
				b = 0;
				p = 5;
				pc <= 50;
			end
		end
		50:
		begin
			//$display("%s %s", str[a], str2[b]);
			if (str[a] == str2[b])
			begin
				a = a + 1;
				b = b + 1;
				if (b == p)
				begin
					//$display("--ok2");
					if (a == v)
					begin
						pc <= 150;
					end
					else
					begin
						b = 0;
						p = 7;
						pc <= 30;
					end
				end
				else
				begin
					pc <= 50;
				end
			end
			else
			begin
				//$display("--pass2");
				a = a - b;
				b = 0;
				p = 5;
				pc <= 60;
			end
		end
		60:
		begin
			//$display("%s %s", str[a], str3[b]);
			if (str[a] == str3[b])
			begin
				a = a + 1;
				b = b + 1;
				if (b == p)
				begin
					//$display("--ok3");
					if (a == v)
					begin
						pc <= 150;
					end
					else
					begin
						b = 0;
						p = 7;
						pc <= 30;
					end
				end
				else
				begin
					pc <= 60;
				end
			end
			else
			begin
				//$display("--pass3");
				pc <= 130;
			end
		end
		130:
		begin
			pc <= 140;
			if (a == p)
			begin
				pc <= 30;
			end
			if (a == v)
			begin
				pc <= 150;
			end
		end
		140:
		begin
			$write("NO");
			pc <= 160;
		end
		150:
		begin
			$write("YES");
			pc <= 160;
		end
		160:
		begin
			$finish(0);
		end
		endcase
	end
	initial
	begin
		//$monitor(" %d", pc);
		//$display("str=%s", str[0]);
		//$display("str0=%s", str0[0]);
		pc <= 10;
		a = 0;
		b = 0;
		v = 0;
		p = 0;
		#200
			$finish(0);
	end
endmodule





実行結果

>vvp a.out
erasedream
YES

以上。

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?