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、やってみた。
shrikeで、自作cpuやってみた。
spiで、オペコードをもらい、実行する。
結果を都度spiで返す。
pythonで、forthの数9を4個使って、1から15までを求める式から、アセンブルして、機械語をspiで送る。

5bit CPUの仕様

stackマシーン
stackは、5bitで6個搭載
RAMはゼロ。
命令は、8bit
ROMは、0個
RAMは、0個
退避レジスタ 5bit 1個
outレジスタ 5bit 1個
プログラムカウンタ(pc) なし
入出力は、spi

機械語

ニーモニック 動作 機械語
end 停止 00
sort5 スタックに積んだ5個を、昇順にソートする。 10
x スタックに数値xを積む。 2x
out スタックTOPを出力。 50
dup スタックTOPを複写。 80
drop スタックTOPを廃棄。 90
+ スタックTOPをNEXTで加算。 F0
- スタックTOPをNEXTで減算。 F1
* スタックTOPをNEXTで乗算。 F2
/ スタックTOPをNEXTで除算。 F3
% スタックTOPをNEXTで剰余。 F4

手順

top.vを、書く。

top.v
(* 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
);
	integer i,
		j;
	reg [4:0] q[0:5];
	reg [7:0] rom[0:15];
	reg [4:0] temp;
	reg [4:0] out;	
	reg [7:0] spi_tx_data;
	reg spi_rx_valid_d;
	wire [7:0] code;
	wire [3:0] op;
	wire [3:0] opland;
	wire spi_rx_pulse;
	wire [7:0] spi_rx_data;
	wire spi_rx_valid;
	assign clk_en = 1'b1;
	assign spi_rx_pulse = spi_rx_valid & ~spi_rx_valid_d;
	assign op = spi_rx_data[7:4];
	assign opland = spi_rx_data[3:0];
	spi_target #(.CPOL(1'b0), .CPHA(1'b0), .WIDTH(8), .LSB(1'b0))
		 u(.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());
	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
			q[5] <= 0; 
			q[4] <= 0; 
			q[3] <= 0; 
			q[2] <= 0; 
			q[1] <= 0;
			q[0] <= 0;
			out <= 0;
			spi_tx_data <= 8'h00;
		end
		else
		begin
			if (spi_rx_pulse)
			begin
				casez (op)
				4'h1://sort5
					begin
						for (i = 0; i < 4; i = i + 1) 
						begin
							for (j = 0; j < 4 - i; j = j + 1) 
							begin
								if (q[j] > q[j + 1]) 
								begin
									temp = q[j];
									q[j] = q[j + 1];
									q[j + 1] = temp;
								end
							end
						end
					end
				4'h2://push
					begin
						q[5] <= q[4]; q[4] <= q[3]; q[3] <= q[2]; q[2] <= q[1]; q[1] <= q[0];
						q[0] <= opland;
						//$display("push");
					end
				4'h5://out
					begin
						out <= q[0];
						q[0] <= q[1];
						q[1] <= q[2]; q[2] <= q[3]; q[3] <= q[4]; q[4] <= q[5]; q[5] <= 4'd0;
						//$display("out");
					end
				4'h8://dup
					begin
						q[5] <= q[4]; q[4] <= q[3]; q[3] <= q[2]; q[2] <= q[1]; q[1] <= q[0];
					end
				4'h9://drop
					begin
						q[0] <= q[1];
						q[1] <= q[2]; q[2] <= q[3]; q[3] <= q[4]; q[4] <= q[5]; q[5] <= 4'd0;
					end
				4'hf:
					begin
						case (opland)
						4'h0://+
							begin
								q[0] <= q[1] + q[0];
								q[1] <= q[2]; q[2] <= q[3]; q[3] <= q[4]; q[4] <= q[5]; q[5] <= 4'd0;
							end
						4'h1://-
							begin
								q[0] <= q[1] - q[0];
								q[1] <= q[2]; q[2] <= q[3]; q[3] <= q[4]; q[4] <= q[5]; q[5] <= 4'd0;
							end
						4'h2://*
							begin
								q[0] <= q[1] * q[0];
								q[1] <= q[2]; q[2] <= q[3]; q[3] <= q[4]; q[4] <= q[5]; q[5] <= 4'd0;
							end
						4'h3:///
							begin
								q[0] <= q[1] / q[0];
								q[1] <= q[2]; q[2] <= q[3]; q[3] <= q[4]; q[4] <= q[5]; q[5] <= 4'd0;
							end
						4'h4://%
							begin
								q[0] <= q[1] % q[0];
								q[1] <= q[2]; q[2] <= q[3]; q[3] <= q[4]; q[4] <= q[5]; q[5] <= 4'd0;
							end
						default: ;
						endcase
					end
				default: ;
				endcase
			end
			case (spi_rx_data)
			8'h5_0: //50 out
				spi_tx_data <= {3'h0, out};
			default:
				spi_tx_data <= 8'h00;
			endcase
		end
	end
endmodule




bitstreamを作る。

cpu4.binにリネームして、usbにコピー

main.pyを書いて、usbにコピー

main.py

from machine import Pin, SPI
import time
import shrike

shrike.reset()
shrike.flash("cpu4.bin")

reset_pin = Pin(14, Pin.OUT, value = 1)
reset_pin.value(0)
time.sleep(1)
reset_pin.value(1)
time.sleep(1)

SCK = 2
CS = 1
MOSI = 3
MISO = 0

cs = Pin(CS, Pin.OUT, value = 1)
spi = SPI(0, baudrate = 1_000_000, polarity = 0, phase = 0, bits = 8, firstbit = SPI.MSB, sck = Pin(SCK), mosi = Pin(MOSI), miso = Pin(MISO))

def send(byte_to_send):
	tx = bytes([byte_to_send])
	rx = bytearray(1)
	cs.value(0)
	spi.write_readinto(tx, rx)
	cs.value(1)
	print(rx[0])

opcodes = {
	"end":  0x00,
	"sort5": 0x10,
	"9":	0x29,
	".": 	0x50,
	"out": 	0x50,
	"dup": 	0x80,
	"drop": 0x90,
	"+": 	0xF0,
	"-": 	0xF1,
	"*": 	0xF2,
	"/": 	0xF3
}

def asm(program_str):
	print("")
	print(program_str)
	tokens = program_str.split(' ')
	machine_code = []
	for token in tokens:
		if token == "":
			continue
		elif token in opcodes:
			send(opcodes[token])
		else:
			print(f"Error: 未定義のトークンです -> {token}")
			return None
	send(0x00)

def main():
	asm("9 9 - 9 9 / .")
	asm("9 9 / 9 9 / + .")
	asm("9 9 + 9 + 9 / .")
	asm("9 9 9 + 9 / dup + .")
	asm("9 9 9 + 9 / dup + - .")
	asm("9 dup 9 + 9 + 9 / - .")
	asm("9 9 9 + 9 / - .")
	asm("9 9 9 drop 9 / - .")
	asm("9 9 - 9 * 9 + .")
	asm("9 9 / 9 dup 9 / + .")
	asm("9 9 9 + 9 / + .")
	asm("9 dup 9 9 + + 9 / + .")
	asm("9 9 9 + 9 / dup + + .")
	asm("9 dup 9 9 + 9 / dup + - + .")
	asm("9 dup dup 9 + 9 + 9 / - + .")




実行結果


>shrike0
Port open ok
[shrike_flash] FPGA reset done
[shrike_flash] FPGA reset done
[shrike_fpga] Starting FPGA flash...
[shrike_fpga] flashing: cpu4.bin
[shrike_flash] FPGA programming done.
MicroPython v1.26.0-preview.527.g599f545a3.dirty on 2025-10-11; Vicharak Shrike FPGA with RP2040
Type "help()" for more information.

>>>
main()
>>> main()

9 9 - 9 9 / .
0
0
0
0
0
0
0
1

9 9 / 9 9 / + .
0
0
0
0
0
0
0
0
2

9 9 + 9 + 9 / .
0
0
0
0
0
0
0
0
3

9 9 9 + 9 / dup + .
0
0
0
0
0
0
0
0
0
4

9 9 9 + 9 / dup + - .
0
0
0
0
0
0
0
0
0
0
5

9 dup 9 + 9 + 9 / - .
0
0
0
0
0
0
0
0
0
0
6

9 9 9 + 9 / - .
0
0
0
0
0
0
0
0
7

9 9 9 drop 9 / - .
0
0
0
0
0
0
0
0
8

9 9 - 9 * 9 + .
0
0
0
0
0
0
0
0
9

9 9 / 9 dup 9 / + .
0
0
0
0
0
0
0
0
0
10

9 9 9 + 9 / + .
0
0
0
0
0
0
0
0
11

9 dup 9 9 + + 9 / + .
0
0
0
0
0
0
0
0
0
0
12

9 9 9 + 9 / dup + + .
0
0
0
0
0
0
0
0
0
0
13

9 dup 9 9 + 9 / dup + - + .
0
0
0
0
0
0
0
0
0
0
0
0
14

9 dup dup 9 + 9 + 9 / - + .
0
0
0
0
0
0
0
0
0
0
0
0
15



以上。

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?