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

概要

micropythonでshrike-lite、やってみた。
exsampleのstack_processorがBRAMでシミュレーション出来なかったので、書き直してみた。

サンプルコード

(* top *) module top ((* iopad_external_pin, clkbuf_inhibit *) input clk, (* iopad_external_pin *) output clk_en,
	(* iopad_external_pin *) input rst_n, (* iopad_external_pin *) input spi_ss_n, (* iopad_external_pin *) input spi_sck,
	(* iopad_external_pin *) input spi_mosi, (* iopad_external_pin *) output spi_miso, (* iopad_external_pin *) output spi_miso_en);
	parameter N = 5;
	integer i,
		j;
	reg [3:0] stack[0:N - 1];
	reg [7:0] A,
		B,
		C;
	reg [7:0] spi_tx_data;
	reg spi_rx_valid_d;
	wire spi_rx_pulse;
	wire [7:0] spi_rx_data;
	wire spi_rx_valid;
	task push;
		begin
			for (i = 1; i < N; i = i + 1)
				stack[i] <= stack[i - 1];
		end
	endtask
	task pop;
		begin
			for (i = 0; i < N - 1; i = i + 1)
				stack[i] <= stack[i + 1];
		end
	endtask
	assign clk_en = 1'b1;
	assign spi_rx_pulse = spi_rx_valid & ~spi_rx_valid_d;
	spi_target #(.CPOL(1'b0), .CPHA(1'b0), .WIDTH(8), .LSB(1'b0))
		u_spi_target(.i_clk(clk), .i_rst_n(rst_n), .i_enable(1'b1), .i_ss_n(spi_ss_n), .i_sck(spi_sck), .i_mosi(spi_mosi), .o_miso(spi_miso),
			.o_miso_oe(spi_miso_en), .o_rx_data(spi_rx_data), .o_rx_data_valid(spi_rx_valid), .i_tx_data(spi_tx_data), .o_tx_data_hold());
	initial
	begin
		$monitor(" A:%d B:%d C:%d ", A, B, C);
	end
	always @(posedge clk or negedge rst_n)
	begin
		if (!rst_n)
			spi_rx_valid_d <= 1'b0;
		else
			spi_rx_valid_d <= spi_rx_valid;
	end
	always @(posedge clk or negedge rst_n)
	begin
		if (!rst_n)
		begin
			spi_tx_data <= 8'b0;
			for (i = 0; i < N; i = i + 1)
				stack[i] <= 0;
		end
		else
		begin
			if (spi_rx_pulse)
			begin
				casez (spi_rx_data)
				8'b0000_0000:
					C <= C;
				8'b0001_????://1n push n
					begin
						push;
						stack[0] <= spi_rx_data[3:0];
					end
				8'b0010_0000://20 pop out
					begin
						spi_tx_data <= {4'b0000, stack[0]};
						pop;
					end
				8'b0011_0000://30 push a
					begin
						push;
						stack[0] <= A[3:0];
					end
				8'b0011_0001://31 push b
					begin
						push;
						stack[0] <= B[3:0];
					end
				8'b0011_0010://32 push c
					begin
						push;
						stack[0] <= C[3:0];
					end
				8'b0011_0011://33 pop a
					begin
						A <= {4'b0000, stack[0]};
						pop;
					end
				8'b0011_0100://34 pop b
					begin
						B <= {4'b0000, stack[0]};
						pop;
					end
				8'b0011_0101://35 pop c
					begin
						C <= {4'b0000, stack[0]};
						pop;
					end
				8'b1100_0000://c0
					C <= A + B;
				8'b1100_0001://c1
					C <= A - B;
				8'b1100_0010://c2
					C <= A * B;
				8'b1100_0011://c3
					C <= A / B;
				8'b1100_0100://c4
					C <= A << B;
				8'b1100_0101://c5
					C <= A >> B;
				8'b1100_0110://c6
					C <= {A[7], A[7:1]};
				default:
					C <= C;
				endcase
			end
		end
	end
endmodule


テストベンチ

`timescale 1ns / 1ps

module test();
	reg clk = 0;
	reg rst_n = 1;
	reg spi_ss_n = 1;
	reg spi_sck = 0;
	reg spi_mosi = 0;
	reg [7:0] rx_data1;
	wire spi_miso;
	wire spi_miso_en;
	wire clk_en;
	top u(.clk(clk), .clk_en(clk_en), .rst_n(rst_n),
		.spi_ss_n(spi_ss_n), .spi_sck(spi_sck), .spi_mosi(spi_mosi), .spi_miso(spi_miso), .spi_miso_en(spi_miso_en));
	task task_spi_transfer(input [7:0] send_val, output [7:0] recv_val);
		integer i;
		begin
			recv_val = 8'h00;
			for (i = 7; i >= 0; i = i - 1)
			begin
				spi_mosi = send_val[i];
				#10;
				spi_sck = 1;
				#10;
				recv_val[i] = spi_miso;
				#10;
				spi_sck = 0;
			end
			spi_mosi = 0;
		end
	endtask
	initial 
	begin
		$dumpfile("test.vcd");
		$dumpvars(0, u);
	end
	initial 
	begin
		clk = 0;
		rst_n = 1;
		spi_ss_n = 1;
		spi_sck = 0;
		spi_mosi = 0;
		#20;
		rst_n = 0;
		#20;
		rst_n = 1;
		#20;
		spi_ss_n = 0;
		#20;
		task_spi_transfer(8'h12, rx_data1); $display("【受信 1件目】 送信:0x12 -> 受信:8'h%h", rx_data1);
		task_spi_transfer(8'h15, rx_data1); $display("【受信 2件目】 送信:0x15 -> 受信:8'h%h", rx_data1);
		task_spi_transfer(8'h33, rx_data1); $display("【受信 3件目】 送信:0x33 -> 受信:8'h%h", rx_data1);
		task_spi_transfer(8'h34, rx_data1); $display("【受信 4件目】 送信:0x34 -> 受信:8'h%h", rx_data1);
		task_spi_transfer(8'hc2, rx_data1); $display("【受信 5件目】 送信:0xc2 -> 受信:8'h%h", rx_data1);
		task_spi_transfer(8'h32, rx_data1); $display("【受信 6件目】 送信:0x32 -> 受信:8'h%h", rx_data1);
		task_spi_transfer(8'h20, rx_data1); $display("【受信 7件目】 送信:0x20 -> 受信:8'h%h", rx_data1);
		task_spi_transfer(8'h00, rx_data1); $display("【受信 8件目】 送信:0x00 -> 受信:8'h%h", rx_data1);
		#20;
		spi_ss_n = 1;
		#20;
		$finish; 
	end
	always 
		#1 
			clk = ~clk;
endmodule




実行結果

>vvp a.out
VCD info: dumpfile test.vcd opened for output.
 A:  x B:  x C:  x
【受信 1件目】 送信:0x12 -> 受信:8'h00
【受信 2件目】 送信:0x15 -> 受信:8'h00
 A:  5 B:  x C:  x
【受信 3件目】 送信:0x33 -> 受信:8'h00
 A:  5 B:  2 C:  x
【受信 4件目】 送信:0x34 -> 受信:8'h00
 A:  5 B:  2 C: 10
【受信 5件目】 送信:0xc2 -> 受信:8'h00
【受信 6件目】 送信:0x32 -> 受信:8'h00
【受信 7件目】 送信:0x20 -> 受信:8'h00
【受信 8件目】 送信:0x00 -> 受信:8'h0a
spi9.v:327: $finish called at 2040000 (1ps)

以上。

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