windowsでiverilog その135

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

# 練習問題
cpuを実装せよ。
zundokoを実行せよ。

# サンプルコード

```



module cpu12(input clk, input rst, output [7:0] outport);
	parameter N = 6;
	integer i;
	reg [7:0] q[0:N - 1];
	reg [11:0] rom[64:0];
	reg [7:0] ram[2:0];
	reg jpflg;
	reg [7:0] pc;
	reg [7:0] out;
	reg [7:0] rnd;
	wire [11:0] code;
	wire [3:0] op;
	wire [7:0] opland;
	wire [7:0] qtop;
	wire [7:0] qnext;
	assign code = rom[pc];
	assign op = code[11:8];
	assign opland = code[7:0];
	assign outport = out;
	assign qtop = q[0];
	assign qnext = q[1];
	task push;
	begin
		for (i = 1; i < N; i = i + 1)
			q[i] <= q[i - 1];
	end
	endtask
	task pop;
	begin
		for (i = 1; i < N - 1; i = i + 1)
			q[i] <= q[i + 1];
	end
	endtask
	always @(posedge clk)
	begin
		if (rst)
		begin
//  0
rom[0] <= 12'h2_00;
//  set  a
rom[1] <= 12'h4_00;
//loop  rnd
rom[2] <= 12'hf_07;
//  127
rom[3] <= 12'h2_7F;
//  >
rom[4] <= 12'hf_06;
//  if  skip1
rom[5] <= 12'h6_17;
//  'z'
rom[6] <= 12'h2_7A;
//  out
rom[7] <= 12'h5_00;
//  'u'
rom[8] <= 12'h2_75;
//  out
rom[9] <= 12'h5_00;
//  'n'
rom[10] <= 12'h2_6E;
//  out
rom[11] <= 12'h5_00;
//  ' '
rom[12] <= 12'h2_20;
//  out
rom[13] <= 12'h5_00;
//  get  a
rom[14] <= 12'h3_00;
//  1
rom[15] <= 12'h2_01;
//  +
rom[16] <= 12'hf_00;
//  set  a
rom[17] <= 12'h4_00;
//  get  a
rom[18] <= 12'h3_00;
//  3
rom[19] <= 12'h2_03;
//  >
rom[20] <= 12'hf_06;
//  if  skip2
rom[21] <= 12'h6_24;
//  jmp  loop
rom[22] <= 12'h1_02;
//skip1  'd'
rom[23] <= 12'h2_64;
//  out
rom[24] <= 12'h5_00;
//  'o'
rom[25] <= 12'h2_6F;
//  out
rom[26] <= 12'h5_00;
//  'k'
rom[27] <= 12'h2_6B;
//  out
rom[28] <= 12'h5_00;
//  'o'
rom[29] <= 12'h2_6F;
//  out
rom[30] <= 12'h5_00;
//  ' '
rom[31] <= 12'h2_20;
//  out
rom[32] <= 12'h5_00;
//  0
rom[33] <= 12'h2_00;
//  set  a
rom[34] <= 12'h4_00;
//  jmp  loop
rom[35] <= 12'h1_02;
//skip2  'd'
rom[36] <= 12'h2_64;
//  out
rom[37] <= 12'h5_00;
//  'o'
rom[38] <= 12'h2_6F;
//  out
rom[39] <= 12'h5_00;
//  'k'
rom[40] <= 12'h2_6B;
//  out
rom[41] <= 12'h5_00;
//  'o'
rom[42] <= 12'h2_6F;
//  out
rom[43] <= 12'h5_00;
//  ' '
rom[44] <= 12'h2_20;
//  out
rom[45] <= 12'h5_00;
//  'k'
rom[46] <= 12'h2_6B;
//  out
rom[47] <= 12'h5_00;
//  'i'
rom[48] <= 12'h2_69;
//  out
rom[49] <= 12'h5_00;
//  'y'
rom[50] <= 12'h2_79;
//  out
rom[51] <= 12'h5_00;
//  'o'
rom[52] <= 12'h2_6F;
//  out
rom[53] <= 12'h5_00;
//  's'
rom[54] <= 12'h2_73;
//  out
rom[55] <= 12'h5_00;
//  'i'
rom[56] <= 12'h2_69;
//  out
rom[57] <= 12'h5_00;
//  '!'
rom[58] <= 12'h2_21;
//  out
rom[59] <= 12'h5_00;
//lup  jmp  lup
rom[60] <= 12'h1_3C;
//
rom[61] <= 12'h2_00;

			for (i = 0; i < N; i = i + 1)
				q[i] <= 0;
			jpflg <= 0;
			pc <= 0;
			ram[0] <= 0;
			ram[1] <= 0;
			ram[2] <= 0;
			rnd <= 234;
		end
		else
		begin
			case (op)
			4'h0://end
			begin
			end
			4'h1://jmp
			begin
				jpflg = 1;
				pc <= opland;
			end
			4'h2://push
			begin
				push;
				q[0] <= opland;
			end
			4'h3://get
			begin
				push;
				q[0] <= ram[opland];
			end
			4'h4://set
			begin
				ram[opland] <= q[0];
				pop;
			end
			4'h5://out
			begin
				case (opland)
				4'h0://out
				begin
					out <= q[0];
					pop;
				end
				4'h1://out1
				begin
					out <= (q[0] % 10) + 48;
					pop;
				end
				4'h2://out2
				begin
					out <= ((q[0] / 10) % 10) + 48;
					pop;
				end
				4'h3:///out3
				begin
					out <= (q[0] / 100) + 48;
					pop;
				end
				endcase
			end
			4'h6://if
			begin
				if (q[0] == 1)
				begin
					jpflg = 1;
					pc <= opland;
				end
			end
			4'h7://if!
			begin
				if (q[0] == 0)
				begin
					jpflg = 1;
					pc <= opland;
				end
			end
			4'h8://dup
			begin
				push;
			end
			4'h9://drop
			begin
				pop;
			end
			4'hf:
			begin
				case (opland)
				4'h0://+
				begin
					q[0] <= q[1] + q[0];
					pop;
				end
				4'h1://-
				begin
					q[0] <= q[1] - q[0];
					pop;
				end
				4'h2://*
				begin
					q[0] <= q[1] * q[0];
					pop;
				end
				4'h3:///
				begin
					q[0] <= q[1] / q[0];
					pop;
				end
				4'h4://%
				begin
					q[0] <= q[1] % q[0];
					pop;
				end
				4'h5://=
				begin
					if (q[1] == q[0])
						q[0] <= 1;
					else
						q[0] <= 0;
					pop;
				end
				4'h6://>
				begin
					if (q[1] > q[0])
						q[0] <= 1;
					else
						q[0] <= 0;
					pop;
				end
				4'h7://rnd
				begin
					rnd <= rnd * 5 + 1;
					push;
					q[0] <= rnd;
				end
				endcase
			end
			endcase
			if (jpflg == 0)
				pc <= pc + 1;
			else
				jpflg <= 0;
		end
	end
	initial
	begin
		//$display("pc, ram0, out");
		//$monitor("   %d   %d   %d   %d   %d   %s", pc, qtop, jpflg, ram[0], rnd, out);
	end
endmodule

module test;
	reg clk,
		rst;
	wire [7:0] outport;
	cpu12 u(.clk(clk), .rst(rst), .outport(outport));
	initial
	begin
		$monitor("   %s", outport);
		clk = 0;
		rst = 0;
		#2
			rst = 1;
		#2
			rst = 0;
		#1400
			$finish;
	end
	always
		#1
			clk = ~clk;
endmodule





```

# 実行結果

```
>iverilog cpu15.v

>vvp a.out

   d
   o
   k
   o

   d
   o
   k
   o

   d
   o
   k
   o

   z
   u
   n

   d
   o
   k
   o

   z
   u
   n

   z
   u
   n

   z
   u
   n

   z
   u
   n

   d
   o
   k
   o

   k
   i
   y
   o
   s
   i
   !



```


以上。