概要
micropythonでshrike-lite、やってみた。
shrikeで、自作cpuやってみた。
UUUをSPIで返す。
8bit CPUの仕様
stackマシーン
stackは、8bitで4個搭載
RAMはゼロ。
命令は、12bit
ROMは、64個
RAMは、0個
プログラムカウンタ(pc) 6bit 1個
アドレスレジスタ 6bit 1個
outレジスタ 8bit 1個
出力は、uart
機械語
12bit
| ニーモニック | 動作 | 機械語 |
|---|---|---|
| halt | 停止 | 000 |
| push x | スタックに数値xxを積む。 | 2xx |
| adr x | アドレスADRに数値xxを代入。 | 3xx |
| ifeq | スタックTOPが真ならアドレスADRに飛ぶ。 | 400 |
| ifne | スタックTOPが偽ならアドレスADRに飛ぶ。 | 401 |
| jmp | アドレスADRに飛ぶ。 | 4FF |
| out | スタックTOPを数値出力。 | 500 |
| emit | スタックTOPを出力。 | 600 |
| + | スタックTOPをNEXTで加算。 | F00 |
| - | スタックTOPをNEXTで減算。 | F01 |
| * | スタックTOPをNEXTで乗算。 | F02 |
| / | スタックTOPをNEXTで除算。 | F03 |
| % | スタックTOPをNEXTで剰余。 | F04 |
| = | スタックTOPをNEXTでイコール。 | F0F |
| < | スタックTOPをNEXTで小さい。 | F10 |
| > | スタックTOPをNEXTで大きい。 | F11 |
| dup | スタックTOPを複写。 | F20 |
| drop | スタックTOPを廃棄。 | F21 |
投入するプログラム
UUUを、印字する。
0: push 'U'
1: emit
2: push 'U'
3: emit
4: push 'U'
5: emit
6: push '\r'
7: emit
8: push '\n'
9: emit
10: halt
写真
サンプルコード
spiで、1を送ると、cpuが、動いてUUUを返す。
spiで、2を送ると、Uを返す。
(* 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,
(* iopad_external_pin *) output reg led,
(* iopad_external_pin *) output led_en
);
reg [1:0] mode = 2'd0;
reg [3:0] state = 4'd0;
reg [7:0] q0,
q1,
q2,
q3;
reg [6:0] pc;
reg [6:0] adr;
reg [7:0] out;
reg [11:0] code;
reg [7:0] rx_data_reg;
wire [3:0] op = code[11:8];
wire [7:0] opland = code[7:0];
wire [7:0] rx_data_wire;
wire rx_valid_pulse;
reg [2:0] sck_sync;
reg [3:0] sck_counter;
wire sck_edge = (sck_sync[1] && !sck_sync[2]);
assign clk_en = 1'b1;
assign led_en = 1'b1;
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(rx_data_wire), .o_rx_data_valid(rx_valid_pulse),
.i_tx_data(out), .o_tx_data_hold());
initial
begin
$monitor("mode: %h out: %h pc: %d ", mode, out, pc);
end
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
sck_sync <= 3'd0;
else
sck_sync <= {sck_sync[1:0], spi_sck};
end
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
mode <= 2'd0;
end
else
begin
if (rx_valid_pulse)
begin
if (rx_data_wire == 8'h1 && mode == 2'd0)
begin
mode <= 2'd1;
end
if (rx_data_wire == 8'h2 && mode == 2'd0)
begin
mode <= 2'd2;
end
end
end
end
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
q0 <= 8'd0;
q1 <= 8'd0;
q2 <= 8'd0;
q3 <= 8'd0;
adr <= 0;
out <= 0;
pc <= 0;
state <= 4'd0;
sck_counter <= 4'd0;
end
else
begin
case (mode)
2'd1:
begin
led <= 0;
case (op)
4'h0: // HALT
begin
pc <= pc;
end
4'h2: // PUSH
begin
q3 <= q2; q2 <= q1; q1 <= q0;
q0 <= opland;
pc <= pc + 1;
end
4'h3: // ADR
begin
adr <= opland[6:0];
pc <= pc + 1;
end
4'h4: // IF
begin
case (opland)
8'h00: // JEQ
begin
if (q0 == 8'd0)
pc <= adr;
else
pc <= pc + 1;
q0 <= q1; q1 <= q2; q2 <= q3; q3 <= 8'd0;
end
8'h01: // JNE
begin
if (q0 == 8'd1)
pc <= adr;
else
pc <= pc + 1;
q0 <= q1; q1 <= q2; q2 <= q3; q3 <= 8'd0;
end
8'hFF: // JMP
pc <= adr;
default:
pc <= pc + 1;
endcase
end
4'h6: // emit
begin
case (state)
4'h0:
begin
out <= q0;
sck_counter <= 4'd0;
state <= 4'h1;
end
4'h1:
begin
if (sck_edge)
begin
sck_counter <= sck_counter + 4'd1;
end
if (sck_counter >= 4'd10)
begin
q0 <= q1; q1 <= q2; q2 <= q3; q3 <= 8'd0;
pc <= pc + 1;
state <= 4'h0;
end
end
default:
state <= 4'h0;
endcase
end
4'hF:
begin
case (opland)
8'h00: begin q0 <= q1 + q0; q1 <= q2; q2 <= q3; q3 <= 8'd0; end // +
8'h01: begin q0 <= q1 - q0; q1 <= q2; q2 <= q3; q3 <= 8'd0; end // -
8'h02: begin q0 <= q1 * q0; q1 <= q2; q2 <= q3; q3 <= 8'd0; end // *
8'h03: begin q0 <= (q0 != 0) ? q1 / q0 : 8'd0; q1 <= q2; q2 <= q3; q3 <= 8'd0; end // /
8'h04: begin q0 <= (q0 != 0) ? q1 % q0 : 8'd0; q1 <= q2; q2 <= q3; q3 <= 8'd0; end // mod
8'h0F: begin q0 <= (q1 == q0) ? 8'd1 : 8'd0; q1 <= q2; q2 <= q3; q3 <= 8'd0; end // =
8'h10: begin q0 <= (q1 < q0) ? 8'd1 : 8'd0; q1 <= q2; q2 <= q3; q3 <= 8'd0; end // <
8'h11: begin q0 <= (q1 > q0) ? 8'd1 : 8'd0; q1 <= q2; q2 <= q3; q3 <= 8'd0; end // >
8'h20: begin q3 <= q2; q2 <= q1; q1 <= q0; end // dup
8'h21: begin q0 <= q1; q1 <= q2; q2 <= q3; q3 <= 8'd0; end // drop
default: ;
endcase
pc <= pc + 1;
end
default:
pc <= pc + 1;
endcase
end
2'd0:
begin
out <= 8'd0;
led <= ~led;
end
2'd2:
begin
out <= 8'h55;
led <= 0;
end
default:
led <= 0;
endcase
end
end
always @(*)
begin
case (pc)
7'd0: code = 12'h2_55; // PUSH 'U'
7'd1: code = 12'h6_00; // emit
7'd2: code = 12'h2_55; // PUSH 'U'
7'd3: code = 12'h6_00; // emit
7'd4: code = 12'h2_55; // PUSH 'U'
7'd5: code = 12'h6_00; // emit
7'd6: code = 12'h2_0D; // PUSH 0D (\r)
7'd7: code = 12'h6_00; // emit
7'd8: code = 12'h2_0A; // PUSH 0A (\n)
7'd9: code = 12'h6_00; // emit
7'd10: code = 12'h0_00; // halt
default: code = 12'h0_00;
endcase
end
endmodule
Resource Report
#### Resource Utilization Report ####
Top level module name: top
Resource usage:
CLB LUT5s: 129/1120 (11.52%)
Lut5: 129 (11.52%)
Lut5 for Shift-register: 0 (0.00%)
Lut5 for Distributed memory: 0 (0.00%)
FFs: 83
CLB FFs: 78/1120 (6.96%)
IOB FFs: 5/736 (0.68%)
CLBs: 26/140 (18.57%)
Tiles: 1/1 (100.00%)
RTL Input ports: 5
RTL Output ports: 5
GPIOs: 6/19 (31.58%)
PLLs: 0/1 (0.00%)
OSCs: 1/1 (100.00%)
4k BRAMs: 0/8 (0.00%)
1 Clock
Clock 0: net 10, clk, Fanout: 30 CLBs
Total 1 unique clocks
#### Resource Utilization Complete ####
TILE-REQ: 0 0 0 1
### Total FPGA resources from current floorplan: ###
184 IOBs
140 Logic 6-LUT CLBs
40 CLBs support RAM/SR
Usage of Logic CLBs is 18.6%.
Logic-Memory CLB for RAM/Shift-Register is 0.0%.
Usage of LUTs is 13.9%.
dual-LUTs is 7.7%.
Usage of FF is 10.0%.
dual-FF is 3.9%.
Usage of IO CLB is 3.8%.
main.py
micropythonから、SPIで操作。
from machine import Pin, SPI
import time
import shrike
shrike.reset()
shrike.flash("cpu0.bin")
def rst():
reset_pin = Pin(14, Pin.OUT, value = 1)
reset_pin.value(0)
time.sleep(1)
reset_pin.value(1)
time.sleep(1)
print("reset")
MISO = 0
CS = 1
SCK = 2
MOSI = 3
rst()
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))
cs.value(1)
def send(value):
tx = bytes([value])
rx = bytearray(1)
cs.value(0)
spi.write_readinto(tx, rx)
cs.value(1)
print(rx[0])
def send_command(cmd_byte, read_length = 0, is_string = False):
cs.value(0)
time.sleep_us(5)
spi.write(bytes([cmd_byte]))
time.sleep_us(10)
result = bytearray()
if is_string:
tx_dummy = bytes([0x00])
rx_char = bytearray(1)
while True:
spi.write_readinto(tx_dummy, rx_char)
char_byte = rx_char[0]
if char_byte == 0x0A or char_byte == 0x00:
break
result.append(char_byte)
else:
if read_length > 0:
tx_dummy = bytes([0x00] * read_length)
result = bytearray(read_length)
spi.write_readinto(tx_dummy, result)
cs.value(1)
return result
def test1():
print("Command 1 send...")
text_res = send_command(cmd_byte = 1, is_string = True)
print("string: ", text_res.decode('utf-8'))
def test2():
print("Command 2 send...")
bin_res = send_command(cmd_byte = 2, read_length = 5, is_string = False)
print("binary: ", [hex(b) for b in bin_res])
実行結果
>>> test1()
Command 1 send...
string: UUUU
rst()
>>> rst()
reset
>>> test2()
Command 2 send...
binary: ['0x55', '0x55', '0x55', '0x55', '0x55']
以上。
